-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathauth.controller.test.js
More file actions
582 lines (475 loc) · 21.8 KB
/
Copy pathauth.controller.test.js
File metadata and controls
582 lines (475 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
'use strict';
// ---------------------------------------------------------------------------
// Mock heavy dependencies before requiring the module under test.
// ---------------------------------------------------------------------------
jest.mock('jsonwebtoken');
jest.mock('bcryptjs');
jest.mock('crypto', () => ({
randomInt: jest.fn(() => 123456),
}));
// Mock @urbackend/common so that mongoose / redis are never touched.
jest.mock('@urbackend/common', () => {
const z = require('zod');
// Minimal mock developer instance returned by the model helpers.
const makeMockUser = (overrides = {}) => ({
_id: 'dev_id_1',
email: 'test@example.com',
isVerified: true,
maxProjects: 3,
refreshToken: null,
password: 'hashed_password',
save: jest.fn().mockResolvedValue(undefined),
...overrides,
});
const Developer = jest.fn().mockImplementation((data) => ({
...makeMockUser(),
...data,
save: jest.fn().mockResolvedValue(undefined),
}));
Developer.findOne = jest.fn();
Developer.findById = jest.fn();
Developer.findByIdAndDelete = jest.fn();
// Helper to mock a Mongoose query that is both chainable (select) and thenable
Developer.__mockQuery = (value) => {
const query = {
select: jest.fn().mockReturnThis(),
then: (resolve, reject) => Promise.resolve(value).then(resolve, reject),
catch: (reject) => Promise.resolve(value).catch(reject),
};
return query;
};
const Otp = jest.fn().mockImplementation((data) => ({
...data,
save: jest.fn().mockResolvedValue(undefined),
}));
Otp.findOne = jest.fn();
Otp.deleteOne = jest.fn().mockResolvedValue(undefined);
const Project = jest.fn();
Project.deleteMany = jest.fn().mockResolvedValue(undefined);
const PlatformEvent = {
create: jest.fn().mockResolvedValue(undefined),
};
class AppError extends Error {
constructor(statusCode, message) {
super(message);
this.statusCode = statusCode;
this.isOperational = true;
}
}
return {
Developer,
Otp,
Project,
PlatformEvent,
AppError,
ApiResponse: class ApiResponse {
constructor(data = {}, message = 'Success') {
this.data = data;
this.message = message;
this.success = true;
}
send(res, statusCode = 200) {
return res.status(statusCode).json({ success: this.success, data: this.data, message: this.message });
}
},
sendOtp: jest.fn().mockResolvedValue(undefined),
// Use real zod shapes so validation logic is exercised.
loginSchema: z.object({
email: z.string().email(),
password: z.string().min(1),
}),
changePasswordSchema: z.object({
currentPassword: z.string().min(1),
newPassword: z.string().min(6),
}),
deleteAccountSchema: z.object({
password: z.string().min(1),
}),
onlyEmailSchema: z.object({
email: z.string().email(),
}),
verifyOtpSchema: z.object({
email: z.string().email(),
otp: z.string(),
}),
resetPasswordSchema: z.object({
email: z.string().email(),
otp: z.string(),
newPassword: z.string().min(6),
}),
};
});
// ---------------------------------------------------------------------------
// Now safe to import the module under test and its mocked peers.
// ---------------------------------------------------------------------------
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');
const { Developer, Otp, Project, sendOtp, AppError } = require('@urbackend/common');
const authController = require('../controllers/auth.controller');
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
const makeRes = () => {
const res = {
status: jest.fn(),
json: jest.fn(),
cookie: jest.fn(),
};
// Allow chaining: res.status(200).cookie(...).json(...)
res.status.mockReturnValue(res);
res.cookie.mockReturnValue(res);
res.json.mockReturnValue(res);
return res;
};
const makeReq = (body = {}, user = null, cookies = {}) => ({
body,
user,
cookies,
});
const next = jest.fn();
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe('auth.controller', () => {
beforeEach(() => {
jest.clearAllMocks();
process.env.JWT_SECRET = 'test-secret';
process.env.JWT_REFRESH_SECRET = 'refresh-secret';
process.env.NODE_ENV = 'test';
});
// -----------------------------------------------------------------------
describe('register', () => {
test('returns 201 and success message on valid new-user registration', async () => {
Developer.findOne.mockResolvedValue(null);
bcrypt.genSalt.mockResolvedValue('salt');
bcrypt.hash.mockResolvedValue('hashed_password');
const mockSave = jest.fn().mockResolvedValue(undefined);
Developer.mockImplementation(() => ({ save: mockSave }));
const req = makeReq({ email: 'new@example.com', password: 'password123' });
const res = makeRes();
await authController.register(req, res, next);
expect(Developer.findOne).toHaveBeenCalledWith({ email: 'new@example.com' });
expect(res.status).toHaveBeenCalledWith(201);
expect(res.json).toHaveBeenCalledWith({ success: true, data: {}, message: 'Registered successfully' });
});
test('returns 400 when email already exists', async () => {
Developer.findOne.mockResolvedValue({ email: 'existing@example.com' });
const req = makeReq({ email: 'existing@example.com', password: 'password123' });
const res = makeRes();
await authController.register(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next.mock.calls[0][0].statusCode).toBe(400);
});
test('returns 400 on Zod validation error (invalid email)', async () => {
const req = makeReq({ email: 'not-an-email', password: 'password123' });
const res = makeRes();
await authController.register(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next.mock.calls[0][0].statusCode).toBe(400);
});
});
// -----------------------------------------------------------------------
describe('login', () => {
const mockUser = () => ({
_id: 'dev_id_1',
email: 'test@example.com',
isVerified: true,
maxProjects: 3,
refreshToken: null,
password: 'hashed_password',
save: jest.fn().mockResolvedValue(undefined),
});
test('returns 200 with cookie tokens on valid credentials', async () => {
const user = mockUser();
Developer.findOne.mockReturnValue(Developer.__mockQuery(user));
bcrypt.compare.mockResolvedValue(true);
jwt.sign.mockReturnValue('signed_token');
const req = makeReq({ email: 'test@example.com', password: 'correctpass' });
const res = makeRes();
await authController.login(req, res, next);
expect(bcrypt.compare).toHaveBeenCalledWith('correctpass', 'hashed_password');
expect(res.status).toHaveBeenCalledWith(200);
expect(res.cookie).toHaveBeenCalledWith('accessToken', expect.any(String), expect.any(Object));
expect(res.cookie).toHaveBeenCalledWith('refreshToken', expect.any(String), expect.any(Object));
expect(res.json).toHaveBeenCalledWith(
expect.objectContaining({ success: true })
);
});
test('returns 400 when user is not found', async () => {
Developer.findOne.mockReturnValue(Developer.__mockQuery(null));
const req = makeReq({ email: 'noone@example.com', password: 'pass' });
const res = makeRes();
await authController.login(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next.mock.calls[0][0].statusCode).toBe(400);
expect(next.mock.calls[0][0].message).toBe('Invalid email or password');
});
test('returns 400 on invalid password', async () => {
Developer.findOne.mockReturnValue(Developer.__mockQuery(mockUser()));
bcrypt.compare.mockResolvedValue(false);
const req = makeReq({ email: 'test@example.com', password: 'wrongpass' });
const res = makeRes();
await authController.login(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next.mock.calls[0][0].statusCode).toBe(400);
expect(next.mock.calls[0][0].message).toBe('Invalid email or password');
});
test('returns 400 on Zod validation error (missing password)', async () => {
const req = makeReq({ email: 'test@example.com' });
const res = makeRes();
await authController.login(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next.mock.calls[0][0].statusCode).toBe(400);
});
});
// -----------------------------------------------------------------------
describe('refreshToken', () => {
const mockUser = () => ({
_id: 'dev_id_1',
email: 'test@example.com',
isVerified: true,
maxProjects: 3,
refreshToken: 'valid_refresh_token',
password: 'hashed_password',
save: jest.fn().mockResolvedValue(undefined),
});
test('returns 200 with new tokens when refresh token is valid', async () => {
const user = mockUser();
jwt.verify.mockReturnValue({ _id: 'dev_id_1' });
Developer.findById.mockReturnValue(Developer.__mockQuery(user));
jwt.sign.mockReturnValue('new_token');
const req = makeReq({}, null, { refreshToken: 'valid_refresh_token' });
const res = makeRes();
await authController.refreshToken(req, res, next);
expect(jwt.verify).toHaveBeenCalledWith('valid_refresh_token', 'refresh-secret');
expect(res.status).toHaveBeenCalledWith(200);
});
test('returns 401 when no refresh token is provided', async () => {
const req = makeReq({}, null, {});
const res = makeRes();
await authController.refreshToken(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next.mock.calls[0][0].statusCode).toBe(401);
expect(next.mock.calls[0][0].message).toBe('No refresh token provided');
});
test('returns 403 when refresh token is invalid (jwt.verify throws)', async () => {
jwt.verify.mockImplementation(() => {
const err = new Error('invalid token');
err.name = 'JsonWebTokenError';
throw err;
});
const req = makeReq({}, null, { refreshToken: 'bad_token' });
const res = makeRes();
await authController.refreshToken(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next.mock.calls[0][0].statusCode).toBe(403);
});
test('returns 403 when stored refresh token does not match', async () => {
jwt.verify.mockReturnValue({ _id: 'dev_id_1' });
Developer.findById.mockReturnValue(Developer.__mockQuery({ ...mockUser(), refreshToken: 'different_token' }));
const req = makeReq({}, null, { refreshToken: 'valid_refresh_token' });
const res = makeRes();
await authController.refreshToken(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next.mock.calls[0][0].statusCode).toBe(403);
expect(next.mock.calls[0][0].message).toBe('Invalid refresh token');
});
});
// -----------------------------------------------------------------------
describe('logout', () => {
test('clears cookies and returns success', async () => {
const mockUser = {
_id: 'dev_id_1',
refreshToken: 'some_token',
save: jest.fn().mockResolvedValue(undefined),
};
Developer.findById.mockResolvedValue(mockUser);
const req = makeReq({}, { _id: 'dev_id_1' });
const res = makeRes();
await authController.logout(req, res, next);
expect(mockUser.save).toHaveBeenCalled();
expect(mockUser.refreshToken).toBeNull();
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({
success: true,
data: {},
message: 'Logged out successfully',
});
});
test('still returns success when req.user is absent', async () => {
const req = makeReq();
const res = makeRes();
await authController.logout(req, res, next);
expect(res.status).toHaveBeenCalledWith(200);
});
});
// -----------------------------------------------------------------------
describe('getMe', () => {
test('returns the user object without sensitive fields', async () => {
const mockSelect = jest.fn().mockResolvedValue({
_id: 'dev_id_1',
email: 'test@example.com',
});
Developer.findById.mockReturnValue({ select: mockSelect });
const req = makeReq({}, { _id: 'dev_id_1' });
const res = makeRes();
await authController.getMe(req, res, next);
expect(Developer.findById).toHaveBeenCalledWith('dev_id_1');
expect(mockSelect).toHaveBeenCalledWith('-password -refreshToken');
expect(res.json).toHaveBeenCalledWith(
expect.objectContaining({ success: true })
);
});
test('returns 404 when user does not exist', async () => {
Developer.findById.mockReturnValue({
select: jest.fn().mockResolvedValue(null),
});
const req = makeReq({}, { _id: 'missing_id' });
const res = makeRes();
await authController.getMe(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next.mock.calls[0][0].statusCode).toBe(404);
expect(next.mock.calls[0][0].message).toBe('User not found');
});
});
// -----------------------------------------------------------------------
describe('changePassword', () => {
test('returns 200 on successful password change', async () => {
const mockUser = {
_id: 'dev_id_1',
password: 'old_hashed',
save: jest.fn().mockResolvedValue(undefined),
};
Developer.findById.mockReturnValue(Developer.__mockQuery(mockUser));
bcrypt.compare.mockResolvedValue(true);
bcrypt.genSalt.mockResolvedValue('salt');
bcrypt.hash.mockResolvedValue('new_hashed');
const req = makeReq(
{ currentPassword: 'oldpass', newPassword: 'newpass123' },
{ _id: 'dev_id_1' }
);
const res = makeRes();
await authController.changePassword(req, res, next);
expect(bcrypt.compare).toHaveBeenCalledWith('oldpass', 'old_hashed');
expect(mockUser.save).toHaveBeenCalled();
expect(res.json).toHaveBeenCalledWith({
success: true,
data: {},
message: 'Password updated successfully',
});
});
test('returns 400 when current password is incorrect', async () => {
Developer.findById.mockReturnValue(Developer.__mockQuery({
password: 'old_hashed',
save: jest.fn(),
}));
bcrypt.compare.mockResolvedValue(false);
const req = makeReq(
{ currentPassword: 'wrongold', newPassword: 'newpass123' },
{ _id: 'dev_id_1' }
);
const res = makeRes();
await authController.changePassword(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next.mock.calls[0][0].statusCode).toBe(400);
expect(next.mock.calls[0][0].message).toBe('Incorrect current password');
});
});
// -----------------------------------------------------------------------
describe('sendOtp', () => {
test('returns 400 when user is not found', async () => {
Developer.findOne.mockReturnValue(Developer.__mockQuery(null));
const req = makeReq({ email: 'noone@example.com' });
const res = makeRes();
await authController.sendOtp(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next.mock.calls[0][0].statusCode).toBe(400);
expect(next.mock.calls[0][0].message).toBe('User not found. Ensure you are using the correct email.');
});
test('returns 400 when user is already verified', async () => {
Developer.findOne.mockReturnValue(Developer.__mockQuery({ _id: 'u1', isVerified: true }));
const req = makeReq({ email: 'verified@example.com' });
const res = makeRes();
await authController.sendOtp(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next.mock.calls[0][0].statusCode).toBe(400);
expect(next.mock.calls[0][0].message).toBe('Account is already verified. Please login.');
});
test('sends OTP and returns success for unverified user', async () => {
const user = { _id: 'u1', isVerified: false };
Developer.findOne.mockReturnValue(Developer.__mockQuery(user));
Otp.deleteOne.mockResolvedValue(undefined);
bcrypt.genSalt.mockResolvedValue('salt');
bcrypt.hash.mockResolvedValue('hashed_otp');
const mockOtpInstance = { save: jest.fn().mockResolvedValue(undefined) };
Otp.mockImplementation(() => mockOtpInstance);
sendOtp.mockResolvedValue(undefined);
const req = makeReq({ email: 'unverified@example.com' });
const res = makeRes();
await authController.sendOtp(req, res, next);
expect(sendOtp).toHaveBeenCalled();
expect(res.json).toHaveBeenCalledWith({ success: true, data: {}, message: 'OTP sent successfully' });
});
});
// -----------------------------------------------------------------------
describe('forgotPassword', () => {
test('returns the same message regardless of whether user exists (prevents email enumeration)', async () => {
Developer.findOne.mockReturnValue(Developer.__mockQuery(null));
const req = makeReq({ email: 'ghost@example.com' });
const res = makeRes();
await authController.forgotPassword(req, res, next);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(
expect.objectContaining({ message: expect.stringContaining('OTP has been sent') })
);
});
});
// -----------------------------------------------------------------------
describe('resetPassword', () => {
test('clears stored refresh token after successful password reset', async () => {
const mockOtpDoc = {
attempts: 0,
otp: 'hashed_otp',
deleteOne: jest.fn().mockResolvedValue(undefined),
save: jest.fn().mockResolvedValue(undefined),
};
const mockUser = {
_id: 'dev_id_1',
email: 'test@example.com',
password: 'old_hashed_password',
refreshToken: 'active_refresh_token',
save: jest.fn().mockResolvedValue(undefined),
};
Developer.findOne.mockReturnValue(Developer.__mockQuery(mockUser));
Otp.findOne.mockResolvedValue(mockOtpDoc);
bcrypt.compare.mockResolvedValue(true);
bcrypt.genSalt.mockResolvedValue('salt');
bcrypt.hash.mockResolvedValue('new_hashed_password');
const req = makeReq({
email: 'test@example.com',
otp: '123456',
newPassword: 'newpassword123',
});
const res = makeRes();
await authController.resetPassword(req, res, next);
expect(mockUser.refreshToken).toBeNull();
expect(mockUser.save).toHaveBeenCalled();
expect(res.cookie).toHaveBeenCalledWith(
'accessToken',
'none',
expect.objectContaining({ httpOnly: true })
);
expect(res.cookie).toHaveBeenCalledWith(
'refreshToken',
'none',
expect.objectContaining({ httpOnly: true })
);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({
success: true,
data: {},
message: 'Password reset successfully. Please log in with your new password.'
});
});
});
});