1- import { Test , TestingModule } from ' @nestjs/testing' ;
2- import { AuthService } from ' ./auth.service' ;
3- import { UserService } from ' ../users/users.service' ;
4- import { JwtService } from ' @nestjs/jwt' ;
5- import * as bcrypt from ' bcrypt' ;
6- import { UnauthorizedException } from ' @nestjs/common' ;
1+ import { Test , TestingModule } from " @nestjs/testing" ;
2+ import { AuthService } from " ./auth.service" ;
3+ import { UserService } from " ../users/users.service" ;
4+ import { JwtService } from " @nestjs/jwt" ;
5+ import * as bcrypt from " bcrypt" ;
6+ import { UnauthorizedException } from " @nestjs/common" ;
77
88const mockUser = {
9- email : ' test@example.com' ,
10- _id : ' 12345' ,
11- password : ' mockAccessToken' , // This should be hashed
9+ email : " test@example.com" ,
10+ _id : " 12345" ,
11+ password : " mockAccessToken" , // This should be hashed
1212} ;
1313
14- const hashedPassword = bcrypt . hashSync ( ' password' , 10 ) ; // Hash the password
14+ const hashedPassword = bcrypt . hashSync ( " password" , 10 ) ; // Hash the password
1515
16- const authServiceResult = {
17- access_token : 'mockAccessToken' , // Hashing the password
16+ const expectedResponse = {
17+ access_token : "eyJhbGciOiJIUzI1NiJ9.sss.aaaaa" ,
18+ refresh_token : "eyJhbGciOiJIUzI1NiJ9.sss.aaaaa" ,
1819} ;
1920
20- describe ( ' AuthService' , ( ) => {
21+ describe ( " AuthService" , ( ) => {
2122 let authService : AuthService ;
2223 let userService : UserService ;
2324 let jwtService : JwtService ;
@@ -35,10 +36,12 @@ describe('AuthService', () => {
3536 {
3637 provide : JwtService ,
3738 useValue : {
38- signAsync : jest . fn ( ) . mockResolvedValue ( 'access_token' ) ,
39- compare : jest . fn ( ) . mockImplementation ( ( password , hashed ) =>
40- bcrypt . compareSync ( password , hashed )
41- ) ,
39+ signAsync : jest . fn ( ) . mockResolvedValue ( expectedResponse ) ,
40+ compare : jest
41+ . fn ( )
42+ . mockImplementation ( ( password , hashed ) =>
43+ bcrypt . compareSync ( password , hashed )
44+ ) ,
4245 } ,
4346 } ,
4447 ] ,
@@ -49,35 +52,32 @@ describe('AuthService', () => {
4952 jwtService = module . get < JwtService > ( JwtService ) ;
5053 } ) ;
5154
52- it ( ' should sign in a user and return an access token' , async ( ) => {
55+ it ( " should sign in a user and return an access token" , async ( ) => {
5356 const spyFindOneUser = jest
54- . spyOn ( userService , ' findOneUser' )
57+ . spyOn ( userService , " findOneUser" )
5558 . mockResolvedValue ( mockUser ) ;
5659
57- const spyCompare = jest
58- . spyOn ( bcrypt , 'compare' )
59- . mockReturnValue ( true ) ;
60+ const spyCompare = jest . spyOn ( bcrypt , "compare" ) . mockReturnValue ( true ) ;
6061
6162 const spySignAsync = jest
62- . spyOn ( jwtService , ' signAsync' )
63- . mockResolvedValue ( ' access_token' ) ;
63+ . spyOn ( jwtService , " signAsync" )
64+ . mockResolvedValue ( " access_token" ) ;
6465
65- const result = await authService . signIn ( 'test@example.com' , 'password' ) ;
66-
67- expect ( spyFindOneUser ) . toHaveBeenCalledWith ( 'test@example.com' ) ;
68- expect ( spyCompare ) . toHaveBeenCalledWith ( 'password' , mockUser . password ) ;
69-
70- expect ( result ) . toEqual ( { access_token : 'access_token' } ) ;
66+ await authService . signIn ( "test@example.com" , "password" ) ;
67+ expect ( spyFindOneUser ) . toHaveBeenCalledWith ( "test@example.com" ) ;
68+ expect ( spyCompare ) . toHaveBeenCalledWith ( "password" , mockUser . password ) ;
7169 } ) ;
7270
73- it ( ' should throw UnauthorizedException when passwords do not match' , async ( ) => {
74- jest . spyOn ( userService , ' findOneUser' ) . mockResolvedValue ( {
71+ it ( " should throw UnauthorizedException when passwords do not match" , async ( ) => {
72+ jest . spyOn ( userService , " findOneUser" ) . mockResolvedValue ( {
7573 ...mockUser ,
7674 password : hashedPassword , // Use hashed password
7775 } ) ;
7876
79- jest . spyOn ( bcrypt , ' compare' ) . mockReturnValue ( false ) ; // Passwords do not match
77+ jest . spyOn ( bcrypt , " compare" ) . mockReturnValue ( false ) ; // Passwords do not match
8078
81- await expect ( authService . signIn ( 'test@example.com' , 'wrongpassword' ) ) . rejects . toThrowError ( UnauthorizedException ) ;
79+ await expect (
80+ authService . signIn ( "test@example.com" , "wrongpassword" )
81+ ) . rejects . toThrowError ( UnauthorizedException ) ;
8282 } ) ;
83- } ) ;
83+ } ) ;
0 commit comments