auditrum 0.3.2__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {auditrum-0.3.2 → auditrum-0.4.0}/PKG-INFO +1 -1
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/admin.py +6 -4
- auditrum-0.4.0/auditrum/integrations/django/migrations/0002_drop_content_type_id.py +22 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/mixins.py +10 -6
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/models.py +0 -6
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/utils.py +20 -3
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/schema.py +0 -1
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/tracking/templates/audit_trigger.sql +2 -2
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum.egg-info/PKG-INFO +1 -1
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum.egg-info/SOURCES.txt +1 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/pyproject.toml +1 -1
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_utils.py +61 -18
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_mixins.py +64 -1
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_schema.py +11 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_triggers.py +9 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/LICENSE +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/README.md +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/__init__.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/blame.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/cli.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/context.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/executor.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/hardening.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/hash_chain.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/__init__.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/__init__.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/apps.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/audit.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/executor.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/management/__init__.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/management/commands/__init__.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/management/commands/audit_add_partitions.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/management/commands/auditrum_makemigrations.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/middleware.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/migrations/0001_initial.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/migrations/__init__.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/operations.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/runtime.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/settings.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/shell_context.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/templates/audit/object_history.html +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/templatetags/__init__.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/templatetags/audit_tags.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/tracking.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/sqlalchemy/__init__.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/sqlalchemy/core.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/observability/__init__.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/observability/otel.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/observability/prometheus.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/observability/sentry.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/retention.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/revert.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/settings.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/timetravel.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/tracking/__init__.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/tracking/_template.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/tracking/manager.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/tracking/spec.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/tracking/templates/__init__.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/tracking/templates/audit_attach_context.sql +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/tracking/templates/audit_current_user_id.sql +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/tracking/templates/audit_reconstruct_row.sql +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/tracking/templates/audit_reconstruct_table.sql +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/triggers.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum/utils.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum.egg-info/dependency_links.txt +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum.egg-info/entry_points.txt +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum.egg-info/requires.txt +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/auditrum.egg-info/top_level.txt +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/setup.cfg +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_blame.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_cli.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_context.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_audit.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_executor.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_init.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_middleware.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_models.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_operations.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_partitions_command.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_runtime.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_settings.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_shell_context.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_templatetags.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_django_tracking_decorator.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_executor.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_hardening.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_hash_chain.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_historical_row.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_observability.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_properties.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_retention.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_revert.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_sqlalchemy_integration.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_timetravel.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_tracking_manager.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_tracking_spec.py +0 -0
- {auditrum-0.3.2 → auditrum-0.4.0}/tests/test_utils.py +0 -0
|
@@ -2,6 +2,7 @@ from django.contrib import admin
|
|
|
2
2
|
from django.utils.html import format_html
|
|
3
3
|
|
|
4
4
|
from auditrum.integrations.django.models import AuditContext, AuditLog
|
|
5
|
+
from auditrum.integrations.django.utils import model_for_table
|
|
5
6
|
|
|
6
7
|
__all__ = [
|
|
7
8
|
"AuditContextAdmin",
|
|
@@ -39,11 +40,12 @@ class AuditLogAdmin(admin.ModelAdmin):
|
|
|
39
40
|
|
|
40
41
|
@admin.display(description="Linked Object")
|
|
41
42
|
def linked_object(self, obj):
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
except Exception:
|
|
43
|
+
model_class = model_for_table(obj.table_name)
|
|
44
|
+
if model_class is None or not obj.object_id:
|
|
45
45
|
return "-"
|
|
46
|
-
|
|
46
|
+
try:
|
|
47
|
+
target = model_class._default_manager.get(pk=obj.object_id)
|
|
48
|
+
except (model_class.DoesNotExist, ValueError, TypeError):
|
|
47
49
|
return "-"
|
|
48
50
|
get_url = getattr(target, "get_absolute_url", None)
|
|
49
51
|
if callable(get_url):
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from django.db import migrations
|
|
2
|
+
|
|
3
|
+
from auditrum.integrations.django.settings import audit_settings
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Migration(migrations.Migration):
|
|
7
|
+
dependencies = [
|
|
8
|
+
("auditrum_django", "0001_initial"),
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
operations = [
|
|
12
|
+
migrations.RunSQL(
|
|
13
|
+
sql=(
|
|
14
|
+
f"ALTER TABLE {audit_settings.table_name} "
|
|
15
|
+
"DROP COLUMN IF EXISTS content_type_id;"
|
|
16
|
+
),
|
|
17
|
+
reverse_sql=(
|
|
18
|
+
f"ALTER TABLE {audit_settings.table_name} "
|
|
19
|
+
"ADD COLUMN IF NOT EXISTS content_type_id integer;"
|
|
20
|
+
),
|
|
21
|
+
),
|
|
22
|
+
]
|
|
@@ -2,7 +2,7 @@ from collections.abc import Iterator
|
|
|
2
2
|
from datetime import datetime
|
|
3
3
|
from typing import TYPE_CHECKING
|
|
4
4
|
|
|
5
|
-
from django.contrib.
|
|
5
|
+
from django.contrib.admin.utils import unquote
|
|
6
6
|
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
|
|
7
7
|
from django.db import connection as _django_connection
|
|
8
8
|
from django.shortcuts import render
|
|
@@ -143,11 +143,15 @@ class AuditHistoryMixin(_ModelAdminBase):
|
|
|
143
143
|
return custom_urls + urls
|
|
144
144
|
|
|
145
145
|
def object_history_view(self, request, object_id):
|
|
146
|
-
obj = self.get_object(request, object_id)
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
146
|
+
obj = self.get_object(request, unquote(object_id))
|
|
147
|
+
if obj is None:
|
|
148
|
+
# django-stubs doesn't expose the underscore-prefixed helper
|
|
149
|
+
# even though ModelAdmin ships it — same pattern Django's own
|
|
150
|
+
# admin.options.ModelAdmin.history_view uses.
|
|
151
|
+
return self._get_obj_does_not_exist_redirect( # ty: ignore[unresolved-attribute]
|
|
152
|
+
request, self.model._meta, object_id
|
|
153
|
+
)
|
|
154
|
+
logs = AuditLog.objects.for_object(obj).order_by("-changed_at")
|
|
151
155
|
|
|
152
156
|
paginator = Paginator(logs, 20)
|
|
153
157
|
page_number = request.GET.get("page", 1)
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import uuid
|
|
2
2
|
from typing import TYPE_CHECKING
|
|
3
3
|
|
|
4
|
-
from django.contrib.contenttypes.fields import GenericForeignKey
|
|
5
|
-
from django.contrib.contenttypes.models import ContentType
|
|
6
4
|
from django.db import models
|
|
7
5
|
|
|
8
6
|
from auditrum.integrations.django.settings import audit_settings
|
|
@@ -102,11 +100,7 @@ class AuditContext(models.Model):
|
|
|
102
100
|
class AuditLog(models.Model):
|
|
103
101
|
operation = models.CharField(max_length=16)
|
|
104
102
|
changed_at = models.DateTimeField(auto_now_add=True)
|
|
105
|
-
content_type = models.ForeignKey(
|
|
106
|
-
ContentType, null=True, blank=True, on_delete=models.SET_NULL
|
|
107
|
-
)
|
|
108
103
|
object_id = models.CharField(max_length=255)
|
|
109
|
-
content_object = GenericForeignKey("content_type", "object_id")
|
|
110
104
|
table_name = models.CharField(max_length=255)
|
|
111
105
|
user_id = models.IntegerField(null=True, blank=True)
|
|
112
106
|
old_data = models.JSONField(null=True, blank=True)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from datetime import datetime
|
|
2
2
|
from typing import Any, Protocol
|
|
3
3
|
|
|
4
|
+
from django.apps import apps
|
|
4
5
|
from django.contrib.admin.templatetags.admin_urls import admin_urlname
|
|
5
6
|
from django.db import models
|
|
6
7
|
from django.shortcuts import resolve_url
|
|
@@ -18,6 +19,7 @@ __all__ = [
|
|
|
18
19
|
"audit_tracked",
|
|
19
20
|
"link",
|
|
20
21
|
"link_to_related_object",
|
|
22
|
+
"model_for_table",
|
|
21
23
|
"resolve_field_value",
|
|
22
24
|
"get_user_display",
|
|
23
25
|
"render_log_changes",
|
|
@@ -25,6 +27,22 @@ __all__ = [
|
|
|
25
27
|
]
|
|
26
28
|
|
|
27
29
|
|
|
30
|
+
def model_for_table(table_name: str) -> type[models.Model] | None:
|
|
31
|
+
"""Resolve a Django model class by its ``_meta.db_table`` name.
|
|
32
|
+
|
|
33
|
+
Returns ``None`` when no installed model matches — the caller is
|
|
34
|
+
expected to fall back to a table-name-only presentation, since audit
|
|
35
|
+
rows can reference tables that live in a different service or were
|
|
36
|
+
dropped after the event was recorded. Scans ``apps.get_models()``
|
|
37
|
+
linearly; the set is small and stable per process, so a cache only
|
|
38
|
+
pays off under pathological schemas.
|
|
39
|
+
"""
|
|
40
|
+
for model in apps.get_models():
|
|
41
|
+
if model._meta.db_table == table_name:
|
|
42
|
+
return model
|
|
43
|
+
return None
|
|
44
|
+
|
|
45
|
+
|
|
28
46
|
def link(href: str, text: str) -> str:
|
|
29
47
|
return format_html('<a href="{}">{}</a>', href, text)
|
|
30
48
|
|
|
@@ -75,11 +93,10 @@ def get_user_display(log):
|
|
|
75
93
|
|
|
76
94
|
|
|
77
95
|
def render_log_changes(log):
|
|
78
|
-
|
|
96
|
+
model_class = model_for_table(log.table_name)
|
|
97
|
+
if model_class is None:
|
|
79
98
|
return "—"
|
|
80
99
|
|
|
81
|
-
model_class = log.content_type.model_class()
|
|
82
|
-
|
|
83
100
|
if log.operation == "INSERT" and log.new_data:
|
|
84
101
|
return format_html(
|
|
85
102
|
"<ul class='space-y-1'>{}</ul>",
|
|
@@ -37,11 +37,11 @@ BEGIN
|
|
|
37
37
|
END IF;
|
|
38
38
|
|
|
39
39
|
INSERT INTO {audit_table} (
|
|
40
|
-
operation, changed_at,
|
|
40
|
+
operation, changed_at, object_id, table_name,
|
|
41
41
|
user_id, old_data, new_data, diff, context_id, meta
|
|
42
42
|
)
|
|
43
43
|
VALUES (
|
|
44
|
-
TG_OP, now(),
|
|
44
|
+
TG_OP, now(),
|
|
45
45
|
CASE
|
|
46
46
|
WHEN TG_OP = 'DELETE' THEN to_jsonb(OLD)->>'id'
|
|
47
47
|
ELSE to_jsonb(NEW)->>'id'
|
|
@@ -41,6 +41,7 @@ auditrum/integrations/django/management/commands/__init__.py
|
|
|
41
41
|
auditrum/integrations/django/management/commands/audit_add_partitions.py
|
|
42
42
|
auditrum/integrations/django/management/commands/auditrum_makemigrations.py
|
|
43
43
|
auditrum/integrations/django/migrations/0001_initial.py
|
|
44
|
+
auditrum/integrations/django/migrations/0002_drop_content_type_id.py
|
|
44
45
|
auditrum/integrations/django/migrations/__init__.py
|
|
45
46
|
auditrum/integrations/django/templates/audit/object_history.html
|
|
46
47
|
auditrum/integrations/django/templatetags/__init__.py
|
|
@@ -55,6 +55,7 @@ from auditrum.integrations.django.utils import ( # noqa: E402
|
|
|
55
55
|
get_user_display,
|
|
56
56
|
link,
|
|
57
57
|
link_to_related_object,
|
|
58
|
+
model_for_table,
|
|
58
59
|
render_log_changes,
|
|
59
60
|
resolve_field_value,
|
|
60
61
|
set_var,
|
|
@@ -230,8 +231,7 @@ class TestGetUserDisplay:
|
|
|
230
231
|
class TestRenderLogChanges:
|
|
231
232
|
def _log(self, **overrides):
|
|
232
233
|
log = MagicMock()
|
|
233
|
-
log.
|
|
234
|
-
log.content_type.model_class.return_value = MagicMock()
|
|
234
|
+
log.table_name = "orders"
|
|
235
235
|
log.operation = "UPDATE"
|
|
236
236
|
log.old_data = {"name": "old"}
|
|
237
237
|
log.new_data = {"name": "new"}
|
|
@@ -239,16 +239,27 @@ class TestRenderLogChanges:
|
|
|
239
239
|
setattr(log, k, v)
|
|
240
240
|
return log
|
|
241
241
|
|
|
242
|
-
def
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
242
|
+
def test_unknown_table_returns_em_dash(self):
|
|
243
|
+
# model_for_table returns None for tables that don't map to any
|
|
244
|
+
# installed model (cross-service audit rows, dropped models).
|
|
245
|
+
log = self._log()
|
|
246
|
+
with patch(
|
|
247
|
+
"auditrum.integrations.django.utils.model_for_table",
|
|
248
|
+
return_value=None,
|
|
249
|
+
):
|
|
250
|
+
assert render_log_changes(log) == "—"
|
|
246
251
|
|
|
247
252
|
def test_insert_renders_field_list(self):
|
|
248
253
|
log = self._log(operation="INSERT", new_data={"name": "x"}, old_data=None)
|
|
249
|
-
with
|
|
250
|
-
|
|
251
|
-
|
|
254
|
+
with (
|
|
255
|
+
patch(
|
|
256
|
+
"auditrum.integrations.django.utils.model_for_table",
|
|
257
|
+
return_value=MagicMock(),
|
|
258
|
+
),
|
|
259
|
+
patch(
|
|
260
|
+
"auditrum.integrations.django.utils.resolve_field_value",
|
|
261
|
+
return_value=("Name", "x"),
|
|
262
|
+
),
|
|
252
263
|
):
|
|
253
264
|
out = render_log_changes(log)
|
|
254
265
|
assert "Name" in out
|
|
@@ -256,9 +267,15 @@ class TestRenderLogChanges:
|
|
|
256
267
|
|
|
257
268
|
def test_delete_renders_strikethrough_list(self):
|
|
258
269
|
log = self._log(operation="DELETE", old_data={"name": "gone"}, new_data=None)
|
|
259
|
-
with
|
|
260
|
-
|
|
261
|
-
|
|
270
|
+
with (
|
|
271
|
+
patch(
|
|
272
|
+
"auditrum.integrations.django.utils.model_for_table",
|
|
273
|
+
return_value=MagicMock(),
|
|
274
|
+
),
|
|
275
|
+
patch(
|
|
276
|
+
"auditrum.integrations.django.utils.resolve_field_value",
|
|
277
|
+
return_value=("Name", "gone"),
|
|
278
|
+
),
|
|
262
279
|
):
|
|
263
280
|
out = render_log_changes(log)
|
|
264
281
|
assert "line-through" in out
|
|
@@ -266,9 +283,15 @@ class TestRenderLogChanges:
|
|
|
266
283
|
|
|
267
284
|
def test_update_renders_diffs(self):
|
|
268
285
|
log = self._log()
|
|
269
|
-
with
|
|
270
|
-
|
|
271
|
-
|
|
286
|
+
with (
|
|
287
|
+
patch(
|
|
288
|
+
"auditrum.integrations.django.utils.model_for_table",
|
|
289
|
+
return_value=MagicMock(),
|
|
290
|
+
),
|
|
291
|
+
patch(
|
|
292
|
+
"auditrum.integrations.django.utils.resolve_field_value",
|
|
293
|
+
side_effect=lambda mc, k, v: (k.title(), str(v) if v is not None else "—"),
|
|
294
|
+
),
|
|
272
295
|
):
|
|
273
296
|
out = render_log_changes(log)
|
|
274
297
|
assert "old" in out
|
|
@@ -277,9 +300,29 @@ class TestRenderLogChanges:
|
|
|
277
300
|
|
|
278
301
|
def test_update_with_no_changes_returns_placeholder(self):
|
|
279
302
|
log = self._log(old_data={"x": 1}, new_data={"x": 1})
|
|
280
|
-
with
|
|
281
|
-
|
|
282
|
-
|
|
303
|
+
with (
|
|
304
|
+
patch(
|
|
305
|
+
"auditrum.integrations.django.utils.model_for_table",
|
|
306
|
+
return_value=MagicMock(),
|
|
307
|
+
),
|
|
308
|
+
patch(
|
|
309
|
+
"auditrum.integrations.django.utils.resolve_field_value",
|
|
310
|
+
return_value=("X", "1"),
|
|
311
|
+
),
|
|
283
312
|
):
|
|
284
313
|
out = render_log_changes(log)
|
|
285
314
|
assert "No changes" in out
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
class TestModelForTable:
|
|
318
|
+
def test_returns_matching_model(self):
|
|
319
|
+
# ContentType is an installed model with a well-known db_table;
|
|
320
|
+
# we use it as a real lookup target so the test doesn't depend on
|
|
321
|
+
# any project-local model being defined.
|
|
322
|
+
from django.contrib.contenttypes.models import ContentType
|
|
323
|
+
|
|
324
|
+
resolved = model_for_table(ContentType._meta.db_table)
|
|
325
|
+
assert resolved is ContentType
|
|
326
|
+
|
|
327
|
+
def test_returns_none_for_unknown_table(self):
|
|
328
|
+
assert model_for_table("__no_such_table__") is None
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Unit tests for AuditedModelMixin."""
|
|
2
2
|
|
|
3
3
|
from datetime import UTC, datetime
|
|
4
|
-
from unittest.mock import patch
|
|
4
|
+
from unittest.mock import MagicMock, patch
|
|
5
5
|
|
|
6
6
|
import pytest
|
|
7
7
|
|
|
@@ -164,3 +164,66 @@ class TestAuditStateAsOf:
|
|
|
164
164
|
assert result[0].object_id == "1"
|
|
165
165
|
assert result[0].data == {"id": 1, "name": "a"}
|
|
166
166
|
assert result[0].at == at
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
class TestObjectHistoryView:
|
|
170
|
+
"""Regression for the 0.3.x ``content_type`` NULL bug.
|
|
171
|
+
|
|
172
|
+
The old view filtered ``AuditLog.objects`` on ``content_type`` — a
|
|
173
|
+
column the framework-agnostic trigger never populates — so every
|
|
174
|
+
admin history page rendered an empty table. The fix routes through
|
|
175
|
+
:meth:`AuditLogQuerySet.for_object`, which keys off ``table_name``
|
|
176
|
+
+ ``object_id`` like the rest of the public API.
|
|
177
|
+
"""
|
|
178
|
+
|
|
179
|
+
def _make_mixin(self, fake_obj):
|
|
180
|
+
from auditrum.integrations.django.mixins import AuditHistoryMixin
|
|
181
|
+
|
|
182
|
+
class FakeMeta:
|
|
183
|
+
db_table = "orders"
|
|
184
|
+
app_label = "shop"
|
|
185
|
+
model_name = "order"
|
|
186
|
+
|
|
187
|
+
mixin = AuditHistoryMixin()
|
|
188
|
+
mixin.model = MagicMock()
|
|
189
|
+
mixin.model._meta = FakeMeta
|
|
190
|
+
mixin.get_object = MagicMock(return_value=fake_obj)
|
|
191
|
+
mixin.admin_site = MagicMock()
|
|
192
|
+
mixin.admin_site.each_context.return_value = {}
|
|
193
|
+
return mixin
|
|
194
|
+
|
|
195
|
+
def test_filters_via_for_object(self):
|
|
196
|
+
fake_obj = MagicMock()
|
|
197
|
+
mixin = self._make_mixin(fake_obj)
|
|
198
|
+
|
|
199
|
+
with (
|
|
200
|
+
patch(
|
|
201
|
+
"auditrum.integrations.django.mixins.AuditLog.objects.for_object"
|
|
202
|
+
) as mock_for_object,
|
|
203
|
+
patch(
|
|
204
|
+
"auditrum.integrations.django.mixins.render",
|
|
205
|
+
return_value="rendered",
|
|
206
|
+
),
|
|
207
|
+
):
|
|
208
|
+
# Paginator needs .count() and slicing — let the MagicMock
|
|
209
|
+
# chain through ``.order_by`` and then behave like a list.
|
|
210
|
+
mock_qs = MagicMock()
|
|
211
|
+
mock_qs.order_by.return_value = []
|
|
212
|
+
mock_for_object.return_value = mock_qs
|
|
213
|
+
|
|
214
|
+
request = MagicMock()
|
|
215
|
+
request.GET = {}
|
|
216
|
+
mixin.object_history_view(request, "42")
|
|
217
|
+
|
|
218
|
+
mock_for_object.assert_called_once_with(fake_obj)
|
|
219
|
+
|
|
220
|
+
def test_redirects_when_object_missing(self):
|
|
221
|
+
mixin = self._make_mixin(fake_obj=None)
|
|
222
|
+
mixin._get_obj_does_not_exist_redirect = MagicMock(return_value="redirect")
|
|
223
|
+
|
|
224
|
+
request = MagicMock()
|
|
225
|
+
request.GET = {}
|
|
226
|
+
result = mixin.object_history_view(request, "does-not-exist")
|
|
227
|
+
|
|
228
|
+
assert result == "redirect"
|
|
229
|
+
mixin._get_obj_does_not_exist_redirect.assert_called_once()
|
|
@@ -48,6 +48,17 @@ class TestGenerateAuditlogTableSql:
|
|
|
48
48
|
sql = generate_auditlog_table_sql()
|
|
49
49
|
assert "auditlog_object_id_idx" not in sql
|
|
50
50
|
|
|
51
|
+
def test_no_content_type_id_column(self):
|
|
52
|
+
"""The legacy ``content_type_id`` column is gone as of 0.4.
|
|
53
|
+
|
|
54
|
+
The column was Django-specific dead weight — never populated by
|
|
55
|
+
the framework-agnostic trigger path — and its absence keeps
|
|
56
|
+
:class:`AuditLog` queries routed through ``table_name`` (the
|
|
57
|
+
canonical identity key), not a NULL GenericForeignKey.
|
|
58
|
+
"""
|
|
59
|
+
sql = generate_auditlog_table_sql()
|
|
60
|
+
assert "content_type_id" not in sql
|
|
61
|
+
|
|
51
62
|
def test_custom_table_name(self):
|
|
52
63
|
sql = generate_auditlog_table_sql("my_audit")
|
|
53
64
|
assert "CREATE TABLE IF NOT EXISTS my_audit" in sql
|
|
@@ -112,3 +112,12 @@ class TestGenerateTriggerSql:
|
|
|
112
112
|
generate_trigger_sql("users")
|
|
113
113
|
captured = capsys.readouterr()
|
|
114
114
|
assert captured.out == ""
|
|
115
|
+
|
|
116
|
+
def test_does_not_write_content_type_id(self):
|
|
117
|
+
"""Triggers must not reference the legacy ``content_type_id`` column.
|
|
118
|
+
|
|
119
|
+
The column was dropped in 0.4 — if a trigger still tries to
|
|
120
|
+
INSERT into it, DDL load will fail with "column does not exist".
|
|
121
|
+
"""
|
|
122
|
+
sql = generate_trigger_sql("users")
|
|
123
|
+
assert "content_type_id" not in sql
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/management/commands/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{auditrum-0.3.2 → auditrum-0.4.0}/auditrum/integrations/django/templates/audit/object_history.html
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|