|
| 1 | +import { Context } from 'aws-lambda'; |
| 2 | +import { mockDeep } from 'jest-mock-extended'; |
| 3 | +import * as letterService from '../../services/letter-operations'; |
| 4 | +import { makeApiGwEvent } from './utils/test-utils'; |
| 5 | +import { getLetter } from '../../index'; |
| 6 | +import { ApiErrorDetail } from '../../contracts/errors'; |
| 7 | +import { NotFoundError } from '../../errors'; |
| 8 | + |
| 9 | +jest.mock('../../services/letter-operations'); |
| 10 | + |
| 11 | +jest.mock('../../config/lambda-config', () => ({ |
| 12 | + lambdaConfig: { |
| 13 | + SUPPLIER_ID_HEADER: 'nhsd-supplier-id', |
| 14 | + APIM_CORRELATION_HEADER: 'nhsd-correlation-id' |
| 15 | + } |
| 16 | +})); |
| 17 | + |
| 18 | +describe('API Lambda handler', () => { |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + jest.resetModules(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns 200 OK and the letter status', async () => { |
| 26 | + |
| 27 | + const mockedGetLetterById = letterService.getLetterById as jest.Mock; |
| 28 | + mockedGetLetterById.mockResolvedValue({ |
| 29 | + id: 'id1', |
| 30 | + specificationId: 'spec1', |
| 31 | + groupId: 'group1', |
| 32 | + status: 'PENDING' |
| 33 | + }); |
| 34 | + |
| 35 | + const event = makeApiGwEvent({path: '/letters/id1', |
| 36 | + headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId'}, |
| 37 | + pathParameters: {id: 'id1'}}); |
| 38 | + |
| 39 | + const result = await getLetter(event, mockDeep<Context>(), jest.fn()); |
| 40 | + |
| 41 | + const expected = { |
| 42 | + data: { |
| 43 | + id: 'id1', |
| 44 | + type: 'Letter', |
| 45 | + attributes: { |
| 46 | + status: 'PENDING', |
| 47 | + specificationId: 'spec1', |
| 48 | + groupId: 'group1' |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + expect(result).toEqual({ |
| 54 | + statusCode: 200, |
| 55 | + body: JSON.stringify(expected, null, 2), |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('includes the reason code and reason text if present', async () => { |
| 60 | + |
| 61 | + const mockedGetLetterById = letterService.getLetterById as jest.Mock; |
| 62 | + mockedGetLetterById.mockResolvedValue({ |
| 63 | + id: 'id1', |
| 64 | + specificationId: 'spec1', |
| 65 | + groupId: 'group1', |
| 66 | + status: 'FAILED', |
| 67 | + reasonCode: 100, |
| 68 | + reasonText: 'failed validation' |
| 69 | + }); |
| 70 | + |
| 71 | + const event = makeApiGwEvent({path: '/letters/id1', |
| 72 | + headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId'}, |
| 73 | + pathParameters: {id: 'id1'}}); |
| 74 | + |
| 75 | + const result = await getLetter(event, mockDeep<Context>(), jest.fn()); |
| 76 | + |
| 77 | + |
| 78 | + const expected = { |
| 79 | + data: { |
| 80 | + id: 'id1', |
| 81 | + type: 'Letter', |
| 82 | + attributes: { |
| 83 | + status: 'FAILED', |
| 84 | + specificationId: 'spec1', |
| 85 | + groupId: 'group1', |
| 86 | + reasonCode: 100, |
| 87 | + reasonText: 'failed validation' |
| 88 | + } |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + expect(result).toEqual({ |
| 93 | + statusCode: 200, |
| 94 | + body: JSON.stringify(expected, null, 2), |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns 404 Not Found when letter matching id is not found', async () => { |
| 99 | + |
| 100 | + const mockedGetLetterById = letterService.getLetterById as jest.Mock; |
| 101 | + mockedGetLetterById.mockImplementation(() => { |
| 102 | + throw new NotFoundError(ApiErrorDetail.NotFoundLetterId); |
| 103 | + }); |
| 104 | + |
| 105 | + const event = makeApiGwEvent({path: '/letters/id1', |
| 106 | + headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId'}, |
| 107 | + pathParameters: {id: 'id1'}}); |
| 108 | + |
| 109 | + const result = await getLetter(event, mockDeep<Context>(), jest.fn()); |
| 110 | + |
| 111 | + expect(result).toEqual(expect.objectContaining({ |
| 112 | + statusCode: 404, |
| 113 | + })); |
| 114 | + }); |
| 115 | + |
| 116 | + it ('returns 500 when correlation id is missing from header', async() => { |
| 117 | + const event = makeApiGwEvent({path: '/letters/id1', |
| 118 | + headers: {'nhsd-supplier-id': 'supplier1'}, |
| 119 | + pathParameters: {id: 'id1'}}); |
| 120 | + |
| 121 | + const result = await getLetter(event, mockDeep<Context>(), jest.fn()); |
| 122 | + |
| 123 | + expect(result).toEqual(expect.objectContaining({ |
| 124 | + statusCode: 500, |
| 125 | + })); |
| 126 | + }); |
| 127 | + |
| 128 | + it ('returns 400 when supplier id is missing from header', async() => { |
| 129 | + const event = makeApiGwEvent({path: '/letters/id1', |
| 130 | + headers: {'nhsd-correlation-id': 'correlationId'}, |
| 131 | + pathParameters: {id: 'id1'}}); |
| 132 | + |
| 133 | + const result = await getLetter(event, mockDeep<Context>(), jest.fn()); |
| 134 | + |
| 135 | + expect(result).toEqual(expect.objectContaining({ |
| 136 | + statusCode: 400, |
| 137 | + })); |
| 138 | + }); |
| 139 | + |
| 140 | + |
| 141 | + it ('returns 400 when letter id is missing from path', async() => { |
| 142 | + const event = makeApiGwEvent({path: '/letters/id1', |
| 143 | + headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId'}}); |
| 144 | + |
| 145 | + const result = await getLetter(event, mockDeep<Context>(), jest.fn()); |
| 146 | + |
| 147 | + expect(result).toEqual(expect.objectContaining({ |
| 148 | + statusCode: 400, |
| 149 | + })); |
| 150 | + }); |
| 151 | +}); |
0 commit comments