@@ -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
cre ated_user = session . query ( Author ) . filt er( Author . email == " test@gmail.com " ) . first ( )
assert cre ated_user is not None
assert created_user . name == " Test User "
assert cre ated_user. email_verified is True
# Проверяем что пользователь был обновле н в БД через OAuth flow
upd ated_user = session . query ( Author ) . wh ere ( Author . email == " test@gmail.com " ) . first ( )
assert upd ated_user is not None
# Проверяем что пользователь существует и имеет OAuth данные
assert upd ated_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 ) . filt er(
oauth_db_session . query ( Community ) . wh ere (
( 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 ) . filt er(
oauth_db_session . query ( CommunityAuthor ) . wh ere (
CommunityAuthor . community_id == community . id
) . delete ( )
oauth_db_session . query ( Community ) . filt er( Community . id == community . id ) . delete ( )
oauth_db_session . query ( Community ) . wh ere ( Community . id == community . id ) . delete ( )
oauth_db_session . commit ( )
except Exception :
oauth_db_session . rollback ( )