Skip to content

Commit

Permalink
test(pipelines): create unit tests for approve lambda handler (#27777)
Browse files Browse the repository at this point in the history
Adds unit test for approve lambda handler.

Closes #27727.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
lpizzinidev authored Nov 2, 2023
1 parent b02752b commit b217e6b
Showing 1 changed file with 171 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit b217e6b

Please sign in to comment.