Skip to content

Commit 8aac0bf

Browse files
refactor delete
1 parent 7d0f37c commit 8aac0bf

File tree

5 files changed

+51
-24
lines changed

5 files changed

+51
-24
lines changed

services/web/server/src/simcore_service_webserver/api_keys/_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ async def get_or_create_api_key(
103103
async def delete_api_key(
104104
app: web.Application,
105105
*,
106-
name: str,
106+
api_key_id: int,
107107
user_id: UserID,
108108
product_name: ProductName,
109109
) -> None:
110-
await db.delete_by_name(
111-
app, display_name=name, user_id=user_id, product_name=product_name
110+
await db.delete(
111+
app, api_key_id=api_key_id, user_id=user_id, product_name=product_name
112112
)
113113

114114

services/web/server/src/simcore_service_webserver/api_keys/_db.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ async def get_or_create(
123123
return ApiKeyInDB.model_validate(row)
124124

125125

126+
async def delete(
127+
app: web.Application,
128+
connection: AsyncConnection | None = None,
129+
*,
130+
api_key_id: int,
131+
user_id: UserID,
132+
product_name: ProductName,
133+
) -> None:
134+
async with transaction_context(get_asyncpg_engine(app), connection) as conn:
135+
stmt = api_keys.delete().where(
136+
(api_keys.c.user_id == user_id)
137+
& (api_keys.c.id == api_key_id)
138+
& (api_keys.c.product_name == product_name)
139+
)
140+
await conn.execute(stmt)
141+
142+
126143
async def delete_by_name(
127144
app: web.Application,
128145
connection: AsyncConnection | None = None,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from models_library.rest_base import StrictRequestParameters
2+
3+
4+
class ApiKeysPathParams(StrictRequestParameters):
5+
api_key_id: int

services/web/server/src/simcore_service_webserver/api_keys/_rest.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
from aiohttp.web import RouteTableDef
55
from models_library.api_schemas_webserver.auth import ApiKeyCreate
66
from servicelib.aiohttp import status
7-
from servicelib.aiohttp.requests_validation import parse_request_body_as
7+
from servicelib.aiohttp.requests_validation import (
8+
parse_request_body_as,
9+
parse_request_path_parameters_as,
10+
)
811
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
912
from simcore_postgres_database.errors import DatabaseError
13+
from simcore_service_webserver.api_keys._models import ApiKeysPathParams
1014

1115
from .._meta import API_VTAG
1216
from ..login.decorators import login_required
@@ -56,26 +60,25 @@ async def create_api_key(request: web.Request):
5660
return envelope_json_response(data)
5761

5862

59-
@routes.delete(f"/{API_VTAG}/auth/api-keys", name="delete_api_key")
63+
@routes.delete(f"/{API_VTAG}/auth/api-keys/{{api_key_id}}", name="delete_api_key")
6064
@login_required
6165
@permission_required("user.apikey.*")
6266
async def delete_api_key(request: web.Request):
6367
req_ctx = RequestContext.model_validate(request)
64-
65-
# NOTE: SEE https://github.com/ITISFoundation/osparc-simcore/issues/4920
66-
body = await request.json()
67-
name = body.get("display_name")
68+
path_params = parse_request_path_parameters_as(ApiKeysPathParams, request)
6869

6970
try:
7071
await _api.delete_api_key(
7172
request.app,
72-
name=name,
73+
api_key_id=path_params.api_key_id,
7374
user_id=req_ctx.user_id,
7475
product_name=req_ctx.product_name,
7576
)
7677
except DatabaseError as err:
7778
_logger.warning(
78-
"Failed to delete API key %s. Ignoring error", name, exc_info=err
79+
"Failed to delete API key with ID: %s. Ignoring error",
80+
path_params.api_key_id,
81+
exc_info=err,
7982
)
8083

8184
return web.json_response(status=status.HTTP_204_NO_CONTENT)

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pytest
1212
import simcore_service_webserver.api_keys._db as db
1313
from aiohttp.test_utils import TestClient
14+
from faker import Faker
1415
from models_library.products import ProductName
1516
from pytest_simcore.helpers.assert_checks import assert_status
1617
from pytest_simcore.helpers.webserver_login import NewUser, UserInfoDict
@@ -28,27 +29,28 @@ async def fake_user_api_keys(
2829
client: TestClient,
2930
logged_user: UserInfoDict,
3031
osparc_product_name: ProductName,
32+
faker: Faker,
3133
) -> AsyncIterable[list[str]]:
3234
assert client.app
33-
names = ["foo", "bar", "beta", "alpha"]
35+
api_key_ids = []
3436

35-
for name in names:
36-
await db.create(
37+
for _ in range(5):
38+
api_key_ids += await db.create(
3739
client.app,
3840
user_id=logged_user["id"],
3941
product_name=osparc_product_name,
40-
display_name=name,
42+
display_name=faker.pystr(),
4143
expiration=None,
42-
api_key=f"{name}-key",
43-
api_secret=f"{name}-secret",
44+
api_key=faker.pystr(),
45+
api_secret=faker.pystr(),
4446
)
4547

46-
yield names
48+
yield api_key_ids
4749

48-
for name in names:
49-
await db.delete_by_name(
50+
for api_key_id in api_key_ids:
51+
await db.delete(
5052
client.app,
51-
display_name=name,
53+
api_key_id=api_key_id,
5254
user_id=logged_user["id"],
5355
product_name=osparc_product_name,
5456
)
@@ -122,11 +124,11 @@ async def test_delete_api_keys(
122124
expected: HTTPStatus,
123125
disable_gc_manual_guest_users: None,
124126
):
125-
resp = await client.delete("/v0/auth/api-keys", json={"display_name": "foo"})
127+
resp = await client.delete("/v0/auth/api-keys/0")
126128
await assert_status(resp, expected)
127129

128-
for name in fake_user_api_keys:
129-
resp = await client.delete("/v0/auth/api-keys", json={"display_name": name})
130+
for api_key_id in fake_user_api_keys:
131+
resp = await client.delete(f"/v0/auth/api-keys/{api_key_id}")
130132
await assert_status(resp, expected)
131133

132134

0 commit comments

Comments
 (0)