Files
core/tests/test_rbac_system.py
Untone f3fc6c34ae
Some checks failed
Deploy on push / deploy (push) Failing after 7s
e2e-improved
2025-08-27 18:31:51 +03:00

77 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Тесты для системы RBAC (Role-Based Access Control).
Проверяет работу с ролями, разрешениями и наследованием ролей.
"""
import pytest
import time
from orm.author import Author
from orm.community import Community
@pytest.fixture
def test_users(db_session):
"""Создает тестовых пользователей"""
users = []
# Создаем пользователей с ID 1-5
for i in range(1, 6):
user = db_session.query(Author).where(Author.id == i).first()
if not user:
user = Author(id=i, email=f"user{i}@example.com", name=f"Test User {i}", slug=f"test-user-{i}")
user.set_password("password123")
db_session.add(user)
users.append(user)
db_session.commit()
return users
@pytest.fixture
def test_community(db_session, test_users):
"""Создает тестовое сообщество"""
community = db_session.query(Community).where(Community.id == 1).first()
if not community:
community = Community(
id=1,
name="RBAC Test Community",
slug="rbac-test-community",
desc="Test community for RBAC tests",
created_by=test_users[0].id,
created_at=int(time.time())
)
db_session.add(community)
db_session.commit()
return community
@pytest.mark.asyncio
async def test_rbac_system_basic(db_session, test_users, test_community):
"""Базовый тест системы RBAC"""
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=test_users[0].id,
roles="reader"
)
db_session.add(ca)
db_session.commit()
# Проверяем базовые разрешения reader
reader_permissions = ["shout:read", "topic:read"]
for perm in reader_permissions:
has_permission = await user_has_permission(test_users[0].id, perm, test_community.id, db_session)
assert has_permission, f"Reader должен иметь разрешение {perm}"
# Проверяем что reader НЕ имеет разрешения author
author_permissions = ["draft:create", "shout:create"]
for perm in author_permissions:
has_permission = await user_has_permission(test_users[0].id, perm, test_community.id, db_session)
assert not has_permission, f"Reader НЕ должен иметь разрешение {perm}"