diff --git a/src/openforms/config/tests/factories.py b/src/openforms/config/tests/factories.py index 4cec877d42..ff3ec56078 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 LeafletMapBackgroundFactory(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.LeafletMapBackground" diff --git a/src/openforms/config/tests/test_admin.py b/src/openforms/config/tests/test_admin.py index cc924ed832..8a1aa889e9 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 LeafletMapBackgroundFactory, 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 LeafletMapBackgroundTests(WebTest): + def test_leaflet_map_background_changelist(self): + LeafletMapBackgroundFactory.create_batch(9) + url = reverse("admin:config_leafletmapbackground_changelist") + user = SuperUserFactory.create() + + response = self.app.get(url, user=user) + + self.assertEqual(response.status_code, 200) + + def test_leaflet_map_background_detail(self): + map = LeafletMapBackgroundFactory.create() + url = reverse("admin:config_leafletmapbackground_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_rewrite_for_request.py b/src/openforms/formio/tests/test_rewrite_for_request.py new file mode 100644 index 0000000000..74129b3c3e --- /dev/null +++ b/src/openforms/formio/tests/test_rewrite_for_request.py @@ -0,0 +1,239 @@ +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 LeafletMapBackgroundFactory +from openforms.formio.components.custom import Map +from openforms.formio.registry import ComponentRegistry + +rf = APIRequestFactory() + + +class RewriteForRequestTests(TestCase): + @patch("openforms.formio.components.vanilla.GlobalConfiguration.get_solo") + def test_map_without_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", + ) + + component = { + "type": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + } + + register = ComponentRegistry() + register("map")(Map) + request = rf.get("/dummy") + register.update_config_for_request(component, request) + + expected = { + "type": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + } + self.assertEqual(component, expected) + + @patch("openforms.formio.components.vanilla.GlobalConfiguration.get_solo") + def test_map_with_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", + ) + + component = { + "type": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": True, + } + + register = ComponentRegistry() + register("map")(Map) + request = rf.get("/dummy") + register.update_config_for_request(component, request) + + expected = { + "type": "map", + "defaultZoom": "8", + "initialCenter": { + "lat": "55.123", + "lng": "56.456", + }, + "useConfigDefaultMapSettings": True, + } + self.assertEqual(component, expected) + + def test_map_without_background_identifier(self): + component = { + "type": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "backgroundIdentifier": None, + } + + register = ComponentRegistry() + register("map")(Map) + request = rf.get("/dummy") + register.update_config_for_request(component, request) + + expected = { + "type": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "backgroundIdentifier": None, + } + self.assertEqual(component, expected) + + def test_map_with_invalid_background_identifier(self): + component = { + "type": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "backgroundIdentifier": "", + } + + register = ComponentRegistry() + register("map")(Map) + request = rf.get("/dummy") + register.update_config_for_request(component, request) + + expected = { + "type": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "backgroundIdentifier": "", + } + self.assertEqual(component, expected) + + def test_map_with_valid_unknown_background_identifier(self): + component = { + "type": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "backgroundIdentifier": "identifier", + } + + register = ComponentRegistry() + register("map")(Map) + request = rf.get("/dummy") + register.update_config_for_request(component, request) + + expected = { + "type": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "backgroundIdentifier": "identifier", + } + self.assertEqual(component, expected) + + def test_map_with_valid_known_background_identifier(self): + leafletMap = LeafletMapBackgroundFactory.create(identifier="identifier") + component = { + "type": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "backgroundIdentifier": "identifier", + } + + register = ComponentRegistry() + register("map")(Map) + request = rf.get("/dummy") + register.update_config_for_request(component, request) + + expected = { + "type": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": False, + "backgroundIdentifier": "identifier", + "url": leafletMap.url, + } + self.assertEqual(component, expected) + + @patch("openforms.formio.components.vanilla.GlobalConfiguration.get_solo") + def test_map_with_valid_known_background_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", + ) + leafletMap = LeafletMapBackgroundFactory.create(identifier="identifier") + component = { + "type": "map", + "defaultZoom": "3", + "initialCenter": { + "lat": "43.23", + "lng": "41.23", + }, + "useConfigDefaultMapSettings": True, + "backgroundIdentifier": "identifier", + } + + register = ComponentRegistry() + register("map")(Map) + request = rf.get("/dummy") + register.update_config_for_request(component, request) + + expected = { + "type": "map", + "defaultZoom": "8", + "initialCenter": { + "lat": "55.123", + "lng": "56.456", + }, + "useConfigDefaultMapSettings": True, + "backgroundIdentifier": "identifier", + "url": leafletMap.url, + } + self.assertEqual(component, expected)