@@ -16,7 +16,8 @@ import { log } from '../../../../src/utils/logger';
1616import {
1717 getCookieValue ,
1818 getUnsignedCookie ,
19- validateUnsignedCookie
19+ validateUnsignedCookie ,
20+ getUserEmailFromColaJwt ,
2021} from '../../../../src/utils/cookie' ;
2122import { cookieSignedValue , req } from '../../../mock/data.mock' ;
2223
@@ -26,10 +27,12 @@ const logErrorRequestMock = log.errorRequest as jest.Mock;
2627const getCookieValueMock = getCookieValue as jest . Mock ;
2728const getUnsignedCookieMock = getUnsignedCookie as jest . Mock ;
2829const validateUnsignedCookieMock = validateUnsignedCookie as jest . Mock ;
30+ const getUserEmailFromColaJwtMock = getUserEmailFromColaJwt as jest . Mock ;
2931
3032export const mockResponse = ( ) => {
3133 const res = { } as Response ;
3234 res . redirect = jest . fn ( ) as any ;
35+ res . locals = { } ;
3336 return res ;
3437} ;
3538
@@ -88,6 +91,21 @@ describe('Cola Authentication Middleware test suites', () => {
8891 expect ( res . redirect ) . toHaveBeenCalledTimes ( 0 ) ;
8992 } ) ;
9093
94+ test ( 'should attach userEmailAuth property to res.locals if validation is successful' , ( ) => {
95+ const unsignedCookie = 'xyz.123' ;
96+ const email = 'placeholder@fake.com' ;
97+
98+ getUnsignedCookieMock . mockReturnValueOnce ( unsignedCookie ) ;
99+ validateUnsignedCookieMock . mockReturnValueOnce ( true ) ;
100+ getUserEmailFromColaJwtMock . mockReturnValueOnce ( email ) ;
101+
102+ authentication ( req , res , next ) ;
103+
104+ expect ( getUserEmailFromColaJwtMock ) . toHaveBeenCalledTimes ( 1 ) ;
105+ expect ( getUserEmailFromColaJwtMock ) . toHaveBeenCalledWith ( unsignedCookie ) ;
106+ expect ( res . locals . userEmailAuth ) . toBe ( email ) ;
107+ } ) ;
108+
91109 test ( 'should call next with error object if error is thrown' , ( ) => {
92110 getCookieValueMock . mockReturnValueOnce ( cookieSignedValue ) ;
93111 validateUnsignedCookieMock . mockReturnValueOnce ( false ) ;
0 commit comments