|
| 1 | +import { Context } from "aws-lambda"; |
| 2 | +import { mockDeep } from "jest-mock-extended"; |
| 3 | +import { makeApiGwEvent } from "./utils/test-utils"; |
| 4 | +import { PostMIRequest, PostMIResponse } from "../../contracts/mi"; |
| 5 | +import * as miService from '../../services/mi-operations'; |
| 6 | +import { postMi } from "../post-mi"; |
| 7 | + |
| 8 | +jest.mock('../../services/mi-operations'); |
| 9 | + |
| 10 | +jest.mock('../../config/lambda-config', () => ({ |
| 11 | + lambdaConfig: { |
| 12 | + SUPPLIER_ID_HEADER: 'nhsd-supplier-id', |
| 13 | + APIM_CORRELATION_HEADER: 'nhsd-correlation-id' |
| 14 | + } |
| 15 | +})); |
| 16 | + |
| 17 | +const postMiRequest : PostMIRequest = { |
| 18 | + data: { |
| 19 | + type: 'ManagementInformation', |
| 20 | + attributes: { |
| 21 | + lineItem: 'envelope-business-standard', |
| 22 | + timestamp: '2023-11-17T14:27:51.413Z', |
| 23 | + quantity: 22, |
| 24 | + specificationId: 'spec1', |
| 25 | + groupId: 'group1', |
| 26 | + stockRemaining: 20000 |
| 27 | + } |
| 28 | + } |
| 29 | +}; |
| 30 | +const requestBody = JSON.stringify(postMiRequest, null, 2); |
| 31 | + |
| 32 | + const postMiResponse : PostMIResponse = { |
| 33 | + data: { |
| 34 | + id: 'id1', |
| 35 | + ...postMiRequest.data |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | +const mockedPostMiOperation = jest.mocked(miService.postMI); |
| 40 | + |
| 41 | +beforeEach(() => { |
| 42 | + jest.clearAllMocks(); |
| 43 | +}); |
| 44 | + |
| 45 | + |
| 46 | +describe('postMI API Handler', () => { |
| 47 | + it('returns 200 OK with updated resource', async () => { |
| 48 | + const event = makeApiGwEvent({ |
| 49 | + path: '/mi', |
| 50 | + body: requestBody, |
| 51 | + headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId'} |
| 52 | + }); |
| 53 | + |
| 54 | + mockedPostMiOperation.mockResolvedValue(postMiResponse); |
| 55 | + |
| 56 | + const result = await postMi(event, mockDeep<Context>(), jest.fn()); |
| 57 | + |
| 58 | + expect(result).toEqual({ |
| 59 | + statusCode: 201, |
| 60 | + body: JSON.stringify(postMiResponse, null, 2) |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + |
| 65 | + it.each([['not a date string'], ['2025-10-16T00:00:00'], ['2025-16-10T00:00:00Z']]) |
| 66 | + ('returns 400 Bad Request when the timestamp is not an ISO8601 instant', async (timestamp: string) => { |
| 67 | + const invalidRequest = JSON.parse(requestBody); |
| 68 | + invalidRequest['data']['attributes']['timestamp'] = timestamp; |
| 69 | + const event = makeApiGwEvent({ |
| 70 | + path: '/mi', |
| 71 | + body: JSON.stringify(invalidRequest), |
| 72 | + headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId'} |
| 73 | + }); |
| 74 | + |
| 75 | + const result = await postMi(event, mockDeep<Context>(), jest.fn()); |
| 76 | + |
| 77 | + expect(result).toEqual(expect.objectContaining({ |
| 78 | + statusCode: 400 |
| 79 | + })); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns 400 Bad Request when there is no body', async () => { |
| 83 | + const event = makeApiGwEvent({ |
| 84 | + path: '/letters/id1', |
| 85 | + pathParameters: {id: 'id1'}, |
| 86 | + headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId'} |
| 87 | + }); |
| 88 | + |
| 89 | + const result = await postMi(event, mockDeep<Context>(), jest.fn()); |
| 90 | + |
| 91 | + expect(result).toEqual(expect.objectContaining({ |
| 92 | + statusCode: 400 |
| 93 | + })); |
| 94 | + }); |
| 95 | + |
| 96 | + |
| 97 | + it('returns 500 Internal Error when error is thrown by service', async () => { |
| 98 | + const event = makeApiGwEvent({ |
| 99 | + path: '/letters/id1', |
| 100 | + body: requestBody, |
| 101 | + pathParameters: {id: 'id1'}, |
| 102 | + headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId'} |
| 103 | + }); |
| 104 | + mockedPostMiOperation.mockRejectedValue(new Error()); |
| 105 | + |
| 106 | + const result = await postMi(event, mockDeep<Context>(), jest.fn()); |
| 107 | + |
| 108 | + expect(result).toEqual(expect.objectContaining({ |
| 109 | + statusCode: 500 |
| 110 | + })); |
| 111 | + }); |
| 112 | + |
| 113 | + it('returns 400 Bad Request when supplier id is missing', async () => { |
| 114 | + const event = makeApiGwEvent({ |
| 115 | + path: '/mi', |
| 116 | + body: requestBody, |
| 117 | + headers: {'nhsd-correlation-id': 'correlationId'} |
| 118 | + }); |
| 119 | + |
| 120 | + const result = await postMi(event, mockDeep<Context>(), jest.fn()); |
| 121 | + |
| 122 | + expect(result).toEqual(expect.objectContaining({ |
| 123 | + statusCode: 400 |
| 124 | + })); |
| 125 | + }); |
| 126 | + |
| 127 | + it('returns 500 Internal Server Error when correlation id is missing', async () => { |
| 128 | + const event = makeApiGwEvent({ |
| 129 | + path: '/mi', |
| 130 | + body: requestBody, |
| 131 | + headers: {'nhsd-supplier-id': 'supplier1'} |
| 132 | + }); |
| 133 | + |
| 134 | + const result = await postMi(event, mockDeep<Context>(), jest.fn()); |
| 135 | + |
| 136 | + expect(result).toEqual(expect.objectContaining({ |
| 137 | + statusCode: 500 |
| 138 | + })); |
| 139 | + }); |
| 140 | + |
| 141 | + it('returns 400 Bad Request when request does not have correct shape', async () => { |
| 142 | + const event = makeApiGwEvent({ |
| 143 | + path: '/mi', |
| 144 | + body: '{"test": "test"}', |
| 145 | + headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId'} |
| 146 | + }); |
| 147 | + |
| 148 | + const result = await postMi(event, mockDeep<Context>(), jest.fn()); |
| 149 | + |
| 150 | + expect(result).toEqual(expect.objectContaining({ |
| 151 | + statusCode: 400 |
| 152 | + })); |
| 153 | + }); |
| 154 | + |
| 155 | + it('returns 400 Bad Request when request body is not json', async () => { |
| 156 | + const event = makeApiGwEvent({ |
| 157 | + path: '/mi', |
| 158 | + body: '{#invalidJSON', |
| 159 | + headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId'} |
| 160 | + }); |
| 161 | + |
| 162 | + const result = await postMi(event, mockDeep<Context>(), jest.fn()); |
| 163 | + |
| 164 | + expect(result).toEqual(expect.objectContaining({ |
| 165 | + statusCode: 400 |
| 166 | + })); |
| 167 | + }); |
| 168 | + |
| 169 | + it('returns 500 Internal Server Error when parsing fails', async () => { |
| 170 | + const event = makeApiGwEvent({ |
| 171 | + path: '/mi', |
| 172 | + body: requestBody, |
| 173 | + headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId'} |
| 174 | + }); |
| 175 | + const spy = jest.spyOn(JSON, 'parse').mockImplementation(() => { |
| 176 | + throw 'Unexpected error'; |
| 177 | + }) |
| 178 | + |
| 179 | + const result = await postMi(event, mockDeep<Context>(), jest.fn()); |
| 180 | + |
| 181 | + expect(result).toEqual(expect.objectContaining({ |
| 182 | + statusCode: 500 |
| 183 | + })); |
| 184 | + |
| 185 | + spy.mockRestore(); |
| 186 | + }); |
| 187 | +}); |
0 commit comments