import { AWSError } from "aws-sdk" ; import { AuthenticateRequestWithCredentials as Request } from "common" ; import { handler , translate } from "handlers/_" ; import { chance } from "_/helpers" ; import { mockAuthenticateRequestWithCredentials } from "_/mocks/common" ; import { mockHandlerCallbackWithError , mockHandlerCallbackWithSuccess } from "_/mocks/handlers" ; import { mockAPIGatewayProxyEvent , mockAPIGatewayProxyEventPathParameters } from "_/mocks/vendor/aws-lambda" ; import { mockValidateWithError , mockValidateWithSuccess } from "_/mocks/vendor/jsonschema" ; describe ( "handlers" , ( ) => { describe ( "translate" , ( ) => { it ( "should translate user validation error" , ( ) => { const message = chance . string ( ) ; expect ( translate ( { code : "UserLambdaValidationException" , message : `PreSignUp failed with error ${ message }` } as AWSError ) ) . toEqual ( { code : "AliasExistsException" , message } as AWSError ) ; } ) ; it ( "should translate request parsing error" , ( ) => { expect ( translate ( { code : "SyntaxError" , message : chance . string ( ) } as AWSError ) ) . toEqual ( { code : "TypeError" , message : "Invalid request" } as AWSError ) ; } ) ; it ( "should pass-through all other errors" , ( ) => { const message = chance . string ( ) ; const err = { code : chance . string ( ) , message } as AWSError ; expect ( translate ( err ) ) . toBe ( err ) ; } ) ; } ) ; describe ( "handler" , ( ) => { it ( "should resolve with callback response body" , async ( ) => { const event = mockAPIGatewayProxyEvent ( ) ; mockValidateWithSuccess ( ) ; const result = chance . string ( ) ; const cb = mockHandlerCallbackWithSuccess ( result ) ; const { statusCode , body } = await handler ( { } , cb ) ( event ) ; expect ( statusCode ) . toEqual ( 200 ) ; expect ( body ) . toEqual ( JSON . stringify ( result ) ) ; } ) ; it ( "should resolve with callback response headers" , async ( ) => { const event = mockAPIGatewayProxyEvent ( ) ; mockValidateWithSuccess ( ) ; const result = { [ chance . string ( ) ] : chance . string ( ) } ; const cb = mockHandlerCallbackWithSuccess ( undefined , result ) ; const { statusCode , headers } = await handler ( { } , cb ) ( event ) ; expect ( statusCode ) . toEqual ( 204 ) ; expect ( headers ) . toEqual ( result ) ; } ) ; it ( "should resolve with error for malformed request" , async ( ) => { const event = mockAPIGatewayProxyEvent ( { body : "/" } ) ; mockValidateWithSuccess ( ) ; const cb = mockHandlerCallbackWithSuccess ( ) ; const { statusCode , body } = await handler ( { } , cb ) ( event ) ; expect ( statusCode ) . toEqual ( 400 ) ; expect ( body ) . toEqual ( JSON . stringify ( { type : "TypeError" , message : "Invalid request" } ) ) ; } ) ; it ( "should resolve with error for invalid request" , async ( ) => { const event = mockAPIGatewayProxyEvent ( ) ; mockValidateWithError ( ) ; const cb = mockHandlerCallbackWithSuccess ( ) ; const { statusCode , body } = await handler ( { } , cb ) ( event ) ; expect ( statusCode ) . toEqual ( 400 ) ; expect ( body ) . toEqual ( JSON . stringify ( { type : "TypeError" , message : "Invalid request" } ) ) ; expect ( cb ) . not . toHaveBeenCalled ( ) ; } ) ; it ( "should resolve with error for internal error" , async ( ) => { const event = mockAPIGatewayProxyEvent ( ) ; mockValidateWithSuccess ( ) ; const cb = mockHandlerCallbackWithError ( ) ; const { statusCode , body } = await handler ( { } , cb ) ( event ) ; expect ( statusCode ) . toEqual ( 400 ) ; expect ( body ) . toEqual ( JSON . stringify ( { type : "Error" , message : "callback" } ) ) ; expect ( cb ) . toHaveBeenCalled ( ) ; } ) ; it ( "should invoke callback with path parameters" , async ( ) => { const pathParameters = mockAPIGatewayProxyEventPathParameters ( ) ; const event = mockAPIGatewayProxyEvent ( { pathParameters } ) ; mockValidateWithSuccess ( ) ; const cb = mockHandlerCallbackWithSuccess ( ) ; await handler ( { } , cb ) ( event ) ; expect ( cb ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { pathParameters } ) ) ; } ) ; it ( "should invoke callback with request body" , async ( ) => { const body = mockAuthenticateRequestWithCredentials ( ) ; const event = mockAPIGatewayProxyEvent < Request > ( { body } ) ; mockValidateWithSuccess ( ) ; const cb = mockHandlerCallbackWithSuccess ( ) ; await handler ( { } , cb ) ( event ) ; expect ( cb ) . toHaveBeenCalledWith ( jasmine . objectContaining ( { body } ) ) ; } ) ; it ( "should handle empty request body" , async ( ) => { const event = mockAPIGatewayProxyEvent ( { method : "GET" } ) ; mockValidateWithSuccess ( ) ; const cb = mockHandlerCallbackWithSuccess ( ) ; const { statusCode } = await handler ( { } , cb ) ( event ) ; expect ( statusCode ) . toEqual ( 204 ) ; } ) ; } ) ; } ) ;