Почистил фикс бага
This commit is contained in:
parent
f4448a5402
commit
3b1e2964ae
6 changed files with 10 additions and 36 deletions
|
@ -23,14 +23,14 @@ class UserDAO(BaseDAO):
|
||||||
|
|
||||||
async def add(self, **data) -> SUser:
|
async def add(self, **data) -> SUser:
|
||||||
try:
|
try:
|
||||||
stmt = insert(self.model).values(**data).returning(Users.__table__.columns)
|
stmt = insert(Users).values(**data).returning(Users.__table__.columns)
|
||||||
result = await self.session.execute(stmt)
|
result = await self.session.execute(stmt)
|
||||||
result = result.mappings().one()
|
result = result.mappings().one()
|
||||||
return SUser.model_validate(result)
|
return SUser.model_validate(result)
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
raise UserAlreadyExistsException
|
raise UserAlreadyExistsException
|
||||||
|
|
||||||
async def find_one_or_none(self, **filter_by) -> SUser:
|
async def find_one(self, **filter_by) -> SUser:
|
||||||
try:
|
try:
|
||||||
query = select(Users.__table__.columns).filter_by(**filter_by)
|
query = select(Users.__table__.columns).filter_by(**filter_by)
|
||||||
result = await self.session.execute(query)
|
result = await self.session.execute(query)
|
||||||
|
|
|
@ -4,7 +4,7 @@ from app.users.schemas import SUser
|
||||||
|
|
||||||
class UserService:
|
class UserService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def find_one_or_none(uow: UnitOfWork, user_id: int) -> SUser | None:
|
async def find_one_or_none(uow: UnitOfWork, user_id: int) -> SUser:
|
||||||
async with uow:
|
async with uow:
|
||||||
user = await uow.user.find_one_or_none(id=user_id)
|
user = await uow.user.find_one(id=user_id)
|
||||||
return user
|
return user
|
||||||
|
|
|
@ -54,7 +54,7 @@ def get_token_ws(sec_websocket_protocol: Annotated[str | None, Header()] = None)
|
||||||
return sec_websocket_protocol.split()[-1]
|
return sec_websocket_protocol.split()[-1]
|
||||||
|
|
||||||
|
|
||||||
async def get_current_user_ws(token: str = Depends(get_token_ws), uow=Depends(UnitOfWork)):
|
async def get_current_user_ws(token: str = Depends(get_token_ws), uow=Depends(UnitOfWork)) -> SUser:
|
||||||
try:
|
try:
|
||||||
payload = jwt.decode(token, settings.SECRET_KEY, settings.ALGORITHM)
|
payload = jwt.decode(token, settings.SECRET_KEY, settings.ALGORITHM)
|
||||||
except ExpiredSignatureError:
|
except ExpiredSignatureError:
|
||||||
|
|
|
@ -60,7 +60,7 @@ async def get_all_users(uow=Depends(UnitOfWork)):
|
||||||
async def check_existing_user(user_filter: SUserFilter, uow=Depends(UnitOfWork)):
|
async def check_existing_user(user_filter: SUserFilter, uow=Depends(UnitOfWork)):
|
||||||
async with uow:
|
async with uow:
|
||||||
try:
|
try:
|
||||||
await uow.user.find_one_or_none(**user_filter.model_dump(exclude_none=True))
|
await uow.user.find_one(**user_filter.model_dump(exclude_none=True))
|
||||||
raise UserAlreadyExistsException
|
raise UserAlreadyExistsException
|
||||||
except UserNotFoundException:
|
except UserNotFoundException:
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -63,7 +63,7 @@ class AuthService:
|
||||||
async def authenticate_user_by_email(uow: UnitOfWork, email: EmailStr, password: str) -> SUser | None:
|
async def authenticate_user_by_email(uow: UnitOfWork, email: EmailStr, password: str) -> SUser | None:
|
||||||
try:
|
try:
|
||||||
async with uow:
|
async with uow:
|
||||||
user = await uow.user.find_one_or_none(email=email)
|
user = await uow.user.find_one(email=email)
|
||||||
if not verify_password(password, user.hashed_password):
|
if not verify_password(password, user.hashed_password):
|
||||||
return
|
return
|
||||||
return user
|
return user
|
||||||
|
@ -74,7 +74,7 @@ class AuthService:
|
||||||
async def authenticate_user_by_username(uow: UnitOfWork, username: str, password: str) -> SUser | None:
|
async def authenticate_user_by_username(uow: UnitOfWork, username: str, password: str) -> SUser | None:
|
||||||
try:
|
try:
|
||||||
async with uow:
|
async with uow:
|
||||||
user = await uow.user.find_one_or_none(username=username)
|
user = await uow.user.find_one(username=username)
|
||||||
if not verify_password(password, user.hashed_password):
|
if not verify_password(password, user.hashed_password):
|
||||||
return
|
return
|
||||||
return user
|
return user
|
||||||
|
@ -93,7 +93,7 @@ class AuthService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def check_verificated_user(uow: UnitOfWork, user_id: int) -> bool:
|
async def check_verificated_user(uow: UnitOfWork, user_id: int) -> bool:
|
||||||
async with uow:
|
async with uow:
|
||||||
user = await uow.user.find_one_or_none(id=user_id)
|
user = await uow.user.find_one(id=user_id)
|
||||||
return user.role >= settings.VERIFICATED_USER
|
return user.role >= settings.VERIFICATED_USER
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -105,7 +105,7 @@ class AuthService:
|
||||||
async def get_user_allowed_chats_id(uow: UnitOfWork, user_id: int) -> set[int]:
|
async def get_user_allowed_chats_id(uow: UnitOfWork, user_id: int) -> set[int]:
|
||||||
async with uow:
|
async with uow:
|
||||||
user_allowed_chats = await uow.user.get_user_allowed_chats(user_id)
|
user_allowed_chats = await uow.user.get_user_allowed_chats(user_id)
|
||||||
user_allowed_chats_id = {chat["chat_id"] for chat in user_allowed_chats.allowed_chats}
|
user_allowed_chats_id = {chat.chat_id for chat in user_allowed_chats.allowed_chats}
|
||||||
return user_allowed_chats_id
|
return user_allowed_chats_id
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
@ -131,29 +131,3 @@ async def test_change_password(
|
||||||
|
|
||||||
assert response.status_code == statuscode
|
assert response.status_code == statuscode
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
|
||||||
"user_id,password1,password2,statuscode,username",
|
|
||||||
[
|
|
||||||
(1, "12311234", "12311231", 409, "12311241"),
|
|
||||||
(1, "12311231", "12311231", 200, "string"),
|
|
||||||
(1, "12341234", "12341234", 200, "string"),
|
|
||||||
(3, "12341234", "12341234", 200, "urec"),
|
|
||||||
(3, "12341234", "1234123", 422, "urec"),
|
|
||||||
(3, "1234123", "12341234", 422, "urec"),
|
|
||||||
(3, "1234123", "1234123", 422, "urec"),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
async def test_password_recovery(
|
|
||||||
user_id: int, password1: str, password2: str, statuscode: int, username: str, ac: AsyncClient
|
|
||||||
):
|
|
||||||
response = await ac.post(
|
|
||||||
"/users/password_recovery", json={"user_id": user_id, "password1": password1, "password2": password2}
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == statuscode
|
|
||||||
if response.status_code == 200:
|
|
||||||
assert response.json()["username"] == username
|
|
||||||
user_data = await UserService.find_one_or_none(user_id=user_id)
|
|
||||||
hashed_password = user_data.hashed_password
|
|
||||||
assert verify_password(password1, hashed_password)
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue