From 0736c07d8e418670ca6c4fa080e2c95ee412b06c Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sat, 7 Nov 2026 07:52:23 -0800 Subject: [PATCH] Fix: use filtered documents for statistics ASN max (#14710) --- src/documents/tests/test_api_documents.py | 44 +++++++++++++++++++++++ src/documents/views.py | 2 +- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/documents/tests/test_api_documents.py b/src/documents/tests/test_api_documents.py index 07616f2d98..9599167e24 100644 --- a/src/documents/tests/test_api_documents.py +++ b/src/documents/tests/test_api_documents.py @@ -1862,6 +1862,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): checksum="A", mime_type="application/pdf", content="abc", + archive_serial_number=42, ) Document.objects.create( title="none2", @@ -1905,6 +1906,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): self.assertEqual(response.data["correspondent_count"], 2) self.assertEqual(response.data["document_type_count"], 1) self.assertEqual(response.data["storage_path_count"], 2) + self.assertEqual(response.data["current_asn"], 42) def test_statistics_excludes_document_versions(self) -> None: root = Document.objects.create( @@ -1912,6 +1914,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): checksum="A", mime_type="application/pdf", content="root", + archive_serial_number=42, ) version = Document.objects.create( title="version", @@ -1920,6 +1923,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): content="version", root_document=root, version_index=1, + archive_serial_number=99, ) tag_inbox = Tag.objects.create(name="t1", is_inbox_tag=True) version.tags.add(tag_inbox) @@ -1930,6 +1934,7 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): self.assertEqual(response.data["documents_total"], 1) self.assertEqual(response.data["documents_inbox"], 0) self.assertEqual(response.data["character_count"], 4) + self.assertEqual(response.data["current_asn"], 42) self.assertEqual( response.data["document_file_type_counts"][0]["mime_type_count"], 1, @@ -1943,6 +1948,45 @@ class TestDocumentApi(DirectoriesMixin, ConsumeTaskMixin, APITestCase): self.assertEqual(response.data["documents_inbox"], None) self.assertEqual(response.data["inbox_tags"], None) + def test_statistics_current_asn_respects_document_permissions(self) -> None: + viewer = UserFactory() + grant_global(viewer, "view_document") + DocumentFactory(owner=viewer, archive_serial_number=12) + shared = DocumentFactory(owner=self.user, archive_serial_number=23) + grant_object(viewer, shared, "view_document") + DocumentFactory(owner=self.user, archive_serial_number=99) + + self.client.force_authenticate(user=viewer) + response = self.client.get("/api/statistics/") + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data["current_asn"], 23) + + def test_statistics_current_asn_without_visible_documents(self) -> None: + DocumentFactory(owner=self.user, archive_serial_number=99) + + for permissions in ((), ("view_document",)): + viewer = UserFactory() + grant_global(viewer, *permissions) + self.client.force_authenticate(user=viewer) + + with self.subTest(permissions=permissions): + response = self.client.get("/api/statistics/") + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data["current_asn"], 0) + + def test_statistics_current_asn_with_global_statistics_permission(self) -> None: + viewer = UserFactory() + grant_global(viewer, "view_global_statistics") + DocumentFactory(owner=self.user, archive_serial_number=99) + + self.client.force_authenticate(user=viewer) + response = self.client.get("/api/statistics/") + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data["current_asn"], 99) + def test_statistics_multiple_users(self) -> None: """ GIVEN: diff --git a/src/documents/views.py b/src/documents/views.py index 1548753400..1c1e90065f 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -4078,7 +4078,7 @@ class StatisticsView(GenericAPIView[Any]): for row in mime_type_stats ] - current_asn = Document.objects.aggregate( + current_asn = documents.aggregate( Max("archive_serial_number", default=0), ).get( "archive_serial_number__max",