Skip to content

Commit

Permalink
Added unit tests for organisation rulesets.
Browse files Browse the repository at this point in the history
  • Loading branch information
jitran committed Jan 15, 2025
1 parent 938631a commit 3e4250c
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 24 deletions.
4 changes: 2 additions & 2 deletions test/unit/lib/plugins/branches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('Branches', () => {

return plugin.sync().then(() => {
expect(github.repos.updateBranchProtection).toHaveBeenCalledWith({
owner: 'jitran',
owner: 'bkeepers',
repo: 'test',
branch: 'main',
required_status_checks: {
Expand Down Expand Up @@ -220,7 +220,7 @@ describe('Branches', () => {

return plugin.sync().then(() => {
expect(github.repos.updateBranchProtection).toHaveBeenCalledWith({
owner: 'jitran',
owner: 'bkeepers',
repo: 'test',
branch: 'main',
required_status_checks: {
Expand Down
193 changes: 171 additions & 22 deletions test/unit/lib/plugins/rulesets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,31 @@ const Rulesets = require('../../../../lib/plugins/rulesets')
const version = {
'X-GitHub-Api-Version': '2022-11-28'
}
const repo_conditions = {
ref_name: {
include: ['~ALL'],
exclude: []
},
}
const org_conditions = {
ref_name: {
include: ['~ALL'],
exclude: []
},
repository_name: {
include: ["~ALL"],
exclude: ["admin"]
}
}

function generateRequestRuleset(id, name, checks) {
return {
function generateRequestRuleset(id, name, conditions, checks) {
response = {
id: id,
name: name,
source_type: 'Repository',
target: 'branch',
enforcement: 'active',
conditions: {
ref_name: {
include: ['~ALL'],
exclude: []
}
},
conditions: conditions,
rules: [
{
type: 'required_status_checks',
Expand All @@ -29,23 +40,17 @@ function generateRequestRuleset(id, name, checks) {
}
]
}
return response
}

function generateResponseRuleset(id, name, checks) {
return {
function generateResponseRuleset(id, name, conditions, checks, org=false) {
response = {
id: id,
name: name,
source_type: 'Repository',
target: 'branch',
enforcement: 'active',
conditions: {
ref_name: {
include: ['~ALL'],
exclude: []
}
},
owner: 'jitran',
repo: 'test',
conditions: conditions,
rules: [
{
type: 'required_status_checks',
Expand All @@ -57,6 +62,13 @@ function generateResponseRuleset(id, name, checks) {
],
headers: version,
}
if (org) {
response.org = 'jitran'
} else {
response.owner = 'jitran'
response.repo = 'test'
}
return response
}

describe('Rulesets', () => {
Expand All @@ -65,10 +77,10 @@ describe('Rulesets', () => {
log.debug = jest.fn()
log.error = jest.fn()

function configure (config) {
function configure (config, scope='repo') {
const noop = false
const errors = []
return new Rulesets(noop, github, { owner: 'jitran', repo: 'test' }, config, log, errors)
return new Rulesets(noop, github, { owner: 'jitran', repo: 'test' }, config, log, errors, scope)
}

beforeEach(() => {
Expand All @@ -82,7 +94,7 @@ describe('Rulesets', () => {
},
request: jest.fn().mockImplementation(() => Promise.resolve('request')),
}

github.request.endpoint = {
merge: jest.fn().mockReturnValue({
method: 'GET',
Expand All @@ -103,6 +115,7 @@ describe('Rulesets', () => {
generateRequestRuleset(
1,
'All branches',
repo_conditions,
[
{ context: 'Status Check 1' },
{ context: 'Status Check 2' }
Expand All @@ -117,6 +130,7 @@ describe('Rulesets', () => {
generateResponseRuleset(
1,
'All branches',
repo_conditions,
[
{ context: 'Status Check 1' },
{ context: 'Status Check 2' }
Expand All @@ -138,6 +152,7 @@ describe('Rulesets', () => {
generateRequestRuleset(
1,
'All branches',
repo_conditions,
[
{ context: 'Status Check 1' },
{ context: '{{EXTERNALLY_DEFINED}}' }
Expand All @@ -152,6 +167,7 @@ describe('Rulesets', () => {
generateResponseRuleset(
1,
'All branches',
repo_conditions,
[]
)
)
Expand All @@ -166,6 +182,7 @@ describe('Rulesets', () => {
generateRequestRuleset(
1,
'All branches 1',
repo_conditions,
[
{ context: 'Custom Check 1' },
{ context: 'Custom Check 2' }
Expand All @@ -174,6 +191,7 @@ describe('Rulesets', () => {
generateRequestRuleset(
2,
'All branches 2',
repo_conditions,
[
{ context: 'Custom Check 3' },
{ context: 'Custom Check 4' }
Expand All @@ -182,6 +200,7 @@ describe('Rulesets', () => {
generateRequestRuleset(
3,
'All branches 3',
repo_conditions,
[
{ context: 'Custom Check 5' },
{ context: 'Custom Check 6' }
Expand All @@ -195,6 +214,7 @@ describe('Rulesets', () => {
generateRequestRuleset(
1,
'All branches 1',
repo_conditions,
[
{ context: 'Status Check 1' },
{ context: '{{EXTERNALLY_DEFINED}}' }
Expand All @@ -203,6 +223,7 @@ describe('Rulesets', () => {
generateRequestRuleset(
2,
'All branches 2',
repo_conditions,
[
{ context: 'Status Check 1' },
{ context: 'Status Check 2' }
Expand All @@ -211,6 +232,7 @@ describe('Rulesets', () => {
generateRequestRuleset(
3,
'All branches 3',
repo_conditions,
[]
)
]
Expand All @@ -223,6 +245,7 @@ describe('Rulesets', () => {
generateResponseRuleset(
1,
'All branches 1',
repo_conditions,
[
{ context: 'Custom Check 1' },
{ context: 'Custom Check 2' }
Expand All @@ -235,6 +258,7 @@ describe('Rulesets', () => {
generateResponseRuleset(
2,
'All branches 2',
repo_conditions,
[
{ context: 'Status Check 1' },
{ context: 'Status Check 2' }
Expand All @@ -247,11 +271,136 @@ describe('Rulesets', () => {
generateResponseRuleset(
3,
'All branches 3',
repo_conditions,
[]
)
)
})
})
})
// TODO: Write tests for org rulesets

describe('[org] sync', () => {
it('syncs ruleset settings', () => {
// Mock the GitHub API response
github.paginate = jest.fn().mockResolvedValue([])

// Initialise safe-settings
const plugin = configure(
[
generateRequestRuleset(
1,
'All branches',
org_conditions,
[
{ context: 'Status Check 1' },
{ context: 'Status Check 2' }
]
)
],
'org'
)

return plugin.sync().then(() => {
expect(github.request).toHaveBeenLastCalledWith(
'POST /orgs/{org}/rulesets',
generateResponseRuleset(
1,
'All branches',
org_conditions,
[
{ context: 'Status Check 1' },
{ context: 'Status Check 2' }
],
true
)
)
})
})
})

describe('[org] when {{EXTERNALLY_DEFINED}} is present in "required_status_checks" and no status checks exists in GitHub', () => {
it('it initialises the status checks with an empty list', () => {
// Mock the GitHub API response
github.paginate = jest.fn().mockResolvedValue([])

// Initialise safe-settings
const plugin = configure(
[
generateRequestRuleset(
1,
'All branches',
org_conditions,
[
{ context: 'Status Check 1' },
{ context: '{{EXTERNALLY_DEFINED}}' }
]
)
],
'org'
)

return plugin.sync().then(() => {
expect(github.request).toHaveBeenLastCalledWith(
'POST /orgs/{org}/rulesets',
generateResponseRuleset(
1,
'All branches',
org_conditions,
[],
true
)
)
})
})
})

describe('[org] when {{EXTERNALLY_DEFINED}} is present in "required_status_checks" and status checks exists in GitHub', () => {
it('it retains the status checks from GitHub', () => {
// Mock the GitHub API response
github.paginate = jest.fn().mockResolvedValue([
generateRequestRuleset(
1,
'All branches 1',
org_conditions,
[
{ context: 'Custom Check 1' },
{ context: 'Custom Check 2' }
]
)
])

// Initialise safe-settings
const plugin = configure(
[
generateRequestRuleset(
1,
'All branches 1',
org_conditions,
[
{ context: 'Status Check 1' },
{ context: '{{EXTERNALLY_DEFINED}}' }
]
)
],
'org'
)

return plugin.sync().then(() => {
expect(github.request).toHaveBeenNthCalledWith(
1,
'PUT /orgs/{org}/rulesets/{id}',
generateResponseRuleset(
1,
'All branches 1',
org_conditions,
[
{ context: 'Custom Check 1' },
{ context: 'Custom Check 2' }
],
true
)
)
})
})
})
})

0 comments on commit 3e4250c

Please sign in to comment.