Skip to content

Commit

Permalink
Make entities.TokenInfo subclass of auth.Token
Browse files Browse the repository at this point in the history
  • Loading branch information
pyhedgehog committed Dec 22, 2016
1 parent 74faf4d commit 74075fb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
23 changes: 3 additions & 20 deletions gogs_client/entities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Various immutable classes that represent Gogs entities.
"""
from gogs_client.auth import Token


def json_get(parsed_json, key):
Expand Down Expand Up @@ -242,14 +243,14 @@ def pull(self):
return self._pull


class TokenInfo(object):
class TokenInfo(Token):
"""
An immutable representation of a Gogs user token information
"""

def __init__(self, name, sha1):
self._name = name
self._sha1 = sha1
Token.__init__(self, sha1)

@staticmethod
def from_json(parsed_json):
Expand All @@ -265,21 +266,3 @@ def name(self):
:rtype: str
"""
return self._name

@property
def sha1(self):
"""
The hash of the token
:rtype: str
"""
return self._sha1

def as_token(self):
"""
Create Token authenticator for this token
:rtype: gogs_client.auth.Token
"""
from gogs_client.auth import Token
return Token(self.sha1)
4 changes: 2 additions & 2 deletions gogs_client/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def ensure_token(self, auth, name, username=None):
username = self.authenticated_user(auth).username
tokens = [token for token in self.get_tokens(auth, username) if token.name == name]
if tokens:
return tokens[0].as_token()
return self.create_token(auth, name, username).as_token()
return tokens[0]
return self.create_token(auth, name, username)

def create_repo(self, auth, name, description=None, private=False, auto_init=False,
gitignore_templates=None, license_template=None, readme_template=None):
Expand Down
15 changes: 9 additions & 6 deletions tests/interface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def setUp(self):
self.expected_repo = gogs_client.GogsRepo.from_json(json.loads(self.repo_json_str))
self.expected_user = gogs_client.GogsUser.from_json(json.loads(self.user_json_str))
self.expected_token = gogs_client.TokenInfo.from_json(json.loads(self.token_json_str))
self.token = self.expected_token.as_token()
self.token = gogs_client.Token(self.expected_token.token)

@responses.activate
def test_create_repo1(self):
Expand Down Expand Up @@ -250,9 +250,9 @@ def test_ensure_token(self):
responses.add(responses.POST, uri, body=self.token_json_str, status=200)
responses.add(responses.GET, uri, body="["+self.token_json_str+"]", status=200)
token = self.client.ensure_token(self.username_password, self.expected_token.name, self.username_password.username)
self.assertEqual(token.token, self.expected_token.sha1)
self.assert_tokens_equals(token, self.expected_token)
token = self.client.ensure_token(self.username_password, self.expected_token.name, self.username_password.username)
self.assertEqual(token.token, self.expected_token.sha1)
self.assert_tokens_equals(token, self.expected_token)

@responses.activate
def test_ensure_auth_token(self):
Expand All @@ -264,10 +264,9 @@ def test_ensure_auth_token(self):
tokens = self.client.get_tokens(self.username_password)
self.assertEqual(tokens, [])
tokeninfo = self.client.create_token(self.username_password, self.expected_token.name)
self.assertEqual(tokeninfo.name, self.expected_token.name)
self.assertEqual(tokeninfo.sha1, self.expected_token.sha1)
self.assert_tokens_equals(tokeninfo, self.expected_token)
token = self.client.ensure_token(self.username_password, self.expected_token.name)
self.assertEqual(token.token, self.expected_token.sha1)
self.assert_tokens_equals(token, self.expected_token)

# helper methods

Expand Down Expand Up @@ -308,6 +307,10 @@ def assert_users_equals(self, user, expected):
self.assertEqual(user.email, expected.email)
self.assertEqual(user.avatar_url, expected.avatar_url)

def assert_tokens_equals(self, token, expected):
self.assertEqual(token.name, expected.name)
self.assertEqual(token.token, expected.token)


if __name__ == "__main__":
unittest.main()

0 comments on commit 74075fb

Please sign in to comment.