From 4102aa0e48a9214977437db53502c2c66c9582f4 Mon Sep 17 00:00:00 2001 From: SilviaAmAm Date: Mon, 23 Dec 2024 17:09:35 +0100 Subject: [PATCH] :white_check_mark: [#557] Start adding tests --- .../tests/endpoints/test_destructionlist.py | 127 +++++++++++++++++ ...est.test_report_not_found_in_openzaak.yaml | 48 +++++++ ...truction_report_from_internal_results.yaml | 44 ++++++ ...st.test_retrieve_report_from_zaak_url.yaml | 91 ++++++++++++ ..._from_openzaak_when_retrieving_report.yaml | 48 +++++++ .../destruction/tests/vcr/test_endpoints.py | 132 ++++++++++++++++++ 6 files changed, 490 insertions(+) create mode 100644 backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_report_not_found_in_openzaak.yaml create mode 100644 backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_retrieve_destruction_report_from_internal_results.yaml create mode 100644 backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_retrieve_report_from_zaak_url.yaml create mode 100644 backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_unexpected_error_response_from_openzaak_when_retrieving_report.yaml create mode 100644 backend/src/openarchiefbeheer/destruction/tests/vcr/test_endpoints.py diff --git a/backend/src/openarchiefbeheer/destruction/tests/endpoints/test_destructionlist.py b/backend/src/openarchiefbeheer/destruction/tests/endpoints/test_destructionlist.py index ad2df9da..bb052bcd 100644 --- a/backend/src/openarchiefbeheer/destruction/tests/endpoints/test_destructionlist.py +++ b/backend/src/openarchiefbeheer/destruction/tests/endpoints/test_destructionlist.py @@ -1,6 +1,9 @@ +from requests_mock import Mocker from rest_framework import status from rest_framework.reverse import reverse from rest_framework.test import APITestCase +from zgw_consumers.constants import APITypes +from zgw_consumers.test.factories import ServiceFactory from openarchiefbeheer.accounts.tests.factories import UserFactory @@ -55,3 +58,127 @@ def test_record_manager_can_delete_new_list(self): self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertFalse(DestructionList.objects.filter(uuid=list_uuid).exists()) + + def test_cannot_download_report_if_not_record_manager(self): + user = UserFactory.create(post__can_start_destruction=False) + destruction_list = DestructionListFactory.create( + name="A deleted list", + status=ListStatus.deleted, + ) + + self.client.force_authenticate(user=user) + response = self.client.get( + reverse( + "api:destructionlist-download-report", + kwargs={"uuid": destruction_list.uuid}, + ), + ) + + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + + def test_cannot_download_report_if_not_deleted(self): + user = UserFactory.create(post__can_start_destruction=True) + destruction_list = DestructionListFactory.create( + name="A not-deleted list", + status=ListStatus.new, + ) + + self.client.force_authenticate(user=user) + response = self.client.get( + reverse( + "api:destructionlist-download-report", + kwargs={"uuid": destruction_list.uuid}, + ), + ) + + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + + def test_no_destruction_report_url(self): + user = UserFactory.create(post__can_start_destruction=True) + + destruction_list = DestructionListFactory.create( + name="A deleted list", + status=ListStatus.deleted, + ) + + self.client.force_authenticate(user=user) + response = self.client.get( + reverse( + "api:destructionlist-download-report", + kwargs={"uuid": destruction_list.uuid}, + ), + ) + + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) + + @Mocker() + def test_destruction_report_url_in_internal_result(self, m): + user = UserFactory.create(post__can_start_destruction=True) + ServiceFactory.create( + api_type=APITypes.drc, + api_root="http://localhost:8003/documenten/api/v1", + ) + destruction_list = DestructionListFactory.create( + name="A deleted list", + status=ListStatus.deleted, + internal_results={ + "created_resources": { + "enkelvoudiginformatieobjecten": [ + "http://localhost:8003/documenten/api/v1/enkelvoudiginformatieobjecten/61a914a2-db24-4a53-acbc-5306e5c346a6" + ] + } + }, + ) + + m.get( + "http://localhost:8003/documenten/api/v1/enkelvoudiginformatieobjecten/61a914a2-db24-4a53-acbc-5306e5c346a6/download" + ) + + self.client.force_authenticate(user=user) + response = self.client.get( + reverse( + "api:destructionlist-download-report", + kwargs={"uuid": destruction_list.uuid}, + ), + ) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + + @Mocker() + def test_destruction_report_url_retrieved_from_openzaak(self, m): + user = UserFactory.create(post__can_start_destruction=True) + ServiceFactory.create( + api_type=APITypes.drc, + api_root="http://localhost:8003/documenten/api/v1", + ) + ServiceFactory.create( + api_type=APITypes.zrc, + api_root="http://localhost:8003/zaken/api/v1", + ) + destruction_list = DestructionListFactory.create( + name="A deleted list", + status=ListStatus.deleted, + zaak_destruction_report_url="http://localhost:8003/zaken/api/v1/zaken/c1490adc-adf7-4e0a-97a7-fa3cd94b8ddf", + ) + + m.get( + "http://localhost:8003/documenten/api/v1/enkelvoudiginformatieobjecten/61a914a2-db24-4a53-acbc-5306e5c346a6/download" + ) + m.get( + "http://localhost:8003/zaken/api/v1/zaakinformatieobjecten?zaak=http://localhost:8003/zaken/api/v1/zaken/c1490adc-adf7-4e0a-97a7-fa3cd94b8ddf", + json=[ + { + "informatieobject": "http://localhost:8003/documenten/api/v1/enkelvoudiginformatieobjecten/61a914a2-db24-4a53-acbc-5306e5c346a6" + } + ], + ) + + self.client.force_authenticate(user=user) + response = self.client.get( + reverse( + "api:destructionlist-download-report", + kwargs={"uuid": destruction_list.uuid}, + ), + ) + + self.assertEqual(response.status_code, status.HTTP_200_OK) diff --git a/backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_report_not_found_in_openzaak.yaml b/backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_report_not_found_in_openzaak.yaml new file mode 100644 index 00000000..ca07c6c7 --- /dev/null +++ b/backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_report_not_found_in_openzaak.yaml @@ -0,0 +1,48 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate, br + Authorization: + - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0LXZjciIsImlhdCI6MTczNTAzOTYxOCwiY2xpZW50X2lkIjoidGVzdC12Y3IiLCJ1c2VyX2lkIjoiIiwidXNlcl9yZXByZXNlbnRhdGlvbiI6IiJ9.OdmIzepFs1PQyu-a2dx9jWGsOgifoG-pEtAENiABqQc + Connection: + - keep-alive + User-Agent: + - python-requests/2.32.3 + method: GET + uri: http://localhost:8003/documenten/api/v1/enkelvoudiginformatieobjecten/111-111-111/download + response: + body: + string: application/octet-stream + headers: + API-version: + - 1.4.2 + Allow: + - GET, HEAD, OPTIONS + Connection: + - keep-alive + Content-Length: + - '24' + Content-Type: + - application/octet-stream + Cross-Origin-Opener-Policy: + - same-origin + Date: + - Tue, 24 Dec 2024 11:26:58 GMT + Referrer-Policy: + - same-origin + Server: + - nginx/1.27.0 + Vary: + - origin + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + status: + code: 404 + message: Not Found +version: 1 diff --git a/backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_retrieve_destruction_report_from_internal_results.yaml b/backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_retrieve_destruction_report_from_internal_results.yaml new file mode 100644 index 00000000..5d8a3776 --- /dev/null +++ b/backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_retrieve_destruction_report_from_internal_results.yaml @@ -0,0 +1,44 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate, br + Authorization: + - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0LXZjciIsImlhdCI6MTczNDk2OTA1NiwiY2xpZW50X2lkIjoidGVzdC12Y3IiLCJ1c2VyX2lkIjoiIiwidXNlcl9yZXByZXNlbnRhdGlvbiI6IiJ9.MvNacVmG7cpDdCrNG4tkDI58JCeJkXHh-BNeGNLDGQg + Connection: + - keep-alive + User-Agent: + - python-requests/2.32.3 + method: GET + uri: http://localhost:8003/documenten/api/v1/enkelvoudiginformatieobjecten/ac7ab173-3b1c-4ea9-8ccb-023c3814e3b3/download + response: + body: + string: '"This is a destruction report" + + ' + headers: + Accept-Ranges: + - bytes + Connection: + - keep-alive + Content-Disposition: + - attachment; filename="01219b90-0d13-4e25-b463-ab1545b05f05.bin" + Content-Length: + - '31' + Content-Type: + - application/octet-stream + Date: + - Mon, 23 Dec 2024 15:50:56 GMT + ETag: + - '"6769861f-1f"' + Last-Modified: + - Mon, 23 Dec 2024 15:47:43 GMT + Server: + - nginx/1.27.0 + status: + code: 200 + message: OK +version: 1 diff --git a/backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_retrieve_report_from_zaak_url.yaml b/backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_retrieve_report_from_zaak_url.yaml new file mode 100644 index 00000000..f211e08b --- /dev/null +++ b/backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_retrieve_report_from_zaak_url.yaml @@ -0,0 +1,91 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate, br + Authorization: + - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0LXZjciIsImlhdCI6MTczNTAzODM2NSwiY2xpZW50X2lkIjoidGVzdC12Y3IiLCJ1c2VyX2lkIjoiIiwidXNlcl9yZXByZXNlbnRhdGlvbiI6IiJ9.B2nxlVAASxM-I-LuvHfX9XgvZIDS-8Tyjanlp1JiEHk + Connection: + - keep-alive + User-Agent: + - python-requests/2.32.3 + method: GET + uri: http://localhost:8003/zaken/api/v1/zaakinformatieobjecten?zaak=http%3A%2F%2Flocalhost%3A8003%2Fzaken%2Fapi%2Fv1%2Fzaken%2Fb316d968-c357-4fbf-a1ad-60b3f8b9c3a6 + response: + body: + string: '[{"url":"http://localhost:8003/zaken/api/v1/zaakinformatieobjecten/be6d2a4f-30bd-4d02-8b2d-522bcc07f90f","uuid":"be6d2a4f-30bd-4d02-8b2d-522bcc07f90f","informatieobject":"http://localhost:8003/documenten/api/v1/enkelvoudiginformatieobjecten/ac7ab173-3b1c-4ea9-8ccb-023c3814e3b3","zaak":"http://localhost:8003/zaken/api/v1/zaken/b316d968-c357-4fbf-a1ad-60b3f8b9c3a6","aardRelatieWeergave":"Hoort + bij, omgekeerd: kent","titel":"","beschrijving":"","registratiedatum":"2024-07-19T13:33:19.659000Z","vernietigingsdatum":null,"status":null}]' + headers: + API-version: + - 1.5.1 + Allow: + - GET, POST, HEAD, OPTIONS + Connection: + - keep-alive + Content-Length: + - '536' + Content-Type: + - application/json + Cross-Origin-Opener-Policy: + - same-origin + Date: + - Tue, 24 Dec 2024 11:06:05 GMT + Referrer-Policy: + - same-origin + Server: + - nginx/1.27.0 + Vary: + - Accept, origin + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + status: + code: 200 + message: OK +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate, br + Authorization: + - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0LXZjciIsImlhdCI6MTczNTAzODM2OSwiY2xpZW50X2lkIjoidGVzdC12Y3IiLCJ1c2VyX2lkIjoiIiwidXNlcl9yZXByZXNlbnRhdGlvbiI6IiJ9.x9jYGvo-s9XCR4bPpiT8qTwCEEi1HfJufbZleEOta-0 + Connection: + - keep-alive + User-Agent: + - python-requests/2.32.3 + method: GET + uri: http://localhost:8003/documenten/api/v1/enkelvoudiginformatieobjecten/ac7ab173-3b1c-4ea9-8ccb-023c3814e3b3/download + response: + body: + string: 'This is a destruction report + + ' + headers: + Accept-Ranges: + - bytes + Connection: + - keep-alive + Content-Disposition: + - attachment; filename="01219b90-0d13-4e25-b463-ab1545b05f05.bin" + Content-Length: + - '29' + Content-Type: + - application/octet-stream + Date: + - Tue, 24 Dec 2024 11:06:13 GMT + ETag: + - '"676a8b05-1d"' + Last-Modified: + - Tue, 24 Dec 2024 10:20:53 GMT + Server: + - nginx/1.27.0 + status: + code: 200 + message: OK +version: 1 diff --git a/backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_unexpected_error_response_from_openzaak_when_retrieving_report.yaml b/backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_unexpected_error_response_from_openzaak_when_retrieving_report.yaml new file mode 100644 index 00000000..873bf859 --- /dev/null +++ b/backend/src/openarchiefbeheer/destruction/tests/vcr/cassettes/DestructionListEndpointTest.test_unexpected_error_response_from_openzaak_when_retrieving_report.yaml @@ -0,0 +1,48 @@ +interactions: +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate, br + Authorization: + - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0LXZjciIsImlhdCI6MTczNTA0MDA4NywiY2xpZW50X2lkIjoidGVzdC12Y3IiLCJ1c2VyX2lkIjoiIiwidXNlcl9yZXByZXNlbnRhdGlvbiI6IiJ9.MahihwdO_IZ4ZyVMN94oyTcL6B6gR3XC6cXrPncLu0c + Connection: + - keep-alive + User-Agent: + - python-requests/2.32.3 + method: GET + uri: http://localhost:8003/documenten/api/v1/enkelvoudiginformatieobjecten/ac7ab173-3b1c-4ea9-8ccb-023c3814e3b3/download + response: + body: + string: application/octet-stream + headers: + API-version: + - 1.4.2 + Allow: + - GET, HEAD, OPTIONS + Connection: + - keep-alive + Content-Length: + - '24' + Content-Type: + - application/octet-stream + Cross-Origin-Opener-Policy: + - same-origin + Date: + - Tue, 24 Dec 2024 11:34:47 GMT + Referrer-Policy: + - same-origin + Server: + - nginx/1.27.0 + Vary: + - origin + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + status: + code: 500 + message: Internal Server Error +version: 1 diff --git a/backend/src/openarchiefbeheer/destruction/tests/vcr/test_endpoints.py b/backend/src/openarchiefbeheer/destruction/tests/vcr/test_endpoints.py new file mode 100644 index 00000000..13a47ed7 --- /dev/null +++ b/backend/src/openarchiefbeheer/destruction/tests/vcr/test_endpoints.py @@ -0,0 +1,132 @@ +from django.test import tag +from django.utils.translation import gettext_lazy as _ + +from rest_framework import status +from rest_framework.reverse import reverse +from rest_framework.test import APITestCase +from vcr.unittest import VCRMixin +from zgw_consumers.constants import APITypes +from zgw_consumers.test.factories import ServiceFactory + +from openarchiefbeheer.accounts.tests.factories import UserFactory + +from ...constants import ListStatus +from ..factories import DestructionListFactory + + +@tag("vcr") +class DestructionListEndpointTest(VCRMixin, APITestCase): + @classmethod + def setUpClass(cls) -> None: + super().setUpClass() + + cls.zrc_service = ServiceFactory.create( + api_type=APITypes.zrc, + api_root="http://localhost:8003/zaken/api/v1", + client_id="test-vcr", + secret="test-vcr", + ) + cls.drc_service = ServiceFactory.create( + api_type=APITypes.drc, + api_root="http://localhost:8003/documenten/api/v1", + client_id="test-vcr", + secret="test-vcr", + ) + + def test_retrieve_destruction_report_from_internal_results(self): + user = UserFactory.create(post__can_start_destruction=True) + destruction_list = DestructionListFactory.create( + name="A deleted list", + status=ListStatus.deleted, + internal_results={ + "created_resources": { + "enkelvoudiginformatieobjecten": [ + "http://localhost:8003/documenten/api/v1/enkelvoudiginformatieobjecten/ac7ab173-3b1c-4ea9-8ccb-023c3814e3b3" + ] + } + }, + ) + + self.client.force_authenticate(user=user) + response = self.client.get( + reverse( + "api:destructionlist-download-report", + kwargs={"uuid": destruction_list.uuid}, + ), + ) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + + def test_retrieve_report_from_zaak_url(self): + user = UserFactory.create(post__can_start_destruction=True) + destruction_list = DestructionListFactory.create( + name="A deleted list", + status=ListStatus.deleted, + zaak_destruction_report_url="http://localhost:8003/zaken/api/v1/zaken/b316d968-c357-4fbf-a1ad-60b3f8b9c3a6", + ) + + self.client.force_authenticate(user=user) + response = self.client.get( + reverse( + "api:destructionlist-download-report", + kwargs={"uuid": destruction_list.uuid}, + ), + ) + + self.assertEqual(response.status_code, status.HTTP_200_OK) + + def test_report_not_found_in_openzaak(self): + user = UserFactory.create(post__can_start_destruction=True) + destruction_list = DestructionListFactory.create( + name="A deleted list", + status=ListStatus.deleted, + internal_results={ + "created_resources": { + "enkelvoudiginformatieobjecten": [ + "http://localhost:8003/documenten/api/v1/enkelvoudiginformatieobjecten/111-111-111" # Doesn't exist in OZ + ] + } + }, + ) + + self.client.force_authenticate(user=user) + response = self.client.get( + reverse( + "api:destructionlist-download-report", + kwargs={"uuid": destruction_list.uuid}, + ), + ) + + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) + + def test_unexpected_error_response_from_openzaak_when_retrieving_report(self): + """ + Cassette recorded with the binary file in the private media of OZ + corresponding to the destruction report deleted. This causes OZ to + return a 500 error. + """ + user = UserFactory.create(post__can_start_destruction=True) + destruction_list = DestructionListFactory.create( + name="A deleted list", + status=ListStatus.deleted, + internal_results={ + "created_resources": { + "enkelvoudiginformatieobjecten": [ + "http://localhost:8003/documenten/api/v1/enkelvoudiginformatieobjecten/ac7ab173-3b1c-4ea9-8ccb-023c3814e3b3" + ] + } + }, + ) + + self.client.force_authenticate(user=user) + response = self.client.get( + reverse( + "api:destructionlist-download-report", + kwargs={"uuid": destruction_list.uuid}, + ), + ) + + self.assertEqual(response.status_code, status.HTTP_502_BAD_GATEWAY) + self.assertEqual( + response.json()["detail"], _("Error response received from Open Zaak.") + )