tests-passed
This commit is contained in:
@@ -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()
|
||||
|
Reference in New Issue
Block a user