Skip to content

Commit b217e6b

Browse files
authored
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*
1 parent b02752b commit b217e6b

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
const mockGetPipelineState = jest.fn();
2+
const mockPutApprovalResult = jest.fn();
3+
const mockCodePipeline = {
4+
getPipelineState: mockGetPipelineState,
5+
putApprovalResult: mockPutApprovalResult,
6+
};
7+
8+
jest.mock('@aws-sdk/client-codepipeline', () => {
9+
return {
10+
CodePipeline: jest.fn(() => mockCodePipeline),
11+
};
12+
});
13+
14+
jest.setTimeout(10_000);
15+
16+
import { handler } from '../../lib/private/approve-lambda/index';
17+
18+
describe('approve-lambda handler', () => {
19+
20+
beforeEach(() => {
21+
jest.spyOn(global, 'setTimeout');
22+
});
23+
24+
afterEach(() => {
25+
jest.clearAllMocks();
26+
});
27+
28+
test('automatic approval works', async () => {
29+
// GIVEN
30+
mockGetPipelineState.mockImplementation(() => {
31+
return {
32+
stageStates: [{
33+
stageName: 'stage',
34+
actionStates: [{
35+
actionName: 'action',
36+
latestExecution: {
37+
lastStatusChange: 1446137358.328,
38+
status: 'Succeeded',
39+
token: 'token',
40+
},
41+
}],
42+
}],
43+
};
44+
});
45+
46+
const event = {
47+
PipelineName: 'pipeline',
48+
StageName: 'stage',
49+
ActionName: 'action',
50+
};
51+
52+
// WHEN
53+
await handler(event, {});
54+
55+
// THEN
56+
expect(mockPutApprovalResult).toHaveBeenCalled();
57+
});
58+
59+
test('does not approve if stageName not found', async () => {
60+
// GIVEN
61+
mockGetPipelineState.mockImplementation(async () => {
62+
return {
63+
stageStates: [{
64+
stageName: 'unknown',
65+
actionStates: [{
66+
actionName: 'action',
67+
latestExecution: {
68+
lastStatusChange: 1446137358.328,
69+
status: 'Succeeded',
70+
token: 'token',
71+
},
72+
}],
73+
}],
74+
};
75+
});
76+
77+
// expire deadline after first loop
78+
jest.spyOn(Date, 'now')
79+
.mockReturnValueOnce(0)
80+
.mockReturnValueOnce(0)
81+
.mockReturnValueOnce(10 * 60_000);
82+
83+
const event = {
84+
PipelineName: 'pipeline',
85+
StageName: 'stage',
86+
ActionName: 'action',
87+
};
88+
89+
// WHEN
90+
await handler(event, {});
91+
92+
// THEN
93+
expect(setTimeout).toHaveBeenCalled();
94+
expect(mockPutApprovalResult).not.toHaveBeenCalled();
95+
});
96+
97+
test('does not approve if actionName not found', async () => {
98+
// GIVEN
99+
mockGetPipelineState.mockImplementation(async () => {
100+
return {
101+
stageStates: [{
102+
stageName: 'stage',
103+
actionStates: [{
104+
actionName: 'unknown',
105+
latestExecution: {
106+
lastStatusChange: 1446137358.328,
107+
status: 'Succeeded',
108+
token: 'token',
109+
},
110+
}],
111+
}],
112+
};
113+
});
114+
115+
// expire deadline after first loop
116+
jest.spyOn(Date, 'now')
117+
.mockReturnValueOnce(0)
118+
.mockReturnValueOnce(0)
119+
.mockReturnValueOnce(10 * 60_000);
120+
121+
const event = {
122+
PipelineName: 'pipeline',
123+
StageName: 'stage',
124+
ActionName: 'action',
125+
};
126+
127+
// WHEN
128+
await handler(event, {});
129+
130+
// THEN
131+
expect(setTimeout).toHaveBeenCalled();
132+
expect(mockPutApprovalResult).not.toHaveBeenCalled();
133+
});
134+
135+
test('does not approve if token not present', async () => {
136+
// GIVEN
137+
mockGetPipelineState.mockImplementation(async () => {
138+
return {
139+
stageStates: [{
140+
stageName: 'stage',
141+
actionStates: [{
142+
actionName: 'unknown',
143+
latestExecution: {
144+
lastStatusChange: 1446137358.328,
145+
status: 'Succeeded',
146+
},
147+
}],
148+
}],
149+
};
150+
});
151+
152+
// expire deadline after first loop
153+
jest.spyOn(Date, 'now')
154+
.mockReturnValueOnce(0)
155+
.mockReturnValueOnce(0)
156+
.mockReturnValueOnce(10 * 60_000);
157+
158+
const event = {
159+
PipelineName: 'pipeline',
160+
StageName: 'stage',
161+
ActionName: 'action',
162+
};
163+
164+
// WHEN
165+
await handler(event, {});
166+
167+
// THEN
168+
expect(setTimeout).toHaveBeenCalled();
169+
expect(mockPutApprovalResult).not.toHaveBeenCalled();
170+
});
171+
});

0 commit comments

Comments
 (0)