-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestPrescriptionIntercept.test.ts
More file actions
275 lines (234 loc) · 10.1 KB
/
testPrescriptionIntercept.test.ts
File metadata and controls
275 lines (234 loc) · 10.1 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* eslint-disable @typescript-eslint/no-explicit-any */
import {APIGatewayProxyEvent, APIGatewayProxyResult} from "aws-lambda"
import {
expect,
it,
describe,
vi,
beforeEach,
afterEach
} from "vitest"
import {
DEFAULT_DATE,
FULL_URL_1,
generateBody,
generateExpectedItems,
generateMockEvent,
TASK_VALUES,
getTestPrescriptions
} from "./utils/testUtils"
import {GetItemCommand, TransactionCanceledException, TransactWriteItemsCommand} from "@aws-sdk/client-dynamodb"
const {mockSend, mockGetParametersByName, mockInitiatedSSMProvider} = vi.hoisted(() => {
const mockGetParametersByName = vi.fn(async () => ({}))
return {
mockSend: vi.fn(),
mockGetParametersByName,
mockInitiatedSSMProvider: {getParametersByName: mockGetParametersByName}
}
})
export {mockGetParametersByName}
vi.mock("@aws-sdk/client-dynamodb", async (importOriginal) => {
const mod = await importOriginal<typeof import("@aws-sdk/client-dynamodb")>()
return {
...mod,
DynamoDBClient: vi.fn(class {
send = mockSend
})
}
})
vi.mock("@psu-common/utilities", async (importOriginal) => {
const mod = await importOriginal<typeof import("@psu-common/utilities")>()
return {
...mod,
initiatedSSMProvider: mockInitiatedSSMProvider,
getTestPrescriptions: getTestPrescriptions
}
})
process.env.ENVIRONMENT = "int"
function resetDynamoMock() {
mockSend.mockClear()
mockSend.mockImplementation(async () => ({}))
}
function setupExistingDynamoEntry() {
mockSend.mockImplementation(async (command) => {
if (command instanceof GetItemCommand) {
return new Object({Item: "Some item"})
} else if (command instanceof TransactWriteItemsCommand) {
throw new TransactionCanceledException({
message: "DynamoDB transaction cancelled due to conditional check failure.",
$metadata: {},
CancellationReasons: [
{
Code: "ConditionalCheckFailed",
Item: {
TaskID: {S: "0ae4daf3-f24b-479d-b8fa-b69e2d873b60"}
},
Message: "The conditional request failed"
}
]
})
}
})
}
function expectGetItemCommand(prescriptionID: string, taskID: string) {
expect(mockSend).toHaveBeenCalledWith(expect.objectContaining({
input: {
Key: {
PrescriptionID: {S: prescriptionID},
TaskID: {S: taskID}
},
TableName: "PrescriptionStatusUpdates"
}
}))
}
describe("testPrescription1Intercept", () => {
beforeEach(async () => {
vi.useFakeTimers().setSystemTime(DEFAULT_DATE)
const {resetTestPrescriptions} = await import("../src/updatePrescriptionStatus")
resetTestPrescriptions()
resetDynamoMock()
vi.clearAllMocks()
})
it("Return 500 and write to DynamoDB when test prescription 1 is submitted for the first time", async () => {
const body = generateBody(2)
// Only include entry for test prescription 1 (TASK_VALUES[1])
body.entry = [body.entry[1]]
const event: APIGatewayProxyEvent = generateMockEvent(body)
let expectedItems = generateExpectedItems(2)
expectedItems.input.TransactItems = [expectedItems.input.TransactItems[1]]
const {handler, logger} = await import("../src/updatePrescriptionStatus")
const loggerInfo = vi.spyOn(logger, "info")
const response: APIGatewayProxyResult = await handler(event, {})
expect(response.statusCode).toEqual(500)
expect(loggerInfo).toHaveBeenCalledWith("First submission of INT test prescription 1, returning 500")
expect(loggerInfo).toHaveBeenCalledWith("Forcing error for INT test prescription")
expectGetItemCommand(TASK_VALUES[1].prescriptionID, TASK_VALUES[1].id)
expect(mockSend).toHaveBeenCalledWith(expect.objectContaining(expectedItems))
})
it("Return 201 and doesn't write to DynamoDB when test prescription 1 is submitted for a second time", async () => {
const body = generateBody(2)
// Only include entry for test prescription 1 (TASK_VALUES[1])
body.entry = [body.entry[1]]
const event: APIGatewayProxyEvent = generateMockEvent(body)
const {handler, logger} = await import("../src/updatePrescriptionStatus")
const loggerInfo = vi.spyOn(logger, "info")
const first_submission_response: APIGatewayProxyResult = await handler(event, {})
expect(first_submission_response.statusCode).toEqual(500)
expect(loggerInfo).toHaveBeenCalledWith("First submission of INT test prescription 1, returning 500")
expect(loggerInfo).toHaveBeenCalledWith("Forcing error for INT test prescription")
setupExistingDynamoEntry()
const response: APIGatewayProxyResult = await handler(event, {})
expect(response.statusCode).toEqual(201)
expect(loggerInfo).toHaveBeenCalledWith("Not first submission of INT test prescription 1, forcing 201")
expect(loggerInfo).toHaveBeenCalledWith("Forcing 201 response for INT test prescription 1")
const responseBody = JSON.parse(response.body)
expect(responseBody.entry[0].response.status).toEqual("201 Created")
expect(responseBody.entry[0].fullUrl).toEqual(FULL_URL_1)
expectGetItemCommand(TASK_VALUES[1].prescriptionID, TASK_VALUES[1].id)
})
})
describe("testPrescription2Intercept", () => {
beforeEach(async () => {
vi.useFakeTimers().setSystemTime(DEFAULT_DATE)
const {resetTestPrescriptions} = await import("../src/updatePrescriptionStatus")
resetTestPrescriptions()
resetDynamoMock()
vi.clearAllMocks()
})
it("Return 500 and write to DynamoDB when test prescription 2 is submitted for the first time", async () => {
const body = generateBody(4)
// Only include entry for test prescription 2 (TASK_VALUES[3])
body.entry = [body.entry[3]]
const event: APIGatewayProxyEvent = generateMockEvent(body)
let expectedItems = generateExpectedItems(4)
expectedItems.input.TransactItems = [expectedItems.input.TransactItems[3]]
const {handler, logger} = await import("../src/updatePrescriptionStatus")
const loggerInfo = vi.spyOn(logger, "info")
const response: APIGatewayProxyResult = await handler(event, {})
expect(response.statusCode).toEqual(500)
expect(loggerInfo).toHaveBeenCalledWith(
"First submission of INT test prescription 2. Updating store then returning 500"
)
expect(loggerInfo).toHaveBeenCalledWith("Forcing error for INT test prescription")
expectGetItemCommand(TASK_VALUES[3].prescriptionID, TASK_VALUES[3].id)
expect(mockSend).toHaveBeenCalledWith(expect.objectContaining(expectedItems))
})
it("Return 409 when test prescription 2 is submitted for a second time", async () => {
const body = generateBody(4)
// Only include entry for test prescription 2 (TASK_VALUES[3])
body.entry = [body.entry[3]]
const event: APIGatewayProxyEvent = generateMockEvent(body)
const {handler, logger} = await import("../src/updatePrescriptionStatus")
const loggerInfo = vi.spyOn(logger, "info")
const first_submission_response: APIGatewayProxyResult = await handler(event, {})
expect(first_submission_response.statusCode).toEqual(500)
expect(loggerInfo).toHaveBeenCalledWith(
"First submission of INT test prescription 2. Updating store then returning 500"
)
expect(loggerInfo).toHaveBeenCalledWith("Forcing error for INT test prescription")
setupExistingDynamoEntry()
const response: APIGatewayProxyResult = await handler(event, {})
expect(response.statusCode).toEqual(409)
expect(loggerInfo).toHaveBeenCalledWith("Not first submission of INT test prescription 2, continuing")
const responseBody = JSON.parse(response.body)
// When a duplicate is detected, there might be multiple entries in the response
// Find the entry with the duplicate error message
const duplicateEntry = responseBody.entry.find((entry: any) =>
entry.response?.outcome?.issue?.[0]?.diagnostics?.includes("task id and prescription id identical")
)
expect(duplicateEntry).toBeDefined()
expect(duplicateEntry.response.outcome.issue[0].diagnostics).toEqual(
"Request contains a task id and prescription id identical to a record already in the data store."
)
expectGetItemCommand(TASK_VALUES[3].prescriptionID, TASK_VALUES[3].id)
})
})
describe("testPrescription3Intercept", () => {
beforeEach(async () => {
vi.useFakeTimers().setSystemTime(DEFAULT_DATE)
vi.resetModules()
const {resetTestPrescriptions} = await import("../src/updatePrescriptionStatus")
resetTestPrescriptions()
resetDynamoMock()
vi.clearAllMocks()
})
afterEach(() => {
vi.resetModules()
})
it("Return 400 when test prescription 3 is submitted", async () => {
const body = generateBody(3)
// Only include entries 0, 1, and 2. Entry 2 contains TASK_VALUES[2] which matches TEST_PRESCRIPTIONS_3
const event: APIGatewayProxyEvent = generateMockEvent(body)
const {handler, logger} = await import("../src/updatePrescriptionStatus")
const loggerInfo = vi.spyOn(logger, "info")
const response: APIGatewayProxyResult = await handler(event, {})
expect(response.statusCode).toEqual(400)
expect(loggerInfo).toHaveBeenCalledWith(
"Forcing error for INT test prescription. Simulating failure to write to database.")
})
})
describe("testPrescription4Intercept", () => {
beforeEach(async () => {
vi.useFakeTimers().setSystemTime(DEFAULT_DATE)
vi.resetModules()
const {resetTestPrescriptions} = await import("../src/updatePrescriptionStatus")
resetTestPrescriptions()
resetDynamoMock()
vi.clearAllMocks()
})
afterEach(() => {
vi.resetModules()
})
it("Return 400 when test prescription 4 is submitted", async () => {
const body = generateBody(1)
// Entry 0 contains TASK_VALUES[0] which matches TEST_PRESCRIPTIONS_4
const event: APIGatewayProxyEvent = generateMockEvent(body)
const {handler, logger} = await import("../src/updatePrescriptionStatus")
const loggerInfo = vi.spyOn(logger, "info")
const response: APIGatewayProxyResult = await handler(event, {})
console.log(response)
expect(loggerInfo).toHaveBeenCalledWith(
"Forcing error for INT test prescription. Simulating PSU capacity failure.")
expect(response.statusCode).toEqual(429)
})
})