mirror of https://github.com/abpframework/abp.git
3 changed files with 98 additions and 0 deletions
@ -0,0 +1,52 @@ |
|||
import { ConfigState } from '@abp/ng.core'; |
|||
import { Component } from '@angular/core'; |
|||
import { createComponentFactory, Spectator } from '@ngneat/spectator'; |
|||
import { NgxValidateCoreModule, validatePassword } from '@ngx-validate/core'; |
|||
import { NgxsModule, Store } from '@ngxs/store'; |
|||
import { HttpClient } from '@angular/common/http'; |
|||
import { getPasswordValidators } from '../utils'; |
|||
import { Validators } from '@angular/forms'; |
|||
|
|||
@Component({ template: '', selector: 'abp-dummy' }) |
|||
class DummyComponent {} |
|||
|
|||
describe('ValidationUtils', () => { |
|||
let spectator: Spectator<DummyComponent>; |
|||
const createComponent = createComponentFactory({ |
|||
component: DummyComponent, |
|||
imports: [NgxsModule.forRoot([ConfigState]), NgxValidateCoreModule.forRoot()], |
|||
mocks: [HttpClient], |
|||
}); |
|||
|
|||
beforeEach(() => (spectator = createComponent())); |
|||
|
|||
describe('#getPasswordValidators', () => { |
|||
it('should return password valdiators', () => { |
|||
const store = spectator.get(Store); |
|||
store.reset({ |
|||
ConfigState: { |
|||
setting: { |
|||
values: { |
|||
'Abp.Identity.Password.RequiredLength': '6', |
|||
'Abp.Identity.Password.RequiredUniqueChars': '1', |
|||
'Abp.Identity.Password.RequireNonAlphanumeric': 'True', |
|||
'Abp.Identity.Password.RequireLowercase': 'True', |
|||
'Abp.Identity.Password.RequireUppercase': 'True', |
|||
'Abp.Identity.Password.RequireDigit': 'True', |
|||
}, |
|||
}, |
|||
}, |
|||
}); |
|||
const validators = getPasswordValidators(store); |
|||
const expectedValidators = [ |
|||
validatePassword(['number', 'small', 'capital', 'special']), |
|||
Validators.minLength(6), |
|||
Validators.maxLength(128), |
|||
]; |
|||
|
|||
validators.forEach((validator, index) => { |
|||
expect(validator.toString()).toBe(expectedValidators[index].toString()); |
|||
}); |
|||
}); |
|||
}); |
|||
}); |
|||
@ -1,2 +1,3 @@ |
|||
export * from './widget-utils'; |
|||
export * from './date-parser-formatter'; |
|||
export * from './validation-utils'; |
|||
|
|||
@ -0,0 +1,45 @@ |
|||
import { Store } from '@ngxs/store'; |
|||
import { ABP, ConfigState } from '@abp/ng.core'; |
|||
import { PasswordRules, validatePassword } from '@ngx-validate/core'; |
|||
import { Validators } from '@angular/forms'; |
|||
|
|||
const { minLength, maxLength } = Validators; |
|||
|
|||
export function getPasswordValidators(store: Store) { |
|||
const getRule = getRuleFn(store); |
|||
|
|||
const passwordRulesArr = [] as PasswordRules; |
|||
let requiredLength = 1; |
|||
|
|||
if (getRule('RequireDigit') === 'true') { |
|||
passwordRulesArr.push('number'); |
|||
} |
|||
|
|||
if (getRule('RequireLowercase') === 'true') { |
|||
passwordRulesArr.push('small'); |
|||
} |
|||
|
|||
if (getRule('RequireUppercase') === 'true') { |
|||
passwordRulesArr.push('capital'); |
|||
} |
|||
|
|||
if (getRule('RequireNonAlphanumeric') === 'true') { |
|||
passwordRulesArr.push('special'); |
|||
} |
|||
|
|||
if (Number.isInteger(+getRule('RequiredLength'))) { |
|||
requiredLength = +getRule('RequiredLength'); |
|||
} |
|||
|
|||
return [validatePassword(passwordRulesArr), minLength(requiredLength), maxLength(128)]; |
|||
} |
|||
|
|||
function getRuleFn(store: Store) { |
|||
return (key: string) => { |
|||
const passwordRules: ABP.Dictionary<string> = store.selectSnapshot( |
|||
ConfigState.getSettings('Identity.Password'), |
|||
); |
|||
|
|||
return (passwordRules[`Abp.Identity.Password.${key}`] || '').toLowerCase(); |
|||
}; |
|||
} |
|||
Loading…
Reference in new issue