Skip to content

Commit e65d42e

Browse files
committed
chore: add test for loginSuccessHandler
1 parent c7c45bc commit e65d42e

File tree

3 files changed

+58
-23
lines changed

3 files changed

+58
-23
lines changed

src/service/routes/auth.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ const getLoginStrategy = () => {
4747
return enabledAppropriateLoginStrategies[0].type.toLowerCase();
4848
};
4949

50+
const loginSuccessHandler = () => async (req, res) => {
51+
try {
52+
const currentUser = { ...req.user };
53+
delete currentUser.password;
54+
console.log(
55+
`serivce.routes.auth.login: user logged in, username=${
56+
currentUser.username
57+
} profile=${JSON.stringify(currentUser)}`,
58+
);
59+
res.send({
60+
message: 'success',
61+
user: toPublicUser(currentUser),
62+
});
63+
} catch (e) {
64+
console.log(`service.routes.auth.login: Error logging user in ${JSON.stringify(e)}`);
65+
res.status(500).send('Failed to login').end();
66+
}
67+
};
68+
5069
// TODO: provide separate auth endpoints for each auth strategy or chain compatibile auth strategies
5170
// TODO: if providing separate auth methods, inform the frontend so it has relevant UI elements and appropriate client-side behavior
5271
router.post(
@@ -60,25 +79,7 @@ router.post(
6079
console.log('going to auth with', authType);
6180
return passport.authenticate(authType)(req, res, next);
6281
},
63-
async (req, res) => {
64-
try {
65-
const currentUser = { ...req.user };
66-
delete currentUser.password;
67-
console.log(
68-
`serivce.routes.auth.login: user logged in, username=${
69-
currentUser.username
70-
} profile=${JSON.stringify(currentUser)}`,
71-
);
72-
res.send({
73-
message: 'success',
74-
user: toPublicUser(currentUser),
75-
});
76-
} catch (e) {
77-
console.log(`service.routes.auth.login: Error logging user in ${JSON.stringify(e)}`);
78-
res.status(500).send('Failed to login').end();
79-
return;
80-
}
81-
},
82+
loginSuccessHandler(),
8283
);
8384

8485
router.get('/oidc', passport.authenticate(authStrategies['openidconnect'].type));
@@ -162,4 +163,8 @@ router.get('/me', async (req, res) => {
162163
res.status(401).end();
163164
}
164165
});
165-
module.exports = router;
166+
167+
module.exports = {
168+
router,
169+
loginSuccessHandler
170+
};

src/service/routes/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const jwtAuthHandler = require('../passport/jwtAuthHandler');
1010
const router = new express.Router();
1111

1212
router.use('/api', home);
13-
router.use('/api/auth', auth);
13+
router.use('/api/auth', auth.router);
1414
router.use('/api/v1/healthcheck', healthcheck);
1515
router.use('/api/v1/push', jwtAuthHandler(), push);
1616
router.use('/api/v1/repo', jwtAuthHandler(), repo);

test/services/routes/auth.test.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const chai = require('chai');
22
const chaiHttp = require('chai-http');
33
const sinon = require('sinon');
44
const express = require('express');
5-
const authRouter = require('../../../src/service/routes/auth');
5+
const { router, loginSuccessHandler } = require('../../../src/service/routes/auth');
66
const db = require('../../../src/db');
77

88
const { expect } = chai;
@@ -19,7 +19,7 @@ const newApp = (username) => {
1919
});
2020
}
2121

22-
app.use('/auth', authRouter);
22+
app.use('/auth', router);
2323
return app;
2424
};
2525

@@ -28,6 +28,36 @@ describe('Auth API', function () {
2828
sinon.restore();
2929
});
3030

31+
describe('loginSuccessHandler', function () {
32+
it('should log in user and return public user data', async function () {
33+
const user = {
34+
username: 'bob',
35+
password: 'secret',
36+
37+
displayName: 'Bob',
38+
};
39+
40+
const res = {
41+
send: sinon.spy(),
42+
};
43+
44+
await loginSuccessHandler()({ user }, res);
45+
46+
expect(res.send.calledOnce).to.be.true;
47+
expect(res.send.firstCall.args[0]).to.deep.equal({
48+
message: 'success',
49+
user: {
50+
admin: false,
51+
displayName: 'Bob',
52+
53+
gitAccount: '',
54+
title: '',
55+
username: 'bob',
56+
},
57+
});
58+
});
59+
});
60+
3161
describe('/me', function () {
3262
it('GET /me returns Unauthorized if authenticated user not in request', async () => {
3363
const res = await chai.request(newApp()).get('/auth/me');

0 commit comments

Comments
 (0)