tests/test_vault_crud.py
| 1 | """Tests for Wallet CRUD operations.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from pqc_agent_wallet import Wallet |
| 8 | from pqc_agent_wallet.errors import CredentialNotFoundError, WalletLockedError |
| 9 | |
| 10 | |
| 11 | def test_put_and_get_roundtrip(open_wallet: Wallet) -> None: |
| 12 | assert open_wallet.get("openai_api_key") == "sk-test-openai" |
| 13 | assert open_wallet.get("postgres_password") == "db-pass-123" |
| 14 | |
| 15 | |
| 16 | def test_get_missing_raises(open_wallet: Wallet) -> None: |
| 17 | with pytest.raises(CredentialNotFoundError): |
| 18 | open_wallet.get("does-not-exist") |
| 19 | |
| 20 | |
| 21 | def test_put_requires_unlock(open_wallet: Wallet) -> None: |
| 22 | open_wallet.lock() |
| 23 | with pytest.raises(WalletLockedError): |
| 24 | open_wallet.put("x", "y") |
| 25 | |
| 26 | |
| 27 | def test_get_requires_unlock(open_wallet: Wallet) -> None: |
| 28 | open_wallet.lock() |
| 29 | with pytest.raises(WalletLockedError): |
| 30 | open_wallet.get("openai_api_key") |
| 31 | |
| 32 | |
| 33 | def test_delete_requires_unlock(open_wallet: Wallet) -> None: |
| 34 | open_wallet.lock() |
| 35 | with pytest.raises(WalletLockedError): |
| 36 | open_wallet.delete("openai_api_key") |
| 37 | |
| 38 | |
| 39 | def test_delete_removes_credential(open_wallet: Wallet) -> None: |
| 40 | open_wallet.delete("openai_api_key") |
| 41 | assert "openai_api_key" not in open_wallet.list_names() |
| 42 | with pytest.raises(CredentialNotFoundError): |
| 43 | open_wallet.get("openai_api_key") |
| 44 | |
| 45 | |
| 46 | def test_delete_missing_raises(open_wallet: Wallet) -> None: |
| 47 | with pytest.raises(CredentialNotFoundError): |
| 48 | open_wallet.delete("does-not-exist") |
| 49 | |
| 50 | |
| 51 | def test_list_names_sorted(open_wallet: Wallet) -> None: |
| 52 | names = open_wallet.list_names() |
| 53 | assert names == sorted(names) |
| 54 | assert "openai_api_key" in names |
| 55 | assert "postgres_password" in names |
| 56 | |
| 57 | |
| 58 | def test_rotate_updates_rotated_at_and_preserves_created_at( |
| 59 | open_wallet: Wallet, |
| 60 | ) -> None: |
| 61 | original_created = next( |
| 62 | m.created_at |
| 63 | for m in open_wallet.list_metadata() |
| 64 | if m.name == "openai_api_key" |
| 65 | ) |
| 66 | assert original_created != "" |
| 67 | |
| 68 | open_wallet.rotate("openai_api_key", "sk-rotated-value") |
| 69 | |
| 70 | rotated_meta = next( |
| 71 | m for m in open_wallet.list_metadata() if m.name == "openai_api_key" |
| 72 | ) |
| 73 | assert rotated_meta.created_at == original_created |
| 74 | assert rotated_meta.rotated_at != "" |
| 75 | assert open_wallet.get("openai_api_key") == "sk-rotated-value" |
| 76 | |
| 77 | |
| 78 | def test_get_credential_returns_metadata_and_value(open_wallet: Wallet) -> None: |
| 79 | cred = open_wallet.get_credential("openai_api_key") |
| 80 | assert cred.value == "sk-test-openai" |
| 81 | assert cred.metadata.name == "openai_api_key" |
| 82 | assert cred.metadata.service == "openai" |
| 83 | |