diff --git a/lambdas/authorizer/src/__tests__/index.test.ts b/lambdas/authorizer/src/__tests__/index.test.ts index 4020b55e..cecfbe10 100644 --- a/lambdas/authorizer/src/__tests__/index.test.ts +++ b/lambdas/authorizer/src/__tests__/index.test.ts @@ -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 () => { @@ -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 () => { @@ -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, @@ -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(); }); }); diff --git a/lambdas/authorizer/src/authorizer.ts b/lambdas/authorizer/src/authorizer.ts index d5a0bd55..37b2224d 100644 --- a/lambdas/authorizer/src/authorizer.ts +++ b/lambdas/authorizer/src/authorizer.ts @@ -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)); } }