Skip to content

Commit

Permalink
✅ [#2173] Added tests for map component
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmolen committed Dec 11, 2024
1 parent 5e4a3b2 commit 9636d7d
Show file tree
Hide file tree
Showing 3 changed files with 323 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/openforms/config/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
23 changes: 22 additions & 1 deletion src/openforms/config/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from openforms.accounts.tests.factories import SuperUserFactory

from .factories import RichTextColorFactory
from .factories import MapTileLayerFactory, RichTextColorFactory


@disable_admin_mfa()
Expand All @@ -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)
292 changes: 292 additions & 0 deletions src/openforms/formio/tests/test_dynamic_config.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 9636d7d

Please sign in to comment.