@@ -8,11 +8,15 @@ const mockedDeps: jest.Mocked<Deps> = {
88 logger : { info : jest . fn ( ) , error : jest . fn ( ) } as unknown as pino . Logger ,
99 env : {
1010 CLOUDWATCH_NAMESPACE : 'cloudwatch-namespace' ,
11- CLIENT_CERTIFICATE_EXPIRATION_ALERT_DAYS : 14
11+ CLIENT_CERTIFICATE_EXPIRATION_ALERT_DAYS : 14 ,
12+ APIM_APPLICATION_ID_HEADER : 'apim-application-id'
1213 } as unknown as EnvVars ,
1314 cloudWatchClient : {
1415 send : jest . fn ( ) . mockResolvedValue ( { } ) ,
1516 } as any ,
17+ supplierRepo : {
18+ getSupplierByApimId : jest . fn ( ) ,
19+ } as any ,
1620 } as Deps ;
1721
1822
@@ -34,100 +38,6 @@ describe('Authorizer Lambda Function', () => {
3438 mockCallback = jest . fn ( ) ;
3539 } ) ;
3640
37- it ( 'Should allow access when headers match' , async ( ) => {
38- mockEvent . headers = { headerauth1 : 'headervalue1' } ;
39-
40- const handler = createAuthorizerHandler ( mockedDeps ) ;
41- handler ( mockEvent , mockContext , mockCallback ) ;
42- await new Promise ( process . nextTick ) ;
43-
44- expect ( mockCallback ) . toHaveBeenCalledWith ( null , expect . objectContaining ( {
45- policyDocument : expect . objectContaining ( {
46- Statement : expect . arrayContaining ( [
47- expect . objectContaining ( {
48- Effect : 'Allow' ,
49- } ) ,
50- ] ) ,
51- } ) ,
52- } ) ) ;
53- } ) ;
54-
55- it ( 'Should deny access when headers do not match' , async ( ) => {
56- mockEvent . headers = { headerauth1 : 'wrongValue' } ;
57-
58- const handler = createAuthorizerHandler ( mockedDeps ) ;
59- handler ( mockEvent , mockContext , mockCallback ) ;
60- await new Promise ( process . nextTick ) ;
61-
62- expect ( mockCallback ) . toHaveBeenCalledWith ( null , expect . objectContaining ( {
63- policyDocument : expect . objectContaining ( {
64- Statement : expect . arrayContaining ( [
65- expect . objectContaining ( {
66- Effect : 'Deny' ,
67- } ) ,
68- ] ) ,
69- } ) ,
70- } ) ) ;
71- } ) ;
72-
73- it ( 'Should handle null headers gracefully' , async ( ) => {
74- mockEvent . headers = null ;
75-
76- const handler = createAuthorizerHandler ( mockedDeps ) ;
77- handler ( mockEvent , mockContext , mockCallback ) ;
78- await new Promise ( process . nextTick ) ;
79-
80- expect ( mockCallback ) . toHaveBeenCalledWith ( null , expect . objectContaining ( {
81- policyDocument : expect . objectContaining ( {
82- Statement : expect . arrayContaining ( [
83- expect . objectContaining ( {
84- Effect : 'Deny' ,
85- } ) ,
86- ] ) ,
87- } ) ,
88- } ) ) ;
89- } ) ;
90-
91- it ( 'Should handle defined headers correctly' , async ( ) => {
92- mockEvent . headers = { headerauth1 : 'headervalue1' } ;
93-
94- const handler = createAuthorizerHandler ( mockedDeps ) ;
95- handler ( mockEvent , mockContext , mockCallback ) ;
96- await new Promise ( process . nextTick ) ;
97-
98- expect ( mockCallback ) . toHaveBeenCalledWith ( null , expect . objectContaining ( {
99- policyDocument : expect . objectContaining ( {
100- Statement : expect . arrayContaining ( [
101- expect . objectContaining ( {
102- Effect : 'Allow' ,
103- } ) ,
104- ] ) ,
105- } ) ,
106- } ) ) ;
107- } ) ;
108-
109- it ( 'Should handle additional headers correctly' , async ( ) => {
110- mockEvent . headers = {
111- headerauth1 : 'headervalue1' ,
112- otherheader1 : 'headervalue2' ,
113- otherheader2 : 'headervalue3'
114- } ;
115-
116- const handler = createAuthorizerHandler ( mockedDeps ) ;
117- handler ( mockEvent , mockContext , mockCallback ) ;
118- await new Promise ( process . nextTick ) ;
119-
120- expect ( mockCallback ) . toHaveBeenCalledWith ( null , expect . objectContaining ( {
121- policyDocument : expect . objectContaining ( {
122- Statement : expect . arrayContaining ( [
123- expect . objectContaining ( {
124- Effect : 'Allow' ,
125- } ) ,
126- ] ) ,
127- } ) ,
128- } ) ) ;
129- } ) ;
130-
13141 describe ( 'Certificate expiry check' , ( ) => {
13242
13343 beforeEach ( ( ) => {
@@ -140,7 +50,6 @@ describe('Authorizer Lambda Function', () => {
14050 } )
14151
14252 it ( 'Should not send CloudWatch metric when certificate is null' , async ( ) => {
143- mockEvent . headers = { headerauth1 : 'headervalue1' } ;
14453 mockEvent . requestContext . identity . clientCert = null ;
14554
14655 const handler = createAuthorizerHandler ( mockedDeps ) ;
@@ -151,7 +60,6 @@ describe('Authorizer Lambda Function', () => {
15160 } ) ;
15261
15362 it ( 'Should send CloudWatch metric when the certificate expiry threshold is reached' , async ( ) => {
154- mockEvent . headers = { headerauth1 : 'headervalue1' } ;
15563 mockEvent . requestContext . identity . clientCert = buildCertWithExpiry ( '2025-11-17T14:19:00Z' ) ;
15664
15765 const handler = createAuthorizerHandler ( mockedDeps ) ;
@@ -177,7 +85,6 @@ describe('Authorizer Lambda Function', () => {
17785 } ) ;
17886
17987 it ( 'Should not send CloudWatch metric when the certificate expiry threshold is not yet reached' , async ( ) => {
180- mockEvent . headers = { headerauth1 : 'headervalue1' } ;
18188 mockEvent . requestContext . identity . clientCert = buildCertWithExpiry ( '2025-11-18T14:19:00Z' ) ;
18289
18390 const handler = createAuthorizerHandler ( mockedDeps ) ;
@@ -197,4 +104,88 @@ describe('Authorizer Lambda Function', () => {
197104 } as APIGatewayEventClientCertificate [ 'validity' ] ,
198105 } as APIGatewayEventClientCertificate ;
199106 }
107+
108+ describe ( 'Supplier ID lookup' , ( ) => {
109+
110+ it ( 'Should deny the request when no headers are present' , async ( ) => {
111+ mockEvent . headers = null ;
112+
113+ const handler = createAuthorizerHandler ( mockedDeps ) ;
114+ handler ( mockEvent , mockContext , mockCallback ) ;
115+ await new Promise ( process . nextTick ) ;
116+
117+ expect ( mockCallback ) . toHaveBeenCalledWith ( null , expect . objectContaining ( {
118+ policyDocument : expect . objectContaining ( {
119+ Statement : [
120+ expect . objectContaining ( {
121+ Effect : 'Deny' ,
122+ } ) ,
123+ ] ,
124+ } ) ,
125+ } ) ) ;
126+ } ) ;
127+
128+ it ( 'Should deny the request when the APIM application ID header is absent' , async ( ) => {
129+ mockEvent . headers = { 'x-apim-correlation-id' : 'correlation-id' } ;
130+
131+ const handler = createAuthorizerHandler ( mockedDeps ) ;
132+ handler ( mockEvent , mockContext , mockCallback ) ;
133+ await new Promise ( process . nextTick ) ;
134+
135+ expect ( mockCallback ) . toHaveBeenCalledWith ( null , expect . objectContaining ( {
136+ policyDocument : expect . objectContaining ( {
137+ Statement : [
138+ expect . objectContaining ( {
139+ Effect : 'Deny' ,
140+ } ) ,
141+ ] ,
142+ } ) ,
143+ } ) ) ;
144+ } ) ;
145+
146+ it ( 'Should deny the request when no supplier ID is found' , async ( ) => {
147+ mockEvent . headers = { 'apim-application-id' : 'unknown-apim-id' } ;
148+ ( mockedDeps . supplierRepo . getSupplierByApimId as jest . Mock ) . mockRejectedValue ( new Error ( 'Supplier not found' ) ) ;
149+
150+ const handler = createAuthorizerHandler ( mockedDeps ) ;
151+ handler ( mockEvent , mockContext , mockCallback ) ;
152+ await new Promise ( process . nextTick ) ;
153+
154+ expect ( mockCallback ) . toHaveBeenCalledWith ( null , expect . objectContaining ( {
155+ policyDocument : expect . objectContaining ( {
156+ Statement : [
157+ expect . objectContaining ( {
158+ Effect : 'Deny' ,
159+ } ) ,
160+ ] ,
161+ } ) ,
162+ } ) ) ;
163+ } ) ;
164+
165+ it ( 'Should allow the request when the supplier ID is found' , async ( ) => {
166+ mockEvent . headers = { 'apim-application-id' : 'valid-apim-id' } ;
167+ ( mockedDeps . supplierRepo . getSupplierByApimId as jest . Mock ) . mockResolvedValue ( {
168+ id : 'supplier-123' ,
169+ apimApplicationId : 'valid-apim-id' ,
170+ name : 'Test Supplier' ,
171+ } ) ;
172+
173+ const handler = createAuthorizerHandler ( mockedDeps ) ;
174+ handler ( mockEvent , mockContext , mockCallback ) ;
175+ await new Promise ( process . nextTick ) ;
176+
177+ expect ( mockCallback ) . toHaveBeenCalledWith ( null , expect . objectContaining ( {
178+ policyDocument : expect . objectContaining ( {
179+ Statement : [
180+ expect . objectContaining ( {
181+ Effect : 'Allow' ,
182+ } ) ,
183+ ] ,
184+ } ) ,
185+ context : {
186+ supplierId : 'supplier-123' ,
187+ } ,
188+ } ) ) ;
189+ } ) ;
190+ } ) ;
200191} ) ;
0 commit comments