Skip to content

Commit 2978aad

Browse files
committed
add api handler tests for get-mi
1 parent d6dfffb commit 2978aad

2 files changed

Lines changed: 166 additions & 0 deletions

File tree

lambdas/api-handler/src/contracts/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export enum ApiErrorStatus {
2929

3030
export enum ApiErrorDetail {
3131
NotFoundLetterId = "No resource found with that ID",
32+
NotFoundMiId = "No resource found with that ID",
3233
InvalidRequestMissingBody = "The request is missing the body",
3334
InvalidRequestMissingLetterIdPathParameter = "The request is missing the letter id path parameter",
3435
InvalidRequestLetterIdsMismatch = "The letter ID in the request body does not match the letter ID path parameter",
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import { Context } from "aws-lambda";
2+
import { mockDeep } from "jest-mock-extended";
3+
import pino from "pino";
4+
import { MIRepository } from "@internal/datastore/src";
5+
import { gettMI as getMiOperation } from "../../services/mi-operations";
6+
import { makeApiGwEvent } from "./utils/test-utils";
7+
import { ApiErrorDetail } from "../../contracts/errors";
8+
import NotFoundError from "../../errors/not-found-error";
9+
import { Deps } from "../../config/deps";
10+
import { EnvVars } from "../../config/env";
11+
import createGetMIHandler from "../get-mi";
12+
13+
jest.mock("../../services/mi-operations");
14+
15+
describe("API Lambda handler", () => {
16+
const mockedDeps: jest.Mocked<Deps> = {
17+
miRepo: {} as unknown as MIRepository,
18+
logger: { info: jest.fn(), error: jest.fn() } as unknown as pino.Logger,
19+
env: {
20+
SUPPLIER_ID_HEADER: "nhsd-supplier-id",
21+
APIM_CORRELATION_HEADER: "nhsd-correlation-id",
22+
DOWNLOAD_URL_TTL_SECONDS: 1,
23+
} as unknown as EnvVars,
24+
} as Deps;
25+
26+
beforeEach(() => {
27+
jest.clearAllMocks();
28+
jest.resetModules();
29+
});
30+
31+
it("returns 200 OK and the letter status", async () => {
32+
const mockedGetMiById = getMiOperation as jest.Mock;
33+
mockedGetMiById.mockResolvedValue({
34+
data:{
35+
id: "id1",
36+
type: "ManagementInformation",
37+
attributes: {
38+
lineItem: "envelope-business-standard",
39+
timestamp: "2023-11-17T14:27:51.413Z",
40+
quantity: 22,
41+
specificationId: "spec1",
42+
groupId: "group1",
43+
stockRemaining: 20_000,
44+
},
45+
},
46+
});
47+
48+
const event = makeApiGwEvent({
49+
path: "/mi/id1",
50+
headers: {
51+
"nhsd-supplier-id": "supplier1",
52+
"nhsd-correlation-id": "correlationId",
53+
"x-request-id": "requestId",
54+
},
55+
pathParameters: { id: "id1" },
56+
});
57+
58+
const getMi = createGetMiHandler(mockedDeps);
59+
const result = await getMi(event, mockDeep<Context>(), jest.fn());
60+
61+
const expected = {
62+
data:{
63+
id: "id1",
64+
type: "ManagementInformation",
65+
attributes: {
66+
lineItem: "envelope-business-standard",
67+
timestamp: "2023-11-17T14:27:51.413Z",
68+
quantity: 22,
69+
specificationId: "spec1",
70+
groupId: "group1",
71+
stockRemaining: 20_000,
72+
},
73+
},
74+
};
75+
76+
expect(result).toEqual({
77+
statusCode: 200,
78+
body: JSON.stringify(expected),
79+
});
80+
});
81+
82+
it("returns 404 Not Found when MI matching id is not found", async () => {
83+
const mockedGetMiById = getMiOperation as jest.Mock;
84+
85+
mockedGetMiById.mockImplementation(() => {
86+
throw new NotFoundError(ApiErrorDetail.NotFoundMiId);
87+
});
88+
89+
const event = makeApiGwEvent({
90+
path: "/mi/id1",
91+
headers: {
92+
"nhsd-supplier-id": "supplier1",
93+
"nhsd-correlation-id": "correlationId",
94+
"x-request-id": "requestId",
95+
},
96+
pathParameters: { id: "id1" },
97+
});
98+
99+
const getMi = createGetMiHandler(mockedDeps);
100+
const result = await getMi(event, mockDeep<Context>(), jest.fn());
101+
102+
expect(result).toEqual(
103+
expect.objectContaining({
104+
statusCode: 404,
105+
}),
106+
);
107+
});
108+
109+
it("returns 500 when correlation id is missing from header", async () => {
110+
const event = makeApiGwEvent({
111+
path: "/mi/id1",
112+
headers: { "nhsd-supplier-id": "supplier1", "x-request-id": "requestId" },
113+
pathParameters: { id: "id1" },
114+
});
115+
116+
const getMi = createGetMiHandler(mockedDeps);
117+
const result = await getMi(event, mockDeep<Context>(), jest.fn());
118+
119+
expect(result).toEqual(
120+
expect.objectContaining({
121+
statusCode: 500,
122+
}),
123+
);
124+
});
125+
126+
it("returns 500 when supplier id is missing from header", async () => {
127+
const event = makeApiGwEvent({
128+
path: "/mi/id1",
129+
headers: {
130+
"nhsd-correlation-id": "correlationId",
131+
"x-request-id": "requestId",
132+
},
133+
pathParameters: { id: "id1" },
134+
});
135+
136+
const getMi = createGetMiHandler(mockedDeps);
137+
const result = await getMi(event, mockDeep<Context>(), jest.fn());
138+
139+
expect(result).toEqual(
140+
expect.objectContaining({
141+
statusCode: 500,
142+
}),
143+
);
144+
});
145+
146+
it("returns 400 when letter id is missing from path", async () => {
147+
const event = makeApiGwEvent({
148+
path: "/mi/id1",
149+
headers: {
150+
"nhsd-supplier-id": "supplier1",
151+
"nhsd-correlation-id": "correlationId",
152+
"x-request-id": "requestId",
153+
},
154+
});
155+
156+
const getMi = createGetLetterHandler(mockedDeps);
157+
const result = await getMi(event, mockDeep<Context>(), jest.fn());
158+
159+
expect(result).toEqual(
160+
expect.objectContaining({
161+
statusCode: 400,
162+
}),
163+
);
164+
});
165+
});

0 commit comments

Comments
 (0)