-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget_status.test.ts
More file actions
71 lines (56 loc) · 2.23 KB
/
get_status.test.ts
File metadata and controls
71 lines (56 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { S3Client } from "@aws-sdk/client-s3";
import { DBHealthcheck } from "@internal/datastore/src";
import pino from "pino";
import { Deps } from "../../config/deps";
import { makeApiGwEvent } from "./utils/test-utils";
import { mockDeep } from "jest-mock-extended";
import { Context } from "aws-lambda";
import { createGetStatusHandler } from "../get-status";
describe('API Lambda handler', () => {
it('passes if S3 and DynamoDB are available', async() => {
const event = makeApiGwEvent({path: '/_status',
headers: undefined
});
const getLetterDataHandler = createGetStatusHandler(getMockedDeps());
const result = await getLetterDataHandler(event, mockDeep<Context>(), jest.fn());
expect(result).toEqual({
statusCode: 200,
body: '{}',
});
});
it('fails if S3 is unavailable', async() => {
const mockedDeps = getMockedDeps();
mockedDeps.s3Client.send = jest.fn().mockRejectedValue(new Error('unexpected error'));
const event = makeApiGwEvent({path: '/_status',
headers: undefined
});
const getLetterDataHandler = createGetStatusHandler(mockedDeps);
const result = await getLetterDataHandler(event, mockDeep<Context>(), jest.fn());
expect(result).toEqual(expect.objectContaining({
statusCode: 500
}));
});
it('fails if DynamoDB is unavailable', async() => {
const mockedDeps = getMockedDeps();
mockedDeps.dbHealthcheck.check = jest.fn().mockRejectedValue(new Error('unexpected error'));
const event = makeApiGwEvent({path: '/_status',
headers: {'Nhsd-Correlation-Id': 'correlationId'}
});
const getLetterDataHandler = createGetStatusHandler(mockedDeps);
const result = await getLetterDataHandler(event, mockDeep<Context>(), jest.fn());
expect(result).toEqual(expect.objectContaining({
statusCode: 500
}));
});
function getMockedDeps(): jest.Mocked<Deps> {
return {
s3Client: { send: jest.fn()} as unknown as S3Client,
dbHealthcheck: {check: jest.fn()} as unknown as DBHealthcheck,
logger: { info: jest.fn(), error: jest.fn() } as unknown as pino.Logger,
env: {
SUPPLIER_ID_HEADER: 'nhsd-supplier-id',
APIM_CORRELATION_HEADER: 'nhsd-correlation-id'
}
} as Deps;
}
});