Skip to content

Commit

Permalink
✅ [#4980] Update tests
Browse files Browse the repository at this point in the history
In the JSON dump docker app, explicitly load the received data before sending again. This prevents the data from being interpreted as a string instead of a JSON object.
  • Loading branch information
viktorvanwijk committed Jan 20, 2025
1 parent 273a533 commit 792dedd
Show file tree
Hide file tree
Showing 18 changed files with 536 additions and 106 deletions.
7 changes: 5 additions & 2 deletions docker/json-dump/app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from flask import Flask, jsonify, request
import json

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route("/json_plugin", methods=["POST"])
def json_plugin_post():
data = request.get_json()

app.logger.info(f"Data received: {data}")

message = "No data" if data is None else "Data received"
return jsonify({"message": message, "data": data}), 201
return jsonify({"message": message, "data": json.loads(data)}), 201


@app.route("/test_connection", methods=["GET"])
Expand Down
1 change: 1 addition & 0 deletions src/openforms/formio/components/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from ..typing import Component


def _normalize_pattern(pattern: str) -> str:
"""
Normalize a regex pattern so that it matches from beginning to the end of the value.
Expand Down
34 changes: 22 additions & 12 deletions src/openforms/formio/tests/test_component_json_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from openforms.formio.typing import (
AddressNLComponent,
Component,
ContentComponent,
DateComponent,
DatetimeComponent,
EditGridComponent,
Expand Down Expand Up @@ -80,7 +81,7 @@ def test_checkbox(self):

def test_content(self):
component_class = Content
component = {
component: ContentComponent = {
"label": "Content",
"key": "content",
"html": "ASDF",
Expand Down Expand Up @@ -303,13 +304,14 @@ def test_postcode(self):
class RadioTests(TestCase):

def test_manual_data_source(self):
component = {
component: RadioComponent = {
"label": "Radio label",
"key": "radio",
"values": [
{"label": "A", "value": "a"},
{"label": "B", "value": "b"},
],
"type": "radio",
}

expected_schema = {
Expand All @@ -321,25 +323,25 @@ def test_manual_data_source(self):
self.assertEqual(expected_schema, schema)

def test_data_source_is_another_form_variable(self):
component = {
component: RadioComponent = {
"label": "Radio label",
"key": "radio",
"values": [
{"label": "", "value": ""},
],
"openForms": {"dataSrc": "variable"},
"type": "radio",
}

expected_schema = {"title": "Radio label", "type": "string"}
schema = Radio.as_json_schema(component)
self.assertEqual(expected_schema, schema)



class SelectTests(TestCase):

def test_manual_data_source(self):
component = {
component: SelectComponent = {
"label": "Select label",
"key": "select",
"data": {
Expand All @@ -353,7 +355,9 @@ def test_manual_data_source(self):

with self.subTest("single"):
expected_schema = {
"title": "Select label", "type": "string", "enum": ["a", "b", ""]
"title": "Select label",
"type": "string",
"enum": ["a", "b", ""],
}
schema = Select.as_json_schema(component)
self.assertEqual(schema, expected_schema)
Expand All @@ -369,7 +373,7 @@ def test_manual_data_source(self):
self.assertEqual(schema, expected_schema)

def test_data_source_is_another_form_variable(self):
component = {
component: SelectComponent = {
"label": "Select label",
"key": "select",
"data": {
Expand All @@ -389,21 +393,24 @@ def test_data_source_is_another_form_variable(self):
with self.subTest("multiple"):
component["multiple"] = True
expected_schema = {
"title": "Select label", "type": "array", "items": {"type": "string"}
"title": "Select label",
"type": "array",
"items": {"type": "string"},
}
schema = Select.as_json_schema(component)
self.assertEqual(schema, expected_schema)


class SelectBoxesTests(TestCase):
def test_manual_data_source(self):
component = {
component: SelectBoxesComponent = {
"label": "Select boxes label",
"key": "selectBoxes",
"values": [
{"label": "A", "value": "a"},
{"label": "B", "value": "b"},
]
],
"type": "selectboxes",
}

expected_schema = {
Expand All @@ -420,17 +427,20 @@ def test_manual_data_source(self):
self.assertEqual(schema, expected_schema)

def test_data_source_is_another_form_variable(self):
component = {
component: SelectBoxesComponent = {
"label": "Select boxes label",
"key": "selectBoxes",
"values": [
{"label": "", "value": ""},
],
"openForms": {"dataSrc": "variable"},
"type": "selectboxes",
}

expected_schema = {
"title": "Select boxes label", "type": "object", "additionalProperties": True
"title": "Select boxes label",
"type": "object",
"additionalProperties": True,
}
schema = SelectBoxes.as_json_schema(component)
self.assertEqual(schema, expected_schema)
114 changes: 109 additions & 5 deletions src/openforms/forms/tests/test_form_to_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
FormDefinitionFactory,
FormFactory,
FormStepFactory,
FormVariableFactory,
)
from openforms.forms.utils import form_variables_to_json_schema
from openforms.forms.utils import (
form_variables_to_json_schema,
get_json_schema_from_form_variable,
is_form_variable_required,
)
from openforms.variables.constants import FormVariableDataTypes, FormVariableSources


class FormToJsonSchemaTestCase(TestCase):
def test_form_to_json_schema(self):
def test_correct_variables_included_in_schema(self):
form = FormFactory.create()
form_def_1 = FormDefinitionFactory.create(
configuration={
Expand Down Expand Up @@ -59,16 +65,26 @@ def test_form_to_json_schema(self):
]
}
)
form_step_1 = FormStepFactory.create(form=form, form_definition=form_def_1)
FormStepFactory.create(form=form, form_definition=form_def_1)

form_def_2 = FormDefinitionFactory.create(
configuration={
"components": [
{"key": "file", "type": "file", "label": "File"},
{
"key": "file",
"type": "file",
"label": "File",
"validate": {"required": True},
},
{
"key": "notIncluded",
"type": "textfield",
"label": "Not included text field",
},
]
}
)
form_step_2 = FormStepFactory.create(form=form, form_definition=form_def_2)
FormStepFactory.create(form=form, form_definition=form_def_2)

vars_to_include = (
"firstName",
Expand All @@ -81,3 +97,91 @@ def test_form_to_json_schema(self):
"today",
)
schema = form_variables_to_json_schema(form, vars_to_include)

self.assertEqual(set(schema["properties"]), set(vars_to_include))
self.assertEqual(schema["required"], ["auth_bsn", "today", "file"])


class TestGetJsonSchemaFromFormVariableTests(TestCase):

def test_component(self):
form = FormFactory.create()
form_def = FormDefinitionFactory.create(
configuration={
"components": [
{
"key": "textfield",
"type": "textfield",
"label": "Foo",
"source": FormVariableSources.component,
},
]
}
)
FormStepFactory.create(form=form, form_definition=form_def)

var = form_def.formvariable_set.first()
schema = get_json_schema_from_form_variable(var)

expected_schema = {"title": "Foo", "type": "string"}
self.assertEqual(schema, expected_schema)

def test_user_defined(self):
var = FormVariableFactory.create(
name="Foo",
key="foo",
source=FormVariableSources.user_defined,
data_type=FormVariableDataTypes.array,
initial_value=["A", "B", "C"],
)

schema = get_json_schema_from_form_variable(var)

expected_schema = {"title": "Foo", "type": "array"}
self.assertEqual(schema, expected_schema)


class TestIsFormVariableRequired(TestCase):

def test_component_default(self):
form = FormFactory.create()
form_def = FormDefinitionFactory.create(
configuration={
"components": [
{
"key": "textfield",
"type": "textfield",
"label": "Foo",
"source": FormVariableSources.component,
},
]
}
)
FormStepFactory.create(form=form, form_definition=form_def)

var = form_def.formvariable_set.first()
required = is_form_variable_required(var)

self.assertFalse(required)

def test_component_required(self):
form = FormFactory.create()
form_def = FormDefinitionFactory.create(
configuration={
"components": [
{
"key": "textfield",
"type": "textfield",
"label": "Foo",
"source": FormVariableSources.component,
"validate": {"required": True},
},
]
}
)
FormStepFactory.create(form=form, form_definition=form_def)

var = form_def.formvariable_set.first()
required = is_form_variable_required(var)

self.assertTrue(required)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from openforms.accounts.tests.factories import SuperUserFactory
from openforms.authentication.service import AuthAttribute
from openforms.typing import JSONObject
from openforms.variables.base import BaseStaticVariable
from openforms.variables.constants import FormVariableDataTypes, FormVariableSources
from openforms.variables.registry import Registry
Expand All @@ -22,6 +23,10 @@ class DemoNow(BaseStaticVariable):
def get_initial_value(self, *args, **kwargs):
return "2021-07-16T21:15:00+00:00"

@staticmethod
def as_json_schema():
return {}


class DemoAuth(BaseStaticVariable):
name = "Authentication identifier"
Expand All @@ -34,6 +39,10 @@ def get_initial_value(self, *args, **kwargs):
"value": "123456782",
}

@staticmethod
def as_json_schema():
return {}


register = Registry()
register("now")(DemoNow)
Expand Down
7 changes: 3 additions & 4 deletions src/openforms/registrations/contrib/json_dump/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
from glom import assign, glom
from zgw_consumers.client import build_client

from openforms.formio.dynamic_config import rewrite_formio_components
from openforms.formio.components.utils import to_multiple
from openforms.formio.dynamic_config import rewrite_formio_components
from openforms.formio.typing import Component
from openforms.forms.utils import to_json
from openforms.forms.utils import form_variables_to_json_schema
from openforms.forms.utils import form_variables_to_json_schema, to_json
from openforms.submissions.logic.datastructures import DataContainer
from openforms.submissions.models import (
Submission,
Expand Down Expand Up @@ -208,7 +207,7 @@ def encode_attachment(attachment: SubmissionFileAttachment) -> str:


def get_component(
variable: SubmissionValueVariable, submission : Submission
variable: SubmissionValueVariable, submission: Submission
) -> Component | None:
"""Get the component from a submission value variable.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
interactions:
- request:
body: '{"values": {"auth_bsn": "123456789", "firstName": "We Are"}, "schema":
{"$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object",
"properties": {"static_var_1": {"type": "string", "pattern": "^cool_pattern$"},
"form_var_1": {"type": "string"}, "form_var_2": {"type": "string"}, "attachment":
{"type": "string", "contentEncoding": "base64"}}, "required": ["static_var_1",
"form_var_1", "form_var_2"], "additionalProperties": false}}'
body: '"{\"values\": {\"auth_bsn\": \"123456789\", \"firstName\": \"We Are\"},
\"schema\": {\"$schema\": \"https://json-schema.org/draft/2020-12/schema\",
\"type\": \"object\", \"properties\": {\"auth_bsn\": {\"title\": \"BSN\", \"description\":
\"Uniquely identifies the authenticated person. This value follows the rules
for Dutch social security numbers.\", \"type\": \"string\", \"pattern\": \"^\\\\d{9}$\",
\"format\": \"nl-bsn\"}, \"firstName\": {\"title\": \"Firstname\", \"type\":
\"string\"}}, \"required\": [\"auth_bsn\"], \"additionalProperties\": false}}"'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Authorization:
- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3MzcwMzM3OTcsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.ZE3A4AQZ8omE_KsvI_LcDSMrmBa9-AJoUiiAdjhtSqo
- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3MzczODQzMTgsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.nphs3uH7g9ZebNyJz3HhM5_fMld0N4MgD_01_MTgL8k
Connection:
- keep-alive
Content-Length:
- '450'
- '560'
Content-Type:
- application/json
User-Agent:
Expand Down Expand Up @@ -45,7 +46,7 @@ interactions:
Content-Type:
- text/html; charset=utf-8
Date:
- Thu, 16 Jan 2025 13:23:17 GMT
- Mon, 20 Jan 2025 14:45:18 GMT
Server:
- Werkzeug/3.1.3 Python/3.12.8
status:
Expand Down
Loading

0 comments on commit 792dedd

Please sign in to comment.