Skip to content

Commit

Permalink
✅ [#5065] Add tests
Browse files Browse the repository at this point in the history
* Add test for a (multiple) file component inside an edit grid component. Ensures the attachments are properly encoded.
  • Loading branch information
viktorvanwijk committed Feb 4, 2025
1 parent c7d7f93 commit 5644149
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
interactions:
- request:
body: '"{\"values\": {\"repeatingGroup\": [{\"fileUploadInRepeating\": [{\"file_name\":
\"test_file.txt\", \"content\": \"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu\"}, {\"file_name\":
\"test_file_2.txt\", \"content\": \"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQgMi4=\"}]},
{\"fileUploadInRepeating\": [{\"file_name\": \"test_file_3.txt\", \"content\":
\"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQgMy4=\"}]}]}, \"schema\": {\"$schema\": \"https://json-schema.org/draft/2020-12/schema\",
\"type\": \"object\", \"properties\": {\"repeatingGroup\": {\"title\": \"Repeating
Group\", \"type\": \"array\", \"items\": {\"type\": \"object\", \"properties\":
{\"fileUploadInRepeating\": {\"title\": \"File upload\", \"type\": \"array\",
\"items\": {\"type\": \"object\", \"properties\": {\"file_name\": {\"type\":
\"string\"}, \"content\": {\"type\": \"string\", \"format\": \"base64\"}}, \"required\":
[\"file_name\", \"content\"], \"additionalProperties\": false}}}, \"required\":
[\"fileUploadInRepeating\"], \"additionalProperties\": false}}}, \"required\":
[\"repeatingGroup\"], \"additionalProperties\": false}}"'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Authorization:
- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3Mzg2ODUwNDEsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.TR9VUmpzO52JTC699ibQF8xxbo2Cii9FKak5Bv88Upg
Connection:
- keep-alive
Content-Length:
- '1071'
Content-Type:
- application/json
User-Agent:
- python-requests/2.32.2
method: POST
uri: http://localhost/json_plugin
response:
body:
string: "{\n \"data\": {\n \"schema\": {\n \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n
\ \"additionalProperties\": false,\n \"properties\": {\n \"repeatingGroup\":
{\n \"items\": {\n \"additionalProperties\": false,\n
\ \"properties\": {\n \"fileUploadInRepeating\": {\n
\ \"items\": {\n \"additionalProperties\":
false,\n \"properties\": {\n \"content\":
{\n \"format\": \"base64\",\n \"type\":
\"string\"\n },\n \"file_name\": {\n
\ \"type\": \"string\"\n }\n },\n
\ \"required\": [\n \"file_name\",\n \"content\"\n
\ ],\n \"type\": \"object\"\n },\n
\ \"title\": \"File upload\",\n \"type\": \"array\"\n
\ }\n },\n \"required\": [\n \"fileUploadInRepeating\"\n
\ ],\n \"type\": \"object\"\n },\n \"title\":
\"Repeating Group\",\n \"type\": \"array\"\n }\n },\n
\ \"required\": [\n \"repeatingGroup\"\n ],\n \"type\":
\"object\"\n },\n \"values\": {\n \"repeatingGroup\": [\n {\n
\ \"fileUploadInRepeating\": [\n {\n \"content\":
\"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu\",\n \"file_name\": \"test_file.txt\"\n
\ },\n {\n \"content\": \"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQgMi4=\",\n
\ \"file_name\": \"test_file_2.txt\"\n }\n ]\n
\ },\n {\n \"fileUploadInRepeating\": [\n {\n
\ \"content\": \"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQgMy4=\",\n \"file_name\":
\"test_file_3.txt\"\n }\n ]\n }\n ]\n }\n
\ },\n \"message\": \"Data received\"\n}\n"
headers:
Connection:
- close
Content-Length:
- '1880'
Content-Type:
- application/json
Date:
- Tue, 04 Feb 2025 16:04:01 GMT
Server:
- Werkzeug/3.1.3 Python/3.12.8
status:
code: 201
message: CREATED
version: 1
188 changes: 188 additions & 0 deletions src/openforms/registrations/contrib/json_dump/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,191 @@ def test_nested_component_key(self):
result["api_response"]["data"]["schema"]["properties"]["foo.bar"]["type"],
"string",
)

def test_file_in_edit_grid_component(self):
submission = SubmissionFactory.from_components(
[
{
"key": "repeatingGroup",
"label": "Repeating Group",
"type": "editgrid",
"components": [
{
"key": "fileUploadInRepeating",
"label": "File upload",
"type": "file",
"multiple": True,
},
],
}
],
completed=True,
submitted_data={
"repeatingGroup": [
{
"fileUploadInRepeating": [
{
"url": "http://example.com/api/v2/submissions/files/1",
"data": {
"url": "https://example.com/api/v2/submissions/files/1",
"form": "",
"name": "test_file.txt",
"size": 29,
"baseUrl": "http://example.com/api/v2/",
"project": "",
},
"name": "test_file.txt",
"size": 29,
"type": "text/plain",
"storage": "url",
"originalName": "test_file.txt",
},
{
"url": "https://example.com/api/v2/submissions/files/2",
"data": {
"url": "https://example.com/api/v2/submissions/files/2",
"form": "",
"name": "test_file_2.txt",
"size": 29,
"baseUrl": "http://example.com/api/v2/",
"project": "",
},
"name": "test_file_2.txt",
"size": 29,
"type": "text/plain",
"storage": "url",
"originalName": "test_file_2.txt",
},
],
},
{
"fileUploadInRepeating": [
{
"url": "https://example.com/api/v2/submissions/files/3",
"data": {
"url": "https://example.com/api/v2/submissions/files/3",
"form": "",
"name": "test_file_3.txt",
"size": 29,
"baseUrl": "http://example.com/api/v2/",
"project": "",
},
"name": "test_file_3.txt",
"size": 29,
"type": "text/plain",
"storage": "url",
"originalName": "test_file_3.txt",
}
],
},
]
},
bsn="123456789",
with_public_registration_reference=True,
)

SubmissionFileAttachmentFactory.create(
form_key="repeatingGroup",
submission_step=submission.steps[0],
file_name="test_file.txt",
original_name="test_file.txt",
content_type="application/text",
content__data=b"This is example content.",
_component_data_path="repeatingGroup.0.fileUploadInRepeating",
)

SubmissionFileAttachmentFactory.create(
form_key="repeatingGroup",
submission_step=submission.steps[0],
file_name="test_file_2.txt",
original_name="test_file_2.txt",
content_type="application/text",
content__data=b"This is example content 2.",
_component_data_path="repeatingGroup.0.fileUploadInRepeating",
)

SubmissionFileAttachmentFactory.create(
form_key="repeatingGroup",
submission_step=submission.steps[0],
file_name="test_file_3.txt",
original_name="test_file_3.txt",
content_type="application/text",
content__data=b"This is example content 3.",
_component_data_path="repeatingGroup.1.fileUploadInRepeating",
)

options: JSONDumpOptions = {
"service": self.service,
"path": "json_plugin",
"variables": ["repeatingGroup"],
}
json_plugin = JSONDumpRegistration("json_registration_plugin")

result = json_plugin.register_submission(submission, options)
assert result is not None

expected_values = {
"repeatingGroup": [
{
"fileUploadInRepeating": [
{
"content": "VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu",
"file_name": "test_file.txt",
},
{
"content": "VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQgMi4=",
"file_name": "test_file_2.txt",
},
]
},
{
"fileUploadInRepeating": [
{
"content": "VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQgMy4=",
"file_name": "test_file_3.txt",
},
]
},
]
}
expected_schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"additionalProperties": False,
"properties": {
"repeatingGroup": {
"items": {
"additionalProperties": False,
"properties": {
"fileUploadInRepeating": {
"items": {
"additionalProperties": False,
"properties": {
"content": {
"format": "base64",
"type": "string",
},
"file_name": {"type": "string"},
},
"required": ["file_name", "content"],
"type": "object",
},
"title": "File upload",
"type": "array",
}
},
"required": ["fileUploadInRepeating"],
"type": "object",
},
"title": "Repeating Group",
"type": "array",
}
},
"required": ["repeatingGroup"],
"type": "object",
}

with self.subTest("values"):
self.assertEqual(result["api_response"]["data"]["values"], expected_values)

with self.subTest("schema"):
self.assertEqual(result["api_response"]["data"]["schema"], expected_schema)

0 comments on commit 5644149

Please sign in to comment.