8000 Split out authenticated user logic into GitHub#me · pythonthings/github3.py@9e8b01f · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e8b01f

Browse files
committed
Split out authenticated user logic into GitHub#me
1 parent e0ac97f commit 9e8b01f

File tree

6 files changed

+60
-30
lines changed

6 files changed

+60
-30
lines changed

LATEST_VERSION_NOTES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ Old name New name
164164

165165
- ``GitHub#starred`` which iterates over the authenticated user's stars
166166

167+
- ``GitHub#user`` was split into two functions:
168+
169+
- ``GitHub#user`` which retrieves an arbitrary user's information
170+
171+
- ``GitHub#me`` which retrieves the authenticated user's information
172+
167173
- Remove legacy watching API:
168174

169175
- ``GitHub#subscribe``

github3/github.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,21 @@ def markdown(self, text, mode='', context='', raw=False):
10301030
return req.content
10311031
return '' # (No coverage)
10321032

1033+
@requires_auth
1034+
def me(self):
1035+
"""Retrieves the info for the authenticated user.
1036+
1037+
.. versionadded:: 1.0
1038+
1039+
This was separated from the ``user`` method.
1040+
1041+
:returns: The representation of the authenticated user.
1042+
:rtype: :class:`User <github3.users.User>`
1043+
"""
1044+
url = self._build_url('user')
1045+
json = self._json(self._get(url), 200)
1046+
return User(json, self) if json else None
1047+
10331048
def meta(self):
10341049
"""Returns a dictionary with arrays of addresses in CIDR format
10351050
specifying theaddresses that the incoming service hooks will originate
@@ -1496,19 +1511,13 @@ def update_user(self, name=None, email=None, blog=None,
14961511
return user.update(name, email, blog, company, location, hireable,
14971512
bio)
14981513

1499-
def user(self, username=None):
1500-
"""Returns a User object for the specified user name if
1501-
provided. If no user name is provided, this will return a User
1502-
object for the authenticated user.
1514+
def user(self, username):
1515+
"""Returns a User object for the specified user name.
15031516
1504-
:param str username: (optional)
1517+
:param str username: name of the user
15051518
:returns: :class:`User <github3.users.User>`
15061519
"""
1507-
if username:
1508-
url = self._build_url('users', username)
1509-
else:
1510-
url = self._build_url('user')
1511-
1520+
url = self._build_url('users', username)
15121521
json = self._json(self._get(url), 200)
15131522
return User(json, self) if json else None
15141523

tests/cassettes/GitHub_user.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"http_interactions": [{"request": {"body": "", "headers": {"Accept-Charset": "utf-8", "Content-Type": "application/json", "Accept-Encoding": "gzip, deflate, compress", "Accept": "application/vnd.github.v3.full+json", "User-Agent": "github3.py/0.8.0"}, "method": "GET", "uri": "https://api.github.com/users/sigmavirus24"}, "response": {"body": {"base64_string": "H4sIAAAAAAAAA51UTYvbMBD9K4tge/JG/owdw9IvKOyhty2FvYSxJDuismQk2Wkatr+9Iye7zeayJCCwsOa9eTPSmz1RppOa1MTJrodJ2tGlOYmI5KRO87jK4ojABB7serQK4zbeD66mNFl09nCwYKanhy1lSV5lxZJXq3aVFmK5SpOyaoRI+CoD1pYf+f1McJt9vk2/4ZJcaC+Z0W7RSb8Zm8CG/3Mok3KZx1nOOIiqLPMqb0SZlknVQJEuF4PuPtj73yj1Rcc6aCbvKUDA20JgkCep6eiEdfSsGxvfq7P6/6s9D26NUmaLLGeIdxPRVySKPOyl7q5kQeSeGr8ReHFY0nNolHT+clEzak/DBzsceBy+Biv4xcKOOJS11ahoT60YzEw4No5ZOXiJ7+By2lM0shnbgZZ/4Do2RDskCdIulzKjEC0mfNWXww+wPR2snIDtQmusYEJO2OwrKc/wyOh3g0Cf/MBHEVovvVgD78MMaEE5ERENfQh4AH3z1VgOjhkMRFsOoHd48EVovtsa+ytU2uD8OE4FHArb7Rb92yk525j+RWPPBHQOi3DYsPlaEPEduHRGRzc/H0LDepBhuqCX2xY8MwoVdWb6hENJqkCGQRtpBTQKxR2VNtKQWo9KRWQYGyXZ+nABdVG9GAhtSOqiOPETzrUSy0Eqj20Fj1nTOInv4vwuiR/jvI7LOkufMN848Dcx2V2S4HqMszrDsOUTef4HMcXtQEAFAAA=", "encoding": "utf-8"}, "headers": {"status": "200 OK", "x-ratelimit-remaining": "54", "x-github-media-type": "github.v3; param=full; format=json", "x-content-type-options": "nosniff", "access-control-expose-headers": "ETag, Link, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes", "transfer-encoding": "chunked", "x-github-request-id": "4270737C:68E2:3DCEB02:528053EA", "content-encoding": "gzip", "vary": "Accept, Accept-Encoding", "server": "GitHub.com", "cache-control": "public, max-age=60, s-maxage=60", "last-modified": "Mon, 11 Nov 2013 03:34:06 GMT", "x-ratelimit-limit": "60", "etag": "\"160d470101abf70f0f6288962c03b4c0\"", "access-control-allow-credentials": "true", "date": "Mon, 11 Nov 2013 03:50:02 GMT", "access-control-allow-origin": "*", "content-type": "application/json; charset=utf-8", "x-ratelimit-reset": "1384142732"}, "url": "https://api.github.com/users/sigmavirus24", "status_code": 200}, "recorded_at": "2013-11-11T03:49:57"}, {"request": {"body": "", "headers": {"Accept-Encoding": "gzip, deflate, compress", "Accept": "application/vnd.github.v3.full+json", "User-Agent": "github3.py/0.8.0", "Accept-Charset": "utf-8", "Content-Type": "application/json", "Authorization": "Basic <BASIC_AUTH>"}, "method": "GET", "uri": "https://api.github.com/user"}, "response": {"body": {"base64_string": "H4sIAAAAAAAAA51UXWvbMBT9K8PQPaXxd+wYyr5g0Ie9dQz2Eq4l2RFVJCPJybLQ/fYdOenWhkJJwWAj33Puuefq3kOkTC911ERO9hvaSju6rIhmkeRRkxVJnSeziLbkya5GqxC39n5wTRyn894ef8yZ2cTHz5ilRZ2XC14vu2VWisUyS6u6FSLly5xYV33gNxPBVf7pKvuKR3KhvWRGu3kv/XpsAxvOC6rSalEkecE4ibqqirpoRZVVad1SmS3mg+7f25tfkPqoYxU0R68pAOB5ITTIJ6nj0Qnr4jM31n6jzur/r/Y8uDNKmR1YzhCvJor/ISHy+C11/0YWIA+x8WuBxqGkh2CUdP5yURPqEIcXHA48DrfBCn6xsBMOsnYaig6xFYOZCMfWMSsHL3EPLqd9igabsT1p+Zvexga0A0mQdrmUCQW02OJWXw4/wg7xYOWW2D5YYwUTcguz30h5hgej3w8Cc/IdlyJYL71YEd+EHdCRcmIWadqEgFvS774Yy8kxg0CM5UB6jx+fheb7nbH3odIW++O0FbAUdrsd5rdXchrj+A8GeyKIp7AZlg2b2gLEN+LSGT179+M2GLYhGbYLZrnryDOjoKg3249YSlIFMgStpRXUKog7KW2liRo9KjWLhrFVkq2ODWjK+nGAMIZRU5ZP5gl7rUI5oPKwlTyyZkmaXCfFdZrcJUWTVE2e/US+ceDPYvLrNMVzl+RNjrBFiPHGk1pNDYORp/TYmeGK8xfOUfQ9BpJ6VJGXebYIzipFrbHkTRAL8KAIFh0eO9FZIUKrBmIBlVRZgqAXYGcqHh7+ApuOoKvfBQAA", "encoding": "utf-8"}, "headers": {"status": "200 OK", "x-ratelimit-remaining": "4989", "x-github-media-type": "github.v3; param=full; format=json", "x-content-type-options": "nosniff", "access-control-expose-headers": "ETag, Link, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes", "transfer-encoding": "chunked", "x-github-request-id": "4270737C:68E2:3DCEB36:528053EA", "content-encoding": "gzip", "vary": "Accept, Authorization, Cookie, Accept-Encoding", "server": "GitHub.com", "cache-control": "private, max-age=60, s-maxage=60", "last-modified": "Mon, 11 Nov 2013 03:34:06 GMT", "x-ratelimit-limit": "5000", "etag": "\"ad2ba35fd2abf3043b07ca1548457630\"", "access-control-allow-credentials": "true", "date": "Mon, 11 Nov 2013 03:50:02 GMT", "access-control-allow-origin": "*", "content-type": "application/json; charset=utf-8", "x-ratelimit-reset": "1384142699"}, "url": "https://api.github.com/user", "status_code": 200}, "recorded_at": "2013-11-11T03:49:57"}], "recorded_with": "betamax"}
1+
{"http_interactions": [{"request": {"body": {"string": "", "encoding": "utf-8"}, "headers": {"Accept-Charset": "utf-8", "Content-Type": "application/json", "Accept-Encoding": "gzip, deflate", "Accept": "application/vnd.github.v3.full+json", "User-Agent": "github3.py/1.0.0b1"}, "method": "GET", "uri": "https://api.github.com/users/sigmavirus24"}, "response": {"body": {"string": "", "base64_string": "H4sIAAAAAAAAA52UQY+bMBCF/8rKZ7IGQkJiqWqlnvbQS7VVpb1EgzFkVGMj2xCl0fa3dwzpapdLlZxAZr43z4OfL0zbFg0TzGPbwYhu8HnBEoY1E3mR7tZpwmCEAO4wOE11xxB6LzifF/1ji+E4VINXTloTlAmP0nZ84DP8efyUk1rrrhpRlsms2K0323q3b/b5Rm33eVbuKqWyer8G2ZQELHr1eO0zi1MzzxeGj6HTC4uztQlZFDdWa3sileWm/teIv5Fkcn5H096pQuSF23BUNFva0mscFPpwu6mJuvD4OGAddTz9MKfqm41dObJ1MuTowp3q7SQ4VF467ANac7vBDzSpWdeCwd9wnxrRnkSitdutTBTRaqSzejs+YxfeOxxBnuNonJIKRxr2nZILnhTDuVeUkx90KOLoMagD1F2MaQPaq4QZ6GLBE5iHr9bV4KWlQjroPZgzffgO8pfvQSparSjh19xSbE+nEwWi1VhNufiDcub5VJbQdSCnv0LEN6jRW5M8/HyK8+oAY/4pyk0DQVpNhlo7fqFrA3UUo6IjOgWVJm9XoxVaJsygdcL6odIoD/P8RVm+rUynl4nN5l+iKJdMZFnxLmF0GW1pg6QeaNAQyEieZukqLVZZ+pwWIi3FOn8hC0Nff6gpVul+lWbP2UasS1FsX9jrXxxAQ2z1BAAA", "encoding": "utf-8"}, "headers": {"vary": "Accept, Accept-Encoding", "x-served-by": "62a1303ae95931e56e387e87d354bb24", "x-xss-protection": "1; mode=block", "x-content-type-options": "nosniff", "etag": "\"73ff57ac28d81e87cc04567e35682b1a\"", "access-control-allow-credentials": "true", "status": "200 OK", "x-ratelimit-remaining": "55", "x-github-media-type": "github.v3; param=full; format=json", "access-control-expose-headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval", "transfer-encoding": "chunked", "x-github-request-id": "45A4CA97:255A:24E6AC9:5404AFE2", "cache-control": "public, max-age=60, s-maxage=60", "last-modified": "Mon, 01 Sep 2014 15:37:46 GMT", "date": "Mon, 01 Sep 2014 17:41:54 GMT", "access-control-allow-origin": "*", "content-security-policy": "default-src 'none'", "content-encoding": "gzip", "strict-transport-security": "max-age=31536000; includeSubdomains; preload", "server": "GitHub.com", "x-ratelimit-limit": "60", "x-frame-options": "deny", "content-type": "application/json; charset=utf-8", "x-ratelimit-reset": "1409596753"}, "status": {"message": "OK", "code": 200}, "url": "https://api.github.com/users/sigmavirus24"}, "recorded_at": "2014-09-01T17:41:54"}], "recorded_with": "betamax/0.4.0"}

tests/integration/test_github.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,11 @@ def test_search_repositories_with_text_match(self):
371371

372372
def test_user(self):
373373
"""Test the ability to retrieve a User."""
374-
self.token_login()
375374
cassette_name = self.cassette_name('user')
376375
with self.recorder.use_cassette(cassette_name):
377-
s = self.gh.user('sigmavirus24')
378-
self.basic_login()
379-
u = self.gh.user()
376+
sigmavirus24 = self.gh.user('sigmavirus24')
380377

381-
assert isinstance(s, github3.users.User)
382-
assert isinstance(u, github3.users.User)
378+
assert isinstance(sigmavirus24, github3.users.User)
383379

384380
def test_zen(self):
385381
"""Test the ability to retrieve tidbits of Zen."""

tests/test_github.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,6 @@ def test_update_user(self):
345345
assert upd.called
346346
upd.assert_called_with(*args)
347347

348-
def test_user(self):
349-
self.response('user')
350-
self.get('https://api.github.com/users/sigmavirus24')
351-
352-
assert isinstance(self.g.user('sigmavirus24'), github3.users.User)
353-
self.mock_assertions()
354-
355-
self.get('https://api.github.com/user')
356-
self.login()
357-
assert isinstance(self.g.user(), github3.users.User)
358-
self.mock_assertions()
359-
360348
def test_utf8_user(self):
361349
self.response('utf8_user')
362350
self.get('https://api.github.com/users/alejandrogomez')

tests/unit/test_github.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from github3 import GitHubError
3+
from github3 import AuthenticationFailed, GitHubError
44
from github3.github import GitHub
55

66
from .helper import UnitHelper, UnitIteratorHelper
@@ -54,6 +54,12 @@ def test_check_authorization(self):
5454
auth=('client-id', 'client-secret')
5555
)
5656

57+
def test_me(self):
58+
"""Test the ability to retrieve the authenticated user's info."""
59+
self.instance.me()
60+
61+
self.session.get.assert_called_once_with(url_for('user'))
62+
5763
def test_two_factor_login(self):
5864
"""Test the ability to pass two_factor_callback."""
5965
self.instance.login('username', 'password',
@@ -64,6 +70,14 @@ def test_can_login_without_two_factor_callback(self):
6470
self.instance.login('username', 'password')
6571
self.instance.login(token='token')
6672

73+
def test_user(self):
74+
"""Test that a user can retrieve information about any user."""
75+
self.instance.user('username')
76+
77+
self.session.get.assert_called_once_with(
78+
url_for('users/username'),
79+
)
80+
6781

6882
class TestGitHubIterators(UnitIteratorHelper):
6983
described_class = GitHub
@@ -625,6 +639,23 @@ def test_repositories_by_with_type(self):
625639
)
626640

627641

642+
class TestGitHubRequiresAuthentication(UnitHelper):
643+
644+
"""Test methods that require authentication."""
645+
646+
described_class = GitHub
647+
example_data = None
648+
649+
def after_setup(self):
650+
"""Disable authentication on the session."""
651+
self.session.has_auth.return_value = False
652+
653+
def test_me(self):
654+
"""Show that GitHub#me requires authentication."""
655+
with pytest.raises(AuthenticationFailed):
656+
self.instance.me()
657+
658+
628659
class TestGitHubAuthorizations(UnitHelper):
629660
described_class = GitHub
630661
example_data = None

0 commit comments

Comments
 (0)
0