fix(mcp): isole le reload de src.app dans test_app_wiring (pollution inter-tests avec AG-Grid, #111)

This commit is contained in:
Colin Maudry
2026-07-10 16:27:05 +02:00
parent 34e5065550
commit ea6998ac11
+24 -1
View File
@@ -1,4 +1,5 @@
import importlib import importlib
import sys
def test_mcp_endpoint_guarded_and_csrf_exempt(monkeypatch, tmp_path): def test_mcp_endpoint_guarded_and_csrf_exempt(monkeypatch, tmp_path):
@@ -12,6 +13,19 @@ def test_mcp_endpoint_guarded_and_csrf_exempt(monkeypatch, tmp_path):
auth_db.reset_conn_for_tests() auth_db.reset_conn_for_tests()
# Recharger src.app avec le flag activé ré-exécute la découverte use_pages,
# qui REMPLACE les objets-modules src.pages.* dans sys.modules (Dash les
# ré-exécute via exec_module). Sans restauration, un test ultérieur ayant
# importé une fonction de page (ex. tests/test_grid.py: get_rows_tableau)
# verrait son patch("src.pages.tableau.<x>") cibler un objet-module différent
# de celui d'où provient la fonction importée → pollution inter-tests. On
# snapshot les modules concernés puis on les restaure en fin de test.
snapshot = {
name: mod
for name, mod in sys.modules.items()
if name == "src.app" or name.startswith("src.pages")
}
try:
import src.app as app_module import src.app as app_module
app_module = importlib.reload(app_module) app_module = importlib.reload(app_module)
@@ -22,5 +36,14 @@ def test_mcp_endpoint_guarded_and_csrf_exempt(monkeypatch, tmp_path):
resp = client.post("/_mcp", json={"jsonrpc": "2.0", "method": "ping", "id": 1}) resp = client.post("/_mcp", json={"jsonrpc": "2.0", "method": "ping", "id": 1})
assert resp.status_code == 401 assert resp.status_code == 401
assert resp.headers.get("WWW-Authenticate") == 'Bearer realm="colibre-mcp"' assert resp.headers.get("WWW-Authenticate") == 'Bearer realm="colibre-mcp"'
finally:
# Restaurer les objets-modules d'origine et purger ceux créés par le
# reload, pour ne pas polluer les tests suivants.
for name in [
n for n in sys.modules if n == "src.app" or n.startswith("src.pages")
]:
if name in snapshot:
sys.modules[name] = snapshot[name]
else:
del sys.modules[name]
auth_db.reset_conn_for_tests() auth_db.reset_conn_for_tests()