1
+ import { beforeEach , describe , expect , it , jest , mock , spyOn } from 'bun:test' ;
2
+
1
3
import type { UserDocument } from '@nbw/database' ;
2
4
import { JwtService } from '@nestjs/jwt' ;
3
5
import { Test , TestingModule } from '@nestjs/testing' ;
4
- import { beforeEach , describe , expect , it , jest , mock , spyOn } from 'bun:test' ;
5
6
import type { Request , Response } from 'express' ;
6
7
7
8
import { UserService } from '@server/user/user.service' ;
@@ -10,9 +11,9 @@ import { AuthService } from './auth.service';
10
11
import { Profile } from './types/profile' ;
11
12
12
13
const mockAxios = {
13
- get : jest . fn ( ) ,
14
- post : jest . fn ( ) ,
15
- put : jest . fn ( ) ,
14
+ get : jest . fn ( ) ,
15
+ post : jest . fn ( ) ,
16
+ put : jest . fn ( ) ,
16
17
delete : jest . fn ( ) ,
17
18
create : jest . fn ( ) ,
18
19
} ;
@@ -21,15 +22,15 @@ mock.module('axios', () => mockAxios);
21
22
22
23
const mockUserService = {
23
24
generateUsername : jest . fn ( ) ,
24
- findByEmail : jest . fn ( ) ,
25
- findByID : jest . fn ( ) ,
26
- create : jest . fn ( ) ,
25
+ findByEmail : jest . fn ( ) ,
26
+ findByID : jest . fn ( ) ,
27
+ create : jest . fn ( ) ,
27
28
} ;
28
29
29
30
const mockJwtService = {
30
- decode : jest . fn ( ) ,
31
+ decode : jest . fn ( ) ,
31
32
signAsync : jest . fn ( ) ,
32
- verify : jest . fn ( ) ,
33
+ verify : jest . fn ( ) ,
33
34
} ;
34
35
35
36
describe ( 'AuthService' , ( ) => {
@@ -42,47 +43,47 @@ describe('AuthService', () => {
42
43
providers : [
43
44
AuthService ,
44
45
{
45
- provide : UserService ,
46
+ provide : UserService ,
46
47
useValue : mockUserService ,
47
48
} ,
48
49
{
49
- provide : JwtService ,
50
+ provide : JwtService ,
50
51
useValue : mockJwtService ,
51
52
} ,
52
53
{
53
- provide : 'COOKIE_EXPIRES_IN' ,
54
+ provide : 'COOKIE_EXPIRES_IN' ,
54
55
useValue : '3600' ,
55
56
} ,
56
57
{
57
- provide : 'FRONTEND_URL' ,
58
+ provide : 'FRONTEND_URL' ,
58
59
useValue : 'http://frontend.test.com' ,
59
60
} ,
60
61
{
61
- provide : 'COOKIE_EXPIRES_IN' ,
62
+ provide : 'COOKIE_EXPIRES_IN' ,
62
63
useValue : '3600' ,
63
64
} ,
64
65
{
65
- provide : 'JWT_SECRET' ,
66
+ provide : 'JWT_SECRET' ,
66
67
useValue : 'test-jwt-secret' ,
67
68
} ,
68
69
{
69
- provide : 'JWT_EXPIRES_IN' ,
70
+ provide : 'JWT_EXPIRES_IN' ,
70
71
useValue : '1d' ,
71
72
} ,
72
73
{
73
- provide : 'JWT_REFRESH_SECRET' ,
74
+ provide : 'JWT_REFRESH_SECRET' ,
74
75
useValue : 'test-jwt-refresh-secret' ,
75
76
} ,
76
77
{
77
- provide : 'JWT_REFRESH_EXPIRES_IN' ,
78
+ provide : 'JWT_REFRESH_EXPIRES_IN' ,
78
79
useValue : '7d' ,
79
80
} ,
80
81
{
81
- provide : 'WHITELISTED_USERS' ,
82
+ provide : 'WHITELISTED_USERS' ,
82
83
useValue : 'tomast1337,bentroen,testuser' ,
83
84
} ,
84
85
{
85
- provide : 'APP_DOMAIN' ,
86
+ provide : 'APP_DOMAIN' ,
86
87
useValue : '.test.com' ,
87
88
} ,
88
89
] ,
@@ -103,7 +104,7 @@ describe('AuthService', () => {
103
104
104
105
const res = {
105
106
status : jest . fn ( ) . mockReturnThis ( ) ,
106
- json : jest . fn ( ) ,
107
+ json : jest . fn ( ) ,
107
108
} as any ;
108
109
109
110
await authService . verifyToken ( req , res ) ;
@@ -120,7 +121,7 @@ describe('AuthService', () => {
120
121
121
122
const res = {
122
123
status : jest . fn ( ) . mockReturnThis ( ) ,
123
- json : jest . fn ( ) ,
124
+ json : jest . fn ( ) ,
124
125
} as any ;
125
126
126
127
await authService . verifyToken ( req , res ) ;
@@ -136,7 +137,7 @@ describe('AuthService', () => {
136
137
137
138
const res = {
138
139
status : jest . fn ( ) . mockReturnThis ( ) ,
139
- json : jest . fn ( ) ,
140
+ json : jest . fn ( ) ,
140
141
} as any ;
141
142
142
143
mockJwtService . verify . mockReturnValueOnce ( { id : 'test-id' } ) ;
@@ -155,7 +156,7 @@ describe('AuthService', () => {
155
156
156
157
const res = {
157
158
status : jest . fn ( ) . mockReturnThis ( ) ,
158
- json : jest . fn ( ) ,
159
+ json : jest . fn ( ) ,
159
160
} as any ;
160
161
161
162
const decodedToken = { id : 'test-id' } ;
@@ -206,17 +207,17 @@ describe('AuthService', () => {
206
207
const tokens = await ( authService as any ) . createJwtPayload ( payload ) ;
207
208
208
209
expect ( tokens ) . toEqual ( {
209
- access_token : accessToken ,
210
+ access_token : accessToken ,
210
211
refresh_token : refreshToken ,
211
212
} ) ;
212
213
213
214
expect ( jwtService . signAsync ) . toHaveBeenCalledWith ( payload , {
214
- secret : 'test-jwt-secret' ,
215
+ secret : 'test-jwt-secret' ,
215
216
expiresIn : '1d' ,
216
217
} ) ;
217
218
218
219
expect ( jwtService . signAsync ) . toHaveBeenCalledWith ( payload , {
219
- secret : 'test-jwt-refresh-secret' ,
220
+ secret : 'test-jwt-refresh-secret' ,
220
221
expiresIn : '7d' ,
221
222
} ) ;
222
223
} ) ;
@@ -225,18 +226,18 @@ describe('AuthService', () => {
225
226
describe ( 'GenTokenRedirect' , ( ) => {
226
227
it ( 'should set cookies and redirect to the frontend URL' , async ( ) => {
227
228
const user_registered = {
228
- _id : 'user-id' ,
229
-
229
+ _id : 'user-id' ,
230
+
230
231
username : 'testuser' ,
231
232
} as unknown as UserDocument ;
232
233
233
234
const res = {
234
- cookie : jest . fn ( ) ,
235
+ cookie : jest . fn ( ) ,
235
236
redirect : jest . fn ( ) ,
236
237
} as unknown as Response ;
237
238
238
239
const tokens = {
239
- access_token : 'access-token' ,
240
+ access_token : 'access-token' ,
240
241
refresh_token : 'refresh-token' ,
241
242
} ;
242
243
@@ -245,8 +246,8 @@ describe('AuthService', () => {
245
246
await ( authService as any ) . GenTokenRedirect ( user_registered , res ) ;
246
247
247
248
expect ( ( authService as any ) . createJwtPayload ) . toHaveBeenCalledWith ( {
248
- id : 'user-id' ,
249
-
249
+ id : 'user-id' ,
250
+
250
251
username : 'testuser' ,
251
252
} ) ;
252
253
@@ -271,8 +272,8 @@ describe('AuthService', () => {
271
272
describe ( 'verifyAndGetUser' , ( ) => {
272
273
it ( 'should create a new user if the user is not registered' , async ( ) => {
273
274
const user : Profile = {
274
- username : 'testuser' ,
275
-
275
+ username : 'testuser' ,
276
+
276
277
profileImage : 'http://example.com/photo.jpg' ,
277
278
} ;
278
279
@@ -285,7 +286,7 @@ describe('AuthService', () => {
285
286
286
287
expect ( userService . create ) . toHaveBeenCalledWith (
287
288
expect . objectContaining ( {
288
-
289
+
289
290
profileImage : 'http://example.com/photo.jpg' ,
290
291
} ) ,
291
292
) ;
@@ -295,13 +296,13 @@ describe('AuthService', () => {
295
296
296
297
it ( 'should return the registered user if the user is already registered' , async ( ) => {
297
298
const user : Profile = {
298
- username : 'testuser' ,
299
-
299
+ username : 'testuser' ,
300
+
300
301
profileImage : 'http://example.com/photo.jpg' ,
301
302
} ;
302
303
303
304
const registeredUser = {
304
- id : 'registered-user-id' ,
305
+ id : 'registered-user-id' ,
305
306
profileImage : 'http://example.com/photo.jpg' ,
306
307
} ;
307
308
@@ -315,15 +316,15 @@ describe('AuthService', () => {
315
316
316
317
it ( 'should update the profile image if it has changed' , async ( ) => {
317
318
const user : Profile = {
318
- username : 'testuser' ,
319
-
319
+ username : 'testuser' ,
320
+
320
321
profileImage : 'http://example.com/new-photo.jpg' ,
321
322
} ;
322
323
323
324
const registeredUser = {
324
- id : 'registered-user-id' ,
325
+ id : 'registered-user-id' ,
325
326
profileImage : 'http://example.com/old-photo.jpg' ,
326
- save : jest . fn ( ) ,
327
+ save : jest . fn ( ) ,
327
328
} ;
328
329
329
330
mockUserService . findByEmail . mockResolvedValue ( registeredUser ) ;
0 commit comments