class UpdateZaakTests(TestCase): @patch( "archiefbeheercomponent.destruction.tasks.update_zaak", return_value={"identificatie": "foobar"}, ) def test_update_zaak_from_list_item(self, mock_update_zaak): list_item = DestructionListItemFactory.create( status=ListItemStatus.removed) DestructionListItemReviewFactory.create( destruction_list_item=list_item, text="Change archive params") archive_data = { "archiefnominatie": "blijvend_bewaren", "archiefactiedatum": "2020-06-17", } update_zaak_from_list_item(list_item.id, archive_data) log = TimelineLog.objects.get() self.assertEqual(log.content_object, list_item) self.assertEqual(log.extra_data, {"zaak": "foobar"}) self.assertEqual(log.template, "destruction/logs/item_update_succeeded.html") mock_update_zaak.assert_called_once_with(list_item.zaak, archive_data, "Change archive params") @patch( "archiefbeheercomponent.destruction.tasks.update_zaak", side_effect=ClientError("something went wrong"), ) def test_update_zaak_from_list_item_fail(self, mock_update_zaak): list_item = DestructionListItemFactory.create( status=ListItemStatus.removed) archive_data = { "archiefnominatie": "blijvend_bewaren", "archiefactiedatum": "2020-06-17", } update_zaak_from_list_item(list_item.id, archive_data) log = TimelineLog.objects.get() self.assertEqual(log.content_object, list_item) extra_data = log.extra_data self.assertEqual(extra_data["zaak"], None) self.assertTrue("something went wrong" in extra_data["error"]) self.assertEqual(log.template, "destruction/logs/item_update_failed.html") mock_update_zaak.assert_called_once_with(list_item.zaak, archive_data, None)
def test_register_webhook_client_error(request_with_middleware): config = NotificationsConfig.get_solo() Subscription.objects.create( config=config, callback_url="https://example.com/callback", client_id="client_id", secret="secret", channels=["zaken"], ) with patch("zds_client.client.Client.create", side_effect=ClientError("exception")): register_webhook(object, request_with_middleware, Subscription.objects.all()) messages = list(get_messages(request_with_middleware)) assert len(messages) == 1 assert messages[0].message == _( "Something went wrong while registering subscription for {callback_url}: {e}" ).format(callback_url="https://example.com/callback", e="exception")
def _client_from_url(url: str): service = Service.get_service(url) if not service: raise ClientError("There is no Service configured for %r" % url) return service.build_client()
class ProcessListItemTests(TestCase): @patch("archiefbeheercomponent.destruction.tasks.remove_zaak") @patch( "archiefbeheercomponent.destruction.tasks.fetch_zaak", return_value={ "identificatie": "foobar", "omschrijving": "Een zaak", "toelichting": "Bah", "startdatum": "2020-01-01", "einddatum": "2021-01-01", "zaaktype": "https://oz.nl/catalogi/api/v1/zaaktypen/uuid-1", "resultaat": "https://oz.nl/zaken/api/v1/resultaat/uuid-1", "verantwoordelijkeOrganisatie": "Some organisation", }, ) @patch( "archiefbeheercomponent.destruction.service.get_resultaat", return_value={ "resultaattype": { "omschrijving": "Nice result type", "archiefactietermijn": "20 days", }, }, ) def test_process_list_item(self, mock_fetch_zaak, mock_remove_zaken, mock_get_resultaat): list_item = DestructionListItemFactory.create() process_list_item(list_item.id) # can't use refresh_from_db() because of django-fsm list_item = DestructionListItem.objects.get(id=list_item.id) self.assertEqual(list_item.status, ListItemStatus.destroyed) log = TimelineLog.objects.get() self.assertEqual(log.content_object, list_item) self.assertEqual(log.extra_data, {"zaak": "foobar"}) mock_remove_zaken.assert_called_once_with(list_item.zaak) @patch( "archiefbeheercomponent.destruction.tasks.remove_zaak", side_effect=ClientError("something went wrong"), ) @patch( "archiefbeheercomponent.destruction.tasks.fetch_zaak", return_value={ "identificatie": "foobar", "omschrijving": "Een zaak", "toelichting": "Bah", "startdatum": "2020-01-01", "einddatum": "2021-01-01", "zaaktype": "https://oz.nl/catalogi/api/v1/zaaktypen/uuid-1", "resultaat": "https://oz.nl/zaken/api/v1/resultaat/uuid-1", "verantwoordelijkeOrganisatie": "Some organisation", }, ) @patch("archiefbeheercomponent.destruction.service.get_resultaat") def test_process_list_item_fail(self, mock_fetch_zaak, mock_remove_zaken, mock_get_resultaat): list_item = DestructionListItemFactory.create() process_list_item(list_item.id) # can't use refresh_from_db() because of django-fsm list_item = DestructionListItem.objects.get(id=list_item.id) self.assertEqual(list_item.status, ListItemStatus.failed) log = TimelineLog.objects.get() self.assertEqual(log.content_object, list_item) extra_data = log.extra_data self.assertEqual(extra_data["zaak"], "foobar") self.assertTrue("something went wrong" in extra_data["error"]) mock_remove_zaken.assert_called_once_with(list_item.zaak) @patch("archiefbeheercomponent.destruction.tasks.remove_zaak") @patch( "archiefbeheercomponent.destruction.tasks.fetch_zaak", return_value={ "identificatie": "foobar", "omschrijving": "Een zaak", "toelichting": "Bah", "startdatum": "2020-01-01", "einddatum": "2021-01-01", "zaaktype": "https://oz.nl/catalogi/api/v1/zaaktypen/uuid-1", "resultaat": "https://oz.nl/zaken/api/v1/resultaat/uuid-1", "verantwoordelijkeOrganisatie": "Some organisation", }, ) @patch( "archiefbeheercomponent.destruction.service.get_resultaat", return_value={ "resultaattype": { "omschrijving": "Nice result type", "archiefactietermijn": "20 days", }, }, ) def test_process_list_item_status_update_processing( self, mock_fetch_zaak, mock_remove_zaken, mock_get_resultaat): """ Test that the db state is updated. """ list_item = DestructionListItemFactory.create() def assert_list_item_status(url: str): # hook into mock call to make the assertion _list_item = DestructionListItem.objects.get(pk=list_item.pk) self.assertEqual(_list_item.status, ListItemStatus.processing) return { "identificatie": "foobar", "omschrijving": "Een zaak", "toelichting": "Bah", "startdatum": "2020-01-01", "einddatum": "2021-01-01", "zaaktype": "https://oz.nl/catalogi/api/v1/zaaktypen/uuid-1", } mock_fetch_zaak.side_effect = assert_list_item_status process_list_item(list_item.id) @patch( "archiefbeheercomponent.destruction.tasks.fetch_zaak", side_effect=ClientError("Oopsiewoopsie"), ) def test_process_list_item_fetch_zaak_fail_recorded(self, mock_fetch_zaak): list_item = DestructionListItemFactory.create() process_list_item(list_item.id) list_item = DestructionListItem.objects.get(pk=list_item.pk) self.assertEqual(list_item.status, ListItemStatus.failed) @patch( "archiefbeheercomponent.destruction.tasks.fetch_zaak", return_value={ "identificatie": "foobar", "omschrijving": "Een zaak", "toelichting": "Bah", "startdatum": "2020-01-01", "einddatum": "2021-01-01", "zaaktype": "https://oz.nl/catalogi/api/v1/zaaktypen/uuid-1", "resultaat": "https://oz.nl/zaken/api/v1/resultaat/uuid-1", "verantwoordelijkeOrganisatie": "Some organisation", }, ) @patch("archiefbeheercomponent.destruction.tasks.remove_zaak") @patch( "archiefbeheercomponent.destruction.service.get_resultaat", return_value={ "resultaattype": { "omschrijving": "Nice result type", "archiefactietermijn": "20 days", }, }, ) @override_settings(ABC_DEMO_MODE=True) def test_zaak_not_removed_in_demo_mode(self, mock_fetch_zaak, mock_remove_zaken, mock_get_resultaat): list_item = DestructionListItemFactory.create() process_list_item(list_item.id) # can't use refresh_from_db() because of django-fsm list_item = DestructionListItem.objects.get(id=list_item.id) self.assertEqual(list_item.status, ListItemStatus.destroyed) log = TimelineLog.objects.get() self.assertEqual(log.content_object, list_item) self.assertEqual(log.extra_data, {"zaak": "foobar"}) mock_remove_zaken.assert_not_called()
class UpdateZaakArchiveDetailsTests(WebTest): def test_cant_access_without_can_start_destruction(self): user = UserFactory(role__can_start_destruction=False) self.client.force_login(user) response = self.client.get( reverse("destruction:update-zaak-archive-details")) self.assertEqual(403, response.status_code) def test_permission_denied_if_no_zaak_url(self): user = UserFactory(role__can_start_destruction=True) self.client.force_login(user) response = self.client.get( reverse("destruction:update-zaak-archive-details")) self.assertEqual(403, response.status_code) @patch("archiefbeheercomponent.destruction.views.record_manager.fetch_zaak" ) def test_can_access_with_can_start_destruction_and_zaak_url( self, m_fetch_zaak): user = UserFactory(role__can_start_destruction=True) self.client.force_login(user) form_url = furl(reverse("destruction:update-zaak-archive-details")) form_url.args.set("url", "http://openzaak.nl/some/zaak") response = self.client.get(form_url.url) self.assertEqual(200, response.status_code) @patch( "archiefbeheercomponent.destruction.views.record_manager.fetch_zaak", return_value={ "url": "http://openzaak.nl/some/valid/zaak/url", "identificatie": "ZAAK-1", "archiefnominatie": "blijvend_bewaren", "archiefstatus": "nog_te_archiveren", "archiefactiedatum": "2030-02-01", }, ) def test_fill_initial_data(self, m_fetch_zaak): user = UserFactory(role__can_start_destruction=True) view_url = furl(reverse("destruction:update-zaak-archive-details")) view_url.args["url"] = "http://openzaak.nl/some/valid/zaak/url" response = self.app.get(view_url.url, user=user) self.assertEqual(200, response.status_code) form = response.form self.assertEqual("http://openzaak.nl/some/valid/zaak/url", form["url"].value) self.assertEqual(Archiefnominatie.blijvend_bewaren, form["archiefnominatie"].value) self.assertEqual(Archiefstatus.nog_te_archiveren, form["archiefstatus"].value) self.assertEqual("1", form["archiefactiedatum_day"].value) self.assertEqual("2", form["archiefactiedatum_month"].value) self.assertEqual("2030", form["archiefactiedatum_year"].value) @patch( "archiefbeheercomponent.destruction.views.record_manager.fetch_zaak", return_value={"url": "http://openzaak.nl/some/zaak"}, ) @patch( "archiefbeheercomponent.destruction.views.record_manager.update_zaak", ) def test_submit_successful_form_redirects(self, m_update_zaak, m_fetch_zaak): user = UserFactory(role__can_start_destruction=True) view_url = furl(reverse("destruction:update-zaak-archive-details")) view_url.args.set("url", "http://openzaak.nl/some/zaak") form = self.app.get(view_url.url, user=user).form form["url"] = "http://openzaak.nl/some/valid/zaak/url" form["archiefnominatie"] = Archiefnominatie.blijvend_bewaren form["archiefactiedatum_day"] = "1" form["archiefactiedatum_month"] = "1" form["archiefactiedatum_year"] = "2030" form["archiefstatus"] = Archiefstatus.nog_te_archiveren form["comment"] = "Some interesting comment" response = form.submit() self.assertRedirects(response, view_url.url, fetch_redirect_response=False) m_update_zaak.assert_called_with( "http://openzaak.nl/some/valid/zaak/url", { "archiefnominatie": Archiefnominatie.blijvend_bewaren, "archiefstatus": Archiefstatus.nog_te_archiveren, "archiefactiedatum": "2030-01-01", }, audit_comment="Some interesting comment", ) response = response.follow() messages = list(response.context.get("messages")) self.assertEqual(1, len(messages)) self.assertEqual(messages[0].tags, "success") @patch("archiefbeheercomponent.destruction.views.record_manager.fetch_zaak" ) @patch( "archiefbeheercomponent.destruction.views.record_manager.update_zaak", side_effect=ClientError({ "type": "http://127.0.0.1:8123/ref/fouten/ValidationError/", "code": "invalid", "title": "Invalid input.", "status": 400, "detail": "", "instance": "urn:uuid:c8d9bf6b-4469-4b93-8411-1ebb5e4f4a40", "invalidParams": [{ "name": "nonFieldErrors", "code": "documents-not-archived", "reason": "Some error message", }], }), ) @override_settings(LANGUAGE_CODE="en") def test_client_error_during_update(self, m_update_zaak, m_fetch_zaak): user = UserFactory(role__can_start_destruction=True) view_url = furl(reverse("destruction:update-zaak-archive-details")) view_url.args.set("url", "http://openzaak.nl/some/zaak") form = self.app.get(view_url.url, user=user).form form["url"] = "http://openzaak.nl/some/valid/zaak/url" form["archiefnominatie"] = Archiefnominatie.blijvend_bewaren form["archiefactiedatum_day"] = "1" form["archiefactiedatum_month"] = "1" form["archiefactiedatum_year"] = "2030" form["comment"] = "Some interesting comment" response = form.submit() self.assertEqual(200, response.status_code) self.assertContains( response, "An error has occurred. The case could not be updated.") self.assertContains( response, "Some error message", ) @patch( "archiefbeheercomponent.destruction.views.record_manager.fetch_zaak", return_value={"url": "http://openzaak.nl/some/zaak"}, ) @patch( "archiefbeheercomponent.destruction.views.record_manager.update_zaak", ) def test_empty_archiefactiedatum(self, m_update_zaak, m_fetch_zaak): user = UserFactory(role__can_start_destruction=True) view_url = furl(reverse("destruction:update-zaak-archive-details")) view_url.args.set("url", "http://openzaak.nl/some/zaak") form = self.app.get(view_url.url, user=user).form form["url"] = "http://openzaak.nl/some/valid/zaak/url" form["archiefnominatie"] = Archiefnominatie.blijvend_bewaren form["comment"] = "Some interesting comment" response = form.submit() self.assertRedirects(response, view_url.url, fetch_redirect_response=False) m_update_zaak.assert_called_with( "http://openzaak.nl/some/valid/zaak/url", {"archiefnominatie": Archiefnominatie.blijvend_bewaren}, audit_comment="Some interesting comment", ) response = response.follow() messages = list(response.context.get("messages")) self.assertEqual(1, len(messages)) self.assertEqual(messages[0].tags, "success") @patch("archiefbeheercomponent.destruction.views.record_manager.fetch_zaak" ) @patch( "archiefbeheercomponent.destruction.views.record_manager.update_zaak", side_effect=ClientError( {"some_unexpected_keys": "some_unexpected_values"}), ) @override_settings(LANGUAGE_CODE="en") def test_unexpected_client_error_format_does_not_cause_error( self, m_update_zaak, m_fetch_zaak): user = UserFactory(role__can_start_destruction=True) view_url = furl(reverse("destruction:update-zaak-archive-details")) view_url.args.set("url", "http://openzaak.nl/some/zaak") form = self.app.get(view_url.url, user=user).form form["url"] = "http://openzaak.nl/some/valid/zaak/url" form["archiefnominatie"] = Archiefnominatie.blijvend_bewaren form["archiefactiedatum_day"] = "1" form["archiefactiedatum_month"] = "1" form["archiefactiedatum_year"] = "2030" form["comment"] = "Some interesting comment" response = form.submit() self.assertEqual(200, response.status_code) self.assertContains( response, "An error has occurred. The case could not be updated.")