Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions lambdas/authorizer/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ describe("Authorizer Lambda Function", () => {
});

describe("Certificate expiry check", () => {
let consoleLogSpy: jest.SpyInstance;

beforeEach(() => {
jest
.useFakeTimers({ doNotFake: ["nextTick"] })
.setSystemTime(new Date("2025-11-03T14:19:00Z"));
consoleLogSpy = jest.spyOn(console, "log").mockImplementation();
});

afterEach(() => {
jest.useRealTimers();
consoleLogSpy.mockRestore();
});

it("Should not log CloudWatch metric when certificate is null", async () => {
Expand All @@ -73,10 +77,7 @@ describe("Authorizer Lambda Function", () => {
handler(mockEvent, mockContext, mockCallback);
await new Promise(process.nextTick);

const mockedInfo = mockedDeps.logger.info as jest.Mock;
expect(mockedInfo.mock.calls).not.toContainEqual(
expect.stringContaining("CloudWatchMetrics"),
);
expect(consoleLogSpy).not.toHaveBeenCalled();
});

it("Should log CloudWatch metric when the certificate expiry threshold is reached", async () => {
Expand All @@ -88,8 +89,7 @@ describe("Authorizer Lambda Function", () => {
handler(mockEvent, mockContext, mockCallback);
await new Promise(process.nextTick);

const mockedInfo = mockedDeps.logger.info as jest.Mock;
expect(mockedInfo.mock.calls.map((call) => call[0])).toContain(
expect(consoleLogSpy).toHaveBeenCalledWith(
JSON.stringify({
_aws: {
Timestamp: 1_762_179_540_000,
Expand Down Expand Up @@ -123,10 +123,7 @@ describe("Authorizer Lambda Function", () => {
handler(mockEvent, mockContext, mockCallback);
await new Promise(process.nextTick);

const mockedInfo = mockedDeps.logger.info as jest.Mock;
expect(mockedInfo.mock.calls).not.toContainEqual(
expect.stringContaining("CloudWatchMetrics"),
);
expect(consoleLogSpy).not.toHaveBeenCalled();
});
});

Expand Down
9 changes: 5 additions & 4 deletions lambdas/authorizer/src/authorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ async function checkCertificateExpiry(
const expiry = getCertificateExpiryInDays(certificate);

if (expiry <= deps.env.CLIENT_CERTIFICATE_EXPIRATION_ALERT_DAYS) {
deps.logger.info(
JSON.stringify(
buildCloudWatchMetric(deps.env.CLOUDWATCH_NAMESPACE, certificate),
),
const metric = buildCloudWatchMetric(
deps.env.CLOUDWATCH_NAMESPACE,
certificate,
);
deps.logger.warn(metric, `APIM Certificated expiry in ${expiry} days`);
console.log(JSON.stringify(metric));
}
}
Loading