import { AuthenticationClient } from "clients/authentication" ; import { ManagementClient } from "clients/management" ; import { chance , request , wait } from "_/helpers" ; import { mockRegisterRequest } from "_/mocks/common" ; describe ( "POST /leave" , ( ) => { const auth = new AuthenticationClient ( ) ; const mgmt = new ManagementClient ( ) ; it ( "should return error for missing authentication header" , ( ) => { return request . post ( "/leave" ) . expect ( 400 ) ; } ) ; it ( "should return error for invalid authentication header" , ( ) => { return request . get ( "/check" ) . set ( "Authorization" , chance . string ( ) ) . expect ( 403 ) ; } ) ; describe ( "with authenticated user" , ( ) => { const { email , password } = mockRegisterRequest ( ) ; beforeAll ( async ( ) => { const { subject } = await auth . register ( email , password ) ; await mgmt . verifyUser ( subject ) ; } ) ; afterAll ( async ( ) => { await mgmt . deleteUser ( email ) ; } ) ; it ( "should return empty body" , async ( ) => { const { body } = await request . post ( "/authenticate" ) . set ( "Content-Type" , "application/json" ) . send ( { username : email , password } ) ; wait ( 2000 ) ; return request . post ( "/leave" ) . set ( "Authorization" , `Bearer ${ body . access . token }` ) . expect ( 204 , "" ) ; } ) ; it ( "should invalidate cookie for valid token" , async ( ) => { const { body } = await request . post ( "/authenticate" ) . set ( "Content-Type" , "application/json" ) . send ( { username : email , password } ) ; wait ( 2000 ) ; const { header } = await request . post ( "/leave" ) . set ( "Authorization" , `Bearer ${ body . access . token }` ) . expect ( 204 , "" ) ; expect ( header [ "set-cookie" ] ) . toMatch ( `__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` ) ; } ) ; it ( "should invalidate cookie for invalid token" , async ( ) => { const { header } = await request . post ( "/leave" ) . set ( "Authorization" , `Bearer ${ chance . string ( ) }` ) . expect ( 400 ) ; expect ( header [ "set-cookie" ] ) . toMatch ( `__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` ) ; } ) ; it ( "should invalidate cookie for missing token" , async ( ) => { const { header } = await request . post ( "/leave" ) . expect ( 400 ) ; expect ( header [ "set-cookie" ] ) . toMatch ( `__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` ) ; } ) ; } ) ; } ) ;