""" Unit tests for FastAPI REST Guard endpoints. """ import unittest from fastapi.testclient import TestClient from open_stanag.server.app import app class TestRestAPI(unittest.TestCase): def setUp(self): self.client = TestClient(app) def test_health(self): resp = self.client.get("/health") self.assertEqual(resp.status_code, 200) data = resp.json() self.assertEqual(data["status"], "healthy") self.assertIn("NATO STANAG 4774 (ADatP-4774 v1.3)", data["supported_standards"]) def test_create_and_validate_label(self): # Create label req_data = { "policy_identifier": "NATO", "classification": "SECRET", "releasable_to": ["NATO", "USA", "GBR"], "privacy_mark": "EYES ONLY", "originator_email": "ops@nato.int", } resp = self.client.post("/api/v1/label/create", json=req_data) self.assertEqual(resp.status_code, 200) res_json = resp.json() self.assertTrue(res_json["valid"]) xml_content = res_json["label_xml"] # Validate label val_resp = self.client.post("/api/v1/label/validate", json={"xml_content": xml_content, "schema_type": "4774"}) self.assertEqual(val_resp.status_code, 200) self.assertTrue(val_resp.json()["valid"]) def test_create_and_verify_binding(self): # Create signed binding req_data = { "label": { "policy_identifier": "NATO", "classification": "SECRET", "releasable_to": ["NATO", "USA"], }, "data_uri": "./target_intel.pdf", "content_type": "application/pdf", "sign": True, } resp = self.client.post("/api/v1/bind/create", json=req_data) self.assertEqual(resp.status_code, 200) data = resp.json() self.assertTrue(data["valid"]) self.assertTrue(data["signed"]) bdo_xml = data["bdo_xml"] # Verify binding v_resp = self.client.post("/api/v1/bind/verify", json={"bdo_xml": bdo_xml}) self.assertEqual(v_resp.status_code, 200) v_data = v_resp.json() self.assertTrue(v_data["valid"]) self.assertTrue(v_data["schema_valid"]) self.assertTrue(v_data["crypto_valid"]) def test_guard_inspect_endpoint(self): # Create binding req_data = { "label": { "policy_identifier": "NATO", "classification": "SECRET", "releasable_to": ["NATO", "USA"], }, "data_uri": "./intel.pdf", "sign": True, } b_resp = self.client.post("/api/v1/bind/create", json=req_data) bdo_xml = b_resp.json()["bdo_xml"] # Inspect permit inspect_data = { "bdo_xml": bdo_xml, "subject_id": "analyst@us.af.mil", "clearance": "SECRET", "citizenship": "USA", "coalition_affiliations": ["NATO"], "aal": 2, } i_resp = self.client.post("/api/v1/guard/inspect", json=inspect_data) self.assertEqual(i_resp.status_code, 200) i_json = i_resp.json() self.assertEqual(i_json["decision"], "PERMIT") self.assertTrue(i_json["authorized"]) if __name__ == "__main__": unittest.main()