diff --git a/src/lib/validate.js b/src/lib/validate.js index fa7377930..f4b4fe9fc 100644 --- a/src/lib/validate.js +++ b/src/lib/validate.js @@ -52,7 +52,10 @@ module.exports.validatePassword = password => { }; module.exports.validatePasswordConfirm = (password, passwordConfirm) => { - if (password !== passwordConfirm) { + if (!passwordConfirm) { + return {valid: false, errMsgId: 'form.validationRequired'}; + } else if (password !== passwordConfirm) { + // TODO: add a new string for this case return {valid: false, errMsgId: 'general.error'}; } return {valid: true}; diff --git a/test/unit/lib/validate.js b/test/unit/lib/validate.js index e35e6b53a..3209a4307 100644 --- a/test/unit/lib/validate.js +++ b/test/unit/lib/validate.js @@ -26,3 +26,41 @@ tap.test('validate username locally', t => { t.deepEqual(response, {valid: false, errMsgId: 'form.validationUsernameRegexp'}); t.end(); }); + +tap.test('validate password', t => { + let response; + t.type(validate.validatePassword, 'function'); + response = validate.validatePassword('abcdef'); + t.deepEqual(response, {valid: true}); + response = validate.validatePassword('abcdefghijklmnopqrst'); + t.deepEqual(response, {valid: true}); + response = validate.validatePassword('passwo'); + t.deepEqual(response, {valid: true}); + response = validate.validatePassword(''); + t.deepEqual(response, {valid: false, errMsgId: 'form.validationRequired'}); + response = validate.validatePassword('abcde'); + t.deepEqual(response, {valid: false, errMsgId: 'registration.validationPasswordLength'}); + response = validate.validatePassword('password'); + t.deepEqual(response, {valid: false, errMsgId: 'registration.validationPasswordNotEquals'}); + t.end(); +}); + +tap.test('validate password confirm', t => { + let response; + t.type(validate.validatePasswordConfirm, 'function'); + response = validate.validatePasswordConfirm('abcdef', 'abcdef'); + t.deepEqual(response, {valid: true}); + response = validate.validatePasswordConfirm('abcdefghijklmnopqrst', 'abcdefghijklmnopqrst'); + t.deepEqual(response, {valid: true}); + response = validate.validatePasswordConfirm('passwo', 'passwo'); + t.deepEqual(response, {valid: true}); + response = validate.validatePasswordConfirm('', ''); + t.deepEqual(response, {valid: false, errMsgId: 'form.validationRequired'}); + response = validate.validatePasswordConfirm('abcdef', 'abcdefg'); + t.deepEqual(response, {valid: false, errMsgId: 'general.error'}); + response = validate.validatePasswordConfirm('abcdef', '123456'); + t.deepEqual(response, {valid: false, errMsgId: 'general.error'}); + response = validate.validatePasswordConfirm('', 'abcdefg'); + t.deepEqual(response, {valid: false, errMsgId: 'general.error'}); + t.end(); +});