Skip to content

Commit

Permalink
AEA-3508 return top level status on status endpoint (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-nhs authored Aug 31, 2023
1 parent 1568c3e commit 2f3f63f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 61 deletions.
7 changes: 5 additions & 2 deletions packages/spineClient/src/live-spine-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ export class LiveSpineClient implements SpineClient {
}

async getStatus(logger: Logger): Promise<StatusCheckResponse> {
const url = this.getSpineEndpoint("healthcheck")
return serviceHealthCheck(url, logger, this.httpsAgent)
if (process.env.healthCheckUrl === undefined) {
return serviceHealthCheck(this.getSpineEndpoint("healthcheck"), logger, this.httpsAgent)
} else {
return serviceHealthCheck(process.env.healthCheckUrl, logger, new Agent())
}
}
}
1 change: 1 addition & 0 deletions packages/statusLambda/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayPro
body: JSON.stringify({
commitId: commitId,
versionNumber: versionNumber,
status: spineStatus.status,
spineStatus: spineStatus
}),
headers: {
Expand Down
150 changes: 91 additions & 59 deletions packages/statusLambda/tests/test-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,29 @@
import {APIGatewayProxyEvent, APIGatewayProxyResult} from "aws-lambda"
import {APIGatewayProxyResult} from "aws-lambda"
import {handler} from "../src/app"
import {expect, describe, it} from "@jest/globals"
import {ContextExamples} from "@aws-lambda-powertools/commons"
import {Logger} from "@aws-lambda-powertools/logger"
import MockAdapter from "axios-mock-adapter"
import axios from "axios"
import {mockAPIGatewayProxyEvent} from "@prescriptionsforpatients_common/testing"

const mock = new MockAdapter(axios)

const dummyContext = ContextExamples.helloworldContext
const mockEvent: APIGatewayProxyEvent = {
httpMethod: "get",
body: "",
headers: {
"nhsd-correlation-id": "test-request-id.test-correlation-id.rrt-5789322914740101037-b-aet2-20145-482635-2",
"x-request-id": "test-request-id",
"nhsd-request-id": "test-request-id",
"x-correlation-id": "test-correlation-id"
},
isBase64Encoded: false,
multiValueHeaders: {},
multiValueQueryStringParameters: {},
path: "/hello",
pathParameters: {},
queryStringParameters: {},
requestContext: {
accountId: "123456789012",
apiId: "1234",
authorizer: {},
httpMethod: "get",
identity: {
accessKey: "",
accountId: "",
apiKey: "",
apiKeyId: "",
caller: "",
clientCert: {
clientCertPem: "",
issuerDN: "",
serialNumber: "",
subjectDN: "",
validity: {notAfter: "", notBefore: ""}
},
cognitoAuthenticationProvider: "",
cognitoAuthenticationType: "",
cognitoIdentityId: "",
cognitoIdentityPoolId: "",
principalOrgId: "",
sourceIp: "",
user: "",
userAgent: "",
userArn: ""
},
path: "/hello",
protocol: "HTTP/1.1",
requestId: "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
requestTimeEpoch: 1428582896000,
resourceId: "123456",
resourcePath: "/hello",
stage: "dev"
},
resource: "",
stageVariables: {}
}

describe("Unit test for status check", function () {
afterEach(() => {
mock.reset()
})

it("returns commit id from environment", async () => {
process.env.COMMIT_ID = "test_commit_id"
process.env.TargetSpineServer = "sandbox"

const result: APIGatewayProxyResult = (await handler(mockEvent, dummyContext)) as APIGatewayProxyResult
const result: APIGatewayProxyResult = (await handler(
mockAPIGatewayProxyEvent,
dummyContext
)) as APIGatewayProxyResult

expect(result.statusCode).toEqual(200)
expect(JSON.parse(result.body)).toMatchObject({
Expand All @@ -77,7 +35,10 @@ describe("Unit test for status check", function () {
process.env.VERSION_NUMBER = "test_version_number"
process.env.TargetSpineServer = "sandbox"

const result: APIGatewayProxyResult = (await handler(mockEvent, dummyContext)) as APIGatewayProxyResult
const result: APIGatewayProxyResult = (await handler(
mockAPIGatewayProxyEvent,
dummyContext
)) as APIGatewayProxyResult

expect(result.statusCode).toEqual(200)
expect(JSON.parse(result.body)).toMatchObject({
Expand All @@ -88,7 +49,7 @@ describe("Unit test for status check", function () {
it("appends trace id's to the logger", async () => {
const mockAppendKeys = jest.spyOn(Logger.prototype, "appendKeys")

await handler(mockEvent, dummyContext)
await handler(mockAPIGatewayProxyEvent, dummyContext)

expect(mockAppendKeys).toHaveBeenCalledWith({
"nhsd-correlation-id": "test-request-id.test-correlation-id.rrt-5789322914740101037-b-aet2-20145-482635-2",
Expand All @@ -103,12 +64,83 @@ describe("Unit test for status check", function () {
process.env.COMMIT_ID = "test_commit_id"
process.env.TargetSpineServer = "sandbox"

const result: APIGatewayProxyResult = (await handler(mockEvent, dummyContext)) as APIGatewayProxyResult
const result: APIGatewayProxyResult = (await handler(
mockAPIGatewayProxyEvent,
dummyContext
)) as APIGatewayProxyResult

const headers = result.headers

expect(headers).toMatchObject({
"Cache-Control": "no-cache"
})
})

it("returns success when spine check succeeds", async () => {
mock.onGet("https://live/healthcheck").reply(200, {})
process.env.TargetSpineServer = "live"

const result: APIGatewayProxyResult = (await handler(
mockAPIGatewayProxyEvent,
dummyContext
)) as APIGatewayProxyResult

expect(result.statusCode).toEqual(200)
const result_body = JSON.parse(result.body)
expect(result_body.status).toEqual("pass")
expect(result_body.spineStatus.status).toEqual("pass")
expect(result_body.spineStatus.timeout).toEqual("false")
expect(result_body.spineStatus.responseCode).toEqual(200)
})

it("returns failure when spine check fails", async () => {
mock.onGet("https://live/healthcheck").reply(500, {})
process.env.TargetSpineServer = "live"

const result: APIGatewayProxyResult = (await handler(
mockAPIGatewayProxyEvent,
dummyContext
)) as APIGatewayProxyResult

expect(result.statusCode).toEqual(200)
const result_body = JSON.parse(result.body)
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)
})

it("returns failure when spine check has network error", async () => {
mock.onGet("https://live/healthcheck").networkError()
process.env.TargetSpineServer = "live"

const result: APIGatewayProxyResult = (await handler(
mockAPIGatewayProxyEvent,
dummyContext
)) as APIGatewayProxyResult

expect(result.statusCode).toEqual(200)
const result_body = JSON.parse(result.body)
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)
})

it("returns failure when spine check has timeout", async () => {
mock.onGet("https://live/healthcheck").timeout()
process.env.TargetSpineServer = "live"

const result: APIGatewayProxyResult = (await handler(
mockAPIGatewayProxyEvent,
dummyContext
)) as APIGatewayProxyResult

expect(result.statusCode).toEqual(200)
const result_body = JSON.parse(result.body)
expect(result_body.status).toEqual("error")
expect(result_body.spineStatus.status).toEqual("error")
expect(result_body.spineStatus.timeout).toEqual("true")
expect(result_body.spineStatus.responseCode).toEqual(500)
})
})

0 comments on commit 2f3f63f

Please sign in to comment.