/ packages / auths-python / tests / test_verify_witnesses.py
test_verify_witnesses.py
 1  """Tests for witness chain verification (fn-25.2)."""
 2  
 3  import pytest
 4  
 5  from auths import Auths
 6  from auths.verify import WitnessConfig, WitnessKey, verify_chain_with_witnesses
 7  
 8  
 9  class TestWitnessConfigValidation:
10  
11      def test_threshold_zero_raises(self):
12          with pytest.raises(ValueError, match="threshold must be >= 1"):
13              WitnessConfig(
14                  receipts=[],
15                  keys=[WitnessKey(did="did:key:z1", public_key_hex="a" * 64)],
16                  threshold=0,
17              )
18  
19      def test_threshold_exceeds_keys_raises(self):
20          with pytest.raises(ValueError, match="cannot exceed"):
21              WitnessConfig(
22                  receipts=[],
23                  keys=[WitnessKey(did="did:key:z1", public_key_hex="a" * 64)],
24                  threshold=2,
25              )
26  
27      def test_valid_config_creates(self):
28          config = WitnessConfig(
29              receipts=[],
30              keys=[
31                  WitnessKey(did="did:key:z1", public_key_hex="a" * 64),
32                  WitnessKey(did="did:key:z2", public_key_hex="b" * 64),
33              ],
34              threshold=1,
35          )
36          assert config.threshold == 1
37          assert len(config.keys) == 2
38  
39  
40  class TestWitnessKeyDataclass:
41  
42      def test_witness_key_fields(self):
43          wk = WitnessKey(did="did:key:zTest", public_key_hex="ab" * 32)
44          assert wk.did == "did:key:zTest"
45          assert wk.public_key_hex == "ab" * 32
46  
47      def test_witness_key_repr(self):
48          wk = WitnessKey(did="did:key:z6MkLongWitnessDid", public_key_hex="ab" * 32)
49          assert "WitnessKey" in repr(wk)
50  
51  
52  class TestVerifyChainWithWitnessesFFI:
53  
54      def test_empty_chain_with_witnesses(self):
55          config = WitnessConfig(
56              receipts=[],
57              keys=[WitnessKey(did="did:key:z1", public_key_hex="a" * 64)],
58              threshold=1,
59          )
60          report = verify_chain_with_witnesses([], "a" * 64, config)
61          assert hasattr(report, "status")
62          assert hasattr(report, "warnings")
63  
64      def test_invalid_root_key_raises(self):
65          config = WitnessConfig(
66              receipts=[],
67              keys=[WitnessKey(did="did:key:z1", public_key_hex="a" * 64)],
68              threshold=1,
69          )
70          with pytest.raises(ValueError, match="hex"):
71              verify_chain_with_witnesses([], "not-hex", config)
72  
73  
74  class TestClientVerifyChainWithWitnesses:
75  
76      def test_client_verify_chain_with_witnesses(self):
77          auths = Auths()
78          config = WitnessConfig(
79              receipts=[],
80              keys=[WitnessKey(did="did:key:z1", public_key_hex="a" * 64)],
81              threshold=1,
82          )
83          report = auths.verify_chain([], "a" * 64, witnesses=config)
84          assert hasattr(report, "status")
85  
86  
87  class TestImports:
88  
89      def test_imports_from_verify_module(self):
90          from auths.verify import WitnessConfig, WitnessKey, verify_chain_with_witnesses
91          assert WitnessConfig is not None
92          assert WitnessKey is not None
93          assert verify_chain_with_witnesses is not None
94  
95      def test_imports_from_top_level(self):
96          from auths import WitnessConfig, WitnessKey
97          assert WitnessConfig is not None
98          assert WitnessKey is not None