tests/test_interop.py

Read-only source from the unchanged 0.3.4 kit. Existing license terms apply.

"""Independent Python signer to JavaScript verifier interoperability check."""
import json
import shutil
import subprocess
import tempfile
import unittest
from pathlib import Path

import test_conformance as fixture

ROOT = Path(__file__).resolve().parents[1]


class InteropTests(unittest.TestCase):
    @unittest.skipUnless(shutil.which("node"), "Node.js is not installed")
    def test_python_packet_verifies_in_javascript(self):
        packet = fixture.ConformanceTests()._signed_identity_packet()
        # JSON object ordering is not part of the signed AIL claims.
        packet["verification"] = dict(reversed(list(packet["verification"].items())))
        with tempfile.TemporaryDirectory() as tmp:
            path = Path(tmp) / "packet.json"
            path.write_text(json.dumps(packet), encoding="utf-8")
            verifier = ROOT / "owner_app" / "verify-packet.cjs"
            result = subprocess.run(["node", str(verifier), str(path)], capture_output=True, text=True)
            self.assertEqual(result.returncode, 0, result.stderr)
            self.assertEqual(json.loads(result.stdout)["signature_status"], "ail:cryptographically_verified")