From 51949858627377e7b89277d3d0b5d5f55f06cfe4 Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Tue, 31 Oct 2023 16:48:54 +0100 Subject: [PATCH] test(pipelines): create unit tests for approve lambda handler --- .../private/approve-lambda-handler.test.ts | 171 ++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 packages/aws-cdk-lib/pipelines/test/private/approve-lambda-handler.test.ts diff --git a/packages/aws-cdk-lib/pipelines/test/private/approve-lambda-handler.test.ts b/packages/aws-cdk-lib/pipelines/test/private/approve-lambda-handler.test.ts new file mode 100644 index 0000000000000..6e8a612abfd26 --- /dev/null +++ b/packages/aws-cdk-lib/pipelines/test/private/approve-lambda-handler.test.ts @@ -0,0 +1,171 @@ +const mockGetPipelineState = jest.fn(); +const mockPutApprovalResult = jest.fn(); +const mockCodePipeline = { + getPipelineState: mockGetPipelineState, + putApprovalResult: mockPutApprovalResult, +}; + +jest.mock('@aws-sdk/client-codepipeline', () => { + return { + CodePipeline: jest.fn(() => mockCodePipeline), + }; +}); + +jest.setTimeout(10_000); + +import { handler } from '../../lib/private/approve-lambda/index'; + +describe('approve-lambda handler', () => { + + beforeEach(() => { + jest.spyOn(global, 'setTimeout'); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + test('automatic approval works', async () => { + // GIVEN + mockGetPipelineState.mockImplementation(() => { + return { + stageStates: [{ + stageName: 'stage', + actionStates: [{ + actionName: 'action', + latestExecution: { + lastStatusChange: 1446137358.328, + status: 'Succeeded', + token: 'token', + }, + }], + }], + }; + }); + + const event = { + PipelineName: 'pipeline', + StageName: 'stage', + ActionName: 'action', + }; + + // WHEN + await handler(event, {}); + + // THEN + expect(mockPutApprovalResult).toHaveBeenCalled(); + }); + + test('does not approve if stageName not found', async () => { + // GIVEN + mockGetPipelineState.mockImplementation(async () => { + return { + stageStates: [{ + stageName: 'unknown', + actionStates: [{ + actionName: 'action', + latestExecution: { + lastStatusChange: 1446137358.328, + status: 'Succeeded', + token: 'token', + }, + }], + }], + }; + }); + + // expire deadline after first loop + jest.spyOn(Date, 'now') + .mockReturnValueOnce(0) + .mockReturnValueOnce(0) + .mockReturnValueOnce(10 * 60_000); + + const event = { + PipelineName: 'pipeline', + StageName: 'stage', + ActionName: 'action', + }; + + // WHEN + await handler(event, {}); + + // THEN + expect(setTimeout).toHaveBeenCalled(); + expect(mockPutApprovalResult).not.toHaveBeenCalled(); + }); + + test('does not approve if actionName not found', async () => { + // GIVEN + mockGetPipelineState.mockImplementation(async () => { + return { + stageStates: [{ + stageName: 'stage', + actionStates: [{ + actionName: 'unknown', + latestExecution: { + lastStatusChange: 1446137358.328, + status: 'Succeeded', + token: 'token', + }, + }], + }], + }; + }); + + // expire deadline after first loop + jest.spyOn(Date, 'now') + .mockReturnValueOnce(0) + .mockReturnValueOnce(0) + .mockReturnValueOnce(10 * 60_000); + + const event = { + PipelineName: 'pipeline', + StageName: 'stage', + ActionName: 'action', + }; + + // WHEN + await handler(event, {}); + + // THEN + expect(setTimeout).toHaveBeenCalled(); + expect(mockPutApprovalResult).not.toHaveBeenCalled(); + }); + + test('does not approve if token not present', async () => { + // GIVEN + mockGetPipelineState.mockImplementation(async () => { + return { + stageStates: [{ + stageName: 'stage', + actionStates: [{ + actionName: 'unknown', + latestExecution: { + lastStatusChange: 1446137358.328, + status: 'Succeeded', + }, + }], + }], + }; + }); + + // expire deadline after first loop + jest.spyOn(Date, 'now') + .mockReturnValueOnce(0) + .mockReturnValueOnce(0) + .mockReturnValueOnce(10 * 60_000); + + const event = { + PipelineName: 'pipeline', + StageName: 'stage', + ActionName: 'action', + }; + + // WHEN + await handler(event, {}); + + // THEN + expect(setTimeout).toHaveBeenCalled(); + expect(mockPutApprovalResult).not.toHaveBeenCalled(); + }); +});