/** * SyncService Unit Tests */ import { IDynamoDbEvent, KushkiError } from "@kushki/core"; import { expect, use } from "chai"; import { IDENTIFIERS } from "constant/Identifiers"; import { DynamoGateway } from "gateway/DynamoGateway"; import { CONTAINER } from "infrastructure/Container"; import * as jsf from "json-schema-faker"; import * as path from "path"; import "reflect-metadata"; import { ISyncService } from "repository/ISyncService"; import { of } from "rxjs"; import { createSandbox, SinonSandbox, SinonStub } from "sinon"; import * as sinonChai from "sinon-chai"; import { Mock } from "ts-mockery"; import { MerchantFetch } from "types/remote/merchant_fetch"; import { ProcessorFetch } from "types/remote/processor_fetch"; use(sinonChai); const PSE_PROCESSOR: string = "Pse Processor"; const REMOTE_SCHEMA_PATH: string = ".kushki/schema"; let gMerchantFetch: MerchantFetch; let gProcessorStream: ProcessorFetch; function syncProcessorEvent(): IDynamoDbEvent { return Mock.of>({ Records: [ { dynamodb: { ApproximateCreationDateTime: 0, NewImage: gProcessorStream, OldImage: gProcessorStream, SizeBytes: 0, Keys: {}, SequenceNumber: "", StreamViewType: "", }, }, ], }); } async function createSchemas(): Promise { gProcessorStream = await jsf.resolve( { $ref: "processor_fetch.json", }, null, path.resolve(REMOTE_SCHEMA_PATH) ); gMerchantFetch = await jsf.resolve( { $ref: "merchant_fetch.json", }, null, path.resolve(REMOTE_SCHEMA_PATH) ); } describe("SyncService - ", () => { let put_item: SinonStub; let get_merchant_item: SinonStub; let sand_box: SinonSandbox; beforeEach(async () => { await createSchemas(); sand_box = createSandbox(); process.env.DYNAMO_MERCHANT = "DYNAMO_TABLE"; put_item = sand_box .stub(DynamoGateway.prototype, "put") .onFirstCall() .returns(of(true)); }); afterEach(() => { sand_box.restore(); }); it("test syncProcessor with Pse Processor - success", (done: MochaDone) => { gProcessorStream.StoreInformation.CreditInfo.processorName = PSE_PROCESSOR; const service: ISyncService = CONTAINER.get(IDENTIFIERS.SyncService); const event: IDynamoDbEvent = Mock.of< IDynamoDbEvent >({ Records: [ { dynamodb: { ApproximateCreationDateTime: 0, NewImage: gProcessorStream, OldImage: gProcessorStream, SizeBytes: 0, Keys: {}, SequenceNumber: "", StreamViewType: "", }, }, ], }); get_merchant_item = sand_box .stub(DynamoGateway.prototype, "getItem") .onFirstCall() .returns(of(gMerchantFetch)); service.syncProcessors(event).subscribe((data: boolean) => { expect(data).to.be.true; expect(put_item).to.be.calledOnce; expect(get_merchant_item).to.be.calledOnce; done(); }); }); it("test syncProcessor with other processor - do nothing success", (done: MochaDone) => { gProcessorStream.StoreInformation.CreditInfo.processorName = "Credimatic Processor"; const event: IDynamoDbEvent = syncProcessorEvent(); get_merchant_item = sand_box .stub(DynamoGateway.prototype, "getItem") .onFirstCall() .returns(of(gMerchantFetch)); const service: ISyncService = CONTAINER.get(IDENTIFIERS.SyncService); service.syncProcessors(event).subscribe((data: boolean) => { expect(put_item).to.have.not.been.called; expect(data).to.be.true; done(); }); }); it("test syncProcessor with undefined merchant fetch - throw error", (done: MochaDone) => { gProcessorStream.StoreInformation.CreditInfo.processorName = PSE_PROCESSOR; const event: IDynamoDbEvent = syncProcessorEvent(); get_merchant_item = sand_box .stub(DynamoGateway.prototype, "getItem") .onFirstCall() .returns(of(undefined)); const service: ISyncService = CONTAINER.get(IDENTIFIERS.SyncService); service.syncProcessors(event).subscribe( () => { done("getMerchantItem gives undefined as response"); }, (error: KushkiError) => { expect(error.code).to.be.eql("T007"); done(); } ); }); });