From d9da651ccc18f506427d29ec23b39a464945b5aa Mon Sep 17 00:00:00 2001 From: Sujith H Date: Thu, 29 Nov 2018 22:29:50 +0530 Subject: [PATCH] [stable10] Backport of Add email validation to core utility This would help us to re-use the email validation in places where required, by just calling, OC.validateEmail(). In this change, email validation for public link is using the new method. Signed-off-by: Sujith H --- core/js/js.js | 15 ++++++++++++ core/js/sharedialogmailview.js | 2 +- core/js/tests/specs/coreSpec.js | 23 +++++++++++++++++++ .../js/tests/specs/sharedialogmailviewSpec.js | 15 ++++++------ 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/core/js/js.js b/core/js/js.js index c57c356c8b94..efe5fee32ef0 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -427,6 +427,21 @@ var OC = { return path.replace(/\\/g, '/').replace(/.*\//, ''); }, + /** + * Returns true if the email regexp matches the email address else false returned + * For example if email is "abc@foo.com", it will return true. + * If email address is "abc@foo.c", then false will be returned. + * + * @param emailAddress + * @returns {boolean} + * + * @since 10.1.0 + */ + validateEmail: function(emailAddress) { + var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@\.]{2,}$/; + return (emailRegex.exec(emailAddress) !== null); + }, + /** * Returns the dir name of the given path. * For example for "/abc/somefile.txt" it will return "/abc" diff --git a/core/js/sharedialogmailview.js b/core/js/sharedialogmailview.js index 60c2412ccac6..dc9fdf87d7bd 100644 --- a/core/js/sharedialogmailview.js +++ b/core/js/sharedialogmailview.js @@ -137,7 +137,7 @@ if (email.length === 0) return true - return email.match(/^[A-Za-z0-9\._%+-]+@(?:[A-Za-z0-9-]+\.)+[a-z]{2,}$/); + return OC.validateEmail(email); }, sendEmails: function() { diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index 2e5486160765..965f21e147ea 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -35,6 +35,29 @@ describe('Core base tests', function() { expect(OC.appswebroots).toBeDefined(); }); }); + describe('validateEmail', function () { + it('Returns false for search abc@foo', function () { + expect(OC.validateEmail('abc@foo')).toEqual(false); + }); + it('Returns false for search abc@foo.', function () { + expect(OC.validateEmail('abc@foo.')).toEqual(false); + }); + it('Returns false for search abc', function () { + expect(OC.validateEmail('abc')).toEqual(false); + }); + it('Returns false for search abc@foo.a', function () { + expect(OC.validateEmail('abc@foo.a')).toEqual(false); + }); + it('Returns true for search abc@foo.aa', function () { + expect(OC.validateEmail('abc@foo.aa')).toEqual(true); + }); + it('Returns true for search abc@f.aaa', function () { + expect(OC.validateEmail('abc@f.aaa')).toEqual(true); + }); + it('Returns true for search müller@Émile.诶西艾弗.буки', function () { + expect(OC.validateEmail('müller@Émile.诶西艾弗.буки')).toEqual(true); + }); + }); describe('basename', function() { it('Returns the nothing if no file name given', function() { expect(OC.basename('')).toEqual(''); diff --git a/core/js/tests/specs/sharedialogmailviewSpec.js b/core/js/tests/specs/sharedialogmailviewSpec.js index c3e6ccc6b107..6b241e48f4e1 100644 --- a/core/js/tests/specs/sharedialogmailviewSpec.js +++ b/core/js/tests/specs/sharedialogmailviewSpec.js @@ -102,14 +102,13 @@ describe('OC.Share.ShareDialogMailView', function() { describe('validating addresses', function() { it('works as expected', function() { - expect(view.validateEmail('Ada.Wong@umbrella.com')[0]).toEqual('Ada.Wong@umbrella.com'); - expect(view.validateEmail('Albert.Wesker@umbrella.sub-domain.com')[0]).toEqual('Albert.Wesker@umbrella.sub-domain.com'); - expect(view.validateEmail('Albert_Wesker@umbrella.sub-domain.com')[0]).toEqual('Albert_Wesker@umbrella.sub-domain.com'); - expect(view.validateEmail('Albert-Wesker@umbrella-new.sub-domain.com')[0]).toEqual('Albert-Wesker@umbrella-new.sub-domain.com'); - - expect(view.validateEmail('Jill.Valentine@umbrella..com')).toEqual(null); - expect(view.validateEmail('Jill.Valentine@um#rella.com')).toEqual(null); - expect(view.validateEmail('Jürgen.Sörensen@umbrella.com')).toEqual(null); + expect(view.validateEmail('Ada.Wong@umbrella.com')).toEqual(true); + expect(view.validateEmail('Albert.Wesker@umbrella.sub-domain.com')).toEqual(true); + expect(view.validateEmail('Albert_Wesker@umbrella.sub-domain.com')).toEqual(true); + expect(view.validateEmail('Albert-Wesker@umbrella-new.sub-domain.com')).toEqual(true); + expect(view.validateEmail('Jill.Valentine@um#rella.com')).toEqual(true); + + expect(view.validateEmail('Jill.Valentine@umbrella..c')).toEqual(false); }); });