Skip to content

Commit 1da293b

Browse files
committed
cleanup
1 parent 4f1e4e7 commit 1da293b

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

services/web/server/src/simcore_service_webserver/security/_authz_policy.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
from models_library.products import ProductName
1414
from models_library.users import UserID
1515
from servicelib.aiohttp.db_asyncpg_engine import get_async_engine
16-
from simcore_postgres_database.aiopg_errors import DatabaseError
16+
from servicelib.logging_errors import create_troubleshotting_log_kwargs
17+
from simcore_postgres_database.aiopg_errors import (
18+
DatabaseError as AiopgDatabaseError, # type: ignore[import-untyped]
19+
)
20+
from sqlalchemy.exc import (
21+
DatabaseError as SQLAlchemyDatabaseError, # type: ignore[import-untyped]
22+
)
1723

1824
from ._authz_access_model import (
1925
AuthContextDict,
@@ -43,8 +49,15 @@
4349
def _handle_exceptions_as_503():
4450
try:
4551
yield
46-
except DatabaseError as err:
47-
_logger.exception("Auth unavailable due to database error")
52+
except (AiopgDatabaseError, SQLAlchemyDatabaseError) as err:
53+
_logger.exception(
54+
**create_troubleshotting_log_kwargs(
55+
"Auth unavailable due to database error",
56+
error=err,
57+
tip="Check database connection",
58+
)
59+
)
60+
4861
raise web.HTTPServiceUnavailable(text=MSG_AUTH_NOT_AVAILABLE) from err
4962

5063

services/web/server/tests/unit/isolated/test_security__authz.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ async def test_check_access_expressions(access_model: RoleBasedAccessModel):
243243
def mock_db(mocker: MockerFixture) -> MagicMock:
244244

245245
mocker.patch(
246-
"simcore_service_webserver.security._authz_policy.get_database_engine",
246+
"simcore_service_webserver.security._authz_policy.get_async_engine",
247247
autospec=True,
248248
return_value="FAKE-ENGINE",
249249
)

services/web/server/tests/unit/isolated/test_security_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ async def basic_db_funs_mocked(client: TestClient, mocker: MockerFixture) -> Non
243243
await clean_auth_policy_cache(client.app)
244244

245245
mocker.patch(
246-
"simcore_service_webserver.security._authz_policy.get_database_engine",
246+
"simcore_service_webserver.security._authz_policy.get_async_engine",
247247
autospec=True,
248248
)
249249

services/web/server/tests/unit/with_dbs/03/test_users_rest_profiles.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
from simcore_service_webserver.users._preferences_service import (
3737
get_frontend_user_preferences_aggregation,
3838
)
39+
from sqlalchemy.exc import OperationalError as SQLAlchemyOperationalError
40+
from sqlalchemy.ext.asyncio import AsyncConnection
3941

4042

4143
@pytest.fixture
@@ -554,6 +556,14 @@ def mock_failing_database_connection(mocker: Mock) -> MagicMock:
554556
conn_execute.side_effect = OperationalError(
555557
"MOCK: server closed the connection unexpectedly"
556558
)
559+
560+
aysncpg_conn_execute = mocker.patch.object(AsyncConnection, "execute")
561+
aysncpg_conn_execute.side_effect = SQLAlchemyOperationalError(
562+
statement="MOCK statement",
563+
params=(),
564+
orig=OperationalError("MOCK: server closed the connection unexpectedly"),
565+
)
566+
557567
return conn_execute
558568

559569

0 commit comments

Comments
 (0)