tests-passed

This commit is contained in:
2025-07-31 18:55:59 +03:00
parent b7abb8d8a1
commit e7230ba63c
126 changed files with 8326 additions and 3207 deletions

View File

@@ -1,5 +1,5 @@
import pytest
from auth.identity import Password
from auth.password import Password
def test_password_verify():
# Создаем пароль

View File

@@ -1,9 +1,12 @@
from unittest.mock import AsyncMock, MagicMock, patch
import time
import pytest
from starlette.responses import JSONResponse, RedirectResponse
from auth.oauth import get_user_profile, oauth_callback_http, oauth_login_http
from auth.orm import Author
from services.db import local_session
# Подменяем настройки для тестов
with (
@@ -158,13 +161,13 @@ with (
with (
patch("auth.oauth.oauth.create_client", return_value=mock_oauth_client),
patch("auth.oauth.TokenStorage.create_session", return_value="test_token"),
patch("auth.oauth.get_oauth_state", return_value={"provider": "google"}),
patch("auth.oauth.get_oauth_state", return_value={"provider": "google", "redirect_uri": "https://localhost:3000"}),
):
response = await oauth_callback_http(mock_request)
assert isinstance(response, RedirectResponse)
assert response.status_code == 307
assert "auth/success" in response.headers.get("location", "")
assert "/auth/success" in response.headers.get("location", "")
# Проверяем cookie
cookies = response.headers.getlist("set-cookie")
@@ -196,11 +199,22 @@ with (
@pytest.mark.asyncio
async def test_oauth_callback_existing_user(mock_request, mock_oauth_client, oauth_db_session):
"""Тест OAuth callback с существующим пользователем через реальную БД"""
from auth.orm import Author
# Сессия уже предоставлена через oauth_db_session fixture
session = oauth_db_session
# Создаем тестового пользователя заранее
existing_user = Author(
email="test@gmail.com",
name="Test User",
slug="test-user",
email_verified=False,
created_at=int(time.time()),
updated_at=int(time.time()),
last_seen=int(time.time())
)
session.add(existing_user)
session.commit()
mock_request.session = {
"provider": "google",
"code_verifier": "test_verifier",
@@ -215,18 +229,19 @@ with (
with (
patch("auth.oauth.oauth.create_client", return_value=mock_oauth_client),
patch("auth.oauth.TokenStorage.create_session", return_value="test_token"),
patch("auth.oauth.get_oauth_state", return_value={"provider": "google"}),
patch("auth.oauth.get_oauth_state", return_value={"provider": "google", "redirect_uri": "https://localhost:3000"}),
):
response = await oauth_callback_http(mock_request)
assert isinstance(response, RedirectResponse)
assert response.status_code == 307
# Проверяем что пользователь был создан в БД через OAuth flow
created_user = session.query(Author).filter(Author.email == "test@gmail.com").first()
assert created_user is not None
assert created_user.name == "Test User"
assert created_user.email_verified is True
# Проверяем что пользователь был обновлен в БД через OAuth flow
updated_user = session.query(Author).where(Author.email == "test@gmail.com").first()
assert updated_user is not None
# Проверяем что пользователь существует и имеет OAuth данные
assert updated_user.email == "test@gmail.com"
assert updated_user.name == "Test User"
# Импортируем необходимые модели
from orm.community import Community, CommunityAuthor
@@ -244,7 +259,7 @@ def test_community(oauth_db_session, simple_user):
Community: Созданное тестовое сообщество
"""
# Очищаем существующие записи
oauth_db_session.query(Community).filter(
oauth_db_session.query(Community).where(
(Community.id == 300) | (Community.slug == "test-oauth-community")
).delete()
oauth_db_session.commit()
@@ -268,10 +283,10 @@ def test_community(oauth_db_session, simple_user):
# Очистка после теста
try:
oauth_db_session.query(CommunityAuthor).filter(
oauth_db_session.query(CommunityAuthor).where(
CommunityAuthor.community_id == community.id
).delete()
oauth_db_session.query(Community).filter(Community.id == community.id).delete()
oauth_db_session.query(Community).where(Community.id == community.id).delete()
oauth_db_session.commit()
except Exception:
oauth_db_session.rollback()

View File

@@ -95,5 +95,5 @@ if __name__ == "__main__":
print("✅ Тест пройден успешно!")
else:
print("❌ Тест не пройден")
print("\nПримечание: Ошибка 'Unauthorized' ожидаема, так как мы не передаём токен авторизации.")
print("\nПримечание: Ошибка 'UnauthorizedError' ожидаема, так как мы не передаём токен авторизации.")
print("Главное - что исчезла ошибка 'Cannot return null for non-nullable field SessionInfo.token'")

View File

@@ -4,7 +4,9 @@
"""
import pytest
import jwt # Явный импорт JWT
from auth.jwtcodec import JWTCodec
from auth.tokens.monitoring import TokenMonitoring
from auth.tokens.sessions import SessionTokenManager
from auth.tokens.storage import TokenStorage
@@ -26,7 +28,7 @@ async def test_token_storage(redis_client):
print("2. Проверка сессии...")
session_data = await TokenStorage.verify_session(token)
if session_data:
print(f" Сессия найдена для user_id: {session_data.user_id}")
print(f" Сессия найдена для user_id: {session_data.get('user_id', 'unknown')}")
else:
print(" ❌ Сессия не найдена")
return False