test_verify.py
1 """Tests for attestation verification functions.""" 2 3 import pytest 4 from auths import ( 5 verify_attestation, 6 verify_chain, 7 verify_device_authorization, 8 VerificationResult, 9 VerificationReport, 10 VerificationStatus, 11 ChainLink, 12 ) 13 14 15 class TestVerifyAttestation: 16 17 def test_invalid_json_returns_error(self): 18 result = verify_attestation("not valid json", "a" * 64) 19 assert not result.valid 20 assert result.error is not None 21 assert "parse" in result.error.lower() or "json" in result.error.lower() 22 23 def test_invalid_hex_raises_value_error(self): 24 with pytest.raises(ValueError) as exc_info: 25 verify_attestation("{}", "not-hex") 26 assert "hex" in str(exc_info.value).lower() 27 28 def test_wrong_key_length_raises_value_error(self): 29 with pytest.raises(ValueError) as exc_info: 30 verify_attestation("{}", "abcd") 31 assert "length" in str(exc_info.value).lower() 32 33 def test_verification_result_is_falsy_when_invalid(self): 34 result = verify_attestation("{}", "a" * 64) 35 assert not result 36 assert not result.valid 37 38 39 class TestVerifyChain: 40 41 def test_empty_chain_returns_report(self): 42 report = verify_chain([], "a" * 64) 43 assert isinstance(report, VerificationReport) 44 assert isinstance(report.status, VerificationStatus) 45 assert isinstance(report.chain, list) 46 assert isinstance(report.warnings, list) 47 48 def test_invalid_json_raises_value_error(self): 49 with pytest.raises(ValueError) as exc_info: 50 verify_chain(["not valid json"], "a" * 64) 51 assert "parse" in str(exc_info.value).lower() 52 53 def test_invalid_root_key_raises_value_error(self): 54 with pytest.raises(ValueError) as exc_info: 55 verify_chain([], "not-hex") 56 assert "hex" in str(exc_info.value).lower() 57 58 59 class TestVerifyDeviceAuthorization: 60 61 def test_empty_attestations_returns_report(self): 62 report = verify_device_authorization( 63 "did:key:identity", "did:key:device", [], "a" * 64, 64 ) 65 assert isinstance(report, VerificationReport) 66 assert not report.is_valid() 67 68 def test_invalid_json_raises_value_error(self): 69 with pytest.raises(ValueError): 70 verify_device_authorization( 71 "did:key:identity", "did:key:device", ["not valid json"], "a" * 64, 72 ) 73 74 def test_invalid_pk_hex_raises_value_error(self): 75 with pytest.raises(ValueError): 76 verify_device_authorization( 77 "did:key:identity", "did:key:device", [], "not-hex", 78 ) 79 80 81 class TestTypes: 82 83 def test_verification_status_types(self): 84 report = verify_chain([], "a" * 64) 85 status = report.status 86 assert hasattr(status, "status_type") 87 assert hasattr(status, "at") 88 assert hasattr(status, "step") 89 assert hasattr(status, "missing_link") 90 assert hasattr(status, "is_valid") 91 92 def test_chain_link_attributes(self): 93 assert hasattr(ChainLink, "issuer") 94 assert hasattr(ChainLink, "subject") 95 assert hasattr(ChainLink, "valid") 96 assert hasattr(ChainLink, "error") 97 98 def test_verification_report_attributes(self): 99 report = verify_chain([], "a" * 64) 100 assert hasattr(report, "status") 101 assert hasattr(report, "chain") 102 assert hasattr(report, "warnings") 103 assert hasattr(report, "is_valid") 104 105 106 class TestRepr: 107 108 def test_verification_result_repr(self): 109 result = verify_attestation("{}", "a" * 64) 110 assert "VerificationResult" in repr(result) 111 112 def test_verification_status_repr(self): 113 report = verify_chain([], "a" * 64) 114 assert "VerificationStatus" in repr(report.status) 115 116 def test_verification_report_repr(self): 117 report = verify_chain([], "a" * 64) 118 assert "VerificationReport" in repr(report)