import { LeaveRequest } from "common" ; import { post } from "handlers/leave" ; import { chance } from "_/helpers" ; import { mockSessionClientSignOutWithError , mockSessionClientSignOutWithSuccess } from "_/mocks/clients" ; import { mockAPIGatewayProxyEvent } from "_/mocks/vendor/aws-lambda" ; describe ( "handlers/leave" , ( ) => { describe ( "post" , ( ) => { const token = chance . hash ( { length : 128 } ) ; const event = mockAPIGatewayProxyEvent < { } , LeaveRequest > ( { headers : { Authorization : `Bearer ${ token }` } } ) ; it ( "should resolve with empty body" , async ( ) => { const signOutMock = mockSessionClientSignOutWithSuccess ( ) ; const { statusCode , body } = await post ( event ) ; expect ( statusCode ) . toEqual ( 204 ) ; expect ( body ) . toEqual ( "" ) ; expect ( signOutMock ) . toHaveBeenCalled ( ) ; } ) ; it ( "should invalidate cookie for valid token" , async ( ) => { const signOutMock = mockSessionClientSignOutWithSuccess ( ) ; const { statusCode , headers } = await post ( event ) ; expect ( statusCode ) . toEqual ( 204 ) ; expect ( headers ) . toEqual ( { "Set-Cookie" : `__Secure-token=; Domain=${ process . env . COGNITO_IDENTITY_POOL_PROVIDER ! }; Path=/${ process . env . API_BASE_PATH ! }/authenticate; Expires=${ new Date ( 0 ) . toUTCString ( ) }; HttpOnly; Secure; SameSite=Strict` } ) ; expect ( signOutMock ) . toHaveBeenCalled ( ) ; } ) ; it ( "should invalidate cookie for invalid token" , async ( ) => { const signOutMock = mockSessionClientSignOutWithError ( ) ; const { statusCode , headers } = await post ( event ) ; expect ( statusCode ) . toEqual ( 400 ) ; expect ( headers ) . toEqual ( { "Set-Cookie" : `__Secure-token=; Domain=${ process . env . COGNITO_IDENTITY_POOL_PROVIDER ! }; Path=/${ process . env . API_BASE_PATH ! }/authenticate; Expires=${ new Date ( 0 ) . toUTCString ( ) }; HttpOnly; Secure; SameSite=Strict` } ) ; expect ( signOutMock ) . toHaveBeenCalled ( ) ; } ) ; it ( "should invalidate cookie for missing token" , async ( ) => { const signOutMock = mockSessionClientSignOutWithError ( ) ; const { statusCode , headers } = await post ( mockAPIGatewayProxyEvent < { } , LeaveRequest > ( ) ) ; expect ( statusCode ) . toEqual ( 400 ) ; expect ( headers ) . toEqual ( { "Set-Cookie" : `__Secure-token=; Domain=${ process . env . COGNITO_IDENTITY_POOL_PROVIDER ! }; Path=/${ process . env . API_BASE_PATH ! }/authenticate; Expires=${ new Date ( 0 ) . toUTCString ( ) }; HttpOnly; Secure; SameSite=Strict` } ) ; expect ( signOutMock ) . toHaveBeenCalled ( ) ; } ) ; it ( "should resolve with session client error" , async ( ) => { const signOutMock = mockSessionClientSignOutWithError ( ) ; const { statusCode , body } = await post ( event ) ; expect ( statusCode ) . toEqual ( 400 ) ; expect ( body ) . toEqual ( JSON . stringify ( { type : "Error" , message : "signOut" } ) ) ; expect ( signOutMock ) . toHaveBeenCalled ( ) ; } ) ; } ) ; } ) ;