import unittest from fastapi.testclient import TestClient from admin_app.api.dependencies import get_current_panel_staff_principal from admin_app.app_factory import create_app from admin_app.core import AdminSettings, AuthenticatedStaffPrincipal from shared.contracts import StaffRole class AdminPanelToolsWebTests(unittest.TestCase): def _build_client_with_role( self, role: StaffRole, settings: AdminSettings | None = None, ) -> tuple[TestClient, object]: app = create_app( settings or AdminSettings( admin_auth_token_secret="test-secret", admin_api_prefix="/admin", ) ) app.dependency_overrides[get_current_panel_staff_principal] = lambda: AuthenticatedStaffPrincipal( id=21, email="colaborador@empresa.com" if role == StaffRole.COLABORADOR else "diretor@empresa.com", display_name="Equipe Web", role=role, is_active=True, ) return TestClient(app), app def test_panel_tools_overview_is_available_for_colaborador_session(self): client, app = self._build_client_with_role(StaffRole.COLABORADOR) try: response = client.get("/admin/panel/tools/overview") finally: app.dependency_overrides.clear() self.assertEqual(response.status_code, 200) payload = response.json() self.assertEqual(payload["mode"], "bootstrap_catalog") self.assertIn("/admin/panel/tools/contracts", [item["href"] for item in payload["actions"]]) self.assertIn("/admin/panel/tools/drafts/intake", [item["href"] for item in payload["actions"]]) def test_panel_tool_intake_accepts_validated_preview_for_colaborador(self): client, app = self._build_client_with_role(StaffRole.COLABORADOR) try: response = client.post( "/admin/panel/tools/drafts/intake", json={ "domain": "vendas", "tool_name": "consultar_vendas_periodo", "display_name": "Consultar vendas por periodo", "description": "Consulta vendas consolidadas por periodo informado no painel.", "business_goal": "Ajudar o time interno a acompanhar o desempenho comercial com mais agilidade.", "parameters": [ { "name": "periodo_inicio", "parameter_type": "string", "description": "Data inicial usada no filtro.", "required": True, }, { "name": "periodo_fim", "parameter_type": "string", "description": "Data final usada no filtro.", "required": True, }, ], }, ) finally: app.dependency_overrides.clear() self.assertEqual(response.status_code, 200) payload = response.json() self.assertEqual(payload["storage_status"], "validated_preview") self.assertEqual(payload["draft_preview"]["status"], "draft") self.assertEqual(payload["draft_preview"]["tool_name"], "consultar_vendas_periodo") self.assertTrue(payload["draft_preview"]["requires_director_approval"]) self.assertEqual(len(payload["draft_preview"]["parameters"]), 2) def test_panel_tools_review_queue_requires_director_session(self): client, app = self._build_client_with_role(StaffRole.COLABORADOR) try: response = client.get("/admin/panel/tools/review-queue") finally: app.dependency_overrides.clear() self.assertEqual(response.status_code, 403) self.assertEqual( response.json()["detail"], "Permissao administrativa insuficiente: 'review_tool_generations'.", ) def test_panel_tools_review_queue_is_available_for_diretor_session(self): client, app = self._build_client_with_role(StaffRole.DIRETOR) try: response = client.get("/admin/panel/tools/review-queue") finally: app.dependency_overrides.clear() self.assertEqual(response.status_code, 200) self.assertEqual(response.json()["queue_mode"], "bootstrap_empty_state") def test_panel_tools_publications_require_director_publication_permission(self): client, app = self._build_client_with_role(StaffRole.COLABORADOR) try: response = client.get("/admin/panel/tools/publications") finally: app.dependency_overrides.clear() self.assertEqual(response.status_code, 403) self.assertEqual( response.json()["detail"], "Permissao administrativa insuficiente: 'publish_tools'.", ) def test_panel_tools_publications_return_catalog_for_diretor_session(self): client, app = self._build_client_with_role(StaffRole.DIRETOR) try: response = client.get("/admin/panel/tools/publications") finally: app.dependency_overrides.clear() self.assertEqual(response.status_code, 200) payload = response.json() self.assertEqual(payload["target_service"], "product") self.assertGreaterEqual(len(payload["publications"]), 10) self.assertIn("consultar_estoque", [item["tool_name"] for item in payload["publications"]]) if __name__ == "__main__": unittest.main()