From 1c242b66a0395f62e988134d636155a3945123f8 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:07:11 -0800 Subject: [PATCH] Fix: skip dont use set_actor if audit log disabled --- src/documents/bulk_operations.py | 7 +++-- src/documents/tests/test_bulk_operations.py | 35 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/documents/bulk_operations.py b/src/documents/bulk_operations.py index 57cc55f862..b2095bd766 100644 --- a/src/documents/bulk_operations.py +++ b/src/documents/bulk_operations.py @@ -1,5 +1,6 @@ """Queued document bulk operations.""" +from contextlib import nullcontext from typing import Any from auditlog.context import set_actor @@ -112,6 +113,8 @@ def execute_bulk_operation( if operation == "run_workflow" and not user.has_perm("documents.view_workflow"): raise PermissionDenied("Insufficient permissions to run this workflow") + actor_context = set_actor(user) if settings.AUDIT_LOG_ENABLED else nullcontext() + if operation not in MODIFIED_FIELDS: execution_parameters = parameters.copy() if operation in USER_OPERATIONS: @@ -119,7 +122,7 @@ def execute_bulk_operation( if operation in TRIGGER_SOURCE_OPERATIONS: execution_parameters["trigger_source"] = trigger_source modified_field = LEGACY_MODIFIED_FIELDS.get(operation) - with set_actor(user): + with actor_context: old_values = ( snapshot_field(document_ids, modified_field) if settings.AUDIT_LOG_ENABLED and modified_field @@ -146,7 +149,7 @@ def execute_bulk_operation( modified_field = MODIFIED_FIELDS[operation] # Keep metadata writes and their audit entries together. Follow-up processing # runs after commit, within this same tracked task. - with set_actor(user), transaction.atomic(): + with actor_context, transaction.atomic(): old_values = ( snapshot_field(document_ids, modified_field) if settings.AUDIT_LOG_ENABLED and modified_field diff --git a/src/documents/tests/test_bulk_operations.py b/src/documents/tests/test_bulk_operations.py index 1e5a495c2d..4190abba90 100644 --- a/src/documents/tests/test_bulk_operations.py +++ b/src/documents/tests/test_bulk_operations.py @@ -123,6 +123,41 @@ def test_document_action_runs_with_user_in_worker(bulk_setup): assert result == {"operation": "rotate", "document_count": 1} +@pytest.mark.django_db +def test_delete_task_without_audit_log(bulk_setup, settings): + user, _, doc, _ = bulk_setup + settings.AUDIT_LOG_ENABLED = False + with mock.patch( + "documents.bulk_operations.set_actor", + side_effect=AssertionError("audit context must not be entered"), + ): + result = execute_bulk_operation("delete", [doc.pk], {}, user.pk) + assert result == {"operation": "delete", "document_count": 1} + assert not Document.objects.filter(pk=doc.pk).exists() + + +@pytest.mark.django_db +def test_metadata_task_without_audit_log(bulk_setup, settings): + user, _, doc, correspondent = bulk_setup + settings.AUDIT_LOG_ENABLED = False + with ( + mock.patch( + "documents.bulk_operations.set_actor", + side_effect=AssertionError("audit context must not be entered"), + ), + mock.patch("documents.bulk_operations.bulk_update_documents"), + ): + result = execute_bulk_operation( + "set_correspondent", + [doc.pk], + {"correspondent": correspondent.pk}, + user.pk, + ) + doc.refresh_from_db() + assert doc.correspondent_id == correspondent.pk + assert result == {"operation": "set_correspondent", "document_count": 1} + + @pytest.mark.django_db def test_document_action_queue_contains_only_serializable_parameters(bulk_setup): user, client, doc, _ = bulk_setup