Skip to content

Commit

Permalink
Avoids writing locale cookie when language not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
garethbowen committed Mar 14, 2019
1 parent 556fc2b commit 6e88d10
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions api/src/controllers/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ const setCookies = (req, res, sessionRes) => {
.then(userCtx => {
setSessionCookie(res, sessionCookie);
setUserCtxCookie(res, userCtx);
return auth.getUserSettings(userCtx).then(settings => {
setLocaleCookie(res, settings.language);
return auth.getUserSettings(userCtx).then(({ language }={}) => {
if (language) {
setLocaleCookie(res, language);
}
res.status(302).send(getRedirectUrl(userCtx));
});
})
Expand Down
20 changes: 20 additions & 0 deletions api/tests/mocha/controllers/login.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,26 @@ describe('login controller', () => {
});
});

it('does not set locale cookie if user language not defined', () => {
req.body = { user: 'sharon', password: 'p4ss' };
const postResponse = {
statusCode: 200,
headers: { 'set-cookie': [ 'AuthSession=abc;' ] }
};
sinon.stub(request, 'post').resolves(postResponse);
sinon.stub(res, 'send');
sinon.stub(res, 'status').returns(res);
const cookie = sinon.stub(res, 'cookie').returns(res);
sinon.stub(auth, 'getUserCtx').resolves({ name: 'shazza', roles: [ 'project-stuff' ] });
sinon.stub(auth, 'hasAllPermissions').returns(false);
sinon.stub(auth, 'getUserSettings').resolves({ });
return controller.post(req, res).then(() => {
chai.expect(cookie.callCount).to.equal(2);
chai.expect(cookie.args[0][0]).to.equal('AuthSession');
chai.expect(cookie.args[1][0]).to.equal('userCtx');
});
});

it('redict admin users to admin app after successful login', () => {
req.body = { user: 'sharon', password: 'p4ss' };
const postResponse = {
Expand Down

0 comments on commit 6e88d10

Please sign in to comment.