Skip to content

Commit bf9626e

Browse files
committed
fix updatePassword test
1 parent eb2fc4f commit bf9626e

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

server/controllers/user.controller/__tests__/authManagement.test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ jest.mock('../../../utils/mail', () => ({
2323
}
2424
}));
2525
jest.mock('../helpers', () => ({
26+
// userResponse: jest.fn(),
27+
...jest.requireActual('../helpers'),
2628
saveUser: jest.fn(),
2729
generateToken: jest.fn()
2830
}));
@@ -259,13 +261,14 @@ describe('user.controller > auth management', () => {
259261
});
260262

261263
describe('and when there is a user with valid token', () => {
262-
const fakeUser = createMockUser({
263-
email: 'test@example.com',
264+
const fakeSanitisedUser = createMockUser({ email: 'test@example.com' });
265+
const fakeUser = {
266+
...fakeSanitisedUser,
264267
password: 'oldpassword',
265268
resetPasswordToken: 'valid-token',
266269
resetPasswordExpires: fixedTime + 10000, // still valid
267270
save: jest.fn()
268-
});
271+
};
269272

270273
beforeEach(async () => {
271274
User.findOne = jest.fn().mockReturnValue({
@@ -275,7 +278,11 @@ describe('user.controller > auth management', () => {
275278
request.setBody({
276279
password: 'newpassword'
277280
});
278-
request.logIn = jest.fn();
281+
// simulate logging in after resetting the password works
282+
request.logIn = jest.fn((user, cb) => {
283+
request.user = user;
284+
cb(null);
285+
});
279286
await updatePassword(request, response, next);
280287
});
281288
it('calls user.save with the updated password and removes the reset password token', () => {
@@ -285,7 +292,7 @@ describe('user.controller > auth management', () => {
285292
expect(fakeUser.save).toHaveBeenCalled();
286293
});
287294
it('returns a success response with the sanitised user', () => {
288-
expect(response.json).toHaveBeenCalledWith({ success: true });
295+
expect(response.json).toHaveBeenCalledWith(fakeSanitisedUser);
289296
});
290297
});
291298
});

0 commit comments

Comments
 (0)