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