""" Pure unit tests for core/permissions.py. Tests the role-group constants, require_roles dependency factory, and get_effective_context behaviour — no HTTP server or database needed for the constant / factory tests; get_effective_context tests use a lightweight mock. """ import pytest from unittest.mock import AsyncMock, MagicMock from fastapi import HTTPException from models.user import UserRole from core.permissions import ( ALL_CUSTOMER_ROLES, ALL_SC_ROLES, WORKSPACE_ADMIN_ROLES, INTERNAL_ADMIN_ROLES, EVERYBODY_ROLES, require_roles, get_effective_context, ) # --------------------------------------------------------------------------- # Role-group constants # --------------------------------------------------------------------------- class TestRoleGroupConstants: def test_all_customer_roles_contains_customer_admin_and_user(self): assert UserRole.CUSTOMER_ADMIN in ALL_CUSTOMER_ROLES assert UserRole.CUSTOMER_USER in ALL_CUSTOMER_ROLES def test_all_customer_roles_excludes_staff(self): for role in ALL_SC_ROLES: assert role not in ALL_CUSTOMER_ROLES def test_all_sc_roles_contains_expected_staff(self): for role in [UserRole.SC_AGENT, UserRole.SC_LEAD, UserRole.SC_ADMIN, UserRole.SUPERADMIN]: assert role in ALL_SC_ROLES def test_all_sc_roles_excludes_customers(self): for role in ALL_CUSTOMER_ROLES: assert role not in ALL_SC_ROLES def test_workspace_admin_roles_includes_customer_admin(self): assert UserRole.CUSTOMER_ADMIN in WORKSPACE_ADMIN_ROLES def test_workspace_admin_roles_includes_sc_admin_and_lead(self): assert UserRole.SC_ADMIN in WORKSPACE_ADMIN_ROLES assert UserRole.SC_LEAD in WORKSPACE_ADMIN_ROLES def test_workspace_admin_roles_excludes_customer_user(self): assert UserRole.CUSTOMER_USER not in WORKSPACE_ADMIN_ROLES def test_workspace_admin_roles_excludes_sc_agent(self): assert UserRole.SC_AGENT not in WORKSPACE_ADMIN_ROLES def test_internal_admin_roles_excludes_sc_agent(self): assert UserRole.SC_AGENT not in INTERNAL_ADMIN_ROLES def test_everybody_roles_is_union_of_customer_and_sc(self): for role in ALL_CUSTOMER_ROLES + ALL_SC_ROLES: assert role in EVERYBODY_ROLES def test_no_duplicate_roles_in_everybody(self): assert len(EVERYBODY_ROLES) == len(set(EVERYBODY_ROLES)) # --------------------------------------------------------------------------- # require_roles factory # --------------------------------------------------------------------------- def _make_user(role: UserRole) -> MagicMock: """Build a minimal User-like object with the given role.""" user = MagicMock() user.role = role.value # token sets role as plain string value return user class TestRequireRoles: def test_allowed_role_passes(self): checker = require_roles([UserRole.SC_ADMIN, UserRole.SC_LEAD]) user = _make_user(UserRole.SC_ADMIN) result = checker(user) assert result is user def test_disallowed_role_raises_403(self): checker = require_roles([UserRole.SC_ADMIN]) user = _make_user(UserRole.CUSTOMER_USER) with pytest.raises(HTTPException) as exc_info: checker(user) assert exc_info.value.status_code == 403 def test_superadmin_bypasses_any_role_restriction(self): """SUPERADMIN has a universal bypass inside require_roles.""" checker = require_roles([UserRole.CUSTOMER_USER]) # only CUSTOMER_USER allowed user = _make_user(UserRole.SUPERADMIN) result = checker(user) assert result is user def test_multiple_allowed_roles_any_passes(self): checker = require_roles([UserRole.SC_AGENT, UserRole.SC_LEAD, UserRole.SC_ADMIN]) for role in [UserRole.SC_AGENT, UserRole.SC_LEAD, UserRole.SC_ADMIN]: assert checker(_make_user(role)) is not None def test_customer_roles_blocked_from_sc_only_endpoint(self): checker = require_roles(ALL_SC_ROLES) for role in ALL_CUSTOMER_ROLES: with pytest.raises(HTTPException): checker(_make_user(role)) # --------------------------------------------------------------------------- # get_effective_context # --------------------------------------------------------------------------- class TestGetEffectiveContext: """ Verifies the workspace-isolation logic without a real database. The DB is mocked to return controlled UserWorkspace results. """ @pytest.mark.asyncio async def test_platform_admin_gets_own_workspace_when_no_id_given(self): import uuid ws_id = uuid.uuid4() user = MagicMock() user.is_platform_admin = True user.workspace_id = ws_id db = AsyncMock() target_ws, role = await get_effective_context(db, user, workspace_id=None) assert target_ws == ws_id assert role == UserRole.SC_ADMIN @pytest.mark.asyncio async def test_platform_admin_can_access_any_workspace(self): import uuid other_ws = uuid.uuid4() user = MagicMock() user.is_platform_admin = True user.workspace_id = uuid.uuid4() db = AsyncMock() target_ws, role = await get_effective_context(db, user, workspace_id=str(other_ws)) assert target_ws == other_ws @pytest.mark.asyncio async def test_regular_user_uses_own_workspace_when_no_id(self): import uuid ws_id = uuid.uuid4() user = MagicMock() user.is_platform_admin = False user.workspace_id = ws_id user.role = UserRole.CUSTOMER_USER db = AsyncMock() target_ws, role = await get_effective_context(db, user, workspace_id=None) assert target_ws == ws_id @pytest.mark.asyncio async def test_regular_user_denied_unauthorized_workspace(self): import uuid from models.user_workspace import UserWorkspace user = MagicMock() user.is_platform_admin = False user.workspace_id = uuid.uuid4() user.email = "test@example.com" # DB returns no UserWorkspace link → access denied mock_result = MagicMock() mock_result.scalar_one_or_none.return_value = None db = AsyncMock() db.execute = AsyncMock(return_value=mock_result) other_ws = uuid.uuid4() with pytest.raises(HTTPException) as exc_info: await get_effective_context(db, user, workspace_id=str(other_ws)) assert exc_info.value.status_code == 403 @pytest.mark.asyncio async def test_regular_user_allowed_when_link_exists(self): import uuid from models.user_workspace import UserWorkspace ws_id = uuid.uuid4() other_ws = uuid.uuid4() user = MagicMock() user.is_platform_admin = False user.workspace_id = ws_id link = MagicMock(spec=UserWorkspace) link.role = UserRole.CUSTOMER_ADMIN mock_result = MagicMock() mock_result.scalar_one_or_none.return_value = link db = AsyncMock() db.execute = AsyncMock(return_value=mock_result) target_ws, role = await get_effective_context(db, user, workspace_id=str(other_ws)) assert target_ws == other_ws assert role == UserRole.CUSTOMER_ADMIN