Compare commits

..

No commits in common. "c67826047d924e67ab36c7465f9fe59e5bc4b35d" and "869bfc45acbc0b69aa7eb8537cc891c2d8660897" have entirely different histories.

3 changed files with 33 additions and 27 deletions

View file

@ -37,10 +37,7 @@ async def save_secret(data: Secret, redis: Redis) -> str:
@lru_cache
async def _is_getdel_available(redis: Redis) -> bool:
"""Checks the availability of GETDEL command on the Redis server instance
GETDEL is not available in Redis prior to version 6.2
"""
"""GETDEL is not available in Redis prior to version 6.2"""
try:
await redis.getdel("test:getdel:availability")
except ResponseError:
@ -50,12 +47,6 @@ async def _is_getdel_available(redis: Redis) -> bool:
async def getdel(redis: Redis, key: str) -> ResponseT:
"""Gets the value of key and deletes the key
Depending on the capabilities of Redis server this function
will either call GETDEL command, either first call GETSET with empty string
and DEL right after that.
"""
result: ResponseT
if await _is_getdel_available(redis):

View file

@ -1,6 +1,5 @@
from unittest.mock import AsyncMock, patch
from unittest.mock import AsyncMock, Mock, patch
import pytest
from redis.asyncio import Redis
from pssecret_server.utils import get_new_key, getdel, save_secret
@ -36,14 +35,27 @@ async def test_save_secret_data(redis_server: Redis) -> None:
assert redis_data.decode() == secret.data
@pytest.mark.parametrize("getdel_available", [True, False])
@patch("pssecret_server.utils._is_getdel_available", new_callable=AsyncMock)
async def test_getdel(
mock_is_getdel_available: AsyncMock,
getdel_available: bool,
redis_server: Redis,
@patch("pssecret_server.utils._is_getdel_available", side_effect=AsyncMock())
async def test_getdel_when_available(
is_getdel_available: Mock, redis_server: Redis
) -> None:
mock_is_getdel_available.return_value = getdel_available
is_getdel_available.side_effect.return_value = True
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_key = "test_key"

View file

@ -34,15 +34,18 @@ def test_secret_is_not_decryptable_by_random_key(fernet: Fernet):
decrypt_secret(encrypted_secret.data.encode(), random_fernet)
@pytest.mark.parametrize(
("getdel_effect", "expected_result"), [(None, True), (ResponseError, False)]
)
async def test_is_getdel_available(
getdel_effect: ResponseError | None, expected_result: bool
):
async def test_is_getdel_available_when_supported():
redis = AsyncMock()
redis.getdel.side_effect = getdel_effect # pyright: ignore[reportAny]
result = await _is_getdel_available(redis)
assert result is expected_result
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