2019-07-22 14:54:25 -04:00
|
|
|
const validate = require('../../../src/lib/validate');
|
|
|
|
|
|
|
|
describe('unit test lib/validate.js', () => {
|
2019-10-30 15:16:15 -04:00
|
|
|
|
|
|
|
test('validate username exists locally', () => {
|
2019-07-22 14:54:25 -04:00
|
|
|
let response;
|
|
|
|
expect(typeof validate.validateUsernameLocally).toBe('function');
|
|
|
|
response = validate.validateUsernameLocally('abc');
|
|
|
|
expect(response).toEqual({valid: true});
|
|
|
|
response = validate.validateUsernameLocally('');
|
2019-08-11 16:13:07 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'general.required'});
|
2019-10-30 15:16:15 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('validate username length locally', () => {
|
|
|
|
let response;
|
|
|
|
response = validate.validateUsernameLocally('abcdefghijklmnopqrst');
|
|
|
|
expect(response).toEqual({valid: true});
|
2019-07-22 14:54:25 -04:00
|
|
|
response = validate.validateUsernameLocally('ab');
|
2019-08-11 17:18:08 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationUsernameMinLength'});
|
2019-07-22 14:54:25 -04:00
|
|
|
response = validate.validateUsernameLocally('abcdefghijklmnopqrstu');
|
2019-08-11 17:18:08 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationUsernameMaxLength'});
|
2019-10-30 15:16:15 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('validate username hyphens allowed', () => {
|
|
|
|
const response = validate.validateUsernameLocally('-abc-def-ghi-');
|
|
|
|
expect(response).toEqual({valid: true});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('validate username underscores allowed', () => {
|
|
|
|
const response = validate.validateUsernameLocally('_abc_def_ghi_');
|
|
|
|
expect(response).toEqual({valid: true});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('validate username spaces not allowed', () => {
|
|
|
|
const response = validate.validateUsernameLocally('abc def');
|
2019-11-19 16:25:21 -05:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationUsernameSpaces'});
|
2019-10-30 15:16:15 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('validate username special chars not allowed', () => {
|
|
|
|
let response;
|
2019-07-22 14:54:25 -04:00
|
|
|
response = validate.validateUsernameLocally('abc!def');
|
2019-08-11 17:18:08 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationUsernameRegexp'});
|
2019-10-30 15:16:15 -04:00
|
|
|
response = validate.validateUsernameLocally('amiascratcher?');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationUsernameRegexp'});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('validate username unicode chars not allowed', () => {
|
|
|
|
let response;
|
2019-07-22 14:54:25 -04:00
|
|
|
response = validate.validateUsernameLocally('abc😄def');
|
2019-08-11 17:18:08 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationUsernameRegexp'});
|
2019-10-30 15:16:15 -04:00
|
|
|
response = validate.validateUsernameLocally('🦆🦆🦆😺😺😺');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationUsernameRegexp'});
|
2019-07-22 14:54:25 -04:00
|
|
|
});
|
|
|
|
|
2019-10-30 15:09:25 -04:00
|
|
|
test('validate password existence', () => {
|
2019-07-22 14:54:25 -04:00
|
|
|
let response;
|
|
|
|
expect(typeof validate.validatePassword).toBe('function');
|
2022-09-22 00:23:59 -04:00
|
|
|
response = validate.validatePasswordLocally('abcdef');
|
2019-07-22 14:54:25 -04:00
|
|
|
expect(response).toEqual({valid: true});
|
2022-09-22 00:23:59 -04:00
|
|
|
response = validate.validatePasswordLocally('');
|
2019-08-11 16:13:07 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'general.required'});
|
2019-10-30 15:09:25 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('validate password length', () => {
|
|
|
|
let response;
|
2022-09-22 00:23:59 -04:00
|
|
|
response = validate.validatePasswordLocally('abcdefghijklmnopqrst');
|
2019-10-30 15:09:25 -04:00
|
|
|
expect(response).toEqual({valid: true});
|
2022-09-22 00:23:59 -04:00
|
|
|
response = validate.validatePasswordLocally('abcde');
|
2019-07-22 14:54:25 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordLength'});
|
2022-09-22 00:23:59 -04:00
|
|
|
response = validate.validatePasswordLocally('😺');
|
2019-10-29 13:48:05 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordLength'});
|
2022-09-22 00:23:59 -04:00
|
|
|
response = validate.validatePasswordLocally('😺🦆🐝');
|
2019-10-29 13:48:05 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordLength'});
|
2022-09-22 00:23:59 -04:00
|
|
|
response = validate.validatePasswordLocally('😺🦆🐝🐮🐠');
|
2019-10-29 13:48:05 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordLength'});
|
2022-09-22 00:23:59 -04:00
|
|
|
response = validate.validatePasswordLocally('😺🦆🐝🐮🐠🐻');
|
2019-10-29 13:48:05 -04:00
|
|
|
expect(response).toEqual({valid: true});
|
2019-10-30 15:09:25 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('validate password cannot be "password"', () => {
|
2022-09-22 00:23:59 -04:00
|
|
|
const response = validate.validatePasswordLocally('password');
|
2019-07-22 14:54:25 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordNotEquals'});
|
2019-10-30 15:09:25 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('validate password cannot be same as username', () => {
|
|
|
|
let response;
|
2022-09-22 00:23:59 -04:00
|
|
|
response = validate.validatePasswordLocally('abcdefg', 'abcdefg');
|
2019-08-12 14:11:40 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordNotUsername'});
|
2022-09-22 00:23:59 -04:00
|
|
|
response = validate.validatePasswordLocally('abcdefg', 'abcdefG');
|
2019-08-12 14:11:40 -04:00
|
|
|
expect(response).toEqual({valid: true});
|
2019-07-22 14:54:25 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('validate password confirm', () => {
|
|
|
|
let response;
|
|
|
|
expect(typeof validate.validatePasswordConfirm).toBe('function');
|
|
|
|
response = validate.validatePasswordConfirm('abcdef', 'abcdef');
|
|
|
|
expect(response).toEqual({valid: true});
|
|
|
|
response = validate.validatePasswordConfirm('abcdefghijklmnopqrst', 'abcdefghijklmnopqrst');
|
|
|
|
expect(response).toEqual({valid: true});
|
|
|
|
response = validate.validatePasswordConfirm('passwo', 'passwo');
|
|
|
|
expect(response).toEqual({valid: true});
|
|
|
|
response = validate.validatePasswordConfirm('', '');
|
2019-08-11 16:13:07 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'general.required'});
|
2019-07-22 14:54:25 -04:00
|
|
|
response = validate.validatePasswordConfirm('abcdef', 'abcdefg');
|
2019-08-11 17:18:08 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordConfirmNotEquals'});
|
2019-07-22 14:54:25 -04:00
|
|
|
response = validate.validatePasswordConfirm('abcdef', '123456');
|
2019-08-11 17:18:08 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordConfirmNotEquals'});
|
2019-07-22 14:54:25 -04:00
|
|
|
response = validate.validatePasswordConfirm('', 'abcdefg');
|
2019-08-11 17:18:08 -04:00
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordConfirmNotEquals'});
|
2019-07-22 14:54:25 -04:00
|
|
|
});
|
2019-08-27 12:18:53 -04:00
|
|
|
|
|
|
|
test('validate email address locally', () => {
|
|
|
|
let response;
|
|
|
|
expect(typeof validate.validateEmailLocally).toBe('function');
|
|
|
|
|
|
|
|
// permitted addresses:
|
|
|
|
response = validate.validateEmailLocally('abc@def.com');
|
|
|
|
expect(response).toEqual({valid: true});
|
|
|
|
response = validate.validateEmailLocally('abcdefghijklmnopqrst@abcdefghijklmnopqrst.info');
|
|
|
|
expect(response).toEqual({valid: true});
|
|
|
|
response = validate.validateEmailLocally('abc-def-ghi@jkl-mno.org');
|
|
|
|
expect(response).toEqual({valid: true});
|
|
|
|
response = validate.validateEmailLocally('_______@example.com');
|
|
|
|
expect(response).toEqual({valid: true});
|
|
|
|
response = validate.validateEmailLocally('email@example.museum');
|
|
|
|
expect(response).toEqual({valid: true});
|
|
|
|
response = validate.validateEmailLocally('email@example.co.jp');
|
|
|
|
expect(response).toEqual({valid: true});
|
|
|
|
|
|
|
|
// non-permitted addresses:
|
|
|
|
response = validate.validateEmailLocally('');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'general.required'});
|
|
|
|
response = validate.validateEmailLocally('a');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationEmailInvalid'});
|
|
|
|
response = validate.validateEmailLocally('abc@def');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationEmailInvalid'});
|
|
|
|
response = validate.validateEmailLocally('abc@def.c');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationEmailInvalid'});
|
|
|
|
response = validate.validateEmailLocally('abc😄def@emoji.pizza');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationEmailInvalid'});
|
|
|
|
response = validate.validateEmailLocally('あいうえお@example.com');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationEmailInvalid'});
|
|
|
|
response = validate.validateEmailLocally('Abc..123@example.com');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationEmailInvalid'});
|
|
|
|
response = validate.validateEmailLocally('Joe Smith <email@example.com>');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationEmailInvalid'});
|
|
|
|
response = validate.validateEmailLocally('email@example@example.com');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationEmailInvalid'});
|
|
|
|
response = validate.validateEmailLocally('email@example..com');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationEmailInvalid'});
|
|
|
|
|
|
|
|
// edge cases:
|
|
|
|
// these are strictly legal according to email addres spec, but rejected by library we use:
|
|
|
|
response = validate.validateEmailLocally('email@123.123.123.123');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationEmailInvalid'});
|
|
|
|
response = validate.validateEmailLocally('much."more unusual"@example.com');
|
|
|
|
expect(response).toEqual({valid: false, errMsgId: 'registration.validationEmailInvalid'});
|
|
|
|
});
|
2019-10-23 05:19:42 -04:00
|
|
|
|
2019-11-01 17:33:33 -04:00
|
|
|
test('get responseErrorMsg in cases where there is a dedicated string for that case', () => {
|
2019-10-23 05:19:42 -04:00
|
|
|
let response = validate.responseErrorMsg('username', 'bad username');
|
|
|
|
expect(response).toEqual('registration.errorBadUsername');
|
|
|
|
response = validate.responseErrorMsg('password', 'Ensure this value has at least 6 characters (it has 3).');
|
|
|
|
expect(response).toEqual('registration.errorPasswordTooShort');
|
|
|
|
});
|
2019-11-01 17:33:33 -04:00
|
|
|
|
|
|
|
test('responseErrorMsg is null in case where there is no dedicated string for that case', () => {
|
|
|
|
let response = validate.responseErrorMsg('username', 'some error that is not covered');
|
|
|
|
expect(response).toEqual(null);
|
|
|
|
});
|
2019-07-22 14:54:25 -04:00
|
|
|
});
|