1- import { createMock } from '@golevelup/ts-jest' ;
2- import { CoreConfigService } from '@lib/core' ;
1+ import { DeepMocked , createMock } from '@golevelup/ts-jest' ;
2+ import { CoreConfigService , UserNotFound , isError } from '@lib/core' ;
33import { RepositoryService } from '@lib/repository' ;
44import { JwtService } from '@nestjs/jwt' ;
55import { Test } from '@nestjs/testing' ;
66import { ConfigServiceMock } from '../../../__mocks__/ConfigServiceMock' ;
77import { getRepositoryServiceMock } from '../../../__mocks__/RepositoryServiceMock' ;
88import { getUserStub } from '../../../__mocks__/stubs/user.stub' ;
99import { ConfigModel } from '../../../config/config.model' ;
10+ import { UserAlreadyExists } from '../../user/common/user.errors' ;
1011import { UserService } from '../../user/user.service' ;
1112import { AuthService } from '../auth.service' ;
13+ import { CannotFindEmailConfirm , EmailAlreadyConfirmed , EmailNotConfirmed } from '../common/auth.errors' ;
14+ import { GetTokenResult } from '../common/auth.model' ;
1215
1316describe ( 'AuthService' , ( ) => {
1417 let rep : ReturnType < typeof getRepositoryServiceMock > ;
1518 let user : ReturnType < typeof getUserStub > ;
1619 let authService : AuthService ;
1720 let configService : CoreConfigService < ConfigModel > ;
18- let userService : UserService ;
21+ let userService : DeepMocked < UserService > ;
22+ let jwt : JwtService ;
1923
2024 beforeEach ( async ( ) => {
2125 const module = await Test . createTestingModule ( {
@@ -38,6 +42,7 @@ describe('AuthService', () => {
3842 configService = module . get ( CoreConfigService ) ;
3943 rep = module . get ( RepositoryService ) ;
4044 userService = module . get ( UserService ) ;
45+ jwt = module . get ( JwtService ) ;
4146
4247 user = getUserStub ( ) ;
4348 } ) ;
@@ -49,4 +54,121 @@ describe('AuthService', () => {
4954 expect ( userService ) . toBeDefined ( ) ;
5055 expect ( user ) . toBeDefined ( ) ;
5156 } ) ;
57+
58+ describe ( 'signUp' , ( ) => {
59+ it ( 'should correct sign-up' , async ( ) => {
60+ userService . createUser . mockResolvedValue ( user ) ;
61+
62+ // TODO test sendConfirmEmail
63+
64+ const createResult = await authService . signUp ( {
65+ email : user . email ,
66+ password : 'password' ,
67+ } ) ;
68+
69+ expect ( isError ( createResult ) ) . toBeFalsy ( ) ;
70+ expect ( createResult ) . toEqual ( undefined ) ;
71+ } ) ;
72+
73+ it ( 'should return error if user already exist' , async ( ) => {
74+ userService . createUser . mockResolvedValue ( new UserAlreadyExists ( user . email ) ) ;
75+
76+ const createResult = await authService . signUp ( {
77+ email : user . email ,
78+ password : 'password' ,
79+ } ) ;
80+
81+ expect ( isError ( createResult ) ) . toBeTruthy ( ) ;
82+ expect ( createResult ) . toBeInstanceOf ( UserAlreadyExists ) ;
83+ } ) ;
84+ } ) ;
85+
86+ describe ( 'sendConfirmEmail' , ( ) => {
87+ it ( 'should return error if user email is already confirmed' , async ( ) => {
88+ const confirmResult = await authService . sendConfirmEmail ( { ...user , emailConfirmed : true } ) ;
89+
90+ expect ( isError ( confirmResult ) ) . toBeTruthy ( ) ;
91+ expect ( confirmResult ) . toBeInstanceOf ( EmailAlreadyConfirmed ) ;
92+ } ) ;
93+ } ) ;
94+
95+ describe ( 'signIn' , ( ) => {
96+ it ( 'should success sign-in with correct login and password' , async ( ) => {
97+ rep . user . findOne . mockResolvedValue ( { ...user , id : 99 , emailConfirmed : true } ) ;
98+
99+ const signInResult = await authService . signIn ( {
100+ email : user . email ,
101+ password : 'password' ,
102+ } ) ;
103+
104+ expect ( isError ( signInResult ) ) . toBeFalsy ( ) ;
105+ expect ( rep . user . save ) . toHaveBeenCalledTimes ( 1 ) ;
106+ expect ( rep . user . save ) . toHaveBeenCalledWith ( { id : 99 , refreshTokenHash : expect . any ( String ) } ) ;
107+ expect ( jwt . decode ( ( signInResult as GetTokenResult ) . token ) ) . toEqual ( {
108+ id : 99 ,
109+ date : expect . any ( Number ) ,
110+ exp : expect . any ( Number ) ,
111+ iat : expect . any ( Number ) ,
112+ } ) ;
113+ } ) ;
114+
115+ it ( 'should return error with incorrect login or password' , async ( ) => {
116+ rep . user . findOne . mockResolvedValue ( null ) ;
117+
118+ const signInResult = await authService . signIn ( {
119+ email : '' ,
120+ password : '' ,
121+ } ) ;
122+
123+ expect ( isError ( signInResult ) ) . toBeTruthy ( ) ;
124+ expect ( signInResult ) . toBeInstanceOf ( UserNotFound ) ;
125+ } ) ;
126+
127+ it ( 'should return error if email not confirmed' , async ( ) => {
128+ rep . user . findOne . mockResolvedValue ( { ...user , emailConfirmed : false } ) ;
129+
130+ const signInResult = await authService . signIn ( {
131+ email : user . email ,
132+ password : 'password' ,
133+ } ) ;
134+
135+ expect ( isError ( signInResult ) ) . toBeTruthy ( ) ;
136+ expect ( signInResult ) . toBeInstanceOf ( EmailNotConfirmed ) ;
137+ } ) ;
138+ } ) ;
139+
140+ describe ( 'confirmEmail' , ( ) => {
141+ it ( 'should confirm email' , async ( ) => {
142+ rep . user . findOne . mockResolvedValue ( { ...user , id : 77 , emailConfirmed : false } ) ;
143+
144+ const confirmResult = await authService . confirmEmail ( 'token' ) ;
145+
146+ expect ( rep . user . findOne ) . toHaveBeenCalledWith ( {
147+ where : { emailConfirmed : false , emailConfirmToken : 'token' } ,
148+ } ) ;
149+ expect ( isError ( confirmResult ) ) . toBeFalsy ( ) ;
150+ expect ( rep . user . save ) . toHaveBeenCalledTimes ( 1 ) ;
151+ expect ( rep . user . save ) . toHaveBeenCalledWith ( {
152+ ...user ,
153+ id : 77 ,
154+ emailConfirmed : true ,
155+ refreshTokenHash : expect . any ( String ) ,
156+ } ) ;
157+ expect ( jwt . decode ( ( confirmResult as GetTokenResult ) . token ) ) . toEqual ( {
158+ id : 77 ,
159+ date : expect . any ( Number ) ,
160+ exp : expect . any ( Number ) ,
161+ iat : expect . any ( Number ) ,
162+ } ) ;
163+ } ) ;
164+
165+ it ( 'should return error if email already confirmed' , async ( ) => {
166+ rep . user . findOne . mockResolvedValue ( null ) ;
167+
168+ const confirmResult = await authService . confirmEmail ( 'token' ) ;
169+
170+ expect ( isError ( confirmResult ) ) . toBeTruthy ( ) ;
171+ expect ( confirmResult ) . toBeInstanceOf ( CannotFindEmailConfirm ) ;
172+ } ) ;
173+ } ) ;
52174} ) ;
0 commit comments