Skip to content

Commit 97a3c0c

Browse files
committed
Endpoint for retrieving user by authentication
1 parent 3e2739c commit 97a3c0c

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

gogs_client/interface.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,28 @@ def __init__(self, base_url):
1919

2020
def valid_authentication(self, auth):
2121
"""
22+
Returns whether the provided authentication is valid
23+
2224
:param auth.Authentication auth: authentication object
2325
:return: whether the provided authentication is valid
2426
:rtype: bool
2527
:raises NetworkFailure: if there is an error communicating with the server
2628
"""
27-
return self._get("/user/repos", auth=auth).ok
29+
return self._get("/user", auth=auth).ok
30+
31+
def authenticated_user(self, auth):
32+
"""
33+
Returns the user authenticated by the given authentication
34+
35+
:param auth.Authentication auth: authentication for user to retrieve
36+
37+
:return: user corresponding to the provided authentication
38+
:rtype: GogsUser
39+
:raises NetworkFailure: if there is an error communicating with the server
40+
:raises ApiFailure: if the request cannot be serviced
41+
"""
42+
response = self._get("/user", auth=auth)
43+
return GogsUser.from_json(self._check_ok(response).json())
2844

2945
def create_repo(self, auth, name, description=None, private=False, auto_init=False,
3046
gitignore_templates=None, license_template=None, readme_template=None):

tests/interface_test.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,29 @@ def test_delete_user1(self):
215215
self.assertEqual(last_call.request.url, uri2)
216216
self.check_for_basic_auth(last_call.request)
217217

218+
@responses.activate
219+
def test_valid_authentication1(self):
220+
uri = self.path("/user")
221+
valid_token = gogs_client.Token("a_valid_token")
222+
invalid_token = gogs_client.Token("an_invalid_token")
223+
def callback(request):
224+
if request.url == self.path_with_token(uri, valid_token):
225+
return 200, {}, self.user_json_str
226+
elif request.url == self.path_with_token(uri, invalid_token):
227+
return 401, {}, ""
228+
else:
229+
self.fail("Unexpected URL: {}".format(request.url))
230+
responses.add_callback(responses.GET, uri, callback=callback)
231+
self.assertTrue(self.client.valid_authentication(valid_token))
232+
self.assertFalse(self.client.valid_authentication(invalid_token))
233+
234+
@responses.activate
235+
def test_authenticated_user(self):
236+
uri = self.path("/user")
237+
responses.add(responses.GET, uri, body=self.user_json_str, status=200)
238+
user = self.client.authenticated_user(self.token)
239+
self.assert_users_equals(user, self.expected_user)
240+
218241
# helper methods
219242

220243
@staticmethod
@@ -224,8 +247,9 @@ def data_of_query(query):
224247
def path(self, relative):
225248
return http_utils.append_url(self.api_endpoint, relative)
226249

227-
def path_with_token(self, path):
228-
return "{p}?token={t}".format(p=path, t=self.token.token)
250+
def path_with_token(self, path, token=None):
251+
token = self.token if token is None else token
252+
return "{p}?token={t}".format(p=path, t=token.token)
229253

230254
def check_for_basic_auth(self, request):
231255
auth = "{u}:{p}".format(u=self.username_password.username,

0 commit comments

Comments
 (0)