Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: user ID #372

Merged
merged 10 commits into from
Nov 8, 2022
2 changes: 1 addition & 1 deletion tests/whist_server/services/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def _create_user():
try:
user_db_service.add(user)
except UserExistsError:
return user_db_service.get(user.username)
return user_db_service.get_by_username(user.username)
return user


Expand Down
14 changes: 10 additions & 4 deletions tests/whist_server/services/test_user_db_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
class UserDbTestCase(UserBaseTestCase):

def test_add_user(self):
self.assertTrue(self.user_database_service.add(self.user))
self.assertEqual(self.user, self.user_database_service.get(self.user.username))
user_id = self.user_database_service.add(self.user)
self.assertTrue(user_id)
self.assertEqual(self.user, self.user_database_service.get(user_id))

def test_user_not_existing(self):
username = '1'
user_id = '5' * 24
with self.assertRaises(UserNotFoundError):
self.user_database_service.get(user_id)

def test_get_name_user_not_existing(self):
username = 'fake'
error_msg = f'User with name "{username}" not found.'
with self.assertRaisesRegex(UserNotFoundError, error_msg):
self.user_database_service.get(username)
self.user_database_service.get_by_username(username)

def test_unique_user(self):
_ = self.user_database_service.add(self.user)
Expand Down
2 changes: 1 addition & 1 deletion tests/whist_server/web_socket/test_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_subscribe(self):

@skip('Probably bug in Test Client')
def test_subscribe_room_not_exists(self):
self.room_service_mock.get = MagicMock(side_effect=RoomNotFoundError)
self.room_service_mock.get_by_username = MagicMock(side_effect=RoomNotFoundError)
app.dependency_overrides[RoomDatabaseService] = lambda: self.room_service_mock
with self.client.websocket_connect(f'/room/{self.room_id}1') as websocket:
websocket.send_text(self.token)
Expand Down
4 changes: 3 additions & 1 deletion whist_server/database/user.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"""User models"""
from typing import Any, Optional

from pydantic import root_validator
from pydantic import Field, root_validator
from whist_core.user.player import Player

from whist_server.const import INITIAL_RATING
from whist_server.database.id_wrapper import PyObjectId
from whist_server.services.password import PasswordService


class UserInDb(Player):
"""
User DO
"""
id: Optional[PyObjectId] = Field(alias='_id')
hashed_password: Optional[str] = None
github_id: Optional[str] = None
github_username: Optional[str] = None
Expand Down
4 changes: 2 additions & 2 deletions whist_server/services/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def get_current_user(token: str = Depends(oauth2_scheme),
:return: DO of User
"""
token_data = await _get_token_data(token)
user = user_db_service.get(token_data.username)
user = user_db_service.get_by_username(token_data.username)
return user.to_player()


Expand All @@ -55,7 +55,7 @@ async def check_credentials(username: str, password: str) -> bool:
UserNotFoundError.
"""
user_db_service = UserDatabaseService()
user = user_db_service.get(username)
user = user_db_service.get_by_username(username)
password_db_service = PasswordService()
return password_db_service.verify(password, user.hashed_password)

Expand Down
19 changes: 16 additions & 3 deletions whist_server/services/user_db_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""User database connector."""
from bson import ObjectId

from whist_server.database import db
from whist_server.database.user import UserInDb
Expand All @@ -22,20 +23,32 @@ def __new__(cls):
@classmethod
def add(cls, user: UserInDb) -> str:
"""
Adds an user to the database.
Adds a user to the database.
:param user: to be added
:return: The id of the successful added user.
"""
try:
_ = cls.get(user.username)
_ = cls.get_by_username(user.username)
except UserNotFoundError:
user_id = cls._users.insert_one(user.dict(exclude={'id'}))
return str(user_id.inserted_id)
else:
raise UserExistsError(f'User with username: "{user.username}" already exists.')

@classmethod
def get(cls, username: str) -> UserInDb:
def get(cls, user_id: str) -> UserInDb:
"""
Gets the user querying the user ID.
:param user_id: of the user
:return: the user database object
"""
user = cls._users.find_one({'_id': ObjectId(user_id)})
if user is None:
raise UserNotFoundError()
return UserInDb(**user)

@classmethod
def get_by_username(cls, username: str) -> UserInDb:
"""
Gets the user querying the username.
:param username: of the user
Expand Down