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

test: add Subscription reaper tests #575

Merged
merged 7 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
48 changes: 45 additions & 3 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 { SubscriptionsHelper } from './SubscriptionsHelper';
import { getFhirClient, waitForMs } from './utils';

jest.setTimeout(300_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,
MULTI_TENANCY_ENABLED,
} = 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,39 @@ if (SUBSCRIPTIONS_ENABLED === 'true') {
console.log(x);
});
});

let client: AxiosInstance;
describe('test subscription creation and deletion', () => {
beforeAll(async () => {
client = await getFhirClient({ tenant: MULTI_TENANCY_ENABLED ? 'tenant1' : undefined });
ssvegaraju marked this conversation as resolved.
Show resolved Hide resolved
});

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 for Organization 123',
ssvegaraju marked this conversation as resolved.
Show resolved Hide resolved
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 4 min until reaper has had a chance to run
// (waiting 5 min would hit the timeout value above)
ssvegaraju marked this conversation as resolved.
Show resolved Hide resolved
// it may be better to do a loop checking every minute or so or increasing the timeout
await waitForMs(4 * 60 * 1000);
await expect(client.get(`Subscription/${subResourceId}`)).rejects.toMatchObject({
response: { status: 404 },
});
carvantes marked this conversation as resolved.
Show resolved Hide resolved
});
});
}
4 changes: 4 additions & 0 deletions integration-tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,7 @@ export const getResourcesFromBundleResponse = (
});
return resourceTypeToExpectedResource;
};

export const waitForMs = (milliseconds: number) => {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
};
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;
ssvegaraju marked this conversation as resolved.
Show resolved Hide resolved
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