Skip to content

Commit f78367b

Browse files
test(auth): add coverage for local strategy unexpected error path
Adds an integration test that mocks AuthService.authenticate to throw a plain Error (non-AppError) and verifies the server responds with 500, covering the done(err) branch in the local passport strategy.
1 parent 9e457cc commit f78367b

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

modules/auth/tests/auth.integration.tests.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import config from '../../../config/index.js';
1616
*/
1717
describe('Auth integration tests:', () => {
1818
let UserService = null;
19+
let AuthService = null;
1920
let agent;
2021
let credentials;
2122
let user;
@@ -28,6 +29,7 @@ describe('Auth integration tests:', () => {
2829
try {
2930
const init = await bootstrap();
3031
UserService = (await import(path.resolve('./modules/users/services/users.service.js'))).default;
32+
AuthService = (await import(path.resolve('./modules/auth/services/auth.service.js'))).default;
3133
agent = request.agent(init.app);
3234
} catch (err) {
3335
console.log(err);
@@ -794,6 +796,12 @@ describe('Auth integration tests:', () => {
794796
const result = await agent.get('/api/auth/reset/sometoken').expect(302);
795797
expect(result.headers.location).toBe('/api/password/reset/invalid');
796798
});
799+
800+
test('should return 500 when local strategy authenticate throws an unexpected error', async () => {
801+
const spy = jest.spyOn(AuthService, 'authenticate').mockRejectedValueOnce(new Error('DB failure'));
802+
await agent.post('/api/auth/signin').send({ email: 'a@b.com', password: 'pass' }).expect(500);
803+
spy.mockRestore();
804+
});
797805
});
798806

799807
// Mongoose disconnect

0 commit comments

Comments
 (0)