@@ -2,21 +2,34 @@ import { Context } from 'aws-lambda';
22import { mockDeep } from 'jest-mock-extended' ;
33import * as letterService from '../../services/letter-operations' ;
44import { makeApiGwEvent } from './utils/test-utils' ;
5- import { getLetter } from '../../index' ;
65import { ApiErrorDetail } from '../../contracts/errors' ;
76import { 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' ;
813
914jest . mock ( '../../services/letter-operations' ) ;
1015
11- jest . mock ( '../../config/lambda-config' , ( ) => ( {
12- lambdaConfig : {
13- SUPPLIER_ID_HEADER : 'nhsd-supplier-id' ,
14- APIM_CORRELATION_HEADER : 'nhsd-correlation-id'
15- }
16- } ) ) ;
1716
1817describe ( 'API Lambda handler' , ( ) => {
1918
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+
2033 beforeEach ( ( ) => {
2134 jest . clearAllMocks ( ) ;
2235 jest . resetModules ( ) ;
@@ -33,9 +46,10 @@ describe('API Lambda handler', () => {
3346 } ) ;
3447
3548 const event = makeApiGwEvent ( { path : '/letters/id1' ,
36- headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' } ,
49+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' , 'x-request-id' : 'requestId' } ,
3750 pathParameters : { id : 'id1' } } ) ;
3851
52+ const getLetter = createGetLetterHandler ( mockedDeps ) ;
3953 const result = await getLetter ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
4054
4155 const expected = {
@@ -69,12 +83,12 @@ describe('API Lambda handler', () => {
6983 } ) ;
7084
7185 const event = makeApiGwEvent ( { path : '/letters/id1' ,
72- headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' } ,
86+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' , 'x-request-id' : 'requestId' } ,
7387 pathParameters : { id : 'id1' } } ) ;
7488
89+ const getLetter = createGetLetterHandler ( mockedDeps ) ;
7590 const result = await getLetter ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
7691
77-
7892 const expected = {
7993 data : {
8094 id : 'id1' ,
@@ -103,9 +117,10 @@ describe('API Lambda handler', () => {
103117 } ) ;
104118
105119 const event = makeApiGwEvent ( { path : '/letters/id1' ,
106- headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' } ,
120+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' , 'x-request-id' : 'requestId' } ,
107121 pathParameters : { id : 'id1' } } ) ;
108122
123+ const getLetter = createGetLetterHandler ( mockedDeps ) ;
109124 const result = await getLetter ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
110125
111126 expect ( result ) . toEqual ( expect . objectContaining ( {
@@ -115,33 +130,36 @@ describe('API Lambda handler', () => {
115130
116131 it ( 'returns 500 when correlation id is missing from header' , async ( ) => {
117132 const event = makeApiGwEvent ( { path : '/letters/id1' ,
118- headers : { 'nhsd-supplier-id' : 'supplier1' } ,
133+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'x-request-id' : 'requestId' } ,
119134 pathParameters : { id : 'id1' } } ) ;
120135
136+ const getLetter = createGetLetterHandler ( mockedDeps ) ;
121137 const result = await getLetter ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
122138
123139 expect ( result ) . toEqual ( expect . objectContaining ( {
124140 statusCode : 500 ,
125141 } ) ) ;
126142 } ) ;
127143
128- it ( 'returns 400 when supplier id is missing from header' , async ( ) => {
144+ it ( 'returns 500 when supplier id is missing from header' , async ( ) => {
129145 const event = makeApiGwEvent ( { path : '/letters/id1' ,
130- headers : { 'nhsd-correlation-id' : 'correlationId' } ,
146+ headers : { 'nhsd-correlation-id' : 'correlationId' , 'x-request-id' : 'requestId' } ,
131147 pathParameters : { id : 'id1' } } ) ;
132148
149+ const getLetter = createGetLetterHandler ( mockedDeps ) ;
133150 const result = await getLetter ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
134151
135152 expect ( result ) . toEqual ( expect . objectContaining ( {
136- statusCode : 400 ,
153+ statusCode : 500 ,
137154 } ) ) ;
138155 } ) ;
139156
140157
141158 it ( 'returns 400 when letter id is missing from path' , async ( ) => {
142159 const event = makeApiGwEvent ( { path : '/letters/id1' ,
143- headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' } } ) ;
160+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' , 'x-request-id' : 'requestId' } } ) ;
144161
162+ const getLetter = createGetLetterHandler ( mockedDeps ) ;
145163 const result = await getLetter ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
146164
147165 expect ( result ) . toEqual ( expect . objectContaining ( {
0 commit comments