diff --git a/src-ui/src/app/components/admin/trash/trash.component.ts b/src-ui/src/app/components/admin/trash/trash.component.ts index 1f2dd69b4..0bf5dac9e 100644 --- a/src-ui/src/app/components/admin/trash/trash.component.ts +++ b/src-ui/src/app/components/admin/trash/trash.component.ts @@ -66,7 +66,7 @@ export class TrashComponent implements OnDestroy { }) } - emptyTrash(documents: Set = null) { + emptyTrash(documents?: Set) { let modal = this.modalService.open(ConfirmDialogComponent, { backdrop: 'static', }) diff --git a/src-ui/src/app/services/trash.service.spec.ts b/src-ui/src/app/services/trash.service.spec.ts index c880f9361..7457960e5 100644 --- a/src-ui/src/app/services/trash.service.spec.ts +++ b/src-ui/src/app/services/trash.service.spec.ts @@ -7,7 +7,7 @@ import { } from '@angular/common/http/testing' import { environment } from 'src/environments/environment' -describe('TrashServiceService', () => { +describe('TrashService', () => { let service: TrashService let httpTestingController: HttpTestingController @@ -33,7 +33,16 @@ describe('TrashServiceService', () => { `${environment.apiBaseUrl}trash/` ) expect(req.request.method).toEqual('POST') - expect(req.request.body).toEqual({ action: 'empty', documents: [] }) + expect(req.request.body).toEqual({ action: 'empty' }) + + service.emptyTrash([1, 2, 3]).subscribe() + const req2 = httpTestingController.expectOne( + `${environment.apiBaseUrl}trash/` + ) + expect(req2.request.body).toEqual({ + action: 'empty', + documents: [1, 2, 3], + }) }) it('should call correct endpoint for restoreDocuments', () => { diff --git a/src-ui/src/app/services/trash.service.ts b/src-ui/src/app/services/trash.service.ts index 3b2eccdbd..6a8bbf1f0 100644 --- a/src-ui/src/app/services/trash.service.ts +++ b/src-ui/src/app/services/trash.service.ts @@ -18,11 +18,14 @@ export class TrashService { }) } - public emptyTrash(documents: number[] = []): Observable { - return this.http.post(`${environment.apiBaseUrl}trash/`, { + public emptyTrash(documents?: number[]): Observable { + const data = { action: 'empty', - documents, - }) + } + if (documents?.length) { + data['documents'] = documents + } + return this.http.post(`${environment.apiBaseUrl}trash/`, data) } public restoreDocuments(documents: number[]): Observable { diff --git a/src/documents/tests/test_api_trash.py b/src/documents/tests/test_api_trash.py index a3a335f63..ef1e8b8bc 100644 --- a/src/documents/tests/test_api_trash.py +++ b/src/documents/tests/test_api_trash.py @@ -64,6 +64,39 @@ class TestTrashAPI(APITestCase): self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(Document.global_objects.count(), 0) + def test_trash_api_empty_all(self): + """ + GIVEN: + - Existing documents in trash + WHEN: + - API request to empty trash + THEN: + - Trash is emptied + """ + + document = Document.objects.create( + title="Title", + content="content", + checksum="checksum", + mime_type="application/pdf", + ) + document.delete() + document2 = Document.objects.create( + title="Title2", + content="content2", + checksum="checksum2", + mime_type="application/pdf", + ) + document2.delete() + + self.client.force_login(user=self.user) + resp = self.client.post( + "/api/trash/", + {"action": "empty", "documents": []}, + ) + self.assertEqual(resp.status_code, status.HTTP_200_OK) + self.assertEqual(Document.global_objects.count(), 0) + def test_api_trash_insufficient_permissions(self): """ GIVEN: diff --git a/src/documents/views.py b/src/documents/views.py index 8883a8b65..4a7ca09ce 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -2076,7 +2076,11 @@ class TrashView(ListModelMixin, PassUserMixin): serializer.is_valid(raise_exception=True) doc_ids = serializer.validated_data.get("documents") - docs = Document.global_objects.filter(id__in=doc_ids) + docs = ( + Document.global_objects.filter(id__in=doc_ids) + if doc_ids is not None + else Document.deleted_objects.all() + ) checker = ObjectPermissionChecker(request.user) checker.prefetch_perms(docs) for doc in docs: @@ -2087,5 +2091,7 @@ class TrashView(ListModelMixin, PassUserMixin): for doc in Document.deleted_objects.filter(id__in=doc_ids).all(): doc.restore(strict=False) elif action == "empty": + if doc_ids is None: + doc_ids = [doc.id for doc in docs] empty_trash(doc_ids=doc_ids) return Response({"result": "OK", "doc_ids": doc_ids})