Skip to content

Commit b893e4a

Browse files
committed
Added unit tests for organisation rulesets.
1 parent ac36f25 commit b893e4a

File tree

2 files changed

+173
-24
lines changed

2 files changed

+173
-24
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe('Branches', () => {
181181

182182
return plugin.sync().then(() => {
183183
expect(github.repos.updateBranchProtection).toHaveBeenCalledWith({
184-
owner: 'jitran',
184+
owner: 'bkeepers',
185185
repo: 'test',
186186
branch: 'main',
187187
required_status_checks: {
@@ -220,7 +220,7 @@ describe('Branches', () => {
220220

221221
return plugin.sync().then(() => {
222222
expect(github.repos.updateBranchProtection).toHaveBeenCalledWith({
223-
owner: 'jitran',
223+
owner: 'bkeepers',
224224
repo: 'test',
225225
branch: 'main',
226226
required_status_checks: {

test/unit/lib/plugins/rulesets.test.js

Lines changed: 171 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,31 @@ const Rulesets = require('../../../../lib/plugins/rulesets')
55
const version = {
66
'X-GitHub-Api-Version': '2022-11-28'
77
}
8+
const repo_conditions = {
9+
ref_name: {
10+
include: ['~ALL'],
11+
exclude: []
12+
},
13+
}
14+
const org_conditions = {
15+
ref_name: {
16+
include: ['~ALL'],
17+
exclude: []
18+
},
19+
repository_name: {
20+
include: ["~ALL"],
21+
exclude: ["admin"]
22+
}
23+
}
824

9-
function generateRequestRuleset(id, name, checks) {
10-
return {
25+
function generateRequestRuleset(id, name, conditions, checks) {
26+
response = {
1127
id: id,
1228
name: name,
1329
source_type: 'Repository',
1430
target: 'branch',
1531
enforcement: 'active',
16-
conditions: {
17-
ref_name: {
18-
include: ['~ALL'],
19-
exclude: []
20-
}
21-
},
32+
conditions: conditions,
2233
rules: [
2334
{
2435
type: 'required_status_checks',
@@ -29,23 +40,17 @@ function generateRequestRuleset(id, name, checks) {
2940
}
3041
]
3142
}
43+
return response
3244
}
3345

34-
function generateResponseRuleset(id, name, checks) {
35-
return {
46+
function generateResponseRuleset(id, name, conditions, checks, org=false) {
47+
response = {
3648
id: id,
3749
name: name,
3850
source_type: 'Repository',
3951
target: 'branch',
4052
enforcement: 'active',
41-
conditions: {
42-
ref_name: {
43-
include: ['~ALL'],
44-
exclude: []
45-
}
46-
},
47-
owner: 'jitran',
48-
repo: 'test',
53+
conditions: conditions,
4954
rules: [
5055
{
5156
type: 'required_status_checks',
@@ -57,6 +62,13 @@ function generateResponseRuleset(id, name, checks) {
5762
],
5863
headers: version,
5964
}
65+
if (org) {
66+
response.org = 'jitran'
67+
} else {
68+
response.owner = 'jitran'
69+
response.repo = 'test'
70+
}
71+
return response
6072
}
6173

6274
describe('Rulesets', () => {
@@ -65,10 +77,10 @@ describe('Rulesets', () => {
6577
log.debug = jest.fn()
6678
log.error = jest.fn()
6779

68-
function configure (config) {
80+
function configure (config, scope='repo') {
6981
const noop = false
7082
const errors = []
71-
return new Rulesets(noop, github, { owner: 'jitran', repo: 'test' }, config, log, errors)
83+
return new Rulesets(noop, github, { owner: 'jitran', repo: 'test' }, config, log, errors, scope)
7284
}
7385

7486
beforeEach(() => {
@@ -82,7 +94,7 @@ describe('Rulesets', () => {
8294
},
8395
request: jest.fn().mockImplementation(() => Promise.resolve('request')),
8496
}
85-
97+
8698
github.request.endpoint = {
8799
merge: jest.fn().mockReturnValue({
88100
method: 'GET',
@@ -103,6 +115,7 @@ describe('Rulesets', () => {
103115
generateRequestRuleset(
104116
1,
105117
'All branches',
118+
repo_conditions,
106119
[
107120
{ context: 'Status Check 1' },
108121
{ context: 'Status Check 2' }
@@ -117,6 +130,7 @@ describe('Rulesets', () => {
117130
generateResponseRuleset(
118131
1,
119132
'All branches',
133+
repo_conditions,
120134
[
121135
{ context: 'Status Check 1' },
122136
{ context: 'Status Check 2' }
@@ -138,6 +152,7 @@ describe('Rulesets', () => {
138152
generateRequestRuleset(
139153
1,
140154
'All branches',
155+
repo_conditions,
141156
[
142157
{ context: 'Status Check 1' },
143158
{ context: '{{EXTERNALLY_DEFINED}}' }
@@ -152,6 +167,7 @@ describe('Rulesets', () => {
152167
generateResponseRuleset(
153168
1,
154169
'All branches',
170+
repo_conditions,
155171
[]
156172
)
157173
)
@@ -166,6 +182,7 @@ describe('Rulesets', () => {
166182
generateRequestRuleset(
167183
1,
168184
'All branches 1',
185+
repo_conditions,
169186
[
170187
{ context: 'Custom Check 1' },
171188
{ context: 'Custom Check 2' }
@@ -174,6 +191,7 @@ describe('Rulesets', () => {
174191
generateRequestRuleset(
175192
2,
176193
'All branches 2',
194+
repo_conditions,
177195
[
178196
{ context: 'Custom Check 3' },
179197
{ context: 'Custom Check 4' }
@@ -182,6 +200,7 @@ describe('Rulesets', () => {
182200
generateRequestRuleset(
183201
3,
184202
'All branches 3',
203+
repo_conditions,
185204
[
186205
{ context: 'Custom Check 5' },
187206
{ context: 'Custom Check 6' }
@@ -195,6 +214,7 @@ describe('Rulesets', () => {
195214
generateRequestRuleset(
196215
1,
197216
'All branches 1',
217+
repo_conditions,
198218
[
199219
{ context: 'Status Check 1' },
200220
{ context: '{{EXTERNALLY_DEFINED}}' }
@@ -203,6 +223,7 @@ describe('Rulesets', () => {
203223
generateRequestRuleset(
204224
2,
205225
'All branches 2',
226+
repo_conditions,
206227
[
207228
{ context: 'Status Check 1' },
208229
{ context: 'Status Check 2' }
@@ -211,6 +232,7 @@ describe('Rulesets', () => {
211232
generateRequestRuleset(
212233
3,
213234
'All branches 3',
235+
repo_conditions,
214236
[]
215237
)
216238
]
@@ -223,6 +245,7 @@ describe('Rulesets', () => {
223245
generateResponseRuleset(
224246
1,
225247
'All branches 1',
248+
repo_conditions,
226249
[
227250
{ context: 'Custom Check 1' },
228251
{ context: 'Custom Check 2' }
@@ -235,6 +258,7 @@ describe('Rulesets', () => {
235258
generateResponseRuleset(
236259
2,
237260
'All branches 2',
261+
repo_conditions,
238262
[
239263
{ context: 'Status Check 1' },
240264
{ context: 'Status Check 2' }
@@ -247,11 +271,136 @@ describe('Rulesets', () => {
247271
generateResponseRuleset(
248272
3,
249273
'All branches 3',
274+
repo_conditions,
250275
[]
251276
)
252277
)
253278
})
254279
})
255280
})
256-
// TODO: Write tests for org rulesets
281+
282+
describe('[org] sync', () => {
283+
it('syncs ruleset settings', () => {
284+
// Mock the GitHub API response
285+
github.paginate = jest.fn().mockResolvedValue([])
286+
287+
// Initialise safe-settings
288+
const plugin = configure(
289+
[
290+
generateRequestRuleset(
291+
1,
292+
'All branches',
293+
org_conditions,
294+
[
295+
{ context: 'Status Check 1' },
296+
{ context: 'Status Check 2' }
297+
]
298+
)
299+
],
300+
'org'
301+
)
302+
303+
return plugin.sync().then(() => {
304+
expect(github.request).toHaveBeenLastCalledWith(
305+
'POST /orgs/{org}/rulesets',
306+
generateResponseRuleset(
307+
1,
308+
'All branches',
309+
org_conditions,
310+
[
311+
{ context: 'Status Check 1' },
312+
{ context: 'Status Check 2' }
313+
],
314+
true
315+
)
316+
)
317+
})
318+
})
319+
})
320+
321+
describe('[org] when {{EXTERNALLY_DEFINED}} is present in "required_status_checks" and no status checks exists in GitHub', () => {
322+
it('it initialises the status checks with an empty list', () => {
323+
// Mock the GitHub API response
324+
github.paginate = jest.fn().mockResolvedValue([])
325+
326+
// Initialise safe-settings
327+
const plugin = configure(
328+
[
329+
generateRequestRuleset(
330+
1,
331+
'All branches',
332+
org_conditions,
333+
[
334+
{ context: 'Status Check 1' },
335+
{ context: '{{EXTERNALLY_DEFINED}}' }
336+
]
337+
)
338+
],
339+
'org'
340+
)
341+
342+
return plugin.sync().then(() => {
343+
expect(github.request).toHaveBeenLastCalledWith(
344+
'POST /orgs/{org}/rulesets',
345+
generateResponseRuleset(
346+
1,
347+
'All branches',
348+
org_conditions,
349+
[],
350+
true
351+
)
352+
)
353+
})
354+
})
355+
})
356+
357+
describe('[org] when {{EXTERNALLY_DEFINED}} is present in "required_status_checks" and status checks exists in GitHub', () => {
358+
it('it retains the status checks from GitHub', () => {
359+
// Mock the GitHub API response
360+
github.paginate = jest.fn().mockResolvedValue([
361+
generateRequestRuleset(
362+
1,
363+
'All branches 1',
364+
org_conditions,
365+
[
366+
{ context: 'Custom Check 1' },
367+
{ context: 'Custom Check 2' }
368+
]
369+
)
370+
])
371+
372+
// Initialise safe-settings
373+
const plugin = configure(
374+
[
375+
generateRequestRuleset(
376+
1,
377+
'All branches 1',
378+
org_conditions,
379+
[
380+
{ context: 'Status Check 1' },
381+
{ context: '{{EXTERNALLY_DEFINED}}' }
382+
]
383+
)
384+
],
385+
'org'
386+
)
387+
388+
return plugin.sync().then(() => {
389+
expect(github.request).toHaveBeenNthCalledWith(
390+
1,
391+
'PUT /orgs/{org}/rulesets/{id}',
392+
generateResponseRuleset(
393+
1,
394+
'All branches 1',
395+
org_conditions,
396+
[
397+
{ context: 'Custom Check 1' },
398+
{ context: 'Custom Check 2' }
399+
],
400+
true
401+
)
402+
)
403+
})
404+
})
405+
})
257406
})

0 commit comments

Comments
 (0)