Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

fix: update unit tests for subscription reaper #567

Merged
merged 4 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/subscriptions/reaperLambda/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { DynamoDbDataService, DynamoDb } from 'fhir-works-on-aws-persistence-ddb';
import reaperHandler from './subscriptionReaper';

const enableMultitenancy = process.env.ENABLE_MULTI_TENANCY === 'true';
const dbServiceWithTenancy = new DynamoDbDataService(DynamoDb, false, {
enableMultiTenancy: enableMultitenancy,
});
const dbService = new DynamoDbDataService(DynamoDb);

/**
* Custom lambda handler that handles deleting expired subscriptions.
*/
exports.handler = reaperHandler;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
exports.handler = async (event: any) => {
return reaperHandler(dbService, dbServiceWithTenancy);
};
30 changes: 18 additions & 12 deletions src/subscriptions/reaperLambda/subscriptionReaper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import { DynamoDbDataService } from 'fhir-works-on-aws-persistence-ddb';
import { DynamoDbDataService, DynamoDb } from 'fhir-works-on-aws-persistence-ddb';
import reaperHandler from './subscriptionReaper';

jest.mock('fhir-works-on-aws-persistence-ddb');
const dbServiceWithTenancy = new DynamoDbDataService(DynamoDb, false, {
enableMultiTenancy: true,
});
const dbService = new DynamoDbDataService(DynamoDb);

describe('subscriptionReaper', () => {
test('no subscriptions to delete', async () => {
const expectedResponse: {
Expand All @@ -21,9 +25,9 @@ describe('subscriptionReaper', () => {
},
];
const mockGetActiveSubscriptions = jest.fn();
DynamoDbDataService.prototype.getActiveSubscriptions = mockGetActiveSubscriptions;
mockGetActiveSubscriptions.mockResolvedValueOnce(subResource);
const actualResponse = await reaperHandler({});
dbService.getActiveSubscriptions = mockGetActiveSubscriptions;
const actualResponse = await reaperHandler(dbService, dbServiceWithTenancy);
expect(actualResponse).toEqual(expectedResponse);
});

Expand All @@ -41,24 +45,26 @@ describe('subscriptionReaper', () => {
const subResource = [
{
resourceType: 'Subscription',
id: 'sub1',
_id: 'sub1',
status: 'requested',
end: '2021-01-01T00:00:00Z',
_tenantId: 'tenant1',
},
{
resourceType: 'Subscription',
id: 'sub2',
_id: 'sub2',
status: 'requested',
end: '2121-01-01T00:00:00Z',
_tenantId: 'tenant1',
},
];
const mockGetActiveSubscriptions = jest.fn();
const mockDeleteResource = jest.fn();
DynamoDbDataService.prototype.getActiveSubscriptions = mockGetActiveSubscriptions;
DynamoDbDataService.prototype.deleteResource = mockDeleteResource;
mockGetActiveSubscriptions.mockResolvedValueOnce(subResource);
mockDeleteResource.mockResolvedValueOnce([{ success: true, message }]);
const actualResponse = await reaperHandler({});
mockDeleteResource.mockResolvedValueOnce({ success: true, message });
dbService.getActiveSubscriptions = mockGetActiveSubscriptions;
dbServiceWithTenancy.deleteResource = mockDeleteResource;
const actualResponse = await reaperHandler(dbService, dbServiceWithTenancy);
expect(actualResponse).toEqual(expectedResponse);
});

Expand All @@ -75,9 +81,9 @@ describe('subscriptionReaper', () => {
},
];
const mockGetActiveSubscriptions = jest.fn();
DynamoDbDataService.prototype.getActiveSubscriptions = mockGetActiveSubscriptions;
mockGetActiveSubscriptions.mockResolvedValueOnce(subResource);
const actualResponse = await reaperHandler({});
dbService.getActiveSubscriptions = mockGetActiveSubscriptions;
const actualResponse = await reaperHandler(dbService, dbServiceWithTenancy);
expect(actualResponse).toEqual(expectedResponse);
});
});
12 changes: 2 additions & 10 deletions src/subscriptions/reaperLambda/subscriptionReaper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@
* SPDX-License-Identifier: Apache-2.0
*
*/
import { DynamoDbDataService, DynamoDb } from 'fhir-works-on-aws-persistence-ddb';
import { DynamoDbDataService } from 'fhir-works-on-aws-persistence-ddb';

const enableMultiTenancy = process.env.ENABLE_MULTI_TENANCY === 'true';
const dbServiceWithTenancy = new DynamoDbDataService(DynamoDb, false, {
enableMultiTenancy,
});
const dbService = new DynamoDbDataService(DynamoDb);

const reaperHandler = async (event: any) => {
console.log('subscriptionReaper event', event);
const reaperHandler = async (dbService: DynamoDbDataService, dbServiceWithTenancy: DynamoDbDataService) => {
const subscriptions = await dbService.getActiveSubscriptions({});
const currentTime = new Date();
// filter out subscriptions without a defined end time.
// check if subscription is past its end date (ISO format)
// example format of subscriptions: https://www.hl7.org/fhir/subscription-example.json.html
console.log(subscriptions);
return Promise.all(
subscriptions
.filter((s: Record<string, any>) => {
Expand Down