Skip to content

Commit f26b262

Browse files
committed
Add unit tests around GitHubSession
1 parent ad79943 commit f26b262

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

tests/unit/test_github_session.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import pytest
2+
3+
from github3 import session
4+
5+
6+
class TestGitHubSession:
7+
def build_session(self, base_url=None):
8+
s = session.GitHubSession()
9+
if base_url:
10+
s.base_url = base_url
11+
return s
12+
13+
def test_has_default_headers(self):
14+
"""Assert the default headers are there upon initialization"""
15+
s = self.build_session()
16+
assert 'Accept' in s.headers
17+
assert s.headers['Accept'] == 'application/vnd.github.v3.full+json'
18+
assert 'Accept-Charset' in s.headers
19+
assert s.headers['Accept-Charset'] == 'utf-8'
20+
assert 'Content-Type' in s.headers
21+
assert s.headers['Content-Type'] == 'application/json'
22+
assert 'User-Agent' in s.headers
23+
assert s.headers['User-Agent'].startswith('github3.py/')
24+
25+
def test_build_url(self):
26+
"""Test that GitHubSessions build basic URLs"""
27+
s = self.build_session()
28+
url = s.build_url('gists', '123456', 'history')
29+
assert url == 'https://api.github.com/gists/123456/history'
30+
31+
def test_build_url_caches_built_urls(self):
32+
"""Test that building a URL caches it"""
33+
s = self.build_session()
34+
url = s.build_url('gists', '123456', 'history')
35+
url_parts = ('https://api.github.com', 'gists', '123456', 'history')
36+
assert url_parts in session.__url_cache__
37+
assert url in session.__url_cache__.values()
38+
39+
def test_build_url_uses_a_different_base(self):
40+
"""Test that you can pass in a different base URL to build_url"""
41+
s = self.build_session()
42+
url = s.build_url('gists', '123456', 'history',
43+
base_url='https://status.github.com')
44+
assert url == 'https://status.github.com/gists/123456/history'
45+
46+
def test_build_url_respects_the_session_base_url(self):
47+
"""Test that build_url uses the session's base_url"""
48+
s = self.build_session('https://enterprise.customer.com')
49+
url = s.build_url('gists')
50+
assert url == 'https://enterprise.customer.com/gists'
51+
52+
def test_basic_login_does_not_use_falsey_values(self):
53+
"""Test that basic auth will not authenticate with falsey values"""
54+
bad_auths = [
55+
(None, 'password'),
56+
('username', None),
57+
('', 'password'),
58+
('username', ''),
59+
]
60+
for auth in bad_auths:
61+
# Make sure we have a clean session to test with
62+
s = self.build_session()
63+
s.basic_auth(*auth)
64+
assert s.auth != auth
65+
66+
def test_basic_login(self):
67+
"""Test that basic auth will work with a valid combination"""
68+
s = self.build_session()
69+
s.basic_auth('username', 'password')
70+
assert s.auth == ('username', 'password')
71+
72+
def test_token_auth(self):
73+
"""Test that token auth will work with a valid token"""
74+
s = self.build_session()
75+
s.token_auth('token goes here')
76+
assert s.headers['Authorization'] == 'token token goes here'
77+
78+
def test_token_auth_does_not_use_falsey_values(self):
79+
"""Test that token auth will not authenticate with falsey values"""
80+
bad_tokens = [None, '']
81+
for token in bad_tokens:
82+
s = self.build_session()
83+
s.token_auth(token)
84+
assert 'Authorization' not in s.headers
85+
86+
def test_oauth2_auth(self):
87+
"""Test that oauth2 authentication works
88+
89+
For now though, it doesn't because it isn't implemented.
90+
"""
91+
s = self.build_session()
92+
with pytest.raises(NotImplementedError):
93+
s.oauth2_auth('Foo', 'bar')

0 commit comments

Comments
 (0)