88} from "@jest/globals"
99import { createHmac } from "crypto"
1010import { DynamoDBDocumentClient , QueryCommand , UpdateCommand } from "@aws-sdk/lib-dynamodb"
11+ import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager"
1112
1213import { response , checkSignature , updateNotificationsTable } from "../src/helpers"
1314import { Logger } from "@aws-lambda-powertools/logger"
@@ -44,8 +45,18 @@ describe("helpers.ts", () => {
4445
4546 describe ( "checkSignature()" , ( ) => {
4647 let logger : Logger
47- let validHeaders : { "x-request-id" : string ; "x-api-key" : string ; "x-hmac-sha256-signature" : string }
48+ let validHeaders : Record < string , string >
49+ let smSendSpy : jest . SpiedFunction < typeof SecretsManagerClient . prototype . send >
50+
4851 beforeEach ( ( ) => {
52+ // Stub SecretsManagerClient.send so we never call AWS in tests
53+ smSendSpy = jest
54+ . spyOn ( SecretsManagerClient . prototype , "send" )
55+ // first call: APP_NAME
56+ . mockImplementationOnce ( ( ) => Promise . resolve ( { SecretString : process . env . APP_NAME_SECRET_ARN ! } ) )
57+ // second call: API_KEY
58+ . mockImplementationOnce ( ( ) => Promise . resolve ( { SecretString : process . env . API_KEY_SECRET_ARN ! } ) )
59+
4960 logger = new Logger ( { serviceName : "nhsNotifyUpdateCallback" } )
5061 validHeaders = {
5162 "x-request-id" : "requestid" ,
@@ -54,40 +65,48 @@ describe("helpers.ts", () => {
5465 }
5566 } )
5667
57- it ( "401 when missing signature header" , ( ) => {
58- const ev = generateMockEvent ( "{}" , { "x-api-key" : "foobar" , "x-request-id" : "rid" } )
59- const resp = checkSignature ( logger , ev )
68+ afterEach ( ( ) => {
69+ smSendSpy . mockRestore ( )
70+ } )
71+
72+ it ( "401 when missing signature header" , async ( ) => {
73+ const ev = generateMockEvent ( "{}" , {
74+ "x-api-key" : "foobar" ,
75+ "x-request-id" : "rid"
76+ } )
77+ const resp = await checkSignature ( logger , ev )
6078 expect ( resp ) . toEqual ( {
6179 statusCode : 401 ,
6280 body : JSON . stringify ( { message : "No x-hmac-sha256-signature given" } )
6381 } )
6482 } )
6583
66- it ( "401 when missing API key header" , ( ) => {
67- const ev = generateMockEvent ( "{}" , { "x-hmac-sha256-signature" : "foobar" , "x-request-id" : "rid" } )
68- const resp = checkSignature ( logger , ev )
69-
84+ it ( "401 when missing API key header" , async ( ) => {
85+ const ev = generateMockEvent ( "{}" , {
86+ "x-hmac-sha256-signature" : "foobar" ,
87+ "x-request-id" : "rid"
88+ } )
89+ const resp = await checkSignature ( logger , ev )
7090 expect ( resp ) . toEqual ( {
7191 statusCode : 401 ,
7292 body : JSON . stringify ( { message : "No x-api-key header given" } )
7393 } )
7494 } )
7595
76- it ( "403 when signature hex is malformed" , ( ) => {
96+ it ( "403 when signature hex is malformed" , async ( ) => {
7797 const headers = {
7898 ...validHeaders ,
7999 "x-hmac-sha256-signature" : "not a hex string!@!#zzz"
80100 }
81101 const ev = generateMockEvent ( JSON . stringify ( { message : "blah blah blah" } ) , headers )
82- const resp = checkSignature ( logger , ev )
83-
102+ const resp = await checkSignature ( logger , ev )
84103 expect ( resp ) . toEqual ( {
85104 statusCode : 403 ,
86105 body : JSON . stringify ( { message : "Incorrect signature" } )
87106 } )
88107 } )
89108
90- it ( "403 when signature does not match HMAC" , ( ) => {
109+ it ( "403 when signature does not match HMAC" , async ( ) => {
91110 const payload = "payload"
92111 const wrongSig = createHmac (
93112 "sha256" ,
@@ -100,17 +119,16 @@ describe("helpers.ts", () => {
100119 ...validHeaders ,
101120 "x-hmac-sha256-signature" : wrongSig
102121 } )
103- const resp = checkSignature ( logger , ev )
104-
122+ const resp = await checkSignature ( logger , ev )
105123 expect ( resp ) . toEqual ( {
106124 statusCode : 403 ,
107125 body : JSON . stringify ( { message : "Incorrect signature" } )
108126 } )
109127 } )
110128
111- it ( "returns undefined when signature is valid" , ( ) => {
129+ it ( "returns undefined when signature is valid" , async ( ) => {
112130 const payload = "hi there"
113- const secret = `${ process . env . APP_NAME } .${ process . env . API_KEY } `
131+ const secret = `${ process . env . APP_NAME_SECRET_ARN } .${ process . env . API_KEY_SECRET_ARN } `
114132 const goodSig = createHmac ( "sha256" , secret )
115133 . update ( payload , "utf8" )
116134 . digest ( "hex" )
@@ -119,13 +137,14 @@ describe("helpers.ts", () => {
119137 ...validHeaders ,
120138 "x-hmac-sha256-signature" : goodSig
121139 } )
122- const resp = checkSignature ( logger , ev )
140+ const resp = await checkSignature ( logger , ev )
123141 expect ( resp ) . toBeUndefined ( )
124142 } )
125143 } )
126144
127145 describe ( "updateNotificationsTable()" , ( ) => {
128146 let logger : Logger
147+
129148 beforeEach ( ( ) => {
130149 logger = new Logger ( { serviceName : "nhsNotifyUpdateCallback" } )
131150 jest . spyOn ( logger , "error" )
0 commit comments