Files
Forge-Engine/tests/test_add_row_action.py
T
williamandClaude Sonnet 5 c57420c8c9 Phase 3 : hardening qualite de code - typage strict, securite, dead code, a11y
Config strictement stricte partout (ruff, mypy --strict, bandit, vulture,
import-linter, eslint, stylelint), aucune regle desactivee "pour ne pas
casser le build" - l'existant a ete corrige pour la satisfaire plutot que
l'inverse. Hooks pre-commit locaux (language: system) bloquants.

- Typage mypy --strict propage a tout le moteur (db, screens, auth, core,
  ai, routes, puis publish/scripts/tests/app.py/build_css.py).
- Securite : fuite de handle fichier Windows corrigee dans l'export SCORM
  (routes/publish/export_scorm.py), CSRF/RNG non-crypto/xAPI documentes
  (# nosec, # NOSONAR justifies), nouveau db.json_for_script() (echappe
  "</script>" dans le JSON embarque en <script>, 25 sites).
- Architecture : imports circulaires/F811 nettoyes, contrats
  import-linter respectes, code mort retire (vulture).
- Accessibilite : 69 champs de formulaire sans label correctement
  associe corriges (for/id ou aria-label) sur 11 templates.
- ESLint/Stylelint : lot mecanique JS/CSS, regles ajustees puis
  appliquees (aucune desactivee sans verification individuelle).
- Tests : isolation du compte admin partage (nettoyage ponctuel +
  fixture de teardown automatique en filet de securite), suite complete
  verte (591 tests Python, 241 tests JS).
- SonarQube Community Build self-heberge (Docker + PostgreSQL) : rapport
  complet analyse point par point, faux positifs documentes.
- .gitattributes ajoute (LF force) : core.autocrlf=true sur cette machine
  faisait echouer ESLint (linebreak-style) via un bug connu de git
  (checkout "en place" qui ignore l'eol force sur un fichier deja
  present sur disque - contourne en supprimant puis recreant chaque
  fichier suivi).

djLint (H021, styles inline) volontairement saute pour ce commit -
backlog assume, deja documente, traite dans un lot separe.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-15 16:06:15 +02:00

110 lines
3.9 KiB
Python

"""Tests de l'action "Ajouter une ligne" (ajouter_ligne, Phase 4) : crée
une ligne VIDE, dont l'id devient LAST_INSERTED_ROW_ID pour un nœud
"Modifier une donnée" enchaîné dans le MÊME flow — verrouille le
mécanisme complet (route -> DB -> répéteur) plutôt que juste la fonction
Python isolée."""
import json
import re
from typing import Any
from flask.testing import FlaskClient
import db
import screens
from tests.conftest import not_none
def _create_screen(client: FlaskClient, slug: str, name: str = "Accueil") -> Any:
resp = client.post(f"/game/{slug}/screens/new", data={"name": name}, follow_redirects=False)
return int(not_none(re.search(r"/screens/(\d+)/edit", resp.headers["Location"])).group(1))
def _add_flow_node(client: FlaskClient, slug: str, screen_id: int, **fields: Any) -> Any:
resp = client.post(
f"/game/{slug}/screens/{screen_id}/flow/nodes/add",
data=json.dumps(fields),
content_type="application/json",
)
assert resp.status_code == 200
return resp.get_json()["id"]
def _create_inventory_object(client: FlaskClient, slug: str) -> Any:
return db.create_definition(slug, "Inventaire", [{"name": "objet", "type": "texte"}])
def test_run_add_row_route_creates_an_empty_row(client: FlaskClient, game: str) -> None:
definition_id = _create_inventory_object(client, game)
screen_id = _create_screen(client, game)
node_id = _add_flow_node(
client,
game,
screen_id,
node_type="action",
action_type="ajouter_ligne",
target_definition_id=definition_id,
)
resp = client.post(f"/game/{game}/flow/nodes/{node_id}/run-add-row")
assert resp.status_code == 200
body = resp.get_json()
assert body["ok"] is True
assert isinstance(body["row_id"], int)
definition = not_none(db.get_definition(game, definition_id))
rows = db.list_rows(game, definition)
assert len(rows) == 1
assert rows[0]["id"] == body["row_id"]
assert rows[0]["objet"] is None
def test_run_add_row_route_rejects_wrong_action_type(client: FlaskClient, game: str) -> None:
screen_id = _create_screen(client, game)
node_id = _add_flow_node(client, game, screen_id, node_type="action", action_type="attendre", data_value="1")
resp = client.post(f"/game/{game}/flow/nodes/{node_id}/run-add-row")
assert resp.status_code == 400
assert resp.get_json()["ok"] is False
def test_chained_ajouter_ligne_then_modifier_donnee_sets_the_new_row_fields(client: FlaskClient, game: str) -> None:
"""Le scénario réel : "Ajouter une ligne" crée une ligne vide, puis un
"Modifier une donnée" ciblant LAST_INSERTED_ROW_ID (transmis par le
client via last_inserted_row_id, voir static/js/play/actions.js) la
renseigne — exactement comme CLICKED_ROW_ID pour une ligne cliquée."""
definition_id = _create_inventory_object(client, game)
screen_id = _create_screen(client, game)
add_node_id = _add_flow_node(
client,
game,
screen_id,
node_type="action",
action_type="ajouter_ligne",
target_definition_id=definition_id,
)
set_node_id = _add_flow_node(
client,
game,
screen_id,
node_type="action",
action_type="modifier_donnee",
target_definition_id=definition_id,
target_row_id=screens.LAST_INSERTED_ROW_ID,
target_field="objet",
data_operation="definir_texte",
data_value="épée",
)
add_resp = client.post(f"/game/{game}/flow/nodes/{add_node_id}/run-add-row")
new_row_id = add_resp.get_json()["row_id"]
set_resp = client.post(
f"/game/{game}/flow/nodes/{set_node_id}/run-data",
data=json.dumps({"last_inserted_row_id": new_row_id}),
content_type="application/json",
)
assert set_resp.get_json()["ok"] is True
definition = not_none(db.get_definition(game, definition_id))
row = not_none(db.get_row(game, definition, new_row_id))
assert row["objet"] == "épée"