From 9636d7d36dfac20fd12ad57250fa6b5e166ed302 Mon Sep 17 00:00:00 2001 From: robinvandermolen Date: Tue, 3 Dec 2024 11:38:18 +0100 Subject: [PATCH] :white_check_mark: [#2173] Added tests for map component --- src/openforms/config/tests/factories.py | 9 + src/openforms/config/tests/test_admin.py | 23 +- .../formio/tests/test_dynamic_config.py | 292 ++++++++++++++++++ 3 files changed, 323 insertions(+), 1 deletion(-) create mode 100644 src/openforms/formio/tests/test_dynamic_config.py diff --git a/src/openforms/config/tests/factories.py b/src/openforms/config/tests/factories.py index 4cec877d42..08476e714b 100644 --- a/src/openforms/config/tests/factories.py +++ b/src/openforms/config/tests/factories.py @@ -14,3 +14,12 @@ class ThemeFactory(factory.django.DjangoModelFactory): class Meta: model = "config.Theme" + + +class MapTileLayerFactory(factory.django.DjangoModelFactory): + identifier = factory.Faker("word") + url = factory.Sequence(lambda n: f"http://example-{n}.com") + label = factory.Faker("word") + + class Meta: + model = "config.MapTileLayer" diff --git a/src/openforms/config/tests/test_admin.py b/src/openforms/config/tests/test_admin.py index cc924ed832..2f60b53bd7 100644 --- a/src/openforms/config/tests/test_admin.py +++ b/src/openforms/config/tests/test_admin.py @@ -5,7 +5,7 @@ from openforms.accounts.tests.factories import SuperUserFactory -from .factories import RichTextColorFactory +from .factories import MapTileLayerFactory, RichTextColorFactory @disable_admin_mfa() @@ -27,3 +27,24 @@ def test_color_detail(self): response = self.app.get(url, user=user) self.assertEqual(response.status_code, 200) + + +@disable_admin_mfa() +class MapTileLayerTests(WebTest): + def test_map_tile_layer_changelist(self): + MapTileLayerFactory.create_batch(9) + url = reverse("admin:config_maptilelayer_changelist") + user = SuperUserFactory.create() + + response = self.app.get(url, user=user) + + self.assertEqual(response.status_code, 200) + + def test_map_tile_layer_detail(self): + map = MapTileLayerFactory.create() + url = reverse("admin:config_maptilelayer_change", args=(map.pk,)) + user = SuperUserFactory.create() + + response = self.app.get(url, user=user) + + self.assertEqual(response.status_code, 200) diff --git a/src/openforms/formio/tests/test_dynamic_config.py b/src/openforms/formio/tests/test_dynamic_config.py new file mode 100644 index 0000000000..cb37ec5694 --- /dev/null +++ b/src/openforms/formio/tests/test_dynamic_config.py @@ -0,0 +1,292 @@ +from unittest.mock import Mock, patch + +from django.test import TestCase + +from rest_framework.test import APIRequestFactory + +from openforms.config.models import GlobalConfiguration +from openforms.config.tests.factories import MapTileLayerFactory +from openforms.formio.datastructures import FormioConfigurationWrapper +from openforms.formio.dynamic_config import ( + rewrite_formio_components, + rewrite_formio_components_for_request, +) +from openforms.submissions.tests.factories import SubmissionFactory + +rf = APIRequestFactory() + + +class DynamicConfigTests(TestCase): + @patch("openforms.formio.components.vanilla.GlobalConfiguration.get_solo") + def test_map_without_default_map_config(self, m_solo: Mock): + m_solo.return_value = GlobalConfiguration( + form_map_default_zoom_level="8", + form_map_default_latitude="55.123", + form_map_default_longitude="56.456", + ) + + configuration = { + "components": [ + { + "type": "map", + "key": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + } + ] + } + formio_configuration = FormioConfigurationWrapper(configuration) + submission = SubmissionFactory.create() + rewrite_formio_components(formio_configuration, submission) + + request = rf.get("/dummy") + rewrite_formio_components_for_request(formio_configuration, request) + + expected = { + "type": "map", + "key": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + } + self.assertEqual(configuration["components"][0], expected) + + @patch("openforms.formio.components.vanilla.GlobalConfiguration.get_solo") + def test_map_with_default_map_config(self, m_solo: Mock): + m_solo.return_value = GlobalConfiguration( + form_map_default_zoom_level="8", + form_map_default_latitude="55.123", + form_map_default_longitude="56.456", + ) + + configuration = { + "components": [ + { + "type": "map", + "key": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": True, + } + ] + } + formio_configuration = FormioConfigurationWrapper(configuration) + submission = SubmissionFactory.create() + rewrite_formio_components(formio_configuration, submission) + + request = rf.get("/dummy") + rewrite_formio_components_for_request(formio_configuration, request) + + expected = { + "type": "map", + "key": "map", + "defaultZoom": "8", + "initialCenter": { + "lat": "55.123", + "lng": "56.456", + }, + "useConfigDefaultMapSettings": True, + } + self.assertEqual(configuration["components"][0], expected) + + def test_map_without_tile_layer_identifier(self): + configuration = { + "components": [ + { + "type": "map", + "key": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "tileLayerIdentifier": None, + } + ] + } + formio_configuration = FormioConfigurationWrapper(configuration) + submission = SubmissionFactory.create() + rewrite_formio_components(formio_configuration, submission) + + request = rf.get("/dummy") + rewrite_formio_components_for_request(formio_configuration, request) + + expected = { + "type": "map", + "key": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "tileLayerIdentifier": None, + } + self.assertEqual(configuration["components"][0], expected) + + def test_map_with_invalid_tile_layer_identifier(self): + configuration = { + "components": [ + { + "type": "map", + "key": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "tileLayerIdentifier": "", + } + ] + } + formio_configuration = FormioConfigurationWrapper(configuration) + submission = SubmissionFactory.create() + rewrite_formio_components(formio_configuration, submission) + + request = rf.get("/dummy") + rewrite_formio_components_for_request(formio_configuration, request) + + expected = { + "type": "map", + "key": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "tileLayerIdentifier": "", + } + self.assertEqual(configuration["components"][0], expected) + + def test_map_with_valid_unknown_tile_layer_identifier(self): + configuration = { + "components": [ + { + "type": "map", + "key": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "tileLayerIdentifier": "identifier", + } + ] + } + formio_configuration = FormioConfigurationWrapper(configuration) + submission = SubmissionFactory.create() + rewrite_formio_components(formio_configuration, submission) + + request = rf.get("/dummy") + rewrite_formio_components_for_request(formio_configuration, request) + + expected = { + "type": "map", + "key": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "tileLayerIdentifier": "identifier", + } + self.assertEqual(configuration["components"][0], expected) + + def test_map_with_valid_known_tile_layer_identifier(self): + map = MapTileLayerFactory.create(identifier="identifier") + configuration = { + "components": [ + { + "type": "map", + "key": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "tileLayerIdentifier": "identifier", + } + ] + } + formio_configuration = FormioConfigurationWrapper(configuration) + submission = SubmissionFactory.create() + rewrite_formio_components(formio_configuration, submission) + + request = rf.get("/dummy") + rewrite_formio_components_for_request(formio_configuration, request) + + expected = { + "type": "map", + "key": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "tileLayerIdentifier": "identifier", + "tileLayerUrl": map.url, + } + self.assertEqual(configuration["components"][0], expected) + + @patch("openforms.formio.components.vanilla.GlobalConfiguration.get_solo") + def test_map_with_valid_known_tile_layer_identifier_and_use_config_default_map_settings( + self, m_solo: Mock + ): + m_solo.return_value = GlobalConfiguration( + form_map_default_zoom_level="8", + form_map_default_latitude="55.123", + form_map_default_longitude="56.456", + ) + map = MapTileLayerFactory.create(identifier="identifier") + configuration = { + "components": [ + { + "type": "map", + "key": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": True, + "tileLayerIdentifier": "identifier", + } + ] + } + formio_configuration = FormioConfigurationWrapper(configuration) + submission = SubmissionFactory.create() + rewrite_formio_components(formio_configuration, submission) + + request = rf.get("/dummy") + rewrite_formio_components_for_request(formio_configuration, request) + + expected = { + "type": "map", + "key": "map", + "defaultZoom": "8", + "initialCenter": { + "lat": "55.123", + "lng": "56.456", + }, + "useConfigDefaultMapSettings": True, + "tileLayerIdentifier": "identifier", + "tileLayerUrl": map.url, + } + self.assertEqual(configuration["components"][0], expected)