Skip to content

Commit ac36f25

Browse files
committed
Added unit tests.
1 parent 3c6790f commit ac36f25

File tree

2 files changed

+325
-0
lines changed

2 files changed

+325
-0
lines changed

test/unit/lib/plugins/branches.test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,74 @@ describe('Branches', () => {
165165
})
166166
})
167167

168+
describe('when {{EXTERNALLY_DEFINED}} is present in "required_status_checks" and no status checks exists in GitHub', () => {
169+
it('it initialises the status checks with an empty list', () => {
170+
const plugin = configure(
171+
[{
172+
name: 'main',
173+
protection: {
174+
required_status_checks: {
175+
strict: true,
176+
contexts: ['{{travis-ci', '{{EXTERNALLY_DEFINED}}']
177+
}
178+
}
179+
}]
180+
)
181+
182+
return plugin.sync().then(() => {
183+
expect(github.repos.updateBranchProtection).toHaveBeenCalledWith({
184+
owner: 'jitran',
185+
repo: 'test',
186+
branch: 'main',
187+
required_status_checks: {
188+
strict: true,
189+
contexts: []
190+
},
191+
headers: { accept: 'application/vnd.github.hellcat-preview+json,application/vnd.github.luke-cage-preview+json,application/vnd.github.zzzax-preview+json' }
192+
})
193+
})
194+
})
195+
})
196+
197+
describe('when {{EXTERNALLY_DEFINED}} is present in "required_status_checks" and status checks exists in GitHub', () => {
198+
it('it retains the status checks from GitHub', () => {
199+
github.repos.getBranchProtection = jest.fn().mockResolvedValue({
200+
data: {
201+
enforce_admins: { enabled: false },
202+
protection: {
203+
required_status_checks: {
204+
contexts: ['check-1', 'check-2']
205+
}
206+
}
207+
}
208+
})
209+
const plugin = configure(
210+
[{
211+
name: 'main',
212+
protection: {
213+
required_status_checks: {
214+
strict: true,
215+
contexts: ['{{travis-ci', '{{EXTERNALLY_DEFINED}}']
216+
}
217+
}
218+
}]
219+
)
220+
221+
return plugin.sync().then(() => {
222+
expect(github.repos.updateBranchProtection).toHaveBeenCalledWith({
223+
owner: 'jitran',
224+
repo: 'test',
225+
branch: 'main',
226+
required_status_checks: {
227+
strict: true,
228+
contexts: ['check-1', 'check-2']
229+
},
230+
headers: { accept: 'application/vnd.github.hellcat-preview+json,application/vnd.github.luke-cage-preview+json,application/vnd.github.zzzax-preview+json' }
231+
})
232+
})
233+
})
234+
})
235+
168236
describe('when multiple branches are configured', () => {
169237
it('updates them each appropriately', () => {
170238
const plugin = configure(
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
/* eslint-disable no-undef */
2+
3+
const { when } = require('jest-when')
4+
const Rulesets = require('../../../../lib/plugins/rulesets')
5+
const version = {
6+
'X-GitHub-Api-Version': '2022-11-28'
7+
}
8+
9+
function generateRequestRuleset(id, name, checks) {
10+
return {
11+
id: id,
12+
name: name,
13+
source_type: 'Repository',
14+
target: 'branch',
15+
enforcement: 'active',
16+
conditions: {
17+
ref_name: {
18+
include: ['~ALL'],
19+
exclude: []
20+
}
21+
},
22+
rules: [
23+
{
24+
type: 'required_status_checks',
25+
parameters: {
26+
strict_required_status_checks_policy: true,
27+
required_status_checks: checks
28+
}
29+
}
30+
]
31+
}
32+
}
33+
34+
function generateResponseRuleset(id, name, checks) {
35+
return {
36+
id: id,
37+
name: name,
38+
source_type: 'Repository',
39+
target: 'branch',
40+
enforcement: 'active',
41+
conditions: {
42+
ref_name: {
43+
include: ['~ALL'],
44+
exclude: []
45+
}
46+
},
47+
owner: 'jitran',
48+
repo: 'test',
49+
rules: [
50+
{
51+
type: 'required_status_checks',
52+
parameters: {
53+
strict_required_status_checks_policy: true,
54+
required_status_checks: checks
55+
}
56+
}
57+
],
58+
headers: version,
59+
}
60+
}
61+
62+
describe('Rulesets', () => {
63+
let github
64+
const log = jest.fn()
65+
log.debug = jest.fn()
66+
log.error = jest.fn()
67+
68+
function configure (config) {
69+
const noop = false
70+
const errors = []
71+
return new Rulesets(noop, github, { owner: 'jitran', repo: 'test' }, config, log, errors)
72+
}
73+
74+
beforeEach(() => {
75+
github = {
76+
repos: {
77+
get: jest.fn().mockResolvedValue({
78+
data: {
79+
default_branch: 'main'
80+
}
81+
})
82+
},
83+
request: jest.fn().mockImplementation(() => Promise.resolve('request')),
84+
}
85+
86+
github.request.endpoint = {
87+
merge: jest.fn().mockReturnValue({
88+
method: 'GET',
89+
url: '/repos/jitran/test/rulesets',
90+
headers: version
91+
})
92+
}
93+
})
94+
95+
describe('sync', () => {
96+
it('syncs ruleset settings', () => {
97+
// Mock the GitHub API response
98+
github.paginate = jest.fn().mockResolvedValue([])
99+
100+
// Initialise safe-settings
101+
const plugin = configure(
102+
[
103+
generateRequestRuleset(
104+
1,
105+
'All branches',
106+
[
107+
{ context: 'Status Check 1' },
108+
{ context: 'Status Check 2' }
109+
]
110+
)
111+
]
112+
)
113+
114+
return plugin.sync().then(() => {
115+
expect(github.request).toHaveBeenLastCalledWith(
116+
'POST /repos/{owner}/{repo}/rulesets',
117+
generateResponseRuleset(
118+
1,
119+
'All branches',
120+
[
121+
{ context: 'Status Check 1' },
122+
{ context: 'Status Check 2' }
123+
]
124+
)
125+
)
126+
})
127+
})
128+
})
129+
130+
describe('when {{EXTERNALLY_DEFINED}} is present in "required_status_checks" and no status checks exists in GitHub', () => {
131+
it('it initialises the status checks with an empty list', () => {
132+
// Mock the GitHub API response
133+
github.paginate = jest.fn().mockResolvedValue([])
134+
135+
// Initialise safe-settings
136+
const plugin = configure(
137+
[
138+
generateRequestRuleset(
139+
1,
140+
'All branches',
141+
[
142+
{ context: 'Status Check 1' },
143+
{ context: '{{EXTERNALLY_DEFINED}}' }
144+
]
145+
)
146+
]
147+
)
148+
149+
return plugin.sync().then(() => {
150+
expect(github.request).toHaveBeenLastCalledWith(
151+
'POST /repos/{owner}/{repo}/rulesets',
152+
generateResponseRuleset(
153+
1,
154+
'All branches',
155+
[]
156+
)
157+
)
158+
})
159+
})
160+
})
161+
162+
describe('when {{EXTERNALLY_DEFINED}} is present in "required_status_checks" and status checks exists in GitHub', () => {
163+
it('it retains the status checks from GitHub and everything else is reset to the safe-settings', () => {
164+
// Mock the GitHub API response
165+
github.paginate = jest.fn().mockResolvedValue([
166+
generateRequestRuleset(
167+
1,
168+
'All branches 1',
169+
[
170+
{ context: 'Custom Check 1' },
171+
{ context: 'Custom Check 2' }
172+
]
173+
),
174+
generateRequestRuleset(
175+
2,
176+
'All branches 2',
177+
[
178+
{ context: 'Custom Check 3' },
179+
{ context: 'Custom Check 4' }
180+
]
181+
),
182+
generateRequestRuleset(
183+
3,
184+
'All branches 3',
185+
[
186+
{ context: 'Custom Check 5' },
187+
{ context: 'Custom Check 6' }
188+
]
189+
)
190+
])
191+
192+
// Initialise safe-settings
193+
const plugin = configure(
194+
[
195+
generateRequestRuleset(
196+
1,
197+
'All branches 1',
198+
[
199+
{ context: 'Status Check 1' },
200+
{ context: '{{EXTERNALLY_DEFINED}}' }
201+
]
202+
),
203+
generateRequestRuleset(
204+
2,
205+
'All branches 2',
206+
[
207+
{ context: 'Status Check 1' },
208+
{ context: 'Status Check 2' }
209+
]
210+
),
211+
generateRequestRuleset(
212+
3,
213+
'All branches 3',
214+
[]
215+
)
216+
]
217+
)
218+
219+
return plugin.sync().then(() => {
220+
expect(github.request).toHaveBeenNthCalledWith(
221+
1,
222+
'PUT /repos/{owner}/{repo}/rulesets/{id}',
223+
generateResponseRuleset(
224+
1,
225+
'All branches 1',
226+
[
227+
{ context: 'Custom Check 1' },
228+
{ context: 'Custom Check 2' }
229+
]
230+
)
231+
)
232+
expect(github.request).toHaveBeenNthCalledWith(
233+
2,
234+
'PUT /repos/{owner}/{repo}/rulesets/{id}',
235+
generateResponseRuleset(
236+
2,
237+
'All branches 2',
238+
[
239+
{ context: 'Status Check 1' },
240+
{ context: 'Status Check 2' }
241+
]
242+
)
243+
)
244+
expect(github.request).toHaveBeenNthCalledWith(
245+
3,
246+
'PUT /repos/{owner}/{repo}/rulesets/{id}',
247+
generateResponseRuleset(
248+
3,
249+
'All branches 3',
250+
[]
251+
)
252+
)
253+
})
254+
})
255+
})
256+
// TODO: Write tests for org rulesets
257+
})

0 commit comments

Comments
 (0)