Skip to content

Commit

Permalink
Merge entities.TokenInfo into auth.Token
Browse files Browse the repository at this point in the history
  • Loading branch information
pyhedgehog committed Dec 24, 2016
1 parent 930246e commit ea139fe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 46 deletions.
2 changes: 1 addition & 1 deletion gogs_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gogs_client.auth import Authentication, Token, UsernamePassword
from gogs_client.entities import GogsUser, GogsRepo, TokenInfo
from gogs_client.entities import GogsUser, GogsRepo
from gogs_client.interface import GogsApi, ApiFailure, NetworkFailure
from gogs_client.updates import GogsUserUpdate
19 changes: 18 additions & 1 deletion gogs_client/auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Various classes for Gogs authentication
"""
from gogs_client.entities import json_get


class Authentication(object):
Expand All @@ -22,11 +23,27 @@ class Token(Authentication):
"""
An immutable represention of a Gogs authentication token
"""
def __init__(self, token):
def __init__(self, token, name=None):
"""
:param str token: contents of Gogs authentication token
"""
self._token = token
self._name = name

@staticmethod
def from_json(parsed_json):
name = json_get(parsed_json, "name")
sha1 = json_get(parsed_json, "sha1")
return Token(sha1, name)

@property
def name(self):
"""
The name of the token
:rtype: str
"""
return self._name

@property
def token(self):
Expand Down
25 changes: 0 additions & 25 deletions gogs_client/entities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Various immutable classes that represent Gogs entities.
"""
from gogs_client.auth import Token


def json_get(parsed_json, key):
Expand Down Expand Up @@ -242,27 +241,3 @@ def pull(self):
"""
return self._pull


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

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

@staticmethod
def from_json(parsed_json):
name = json_get(parsed_json, "name")
sha1 = json_get(parsed_json, "sha1")
return TokenInfo(name, sha1)

@property
def name(self):
"""
The name of the token
:rtype: str
"""
return self._name
19 changes: 10 additions & 9 deletions gogs_client/interface.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import requests

from gogs_client._implementation.http_utils import RelativeHttpRequestor, append_url
from gogs_client.entities import GogsUser, GogsRepo, TokenInfo
from gogs_client.entities import GogsUser, GogsRepo
from gogs_client.auth import Token


class GogsApi(object):
Expand Down Expand Up @@ -51,15 +52,15 @@ def get_tokens(self, auth, username=None):
:param auth.Authentication auth: authentication for user to retrieve
:param str username: username of owner of tokens
:return: list of token info representation
:rtype: List[TokenInfo]
:return: list of token representation
:rtype: List[Token]
:raises NetworkFailure: if there is an error communicating with the server
:raises ApiFailure: if the request cannot be serviced
"""
if username is None:
username = self.authenticated_user(auth).username
response = self._get("/users/{u}/tokens".format(u=username), auth=auth)
return [TokenInfo.from_json(o) for o in self._check_ok(response).json()]
return [Token.from_json(o) for o in self._check_ok(response).json()]

def create_token(self, auth, name, username=None):
"""
Expand All @@ -71,16 +72,16 @@ def create_token(self, auth, name, username=None):
:param str name: name of new token
:param str username: username of owner of new token
:return: representation of token info
:rtype: TokenInfo
:return: new token representation
:rtype: Token
:raises NetworkFailure: if there is an error communicating with the server
:raises ApiFailure: if the request cannot be serviced
"""
if username is None:
username = self.authenticated_user(auth).username
data = {"name": name}
response = self._post("/users/{u}/tokens".format(u=username), auth=auth, data=data)
return TokenInfo.from_json(self._check_ok(response).json())
return Token.from_json(self._check_ok(response).json())

def ensure_token(self, auth, name, username=None):
"""
Expand All @@ -92,8 +93,8 @@ def ensure_token(self, auth, name, username=None):
:param str name: name of new token
:param str username: username of owner of new token
:return: token authenticator
:rtype: TokenInfo
:return: token representation
:rtype: Token
:raises NetworkFailure: if there is an error communicating with the server
:raises ApiFailure: if the request cannot be serviced
"""
Expand Down
19 changes: 9 additions & 10 deletions tests/interface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ def setUp(self):
"auth_username", "password")
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 = gogs_client.Token(self.expected_token.token)
self.token = gogs_client.Token.from_json(json.loads(self.token_json_str))

@responses.activate
def test_create_repo1(self):
Expand Down Expand Up @@ -249,10 +248,10 @@ def test_ensure_token(self):
responses.add(responses.GET, uri, body="[]", status=200)
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.assert_tokens_equals(token, self.expected_token)
token = self.client.ensure_token(self.username_password, self.expected_token.name, self.username_password.username)
self.assert_tokens_equals(token, self.expected_token)
token = self.client.ensure_token(self.username_password, self.token.name, self.username_password.username)
self.assert_tokens_equals(token, self.token)
token = self.client.ensure_token(self.username_password, self.token.name, self.username_password.username)
self.assert_tokens_equals(token, self.token)

@responses.activate
def test_ensure_auth_token(self):
Expand All @@ -263,10 +262,10 @@ def test_ensure_auth_token(self):
responses.add(responses.POST, uri, body=self.token_json_str, status=200)
tokens = self.client.get_tokens(self.username_password)
self.assertEqual(tokens, [])
tokeninfo = self.client.create_token(self.username_password, self.expected_token.name)
self.assert_tokens_equals(tokeninfo, self.expected_token)
token = self.client.ensure_token(self.username_password, self.expected_token.name)
self.assert_tokens_equals(token, self.expected_token)
tokeninfo = self.client.create_token(self.username_password, self.token.name)
self.assert_tokens_equals(tokeninfo, self.token)
token = self.client.ensure_token(self.username_password, self.token.name)
self.assert_tokens_equals(token, self.token)

# helper methods

Expand Down

0 comments on commit ea139fe

Please sign in to comment.