test_secrets.py 908 B

123456789101112131415161718192021222324252627282930
  1. import pytest
  2. from core_shared.secrets import EncryptedSecret, SecretCipher
  3. def test_secret_cipher_round_trips_json_payload() -> None:
  4. cipher = SecretCipher(key="test-key")
  5. payload = {"api_key": "secret-value", "nested": {"token": "abc"}}
  6. encrypted = cipher.encrypt_json(payload)
  7. decrypted = cipher.decrypt_json(encrypted)
  8. assert encrypted.ciphertext
  9. assert encrypted.ciphertext != "secret-value"
  10. assert encrypted.fingerprint
  11. assert decrypted == payload
  12. def test_secret_cipher_rejects_tampered_payload() -> None:
  13. cipher = SecretCipher(key="test-key")
  14. encrypted = cipher.encrypt_json({"api_key": "secret-value"})
  15. tampered = EncryptedSecret(
  16. ciphertext=encrypted.ciphertext[:-2] + "AA",
  17. fingerprint=encrypted.fingerprint,
  18. algorithm=encrypted.algorithm,
  19. )
  20. with pytest.raises(ValueError):
  21. cipher.decrypt_json(tampered)