From 355b4a099515c601ede0ae1f9d9d4f2073b50c9d Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Wed, 27 Sep 2023 09:14:51 +0200 Subject: [PATCH] :white_check_mark: [#3489] Address missing coverage --- .../contrib/stufbg/tests/test_plugin.py | 28 ++++++++++- .../registrations/contrib/stuf_zds/plugin.py | 4 +- .../stuf_zds/tests/test_config_check.py | 50 +++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 src/openforms/registrations/contrib/stuf_zds/tests/test_config_check.py diff --git a/src/openforms/prefill/contrib/stufbg/tests/test_plugin.py b/src/openforms/prefill/contrib/stufbg/tests/test_plugin.py index 889cbcaee3..4ccc1fdc26 100644 --- a/src/openforms/prefill/contrib/stufbg/tests/test_plugin.py +++ b/src/openforms/prefill/contrib/stufbg/tests/test_plugin.py @@ -53,6 +53,18 @@ def setUp(self) -> None: stufbg_config_patcher.start() self.addCleanup(stufbg_config_patcher.stop) + def test_getting_available_attributes(self): + attributes = self.plugin.get_available_attributes() + self.assertIsInstance(attributes, list) + + for entry in attributes: + with self.subTest(entry=entry): + self.assertIsInstance(entry, tuple) + self.assertEqual(len(entry), 2) + value, label = entry + self.assertEqual(value, str(value)) + self.assertEqual(label, str(label)) + def test_get_available_attributes_returns_correct_attributes(self): client_patcher = mock_stufbg_client("StufBgResponse.xml") self.addCleanup(client_patcher.stop) @@ -241,13 +253,20 @@ def setUp(self) -> None: self.plugin = StufBgPrefill("test-plugin") # mock out django-solo interface (we don't have to deal with caches then) + self.config = StufBGConfig(service=self.stuf_bg_service) stufbg_config_patcher = patch( "openforms.prefill.contrib.stufbg.plugin.StufBGConfig.get_solo", - return_value=StufBGConfig(service=self.stuf_bg_service), + return_value=self.config, ) stufbg_config_patcher.start() self.addCleanup(stufbg_config_patcher.stop) + def test_no_service_configured(self): + self.config.service = None + + with self.assertRaises(InvalidPluginConfiguration): + self.plugin.check_config() + def test_check_config_exception(self): with patch( "stuf.stuf_bg.client.Client.get_values_for_attributes", @@ -256,6 +275,13 @@ def test_check_config_exception(self): with self.assertRaises(InvalidPluginConfiguration): self.plugin.check_config() + @requests_mock.Mocker() + def test_check_config_invalid_xml_returned(self, m): + m.register_uri(requests_mock.ANY, requests_mock.ANY, json={"not": "xml"}) + + with self.assertRaises(InvalidPluginConfiguration): + self.plugin.check_config() + def test_check_config_ok_not_found(self): try: with mock_stufbg_make_request("StufBgNotFoundResponse.xml"): diff --git a/src/openforms/registrations/contrib/stuf_zds/plugin.py b/src/openforms/registrations/contrib/stuf_zds/plugin.py index b7a731c9f5..ef41bb9b1c 100644 --- a/src/openforms/registrations/contrib/stuf_zds/plugin.py +++ b/src/openforms/registrations/contrib/stuf_zds/plugin.py @@ -33,6 +33,8 @@ logger = logging.getLogger(__name__) +PLUGIN_IDENTIFIER = "stuf-zds-create-zaak" + class ZaakOptionsSerializer(JsonSchemaSerializerMixin, serializers.Serializer): gemeentecode = serializers.CharField( @@ -155,7 +157,7 @@ def _gender_choices(value): return value -@register("stuf-zds-create-zaak") +@register(PLUGIN_IDENTIFIER) class StufZDSRegistration(BasePlugin): verbose_name = _("StUF-ZDS") configuration_options = ZaakOptionsSerializer diff --git a/src/openforms/registrations/contrib/stuf_zds/tests/test_config_check.py b/src/openforms/registrations/contrib/stuf_zds/tests/test_config_check.py new file mode 100644 index 0000000000..777f170437 --- /dev/null +++ b/src/openforms/registrations/contrib/stuf_zds/tests/test_config_check.py @@ -0,0 +1,50 @@ +from unittest.mock import patch + +from django.test import SimpleTestCase + +import requests +import requests_mock + +from openforms.plugins.exceptions import InvalidPluginConfiguration +from stuf.stuf_zds.models import StufZDSConfig +from stuf.tests.factories import StufServiceFactory + +from ..plugin import PLUGIN_IDENTIFIER, StufZDSRegistration + + +class ConfigCheckTests(SimpleTestCase): + def setUp(self): + super().setUp() + + self.config = StufZDSConfig( + service=StufServiceFactory.build(), + gemeentecode="foo", + ) + patcher = patch( + "stuf.stuf_zds.client.StufZDSConfig.get_solo", + return_value=self.config, + ) + patcher.start() + self.addCleanup(patcher.stop) + + def test_no_service_configured(self): + self.config.service = None + plugin = StufZDSRegistration(PLUGIN_IDENTIFIER) + + with self.assertRaises(InvalidPluginConfiguration): + plugin.check_config() + + def test_missing_gemeentecode(self): + self.config.gemeentecode = "" + plugin = StufZDSRegistration(PLUGIN_IDENTIFIER) + + with self.assertRaises(InvalidPluginConfiguration): + plugin.check_config() + + @requests_mock.Mocker() + def test_failing_request(self, m): + m.post(requests_mock.ANY, exc=requests.ConnectionError("nope")) + plugin = StufZDSRegistration(PLUGIN_IDENTIFIER) + + with self.assertRaises(InvalidPluginConfiguration): + plugin.check_config()