Added support for all Redis versions (>=1.0.0) #5
2 changed files with 17 additions and 32 deletions
|
@ -1,5 +1,6 @@
|
||||||
from unittest.mock import AsyncMock, Mock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
from redis.asyncio import Redis
|
from redis.asyncio import Redis
|
||||||
|
|
||||||
from pssecret_server.utils import get_new_key, getdel, save_secret
|
from pssecret_server.utils import get_new_key, getdel, save_secret
|
||||||
|
@ -35,27 +36,14 @@ async def test_save_secret_data(redis_server: Redis) -> None:
|
||||||
assert redis_data.decode() == secret.data
|
assert redis_data.decode() == secret.data
|
||||||
|
|
||||||
|
|
||||||
@patch("pssecret_server.utils._is_getdel_available", side_effect=AsyncMock())
|
@pytest.mark.parametrize("getdel_available", [True, False])
|
||||||
async def test_getdel_when_available(
|
@patch("pssecret_server.utils._is_getdel_available", new_callable=AsyncMock)
|
||||||
is_getdel_available: Mock, redis_server: Redis
|
async def test_getdel(
|
||||||
|
mock_is_getdel_available: AsyncMock,
|
||||||
|
getdel_available: bool,
|
||||||
|
redis_server: Redis,
|
||||||
) -> None:
|
) -> None:
|
||||||
is_getdel_available.side_effect.return_value = True
|
mock_is_getdel_available.return_value = getdel_available
|
||||||
|
|
||||||
test_value = "test_data"
|
|
||||||
test_key = "test_key"
|
|
||||||
await redis_server.set(test_key, test_value)
|
|
||||||
|
|
||||||
result = await getdel(redis_server, test_key)
|
|
||||||
|
|
||||||
assert result.decode() == test_value
|
|
||||||
assert not await redis_server.exists(test_key)
|
|
||||||
|
|
||||||
|
|
||||||
@patch("pssecret_server.utils._is_getdel_available", side_effect=AsyncMock())
|
|
||||||
async def test_getdel_when_not_available(
|
|
||||||
is_getdel_available: Mock, redis_server: Redis
|
|
||||||
) -> None:
|
|
||||||
is_getdel_available.side_effect.return_value = False
|
|
||||||
|
|
||||||
test_value = "test_data"
|
test_value = "test_data"
|
||||||
test_key = "test_key"
|
test_key = "test_key"
|
||||||
|
|
|
@ -34,18 +34,15 @@ def test_secret_is_not_decryptable_by_random_key(fernet: Fernet):
|
||||||
decrypt_secret(encrypted_secret.data.encode(), random_fernet)
|
decrypt_secret(encrypted_secret.data.encode(), random_fernet)
|
||||||
|
|
||||||
|
|
||||||
async def test_is_getdel_available_when_supported():
|
@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 = AsyncMock()
|
||||||
|
redis.getdel.side_effect = getdel_effect # pyright: ignore[reportAny]
|
||||||
|
|
||||||
result = await _is_getdel_available(redis)
|
result = await _is_getdel_available(redis)
|
||||||
|
|
||||||
assert result is True
|
assert result is expected_result
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
Loading…
Reference in a new issue