/ packages / auths-python / tests / test_client.py
test_client.py
 1  """Tests for the Auths client DX layer."""
 2  
 3  import pytest
 4  from auths import Auths, AuthsError, VerificationError, CryptoError, NetworkError
 5  from auths._errors import KeychainError, StorageError, IdentityError
 6  
 7  TEST_SEED_HEX = "a" * 64
 8  
 9  
10  def test_client_instantiation():
11      auths = Auths()
12      assert auths.repo_path == "~/.auths"
13  
14  
15  def test_client_with_repo_path():
16      auths = Auths(repo_path="/tmp/test-repo")
17      assert auths.repo_path == "/tmp/test-repo"
18  
19  
20  def test_verify_invalid_raises_crypto_error():
21      auths = Auths()
22      with pytest.raises(CryptoError) as exc_info:
23          auths.verify(attestation_json="{}", issuer_key="bad-hex")
24      assert exc_info.value.code is not None
25      assert exc_info.value.message is not None
26  
27  
28  def test_sign_returns_hex_string():
29      auths = Auths()
30      sig = auths.sign(b"hello", private_key=TEST_SEED_HEX)
31      assert isinstance(sig, str)
32      bytes.fromhex(sig)
33  
34  
35  def test_sign_invalid_key_raises_crypto_error():
36      auths = Auths()
37      with pytest.raises(CryptoError) as exc_info:
38          auths.sign(b"hello", private_key="bad-key")
39      assert exc_info.value.code == "invalid_key"
40  
41  
42  def test_error_hierarchy():
43      for cls in [VerificationError, CryptoError, KeychainError,
44                  StorageError, NetworkError, IdentityError]:
45          assert issubclass(cls, AuthsError)
46  
47  
48  def test_error_has_code_and_message():
49      err = AuthsError("something broke", code="test_error")
50      assert err.code == "test_error"
51      assert err.message == "something broke"
52      assert "test_error" in repr(err)
53  
54  
55  def test_network_error_has_should_retry():
56      err = NetworkError("timeout", code="timeout", should_retry=True)
57      assert err.should_retry is True