2025-01-08 21:22:25 +00:00
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
|
2025-01-03 15:06:08 +00:00
|
|
|
import pytest
|
|
|
|
from cryptography.fernet import Fernet, InvalidToken
|
2025-01-08 21:22:25 +00:00
|
|
|
from redis.exceptions import ResponseError
|
2025-01-03 15:06:08 +00:00
|
|
|
|
2025-01-08 21:22:25 +00:00
|
|
|
from pssecret_server.utils import _is_getdel_available, decrypt_secret, encrypt_secret
|
2025-01-03 15:06:08 +00:00
|
|
|
|
|
|
|
from ..factories import SecretFactory
|
|
|
|
|
|
|
|
|
|
|
|
def test_encrypte_secret_ok(fernet: Fernet):
|
|
|
|
secret = SecretFactory().build()
|
|
|
|
encrypted_secret = encrypt_secret(secret, fernet)
|
|
|
|
|
|
|
|
assert secret.data != encrypted_secret.data
|
|
|
|
|
|
|
|
|
|
|
|
def test_secret_is_decryptable_by_correct_key(fernet: Fernet):
|
|
|
|
secret = SecretFactory().build()
|
|
|
|
encrypted_secret = encrypt_secret(secret, fernet)
|
|
|
|
decrypted_secret = decrypt_secret(encrypted_secret.data.encode(), fernet)
|
|
|
|
|
|
|
|
assert decrypted_secret.decode() == secret.data
|
|
|
|
|
|
|
|
|
|
|
|
def test_secret_is_not_decryptable_by_random_key(fernet: Fernet):
|
|
|
|
secret = SecretFactory().build()
|
|
|
|
encrypted_secret = encrypt_secret(secret, fernet)
|
|
|
|
|
|
|
|
random_fernet = Fernet(Fernet.generate_key())
|
|
|
|
|
|
|
|
with pytest.raises(InvalidToken):
|
|
|
|
decrypt_secret(encrypted_secret.data.encode(), random_fernet)
|
2025-01-08 21:22:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("getdel_effect", "expected_result"), [(None, True), (ResponseError, False)]
|
|
|
|
)
|
|
|
|
async def test_is_getdel_available(
|
|
|
|
getdel_effect: ResponseError | None, expected_result: bool
|
|
|
|
):
|
|
|
|
redis = AsyncMock()
|
|
|
|
redis.getdel.side_effect = getdel_effect # pyright: ignore[reportAny]
|
|
|
|
|
|
|
|
result = await _is_getdel_available(redis)
|
|
|
|
|
|
|
|
assert result is expected_result
|