Compare commits
No commits in common. "c67826047d924e67ab36c7465f9fe59e5bc4b35d" and "869bfc45acbc0b69aa7eb8537cc891c2d8660897" have entirely different histories.
c67826047d
...
869bfc45ac
3 changed files with 33 additions and 27 deletions
|
@ -37,10 +37,7 @@ async def save_secret(data: Secret, redis: Redis) -> str:
|
||||||
|
|
||||||
@lru_cache
|
@lru_cache
|
||||||
async def _is_getdel_available(redis: Redis) -> bool:
|
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:
|
try:
|
||||||
await redis.getdel("test:getdel:availability")
|
await redis.getdel("test:getdel:availability")
|
||||||
except ResponseError:
|
except ResponseError:
|
||||||
|
@ -50,12 +47,6 @@ async def _is_getdel_available(redis: Redis) -> bool:
|
||||||
|
|
||||||
|
|
||||||
async def getdel(redis: Redis, key: str) -> ResponseT:
|
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
|
result: ResponseT
|
||||||
|
|
||||||
if await _is_getdel_available(redis):
|
if await _is_getdel_available(redis):
|
||||||
|
|
|
@ -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 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
|
||||||
|
@ -36,14 +35,27 @@ async def test_save_secret_data(redis_server: Redis) -> None:
|
||||||
assert redis_data.decode() == secret.data
|
assert redis_data.decode() == secret.data
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("getdel_available", [True, False])
|
@patch("pssecret_server.utils._is_getdel_available", side_effect=AsyncMock())
|
||||||
@patch("pssecret_server.utils._is_getdel_available", new_callable=AsyncMock)
|
async def test_getdel_when_available(
|
||||||
async def test_getdel(
|
is_getdel_available: Mock, redis_server: Redis
|
||||||
mock_is_getdel_available: AsyncMock,
|
|
||||||
getdel_available: bool,
|
|
||||||
redis_server: Redis,
|
|
||||||
) -> None:
|
) -> 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_value = "test_data"
|
||||||
test_key = "test_key"
|
test_key = "test_key"
|
||||||
|
|
|
@ -34,15 +34,18 @@ 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)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
async def test_is_getdel_available_when_supported():
|
||||||
("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 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
|
||||||
|
|
Loading…
Reference in a new issue