You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
import unittest
|
|
from datetime import datetime, timezone
|
|
|
|
from shared.contracts import (
|
|
AdminPermission,
|
|
PublishedToolContract,
|
|
ServiceName,
|
|
StaffRole,
|
|
ToolLifecycleStatus,
|
|
ToolParameterContract,
|
|
ToolParameterType,
|
|
ToolPublicationEnvelope,
|
|
permissions_for_role,
|
|
role_has_permission,
|
|
role_includes,
|
|
)
|
|
|
|
|
|
class AccessControlContractTests(unittest.TestCase):
|
|
def test_role_hierarchy_is_ordered(self):
|
|
self.assertTrue(role_includes(StaffRole.ADMIN, StaffRole.STAFF))
|
|
self.assertTrue(role_includes(StaffRole.STAFF, StaffRole.VIEWER))
|
|
self.assertFalse(role_includes(StaffRole.VIEWER, StaffRole.ADMIN))
|
|
|
|
def test_permissions_are_inherited_by_higher_roles(self):
|
|
self.assertIn(
|
|
AdminPermission.VIEW_REPORTS,
|
|
permissions_for_role(StaffRole.VIEWER),
|
|
)
|
|
self.assertTrue(
|
|
role_has_permission(StaffRole.STAFF, AdminPermission.MANAGE_TOOL_DRAFTS)
|
|
)
|
|
self.assertTrue(
|
|
role_has_permission(StaffRole.ADMIN, AdminPermission.MANAGE_SETTINGS)
|
|
)
|
|
self.assertFalse(
|
|
role_has_permission(StaffRole.VIEWER, AdminPermission.PUBLISH_TOOLS)
|
|
)
|
|
|
|
|
|
class ToolPublicationContractTests(unittest.TestCase):
|
|
def test_tool_publication_envelope_is_built_with_shared_contract(self):
|
|
published_tool = PublishedToolContract(
|
|
tool_name="consultar_financiamento",
|
|
display_name="Consultar Financiamento",
|
|
description="Consulta opcoes de financiamento.",
|
|
version=2,
|
|
status=ToolLifecycleStatus.APPROVED,
|
|
parameters=(
|
|
ToolParameterContract(
|
|
name="valor_veiculo",
|
|
parameter_type=ToolParameterType.NUMBER,
|
|
description="Valor do veiculo em reais.",
|
|
required=True,
|
|
),
|
|
),
|
|
implementation_module="generated_tools.consultar_financiamento",
|
|
implementation_callable="run",
|
|
checksum="sha256:abc123",
|
|
published_at=datetime(2026, 3, 26, 12, 0, tzinfo=timezone.utc),
|
|
published_by="staff:1",
|
|
)
|
|
|
|
envelope = ToolPublicationEnvelope(
|
|
source_service=ServiceName.ADMIN,
|
|
target_service=ServiceName.PRODUCT,
|
|
publication_id="pub_001",
|
|
published_tool=published_tool,
|
|
emitted_at=datetime(2026, 3, 26, 12, 1, tzinfo=timezone.utc),
|
|
)
|
|
|
|
self.assertEqual(envelope.source_service, ServiceName.ADMIN)
|
|
self.assertEqual(envelope.target_service, ServiceName.PRODUCT)
|
|
self.assertEqual(envelope.published_tool.tool_name, "consultar_financiamento")
|
|
self.assertEqual(
|
|
envelope.published_tool.parameters[0].parameter_type,
|
|
ToolParameterType.NUMBER,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|