UNPKG

component-dbs-core

Version:

DTO objects shared among device backend

465 lines 21.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const ts_mockito_1 = require("ts-mockito"); const uuid_1 = require("uuid"); const transactionService_1 = require("./transactionService"); const config_1 = require("../common_services/config"); const transactionDb_1 = require("./transactionDb"); const devices_1 = require("../devices"); const appsyncUtils_1 = require("../common_services/appsync/appsyncUtils"); const common_services_1 = require("../common_services"); describe('TransactionService test suite', () => { let txnService; const mockEnvService = ts_mockito_1.mock(config_1.EnvironmentService); const mockDynamodbService = ts_mockito_1.mock(common_services_1.DynamodbService); const mockTransactionDb = ts_mockito_1.mock(transactionDb_1.TransactionDb); const mockDeviceVerify = ts_mockito_1.mock(devices_1.DeviceVerification); const mockDeviceService = ts_mockito_1.mock(devices_1.DeviceService); const mockAppSyncClient = ts_mockito_1.mock(common_services_1.AppSyncClient); const mockLambdaService = ts_mockito_1.mock(common_services_1.LambdaService); beforeEach(() => { const mAppsyncClient = ts_mockito_1.instance(mockAppSyncClient); mAppsyncClient.utils = new appsyncUtils_1.AppSyncUtils(); txnService = new transactionService_1.TransactionService(ts_mockito_1.instance(mockDynamodbService), ts_mockito_1.instance(mockEnvService), ts_mockito_1.instance(mockDeviceVerify), ts_mockito_1.instance(mockDeviceService), mAppsyncClient, ts_mockito_1.instance(mockLambdaService)); txnService.transactionDb = ts_mockito_1.instance(mockTransactionDb); }); it('should return empty array if not found any transaction', () => __awaiter(void 0, void 0, void 0, function* () { ts_mockito_1.when(mockTransactionDb.getTransactions).thenReturn(() => Promise.resolve({ Items: [] })); ts_mockito_1.when(mockDeviceVerify.isDeviceTokenValid).thenReturn(() => Promise.resolve(true)); ts_mockito_1.when(mockDeviceService.getEntityUuidByAccessToken).thenReturn(() => Promise.resolve('valid entity uuid')); const txns = yield txnService.getTransactions({ deviceUuid: '00', limit: 10, }, ''); expect(txns.transactions.length).toBe(0); expect(txns.nextToken).toBeUndefined(); })); it('should be able to response transaction as an array', () => __awaiter(void 0, void 0, void 0, function* () { ts_mockito_1.when(mockTransactionDb.getTransactions).thenReturn(() => Promise.resolve({ Items: [{ transactionUuid: '00' }] })); ts_mockito_1.when(mockDeviceVerify.isDeviceTokenValid).thenReturn(() => Promise.resolve(true)); ts_mockito_1.when(mockDeviceService.getEntityUuidByAccessToken).thenReturn(() => Promise.resolve('valid entity uuid')); const txns = yield txnService.getTransactions({ deviceUuid: '00', limit: 10, }, ''); expect(txns.transactions[0].id).toBe('00'); expect(txns.nextToken).toBeUndefined(); })); it('should be able to response nextToken if there are more items', () => __awaiter(void 0, void 0, void 0, function* () { ts_mockito_1.when(mockTransactionDb.getTransactions).thenReturn(() => Promise.resolve({ Items: [{ transactionUuid: '00' }, { transactionUuid: '01' }], LastEvaluatedKey: { id: '01' }, })); ts_mockito_1.when(mockDeviceVerify.isDeviceTokenValid).thenReturn(() => Promise.resolve(true)); ts_mockito_1.when(mockDeviceService.getEntityUuidByAccessToken).thenReturn(() => Promise.resolve('valid entity uuid')); const txns = yield txnService.getTransactions({ deviceUuid: '00', limit: 2, }, ''); expect(txns.transactions.length).toBe(2); expect(txns.transactions[0].id).toBe('00'); expect(txns.transactions[1].id).toBe('01'); expect(txns.nextToken.id).toBe('01'); })); it('should throw error if access token is not valid', () => __awaiter(void 0, void 0, void 0, function* () { ts_mockito_1.when(mockDeviceVerify.isDeviceTokenValid).thenReturn(() => Promise.resolve(false)); ts_mockito_1.when(mockDeviceService.getEntityUuidByAccessToken).thenReturn(() => Promise.resolve('valid entity uuid')); yield expect(txnService.getTransactions({ deviceUuid: '00', limit: 2, }, '')).rejects.toThrowError(Error); })); it('should be able to get transaction based on uuid', () => __awaiter(void 0, void 0, void 0, function* () { ts_mockito_1.when(mockTransactionDb.getTransaction).thenReturn(() => Promise.resolve({ Items: [{ transactionUuid: '00' }], })); const txn = yield txnService.getTransaction('00'); expect(txn.id).toBe('00'); })); it('should throw error if transaction uuid not found', () => __awaiter(void 0, void 0, void 0, function* () { ts_mockito_1.when(mockTransactionDb.getTransaction).thenReturn(() => Promise.resolve({ Items: [], })); yield expect(txnService.getTransaction('00')).rejects.toThrowError(Error); })); it('should response ack to transaction request', () => __awaiter(void 0, void 0, void 0, function* () { const request = { id: uuid_1.v4(), catid: uuid_1.v4(), caid: uuid_1.v4(), iso8583: uuid_1.v4(), }; ts_mockito_1.when(mockLambdaService.lambda).thenReturn({ invoke: jest.fn().mockReturnValue({ promise: jest.fn() }) }); const response = yield txnService.requestTransaction('', request); expect(response).toMatchObject({ status: true }); })); it('should be able to response false if request transaction failed', () => __awaiter(void 0, void 0, void 0, function* () { const request = { id: uuid_1.v4(), catid: uuid_1.v4(), caid: uuid_1.v4(), iso8583: uuid_1.v4(), }; ts_mockito_1.when(mockLambdaService.lambda).thenReturn({ invoke: jest.fn().mockRejectedValue('error') }); const response = yield txnService.requestTransaction('', request); expect(response).toMatchObject({ status: false }); })); it('should handle transaction subscription request', () => __awaiter(void 0, void 0, void 0, function* () { const id = '123'; const response = yield txnService.subscribeTransactionResponse({ args: { id }, }); expect(response.id).toBe(id); })); it('should be able to query transaction based on transactionuuid', () => __awaiter(void 0, void 0, void 0, function* () { ts_mockito_1.when(mockTransactionDb.getTransaction).thenReturn(() => Promise.resolve({ Items: [{ transactionUuid: '00' }], })); const txns = yield txnService.getTransaction('00'); expect(txns.id).toBe('00'); })); it('should be able to listen on dynamodb stream event', () => __awaiter(void 0, void 0, void 0, function* () { const event = [ { eventID: '7f09a29cc64a0c94e020df18d901b859', eventName: 'MODIFY', eventVersion: '1.1', eventSource: 'aws:dynamodb', awsRegion: 'ap-southeast-2', dynamodb: { ApproximateCreationDateTime: 1594608723, Keys: { id: { S: '65c3140c-b6d0-4675-9e7e-fbc4750910cd', }, type: { S: 'transaction.1594608546752', }, }, NewImage: { surchargeAmount: { N: '500', }, scheme: { S: 'AMEX', }, siteName: { S: 'ac1260b5-9b90-49eb-a5fd-3e94b084bc5d', }, type: { S: 'transaction.1594608546752', }, deviceName: { S: 'b6b0abd4-9e7b-4304-8d49-6f24f9b02cc5', }, reference: { S: 'ab2a1b13-08e8-410b-9270-b2b3b07468ac', }, 'aws:rep:updatetime': { N: '1594608723.881001', }, depositDate: { S: '2020-07-13T02:49:06.752Z', }, panToken: { S: '73c5e6d2-2a7e-4ca9-99bf-4a22526b611b', }, id: { S: '65c3140c-b6d0-4675-9e7e-fbc4750910cd', }, 'aws:rep:updateregion': { S: 'ap-southeast-2', }, siteUuid: { S: '5625a830-7009-40d6-abee-12d29b255ee6', }, timestamp: { S: '2020-07-13T02:49:06.752Z', }, par: { S: '0df5d91f-dec7-4631-8727-f2479bf0c6d3', }, saleAmount: { N: '100', }, amount: { N: '200', }, maskedPan: { S: 'masked', }, entityUuid: { S: '08e6c957-b3d8-4453-87ee-0dcc7d4adb70', }, tipAmount: { N: '100', }, 'aws:rep:deleting': { BOOL: false, }, transactionType: { S: 'REFUND', }, feeAmount: { N: '400', }, transactionUuid: { S: 'c18ee19b-1d61-420e-b68b-d9f98b4f34ad', }, taxAmounts: { L: [ { M: { amount: { N: '100', }, name: { S: 'name', }, }, }, ], }, depositUuid: { S: '28fa3831-468c-4c66-aaac-065e54318109', }, status: { S: 'APPROVED', }, }, OldImage: { surchargeAmount: { N: '500', }, scheme: { S: 'AMEX', }, siteName: { S: 'ac1260b5-9b90-49eb-a5fd-3e94b084bc5d', }, type: { S: 'transaction.1594608546752', }, deviceName: { S: 'b6b0abd4-9e7b-4304-8d49-6f24f9b02cc5', }, reference: { S: 'ab2a1b13-08e8-410b-9270-b2b3b07468ac', }, 'aws:rep:updatetime': { N: '1594608547.852001', }, depositDate: { S: '2020-07-13T02:49:06.752Z', }, panToken: { S: '73c5e6d2-2a7e-4ca9-99bf-4a22526b611b', }, id: { S: '65c3140c-b6d0-4675-9e7e-fbc4750910cd', }, 'aws:rep:updateregion': { S: 'ap-southeast-2', }, siteUuid: { S: '5625a830-7009-40d6-abee-12d29b255ee6', }, timestamp: { S: '2020-07-13T02:49:06.752Z', }, par: { S: '0df5d91f-dec7-4631-8727-f2479bf0c6d3', }, saleAmount: { N: '100', }, amount: { N: '200', }, maskedPan: { S: 'masked', }, entityUuid: { S: '08e6c957-b3d8-4453-87ee-0dcc7d4adb70', }, tipAmount: { N: '100', }, 'aws:rep:deleting': { BOOL: false, }, transactionType: { S: 'REFUND', }, feeAmount: { N: '400', }, transactionUuid: { S: 'c18ee19b-1d61-420e-b68b-d9f98b4f34ad', }, taxAmounts: { L: [ { M: { amount: { N: '100', }, name: { S: 'name', }, }, }, ], }, depositUuid: { S: '28fa3831-468c-4c66-aaac-065e54318109', }, status: { S: 'APPROVED', }, }, SequenceNumber: '14394500000000012177044516', SizeBytes: 1613, StreamViewType: 'NEW_AND_OLD_IMAGES', }, eventSourceARN: 'arn:aws:dynamodb:ap-southeast-2:115136697128:table/joeyDevices/stream/2020-07-10T04:42:54.695', }, ]; const mutate = jest.fn(); ts_mockito_1.when(mockAppSyncClient.getAppSyncClient()).thenReturn(Promise.resolve({ mutate })); ts_mockito_1.when(mockDeviceService.getDeviceStatus).thenReturn(() => Promise.resolve("ACTIVE" /* ACTIVE */)); yield txnService.onTransactionStream(event); expect(mutate).toBeCalledTimes(1); })); it('should not publish transaction update event to deivce whose status is not active', () => __awaiter(void 0, void 0, void 0, function* () { const mutate = jest.fn(); ts_mockito_1.when(mockAppSyncClient.getAppSyncClient()).thenReturn(Promise.resolve({ mutate })); ts_mockito_1.when(mockDeviceService.getDeviceStatus).thenReturn(() => Promise.resolve("INACTIVE" /* INACTIVE */)); const event = [ { dynamodb: { ApproximateCreationDateTime: 1594608723, Keys: { id: { S: '65c3140c-b6d0-4675-9e7e-fbc4750910cd', }, type: { S: 'core', }, }, }, }, ]; yield txnService.onTransactionStream(event); expect(mutate).toBeCalledTimes(0); })); it('should not publish transaction update event if the update is not transaction', () => __awaiter(void 0, void 0, void 0, function* () { const event = [ { dynamodb: { ApproximateCreationDateTime: 1594608723, Keys: { id: { S: '65c3140c-b6d0-4675-9e7e-fbc4750910cd', }, type: { S: 'core', }, }, }, }, ]; const mutate = jest.fn(); ts_mockito_1.when(mockAppSyncClient.getAppSyncClient()).thenReturn(Promise.resolve({ mutate })); yield txnService.onTransactionStream(event); expect(mutate).toHaveBeenCalledTimes(0); })); it('should not publish transaction update event if the update doesnt have NewImage ', () => __awaiter(void 0, void 0, void 0, function* () { const event = [ { dynamodb: { ApproximateCreationDateTime: 1594608723, Keys: { id: { S: '65c3140c-b6d0-4675-9e7e-fbc4750910cd', }, type: { S: 'transaction.1234', }, }, OldImage: {}, }, }, ]; const mutate = jest.fn(); ts_mockito_1.when(mockAppSyncClient.getAppSyncClient()).thenReturn(Promise.resolve({ mutate })); yield txnService.onTransactionStream(event); expect(mutate).toHaveBeenCalledTimes(0); })); it('should not publish transaction update event if the update doesnt have NewImage and OldImage ', () => __awaiter(void 0, void 0, void 0, function* () { const event = [ { dynamodb: { ApproximateCreationDateTime: 1594608723, Keys: { id: { S: '65c3140c-b6d0-4675-9e7e-fbc4750910cd', }, type: { S: 'transaction.1234', }, }, }, }, ]; const mutate = jest.fn(); ts_mockito_1.when(mockAppSyncClient.getAppSyncClient()).thenReturn(Promise.resolve({ mutate })); yield txnService.onTransactionStream(event); expect(mutate).toHaveBeenCalledTimes(0); })); it('should be able to response subscribe request', () => __awaiter(void 0, void 0, void 0, function* () { ts_mockito_1.when(mockDeviceVerify.checkAccessToken).thenReturn(() => Promise.resolve()); yield txnService.onTransactionUpdate('access-token'); ts_mockito_1.when(mockDeviceVerify.checkAccessToken).thenReturn(() => Promise.reject(new Error('invalid access token'))); yield expect(txnService.onTransactionUpdate('access-token')).rejects.toThrowError(Error); })); it('should publish transaction update event for new transaction', () => __awaiter(void 0, void 0, void 0, function* () { const event = [ { dynamodb: { ApproximateCreationDateTime: 1594608723, Keys: { id: { S: '65c3140c-b6d0-4675-9e7e-fbc4750910cd', }, type: { S: 'transaction.1234', }, }, NewImage: { id: { S: '123' }, }, }, }, ]; const mutate = jest.fn(); ts_mockito_1.when(mockAppSyncClient.getAppSyncClient()).thenReturn(Promise.resolve({ mutate })); ts_mockito_1.when(mockDeviceService.getDeviceStatus).thenReturn(() => Promise.resolve("ACTIVE" /* ACTIVE */)); yield txnService.onTransactionStream(event); expect(mutate).toHaveBeenCalledTimes(1); })); }); //# sourceMappingURL=transactionService.spec.js.map