import unittest from fastapi.testclient import TestClient from src.apps.api.gateway import create_app from src.tests.runtime.fixtures import setup, workspace, USER, REVIEWER, AUTHOR, OTHER class APITests(unittest.TestCase): def setUp(self): self.runtime, self.request, self.model, self.sandbox = setup(self.enterContext(workspace())) actors = {a.user_id + a.tenant_id: a for a in [USER, REVIEWER, AUTHOR, OTHER]} app = create_app(self.runtime.skills, self.runtime.knowledge, runtime=self.runtime, verifier=lambda token: actors[token].model_dump()) self.client = self.enterContext(TestClient(app)) self.auth = {'Authorization': 'Bearer ' + USER.user_id + USER.tenant_id, 'Idempotency-Key': 'api-request-1'} def test_api_auth_and_no_client_identity_headers(self): self.assertEqual(self.client.post('/api/v1/coding/runs', json=self.request).status_code, 401) self.assertEqual(self.client.post('/api/v1/coding/runs', json=self.request, headers=dict(self.auth, **{'x-user-id': 'forged'})).status_code, 400) self.assertEqual(self.model.count, 0) def test_no_implicit_execution_and_unknown_request_fields_denied(self): bad = self.client.post('/api/v1/coding/runs', json=dict(self.request, shell='untrusted'), headers=self.auth) self.assertEqual(bad.status_code, 422) created = self.client.post('/api/v1/coding/runs', json=self.request, headers=self.auth) self.assertEqual(created.status_code, 201, created.text) self.assertEqual(created.json()['status'], 'queued') self.assertEqual(self.model.count, 0) self.assertIn('x-trace-id', created.headers) def test_http_full_flow_and_foreign_tenant_denial(self): created = self.client.post('/api/v1/coding/runs', json=self.request, headers=self.auth).json() path = '/api/v1/coding/runs/' + created['id'] other = {'Authorization': 'Bearer ' + OTHER.user_id + OTHER.tenant_id} self.assertEqual(self.client.get(path, headers=other).status_code, 404) for scope in ['design', 'execution', 'acceptance']: response = self.client.post(path + '/advance', headers=self.auth) self.assertEqual(response.status_code, 200, response.text) run = response.json() self.assertEqual(run['pending_approval']['scope'], scope) headers = self.auth if scope != 'acceptance' else {'Authorization': 'Bearer ' + REVIEWER.user_id + REVIEWER.tenant_id} self.assertEqual(self.client.post(path + '/approvals', headers=headers, json={'expected_digest': run['pending_approval']['digest']}).status_code, 200) self.assertEqual(self.client.post(path + '/advance', headers=self.auth).json()['status'], 'succeeded') def test_trial_endpoint_connected_and_release_required(self): headers = {'Authorization': 'Bearer ' + AUTHOR.user_id + AUTHOR.tenant_id, 'Idempotency-Key': 'trial-request-1'} path = '/api/v1/admin/skills/' + self.request['release_ref'].split('@')[0] + '/trials' response = self.client.post(path, headers=headers, json=self.request) self.assertEqual(response.status_code, 200, response.text) self.assertEqual(response.json()['status'], 'queued') self.assertEqual(self.client.post(path, headers=headers, json=dict(self.request, release_ref='missing@0.1.0')).status_code, 422) def test_payload_limit_and_ordinary_mode_has_no_tools(self): self.assertEqual(self.client.post('/api/v1/coding/runs', headers=self.auth, content='x' * (2 * 1024 * 1024 + 1)).status_code, 413) response = self.client.get('/api/v1/coding/policy', headers=self.auth) self.assertFalse(response.json()['skill_required']) self.assertEqual(self.model.count, 0)