Skip to content

Commit 6362c1b

Browse files
bisgaard-itismrnicegyu11
authored andcommitted
Add a test to check if pruning of api keys is triggered (ITISFoundation#7290)
1 parent 34e848a commit 6362c1b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

services/web/server/tests/unit/with_dbs/01/test_api_keys.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,26 @@
1010
from http.client import HTTPException
1111

1212
import pytest
13+
import tenacity
1314
from aiohttp.test_utils import TestClient
1415
from faker import Faker
1516
from models_library.products import ProductName
17+
from pytest_mock import MockerFixture
1618
from pytest_simcore.helpers.assert_checks import assert_status
19+
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
20+
from pytest_simcore.helpers.typing_env import EnvVarsDict
1721
from pytest_simcore.helpers.webserver_login import NewUser, UserInfoDict
1822
from servicelib.aiohttp import status
23+
from servicelib.aiohttp.application_keys import APP_SETTINGS_KEY
1924
from simcore_service_webserver.api_keys import _repository as repo
2025
from simcore_service_webserver.api_keys._models import ApiKey
2126
from simcore_service_webserver.api_keys._service import (
2227
get_or_create_api_key,
2328
prune_expired_api_keys,
2429
)
30+
from simcore_service_webserver.application_settings import GarbageCollectorSettings
2531
from simcore_service_webserver.db.models import UserRole
32+
from tenacity import retry_if_exception_type, stop_after_attempt, wait_fixed
2633

2734

2835
@pytest.fixture
@@ -215,3 +222,36 @@ async def test_get_not_existing_api_key(
215222

216223
if not errors:
217224
assert data is None
225+
226+
227+
@pytest.fixture
228+
async def app_environment(
229+
app_environment: EnvVarsDict,
230+
monkeypatch: pytest.MonkeyPatch,
231+
) -> EnvVarsDict:
232+
return app_environment | setenvs_from_dict(
233+
monkeypatch,
234+
{
235+
"WEBSERVER_GARBAGE_COLLECTOR": '{"GARBAGE_COLLECTOR_INTERVAL_S": 30, "GARBAGE_COLLECTOR_PRUNE_APIKEYS_INTERVAL_S": 1}'
236+
},
237+
)
238+
239+
240+
async def test_prune_expired_api_keys_task_is_triggered(
241+
app_environment: EnvVarsDict, mocker: MockerFixture, client: TestClient
242+
):
243+
mock = mocker.patch(
244+
"simcore_service_webserver.api_keys._service._repository.prune_expired"
245+
)
246+
settings = client.server.app[ # type: ignore
247+
APP_SETTINGS_KEY
248+
].WEBSERVER_GARBAGE_COLLECTOR
249+
assert isinstance(settings, GarbageCollectorSettings)
250+
async for attempt in tenacity.AsyncRetrying(
251+
stop=stop_after_attempt(5),
252+
wait=wait_fixed(1),
253+
retry=retry_if_exception_type(AssertionError),
254+
reraise=True,
255+
):
256+
with attempt:
257+
mock.assert_called()

0 commit comments

Comments
 (0)