Browse Source

Add serializer for UserToken and update tests

merge-requests/1/head
Drew Short 7 years ago
parent
commit
8fed7b57ba
  1. 2
      server/atheneum/api/authentication_api.py
  2. 4
      server/atheneum/service/serialization_service.py
  3. 53
      server/atheneum/service/user_token_service.py
  4. 4
      server/tests/api/test_authentication_api.py

2
server/atheneum/api/authentication_api.py

@ -24,7 +24,7 @@ def login() -> APIResponse:
:return: A login token for continued authentication :return: A login token for continued authentication
""" """
user_token = user_token_service.create(g.user) user_token = user_token_service.create(g.user)
return APIResponse({'token': user_token.token}, 200)
return APIResponse(user_token, 200)
@AUTH_BLUEPRINT.route('/bump', methods=['POST']) @AUTH_BLUEPRINT.route('/bump', methods=['POST'])

4
server/atheneum/service/serialization_service.py

@ -25,7 +25,9 @@ class BaseSerializer: # pylint: disable=too-few-public-methods
'Invalid key: %r. Valid keys: %r.' % ( 'Invalid key: %r. Valid keys: %r.' % (
key, list(sorted(field_factories.keys())))) key, list(sorted(field_factories.keys()))))
factory = field_factories[key] factory = field_factories[key]
ret[key] = factory()
val = factory()
if val is not None:
ret[key] = val
return ret return ret
def _serializers(self) -> Dict[str, Callable[[], Any]]: def _serializers(self) -> Dict[str, Callable[[], Any]]:

53
server/atheneum/service/user_token_service.py

@ -1,10 +1,61 @@
"""Service to handle user_token operations.""" """Service to handle user_token operations."""
import uuid import uuid
from datetime import datetime from datetime import datetime
from typing import Optional
from typing import Optional, Dict, Callable, Any
from atheneum.db import db from atheneum.db import db
from atheneum.model import User, UserToken from atheneum.model import User, UserToken
from atheneum.service.serialization_service import (
BaseSerializer,
register_serializer
)
class UserTokenSerializer(BaseSerializer):
"""Serialize User model."""
def _serializers(self) -> Dict[str, Callable[[], Any]]:
"""Define the fields and the accompanying serializer factory."""
return {
'token': self.serialize_token,
'note': self.serialize_note,
'enabled': self.serialize_enabled,
'expirationTime': self.serialize_expiration_time,
'creationTime': self.serialize_creation_time,
'lastUsageTime': self.serialize_last_usage_time,
'version': self.serialize_version
}
def serialize_token(self) -> str:
"""User token."""
return self.model.token
def serialize_note(self) -> str:
"""User token note."""
return self.model.note
def serialize_enabled(self) -> bool:
"""User token enabled."""
return self.model.enabled
def serialize_expiration_time(self) -> datetime:
"""User token expiration time."""
return self.model.expiration_time
def serialize_creation_time(self) -> datetime:
"""User token creation time."""
return self.model.creation_time
def serialize_last_usage_time(self) -> datetime:
"""User token last usage time."""
return self.model.last_usage_time
def serialize_version(self) -> int:
"""User token version."""
return self.model.version
register_serializer(UserToken.__name__, UserTokenSerializer)
def generate_token() -> uuid.UUID: def generate_token() -> uuid.UUID:

4
server/tests/api/test_authentication_api.py

@ -11,8 +11,8 @@ def test_bump_happy_path(auth: AuthActions):
auth.login() auth.login()
result = auth.bump() result = auth.bump()
assert result.status_code == 200 assert result.status_code == 200
assert (result.json['last_login_time'] is not None
and len(result.json['last_login_time']) > 0)
assert (result.json['lastLoginTime'] is not None
and len(result.json['lastLoginTime']) > 0)
def test_logout_happy_path(auth: AuthActions): def test_logout_happy_path(auth: AuthActions):

Loading…
Cancel
Save