Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
fix: ensure that password has value before validating it and better r…
Browse files Browse the repository at this point in the history
…egex (#1927)
  • Loading branch information
faustbrian authored May 18, 2020
1 parent 7c77e12 commit 6fcf597
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
26 changes: 23 additions & 3 deletions __tests__/unit/components/Input/InputPassword.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,29 @@ describe('InputPassword', () => {
})
})

describe('when the password does not include a lowercase character', () => {
it('should provide feedback about it', () => {
mountData.propsData.value = 'A123'
mountData.propsData.minLength = 2
const wrapper = mount(InputPassword, mountData)

expect(wrapper.vm.passwordFeedback()).toEqual('VALIDATION.PASSWORD.LOWER_CASE')
})
})

describe('when the password does not include an uppercase character', () => {
it('should provide feedback about it', () => {
mountData.propsData.value = 'a123'
mountData.propsData.minLength = 2
const wrapper = mount(InputPassword, mountData)

expect(wrapper.vm.passwordFeedback()).toEqual('VALIDATION.PASSWORD.UPPER_CASE')
})
})

describe('when the password does not include a number', () => {
it('should provide feedback about it', () => {
mountData.propsData.value = 'aaaabbbb'
mountData.propsData.value = 'aB'
mountData.propsData.minLength = 2
const wrapper = mount(InputPassword, mountData)

Expand All @@ -87,7 +107,7 @@ describe('InputPassword', () => {

describe('when the password does not include a special character', () => {
it('should provide feedback about it', () => {
mountData.propsData.value = 'aaaa0000'
mountData.propsData.value = 'aB0'
mountData.propsData.minLength = 2
const wrapper = mount(InputPassword, mountData)

Expand All @@ -97,7 +117,7 @@ describe('InputPassword', () => {

describe('when the password follows all the constraints', () => {
it('should not provide feedback', () => {
const value = 'aaaa000+'
const value = 'aB1+'
mountData.propsData.value = value
mountData.propsData.minLength = value.length
const wrapper = mount(InputPassword, mountData)
Expand Down
41 changes: 30 additions & 11 deletions src/renderer/components/Input/InputPassword.vue
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,21 @@ export default {
if (this.minLength && this.model.length < this.minLength) {
return this.$t('VALIDATION.PASSWORD.TOO_SHORT', [this.minLength])
} else if (!this.model.match(/[0-9]+/)) {
}
if (!this.model.match(/[a-z]/)) {
return this.$t('VALIDATION.PASSWORD.LOWER_CASE')
}
if (!this.model.match(/[A-Z]/)) {
return this.$t('VALIDATION.PASSWORD.UPPER_CASE')
}
if (!this.model.match(/[0-9]/)) {
return this.$t('VALIDATION.PASSWORD.NUMBERS')
} else if (!this.model.match(/[^a-zA-Z0-9]+/)) {
}
if (!this.model.match(/\W|_/)) {
return this.$t('VALIDATION.PASSWORD.SPECIAL_CHARACTERS')
}
Expand All @@ -217,22 +229,29 @@ export default {
return true
},
isValid () {
if (!this.isRequired && !this.model.length) {
isValid (value) {
if (!value) {
return false
}
if (!this.isRequired && !value.length) {
return true
} else if (!this.isCreate) {
}
if (!this.isCreate) {
return true
}
if (this.minLength && this.model.length < this.minLength) {
return false
} else if (!this.model.match(/[0-9]+/)) {
return false
} else if (!this.model.match(/[^a-zA-Z0-9]+/)) {
if (this.minLength && value.length < this.minLength) {
return false
}
return true
const containsLowercase = /[a-z]/.test(value)
const containsUppercase = /[A-Z]/.test(value)
const containsNumbers = /[0-9]/.test(value)
const containsSpecial = /\W|_/.test(value)
return containsLowercase && containsUppercase && containsNumbers && containsSpecial
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/i18n/locales/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ export default {
},
PASSWORD: {
TOO_SHORT: 'Your password must be at least {0} characters long',
LOWER_CASE: 'Your password must contain at least 1 lowercase character',
UPPER_CASE: 'Your password must contain at least 1 uppercase character',
NUMBERS: 'Your password must contain at least 1 number',
SPECIAL_CHARACTERS: 'Your password must contain at least 1 special character',
NO_MATCH: 'Your passwords do not match'
Expand Down

0 comments on commit 6fcf597

Please sign in to comment.