|
10 | 10 | from http.client import HTTPException |
11 | 11 |
|
12 | 12 | import pytest |
| 13 | +import tenacity |
13 | 14 | from aiohttp.test_utils import TestClient |
14 | 15 | from faker import Faker |
15 | 16 | from models_library.products import ProductName |
| 17 | +from pytest_mock import MockerFixture |
16 | 18 | 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 |
17 | 21 | from pytest_simcore.helpers.webserver_login import NewUser, UserInfoDict |
18 | 22 | from servicelib.aiohttp import status |
| 23 | +from servicelib.aiohttp.application_keys import APP_SETTINGS_KEY |
19 | 24 | from simcore_service_webserver.api_keys import _repository as repo |
20 | 25 | from simcore_service_webserver.api_keys._models import ApiKey |
21 | 26 | from simcore_service_webserver.api_keys._service import ( |
22 | 27 | get_or_create_api_key, |
23 | 28 | prune_expired_api_keys, |
24 | 29 | ) |
| 30 | +from simcore_service_webserver.application_settings import GarbageCollectorSettings |
25 | 31 | from simcore_service_webserver.db.models import UserRole |
| 32 | +from tenacity import retry_if_exception_type, stop_after_attempt, wait_fixed |
26 | 33 |
|
27 | 34 |
|
28 | 35 | @pytest.fixture |
@@ -215,3 +222,36 @@ async def test_get_not_existing_api_key( |
215 | 222 |
|
216 | 223 | if not errors: |
217 | 224 | 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