Skip to content

Commit

Permalink
🐛 [#2193] Fix failing WebTests due to django-jsonform
Browse files Browse the repository at this point in the history
django-jsonform requires JS to work properly and in WebTest the ArrayFields get an empty string as their default value, which causes the form to crash because it tries to parse the value as JSON
  • Loading branch information
stevenbal committed Mar 12, 2024
1 parent b90b562 commit 2b5b2bc
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/open_inwoner/configurations/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def setUp(self):
self.form["secondary_font_color"] = "#FFFFFF"
self.form["accent_color"] = "#FFFFFF"
self.form["accent_font_color"] = "#FFFFFF"
# django-jsonform requires JS to work properly and with Webtest the default
# value for ArrayFields is an empty string, causing it crash to when trying to parse
# that value as JSON
self.form["recipients_email_digest"] = "[]"

def test_valid_path_is_saved(self):
config = SiteConfiguration.get_solo()
Expand Down
4 changes: 4 additions & 0 deletions src/open_inwoner/configurations/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def test_contrast_is_checked(self):
form["secondary_font_color"] = "#FFFFFF"
form["accent_color"] = "#FFFFFF"
form["accent_font_color"] = "#FFFFFF"
# django-jsonform requires JS to work properly and with Webtest the default
# value for ArrayFields is an empty string, causing it crash to when trying to parse
# that value as JSON
form["recipients_email_digest"] = "[]"
response = form.submit("_continue").follow()

messages = list(response.context["messages"])
Expand Down
5 changes: 5 additions & 0 deletions src/open_inwoner/configurations/tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def setUp(self):
reverse("admin:configurations_siteconfiguration_change"), user=self.user
).forms["siteconfiguration_form"]

# django-jsonform requires JS to work properly and with Webtest the default
# value for ArrayFields is an empty string, causing it crash to when trying to parse
# that value as JSON
self.form["recipients_email_digest"] = "[]"

def test_upload_font_correct_filetype(self):
font_file = Upload("valid.ttf", b"content", content_type="font/ttf")
self.form["name"] = "Test"
Expand Down
2 changes: 1 addition & 1 deletion src/open_inwoner/pdc/admin/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def zaaktypen_select_schema() -> dict:


class CategoryAdminForm(movenodeform_factory(Category)):
zaaktypen = JSONFormField(schema=zaaktypen_select_schema)
zaaktypen = JSONFormField(schema=zaaktypen_select_schema, required=False)

class Meta:
model = Category
Expand Down
13 changes: 11 additions & 2 deletions src/open_inwoner/pdc/tests/test_category_admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from unittest.mock import patch

from django.contrib.auth.models import Permission
Expand Down Expand Up @@ -28,6 +29,10 @@ def test_user_can_publish_root_category_on_add_form(self):
form["published"] = True
form["_position"] = "first-child"
form["_ref_node_id"] = 0
# django-jsonform requires JS to work properly and with Webtest the default
# value for ArrayFields is an empty string, causing it crash to when trying to parse
# that value as JSON
form["zaaktypen"] = "[]"
form.submit()
category = Category.objects.first()
self.assertEqual(category.slug, "foo1")
Expand All @@ -43,6 +48,10 @@ def test_user_can_publish_child_category_with_root_published_on_add_form(self):
form["published"] = True
form["_position"] = "first-child"
form["_ref_node_id"] = root.id
# django-jsonform requires JS to work properly and with Webtest the default
# value for ArrayFields is an empty string, causing it crash to when trying to parse
# that value as JSON
form["zaaktypen"] = "[]"
form.submit()
updated_category = Category.objects.get(slug="bar1")
self.assertTrue(updated_category.published)
Expand Down Expand Up @@ -178,7 +187,7 @@ def test_user_can_link_zaaktypen_if_category_filtering_with_zaken_feature_flag_e
user=self.user,
).form

form["zaaktypen"] = "001"
form["zaaktypen"] = json.dumps(["001"])
response = form.submit("_save")

self.assertEqual(response.status_code, 302)
Expand All @@ -201,7 +210,7 @@ def test_user_cannot_link_zaaktypen_if_category_filtering_with_zaken_feature_fla
user=self.user,
).form

form["zaaktypen"] = "001"
form["zaaktypen"] = json.dumps(["001"])
response = form.submit()

self.assertEqual(response.status_code, 200)
Expand Down
10 changes: 9 additions & 1 deletion src/open_inwoner/pdc/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ def test_addition(self):
form = response.forms["category_form"]
form["name"] = self.category.name
form["slug"] = self.category.slug
# django-jsonform requires JS to work properly and with Webtest the default
# value for ArrayFields is an empty string, causing it crash to when trying to parse
# that value as JSON
form["zaaktypen"] = "[]"
form.submit()
category = Category.objects.filter(slug=self.category.slug).first()
log_entry = TimelineLog.objects.last()
Expand All @@ -201,6 +205,10 @@ def test_change(self):
)
form = response.forms["category_form"]
form["description"] = "Updated description"
# django-jsonform requires JS to work properly and with Webtest the default
# value for ArrayFields is an empty string, causing it crash to when trying to parse
# that value as JSON
form["zaaktypen"] = "[]"
form.submit()
log_entry = TimelineLog.objects.last()

Expand All @@ -212,7 +220,7 @@ def test_change(self):
self.assertEqual(
log_entry.extra_data,
{
"message": "Omschrijving, Ten opzichte van and Zaaktypen gewijzigd.",
"message": "Omschrijving and Ten opzichte van gewijzigd.",
"action_flag": [2, "Change"],
"content_object_repr": category.name,
},
Expand Down

0 comments on commit 2b5b2bc

Please sign in to comment.