Skip to content

Commit d68eee0

Browse files
author
Michael Kaiser
committed
Fix api-gateway-lambda-token-authorizer
1 parent 81dfc94 commit d68eee0

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

typescript/api-gateway-lambda-token-authorizer/test/stack/__snapshots__/gateway-lambda-auth-stack.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ exports[`Snapshot Stack 1`] = `
198198
"S3Bucket": {
199199
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}",
200200
},
201-
"S3Key": "2819175352ad1ce0dae768e83fc328fb70fb5f10b4a8ff0ccbcb791f02b0716d.zip",
201+
"S3Key": "NORMALIZED_ASSET_HASH.zip",
202202
},
203203
"Handler": "index.handler",
204204
"Role": {
@@ -322,7 +322,7 @@ exports[`Snapshot Stack 1`] = `
322322
"S3Bucket": {
323323
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}",
324324
},
325-
"S3Key": "2ad282c80ed8bef2dc8a20da8b07e92dd5259826cca0b685f8cc11f808eca9bb.zip",
325+
"S3Key": "NORMALIZED_ASSET_HASH.zip",
326326
},
327327
"Description": "Lambda Authorizer",
328328
"Environment": {
@@ -507,7 +507,7 @@ exports[`Snapshot Stack 1`] = `
507507
"S3Bucket": {
508508
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}",
509509
},
510-
"S3Key": "dc8dcb7f07403837f3ff1672cbf17e30ee3eaa1a3a058c5397b9db8b9e964b6b.zip",
510+
"S3Key": "NORMALIZED_ASSET_HASH.zip",
511511
},
512512
"Description": "Operational Lambda",
513513
"Environment": {

typescript/api-gateway-lambda-token-authorizer/test/stack/gateway-lambda-auth-stack.test.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,43 @@ import { App } from "aws-cdk-lib";
22
import { GatewayLambdaAuth } from "../../lib/stack/gateway-lambda-auth-stack";
33
import { Template } from "aws-cdk-lib/assertions";
44

5-
describe('Snapshot', () => {
5+
/**
6+
* Normalizes asset hashes in CloudFormation templates to ensure consistent snapshots
7+
* across different environments (local vs CI/CD).
8+
*/
9+
function normalizeAssetHashes(template: any): any {
10+
// Create a deep copy of the template to avoid modifying the original
11+
const templateCopy = JSON.parse(JSON.stringify(template));
12+
13+
// Function to recursively traverse the template and replace asset hashes
14+
function replaceAssetHashes(obj: any) {
15+
if (!obj || typeof obj !== 'object') return;
16+
17+
// Process object properties
18+
for (const key in obj) {
19+
if (key === 'S3Key' && typeof obj[key] === 'string' && /^[a-f0-9]{64}\.zip$/.test(obj[key])) {
20+
// Replace asset hash with a constant placeholder
21+
obj[key] = 'NORMALIZED_ASSET_HASH.zip';
22+
} else if (typeof obj[key] === 'object') {
23+
// Recursively process nested objects and arrays
24+
replaceAssetHashes(obj[key]);
25+
}
26+
}
27+
}
28+
29+
replaceAssetHashes(templateCopy);
30+
return templateCopy;
31+
}
632

33+
describe('Snapshot', () => {
734
it('Stack', () => {
835
const app = new App();
936
const stack = new GatewayLambdaAuth(app, 'test-api-gateway-lambda-auth');
1037
const template = Template.fromStack(stack);
11-
expect(template.toJSON()).toMatchSnapshot();
38+
39+
// Normalize asset hashes before snapshot comparison
40+
const normalizedTemplate = normalizeAssetHashes(template.toJSON());
41+
expect(normalizedTemplate).toMatchSnapshot();
1242
});
13-
1443
})
1544

0 commit comments

Comments
 (0)