diff --git a/packages/getMyPrescriptions/src/getMyPrescriptions.ts b/packages/getMyPrescriptions/src/getMyPrescriptions.ts index d821a5aae..20bbe479f 100644 --- a/packages/getMyPrescriptions/src/getMyPrescriptions.ts +++ b/packages/getMyPrescriptions/src/getMyPrescriptions.ts @@ -33,6 +33,36 @@ const lambdaHandler = async (event: APIGatewayProxyEvent): Promise { + process.env.SpinePublicCertificate = "ChangeMe" + process.env.SpinePrivateKey = "ChangeMe" + process.env.SpineCAChain = "ChangeMe" + + mock.onGet("https://live/mm/patientfacingprescriptions").reply(500, {resourceType: "Bundle"}) + const event: APIGatewayProxyEvent = JSON.parse(exampleEvent) + const result: APIGatewayProxyResult = await handler(event, dummyContext) + + expect(result.statusCode).toBe(500) + expect(result.headers).toEqual({ + "Content-Type": "application/fhir+json", + "Cache-Control": "no-cache" + }) + expect(JSON.parse(result.body)).toEqual(responseNotConfCertStatus500) + }) }) export {} diff --git a/packages/spineClient/src/live-spine-client.ts b/packages/spineClient/src/live-spine-client.ts index 7d1a435db..5ed06df2e 100644 --- a/packages/spineClient/src/live-spine-client.ts +++ b/packages/spineClient/src/live-spine-client.ts @@ -116,4 +116,13 @@ export class LiveSpineClient implements SpineClient { return serviceHealthCheck(process.env.healthCheckUrl, logger, new Agent()) } } + + isCertificateConfigured(): boolean { + // Check if the required certificate-related environment variables are defined + return ( + process.env.SpinePublicCertificate !== "ChangeMe" && + process.env.SpinePrivateKey !== "ChangeMe" && + process.env.SpineCAChain !== "ChangeMe" + ) + } } diff --git a/packages/spineClient/src/sandbox-spine-client.ts b/packages/spineClient/src/sandbox-spine-client.ts index 377f4489d..42e1c4e6b 100644 --- a/packages/spineClient/src/sandbox-spine-client.ts +++ b/packages/spineClient/src/sandbox-spine-client.ts @@ -15,4 +15,9 @@ export class SandboxSpineClient implements SpineClient { // This is not implemented as sandbox lambda does not use this code throw new Error("INTERACTION_NOT_SUPPORTED_BY_SANDBOX") } + + isCertificateConfigured(): boolean { + // In the sandbox environment, assume the certificate is always configured + return true + } } diff --git a/packages/spineClient/src/spine-client.ts b/packages/spineClient/src/spine-client.ts index a02bcda69..235b3aceb 100644 --- a/packages/spineClient/src/spine-client.ts +++ b/packages/spineClient/src/spine-client.ts @@ -8,6 +8,7 @@ import {AxiosResponse} from "axios" export interface SpineClient { getStatus(logger: Logger): Promise getPrescriptions(inboundHeaders: APIGatewayProxyEventHeaders, logger: Logger): Promise + isCertificateConfigured(): boolean } export function createSpineClient(): SpineClient { diff --git a/packages/statusLambda/src/statusLambda.ts b/packages/statusLambda/src/statusLambda.ts index e96201e0a..7102a0bf7 100644 --- a/packages/statusLambda/src/statusLambda.ts +++ b/packages/statusLambda/src/statusLambda.ts @@ -31,11 +31,29 @@ const lambdaHandler = async (event: APIGatewayProxyEvent): Promise { + process.env = {...originalEnv} mock.reset() }) @@ -76,6 +78,19 @@ describe("Unit test for status check", function () { }) }) + it("checks if the certificate is always configured for the sandbox", async () => { + process.env.TargetSpineServer = "sandbox" + process.env.SpinePublicCertificate = "ChangeMe" + process.env.SpinePrivateKey = "ChangeMe" + process.env.SpineCAChain = "ChangeMe" + + const result: APIGatewayProxyResult = await handler(mockAPIGatewayProxyEvent, dummyContext) + + expect(result.statusCode).toEqual(200) + const result_body = JSON.parse(result.body) + expect(result_body).not.toHaveProperty("message") + }) + it("returns success when spine check succeeds", async () => { mock.onGet("https://live/healthcheck").reply(200, {}) process.env.TargetSpineServer = "live" @@ -143,4 +158,73 @@ describe("Unit test for status check", function () { expect(result_body.spineStatus.timeout).toEqual("true") expect(result_body.spineStatus.responseCode).toEqual(500) }) + + it("returns success when Spine check succeeds and the certificate is not configured", async () => { + mock.onGet("https://live/healthcheck").reply(200, {}) + process.env.TargetSpineServer = "live" + process.env.SpinePublicCertificate = "ChangeMe" + process.env.SpinePrivateKey = "ChangeMe" + process.env.SpineCAChain = "ChangeMe" + + const result: APIGatewayProxyResult = await handler(mockAPIGatewayProxyEvent, dummyContext) + + expect(result.statusCode).toEqual(200) + const result_body = JSON.parse(result.body) + expect(result_body.status).toEqual("pass") + expect(result_body.message).toEqual("Spine certificate is not configured") + }) + + it("returns success when Spine check succeeds without SpinePublicCertificate", async () => { + mock.onGet("https://live/healthcheck").reply(200, {}) + process.env.TargetSpineServer = "live" + process.env.SpinePublicCertificate = "ChangeMe" + + const result: APIGatewayProxyResult = await handler(mockAPIGatewayProxyEvent, dummyContext) + + expect(result.statusCode).toEqual(200) + const result_body = JSON.parse(result.body) + expect(result_body.status).toEqual("pass") + expect(result_body.message).toEqual("Spine certificate is not configured") + }) + + it("returns success when Spine check succeeds without SpinePrivateKey", async () => { + mock.onGet("https://live/healthcheck").reply(200, {}) + process.env.TargetSpineServer = "live" + process.env.SpinePrivateKey = "ChangeMe" + + const result: APIGatewayProxyResult = await handler(mockAPIGatewayProxyEvent, dummyContext) + + expect(result.statusCode).toEqual(200) + const result_body = JSON.parse(result.body) + expect(result_body.status).toEqual("pass") + expect(result_body.message).toEqual("Spine certificate is not configured") + }) + + it("returns success when Spine check succeeds without SpineCAChain", async () => { + mock.onGet("https://live/healthcheck").reply(200, {}) + process.env.TargetSpineServer = "live" + process.env.SpineCAChain = "ChangeMe" + + const result: APIGatewayProxyResult = await handler(mockAPIGatewayProxyEvent, dummyContext) + + expect(result.statusCode).toEqual(200) + const result_body = JSON.parse(result.body) + expect(result_body.status).toEqual("pass") + expect(result_body.message).toEqual("Spine certificate is not configured") + }) + + it("returns failure when Spine check fails and the certificate is configured", async () => { + mock.onGet("https://live/healthcheck").reply(500, {}) + process.env.TargetSpineServer = "live" + + const result: APIGatewayProxyResult = await handler(mockAPIGatewayProxyEvent, dummyContext) + + expect(result.statusCode).toEqual(200) + const result_body = JSON.parse(result.body) + expect(result_body).not.toHaveProperty("message") + expect(result_body.status).toEqual("error") + expect(result_body.spineStatus.status).toEqual("error") + expect(result_body.spineStatus.timeout).toEqual("false") + expect(result_body.spineStatus.responseCode).toEqual(500) + }) })