From 903cb7c9303c56a0b09c7e6483eb0dd598ce54d5 Mon Sep 17 00:00:00 2001 From: "MH.Dmitrii" Date: Sun, 12 Oct 2025 18:59:42 +0300 Subject: [PATCH] pytest 1.7 --- server/backend/JWT.py | 4 ++-- server/testing/tests/endpoints_test.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/server/backend/JWT.py b/server/backend/JWT.py index 0b14732..787c0d9 100644 --- a/server/backend/JWT.py +++ b/server/backend/JWT.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta #jwt +from datetime import datetime, timedelta, timezone #jwt from jose import JWTError, jwt from fastapi import HTTPException, Depends, status from fastapi.security import OAuth2PasswordBearer @@ -17,7 +17,7 @@ class Token(): @staticmethod async def create_token(data: dict, expires_delta: timedelta | None = None): to_encode = data.copy() - expire = datetime.utcnow() + (expires_delta or timedelta(minutes=15)) + expire = datetime.now(timezone.utc) + (expires_delta or timedelta(minutes=15)) to_encode.update({"exp": expire}) encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) return encoded_jwt diff --git a/server/testing/tests/endpoints_test.py b/server/testing/tests/endpoints_test.py index 0a5321b..ed5a3c9 100644 --- a/server/testing/tests/endpoints_test.py +++ b/server/testing/tests/endpoints_test.py @@ -77,12 +77,35 @@ async def test_is_admin(client, monkeypatch, global_email, is_admin, expected_st assert response.status_code == expected_status api.dependency_overrides.clear() +@pytest.mark.asyncio +@pytest.mark.parametrize("email,password,expected_status", [ + ("valid", "123qwe!@#", 200), + ("invalidemail.com", "123qwe!@#", 422), + ("invalidpassword", "1234", 422), + ("invalidall", "1234", 422) +]) +async def test_login_user(client, email, password, expected_status, global_email): + '''Проверка: Логин пользователя''' + + + if email == "valid": + email = global_email #Подставка глобальной фикстуры под конкретный вариант + + user_data = { + "username": email, + "password": password + } + response = await client.post(f"/login", data=user_data) + assert response.status_code == expected_status + + @pytest.mark.parametrize("is_admin, can_delete,expected_status", [ (False,True,200), (True,True,200), (True,False,200), (False, False,403) ]) + @pytest.mark.asyncio async def test_delete_user(client, global_email, monkeypatch, is_admin, expected_status, can_delete): """Проверка: Удаление пользователя"""