|
1 | 1 | import { Request as MockRequest } from 'jest-express/lib/request'; |
2 | 2 | import { Response as MockResponse } from 'jest-express/lib/response'; |
3 | 3 | import { NextFunction as MockNext } from 'jest-express/lib/next'; |
4 | | -import { User } from '../../../models/user'; |
| 4 | +import { User } from '../../../../models/user'; |
5 | 5 | import { |
6 | 6 | resetPasswordInitiate, |
7 | 7 | validateResetPasswordToken, |
8 | | - updatePassword, |
9 | | - updateSettings, |
10 | | - unlinkGithub, |
11 | | - unlinkGoogle |
12 | | -} from '../authManagement'; |
13 | | -import { saveUser, generateToken, userResponse } from '../helpers'; |
14 | | -import { createMockUser } from '../__testUtils__'; |
15 | | - |
16 | | -import { mailerService } from '../../../utils/mail'; |
17 | | -import { UserDocument } from '../../../types'; |
18 | | - |
19 | | -jest.mock('../../../models/user'); |
20 | | -jest.mock('../../../utils/mail', () => ({ |
| 8 | + updatePassword |
| 9 | +} from '../../authManagement'; |
| 10 | +import { generateToken } from '../../helpers'; |
| 11 | +import { createMockUser } from '../../__testUtils__'; |
| 12 | + |
| 13 | +import { mailerService } from '../../../../utils/mail'; |
| 14 | +import { UserDocument } from '../../../../types'; |
| 15 | + |
| 16 | +jest.mock('../../../../models/user'); |
| 17 | +jest.mock('../../../../utils/mail', () => ({ |
21 | 18 | mailerService: { |
22 | 19 | send: jest.fn() |
23 | 20 | } |
24 | 21 | })); |
25 | | -jest.mock('../helpers', () => ({ |
26 | | - ...jest.requireActual('../helpers'), |
27 | | - saveUser: jest.fn(), |
| 22 | +jest.mock('../../helpers', () => ({ |
| 23 | + ...jest.requireActual('../../helpers'), |
28 | 24 | generateToken: jest.fn() |
29 | 25 | })); |
30 | 26 |
|
31 | | -describe('user.controller > auth management', () => { |
| 27 | +describe('user.controller > auth management > password management', () => { |
32 | 28 | let request: any; |
33 | 29 | let response: any; |
34 | 30 | let next: MockNext; |
@@ -293,165 +289,4 @@ describe('user.controller > auth management', () => { |
293 | 289 | }); |
294 | 290 | }); |
295 | 291 | }); |
296 | | - |
297 | | - describe('updateSettings', () => { |
298 | | - beforeAll(() => { |
299 | | - jest.useFakeTimers().setSystemTime(fixedTime); |
300 | | - }); |
301 | | - |
302 | | - afterAll(() => { |
303 | | - jest.useRealTimers(); |
304 | | - }); |
305 | | - |
306 | | - describe('if the user is not found', () => { |
307 | | - beforeEach(async () => { |
308 | | - User.findById = jest.fn().mockResolvedValue(null); |
309 | | - |
310 | | - request.user = { id: 'nonexistent-id' }; |
311 | | - |
312 | | - (saveUser as jest.Mock).mockResolvedValue(null); |
313 | | - (generateToken as jest.Mock).mockResolvedValue('token12343'); |
314 | | - |
315 | | - await updateSettings(request, response, next); |
316 | | - }); |
317 | | - |
318 | | - it('returns 404 and a user-not-found error', async () => { |
319 | | - expect(response.status).toHaveBeenCalledWith(404); |
320 | | - expect(response.json).toHaveBeenCalledWith({ |
321 | | - error: 'User not found' |
322 | | - }); |
323 | | - }); |
324 | | - it('does not save the user', () => { |
325 | | - expect(saveUser).not.toHaveBeenCalled(); |
326 | | - }); |
327 | | - }); |
328 | | - |
329 | | - // the below tests match the current logic, but logic can be improved |
330 | | - describe('if the user is found', () => { |
331 | | - const startingUser = createMockUser({ |
332 | | - username: 'oldusername', |
333 | | - email: 'old@email.com', |
334 | | - id: 'valid-id', |
335 | | - comparePassword: jest.fn().mockResolvedValue(true) |
336 | | - }); |
337 | | - |
338 | | - beforeEach(() => { |
339 | | - User.findById = jest.fn().mockResolvedValue(startingUser); |
340 | | - |
341 | | - request.user = { id: 'valid-id' }; |
342 | | - |
343 | | - (saveUser as jest.Mock).mockResolvedValue(null); |
344 | | - (generateToken as jest.Mock).mockResolvedValue('token12343'); |
345 | | - }); |
346 | | - |
347 | | - describe('and when there is a username in the request', () => { |
348 | | - beforeEach(async () => { |
349 | | - request.setBody({ |
350 | | - username: 'newusername' |
351 | | - }); |
352 | | - await updateSettings(request, response, next); |
353 | | - }); |
354 | | - it('calls saveUser', () => { |
355 | | - expect(saveUser).toHaveBeenCalledWith(response, { |
356 | | - ...startingUser, |
357 | | - username: 'newusername' |
358 | | - }); |
359 | | - }); |
360 | | - }); |
361 | | - |
362 | | - // currently frontend doesn't seem to call the below |
363 | | - describe('and when there is a newPassword in the request', () => { |
364 | | - beforeEach(async () => {}); |
365 | | - describe('and the current password is not provided', () => { |
366 | | - it('returns 401 with a "current password not provided" message', () => {}); |
367 | | - it('does not save the user with the new password', () => {}); |
368 | | - }); |
369 | | - }); |
370 | | - describe('and when there is a currentPassword in the request', () => { |
371 | | - describe('and the current password does not match', () => { |
372 | | - it('returns 401 with a "current password invalid" message', () => {}); |
373 | | - it('does not save the user with the new password', () => {}); |
374 | | - }); |
375 | | - describe('and when the current password does match', () => { |
376 | | - it('calls saveUser with the new password', () => {}); |
377 | | - }); |
378 | | - }); |
379 | | - }); |
380 | | - }); |
381 | | - |
382 | | - describe('unlinkGithub', () => { |
383 | | - describe('and when there is no user in the request', () => { |
384 | | - beforeEach(async () => { |
385 | | - await unlinkGithub(request, response, next); |
386 | | - }); |
387 | | - it('does not call saveUser', () => { |
388 | | - expect(saveUser).not.toHaveBeenCalled(); |
389 | | - }); |
390 | | - it('returns a 404 with the correct status and message', () => { |
391 | | - expect(response.status).toHaveBeenCalledWith(404); |
392 | | - expect(response.json).toHaveBeenCalledWith({ |
393 | | - success: false, |
394 | | - message: 'You must be logged in to complete this action.' |
395 | | - }); |
396 | | - }); |
397 | | - }); |
398 | | - describe('and when there is a user in the request', () => { |
399 | | - const user = createMockUser({ |
400 | | - github: 'testuser', |
401 | | - tokens: [{ kind: 'github' }, { kind: 'google' }] |
402 | | - }); |
403 | | - |
404 | | - beforeEach(async () => { |
405 | | - request.user = user; |
406 | | - await unlinkGithub(request, response, next); |
407 | | - }); |
408 | | - it('removes the users github property', () => { |
409 | | - expect(user.github).toBeUndefined(); |
410 | | - }); |
411 | | - it('filters out the github token', () => { |
412 | | - expect(user.tokens).toEqual([{ kind: 'google' }]); |
413 | | - }); |
414 | | - it('does calls saveUser', () => { |
415 | | - expect(saveUser).toHaveBeenCalledWith(response, user); |
416 | | - }); |
417 | | - }); |
418 | | - }); |
419 | | - |
420 | | - describe('unlinkGoogle', () => { |
421 | | - describe('and when there is no user in the request', () => { |
422 | | - beforeEach(async () => { |
423 | | - await unlinkGoogle(request, response, next); |
424 | | - }); |
425 | | - it('does not call saveUser', () => { |
426 | | - expect(saveUser).not.toHaveBeenCalled(); |
427 | | - }); |
428 | | - it('returns a 404 with the correct status and message', () => { |
429 | | - expect(response.status).toHaveBeenCalledWith(404); |
430 | | - expect(response.json).toHaveBeenCalledWith({ |
431 | | - success: false, |
432 | | - message: 'You must be logged in to complete this action.' |
433 | | - }); |
434 | | - }); |
435 | | - }); |
436 | | - describe('and when there is a user in the request', () => { |
437 | | - const user = createMockUser({ |
438 | | - google: 'testuser', |
439 | | - tokens: [{ kind: 'github' }, { kind: 'google' }] |
440 | | - }); |
441 | | - |
442 | | - beforeEach(async () => { |
443 | | - request.user = user; |
444 | | - await unlinkGoogle(request, response, next); |
445 | | - }); |
446 | | - it('removes the users google property', () => { |
447 | | - expect(user.google).toBeUndefined(); |
448 | | - }); |
449 | | - it('filters out the google token', () => { |
450 | | - expect(user.tokens).toEqual([{ kind: 'github' }]); |
451 | | - }); |
452 | | - it('does calls saveUser', () => { |
453 | | - expect(saveUser).toHaveBeenCalledWith(response, user); |
454 | | - }); |
455 | | - }); |
456 | | - }); |
457 | 292 | }); |
0 commit comments