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"
1211
1312import { response , checkSignature , updateNotificationsTable } from "../src/helpers"
1413import { Logger } from "@aws-lambda-powertools/logger"
@@ -45,18 +44,8 @@ describe("helpers.ts", () => {
4544
4645 describe ( "checkSignature()" , ( ) => {
4746 let logger : Logger
48- let validHeaders : Record < string , string >
49- let smSendSpy : jest . SpiedFunction < typeof SecretsManagerClient . prototype . send >
50-
47+ let validHeaders : { "x-request-id" : string ; "x-api-key" : string ; "x-hmac-sha256-signature" : string }
5148 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-
6049 logger = new Logger ( { serviceName : "nhsNotifyUpdateCallback" } )
6150 validHeaders = {
6251 "x-request-id" : "requestid" ,
@@ -65,48 +54,40 @@ describe("helpers.ts", () => {
6554 }
6655 } )
6756
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 )
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 )
7860 expect ( resp ) . toEqual ( {
7961 statusCode : 401 ,
8062 body : JSON . stringify ( { message : "No x-hmac-sha256-signature given" } )
8163 } )
8264 } )
8365
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 )
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+
9070 expect ( resp ) . toEqual ( {
9171 statusCode : 401 ,
9272 body : JSON . stringify ( { message : "No x-api-key header given" } )
9373 } )
9474 } )
9575
96- it ( "403 when signature hex is malformed" , async ( ) => {
76+ it ( "403 when signature hex is malformed" , ( ) => {
9777 const headers = {
9878 ...validHeaders ,
9979 "x-hmac-sha256-signature" : "not a hex string!@!#zzz"
10080 }
10181 const ev = generateMockEvent ( JSON . stringify ( { message : "blah blah blah" } ) , headers )
102- const resp = await checkSignature ( logger , ev )
82+ const resp = checkSignature ( logger , ev )
83+
10384 expect ( resp ) . toEqual ( {
10485 statusCode : 403 ,
10586 body : JSON . stringify ( { message : "Incorrect signature" } )
10687 } )
10788 } )
10889
109- it ( "403 when signature does not match HMAC" , async ( ) => {
90+ it ( "403 when signature does not match HMAC" , ( ) => {
11091 const payload = "payload"
11192 const wrongSig = createHmac (
11293 "sha256" ,
@@ -119,16 +100,17 @@ describe("helpers.ts", () => {
119100 ...validHeaders ,
120101 "x-hmac-sha256-signature" : wrongSig
121102 } )
122- const resp = await checkSignature ( logger , ev )
103+ const resp = checkSignature ( logger , ev )
104+
123105 expect ( resp ) . toEqual ( {
124106 statusCode : 403 ,
125107 body : JSON . stringify ( { message : "Incorrect signature" } )
126108 } )
127109 } )
128110
129- it ( "returns undefined when signature is valid" , async ( ) => {
111+ it ( "returns undefined when signature is valid" , ( ) => {
130112 const payload = "hi there"
131- const secret = `${ process . env . APP_NAME_SECRET_ARN } .${ process . env . API_KEY_SECRET_ARN } `
113+ const secret = `${ process . env . APP_NAME } .${ process . env . API_KEY } `
132114 const goodSig = createHmac ( "sha256" , secret )
133115 . update ( payload , "utf8" )
134116 . digest ( "hex" )
@@ -137,14 +119,13 @@ describe("helpers.ts", () => {
137119 ...validHeaders ,
138120 "x-hmac-sha256-signature" : goodSig
139121 } )
140- const resp = await checkSignature ( logger , ev )
122+ const resp = checkSignature ( logger , ev )
141123 expect ( resp ) . toBeUndefined ( )
142124 } )
143125 } )
144126
145127 describe ( "updateNotificationsTable()" , ( ) => {
146128 let logger : Logger
147-
148129 beforeEach ( ( ) => {
149130 logger = new Logger ( { serviceName : "nhsNotifyUpdateCallback" } )
150131 jest . spyOn ( logger , "error" )
0 commit comments