Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
giancarloromeo committed Dec 9, 2024
1 parent 5dfe8e3 commit de1c021
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
exception_handling_decorator,
to_exceptions_handlers_map,
)
from .errors import ApiKeyNotFoundError
from .errors import ApiKeyDuplicatedDisplayNameError, ApiKeyNotFoundError

_TO_HTTP_ERROR_MAP: ExceptionToHttpErrorMap = {
ApiKeyDuplicatedDisplayNameError: HttpErrorInfo(
status.HTTP_409_CONFLICT,
"API key display name duplicated",
),
ApiKeyNotFoundError: HttpErrorInfo(
status.HTTP_404_NOT_FOUND,
"API key was not found",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import sqlalchemy as sa
from aiohttp import web
from asyncpg.exceptions import UniqueViolationError
from models_library.products import ProductName
from models_library.users import UserID
from simcore_postgres_database.models.api_keys import api_keys
Expand All @@ -12,6 +13,7 @@
from sqlalchemy.ext.asyncio import AsyncConnection

from ..db.plugin import get_asyncpg_engine
from .errors import ApiKeyDuplicatedDisplayNameError

_logger = logging.getLogger(__name__)

Expand All @@ -28,29 +30,32 @@ async def create_api_key(
api_secret: str,
) -> ApiKey:
async with transaction_context(get_asyncpg_engine(app), connection) as conn:
stmt = (
api_keys.insert()
.values(
try:
stmt = (
api_keys.insert()
.values(
display_name=display_name,
user_id=user_id,
product_name=product_name,
api_key=api_key,
api_secret=api_secret,
expires_at=(sa.func.now() + expiration) if expiration else None,
)
.returning(api_keys.c.id)
)

result = await conn.stream(stmt)
row = await result.first()

return ApiKey(
id=f"{row.id}", # NOTE See: https://github.com/ITISFoundation/osparc-simcore/issues/6919
display_name=display_name,
user_id=user_id,
product_name=product_name,
expiration=expiration,
api_key=api_key,
api_secret=api_secret,
expires_at=(sa.func.now() + expiration) if expiration else None,
)
.returning(api_keys.c.id)
)

result = await conn.stream(stmt)
row = await result.first()

return ApiKey(
id=f"{row.id}", # NOTE See: https://github.com/ITISFoundation/osparc-simcore/issues/6919
display_name=display_name,
expiration=expiration,
api_key=api_key,
api_secret=api_secret,
)
except UniqueViolationError as exc:
raise ApiKeyDuplicatedDisplayNameError(display_name=display_name) from exc


async def get_or_create_api_key(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ class ApiKeysValueError(WebServerBaseError, ValueError):
...


class ApiKeyDuplicatedDisplayNameError(ApiKeysValueError):
msg_template = "API Key with display name '{display_name}' already exists. {reason}"


class ApiKeyNotFoundError(ApiKeysValueError):
msg_template = "API Key with ID '{api_key_id}' not found. {reason}"
4 changes: 2 additions & 2 deletions tests/public-api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def registered_user(

yield user

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


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -281,7 +281,7 @@ def api_client(
def as_dict(obj: object):
return {
attr: getattr(obj, attr)
for attr in obj.__dict__.keys()
for attr in obj.__dict__
if not attr.startswith("_")
}

Expand Down

0 comments on commit de1c021

Please sign in to comment.