Skip to content

Commit b0548ea

Browse files
committed
split up authManagement tests into sub files for easier reading
1 parent 47015cd commit b0548ea

File tree

3 files changed

+250
-179
lines changed

3 files changed

+250
-179
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { Request as MockRequest } from 'jest-express/lib/request';
2+
import { Response as MockResponse } from 'jest-express/lib/response';
3+
import { NextFunction as MockNext } from 'jest-express/lib/next';
4+
import { unlinkGithub, unlinkGoogle } from '../../authManagement';
5+
import { saveUser } from '../../helpers';
6+
import { createMockUser } from '../../__testUtils__';
7+
8+
jest.mock('../../helpers', () => ({
9+
...jest.requireActual('../../helpers'),
10+
saveUser: jest.fn()
11+
}));
12+
jest.mock('../../../../utils/mail', () => ({
13+
mailerService: {
14+
send: jest.fn()
15+
}
16+
}));
17+
18+
describe('user.controller > auth management > 3rd party auth', () => {
19+
let request: any;
20+
let response: any;
21+
let next: MockNext;
22+
23+
beforeEach(() => {
24+
request = new MockRequest();
25+
response = new MockResponse();
26+
next = jest.fn();
27+
});
28+
29+
afterEach(() => {
30+
request.resetMocked();
31+
response.resetMocked();
32+
jest.clearAllMocks();
33+
});
34+
35+
describe('unlinkGithub', () => {
36+
describe('and when there is no user in the request', () => {
37+
beforeEach(async () => {
38+
await unlinkGithub(request, response, next);
39+
});
40+
it('does not call saveUser', () => {
41+
expect(saveUser).not.toHaveBeenCalled();
42+
});
43+
it('returns a 404 with the correct status and message', () => {
44+
expect(response.status).toHaveBeenCalledWith(404);
45+
expect(response.json).toHaveBeenCalledWith({
46+
success: false,
47+
message: 'You must be logged in to complete this action.'
48+
});
49+
});
50+
});
51+
describe('and when there is a user in the request', () => {
52+
const user = createMockUser({
53+
github: 'testuser',
54+
tokens: [{ kind: 'github' }, { kind: 'google' }]
55+
});
56+
57+
beforeEach(async () => {
58+
request.user = user;
59+
await unlinkGithub(request, response, next);
60+
});
61+
it('removes the users github property', () => {
62+
expect(user.github).toBeUndefined();
63+
});
64+
it('filters out the github token', () => {
65+
expect(user.tokens).toEqual([{ kind: 'google' }]);
66+
});
67+
it('does calls saveUser', () => {
68+
expect(saveUser).toHaveBeenCalledWith(response, user);
69+
});
70+
});
71+
});
72+
73+
describe('unlinkGoogle', () => {
74+
describe('and when there is no user in the request', () => {
75+
beforeEach(async () => {
76+
await unlinkGoogle(request, response, next);
77+
});
78+
it('does not call saveUser', () => {
79+
expect(saveUser).not.toHaveBeenCalled();
80+
});
81+
it('returns a 404 with the correct status and message', () => {
82+
expect(response.status).toHaveBeenCalledWith(404);
83+
expect(response.json).toHaveBeenCalledWith({
84+
success: false,
85+
message: 'You must be logged in to complete this action.'
86+
});
87+
});
88+
});
89+
describe('and when there is a user in the request', () => {
90+
const user = createMockUser({
91+
google: 'testuser',
92+
tokens: [{ kind: 'github' }, { kind: 'google' }]
93+
});
94+
95+
beforeEach(async () => {
96+
request.user = user;
97+
await unlinkGoogle(request, response, next);
98+
});
99+
it('removes the users google property', () => {
100+
expect(user.google).toBeUndefined();
101+
});
102+
it('filters out the google token', () => {
103+
expect(user.tokens).toEqual([{ kind: 'github' }]);
104+
});
105+
it('does calls saveUser', () => {
106+
expect(saveUser).toHaveBeenCalledWith(response, user);
107+
});
108+
});
109+
});
110+
});

server/controllers/user.controller/__tests__/authManagement.test.ts renamed to server/controllers/user.controller/__tests__/authManagement/passwordManagement.test.ts

Lines changed: 14 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
import { Request as MockRequest } from 'jest-express/lib/request';
22
import { Response as MockResponse } from 'jest-express/lib/response';
33
import { NextFunction as MockNext } from 'jest-express/lib/next';
4-
import { User } from '../../../models/user';
4+
import { User } from '../../../../models/user';
55
import {
66
resetPasswordInitiate,
77
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', () => ({
2118
mailerService: {
2219
send: jest.fn()
2320
}
2421
}));
25-
jest.mock('../helpers', () => ({
26-
...jest.requireActual('../helpers'),
27-
saveUser: jest.fn(),
22+
jest.mock('../../helpers', () => ({
23+
...jest.requireActual('../../helpers'),
2824
generateToken: jest.fn()
2925
}));
3026

31-
describe('user.controller > auth management', () => {
27+
describe('user.controller > auth management > password management', () => {
3228
let request: any;
3329
let response: any;
3430
let next: MockNext;
@@ -293,165 +289,4 @@ describe('user.controller > auth management', () => {
293289
});
294290
});
295291
});
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-
});
457292
});

0 commit comments

Comments
 (0)