8000 Merge pull request #516 from sigmavirus24/refactor-requires-auth-tests · pljensen/github3.py@5869154 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5869154

Browse files
Merge pull request sigmavirus24#516 from sigmavirus24/refactor-requires-auth-tests
Add RequiresAuth Helper class
2 parents 1948122 + 5a77dc7 commit 5869154

11 files changed

+97
-118
lines changed

tests/unit/helper.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,13 @@ def tearDown(self):
202202
"""Stop mocking _get_json."""
203203
super(UnitIteratorHelper, self).tearDown()
204204
self.get_json_mock.stop()
205+
206+
207+
class UnitRequiresAuthenticationHelper(UnitHelper):
208+
209+
"""Helper for unit tests that demonstrate authentication is required."""
210+
211+
def after_setup(self):
212+
"""Disable authentication on the session."""
213+
self.session.auth = None
214+
self.session.has_auth.return_value = False

tests/unit/test_auths.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
import github3
33
import pytest
44

5-
from .helper import UnitHelper, create_url_helper, create_example_data_helper
5+
from . import helper
66

7-
url_for = create_url_helper('https://api.github.com/authorizations/1')
7+
url_for = helper.create_url_helper('https://api.github.com/authorizations/1')
88

99

10-
class TestAuthorization(UnitHelper):
10+
class TestAuthorization(helper.UnitHelper):
1111

1212
"""Authorization unit tests."""
1313

1414
described_class = github3.auths.Authorization
15-
get_auth_example_data = create_example_data_helper('authorization_example')
15+
get_auth_example_data = helper.create_example_data_helper(
16+
'authorization_example'
17+
)
1618
example_data = get_auth_example_data()
1719

1820
def test_add_scopes(self):
@@ -46,7 +48,7 @@ def test_replace_scopes(self):
4648
})
4749

4850

49-
class TestAuthorizationRequiresAuth(UnitHelper):
51+
class TestAuthorizationRequiresAuth(helper.UnitRequiresAuthenticationHelper):
5052

5153
"""Test methods that require authentication on Authorization."""
5254

tests/unit/test_gists.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@
22
import pytest
33
import github3
44

5-
from .helper import (create_example_data_helper, create_url_helper,
6-
UnitHelper, UnitIteratorHelper)
5+
from . import helper
76

8-
gist_example_data = create_example_data_helper('gist_example')
9-
gist_example_short_data = create_example_data_helper('gist_example_short')
10-
gist_history_example_data = create_example_data_helper('gist_history_example')
11-
gist_comment_example_data = create_example_data_helper('gist_comment_example')
7+
gist_example_data = helper.create_example_data_helper('gist_example')
8+
gist_example_short_data = helper.create_example_data_helper(
9+
'gist_example_short'
10+
)
11+
gist_history_example_data = helper.create_example_data_helper(
12+
'gist_history_example'
13+
)
14+
gist_comment_example_data = helper.create_example_data_helper(
15+
'gist_comment_example'
16+
)
1217

13-
url_for = create_url_helper(
18+
url_for = helper.create_url_helper(
1419
'https://api.github.com/gists/b4c7ac7be6e591d0d155'
1520
)
1621

1722

18-
class TestGist(UnitHelper):
23+
class TestGist(helper.UnitHelper):
1924

2025
"""Test regular Gist methods."""
2126

@@ -95,17 +100,13 @@ def test_to_str(self):
95100
assert str(self.instance) == str(self.instance.id)
96101

97102

98-
class TestGistRequiresAuth(UnitHelper):
103+
class TestGistRequiresAuth(helper.UnitRequiresAuthenticationHelper):
99104

100105
"""Test Gist methods which require authentication."""
101106

102107
described_class = github3.gists.Gist
103108
example_data = gist_example_data()
104109

105-
def after_setup(self):
106-
"""Disable authentication."""
107-
self.session.has_auth.return_value = False
108-
109110
def test_create_comment(self):
110111
"""Show that a user needs to authenticate to create a comment."""
111112
with pytest.raises(github3.GitHubError):
@@ -142,7 +143,7 @@ def test_unstar(self):
142143
self.instance.unstar()
143144

144145

145-
class TestGistIterators(UnitIteratorHelper):
146+
class TestGistIterators(helper.UnitIteratorHelper):
146147

147148
"""Test Gist methods that return Iterators."""
148149

@@ -190,7 +191,7 @@ def test_forks(self):
190191
)
191192

192193

193-
class TestGistHistory(UnitHelper):
194+
class TestGistHistory(helper.UnitHelper):
194195

195196
"""Test Gist History."""
196197

@@ -207,7 +208,7 @@ def test_equality(self):
207208
assert self.instance != history
208209

209210

210-
class TestGistComment(UnitHelper):
211+
class TestGistComment(helper.UnitHelper):
211212

212213
"""Test Gist Comments."""
213214

@@ -228,21 +229,19 @@ def test_repr(self):
228229
assert repr(self.instance).startswith('<Gist Comment')
229230

230231

231-
class TestGistFile(UnitHelper):
232+
class TestGistFile(helper.UnitHelper):
232233

233234
"""Test Gist File."""
234235

235236
described_class = github3.gists.file.GistFile
236237
example_data = gist_example_short_data()
237238

238239
def test_no_original_content(self):
239-
"""Show that attribute original content is None"""
240+
"""Show that attribute original content is None."""
240241
assert self.instance.original_content is None
241242

242243
def test_get_file_content_from_raw_url(self):
243-
"""Show that Get request is made to Gist raw_url when method content
244-
is called.
245-
"""
244+
"""Verify the request made to retrieve a GistFile's content."""
246245
self.instance.content()
247246

248247
self.session.get.assert_called_once_with(self.instance.raw_url)

tests/unit/test_github.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
from github3 import AuthenticationFailed, GitHubError
44
from github3.github import GitHub
55

6-
from .helper import UnitHelper, UnitIteratorHelper
6+
from . import helper
77

88

99
def url_for(path=''):
1010
"""Simple function to generate URLs with the base GitHub URL."""
1111
return 'https://api.github.com/' + path.strip('/')
1212

1313

14-
class TestGitHub(UnitHelper):
14+
class TestGitHub(helper.UnitHelper):
1515
described_class = GitHub
1616
example_data = None
1717

@@ -345,7 +345,7 @@ def test_user_with_id_accepts_a_string(self):
345345
self.session.get.assert_called_once_with(url_for('user/10'))
346346

347347

348-
class TestGitHubIterators(UnitIteratorHelper):
348+
class TestGitHubIterators(helper.UnitIteratorHelper):
349349
described_class = GitHub
350350
example_data = None
351351

@@ -868,18 +868,14 @@ def test_repositories_by_with_type(self):
868868
)
869869

870870

871-
class TestGitHubRequiresAuthentication(UnitHelper):
871+
class TestGitHubRequiresAuthentication(
872+
helper.UnitRequiresAuthenticationHelper):
872873

873874
"""Test methods that require authentication."""
874875

875876
described_class = GitHub
876877
example_data = None
877878

878-
def after_setup(self):
879-
"""Disable authentication on the session."""
880-
self.session.auth = None
881-
self.session.has_auth.return_value = False
882-
883879
def test_add_email_addresses(self):
884880
"""Verify a user must be authenticated to add email addresses."""
885881
with pytest.raises(AuthenticationFailed):
@@ -991,7 +987,7 @@ def test_user_issues(self):
991987
self.instance.user_issues()
992988

993989

994-
class TestGitHubAuthorizations(UnitHelper):
990+
class TestGitHubAuthorizations(helper.UnitHelper):
995991
described_class = GitHub
996992
example_data = None
997993

tests/unit/test_issues_issue.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,17 +332,13 @@ def test_labels(self):
332332
)
333333

334334

335-
class TestLabelRequiresAuth(helper.UnitHelper):
335+
class TestLabelRequiresAuth(helper.UnitRequiresAuthenticationHelper):
336336

337337
"""Test that ensure certain methods on Label class requires auth."""
338338

339339
described_class = github3.issues.label.Label
340340
example_data = get_issue_label_example_data()
341341

342-
def after_setup(self):
343-
"""Disable authentication on sessions."""
344-
self.session.has_auth.return_value = False
345-
346342
def test_delete(self):
347343
"""Test that deleting a label requires authentication."""
348344
with pytest.raises(github3.AuthenticationFailed):

tests/unit/test_issues_milestone.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@
33
import github3
44
import pytest
55

6-
from .helper import (UnitIteratorHelper, UnitHelper, create_url_helper,
7-
create_example_data_helper)
6+
from . import helper
87

9-
get_milestone_example_data = create_example_data_helper('milestone_example')
8+
get_milestone_example_data = helper.create_example_data_helper(
9+
'milestone_example'
10+
)
1011
example_data = get_milestone_example_data()
1112

12-
url_for = create_url_helper("https://api.github.com/repos/octocat/Hello-World/"
13-
"milestones/1")
13+
url_for = helper.create_url_helper("https://api.github.com/repos/octocat/"
14+
"Hello-World/milestones/1")
1415

1516

16-
class TestMilestoneRequiresAuth(UnitHelper):
17+
class TestMilestoneRequiresAuth(helper.UnitRequiresAuthenticationHelper):
1718
"""Test Milestone methods that require authentication."""
1819

1920
described_class = github3.issues.milestone.Milestone
2021
example_data = example_data
2122

22-
def after_setup(self):
23-
self.session.has_auth.return_value = False
24-
2523
def test_delete(self):
2624
"""Test that deleting milestone requires authentication."""
2725
with pytest.raises(github3.AuthenticationFailed):
@@ -39,7 +37,7 @@ def test_update(self):
3937
self.instance.update(**data)
4038

4139

42-
class TestMilestone(UnitHelper):
40+
class TestMilestone(helper.UnitHelper):
4341
"""Test Milestone methods."""
4442

4543
described_class = github3.issues.milestone.Milestone
@@ -98,7 +96,7 @@ def test_update_no_parameters(self):
9896
assert self.session.post.called is False
9997

10098

101-
class TestMilestoneIterator(UnitIteratorHelper):
99+
class TestMilestoneIterator(helper.UnitIteratorHelper):
102100

103101
"""Test Milestone methods that return iterators."""
104102

tests/unit/test_orgs.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
from github3 import GitHubError
44
from github3.orgs import Organization
55

6-
from .helper import (UnitHelper, UnitIteratorHelper, create_url_helper,
7-
create_example_data_helper)
6+
from . import helper
87

9-
url_for = create_url_helper('https://api.github.com/orgs/github')
8+
url_for = helper.create_url_helper('https://api.github.com/orgs/github')
109

11-
get_org_example_data = create_example_data_helper('org_example')
10+
get_org_example_data = helper.create_example_data_helper('org_example')
1211

1312

14-
class TestOrganization(UnitHelper):
13+
class TestOrganization(helper.UnitHelper):
1514
described_class = Organization
1615
example_data = get_org_example_data()
1716

@@ -154,14 +153,10 @@ def test_team_requires_positive_team_id(self):
154153
assert self.session.get.called is False
155154

156155

157-
class TestOrganizationRequiresAuth(UnitHelper):
156+
class TestOrganizationRequiresAuth(helper.UnitRequiresAuthenticationHelper):
158157
described_class = Organization
159158
example_data = get_org_example_data()
160159

161-
def after_setup(self):
162-
"""Set MockedSession#has_auth.return_value to False."""
163-
self.session.has_auth.return_value = False
164-
165160
def test_add_member(self):
166161
"""Show that one must be authenticated to add a member to an org."""
167162
with pytest.raises(GitHubError):
@@ -213,7 +208,7 @@ def test_team(self):
213208
self.instance.team(10)
214209

215210

216-
class TestOrganizationIterator(UnitIteratorHelper):
211+
class TestOrganizationIterator(helper.UnitIteratorHelper):
217212
described_class = Organization
218213

219214
example_data = {

tests/unit/test_orgs_team.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
from github3 import GitHubError
44
from github3.orgs import Team
55

6-
from .helper import (UnitHelper, UnitIteratorHelper, create_url_helper,
7-
create_example_data_helper)
6+
from . import helper
87

9-
url_for = create_url_helper('https://api.github.com/teams/10')
8+
url_for = helper.create_url_helper('https://api.github.com/teams/10')
109

11-
get_team_example_data = create_example_data_helper('orgs_team_example')
10+
get_team_example_data = helper.create_example_data_helper('orgs_team_example')
1211

1312

14-
class TestTeam(UnitHelper):
13+
class TestTeam(helper.UnitHelper):
1514
described_class = Team
1615
example_data = get_team_example_data()
1716

@@ -67,14 +66,10 @@ def test_remove_repository(self):
6766
self.session.delete.assert_called_once_with(url_for('/repos/repo'))
6867

6968

70-
class TestTeamRequiresAuth(UnitHelper):
69+
class TestTeamRequiresAuth(helper.UnitRequiresAuthenticationHelper):
7170
described_class = Team
7271
example_data = get_team_example_data()
7372

74-
def after_setup(self):
75-
"""Set up for test cases in TestT 8842 eamRequiresAuth."""
76-
self.session.has_auth.return_value = False
77-
7873
def test_add_member_requires_auth(self):
7974
"""Show that adding a repo to a team requires authentication."""
8075
with pytest.raises(GitHubError):
@@ -116,7 +111,7 @@ def test_remove_repository_requires_auth(self):
116111
self.instance.remove_repository('repo')
117112

118113

119-
class TestTeamIterator(UnitIteratorHelper):
114+
class TestTeamIterator(helper.UnitIteratorHelper):
120115
described_class = Team
121116

122117
example_data = {

0 commit comments

Comments
 (0)
0