mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-08-12 00:19:48 +00:00
Feature: document history (audit log UI) (#6388)
This commit is contained in:
@@ -316,6 +316,133 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase):
|
||||
response = self.client.get(f"/api/documents/{doc.pk}/thumb/")
|
||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
|
||||
|
||||
def test_document_history_action(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Document
|
||||
WHEN:
|
||||
- Document is updated
|
||||
THEN:
|
||||
- Audit log contains changes
|
||||
"""
|
||||
doc = Document.objects.create(
|
||||
title="First title",
|
||||
checksum="123",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
self.client.force_login(user=self.user)
|
||||
self.client.patch(
|
||||
f"/api/documents/{doc.pk}/",
|
||||
{"title": "New title"},
|
||||
format="json",
|
||||
)
|
||||
|
||||
response = self.client.get(f"/api/documents/{doc.pk}/history/")
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(len(response.data), 2)
|
||||
self.assertEqual(response.data[0]["actor"]["id"], self.user.id)
|
||||
self.assertEqual(response.data[0]["action"], "update")
|
||||
self.assertEqual(
|
||||
response.data[0]["changes"],
|
||||
{"title": ["First title", "New title"]},
|
||||
)
|
||||
|
||||
def test_document_history_action_w_custom_fields(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Document with custom fields
|
||||
WHEN:
|
||||
- Document is updated
|
||||
THEN:
|
||||
- Audit log contains custom field changes
|
||||
"""
|
||||
doc = Document.objects.create(
|
||||
title="First title",
|
||||
checksum="123",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
custom_field = CustomField.objects.create(
|
||||
name="custom field str",
|
||||
data_type=CustomField.FieldDataType.STRING,
|
||||
)
|
||||
self.client.force_login(user=self.user)
|
||||
self.client.patch(
|
||||
f"/api/documents/{doc.pk}/",
|
||||
data={
|
||||
"custom_fields": [
|
||||
{
|
||||
"field": custom_field.pk,
|
||||
"value": "custom value",
|
||||
},
|
||||
],
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
|
||||
response = self.client.get(f"/api/documents/{doc.pk}/history/")
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data[1]["actor"]["id"], self.user.id)
|
||||
self.assertEqual(response.data[1]["action"], "create")
|
||||
self.assertEqual(
|
||||
response.data[1]["changes"],
|
||||
{
|
||||
"custom_fields": {
|
||||
"type": "custom_field",
|
||||
"field": "custom field str",
|
||||
"value": "custom value",
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
@override_settings(AUDIT_LOG_ENABLED=False)
|
||||
def test_document_history_action_disabled(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Audit log is disabled
|
||||
WHEN:
|
||||
- Document is updated
|
||||
- Audit log is requested
|
||||
THEN:
|
||||
- Audit log returns HTTP 400 Bad Request
|
||||
"""
|
||||
doc = Document.objects.create(
|
||||
title="First title",
|
||||
checksum="123",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
self.client.force_login(user=self.user)
|
||||
self.client.patch(
|
||||
f"/api/documents/{doc.pk}/",
|
||||
{"title": "New title"},
|
||||
format="json",
|
||||
)
|
||||
|
||||
response = self.client.get(f"/api/documents/{doc.pk}/history/")
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_document_history_insufficient_perms(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Audit log is disabled
|
||||
WHEN:
|
||||
- Document is updated
|
||||
- Audit log is requested
|
||||
THEN:
|
||||
- Audit log returns HTTP 400 Bad Request
|
||||
"""
|
||||
user = User.objects.create_user(username="test")
|
||||
user.user_permissions.add(*Permission.objects.filter(codename="view_document"))
|
||||
self.client.force_login(user=user)
|
||||
doc = Document.objects.create(
|
||||
title="First title",
|
||||
checksum="123",
|
||||
mime_type="application/pdf",
|
||||
owner=user,
|
||||
)
|
||||
|
||||
response = self.client.get(f"/api/documents/{doc.pk}/history/")
|
||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||
|
||||
def test_document_filters(self):
|
||||
doc1 = Document.objects.create(
|
||||
title="none1",
|
||||
|
@@ -39,6 +39,7 @@ class TestApiUiSettings(DirectoriesMixin, APITestCase):
|
||||
{
|
||||
"app_title": None,
|
||||
"app_logo": None,
|
||||
"auditlog_enabled": True,
|
||||
"update_checking": {
|
||||
"backend_setting": "default",
|
||||
},
|
||||
|
@@ -4,6 +4,7 @@ import tempfile
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from auditlog.context import disable_auditlog
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import DatabaseError
|
||||
@@ -143,7 +144,9 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
||||
# Set a correspondent and save the document
|
||||
document.correspondent = Correspondent.objects.get_or_create(name="test")[0]
|
||||
|
||||
with mock.patch("documents.signals.handlers.Document.objects.filter") as m:
|
||||
with mock.patch(
|
||||
"documents.signals.handlers.Document.objects.filter",
|
||||
) as m, disable_auditlog():
|
||||
m.side_effect = DatabaseError()
|
||||
document.save()
|
||||
|
||||
@@ -557,20 +560,21 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
||||
@override_settings(FILENAME_FORMAT="{title}")
|
||||
@mock.patch("documents.signals.handlers.Document.objects.filter")
|
||||
def test_no_update_without_change(self, m):
|
||||
doc = Document.objects.create(
|
||||
title="document",
|
||||
filename="document.pdf",
|
||||
archive_filename="document.pdf",
|
||||
checksum="A",
|
||||
archive_checksum="B",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
Path(doc.source_path).touch()
|
||||
Path(doc.archive_path).touch()
|
||||
with disable_auditlog():
|
||||
doc = Document.objects.create(
|
||||
title="document",
|
||||
filename="document.pdf",
|
||||
archive_filename="document.pdf",
|
||||
checksum="A",
|
||||
archive_checksum="B",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
Path(doc.source_path).touch()
|
||||
Path(doc.archive_path).touch()
|
||||
|
||||
doc.save()
|
||||
doc.save()
|
||||
|
||||
m.assert_not_called()
|
||||
m.assert_not_called()
|
||||
|
||||
|
||||
class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
||||
|
Reference in New Issue
Block a user