Skip to content

Commit 20be01b

Browse files
committed
auth api-keys
1 parent 3db651d commit 20be01b

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

services/web/server/src/simcore_service_webserver/rabbitmq.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from aiohttp import web
66
from models_library.api_schemas_webserver import get_webserver_rpc_namespace
77
from models_library.errors import RABBITMQ_CLIENT_UNHEALTHY_MSG
8+
from models_library.rabbitmq_basic_types import RPCNamespace
89
from servicelib.aiohttp.application_keys import (
910
APP_RABBITMQ_CLIENT_KEY,
1011
APP_RABBITMQ_RPC_SERVER_KEY,
@@ -99,6 +100,13 @@ def get_rabbitmq_rpc_server(app: web.Application) -> RabbitMQRPCClient:
99100
return cast(RabbitMQRPCClient, app[APP_RABBITMQ_RPC_SERVER_KEY])
100101

101102

103+
def get_rpc_namespace(app: web.Application) -> RPCNamespace:
104+
settings = get_application_settings(app)
105+
106+
assert settings.WEBSERVER_HOST # nosec
107+
return get_webserver_rpc_namespace(settings.WEBSERVER_HOST)
108+
109+
102110
def create_register_rpc_routes_on_startup(router: RPCRouter):
103111
"""
104112
This high-order function allows for more flexible router registration
@@ -111,10 +119,7 @@ def create_register_rpc_routes_on_startup(router: RPCRouter):
111119

112120
async def _on_startup(app: web.Application):
113121
rpc_server = get_rabbitmq_rpc_server(app)
114-
settings = get_application_settings(app)
115-
116-
assert settings.WEBSERVER_HOST # nosec
117-
rpc_namespace = get_webserver_rpc_namespace(settings.WEBSERVER_HOST)
122+
rpc_namespace = get_rpc_namespace(app)
118123
await rpc_server.register_router(router, rpc_namespace, app)
119124

120125
return _on_startup

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from collections.abc import AsyncIterable, Awaitable, Callable
77

88
import pytest
9-
from aiohttp.test_utils import TestServer
9+
from aiohttp.test_utils import TestClient, TestServer
1010
from faker import Faker
1111
from models_library.basic_types import IDStr
1212
from models_library.products import ProductName
@@ -15,7 +15,7 @@
1515
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
1616
from pytest_simcore.helpers.typing_env import EnvVarsDict
1717
from pytest_simcore.helpers.webserver_login import UserInfoDict
18-
from servicelib.rabbitmq import RabbitMQRPCClient
18+
from servicelib.rabbitmq import RabbitMQRPCClient, RPCNamespace
1919
from servicelib.rabbitmq.rpc_interfaces.webserver.auth.api_keys import (
2020
create_api_key,
2121
delete_api_key_by_key,
@@ -27,6 +27,7 @@
2727
from simcore_service_webserver.api_keys.errors import ApiKeyNotFoundError
2828
from simcore_service_webserver.api_keys.models import ApiKey
2929
from simcore_service_webserver.application_settings import ApplicationSettings
30+
from simcore_service_webserver.rabbitmq import get_rpc_namespace
3031

3132
pytest_simcore_core_services_selection = [
3233
"rabbit",
@@ -104,15 +105,23 @@ async def rpc_client(
104105
return await rabbitmq_rpc_client("client")
105106

106107

108+
@pytest.fixture
109+
async def app_rpc_namespace(client: TestClient) -> RPCNamespace:
110+
assert client.app is not None
111+
return get_rpc_namespace(client.app)
112+
113+
107114
async def test_get_api_key(
108115
fake_user_api_keys: list[ApiKey],
109116
rpc_client: RabbitMQRPCClient,
110117
osparc_product_name: ProductName,
111118
logged_user: UserInfoDict,
119+
app_rpc_namespace: RPCNamespace,
112120
):
113121
for api_key in fake_user_api_keys:
114122
result = await get_api_key(
115123
rpc_client,
124+
app_rpc_namespace,
116125
user_id=logged_user["id"],
117126
product_name=osparc_product_name,
118127
api_key_id=IDStr(api_key.id),
@@ -121,17 +130,18 @@ async def test_get_api_key(
121130

122131

123132
async def test_api_keys_workflow(
124-
web_server: TestServer,
125133
rpc_client: RabbitMQRPCClient,
126134
osparc_product_name: ProductName,
127135
logged_user: UserInfoDict,
136+
app_rpc_namespace: RPCNamespace,
128137
faker: Faker,
129138
):
130139
key_name = faker.pystr()
131140

132141
# creating a key
133142
created_api_key = await create_api_key(
134143
rpc_client,
144+
app_rpc_namespace,
135145
user_id=logged_user["id"],
136146
product_name=osparc_product_name,
137147
api_key=ApiKeyCreate(display_name=key_name, expiration=None),
@@ -141,6 +151,7 @@ async def test_api_keys_workflow(
141151
# query the key is still present
142152
queried_api_key = await get_api_key(
143153
rpc_client,
154+
app_rpc_namespace,
144155
product_name=osparc_product_name,
145156
user_id=logged_user["id"],
146157
api_key_id=created_api_key.id,
@@ -155,6 +166,7 @@ async def test_api_keys_workflow(
155166
# remove the key
156167
await delete_api_key_by_key(
157168
rpc_client,
169+
app_rpc_namespace,
158170
user_id=logged_user["id"],
159171
product_name=osparc_product_name,
160172
api_key=created_api_key.api_key,
@@ -164,6 +176,7 @@ async def test_api_keys_workflow(
164176
# key no longer present
165177
await get_api_key(
166178
rpc_client,
179+
app_rpc_namespace,
167180
product_name=osparc_product_name,
168181
user_id=logged_user["id"],
169182
api_key_id=created_api_key.id,

0 commit comments

Comments
 (0)