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 3, 2024
1 parent aa7736d commit 3f7f899
Show file tree
Hide file tree
Showing 3 changed files with 270 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 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"
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 LeafletMapBackgroundFactory, 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 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)
239 changes: 239 additions & 0 deletions src/openforms/formio/tests/test_rewrite_for_request.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3f7f899

Please sign in to comment.