Skip to content

Commit dff6235

Browse files
small refactor
1 parent 38026a5 commit dff6235

File tree

2 files changed

+48
-50
lines changed

2 files changed

+48
-50
lines changed

services/api-server/tests/unit/pact_broker/conftest.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import os
2+
from threading import Thread
3+
from time import sleep
24

35
import pytest
6+
import uvicorn
7+
from fastapi import FastAPI
8+
from servicelib.utils import unused_port
9+
from simcore_service_api_server.api.dependencies.authentication import (
10+
Identity,
11+
get_current_identity,
12+
)
413

514

615
def pytest_addoption(parser: pytest.Parser) -> None:
@@ -60,3 +69,42 @@ def pact_broker_credentials(
6069
)
6170

6271
return broker_url, broker_username, broker_password
72+
73+
74+
def mock_get_current_identity() -> Identity:
75+
return Identity(user_id=1, product_name="osparc", email="[email protected]")
76+
77+
78+
@pytest.fixture()
79+
def run_test_server(
80+
app: FastAPI,
81+
):
82+
"""
83+
Spins up a FastAPI server in a background thread and yields a base URL.
84+
The 'mocked_catalog_service' fixture ensures the function is already
85+
patched by the time we start the server.
86+
"""
87+
# Override
88+
app.dependency_overrides[get_current_identity] = mock_get_current_identity
89+
90+
port = unused_port()
91+
base_url = f"http://localhost:{port}"
92+
93+
config = uvicorn.Config(
94+
app,
95+
host="localhost",
96+
port=port,
97+
log_level="info",
98+
)
99+
server = uvicorn.Server(config)
100+
101+
thread = Thread(target=server.run, daemon=True)
102+
thread.start()
103+
104+
# Wait a bit for the server to be ready
105+
sleep(1)
106+
107+
yield base_url # , before_server_start
108+
109+
server.should_exit = True
110+
thread.join()

services/api-server/tests/unit/pact_broker/test_pact_licensed_items.py

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,21 @@
44
# pylint: disable=too-many-arguments
55

66
import os
7-
from threading import Thread
8-
from time import sleep
97

108
import pytest
11-
import uvicorn
129
from fastapi import FastAPI
1310
from models_library.api_schemas_webserver.licensed_items import (
1411
LicensedItemRpcGet,
1512
LicensedItemRpcGetPage,
1613
)
1714
from pact.v3 import Verifier
1815
from pytest_mock import MockerFixture
19-
from servicelib.utils import unused_port
2016
from simcore_service_api_server._meta import API_VERSION
21-
from simcore_service_api_server.api.dependencies.authentication import (
22-
Identity,
23-
get_current_identity,
24-
)
2517
from simcore_service_api_server.api.dependencies.webserver_rpc import (
2618
get_wb_api_rpc_client,
2719
)
2820
from simcore_service_api_server.services_rpc.wb_api_server import WbApiRpcClient
2921

30-
31-
def mock_get_current_identity() -> Identity:
32-
return Identity(user_id=1, product_name="osparc", email="[email protected]")
33-
34-
3522
# Fake response based on values from 05_licensed_items.json
3623
EXPECTED_LICENSED_ITEMS = [
3724
{
@@ -164,43 +151,6 @@ async def mock_wb_api_server_rpc(app: FastAPI, mocker: MockerFixture) -> MockerF
164151
return mocker
165152

166153

167-
@pytest.fixture()
168-
def run_test_server(
169-
# get_free_port: int,
170-
# get_unused_port: int,
171-
app: FastAPI,
172-
):
173-
"""
174-
Spins up a FastAPI server in a background thread and yields a base URL.
175-
The 'mocked_catalog_service' fixture ensures the function is already
176-
patched by the time we start the server.
177-
"""
178-
# Override
179-
app.dependency_overrides[get_current_identity] = mock_get_current_identity
180-
181-
port = unused_port()
182-
base_url = f"http://localhost:{port}"
183-
184-
config = uvicorn.Config(
185-
app,
186-
host="localhost",
187-
port=port,
188-
log_level="info",
189-
)
190-
server = uvicorn.Server(config)
191-
192-
thread = Thread(target=server.run, daemon=True)
193-
thread.start()
194-
195-
# Wait a bit for the server to be ready
196-
sleep(1)
197-
198-
yield base_url # , before_server_start
199-
200-
server.should_exit = True
201-
thread.join()
202-
203-
204154
@pytest.mark.skipif(
205155
not os.getenv("PACT_BROKER_URL"),
206156
reason="This test runs only if PACT_BROKER_URL is provided",

0 commit comments

Comments
 (0)