-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(pipelines): create unit tests for approve lambda handler (#27777)
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
1 parent
b02752b
commit b217e6b
Showing
1 changed file
with
171 additions
and
0 deletions.
There are no files selected for viewing
171 changes: 171 additions & 0 deletions
171
packages/aws-cdk-lib/pipelines/test/private/approve-lambda-handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |