Spaces:
Running
Running
File size: 4,857 Bytes
7cc81cb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | from __future__ import annotations
from uuid import uuid4
import unittest
from fastapi.testclient import TestClient
from app.container import build_container
from app.core.config import Settings
from app.security.schemas import APIKeyCreate
from app.security.service import APIKeyService
from main import create_app
def _security_settings(tmp_path):
return Settings(
_env_file=None,
temp_dir=tmp_path / "temp",
output_dir=tmp_path / "outputs",
database_url=f"sqlite+aiosqlite:///{tmp_path / 'security.db'}",
social_database_url=f"sqlite+aiosqlite:///{tmp_path / 'social.db'}",
social_auto_migrate=True,
social_worker_enabled=False,
social_oauth_encryption_key="test-only-encryption-material",
auth_enabled=True,
auth_last_used_update_seconds=0,
cleanup_interval_seconds=3600,
whisper_model="tiny",
max_workers=1,
)
async def _create_app(tmp_path):
settings = _security_settings(tmp_path)
container = build_container(settings)
await container.security_database.initialize()
application = create_app(settings)
return application, container
async def _project_context(container, name, scopes):
record, secret = await container.api_keys.create(
APIKeyCreate(name=name, environment="test", role=None, scopes=scopes),
created_by="tests",
)
return record, secret, await container.api_keys.authenticate(secret)
class ApprovalAuthorizationTests(unittest.TestCase):
def test_list_approval_requests_requires_workflow_workspace_membership(self) -> None:
import asyncio
application, _ = asyncio.run(_create_app(self))
with TestClient(application) as client:
unauthorized = client.get(
"/v1/projects/workspace/workflows/other-workflow/requests",
headers={"Authorization": "Bearer invalid"},
)
self.assertEqual(unauthorized.status_code, 401)
def test_approve_and_reject_endpoints_authorize_by_request_workspace(self) -> None:
import asyncio
application, _ = asyncio.run(_create_app(self))
with TestClient(application) as client:
missing = client.post(
"/v1/projects/workspace/requests/missing/approve",
headers={"Authorization": "Bearer invalid"},
)
self.assertEqual(missing.status_code, 404)
def test_approval_workflow_requests_are_scoped_to_owner_workspace(self) -> None:
import asyncio
application, container = asyncio.run(_create_app(self))
with TestClient(application) as client:
owner_key, _, actor_a = asyncio.run(_project_context(container, "Workspace A", [
"projects:read",
"projects:create",
"approvals:create",
"approvals:read",
"approvals:review",
]))
_, _, actor_b = asyncio.run(_project_context(container, "Workspace B", [
"projects:read",
"projects:create",
"approvals:create",
"approvals:read",
"approvals:review",
]))
project = client.post(
"/v1/projects",
json={"name": "Approval Project"},
headers={"Authorization": f"Bearer {owner_key}"},
).json()
workflow = client.post(
"/v1/projects/workspace/workflows",
json={"project_id": project["id"], "name": "Review"},
headers={"Authorization": f"Bearer {owner_key}"},
).json()
approval_request = client.post(
f"/v1/projects/workspace/workflows/{workflow['id']}/requests",
json={"project_id": project["id"]},
headers={"Authorization": f"Bearer {owner_key}"},
).json()
self.assertEqual(
client.get(
f"/v1/projects/workspace/workflows/{workflow['id']}/requests",
headers={"Authorization": f"Bearer {actor_b.api_key_id}"},
).status_code,
404,
)
self.assertEqual(
client.post(
f"/v1/projects/workspace/requests/{approval_request['id']}/approve",
headers={"Authorization": f"Bearer {actor_b.api_key_id}"},
).status_code,
404,
)
self.assertEqual(
client.post(
f"/v1/projects/workspace/requests/{approval_request['id']}/reject",
headers={"Authorization": f"Bearer {actor_b.api_key_id}"},
).status_code,
404,
)
if __name__ == "__main__":
unittest.main()
|