diff --git a/integration-tests/subscriptions.test.ts b/integration-tests/subscriptions.test.ts index 9b6fec88..5f3b8850 100644 --- a/integration-tests/subscriptions.test.ts +++ b/integration-tests/subscriptions.test.ts @@ -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) { @@ -31,23 +33,61 @@ 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(); @@ -55,20 +95,9 @@ if (SUBSCRIPTIONS_ENABLED === 'true') { 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; diff --git a/integration-tests/utils.ts b/integration-tests/utils.ts index 34612ec5..17a9e826 100644 --- a/integration-tests/utils.ts +++ b/integration-tests/utils.ts @@ -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 { diff --git a/package.json b/package.json index bc8c1d7d..be47788d 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": { diff --git a/yarn.lock b/yarn.lock index 4b5d3ed0..292478e7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"