pssecret-server/tests/unit/test_utils.py
Ivan Golikov 869bfc45ac Added support for all Redis versions (>=1.0.0)
Previously support was provided for Redis>=6.2.0
2025-01-08 21:44:41 +01:00

51 lines
1.4 KiB
Python

from unittest.mock import AsyncMock
import pytest
from cryptography.fernet import Fernet, InvalidToken
from redis.exceptions import ResponseError
from pssecret_server.utils import _is_getdel_available, decrypt_secret, encrypt_secret
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)
async def test_is_getdel_available_when_supported():
redis = AsyncMock()
result = await _is_getdel_available(redis)
assert result is True
async def test_is_getdel_available_when_not_supported():
redis = AsyncMock()
redis.getdel.side_effect = ResponseError
result = await _is_getdel_available(redis)
assert result is False