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

Commit

Permalink
test: add Subscription reaper tests (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssvegaraju authored Feb 22, 2022
1 parent 71218e9 commit 4732749
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
54 changes: 50 additions & 4 deletions integration-tests/subscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@
*/

import * as AWS from 'aws-sdk';
import { AxiosInstance } from 'axios';
import waitForExpect from 'wait-for-expect';
import { SubscriptionsHelper } from './SubscriptionsHelper';
import { getFhirClient } from './utils';

jest.setTimeout(300_000);
jest.setTimeout(700_000);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { SUBSCRIPTIONS_ENABLED, SUBSCRIPTIONS_NOTIFICATIONS_TABLE, SUBSCRIPTIONS_ENDPOINT, API_AWS_REGION } =
process.env;
const {
SUBSCRIPTIONS_ENABLED,
SUBSCRIPTIONS_NOTIFICATIONS_TABLE,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
SUBSCRIPTIONS_ENDPOINT,
API_AWS_REGION,
} = process.env;

if (API_AWS_REGION === undefined) {
throw new Error('API_AWS_REGION environment variable is not defined');
Expand Down Expand Up @@ -39,4 +46,43 @@ if (SUBSCRIPTIONS_ENABLED === 'true') {
console.log(x);
});
});

let client: AxiosInstance;
describe('test subscription creation and deletion', () => {
beforeAll(async () => {
client = await getFhirClient();
});

test('creation of almost expiring subscription should be deleted by reaper', async () => {
// OPERATE
const subscriptionResource = {
resourceType: 'Subscription',
status: 'requested',
// get a time 10 seconds (10000 ms) in the future
end: new Date(new Date().getTime() + 10000).toISOString(),
reason: 'Monitor Patients with name Smith',
criteria: 'Patient?name=Smith',
channel: {
type: 'rest-hook',
endpoint: 'https://customer-endpoint.com',
payload: 'application/fhir+json',
header: ['Authorization: Bearer secret-token-abc-123'],
},
};
const postSubResult = await client.post('Subscription', subscriptionResource);
expect(postSubResult.status).toEqual(201); // ensure that the sub resource is created
const subResourceId = postSubResult.data.id;
// wait until reaper has had a chance to run
await waitForExpect(
async () => {
console.log(`Checking if Subscription/${subResourceId} has already been deleted`);
await expect(client.get(`Subscription/${subResourceId}`)).rejects.toMatchObject({
response: { status: 404 },
});
},
360_000, // check for 6 minutes
30_000, // every 30 seconds
);
});
});
}
2 changes: 1 addition & 1 deletion src/subscriptions/reaperLambda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ const dbService = new DynamoDbDataService(DynamoDb);
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
exports.handler = async (event: any) => {
return reaperHandler(dbService, dbServiceWithTenancy);
return reaperHandler(dbService, dbServiceWithTenancy, enableMultitenancy);
};
6 changes: 3 additions & 3 deletions src/subscriptions/reaperLambda/subscriptionReaper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('subscriptionReaper', () => {
const mockGetActiveSubscriptions = jest.fn();
mockGetActiveSubscriptions.mockResolvedValueOnce(subResource);
dbService.getActiveSubscriptions = mockGetActiveSubscriptions;
const actualResponse = await reaperHandler(dbService, dbServiceWithTenancy);
const actualResponse = await reaperHandler(dbService, dbServiceWithTenancy, false);
expect(actualResponse).toEqual(expectedResponse);
});

Expand Down Expand Up @@ -64,7 +64,7 @@ describe('subscriptionReaper', () => {
mockDeleteResource.mockResolvedValueOnce({ success: true, message });
dbService.getActiveSubscriptions = mockGetActiveSubscriptions;
dbServiceWithTenancy.deleteResource = mockDeleteResource;
const actualResponse = await reaperHandler(dbService, dbServiceWithTenancy);
const actualResponse = await reaperHandler(dbService, dbServiceWithTenancy, true);
expect(actualResponse).toEqual(expectedResponse);
});

Expand All @@ -83,7 +83,7 @@ describe('subscriptionReaper', () => {
const mockGetActiveSubscriptions = jest.fn();
mockGetActiveSubscriptions.mockResolvedValueOnce(subResource);
dbService.getActiveSubscriptions = mockGetActiveSubscriptions;
const actualResponse = await reaperHandler(dbService, dbServiceWithTenancy);
const actualResponse = await reaperHandler(dbService, dbServiceWithTenancy, false);
expect(actualResponse).toEqual(expectedResponse);
});
});
12 changes: 9 additions & 3 deletions src/subscriptions/reaperLambda/subscriptionReaper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
*/
import { DynamoDbDataService } from 'fhir-works-on-aws-persistence-ddb';

const reaperHandler = async (dbService: DynamoDbDataService, dbServiceWithTenancy: DynamoDbDataService) => {
const reaperHandler = async (
dbService: DynamoDbDataService,
dbServiceWithTenancy: DynamoDbDataService,
multiTenancyEnabled: boolean,
) => {
const subscriptions = await dbService.getActiveSubscriptions({});
const currentTime = new Date();
// filter out subscriptions without a defined end time.
Expand All @@ -26,10 +30,12 @@ const reaperHandler = async (dbService: DynamoDbDataService, dbServiceWithTenanc
})
.map(async (subscription) => {
// delete the subscription as it has reached its end time
// multi-tenant deployments have the .id as {tenantid}|{id}
// eslint-disable-next-line no-underscore-dangle
const id = multiTenancyEnabled ? subscription._id : subscription.id;
return dbServiceWithTenancy.deleteResource({
resourceType: subscription.resourceType,
// eslint-disable-next-line no-underscore-dangle
id: subscription._id,
id,
// _tenantId is an internal field, and getActiveSubscriptions returns the raw Record<string, any>
// eslint-disable-next-line no-underscore-dangle
tenantId: subscription._tenantId,
Expand Down

0 comments on commit 4732749

Please sign in to comment.