test_secrets.py 901 B

12345678910111213141516171819202122232425262728
  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. with pytest.raises(ValueError):
  20. cipher.decrypt_json(tampered)