2020-05-19 12:49:06 +02:00
import { TestBed } from '@angular/core/testing' ;
import { ValidationService } from './validation.service' ;
2020-05-22 09:36:50 +02:00
let validationService : ValidationService ;
2020-05-19 12:49:06 +02:00
describe ( 'ValidationService' , ( ) = > {
2020-05-22 09:36:50 +02:00
beforeEach ( ( ) = > {
TestBed . configureTestingModule ( {
providers : [ ValidationService ]
} ) ;
validationService = TestBed . inject ( ValidationService ) ;
} ) ;
2020-05-19 12:49:06 +02:00
it ( 'should be created' , ( ) = > {
2020-05-22 09:36:50 +02:00
expect ( validationService ) . toBeTruthy ( ) ;
2020-05-19 12:49:06 +02:00
} ) ;
2020-05-22 09:36:50 +02:00
it ( 'should return true on a correct username' , ( ) = > {
expect ( validationService . username ( 'abc' ) ) . toEqual ( { ok : true , error : '' } ) ;
} ) ;
it ( 'should return an error on an incorrect username' , ( ) = > {
expect ( validationService . username ( 'abc#' ) ) . toEqual ( { ok : false , error : 'username must only contain a-z0-9-_.' } ) ;
} ) ;
it ( 'should return true on a correct password' , ( ) = > {
expect ( validationService . password ( 'Abc123!#' ) ) . toEqual ( { ok : true , error : '' } ) ;
} ) ;
2020-06-22 10:22:45 +02:00
it ( 'should return an error on a password too short' , ( ) = > {
expect ( validationService . password ( 'Abc123' ) ) . toEqual ( { ok : false , error : 'password must have at least 8 characters' } ) ;
2020-05-22 09:36:50 +02:00
} ) ;
2020-06-22 10:22:45 +02:00
it ( 'should return an error on a password without a lowercase letter' , ( ) = > {
expect ( validationService . password ( 'ABC123!#' ) ) . toEqual ( { ok : false , error : 'password must have at least one lowercase character' } ) ;
} ) ;
it ( 'should return an error on a password without an uppercase letter' , ( ) = > {
expect ( validationService . password ( 'abc123!#' ) ) . toEqual ( { ok : false , error : 'password must have at least one uppercase character' } ) ;
} ) ;
it ( 'should return an error on a password without a number' , ( ) = > {
expect ( validationService . password ( 'Abcabc!#' ) ) . toEqual ( { ok : false , error : 'password must have at least one number' } ) ;
} ) ;
it ( 'should return an error on a password without a special character' , ( ) = > {
expect ( validationService . password ( 'Abc12345' ) ) . toEqual ( { ok : false , error : 'password must have at least one of the following characters !"#%&\'()*+,-.\\/:;<=>?@[]^_`{|}~' } ) ;
} ) ;
it ( 'should return an error on a password with a character not allowed' , ( ) = > {
expect ( validationService . password ( 'Abc123!€' ) ) . toEqual ( { ok : false , error : 'password must only contain a-zA-Z0-9!"#%&\'()*+,-./:;<=>?@[]^_`{|}~' } ) ;
} ) ;
it ( 'should return true on a correct string' , ( ) = > {
expect ( validationService . string ( 'Abc' ) ) . toEqual ( { ok : true , error : '' } ) ;
} ) ;
it ( 'should return an error on a string too long' , ( ) = > {
expect ( validationService . string ( 'abcabcabcbabcbabcabcabacbabcabcabcbabcbabcabcabacbabcabcabcbabcbabcabcabacbabcabcabcbabcbabcabcabacbabcabcabcbabcbabcabcabacbacab' ) ) . toEqual ( { ok : false , error : 'must contain max 128 characters' } ) ;
} ) ;
it ( 'should return true on a string in the list' , ( ) = > {
expect ( validationService . stringOf ( 'Abc' , [ 'Abc' , 'Def' ] ) ) . toEqual ( { ok : true , error : '' } ) ;
} ) ;
it ( 'should return an error on a string not in the list' , ( ) = > {
expect ( validationService . stringOf ( 'abc' , [ 'Abc' , 'Def' ] ) ) . toEqual ( { ok : false , error : 'must be one of Abc, Def' } ) ;
} ) ;
it ( 'should return true on a string of correct length' , ( ) = > {
expect ( validationService . stringLength ( 'Abc' , 5 ) ) . toEqual ( { ok : true , error : '' } ) ;
} ) ;
it ( 'should return an error on a string longer than specified' , ( ) = > {
expect ( validationService . stringLength ( 'Abc' , 2 ) ) . toEqual ( { ok : false , error : 'must contain max 2 characters' } ) ;
} ) ;
it ( 'should return true on a number in the range' , ( ) = > {
expect ( validationService . minMax ( 2 , - 2 , 2 ) ) . toEqual ( { ok : true , error : '' } ) ;
} ) ;
it ( 'should return an error on a number below the range' , ( ) = > {
expect ( validationService . minMax ( 0 , 1 , 3 ) ) . toEqual ( { ok : false , error : 'must be between 1 and 3' } ) ;
} ) ;
it ( 'should return an error on a number above the range' , ( ) = > {
expect ( validationService . minMax ( 3.1 , 1 , 3 ) ) . toEqual ( { ok : false , error : 'must be between 1 and 3' } ) ;
} ) ;
it ( 'should return true on a number above min' , ( ) = > {
expect ( validationService . min ( 2 , - 2 ) ) . toEqual ( { ok : true , error : '' } ) ;
} ) ;
it ( 'should return an error on a number below min' , ( ) = > {
expect ( validationService . min ( 0 , 1 ) ) . toEqual ( { ok : false , error : 'must not be below 1' } ) ;
} ) ;
it ( 'should return true on a number below max' , ( ) = > {
expect ( validationService . max ( 2 , 2 ) ) . toEqual ( { ok : true , error : '' } ) ;
} ) ;
it ( 'should return an error on a number above max' , ( ) = > {
expect ( validationService . max ( 2 , 1 ) ) . toEqual ( { ok : false , error : 'must not be above 1' } ) ;
} ) ;
it ( 'should return true on a string not in the list' , ( ) = > {
expect ( validationService . unique ( 'Abc' , [ 'Def' , 'Ghi' ] ) ) . toEqual ( { ok : true , error : '' } ) ;
} ) ;
it ( 'should return an error on a string from the list' , ( ) = > {
expect ( validationService . unique ( 'Abc' , [ 'Abc' , 'Def' ] ) ) . toEqual ( { ok : false , error : 'values must be unique' } ) ;
} ) ;
2020-05-19 12:49:06 +02:00
} ) ;