@@ -11,6 +11,8 @@ import { HttpCode } from '../../../src/util/errors';
1111
1212beforeAll ( async ( ) => {
1313 await createDBConnection ( ) ;
14+ const spy = jest . spyOn ( passwordUtils , 'hashPassword' ) ;
15+ spy . mockImplementation ( password => Promise . resolve ( password . split ( '' ) . reverse ( ) . join ( '' ) ) ) ;
1416} ) ;
1517
1618afterEach ( async ( ) => {
@@ -23,10 +25,6 @@ const userService = new UserService();
2325describe ( 'UserService' , ( ) => {
2426 describe ( 'registerUser' , ( ) => {
2527 test ( 'Registers a user with valid details' , async ( ) => {
26- // Mock
27- const spy = jest . spyOn ( passwordUtils , 'hashPassword' ) ;
28- spy . mockImplementation ( ( ) => Promise . resolve ( 'passwordhash' ) ) ;
29-
3028 const { email, forename, surname } = users [ 0 ] ;
3129 const details = { email, forename, surname, password : 'thunderbolt' } ;
3230 const confirmation = await userService . registerUser ( details ) ;
@@ -38,10 +36,9 @@ describe('UserService', () => {
3836 accountType : AccountType . User ,
3937 accountStatus : AccountStatus . Unverified
4038 } ) ;
41- expect ( confirmation . user . password ) . toStrictEqual ( 'passwordhash ' ) ;
39+ expect ( confirmation . user . password ) . toStrictEqual ( 'tlobrednuht ' ) ;
4240 // 2nd registration should fail
4341 await expect ( userService . registerUser ( details ) ) . rejects . toMatchObject ( { httpCode : HttpCode . BadRequest } ) ;
44- spy . mockReset ( ) ;
4542 } ) ;
4643
4744 test ( 'Fails with invalid email' , async ( ) => {
@@ -65,11 +62,33 @@ describe('UserService', () => {
6562 await expect ( getRepository ( User ) . findOneOrFail ( ) ) . rejects . toThrow ( ) ;
6663 } ) ;
6764
68- test ( 'Fails with very long forename/surname (50 chars)' , async ( ) => {
65+ test ( 'Fails with very long forename/surname (41 chars)' , async ( ) => {
66+ const { email, forename, surname } = users [ 0 ] ;
67+ await expect ( userService . registerUser ( { email, forename : 'f' . repeat ( 41 ) , surname, password : 'thunderbolt' } ) ) . rejects . toMatchObject ( { httpCode : HttpCode . BadRequest } ) ;
68+ await expect ( getRepository ( User ) . findOneOrFail ( ) ) . rejects . toThrow ( ) ;
69+ await expect ( userService . registerUser ( { email, forename, surname : 'h' . repeat ( 41 ) , password : 'thunderbolt' } ) ) . rejects . toMatchObject ( { httpCode : HttpCode . BadRequest } ) ;
70+ await expect ( getRepository ( User ) . findOneOrFail ( ) ) . rejects . toThrow ( ) ;
71+ } ) ;
72+
73+ test ( 'Success for valid forename/surname (20 chars)' , async ( ) => {
74+ const { email, surname } = users [ 0 ] ;
75+ const forename = 'f' . repeat ( 20 ) ;
76+ const details = { email, forename, surname, password : 'thunderbolt' } ;
77+ const confirmation = await userService . registerUser ( details ) ;
78+ expect ( confirmation . user ) . toMatchObject ( {
79+ email,
80+ forename,
81+ surname,
82+ accountType : AccountType . User ,
83+ accountStatus : AccountStatus . Unverified
84+ } ) ;
85+ } ) ;
86+
87+ test ( 'Fails with no forename/surname (0 chars)' , async ( ) => {
6988 const { email, forename, surname } = users [ 0 ] ;
70- await expect ( userService . registerUser ( { email, forename : 'f' . repeat ( 50 ) , surname, password : 'thunderbolt' } ) ) . rejects . toMatchObject ( { httpCode : HttpCode . BadRequest } ) ;
89+ await expect ( userService . registerUser ( { email, forename : '' , surname, password : 'thunderbolt' } ) ) . rejects . toMatchObject ( { httpCode : HttpCode . BadRequest } ) ;
7190 await expect ( getRepository ( User ) . findOneOrFail ( ) ) . rejects . toThrow ( ) ;
72- await expect ( userService . registerUser ( { email, forename, surname : 'h' . repeat ( 50 ) , password : 'thunderbolt' } ) ) . rejects . toMatchObject ( { httpCode : HttpCode . BadRequest } ) ;
91+ await expect ( userService . registerUser ( { email, forename, surname : '' , password : 'thunderbolt' } ) ) . rejects . toMatchObject ( { httpCode : HttpCode . BadRequest } ) ;
7392 await expect ( getRepository ( User ) . findOneOrFail ( ) ) . rejects . toThrow ( ) ;
7493 } ) ;
7594 } ) ;
0 commit comments