import { ExecutionContext } from '@nestjs/common' ; import { InvalidHttpAuthTokenException , MissingHttpAuthTokenException , } from '../exceptions' ; import { extractHttpAuthToken , HttpAuthTokenDecorator , } from './http-auth-token.decorator' ; describe ( 'HttpAuthTokenDecorator' , ( ) => { describe ( 'extractHttpAuthToken' , ( ) => { it ( 'should return undefined if bearer does not contain "Bearer " prefix' , ( ) => { const inputMock = 'Some random string' ; const result = extractHttpAuthToken ( inputMock ) ; expect ( result ) . toBe ( undefined ) ; } ) ; it ( 'should return undefined if bearer does not contain a token' , ( ) => { const inputMock = 'Bearer ' ; const result = extractHttpAuthToken ( inputMock ) ; expect ( result ) . toBe ( undefined ) ; } ) ; it ( 'should return undefined if bearer contains a too long string' , ( ) => { const inputMock = 'Bearer Loremipsumdolorsitametconsecteturadipiscingelitseddoeiusmodtempos' ; const result = extractHttpAuthToken ( inputMock ) ; expect ( result ) . toBe ( undefined ) ; } ) ; it ( 'should return undefined is bearer contains space characters' , ( ) => { const inputMock = 'Bearer some space separated Random bearer' ; const result = extractHttpAuthToken ( inputMock ) ; expect ( result ) . toBe ( undefined ) ; } ) ; it ( 'should return undefined is bearer contains strange characters' , ( ) => { const inputMock = 'SomeStrange😉🍌input' ; const result = extractHttpAuthToken ( inputMock ) ; expect ( result ) . toBe ( undefined ) ; } ) ; it ( 'should return true is bearer is properly formed' , ( ) => { const inputMock = 'Bearer some-Random_bearer+string' ; const result = extractHttpAuthToken ( inputMock ) ; expect ( result ) . toBe ( 'some-Random_bearer+string' ) ; } ) ; } ) ; describe ( 'HttpAuthToken' , ( ) => { const dataMock = { } ; const getRequestMock = jest . fn ( ) ; const ctxMock = { switchToHttp : ( ) => ( { getRequest : getRequestMock , } ) , } as unknown as ExecutionContext ; it ( 'should throw if validate returns undefined' , ( ) => { getRequestMock . mockReturnValueOnce ( { headers : { } , } ) ; expect ( ( ) => HttpAuthTokenDecorator ( dataMock , ctxMock ) ) . toThrow ( MissingHttpAuthTokenException ) ; } ) ; it ( 'should throw if validate returns undefined' , ( ) => { getRequestMock . mockReturnValueOnce ( { headers : { authorization : 'invalid header value' , } , } ) ; expect ( ( ) => HttpAuthTokenDecorator ( dataMock , ctxMock ) ) . toThrow ( InvalidHttpAuthTokenException ) ; } ) ; it ( 'should return the validated token' , ( ) => { getRequestMock . mockReturnValueOnce ( { headers : { authorization : 'Bearer valid-header-value' , } , } ) ; const result = HttpAuthTokenDecorator ( dataMock , ctxMock ) ; expect ( result ) . toBe ( 'valid-header-value' ) ; } ) ; } ) ; } ) ;