diff --git a/src/validators/validateEmail/validateEmail.js b/src/validators/validateEmail/validateEmail.js index 66e379143..49b296bf9 100644 --- a/src/validators/validateEmail/validateEmail.js +++ b/src/validators/validateEmail/validateEmail.js @@ -1,3 +1,11 @@ +/** + * validate an email address + * Email address validation is notoriously difficult. I don't know where this + * particular regex came from but it shares substantially with well-known + * versions such as https://emailregex.com/ and does a good enough job for us. + * + * This was introduced in STCOR-276/PR #496 + */ export default email => { // eslint-disable-next-line no-useless-escape const emailRegExp = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; diff --git a/src/validators/validateEmail/validateEmail.test.js b/src/validators/validateEmail/validateEmail.test.js new file mode 100644 index 000000000..8ace387c6 --- /dev/null +++ b/src/validators/validateEmail/validateEmail.test.js @@ -0,0 +1,23 @@ +import validateEmail from './validateEmail'; + +describe('validateEmail', () => { + it('accepts valid email addresses', () => { + expect(validateEmail('test@example.edu')).toBe(true); + }); + + it('accepts values with comments in username', () => { + expect(validateEmail('test+comment@example.edu')).toBe(true); + }); + + it('rejects values without @', () => { + expect(validateEmail('test$example.edu')).toBe(false); + }); + + it('rejects values without domain suffix', () => { + expect(validateEmail('test@example')).toBe(false); + }); + + it('rejects values with whitespace', () => { + expect(validateEmail(' test@example.com ')).toBe(false); + }); +}); diff --git a/src/validators/validateForgotUsernameForm/validateForgotUsernameForm.test.js b/src/validators/validateForgotUsernameForm/validateForgotUsernameForm.test.js new file mode 100644 index 000000000..d5563ddd0 --- /dev/null +++ b/src/validators/validateForgotUsernameForm/validateForgotUsernameForm.test.js @@ -0,0 +1,36 @@ +import validateForgotUsernameForm from './validateForgotUsernameForm'; + +describe('validateForgotUsernameForm', () => { + describe('accepts valid email addresses and phone numbers', () => { + it('accepts valid email addresses', () => { + expect(validateForgotUsernameForm('test@example.edu')).toBe(true); + }); + + it('accepts values with comments in username', () => { + expect(validateForgotUsernameForm('test+comment@example.edu')).toBe(true); + }); + + it('accepts values with numbers only', () => { + expect(validateForgotUsernameForm('12')).toBe(true); + }); + + it('accepts values with numbers and dashes', () => { + expect(validateForgotUsernameForm('1-2')).toBe(true); + }); + + it('accepts values with numbers and dots', () => { + expect(validateForgotUsernameForm('1.2')).toBe(true); + }); + }); + + it('rejects values that are not valid email addresses or phone numbers', () => { + // validateForgotUsernameForm trims whitespace so we don't test for + // whitespace rejection here, even though other validators do. + expect(validateForgotUsernameForm('not an email address')).toBe(false); + expect(validateForgotUsernameForm('not a phone number')).toBe(false); + expect(validateForgotUsernameForm('test$example.edu')).toBe(false); + expect(validateForgotUsernameForm('test@example')).toBe(false); + expect(validateForgotUsernameForm('1.2a')).toBe(false); + expect(validateForgotUsernameForm('1.2!')).toBe(false); + }); +}); diff --git a/src/validators/validatePhoneNumber/validatePhoneNumber.test.js b/src/validators/validatePhoneNumber/validatePhoneNumber.test.js new file mode 100644 index 000000000..7f9eebe45 --- /dev/null +++ b/src/validators/validatePhoneNumber/validatePhoneNumber.test.js @@ -0,0 +1,22 @@ +import validatePhoneNumber from './validatePhoneNumber'; + +describe('validatePhoneNumber', () => { + it('accepts values with numbers only', () => { + expect(validatePhoneNumber('12')).toBe(true); + }); + + it('accepts values with numbers and dashes', () => { + expect(validatePhoneNumber('1-2')).toBe(true); + }); + + it('accepts values with numbers and dots', () => { + expect(validatePhoneNumber('1.2')).toBe(true); + }); + + it('rejects values containing characters other than numbers, dashes, and dots', () => { + expect(validatePhoneNumber('1.2 ')).toBe(false); + expect(validatePhoneNumber('1.2a')).toBe(false); + expect(validatePhoneNumber('1.2!')).toBe(false); + }); +}); +