Skip to content

Commit de1c021

Browse files
fix test
1 parent 5dfe8e3 commit de1c021

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
exception_handling_decorator,
77
to_exceptions_handlers_map,
88
)
9-
from .errors import ApiKeyNotFoundError
9+
from .errors import ApiKeyDuplicatedDisplayNameError, ApiKeyNotFoundError
1010

1111
_TO_HTTP_ERROR_MAP: ExceptionToHttpErrorMap = {
12+
ApiKeyDuplicatedDisplayNameError: HttpErrorInfo(
13+
status.HTTP_409_CONFLICT,
14+
"API key display name duplicated",
15+
),
1216
ApiKeyNotFoundError: HttpErrorInfo(
1317
status.HTTP_404_NOT_FOUND,
1418
"API key was not found",

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import sqlalchemy as sa
55
from aiohttp import web
6+
from asyncpg.exceptions import UniqueViolationError
67
from models_library.products import ProductName
78
from models_library.users import UserID
89
from simcore_postgres_database.models.api_keys import api_keys
@@ -12,6 +13,7 @@
1213
from sqlalchemy.ext.asyncio import AsyncConnection
1314

1415
from ..db.plugin import get_asyncpg_engine
16+
from .errors import ApiKeyDuplicatedDisplayNameError
1517

1618
_logger = logging.getLogger(__name__)
1719

@@ -28,29 +30,32 @@ async def create_api_key(
2830
api_secret: str,
2931
) -> ApiKey:
3032
async with transaction_context(get_asyncpg_engine(app), connection) as conn:
31-
stmt = (
32-
api_keys.insert()
33-
.values(
33+
try:
34+
stmt = (
35+
api_keys.insert()
36+
.values(
37+
display_name=display_name,
38+
user_id=user_id,
39+
product_name=product_name,
40+
api_key=api_key,
41+
api_secret=api_secret,
42+
expires_at=(sa.func.now() + expiration) if expiration else None,
43+
)
44+
.returning(api_keys.c.id)
45+
)
46+
47+
result = await conn.stream(stmt)
48+
row = await result.first()
49+
50+
return ApiKey(
51+
id=f"{row.id}", # NOTE See: https://github.com/ITISFoundation/osparc-simcore/issues/6919
3452
display_name=display_name,
35-
user_id=user_id,
36-
product_name=product_name,
53+
expiration=expiration,
3754
api_key=api_key,
3855
api_secret=api_secret,
39-
expires_at=(sa.func.now() + expiration) if expiration else None,
4056
)
41-
.returning(api_keys.c.id)
42-
)
43-
44-
result = await conn.stream(stmt)
45-
row = await result.first()
46-
47-
return ApiKey(
48-
id=f"{row.id}", # NOTE See: https://github.com/ITISFoundation/osparc-simcore/issues/6919
49-
display_name=display_name,
50-
expiration=expiration,
51-
api_key=api_key,
52-
api_secret=api_secret,
53-
)
57+
except UniqueViolationError as exc:
58+
raise ApiKeyDuplicatedDisplayNameError(display_name=display_name) from exc
5459

5560

5661
async def get_or_create_api_key(

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ class ApiKeysValueError(WebServerBaseError, ValueError):
55
...
66

77

8+
class ApiKeyDuplicatedDisplayNameError(ApiKeysValueError):
9+
msg_template = "API Key with display name '{display_name}' already exists. {reason}"
10+
11+
812
class ApiKeyNotFoundError(ApiKeysValueError):
913
msg_template = "API Key with ID '{api_key_id}' not found. {reason}"

tests/public-api/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def registered_user(
163163

164164
yield user
165165

166-
resp = client.request("DELETE", f"/auth/api-keys{data['id']}")
166+
resp = client.delete(f"/auth/api-keys/{data['id']}")
167167

168168

169169
@pytest.fixture(scope="module")
@@ -281,7 +281,7 @@ def api_client(
281281
def as_dict(obj: object):
282282
return {
283283
attr: getattr(obj, attr)
284-
for attr in obj.__dict__.keys()
284+
for attr in obj.__dict__
285285
if not attr.startswith("_")
286286
}
287287

0 commit comments

Comments
 (0)