|
3 | 3 | # pylint: disable=unused-variable |
4 | 4 | # pylint: disable=too-many-arguments |
5 | 5 |
|
6 | | -from collections.abc import AsyncGenerator, Callable |
| 6 | +from collections.abc import AsyncGenerator, AsyncIterator, Callable |
7 | 7 |
|
8 | | -import httpx |
9 | 8 | import pytest |
| 9 | +from asgi_lifespan import LifespanManager |
| 10 | +from fastapi import FastAPI |
10 | 11 | from fastapi.security import HTTPBasicCredentials |
11 | 12 | from models_library.api_schemas_api_server.api_keys import ApiKeyInDB |
12 | 13 | from pydantic import PositiveInt |
13 | 14 | from simcore_service_api_server.api.dependencies.authentication import ( |
14 | 15 | get_current_identity, |
15 | 16 | ) |
| 17 | +from simcore_service_api_server.clients.postgres import get_engine |
16 | 18 | from simcore_service_api_server.repository.api_keys import ApiKeysRepository |
17 | 19 | from simcore_service_api_server.repository.users import UsersRepository |
18 | 20 | from sqlalchemy.ext.asyncio import AsyncEngine |
19 | 21 |
|
| 22 | +MAX_TIME_FOR_APP_TO_STARTUP = 10 |
| 23 | +MAX_TIME_FOR_APP_TO_SHUTDOWN = 10 |
| 24 | + |
20 | 25 |
|
21 | 26 | @pytest.fixture |
22 | | -def async_engine_after_app_started( |
23 | | - client: httpx.AsyncClient, # ensures app context is available |
24 | | - async_engine: AsyncEngine, |
25 | | -) -> AsyncEngine: |
26 | | - return async_engine |
| 27 | +async def app_started(app: FastAPI, is_pdb_enabled: bool) -> AsyncIterator[FastAPI]: |
| 28 | + # LifespanManager will trigger app's startup&shutown event handlers |
| 29 | + async with LifespanManager( |
| 30 | + app, |
| 31 | + startup_timeout=None if is_pdb_enabled else MAX_TIME_FOR_APP_TO_STARTUP, |
| 32 | + shutdown_timeout=None if is_pdb_enabled else MAX_TIME_FOR_APP_TO_SHUTDOWN, |
| 33 | + ): |
| 34 | + yield app |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +async def async_engine(app_started: FastAPI) -> AsyncEngine: |
| 39 | + # Overrides |
| 40 | + return get_engine(app_started) |
27 | 41 |
|
28 | 42 |
|
29 | 43 | @pytest.fixture |
30 | 44 | def api_key_repo( |
31 | | - async_engine_after_app_started: AsyncEngine, |
| 45 | + async_engine: AsyncEngine, |
32 | 46 | ) -> ApiKeysRepository: |
33 | | - return ApiKeysRepository(db_engine=async_engine_after_app_started) |
| 47 | + return ApiKeysRepository(db_engine=async_engine) |
34 | 48 |
|
35 | 49 |
|
36 | 50 | @pytest.fixture |
37 | 51 | def users_repo( |
38 | | - async_engine_after_app_started: AsyncEngine, |
| 52 | + async_engine: AsyncEngine, |
39 | 53 | ) -> UsersRepository: |
40 | | - return UsersRepository(db_engine=async_engine_after_app_started) |
| 54 | + return UsersRepository(db_engine=async_engine) |
41 | 55 |
|
42 | 56 |
|
43 | 57 | async def test_get_user_with_valid_credentials( |
|
0 commit comments