tests-fix-1

This commit is contained in:
2025-08-27 02:45:15 +03:00
parent 90aece7a60
commit eef2ae1d5e
9 changed files with 413 additions and 115 deletions

View File

@@ -57,14 +57,14 @@ def simple_user(db_session):
@pytest.fixture
def test_community(db_session, simple_user):
"""Создает тестовое сообщество"""
# Очищаем существующие записи
db_session.query(Community).where(Community.id == 999).delete()
db_session.commit()
import uuid
# Генерируем уникальный slug
unique_slug = f"integration-test-{uuid.uuid4().hex[:8]}"
community = Community(
id=999,
name="Integration Test Community",
slug="integration-test-community",
slug=unique_slug,
desc="Community for integration RBAC tests",
created_by=simple_user.id,
created_at=int(time.time())
@@ -83,7 +83,7 @@ def test_community(db_session, simple_user):
@pytest.fixture(autouse=True)
def setup_redis():
def setup_redis(test_community):
"""Настройка Redis для каждого теста"""
# FakeRedis уже подключен, ничего не делаем
yield
@@ -91,7 +91,7 @@ def setup_redis():
# Очищаем данные тестового сообщества из Redis
try:
# Используем execute вместо delete
redis.execute("DEL", "community:roles:999")
redis.execute("DEL", f"community:roles:{test_community.id}")
except Exception:
pass
@@ -105,34 +105,222 @@ class TestRBACIntegrationWithInheritance:
# TODO: Implement test logic
assert True # Placeholder assertion
def test_editor_role_inheritance_integration(self, db_session, simple_user, test_community):
@pytest.mark.asyncio
async def test_editor_role_inheritance_integration(self, db_session, simple_user, test_community):
"""Интеграционный тест наследования ролей для editor"""
pytest.skip("RBAC integration тесты временно отключены из-за проблем с Redis")
from rbac.api import initialize_community_permissions, user_has_permission
from orm.community import CommunityAuthor
# Инициализируем разрешения для сообщества
await initialize_community_permissions(test_community.id)
# Создаем CommunityAuthor с ролью editor
ca = CommunityAuthor(
community_id=test_community.id,
author_id=simple_user.id,
roles="editor"
)
db_session.add(ca)
db_session.commit()
# Проверяем базовые разрешения reader (editor наследует их через author)
reader_permissions = ["shout:read", "topic:read", "collection:read"]
for perm in reader_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert has_permission, f"Editor должен наследовать разрешение {perm} от reader через author"
# Проверяем разрешения author (editor наследует их напрямую)
author_permissions = ["draft:create", "shout:create", "collection:create"]
for perm in author_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert has_permission, f"Editor должен наследовать разрешение {perm} от author"
# Проверяем специфичные разрешения editor
editor_permissions = ["shout:delete_any", "shout:update_any", "topic:create"]
for perm in editor_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert has_permission, f"Editor должен иметь разрешение {perm}"
def test_admin_role_inheritance_integration(self, db_session, simple_user, test_community):
@pytest.mark.asyncio
async def test_admin_role_inheritance_integration(self, db_session, simple_user, test_community):
"""Интеграционный тест наследования ролей для admin"""
pytest.skip("RBAC integration тесты временно отключены из-за проблем с Redis")
from rbac.api import initialize_community_permissions, user_has_permission
from orm.community import CommunityAuthor
# Инициализируем разрешения для сообщества
await initialize_community_permissions(test_community.id)
# Создаем CommunityAuthor с ролью admin
ca = CommunityAuthor(
community_id=test_community.id,
author_id=simple_user.id,
roles="admin"
)
db_session.add(ca)
db_session.commit()
# Проверяем что admin наследует разрешения author
author_permissions = ["draft:create", "shout:create", "collection:create"]
for perm in author_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert has_permission, f"Admin должен наследовать разрешение {perm} от author"
# Проверяем специфичные разрешения admin
admin_permissions = ["shout:delete_any", "author:delete_any", "community:delete_any"]
for perm in admin_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert has_permission, f"Admin должен иметь разрешение {perm}"
def test_expert_role_inheritance_integration(self, db_session, simple_user, test_community):
@pytest.mark.asyncio
async def test_expert_role_inheritance_integration(self, db_session, simple_user, test_community):
"""Интеграционный тест наследования ролей для expert"""
pytest.skip("RBAC integration тесты временно отключены из-за проблем с Redis")
from rbac.api import initialize_community_permissions, user_has_permission
from orm.community import CommunityAuthor
# Инициализируем разрешения для сообщества
await initialize_community_permissions(test_community.id)
# Создаем CommunityAuthor с ролью expert
ca = CommunityAuthor(
community_id=test_community.id,
author_id=simple_user.id,
roles="expert"
)
db_session.add(ca)
db_session.commit()
# Проверяем что expert наследует разрешения reader
reader_permissions = ["shout:read", "topic:read", "collection:read"]
for perm in reader_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert has_permission, f"Expert должен наследовать разрешение {perm} от reader"
# Проверяем специфичные разрешения expert
expert_permissions = ["reaction:create:PROOF", "reaction:create:DISPROOF", "reaction:create:AGREE"]
for perm in expert_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert has_permission, f"Expert должен иметь разрешение {perm}"
def test_artist_role_inheritance_integration(self, db_session, simple_user, test_community):
@pytest.mark.asyncio
async def test_artist_role_inheritance_integration(self, db_session, simple_user, test_community):
"""Интеграционный тест наследования ролей для artist"""
pytest.skip("RBAC integration тесты временно отключены из-за проблем с Redis")
from rbac.api import initialize_community_permissions, user_has_permission
from orm.community import CommunityAuthor
# Инициализируем разрешения для сообщества
await initialize_community_permissions(test_community.id)
# Создаем CommunityAuthor с ролью artist
ca = CommunityAuthor(
community_id=test_community.id,
author_id=simple_user.id,
roles="artist"
)
db_session.add(ca)
db_session.commit()
# Проверяем что artist наследует разрешения author
author_permissions = ["draft:create", "shout:create", "collection:create"]
for perm in author_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert has_permission, f"Artist должен наследовать разрешение {perm} от author"
# Проверяем специфичные разрешения artist
artist_permissions = ["reaction:create:CREDIT", "reaction:read:CREDIT"]
for perm in artist_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert has_permission, f"Artist должен иметь разрешение {perm}"
def test_multiple_roles_inheritance_integration(self, db_session, simple_user, test_community):
@pytest.mark.asyncio
async def test_multiple_roles_inheritance_integration(self, db_session, simple_user, test_community):
"""Интеграционный тест наследования для пользователя с несколькими ролями"""
pytest.skip("RBAC integration тесты временно отключены из-за проблем с Redis")
from rbac.api import initialize_community_permissions, user_has_permission
from orm.community import CommunityAuthor
# Инициализируем разрешения для сообщества
await initialize_community_permissions(test_community.id)
# Создаем CommunityAuthor с несколькими ролями
ca = CommunityAuthor(
community_id=test_community.id,
author_id=simple_user.id,
roles="author,expert"
)
db_session.add(ca)
db_session.commit()
# Проверяем разрешения от author
author_permissions = ["draft:create", "shout:create"]
for perm in author_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert has_permission, f"Пользователь с ролями author,expert должен иметь разрешение {perm}"
# Проверяем разрешения от expert
expert_permissions = ["reaction:create:PROOF", "reaction:create:DISPROOF"]
for perm in expert_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert has_permission, f"Пользователь с ролями author,expert должен иметь разрешение {perm}"
def test_roles_have_permission_inheritance_integration(self, db_session, test_community):
@pytest.mark.asyncio
async def test_roles_have_permission_inheritance_integration(self, db_session, test_community):
"""Интеграционный тест функции roles_have_permission с учетом наследования"""
pytest.skip("RBAC integration тесты временно отключены из-за проблем с Redis")
from rbac.api import initialize_community_permissions, roles_have_permission
# Инициализируем разрешения для сообщества
await initialize_community_permissions(test_community.id)
# Проверяем что admin роли имеют разрешения author
has_permission = await roles_have_permission(["admin"], "draft:create", test_community.id)
assert has_permission, "Admin должен наследовать разрешение draft:create от author"
# Проверяем что editor роли имеют разрешения reader
has_permission = await roles_have_permission(["editor"], "shout:read", test_community.id)
assert has_permission, "Editor должен наследовать разрешение shout:read от reader"
def test_permission_denial_inheritance_integration(self, db_session, simple_user, test_community):
@pytest.mark.asyncio
async def test_permission_denial_inheritance_integration(self, db_session, simple_user, test_community):
"""Интеграционный тест отказа в разрешениях с учетом наследования"""
pytest.skip("RBAC integration тесты временно отключены из-за проблем с Redis")
from rbac.api import initialize_community_permissions, user_has_permission
from orm.community import CommunityAuthor
# Инициализируем разрешения для сообщества
await initialize_community_permissions(test_community.id)
# Создаем CommunityAuthor с ролью reader
ca = CommunityAuthor(
community_id=test_community.id,
author_id=simple_user.id,
roles="reader"
)
db_session.add(ca)
db_session.commit()
# Проверяем что reader НЕ имеет разрешения author
author_permissions = ["draft:create", "shout:create"]
for perm in author_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert not has_permission, f"Reader НЕ должен иметь разрешение {perm}"
def test_deep_inheritance_chain_integration(self, db_session, simple_user, test_community):
@pytest.mark.asyncio
async def test_deep_inheritance_chain_integration(self, db_session, simple_user, test_community):
"""Интеграционный тест глубокой цепочки наследования ролей"""
pytest.skip("RBAC integration тесты временно отключены из-за проблем с Redis")
from rbac.api import initialize_community_permissions, user_has_permission
from orm.community import CommunityAuthor
# Инициализируем разрешения для сообщества
await initialize_community_permissions(test_community.id)
# Создаем CommunityAuthor с ролью admin
ca = CommunityAuthor(
community_id=test_community.id,
author_id=simple_user.id,
roles="admin"
)
db_session.add(ca)
db_session.commit()
# Проверяем глубокую цепочку наследования: admin -> author -> reader
reader_permissions = ["shout:read", "topic:read"]
for perm in reader_permissions:
has_permission = await user_has_permission(simple_user.id, perm, test_community.id, db_session)
assert has_permission, f"Admin должен наследовать разрешение {perm} через цепочку admin->author->reader"