Compare commits

...
Author SHA1 Message Date
shamoon 1c242b66a0 Fix: skip dont use set_actor if audit log disabled 2026-11-17 09:07:11 -08:00
2 changed files with 40 additions and 2 deletions

No files matched your search

+5 -2
View File
@@ -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
@@ -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