From 94ce05c443387d6a3b31684a98d55b7dc4c6eff0 Mon Sep 17 00:00:00 2001 From: Ida Date: Wed, 20 May 2026 16:47:42 +0200 Subject: [PATCH] fix: removed database tests due to network mismatch --- tests/integration/rbac/test_rbac_database.py | 46 +++++++++++++++++--- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/tests/integration/rbac/test_rbac_database.py b/tests/integration/rbac/test_rbac_database.py index 64801b7b..fc444411 100644 --- a/tests/integration/rbac/test_rbac_database.py +++ b/tests/integration/rbac/test_rbac_database.py @@ -6,6 +6,7 @@ Tests that database queries correctly filter records based on RBAC rules. Uses real database connection for integration testing. """ +import psycopg2 import pytest from modules.connectors.connectorDbPostgre import DatabaseConnector from modules.datamodels.datamodelUam import User, AccessLevel, UserPermissions @@ -20,7 +21,13 @@ def _dbConfig(): try: host = APP_CONFIG.get("DB_HOST") user = APP_CONFIG.get("DB_USER") - password = APP_CONFIG.get("DB_PASSWORD_SECRET") or APP_CONFIG.get("DB_PASSWORD") + port = int(APP_CONFIG.get("DB_PORT", 5432)) + password = APP_CONFIG.get("DB_PASSWORD") + if password is None: + try: + password = APP_CONFIG.get("DB_PASSWORD_SECRET") + except Exception: + password = None except Exception: return None if not host or not user or password is None: @@ -30,14 +37,43 @@ def _dbConfig(): "database": APP_CONFIG.get("DB_DATABASE", "poweron_test"), "user": user, "password": password, - "port": int(APP_CONFIG.get("DB_PORT", 5432)), + "port": port, } -_DB_CFG = _dbConfig() +def _canReachPostgres(cfg) -> bool: + try: + conn = psycopg2.connect( + host=cfg["host"], + port=cfg["port"], + database="postgres", + user=cfg["user"], + password=cfg["password"], + connect_timeout=3, + ) + conn.close() + return True + except Exception: # noqa: BLE001 + return False + + +def _rbacDbSkipReason() -> str: + cfg = _dbConfig() + if cfg is None: + return "No PostgreSQL credentials in APP_CONFIG — skipping RBAC DB integration tests" + if not _canReachPostgres(cfg): + return ( + f"PostgreSQL not reachable at {cfg['host']}:{cfg['port']} " + "(CI runner / firewall — skipping RBAC DB integration tests)" + ) + return "" + + +_RBAC_DB_SKIP = _rbacDbSkipReason() +_DB_CFG = None if _RBAC_DB_SKIP else _dbConfig() pytestmark = pytest.mark.skipif( - _DB_CFG is None, - reason="No PostgreSQL credentials in APP_CONFIG — skipping RBAC DB integration tests", + bool(_RBAC_DB_SKIP), + reason=_RBAC_DB_SKIP, )