/** * Lambda Unit Tests */ import { KushkiError } from "@kushki/core"; import * as AWS_SDK from "aws-sdk"; import * as AWS from "aws-sdk-mock"; import { expect, use } from "chai"; import { IDENTIFIERS } from "constant/Identifiers"; import { CONTAINER } from "infrastructure/Container"; import { ILambdaGateway } from "repository/ILambdaGateway"; import { createSandbox, match, SinonSandbox, SinonStub } from "sinon"; import * as sinonChai from "sinon-chai"; import { Mock } from "ts-mockery"; AWS.setSDKInstance(AWS_SDK); use(sinonChai); describe("Lambda Gateway - success", () => { let gateway: ILambdaGateway; let box: SinonSandbox; let invoke_stub: SinonStub; beforeEach(() => { box = createSandbox(); }); afterEach(() => { AWS.restore(); box.restore(); }); it("invoke lambda, success", (done: Mocha.Done) => { invoke_stub = box .stub() .callsFake( ( _params: AWS_SDK.Lambda.Types.InvocationRequest, callback: ( err: null, data: AWS_SDK.Lambda.Types.InvocationResponse ) => void ) => { callback( null, Mock.of({ Payload: JSON.stringify({ qwerty: 123, }), }) ); } ); AWS.mock("Lambda", "invoke", invoke_stub); gateway = CONTAINER.get(IDENTIFIERS.LambdaGateway); gateway .invokeFunction("qwerty", { lorem: "ipsum", }) .subscribe( (response: object) => { expect(invoke_stub).to.be.calledOnce.and.calledWithMatch( match.has( "Payload", JSON.stringify({ lorem: "ipsum", }) ) ); expect(response).to.have.property("qwerty", 123); }, done, done ); }); it("invoke lambda with body, success", (done: Mocha.Done) => { invoke_stub = box .stub() .callsFake( ( _params: AWS_SDK.Lambda.Types.InvocationRequest, callback: ( err: null, data: AWS_SDK.Lambda.Types.InvocationResponse ) => void ) => { callback( null, Mock.of({ Payload: JSON.stringify({ body: JSON.stringify({ query: 123, }), }), }) ); } ); AWS.mock("Lambda", "invoke", invoke_stub); gateway = CONTAINER.get(IDENTIFIERS.LambdaGateway); gateway .invokeFunction("http", { lorem: "ipsum", }) .subscribe( (response: object) => { expect(invoke_stub).to.be.calledOnce; expect(response) .to.have.property("body") .and.to.have.property("query", 123); }, done, done ); }); }); describe("Lambda Gateway - fail", () => { let gateway: ILambdaGateway; let box: SinonSandbox; let invoke_stub: SinonStub; beforeEach(() => { box = createSandbox(); }); afterEach(() => { AWS.restore(); box.restore(); }); it("invoke lambda, fail null response", (done: Mocha.Done) => { invoke_stub = box .stub() .callsFake( ( _params: AWS_SDK.Lambda.Types.InvocationRequest, callback: (err: null, data: object) => void ) => { callback(null, { Payload: null, }); } ); AWS.mock("Lambda", "invoke", invoke_stub); gateway = CONTAINER.get(IDENTIFIERS.LambdaGateway); gateway .invokeFunction("qwerty", { lorem: "ipsum", }) .subscribe( () => { done("error"); }, (e: Error) => { expect(e.message).to.be.eq("Lambda response is null"); done(); }, done ); }); it("invoke lambda, fail", (done: Mocha.Done) => { invoke_stub = box .stub() .callsFake( ( _params: AWS_SDK.Lambda.Types.InvocationRequest, callback: (err: null, data: object) => void ) => { callback(null, { Payload: JSON.stringify({ statusCode: 500, code: "qwerty", message: "qwerty", }), }); } ); AWS.mock("Lambda", "invoke", invoke_stub); gateway = CONTAINER.get(IDENTIFIERS.LambdaGateway); gateway .invokeFunction("qwerty", { lorem: "ipsum", }) .subscribe( () => { done("error"); }, (e: Error) => { expect(e.message).to.be.eq("qwerty"); done(); }, done ); }); it("invoke lambda, fail with body", (done: Mocha.Done) => { invoke_stub = box .stub() .callsFake( ( _params: AWS_SDK.Lambda.Types.InvocationRequest, callback: (err: null, data: object) => void ) => { callback(null, { Payload: JSON.stringify({ statusCode: 500, body: JSON.stringify({ code: "qwerty", message: "qwerty", }), }), }); } ); AWS.mock("Lambda", "invoke", invoke_stub); gateway = CONTAINER.get(IDENTIFIERS.LambdaGateway); gateway .invokeFunction("qwerty", { lorem: "ipsum", }) .subscribe( () => { done("error"); }, (e: Error) => { expect(e.message).to.be.eq("qwerty"); done(); }, done ); }); it("invoke lambda, fail KushkiError on body", (done: Mocha.Done) => { invoke_stub = box .stub() .callsFake( ( _params: AWS_SDK.Lambda.Types.InvocationRequest, callback: (err: null, data: object) => void ) => { callback(null, { Payload: JSON.stringify({ statusCode: 400, body: JSON.stringify({ code: "T004", }), }), }); } ); AWS.mock("Lambda", "invoke", invoke_stub); gateway = CONTAINER.get(IDENTIFIERS.LambdaGateway); gateway .invokeFunction("qwerty", { lorem: "ipsum", }) .subscribe( () => { done("error"); }, (e: KushkiError) => { expect(e).to.be.instanceOf(KushkiError); expect(e.getMessage()).to.be.eq("No existe la transacción"); expect(e.code).to.be.eq("T004"); done(); }, done ); }); it("invoke lambda, fail kushkiError", (done: Mocha.Done) => { invoke_stub = box .stub() .callsFake( ( _params: AWS_SDK.Lambda.Types.InvocationRequest, callback: (err: null, data: object) => void ) => { callback(null, { Payload: JSON.stringify({ statusCode: 500, code: "T004", }), }); } ); AWS.mock("Lambda", "invoke", invoke_stub); gateway = CONTAINER.get(IDENTIFIERS.LambdaGateway); gateway .invokeFunction("qwerty", { lorem: "ipsum", }) .subscribe( () => { done("error"); }, (e: KushkiError) => { expect(e.code).to.be.eq("T004"); expect(e).to.be.instanceOf(KushkiError); expect(e.getMessage()).to.be.eq("No existe la transacción"); done(); }, done ); }); });