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

test: add tests for tenant isolation of subscriptions #577

Merged
merged 5 commits into from
Feb 24, 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
65 changes: 47 additions & 18 deletions integration-tests/subscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@

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

jest.setTimeout(700_000);

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

if (API_AWS_REGION === undefined) {
Expand All @@ -31,44 +33,71 @@ test('empty test placeholder', () => {
});

if (SUBSCRIPTIONS_ENABLED === 'true') {
if (SUBSCRIPTIONS_ENDPOINT === undefined) {
throw new Error('SUBSCRIPTIONS_ENDPOINT environment variable is not defined');
}
if (SUBSCRIPTIONS_API_KEY === undefined) {
throw new Error('SUBSCRIPTIONS_API_KEY environment variable is not defined');
}
let client: AxiosInstance;

describe('FHIR Subscriptions', () => {
let subscriptionsHelper: SubscriptionsHelper;

beforeAll(() => {
beforeAll(async () => {
if (SUBSCRIPTIONS_NOTIFICATIONS_TABLE === undefined) {
throw new Error('SUBSCRIPTIONS_NOTIFICATIONS_TABLE environment variable is not defined');
}
subscriptionsHelper = new SubscriptionsHelper(SUBSCRIPTIONS_NOTIFICATIONS_TABLE);
client = await getFhirClient();
});

test('test', async () => {
const x = await subscriptionsHelper.getNotifications('/lala');
console.log(x);
});

if (MULTI_TENANCY_ENABLED === 'true') {
test('tenant isolation', async () => {
const clientAnotherTenant = await getFhirClient({ tenant: 'tenant2' });
// tenant 1 creates a subscription
const uuid = v4();
const subResource = randomSubscription(uuid);
const postResult = await client.post('Subscription', subResource);
expect(postResult.status).toEqual(201);
const resourceThatMatchesSubscription = {
resourceType: 'Patient',
name: [
{
given: ['Smith'],
family: 'Smith',
},
],
};
// post matching resource on another tenant
const postPatientResult = await clientAnotherTenant.post('Patient', resourceThatMatchesSubscription);
expect(postPatientResult.status).toEqual(201);
// give 2 minutes for notification to be placed in ddb table
await new Promise((r) => setTimeout(r, 120_000));
// make sure no notification was receieved for first tenant
const notifications = await subscriptionsHelper.getNotifications(
`${uuid}/Patient/${postPatientResult.data.id}`,
);
expect(notifications).toEqual([]);
});
}
});

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 subscriptionResource = randomSubscription(v4());
// make end date 10 seconds in the future so it is caught by the reaper
subscriptionResource.end = new Date(new Date().getTime() + 10000).toISOString();
const postSubResult = await client.post('Subscription', subscriptionResource);
expect(postSubResult.status).toEqual(201); // ensure that the sub resource is created
const subResourceId = postSubResult.data.id;
Expand Down
18 changes: 18 additions & 0 deletions integration-tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,24 @@ export const randomPatient = () => {
};
};

export const randomSubscription = (endpointUUID: string) => {
// we checked if environment variable were defined already
return {
resourceType: 'Subscription',
status: 'requested',
// get a time 10 minutes (600_000 ms) in the future
end: new Date(new Date().getTime() + 600_000).toISOString(),
reason: 'Monitor Patients with name Smith',
criteria: 'Patient?name=Smith',
channel: {
type: 'rest-hook',
endpoint: `${process.env.SUBSCRIPTIONS_ENDPOINT}/${endpointUUID}`,
payload: 'application/fhir+json',
header: [`x-api-key: ${process.env.SUBSCRIPTIONS_API_KEY}`],
},
};
};

export const randomChainedParamBundle = () => {
const chance = new Chance();
return {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@types/jsonwebtoken": "^8.5.4",
"@types/node": "^12",
"@types/sinon": "^9.0.0",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"chance": "^1.1.7",
Expand All @@ -77,6 +78,7 @@
"ts-jest": "^26.4.4",
"ts-node": "^10.3.0",
"typescript": "^4.1.3",
"uuid": "^8.3.2",
"wait-for-expect": "^3.0.2"
},
"resolutions": {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,11 @@
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.1.tgz#8f80dd965ad81f3e1bc26d6f5c727e132721ff40"
integrity sha512-Y0K95ThC3esLEYD6ZuqNek29lNX2EM1qxV8y2FTLUB0ff5wWrk7az+mLrnNFUnaXcgKye22+sFBRXOgpPILZNg==

"@types/uuid@^8.3.4":
version "8.3.4"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.4.tgz#bd86a43617df0594787d38b735f55c805becf1bc"
integrity sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==

"@types/yargs-parser@*":
version "20.2.1"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.1.tgz#3b9ce2489919d9e4fea439b76916abc34b2df129"
Expand Down