From 1a3adfa24531ec54afeacabfece5c44c52e246e3 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Fri, 6 Dec 2024 17:29:23 +0100 Subject: [PATCH] :white_check_mark: [#4789] Add tests for ZGWApiConfigurationStep --- .../zgw/tests/setup_configuration/__init__.py | 0 .../files/setup_config.yaml | 32 +++ .../files/setup_config_all_fields.yaml | 23 ++ .../files/setup_config_required_fields.yaml | 9 + .../test_zgw_api_config.py | 220 ++++++++++++++++++ .../contrib/zgw_apis/tests/factories.py | 4 +- .../contrib/zgw_apis/tests/test_migrations.py | 2 +- 7 files changed, 286 insertions(+), 4 deletions(-) create mode 100644 src/openforms/contrib/zgw/tests/setup_configuration/__init__.py create mode 100644 src/openforms/contrib/zgw/tests/setup_configuration/files/setup_config.yaml create mode 100644 src/openforms/contrib/zgw/tests/setup_configuration/files/setup_config_all_fields.yaml create mode 100644 src/openforms/contrib/zgw/tests/setup_configuration/files/setup_config_required_fields.yaml create mode 100644 src/openforms/contrib/zgw/tests/setup_configuration/test_zgw_api_config.py diff --git a/src/openforms/contrib/zgw/tests/setup_configuration/__init__.py b/src/openforms/contrib/zgw/tests/setup_configuration/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/openforms/contrib/zgw/tests/setup_configuration/files/setup_config.yaml b/src/openforms/contrib/zgw/tests/setup_configuration/files/setup_config.yaml new file mode 100644 index 0000000000..520a5d83f6 --- /dev/null +++ b/src/openforms/contrib/zgw/tests/setup_configuration/files/setup_config.yaml @@ -0,0 +1,32 @@ +zgw_api_config_enable: True +zgw_api: + groups: + - name: Config 1 + identifier: config-1 + zaken_service_identifier: zaken-test + documenten_service_identifier: documenten-test + catalogi_service_identifier: catalogi-test + catalogue_domain: TEST + catalogue_rsin: "000000000" + organisatie_rsin: "000000000" + zaak_vertrouwelijkheidaanduiding: zaakvertrouwelijk + doc_vertrouwelijkheidaanduiding: openbaar + auteur: John Doe + objects_api_json_content_template: | + { + "data": {% json_summary %}, + "type": "{{ productaanvraag_type }}", + "bsn": "{{ variables.auth_bsn }}", + "submission_id": "{{ submission.kenmerk }}", + "language_code": "{{ submission.language_code }}", + "custom_field": "foo" + } + - name: Config 2 + identifier: config-2 + zaken_service_identifier: zaken-test + documenten_service_identifier: documenten-test + catalogi_service_identifier: catalogi-test + catalogue_domain: OTHER + catalogue_rsin: "000000000" + organisatie_rsin: "000000000" + auteur: Jane Doe \ No newline at end of file diff --git a/src/openforms/contrib/zgw/tests/setup_configuration/files/setup_config_all_fields.yaml b/src/openforms/contrib/zgw/tests/setup_configuration/files/setup_config_all_fields.yaml new file mode 100644 index 0000000000..8ce6667fba --- /dev/null +++ b/src/openforms/contrib/zgw/tests/setup_configuration/files/setup_config_all_fields.yaml @@ -0,0 +1,23 @@ +zgw_api_config_enable: True +zgw_api: + groups: + - name: Config 1 + identifier: config-1 + zaken_service_identifier: zaken-test + documenten_service_identifier: documenten-test + catalogi_service_identifier: catalogi-test + catalogue_domain: TEST + catalogue_rsin: "000000000" + organisatie_rsin: "000000000" + zaak_vertrouwelijkheidaanduiding: zaakvertrouwelijk + doc_vertrouwelijkheidaanduiding: openbaar + auteur: John Doe + objects_api_json_content_template: | + { + "data": {% json_summary %}, + "type": "{{ productaanvraag_type }}", + "bsn": "{{ variables.auth_bsn }}", + "submission_id": "{{ submission.kenmerk }}", + "language_code": "{{ submission.language_code }}", + "custom_field": "foo" + } \ No newline at end of file diff --git a/src/openforms/contrib/zgw/tests/setup_configuration/files/setup_config_required_fields.yaml b/src/openforms/contrib/zgw/tests/setup_configuration/files/setup_config_required_fields.yaml new file mode 100644 index 0000000000..ba283093de --- /dev/null +++ b/src/openforms/contrib/zgw/tests/setup_configuration/files/setup_config_required_fields.yaml @@ -0,0 +1,9 @@ +zgw_api_config_enable: True +zgw_api: + groups: + - name: Config 1 + identifier: config-1 + zaken_service_identifier: zaken-test + documenten_service_identifier: documenten-test + catalogi_service_identifier: catalogi-test + auteur: Jane Doe diff --git a/src/openforms/contrib/zgw/tests/setup_configuration/test_zgw_api_config.py b/src/openforms/contrib/zgw/tests/setup_configuration/test_zgw_api_config.py new file mode 100644 index 0000000000..cdff1519e4 --- /dev/null +++ b/src/openforms/contrib/zgw/tests/setup_configuration/test_zgw_api_config.py @@ -0,0 +1,220 @@ +import textwrap +from pathlib import Path + +from django.test import TestCase + +from django_setup_configuration.test_utils import execute_single_step +from zgw_consumers.api_models.constants import VertrouwelijkheidsAanduidingen +from zgw_consumers.constants import APITypes, AuthTypes +from zgw_consumers.models import Service +from zgw_consumers.test.factories import ServiceFactory + +from openforms.contrib.zgw.setup_configuration.steps import ZGWApiConfigurationStep +from openforms.registrations.contrib.zgw_apis.models import ( + ZGWApiGroupConfig, + get_content_text, +) +from openforms.registrations.contrib.zgw_apis.tests.factories import ( + ZGWApiGroupConfigFactory, +) + +TEST_FILES = (Path(__file__).parent / "files").resolve() +CONFIG_FILE_PATH = str(TEST_FILES / "setup_config.yaml") +CONFIG_FILE_PATH_REQUIRED_FIELDS = str(TEST_FILES / "setup_config_required_fields.yaml") +CONFIG_FILE_PATH_ALL_FIELDS = str(TEST_FILES / "setup_config_all_fields.yaml") + + +class ZGWApiConfigurationStepTests(TestCase): + maxDiff = None + + @classmethod + def setUpTestData(cls): + super().setUpTestData() + + cls.zaken_service = ServiceFactory.create( + slug="zaken-test", + label="Zaken API test", + api_root="http://localhost:8003/zaken/api/v1/", + api_type=APITypes.zrc, + auth_type=AuthTypes.zgw, + client_id="test_client_id", + secret="test_secret_key", + ) + cls.documenten_service = ServiceFactory.create( + slug="documenten-test", + label="Documenten API test", + api_root="http://localhost:8003/documenten/api/v1/", + api_type=APITypes.drc, + auth_type=AuthTypes.zgw, + client_id="test_client_id", + secret="test_secret_key", + ) + cls.catalogi_service = ServiceFactory.create( + slug="catalogi-test", + label="Catalogi API test", + api_root="http://localhost:8003/catalogi/api/v1/", + api_type=APITypes.ztc, + auth_type=AuthTypes.zgw, + client_id="test_client_id", + secret="test_secret_key", + ) + + def test_execute_success(self): + execute_single_step(ZGWApiConfigurationStep, yaml_source=CONFIG_FILE_PATH) + + self.assertEqual(ZGWApiGroupConfig.objects.count(), 2) + + config1, config2 = ZGWApiGroupConfig.objects.order_by("pk") + + self.assertEqual(config1.name, "Config 1") + self.assertEqual(config1.identifier, "config-1") + self.assertEqual(config1.zrc_service, self.zaken_service) + self.assertEqual(config1.drc_service, self.documenten_service) + self.assertEqual(config1.ztc_service, self.catalogi_service) + self.assertEqual(config1.catalogue_domain, "TEST") + self.assertEqual(config1.catalogue_rsin, "000000000") + self.assertEqual(config1.organisatie_rsin, "000000000") + self.assertEqual( + config1.zaak_vertrouwelijkheidaanduiding, + VertrouwelijkheidsAanduidingen.zaakvertrouwelijk, + ) + self.assertEqual( + config1.doc_vertrouwelijkheidaanduiding, + VertrouwelijkheidsAanduidingen.openbaar, + ) + + expected_json = textwrap.dedent( + """\ + { + "data": {% json_summary %}, + "type": "{{ productaanvraag_type }}", + "bsn": "{{ variables.auth_bsn }}", + "submission_id": "{{ submission.kenmerk }}", + "language_code": "{{ submission.language_code }}", + "custom_field": "foo" + } + """ + ) + + self.assertEqual(config1.content_json, expected_json) + + self.assertEqual(config2.name, "Config 2") + self.assertEqual(config2.identifier, "config-2") + self.assertEqual(config2.zrc_service, self.zaken_service) + self.assertEqual(config2.drc_service, self.documenten_service) + self.assertEqual(config2.ztc_service, self.catalogi_service) + self.assertEqual(config2.catalogue_domain, "OTHER") + self.assertEqual(config2.catalogue_rsin, "000000000") + self.assertEqual(config2.organisatie_rsin, "000000000") + self.assertEqual(config2.zaak_vertrouwelijkheidaanduiding, "") + self.assertEqual(config2.doc_vertrouwelijkheidaanduiding, "") + self.assertEqual(config2.content_json, get_content_text()) + + def test_execute_update_existing_config(self): + ZGWApiGroupConfigFactory.create(name="old name", identifier="config-1") + + execute_single_step(ZGWApiConfigurationStep, yaml_source=CONFIG_FILE_PATH) + + self.assertEqual(ZGWApiGroupConfig.objects.count(), 2) + + config1, config2 = ZGWApiGroupConfig.objects.order_by("pk") + + self.assertEqual(config1.name, "Config 1") + self.assertEqual(config1.identifier, "config-1") + + self.assertEqual(config2.name, "Config 2") + self.assertEqual(config2.identifier, "config-2") + + def test_execute_with_required_fields(self): + execute_single_step( + ZGWApiConfigurationStep, yaml_source=CONFIG_FILE_PATH_REQUIRED_FIELDS + ) + + self.assertEqual(ZGWApiGroupConfig.objects.count(), 1) + + config = ZGWApiGroupConfig.objects.get() + + self.assertEqual(config.name, "Config 1") + self.assertEqual(config.identifier, "config-1") + self.assertEqual(config.zrc_service, self.zaken_service) + self.assertEqual(config.drc_service, self.documenten_service) + self.assertEqual(config.ztc_service, self.catalogi_service) + + self.assertEqual(config.catalogue_domain, "") + self.assertEqual(config.catalogue_rsin, "") + self.assertEqual(config.organisatie_rsin, "") + self.assertEqual(config.zaak_vertrouwelijkheidaanduiding, "") + self.assertEqual(config.doc_vertrouwelijkheidaanduiding, "") + self.assertEqual(config.content_json, get_content_text()) + + def test_execute_with_all_fields(self): + execute_single_step( + ZGWApiConfigurationStep, yaml_source=CONFIG_FILE_PATH_ALL_FIELDS + ) + + self.assertEqual(ZGWApiGroupConfig.objects.count(), 1) + + config = ZGWApiGroupConfig.objects.get() + + self.assertEqual(config.name, "Config 1") + self.assertEqual(config.identifier, "config-1") + self.assertEqual(config.zrc_service, self.zaken_service) + self.assertEqual(config.drc_service, self.documenten_service) + self.assertEqual(config.ztc_service, self.catalogi_service) + self.assertEqual(config.catalogue_domain, "TEST") + self.assertEqual(config.catalogue_rsin, "000000000") + self.assertEqual(config.organisatie_rsin, "000000000") + self.assertEqual( + config.zaak_vertrouwelijkheidaanduiding, + VertrouwelijkheidsAanduidingen.zaakvertrouwelijk, + ) + self.assertEqual( + config.doc_vertrouwelijkheidaanduiding, + VertrouwelijkheidsAanduidingen.openbaar, + ) + + expected_json = textwrap.dedent( + """\ + { + "data": {% json_summary %}, + "type": "{{ productaanvraag_type }}", + "bsn": "{{ variables.auth_bsn }}", + "submission_id": "{{ submission.kenmerk }}", + "language_code": "{{ submission.language_code }}", + "custom_field": "foo" + }""" + ) + + self.assertEqual(config.content_json, expected_json) + + def test_execute_is_idempotent(self): + self.assertFalse(ZGWApiGroupConfig.objects.exists()) + + with self.subTest("run step first time"): + execute_single_step( + ZGWApiConfigurationStep, yaml_source=CONFIG_FILE_PATH_ALL_FIELDS + ) + + self.assertEqual(ZGWApiGroupConfig.objects.count(), 1) + + with self.subTest("run step second time"): + execute_single_step( + ZGWApiConfigurationStep, yaml_source=CONFIG_FILE_PATH_ALL_FIELDS + ) + + # no additional configs created, but existing one updated + self.assertEqual(ZGWApiGroupConfig.objects.count(), 1) + + def test_execute_service_not_found_raises_error(self): + self.zaken_service.delete() + + with self.assertRaisesMessage( + Service.DoesNotExist, + "Service matching query does not exist. (identifier = zaken-test)", + ): + execute_single_step( + ZGWApiConfigurationStep, + yaml_source=CONFIG_FILE_PATH_REQUIRED_FIELDS, + ) + + self.assertEqual(ZGWApiGroupConfig.objects.count(), 0) diff --git a/src/openforms/registrations/contrib/zgw_apis/tests/factories.py b/src/openforms/registrations/contrib/zgw_apis/tests/factories.py index 719fe3fb75..f795295307 100644 --- a/src/openforms/registrations/contrib/zgw_apis/tests/factories.py +++ b/src/openforms/registrations/contrib/zgw_apis/tests/factories.py @@ -1,5 +1,3 @@ -from django.utils.text import slugify - import factory from zgw_consumers.constants import APITypes, AuthTypes from zgw_consumers.test.factories import ServiceFactory @@ -9,7 +7,7 @@ class ZGWApiGroupConfigFactory(factory.django.DjangoModelFactory): name = factory.Sequence(lambda n: "ZGW API set %03d" % n) - identifier = factory.LazyAttribute(lambda o: slugify(o.name)) + identifier = factory.Sequence(lambda n: f"zgw-api-group-{n}") zrc_service = factory.SubFactory( "zgw_consumers.test.factories.ServiceFactory", api_type=APITypes.zrc ) diff --git a/src/openforms/registrations/contrib/zgw_apis/tests/test_migrations.py b/src/openforms/registrations/contrib/zgw_apis/tests/test_migrations.py index f1631ef6e9..9d94186cb7 100644 --- a/src/openforms/registrations/contrib/zgw_apis/tests/test_migrations.py +++ b/src/openforms/registrations/contrib/zgw_apis/tests/test_migrations.py @@ -58,7 +58,7 @@ def setUpBeforeMigration(self, apps: StateApps): def test_identifiers_generated(self): ZGWApiGroupConfig = self.apps.get_model("zgw_apis", "ZGWApiGroupConfig") - groups = ZGWApiGroupConfig.objects.all() + groups = ZGWApiGroupConfig.objects.order_by("pk") self.assertEqual(groups.count(), 3)