Spaces:
Running
Running
Replace ineffective collaborator authorization tests with async regressions
Browse files
tests/test_project_collaboration_authorization.py
CHANGED
|
@@ -1,23 +1,53 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-
from app.projects.services.collaboration_service import CollaborationService
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
class CollaborationAuthorizationTests(unittest.TestCase):
|
| 10 |
-
def test_project_collaborator_list_requires_project_workspace(self) -> None:
|
| 11 |
-
repository = CollaborationRepository(None)
|
| 12 |
-
service = CollaborationService(repository)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
service.list_project_collaborators("project-from-another-workspace")
|
| 16 |
-
|
| 17 |
-
def test_project_collaborator_removal_requires_project_workspace(self) -> None:
|
| 18 |
-
repository = CollaborationRepository(None)
|
| 19 |
-
service = CollaborationService(repository)
|
| 20 |
|
| 21 |
-
with self.assertRaises(Exception):
|
| 22 |
-
service.remove_project_collaborator("project-from-another-workspace", "user-id")
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
+
from unittest.mock import AsyncMock
|
| 4 |
|
| 5 |
+
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
from app.projects.services.collaboration_service import CollaborationService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
@pytest.mark.asyncio
|
| 11 |
+
async def test_project_collaborator_listing_passes_authenticated_workspace() -> None:
|
| 12 |
+
repository = type("Repository", (), {})()
|
| 13 |
+
repository.list_project_collaborators = AsyncMock(return_value=[])
|
| 14 |
+
service = CollaborationService(repository)
|
| 15 |
+
assert await service.list_project_collaborators("workspace-a", "project-a") == []
|
| 16 |
+
repository.list_project_collaborators.assert_awaited_once_with("workspace-a", "project-a")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@pytest.mark.asyncio
|
| 20 |
+
async def test_project_collaborator_removal_passes_authenticated_workspace() -> None:
|
| 21 |
+
repository = type("Repository", (), {})()
|
| 22 |
+
repository.remove_project_collaborator = AsyncMock()
|
| 23 |
+
service = CollaborationService(repository)
|
| 24 |
+
await service.remove_project_collaborator("workspace-a", "project-a", "user-b")
|
| 25 |
+
repository.remove_project_collaborator.assert_awaited_once_with(
|
| 26 |
+
"workspace-a", "project-a", "user-b"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@pytest.mark.asyncio
|
| 31 |
+
async def test_invitation_requires_workspace_admin_or_owner() -> None:
|
| 32 |
+
repository = type("Repository", (), {})()
|
| 33 |
+
repository.get_membership = AsyncMock(return_value=type(
|
| 34 |
+
"Membership", (), {"role": "member", "status": "active"}
|
| 35 |
+
)())
|
| 36 |
+
service = CollaborationService(repository)
|
| 37 |
+
with pytest.raises(Exception, match="Only workspace owners or admins"):
|
| 38 |
+
await service.invite_member("workspace-a", "user-a", "new@example.com", "member")
|
| 39 |
+
repository.invite_member_with_hash = AsyncMock()
|
| 40 |
+
repository.invite_member_with_hash.assert_not_awaited()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@pytest.mark.asyncio
|
| 44 |
+
async def test_member_role_changes_reject_privileged_unknown_roles() -> None:
|
| 45 |
+
repository = type("Repository", (), {})()
|
| 46 |
+
repository.get_membership = AsyncMock(return_value=type(
|
| 47 |
+
"Membership", (), {"role": "admin", "status": "active", "user_id": "admin-a"}
|
| 48 |
+
)())
|
| 49 |
+
repository.update_member_role = AsyncMock()
|
| 50 |
+
service = CollaborationService(repository)
|
| 51 |
+
with pytest.raises(ValueError, match="Member role must be admin or member"):
|
| 52 |
+
await service.update_member_role("workspace-a", "admin-a", "user-b", "owner")
|
| 53 |
+
repository.update_member_role.assert_not_awaited()
|