1- import { S3 , S3Client } from "@aws-sdk/client-s3" ;
1+ import { S3Client } from "@aws-sdk/client-s3" ;
22import { DBHealthcheck } from "../../../../../internal/datastore/src" ;
33import pino from "pino" ;
44import { Deps } from "../../config/deps" ;
@@ -8,30 +8,13 @@ import { Context } from "aws-lambda";
88import { createGetStatusHandler } from "../get-status" ;
99
1010describe ( 'API Lambda handler' , ( ) => {
11-
12- const mockedDeps : jest . Mocked < Deps > = {
13- s3Client : { send : jest . fn ( ) } as unknown as S3Client ,
14- dbHealthcheck : { check : jest . fn ( ) } as unknown as DBHealthcheck ,
15- logger : { info : jest . fn ( ) , error : jest . fn ( ) } as unknown as pino . Logger ,
16- env : {
17- SUPPLIER_ID_HEADER : 'nhsd-supplier-id' ,
18- APIM_CORRELATION_HEADER : 'nhsd-correlation-id'
19- }
20- } as Deps ;
21-
22- beforeEach ( ( ) => {
23- jest . clearAllMocks ( ) ;
24- jest . resetModules ( ) ;
25- } ) ;
26-
2711 it ( 'passes if S3 and DynamoDB are available' , async ( ) => {
2812
2913 const event = makeApiGwEvent ( { path : '/_status' ,
30- headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' } ,
31- pathParameters : { id : 'id1' }
14+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' , 'x-request-id' : 'requestId' }
3215 } ) ;
3316
34- const getLetterDataHandler = createGetStatusHandler ( mockedDeps ) ;
17+ const getLetterDataHandler = createGetStatusHandler ( getMockedDeps ( ) ) ;
3518 const result = await getLetterDataHandler ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
3619
3720 expect ( result ) . toEqual ( {
@@ -41,15 +24,29 @@ describe('API Lambda handler', () => {
4124 } ) ;
4225
4326 it ( 'fails if S3 is unavailable' , async ( ) => {
44- mockedDeps . s3Client = {
45- send : jest . fn ( ) . mockRejectedValue ( new Error ( 'unexpected error' ) )
46- } as unknown as S3Client ;
27+ const mockedDeps = getMockedDeps ( ) ;
28+ mockedDeps . s3Client . send = jest . fn ( ) . mockRejectedValue ( new Error ( 'unexpected error' ) ) ;
4729
4830 const event = makeApiGwEvent ( { path : '/_status' ,
49- headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' } ,
50- pathParameters : { id : 'id1' }
31+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' , 'x-request-id' : 'requestId' }
5132 } ) ;
5233
34+ const getLetterDataHandler = createGetStatusHandler ( mockedDeps ) ;
35+ const result = await getLetterDataHandler ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
36+
37+ expect ( result ) . toEqual ( expect . objectContaining ( {
38+ statusCode : 500
39+ } ) ) ;
40+ } ) ;
41+
42+
43+ it ( 'fails if DynamoDB is unavailable' , async ( ) => {
44+ const mockedDeps = getMockedDeps ( ) ;
45+ mockedDeps . dbHealthcheck . check = jest . fn ( ) . mockRejectedValue ( new Error ( 'unexpected error' ) ) ;
46+
47+ const event = makeApiGwEvent ( { path : '/_status' ,
48+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' , 'x-request-id' : 'requestId' }
49+ } ) ;
5350
5451 const getLetterDataHandler = createGetStatusHandler ( mockedDeps ) ;
5552 const result = await getLetterDataHandler ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
@@ -59,4 +56,28 @@ describe('API Lambda handler', () => {
5956 } ) ) ;
6057 } ) ;
6158
59+ it ( 'fails if request ID is absent' , async ( ) => {
60+ const event = makeApiGwEvent ( { path : '/_status' ,
61+ headers : { 'nhsd-supplier-id' : 'supplier1' , 'nhsd-correlation-id' : 'correlationId' }
62+ } ) ;
63+
64+ const getLetterDataHandler = createGetStatusHandler ( getMockedDeps ( ) ) ;
65+ const result = await getLetterDataHandler ( event , mockDeep < Context > ( ) , jest . fn ( ) ) ;
66+
67+ expect ( result ) . toEqual ( expect . objectContaining ( {
68+ statusCode : 500
69+ } ) ) ;
70+ } ) ;
71+
72+ function getMockedDeps ( ) : jest . Mocked < Deps > {
73+ return {
74+ s3Client : { send : jest . fn ( ) } as unknown as S3Client ,
75+ dbHealthcheck : { check : jest . fn ( ) } as unknown as DBHealthcheck ,
76+ logger : { info : jest . fn ( ) , error : jest . fn ( ) } as unknown as pino . Logger ,
77+ env : {
78+ SUPPLIER_ID_HEADER : 'nhsd-supplier-id' ,
79+ APIM_CORRELATION_HEADER : 'nhsd-correlation-id'
80+ }
81+ } as Deps ;
82+ }
6283} ) ;
0 commit comments