Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(pipelines): create unit tests for approve lambda handler #27777

Merged
merged 2 commits into from
Nov 2, 2023
Merged
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
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();
});
});