From 42f919305234319afddee2e698843414ef33842c Mon Sep 17 00:00:00 2001 From: Sergey Natalenko Date: Sat, 2 Mar 2024 13:54:13 +0300 Subject: [PATCH] Fix create user --- industry_game/utils/users/base.py | 8 ++-- industry_game/utils/users/models.py | 4 ++ industry_game/utils/users/processor.py | 4 ++ industry_game/utils/users/storage.py | 3 ++ tests/{test_api => api}/__init__.py | 0 .../test_games => api/games}/__init__.py | 0 .../games}/test_game_create.py | 0 .../games}/test_game_details.py | 0 .../games}/test_game_list.py | 0 .../games}/test_lobby/__init__.py | 0 .../test_players => api/players}/__init__.py | 0 .../players/test_player_login.py} | 0 tests/api/players/test_player_register.py | 45 +++++++++++++++++++ tests/{test_api => api}/test_ping.py | 0 tests/{test_db => db}/__init__.py | 0 tests/{test_db => db}/test_migrations.py | 0 16 files changed, 61 insertions(+), 3 deletions(-) rename tests/{test_api => api}/__init__.py (100%) rename tests/{test_api/test_games => api/games}/__init__.py (100%) rename tests/{test_api/test_games => api/games}/test_game_create.py (100%) rename tests/{test_api/test_games => api/games}/test_game_details.py (100%) rename tests/{test_api/test_games => api/games}/test_game_list.py (100%) rename tests/{test_api/test_games => api/games}/test_lobby/__init__.py (100%) rename tests/{test_api/test_players => api/players}/__init__.py (100%) rename tests/{test_api/test_players/test_login_player.py => api/players/test_player_login.py} (100%) create mode 100644 tests/api/players/test_player_register.py rename tests/{test_api => api}/test_ping.py (100%) rename tests/{test_db => db}/__init__.py (100%) rename tests/{test_db => db}/test_migrations.py (100%) diff --git a/industry_game/utils/users/base.py b/industry_game/utils/users/base.py index 8071cf3..f22cfe5 100644 --- a/industry_game/utils/users/base.py +++ b/industry_game/utils/users/base.py @@ -1,6 +1,6 @@ from enum import StrEnum -from pydantic import BaseModel, ConfigDict +from pydantic import BaseModel, ConfigDict, Field from industry_game.utils.msgspec import CustomStruct @@ -11,8 +11,10 @@ class UserType(StrEnum): class RegisterPlayerModel(BaseModel): - username: str - password: str + username: str = Field(min_length=8) + password: str = Field(min_length=8) + telegram: str = Field(min_length=2) + name: str = Field(min_length=5) class AuthUserModel(BaseModel): diff --git a/industry_game/utils/users/models.py b/industry_game/utils/users/models.py index a6492f4..f1866d1 100644 --- a/industry_game/utils/users/models.py +++ b/industry_game/utils/users/models.py @@ -29,6 +29,8 @@ class FullUser(CustomStruct, frozen=True): id: int type: UserType username: str + telegram: str + name: str @classmethod def from_model(self, obj: UserDb) -> "FullUser": @@ -36,4 +38,6 @@ def from_model(self, obj: UserDb) -> "FullUser": id=obj.id, type=obj.type, username=obj.username, + telegram=obj.properties.get("telegram", ""), + name=obj.properties.get("name", ""), ) diff --git a/industry_game/utils/users/processor.py b/industry_game/utils/users/processor.py index 487f8d8..103e414 100644 --- a/industry_game/utils/users/processor.py +++ b/industry_game/utils/users/processor.py @@ -28,6 +28,10 @@ async def register(self, player: RegisterPlayerModel) -> AuthToken: user = await self.player_storage.create( username=player.username, password_hash=self.passgen.hashpw(player.password), + properties={ + "telegram": player.telegram, + "name": player.name, + }, ) token = self.authorization_provider.generate_token(user=user) return AuthToken(token=token) diff --git a/industry_game/utils/users/storage.py b/industry_game/utils/users/storage.py index a028449..8d42e6d 100644 --- a/industry_game/utils/users/storage.py +++ b/industry_game/utils/users/storage.py @@ -1,4 +1,5 @@ import asyncio +from collections.abc import Mapping from sqlalchemy import func, insert, select from sqlalchemy.ext.asyncio import AsyncSession @@ -18,6 +19,7 @@ async def create( *, username: str, password_hash: str, + properties: Mapping[str, str], commit: bool = True, ) -> FullUser: stmt = ( @@ -25,6 +27,7 @@ async def create( .values( username=username, password_hash=password_hash, + properties=properties, ) .returning(UserDb) ) diff --git a/tests/test_api/__init__.py b/tests/api/__init__.py similarity index 100% rename from tests/test_api/__init__.py rename to tests/api/__init__.py diff --git a/tests/test_api/test_games/__init__.py b/tests/api/games/__init__.py similarity index 100% rename from tests/test_api/test_games/__init__.py rename to tests/api/games/__init__.py diff --git a/tests/test_api/test_games/test_game_create.py b/tests/api/games/test_game_create.py similarity index 100% rename from tests/test_api/test_games/test_game_create.py rename to tests/api/games/test_game_create.py diff --git a/tests/test_api/test_games/test_game_details.py b/tests/api/games/test_game_details.py similarity index 100% rename from tests/test_api/test_games/test_game_details.py rename to tests/api/games/test_game_details.py diff --git a/tests/test_api/test_games/test_game_list.py b/tests/api/games/test_game_list.py similarity index 100% rename from tests/test_api/test_games/test_game_list.py rename to tests/api/games/test_game_list.py diff --git a/tests/test_api/test_games/test_lobby/__init__.py b/tests/api/games/test_lobby/__init__.py similarity index 100% rename from tests/test_api/test_games/test_lobby/__init__.py rename to tests/api/games/test_lobby/__init__.py diff --git a/tests/test_api/test_players/__init__.py b/tests/api/players/__init__.py similarity index 100% rename from tests/test_api/test_players/__init__.py rename to tests/api/players/__init__.py diff --git a/tests/test_api/test_players/test_login_player.py b/tests/api/players/test_player_login.py similarity index 100% rename from tests/test_api/test_players/test_login_player.py rename to tests/api/players/test_player_login.py diff --git a/tests/api/players/test_player_register.py b/tests/api/players/test_player_register.py new file mode 100644 index 0000000..0ea9d18 --- /dev/null +++ b/tests/api/players/test_player_register.py @@ -0,0 +1,45 @@ +from aiohttp.test_utils import TestClient +from sqlalchemy import select +from sqlalchemy.ext.asyncio import AsyncSession +from yarl import URL + +from industry_game.db.models import User +from industry_game.utils.users.base import UserType + +API_URL = URL("/api/v1/players/register/") + + +async def test_player_register_successful_status_created( + api_client: TestClient, +): + response = await api_client.post( + API_URL, + json={ + "username": "username", + "password": "password", + "telegram": "telegram", + "name": "your name", + }, + ) + assert response.status == 201 + + +async def test_player_register_successful_check_db( + api_client: TestClient, + session: AsyncSession, +): + await api_client.post( + API_URL, + json={ + "username": "username", + "password": "password", + "telegram": "telegram", + "name": "your name", + }, + ) + user = ( + await session.scalars(select(User).where(User.username == "username")) + ).one() + + assert user.username == "username" + assert user.type == UserType.PLAYER diff --git a/tests/test_api/test_ping.py b/tests/api/test_ping.py similarity index 100% rename from tests/test_api/test_ping.py rename to tests/api/test_ping.py diff --git a/tests/test_db/__init__.py b/tests/db/__init__.py similarity index 100% rename from tests/test_db/__init__.py rename to tests/db/__init__.py diff --git a/tests/test_db/test_migrations.py b/tests/db/test_migrations.py similarity index 100% rename from tests/test_db/test_migrations.py rename to tests/db/test_migrations.py