test_ontology_exchange.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from __future__ import annotations
  2. import pytest
  3. from tests.data_research.test_ontology_validation import base_graph
  4. def test_json_and_owl_round_trip_preserve_stable_uids_and_hashes():
  5. from app.core.data_research.ontology.exchange import OntologyExchange
  6. from app.core.data_research.ontology.repository import canonical_graph_hash
  7. exchange = OntologyExchange(max_import_bytes=100_000)
  8. graph = base_graph()
  9. json_bytes = exchange.export_json("ontology-1", 3, graph)
  10. owl_bytes = exchange.export_owl("ontology-1", 3, graph)
  11. imported_json = exchange.import_json(json_bytes)
  12. imported_owl = exchange.import_owl(owl_bytes)
  13. assert imported_json["ontology_uid"] == "ontology-1"
  14. assert imported_owl["ontology_uid"] == "ontology-1"
  15. assert imported_owl["graph_document"] == graph
  16. assert canonical_graph_hash(imported_json["graph_document"]) == canonical_graph_hash(graph)
  17. assert canonical_graph_hash(imported_owl["graph_document"]) == canonical_graph_hash(graph)
  18. def test_owl_import_rejects_unsafe_xml_and_size_limits():
  19. from app.core.data_research.ontology.exchange import ExchangeInvalid, OntologyExchange
  20. exchange = OntologyExchange(max_import_bytes=80)
  21. with pytest.raises(ExchangeInvalid, match="unsafe XML"):
  22. exchange.import_owl(b'<!DOCTYPE x [<!ENTITY e SYSTEM "file:///etc/passwd">]><x>&e;</x>')
  23. with pytest.raises(ExchangeInvalid, match="size limit"):
  24. exchange.import_json(b"{" + b" " * 100 + b"}")
  25. def test_exports_redact_secret_like_fields_deterministically():
  26. from app.core.data_research.ontology.exchange import OntologyExchange
  27. graph = base_graph()
  28. graph["classes"][0]["password"] = "must-not-export"
  29. first = OntologyExchange().export_json("ontology-1", 1, graph)
  30. second = OntologyExchange().export_json("ontology-1", 1, graph)
  31. assert first == second
  32. assert b"must-not-export" not in first
  33. assert b"[redacted]" in first