-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from cabinetoffice/NTRNL-499-extract-user-emai…
…l-from-cola-jwt Ntrnl 499 extract user email from cola jwt
- Loading branch information
Showing
4 changed files
with
24 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,8 @@ import { log } from '../../../../src/utils/logger'; | |
import { | ||
getCookieValue, | ||
getUnsignedCookie, | ||
validateUnsignedCookie | ||
validateUnsignedCookie, | ||
getUserEmailFromColaJwt, | ||
} from '../../../../src/utils/cookie'; | ||
import { cookieSignedValue, req } from '../../../mock/data.mock'; | ||
|
||
|
@@ -26,10 +27,12 @@ const logErrorRequestMock = log.errorRequest as jest.Mock; | |
const getCookieValueMock = getCookieValue as jest.Mock; | ||
const getUnsignedCookieMock = getUnsignedCookie as jest.Mock; | ||
const validateUnsignedCookieMock = validateUnsignedCookie as jest.Mock; | ||
const getUserEmailFromColaJwtMock = getUserEmailFromColaJwt as jest.Mock; | ||
|
||
export const mockResponse = () => { | ||
const res = {} as Response; | ||
res.redirect = jest.fn() as any; | ||
res.locals = {}; | ||
return res; | ||
}; | ||
|
||
|
@@ -88,6 +91,21 @@ describe('Cola Authentication Middleware test suites', () => { | |
expect(res.redirect).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('should attach userEmailAuth property to res.locals if validation is successful', () => { | ||
const unsignedCookie = 'xyz.123'; | ||
const email = '[email protected]'; | ||
|
||
getUnsignedCookieMock.mockReturnValueOnce(unsignedCookie); | ||
validateUnsignedCookieMock.mockReturnValueOnce(true); | ||
getUserEmailFromColaJwtMock.mockReturnValueOnce(email); | ||
|
||
authentication(req, res, next); | ||
|
||
expect(getUserEmailFromColaJwtMock).toHaveBeenCalledTimes(1); | ||
expect(getUserEmailFromColaJwtMock).toHaveBeenCalledWith(unsignedCookie); | ||
expect(res.locals.userEmailAuth).toBe(email); | ||
}); | ||
|
||
test('should call next with error object if error is thrown', () => { | ||
getCookieValueMock.mockReturnValueOnce(cookieSignedValue); | ||
validateUnsignedCookieMock.mockReturnValueOnce(false); | ||
|