Skip to content

Commit

Permalink
Fix create user
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-takker committed Mar 2, 2024
1 parent 2616362 commit 42f9193
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 3 deletions.
8 changes: 5 additions & 3 deletions industry_game/utils/users/base.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions industry_game/utils/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ class FullUser(CustomStruct, frozen=True):
id: int
type: UserType
username: str
telegram: str
name: str

@classmethod
def from_model(self, obj: UserDb) -> "FullUser":
return FullUser(
id=obj.id,
type=obj.type,
username=obj.username,
telegram=obj.properties.get("telegram", ""),
name=obj.properties.get("name", ""),
)
4 changes: 4 additions & 0 deletions industry_game/utils/users/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions industry_game/utils/users/storage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from collections.abc import Mapping

from sqlalchemy import func, insert, select
from sqlalchemy.ext.asyncio import AsyncSession
Expand All @@ -18,13 +19,15 @@ async def create(
*,
username: str,
password_hash: str,
properties: Mapping[str, str],
commit: bool = True,
) -> FullUser:
stmt = (
insert(UserDb)
.values(
username=username,
password_hash=password_hash,
properties=properties,
)
.returning(UserDb)
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
45 changes: 45 additions & 0 deletions tests/api/players/test_player_register.py
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 42f9193

Please sign in to comment.