2025-02-11 09:00:35 +00:00
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
2025-02-09 19:26:50 +00:00
|
|
|
|
import pytest
|
2025-02-11 09:00:35 +00:00
|
|
|
|
|
2025-07-02 19:30:21 +00:00
|
|
|
|
from auth.orm import Author
|
|
|
|
|
from orm.community import CommunityAuthor
|
2025-02-11 21:31:18 +00:00
|
|
|
|
from orm.reaction import ReactionKind
|
2025-02-09 19:26:50 +00:00
|
|
|
|
from orm.shout import Shout
|
2025-06-01 23:56:11 +00:00
|
|
|
|
from resolvers.reaction import create_reaction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_test_user_with_roles(db_session):
|
2025-07-02 19:30:21 +00:00
|
|
|
|
"""Создает тестового пользователя с ID 1 и назначает ему роли через CSV"""
|
2025-06-01 23:56:11 +00:00
|
|
|
|
# Создаем пользователя с ID 1 если его нет
|
|
|
|
|
test_user = db_session.query(Author).filter(Author.id == 1).first()
|
|
|
|
|
if not test_user:
|
|
|
|
|
test_user = Author(id=1, email="test@example.com", name="Test User", slug="test-user")
|
|
|
|
|
test_user.set_password("password123")
|
|
|
|
|
db_session.add(test_user)
|
|
|
|
|
db_session.flush()
|
|
|
|
|
|
2025-07-02 19:30:21 +00:00
|
|
|
|
# Создаем связь пользователя с сообществом с ролями через CSV
|
|
|
|
|
community_author = (
|
|
|
|
|
db_session.query(CommunityAuthor)
|
|
|
|
|
.filter(CommunityAuthor.community_id == 1, CommunityAuthor.author_id == 1)
|
|
|
|
|
.first()
|
|
|
|
|
)
|
2025-06-01 23:56:11 +00:00
|
|
|
|
|
2025-07-02 19:30:21 +00:00
|
|
|
|
if not community_author:
|
|
|
|
|
community_author = CommunityAuthor(
|
|
|
|
|
community_id=1,
|
|
|
|
|
author_id=1,
|
|
|
|
|
roles="reader,author", # Роли через CSV
|
|
|
|
|
joined_at=int(datetime.now().timestamp()),
|
|
|
|
|
)
|
|
|
|
|
db_session.add(community_author)
|
|
|
|
|
else:
|
|
|
|
|
# Обновляем роли если связь уже существует
|
|
|
|
|
community_author.roles = "reader,author"
|
2025-06-01 23:56:11 +00:00
|
|
|
|
|
|
|
|
|
db_session.commit()
|
|
|
|
|
return test_user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MockInfo:
|
|
|
|
|
"""Мок для GraphQL info объекта"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, author_id: int):
|
|
|
|
|
self.context = {
|
|
|
|
|
"request": None, # Тестовый режим
|
|
|
|
|
"author": {"id": author_id, "name": "Test User"},
|
|
|
|
|
"roles": ["reader", "author"],
|
|
|
|
|
"is_admin": False,
|
|
|
|
|
}
|
2025-02-11 09:00:35 +00:00
|
|
|
|
|
2025-02-09 19:26:50 +00:00
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def test_setup(db_session):
|
|
|
|
|
"""Set up test data."""
|
|
|
|
|
now = int(datetime.now().timestamp())
|
2025-06-01 23:56:11 +00:00
|
|
|
|
author = ensure_test_user_with_roles(db_session)
|
2025-02-11 09:00:35 +00:00
|
|
|
|
|
2025-02-09 19:26:50 +00:00
|
|
|
|
shout = Shout(
|
2025-02-11 09:00:35 +00:00
|
|
|
|
title="Test Shout",
|
2025-06-01 23:56:11 +00:00
|
|
|
|
slug="test-shout-reactions",
|
2025-02-09 19:26:50 +00:00
|
|
|
|
created_by=author.id,
|
|
|
|
|
body="This is a test shout",
|
|
|
|
|
layout="article",
|
|
|
|
|
lang="ru",
|
|
|
|
|
community=1,
|
|
|
|
|
created_at=now,
|
2025-02-11 09:00:35 +00:00
|
|
|
|
updated_at=now,
|
2025-02-09 19:26:50 +00:00
|
|
|
|
)
|
2025-06-01 23:56:11 +00:00
|
|
|
|
db_session.add(shout)
|
2025-02-09 19:26:50 +00:00
|
|
|
|
db_session.commit()
|
|
|
|
|
return {"author": author, "shout": shout}
|
|
|
|
|
|
2025-02-11 09:00:35 +00:00
|
|
|
|
|
2025-02-09 19:26:50 +00:00
|
|
|
|
@pytest.mark.asyncio
|
2025-06-01 23:56:11 +00:00
|
|
|
|
async def test_create_reaction(db_session, test_setup):
|
|
|
|
|
"""Test creating a reaction on a shout using direct resolver call."""
|
|
|
|
|
# Создаем мок info
|
|
|
|
|
info = MockInfo(test_setup["author"].id)
|
|
|
|
|
|
|
|
|
|
# Вызываем резолвер напрямую
|
|
|
|
|
result = await create_reaction(
|
|
|
|
|
None,
|
|
|
|
|
info,
|
|
|
|
|
reaction={
|
|
|
|
|
"shout": test_setup["shout"].id,
|
|
|
|
|
"kind": ReactionKind.LIKE.value,
|
|
|
|
|
"body": "Great post!",
|
2025-02-11 09:00:35 +00:00
|
|
|
|
},
|
2025-02-09 19:26:50 +00:00
|
|
|
|
)
|
2025-02-11 09:00:35 +00:00
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
# Проверяем результат - резолвер должен работать без падения
|
|
|
|
|
assert result is not None
|
|
|
|
|
assert isinstance(result, dict) # Должен вернуть словарь
|