From c60081a5ac4688f61dbb88bf29da617f124d37b4 Mon Sep 17 00:00:00 2001 From: Johan LEROY Date: Tue, 15 Sep 2026 14:58:15 +0200 Subject: [PATCH] test(backend): isole les tests d'audit par cible unique MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ils interrogeaient `audit_log` sans filtre, ce qui supposait une table vide. Le parcours d'authentification y écrit désormais de vraies lignes, et comme la table est en ajout seul, elles ne s'effacent pas entre deux exécutions. Chaque test filtre maintenant sur son propre `target_id`. --- .../tests/repositories/test_audit_log.py | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/apps/backend/tests/repositories/test_audit_log.py b/apps/backend/tests/repositories/test_audit_log.py index 1c6fc64..beacc8e 100644 --- a/apps/backend/tests/repositories/test_audit_log.py +++ b/apps/backend/tests/repositories/test_audit_log.py @@ -56,12 +56,17 @@ async def test_the_database_refuses_to_mutate_the_audit_log( async def test_record_keeps_a_snapshot_of_the_actor(session: AsyncSession) -> None: depot = AuditLogRepository(session) + cible = uuid.uuid4().hex - await depot.record(action=AuditAction.COMPTE_DESACTIVE, actor=ACTEUR) + await depot.record(action=AuditAction.COMPTE_DESACTIVE, actor=ACTEUR, target_id=cible) await session.flush() ligne = ( await session.execute( - text("select actor_id, actor_email, actor_role, outcome from audit_log") + text( + "select actor_id, actor_email, actor_role, outcome from audit_log " + "where target_id = :c" + ), + {"c": cible}, ) ).one() await session.rollback() @@ -77,9 +82,15 @@ async def test_record_accepts_a_label_when_there_is_no_authenticated_actor( ) -> None: depot = AuditLogRepository(session) - await depot.record(action=AuditAction.ADMIN_AMORCE, actor_label="cli") + cible = uuid.uuid4().hex + await depot.record(action=AuditAction.ADMIN_AMORCE, actor_label="cli", target_id=cible) await session.flush() - ligne = (await session.execute(text("select actor_id, actor_email from audit_log"))).one() + ligne = ( + await session.execute( + text("select actor_id, actor_email from audit_log where target_id = :c"), + {"c": cible}, + ) + ).one() await session.rollback() assert ligne.actor_id is None @@ -91,13 +102,19 @@ async def test_record_drops_the_detail_keys_outside_the_allow_list( ) -> None: depot = AuditLogRepository(session) + cible = uuid.uuid4().hex await depot.record( action=AuditAction.COMPTE_ROLE_CHANGE, actor=ACTEUR, + target_id=cible, detail={"role_avant": "lecteur", "mot_de_passe": "ne-doit-pas-passer"}, ) await session.flush() - detail = (await session.execute(text("select detail from audit_log"))).scalar_one() + detail = ( + await session.execute( + text("select detail from audit_log where target_id = :c"), {"c": cible} + ) + ).scalar_one() await session.rollback() assert detail == {"role_avant": "lecteur"}