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 3b6332a commit d92b313
Show file tree
Hide file tree
Showing 15 changed files with 504 additions and 102 deletions.
5 changes: 3 additions & 2 deletions docker/json-dump/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Flask, jsonify, request
import json

from flask import Flask, jsonify, request

app = Flask(__name__)

Expand All @@ -8,7 +9,7 @@ def json_plugin_post():
data = request.get_json()

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
21 changes: 13 additions & 8 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,13 +323,14 @@ 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"}
Expand All @@ -339,7 +342,7 @@ def test_data_source_is_another_form_variable(self):
class SelectTests(TestCase):

def test_manual_data_source(self):
component = {
component: SelectComponent = {
"label": "Select label",
"key": "select",
"data": {
Expand Down Expand Up @@ -369,7 +372,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 Down Expand Up @@ -397,13 +400,14 @@ def test_data_source_is_another_form_variable(self):

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,13 +424,14 @@ 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 = {
Expand Down
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)
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 d92b313

Please sign in to comment.