-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpost-mi.test.ts
More file actions
203 lines (163 loc) · 6.31 KB
/
post-mi.test.ts
File metadata and controls
203 lines (163 loc) · 6.31 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { Context } from "aws-lambda";
import { mockDeep } from "jest-mock-extended";
import { makeApiGwEvent } from "./utils/test-utils";
import { PostMIRequest, PostMIResponse } from "../../contracts/mi";
import * as miService from '../../services/mi-operations';
import pino from 'pino';
import { MIRepository } from "@internal/datastore/src";
import { Deps } from "../../config/deps";
import { EnvVars } from "../../config/env";
import { createPostMIHandler } from "../post-mi";
jest.mock('../../services/mi-operations');
const postMIRequest : PostMIRequest = {
data: {
type: 'ManagementInformation',
attributes: {
lineItem: 'envelope-business-standard',
timestamp: '2023-11-17T14:27:51.413Z',
quantity: 22,
specificationId: 'spec1',
groupId: 'group1',
stockRemaining: 20000
}
}
};
const requestBody = JSON.stringify(postMIRequest, null, 2);
const postMIResponse : PostMIResponse = {
data: {
id: 'id1',
...postMIRequest.data
}
};
const mockedPostMIOperation = jest.mocked(miService.postMI);
beforeEach(() => {
jest.clearAllMocks();
});
describe('postMI API Handler', () => {
const mockedDeps: jest.Mocked<Deps> = {
miRepo: {} as unknown as MIRepository,
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',
DOWNLOAD_URL_TTL_SECONDS: 1
} as unknown as EnvVars
} as Deps;
it('returns 200 OK with updated resource', async () => {
const event = makeApiGwEvent({
path: '/mi',
body: requestBody,
headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}
});
mockedPostMIOperation.mockResolvedValue(postMIResponse);
const postMI = createPostMIHandler(mockedDeps);
const result = await postMI(event, mockDeep<Context>(), jest.fn());
expect(result).toEqual({
statusCode: 201,
body: JSON.stringify(postMIResponse, null, 2)
});
});
it('rejects invalid timestamps', async() => {
const modifiedRequest = JSON.parse(requestBody);
modifiedRequest['data']['attributes']['timestamp'] = '2025-02-31T00:00:00Z';
const event = makeApiGwEvent({
path: '/mi',
body: JSON.stringify(modifiedRequest),
headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}
});
const postMI = createPostMIHandler(mockedDeps);
const result = await postMI(event, mockDeep<Context>(), jest.fn());
expect(result).toEqual(expect.objectContaining({
statusCode: 400
}));
});
it('returns 400 Bad Request when there is no body', async () => {
const event = makeApiGwEvent({
path: '/mi',
headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}
});
const postMI = createPostMIHandler(mockedDeps);
const result = await postMI(event, mockDeep<Context>(), jest.fn());
expect(result).toEqual(expect.objectContaining({
statusCode: 400
}));
});
it('returns 500 Internal Error when error is thrown by service', async () => {
const event = makeApiGwEvent({
path: '/mi',
body: requestBody,
pathParameters: {id: 'id1'},
headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}
});
mockedPostMIOperation.mockRejectedValue(new Error());
const postMI = createPostMIHandler(mockedDeps);
const result = await postMI(event, mockDeep<Context>(), jest.fn());
expect(result).toEqual(expect.objectContaining({
statusCode: 500
}));
});
it('returns 500 Bad Request when supplier id is missing', async () => {
const event = makeApiGwEvent({
path: '/mi',
body: requestBody,
headers: {'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}
});
const postMI = createPostMIHandler(mockedDeps);
const result = await postMI(event, mockDeep<Context>(), jest.fn());
expect(result).toEqual(expect.objectContaining({
statusCode: 500
}));
});
it('returns 500 Internal Server Error when correlation id is missing', async () => {
const event = makeApiGwEvent({
path: '/mi',
body: requestBody,
headers: {'nhsd-supplier-id': 'supplier1', 'x-request-id': 'requestId'}
});
const postMI = createPostMIHandler(mockedDeps);
const result = await postMI(event, mockDeep<Context>(), jest.fn());
expect(result).toEqual(expect.objectContaining({
statusCode: 500
}));
});
it('returns 400 Bad Request when request does not have correct shape', async () => {
const event = makeApiGwEvent({
path: '/mi',
body: '{"test": "test"}',
headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}
});
const postMI = createPostMIHandler(mockedDeps);
const result = await postMI(event, mockDeep<Context>(), jest.fn());
expect(result).toEqual(expect.objectContaining({
statusCode: 400
}));
});
it('returns 400 Bad Request when request body is not json', async () => {
const event = makeApiGwEvent({
path: '/mi',
body: '{#invalidJSON',
headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}
});
const postMI = createPostMIHandler(mockedDeps);
const result = await postMI(event, mockDeep<Context>(), jest.fn());
expect(result).toEqual(expect.objectContaining({
statusCode: 400
}));
});
it('returns 500 Internal Server Error when parsing fails', async () => {
const event = makeApiGwEvent({
path: '/mi',
body: requestBody,
headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}
});
const spy = jest.spyOn(JSON, 'parse').mockImplementation(() => {
throw 'Unexpected error';
})
const postMI = createPostMIHandler(mockedDeps);
const result = await postMI(event, mockDeep<Context>(), jest.fn());
expect(result).toEqual(expect.objectContaining({
statusCode: 500
}));
spy.mockRestore();
});
});