Skip to content

Commit c34e553

Browse files
authored
Merge pull request sigmavirus24#870 from jacquerie/add-unit-tests-for-branch-protection
Add unit tests for protect and unprotect methods
2 parents e2f594b + a63725b commit c34e553

File tree

1 file changed

+111
-2
lines changed

1 file changed

+111
-2
lines changed

tests/unit/test_repos_branch.py

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
from . import helper
44

55
get_example_data = helper.create_example_data_helper('repos_branch_example')
6-
url_for = helper.create_url_helper(
6+
url_for_branches = helper.create_url_helper(
7+
'https://api.github.com/repos/octocat/Hello-World/branches/master'
8+
)
9+
url_for_commits = helper.create_url_helper(
710
'https://api.github.com/repos/octocat/Hello-World/commits/master'
811
)
912

@@ -22,6 +25,112 @@ def test_latest_sha(self):
2225
}
2326
self.instance.latest_sha(differs_from='123')
2427
self.session.get.assert_called_once_with(
25-
url_for(),
28+
url_for_commits(),
2629
headers=headers
2730
)
31+
32+
def test_protect(self):
33+
"""Verify the request to protect a branch."""
34+
headers = {
35+
'Accept': 'application/vnd.github.loki-preview+json'
36+
}
37+
json = {
38+
'protection': {
39+
'enabled': True,
40+
'required_status_checks': {
41+
'enforcement_level': 'non_admins',
42+
'contexts': [
43+
'continuous-integration/travis-ci'
44+
]
45+
}
46+
}
47+
}
48+
49+
self.instance.protect()
50+
self.session.patch.assert_called_once_with(
51+
url_for_branches(),
52+
headers=headers,
53+
json=json
54+
)
55+
56+
def test_protect_enforcement(self):
57+
"""Verify the request to protect a branch changing enforcement."""
58+
headers = {
59+
'Accept': 'application/vnd.github.loki-preview+json'
60+
}
61+
json = {
62+
'protection': {
63+
'enabled': True,
64+
'required_status_checks': {
65+
'enforcement_level': 'off',
66+
'contexts': [
67+
'continuous-integration/travis-ci'
68+
]
69+
}
70+
}
71+
}
72+
73+
self.instance.protect(enforcement='off')
74+
self.session.patch.assert_called_once_with(
75+
url_for_branches(),
76+
headers=headers,
77+
json=json
78+
)
79+
80+
def test_protect_status_checks(self):
81+
"""Verify the request to protect a branch changing status checks."""
82+
headers = {
83+
'Accept': 'application/vnd.github.loki-preview+json'
84+
}
85+
json = {
86+
'protection': {
87+
'enabled': True,
88+
'required_status_checks': {
89+
'enforcement_level': 'non_admins',
90+
'contexts': [
91+
'another/status-check'
92+
]
93+
}
94+
}
95+
}
96+
97+
self.instance.protect(status_checks=['another/status-check'])
98+
self.session.patch.assert_called_once_with(
99+
url_for_branches(),
100+
headers=headers,
101+
json=json
102+
)
103+
104+
def test_unprotect(self):
105+
"""Verify the request to unprotect a branch."""
106+
headers = {
107+
'Accept': 'application/vnd.github.loki-preview+json'
108+
}
109+
json = {
110+
'protection': {
111+
'enabled': False
112+
}
113+
}
114+
115+
self.instance.unprotect()
116+
self.session.patch.assert_called_once_with(
117+
url_for_branches(),
118+
headers=headers,
119+
json=json
120+
)
121+
122+
123+
class TestBranchRequiresAuth(helper.UnitRequiresAuthenticationHelper):
124+
125+
"""Unit tests for Branch methods that require authentication."""
126+
127+
described_class = github3.repos.branch.Branch
128+
example_data = get_example_data()
129+
130+
def test_protect(self):
131+
"""Verify that protecting a branch requires authentication."""
132+
self.assert_requires_auth(self.instance.protect)
133+
134+
def test_unprotect(self):
135+
"""Verify that unprotecting a branch requires authentication."""
136+
self.assert_requires_auth(self.instance.unprotect)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy