diff --git a/docker/json-dump/app.py b/docker/json-dump/app.py index e2e8b08bcb..963c91e03f 100644 --- a/docker/json-dump/app.py +++ b/docker/json-dump/app.py @@ -1,5 +1,6 @@ -from flask import Flask, jsonify, request +import json +from flask import Flask, jsonify, request app = Flask(__name__) @@ -7,8 +8,10 @@ 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"]) diff --git a/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_exception_raised_when_service_returns_unexpected_status_code.yaml b/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_exception_raised_when_service_returns_unexpected_status_code.yaml index 781d459570..874b327225 100644 --- a/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_exception_raised_when_service_returns_unexpected_status_code.yaml +++ b/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_exception_raised_when_service_returns_unexpected_status_code.yaml @@ -1,22 +1,23 @@ interactions: - request: - body: '{"values": {"firstName": "We Are", "auth_bsn": "123456789"}, "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\": {\"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}}"' headers: Accept: - '*/*' Accept-Encoding: - gzip, deflate, br Authorization: - - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3MzU4MjY2MzksImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.8PuPAIY6PI3_g4edfqzFFbHNldYxxRIBjPuAh-p00xk + - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3Mzc0Njk5MDAsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.VaaQtoX7c3ac2_Zf8GPvB8fAMYfmJft53c7qXNBY_lk Connection: - keep-alive Content-Length: - - '450' + - '516' Content-Type: - application/json User-Agent: @@ -45,7 +46,7 @@ interactions: Content-Type: - text/html; charset=utf-8 Date: - - Thu, 02 Jan 2025 14:03:59 GMT + - Tue, 21 Jan 2025 14:31:40 GMT Server: - Werkzeug/3.1.3 Python/3.12.8 status: diff --git a/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_multiple_file_uploads.yaml b/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_multiple_file_uploads.yaml new file mode 100644 index 0000000000..da0de50808 --- /dev/null +++ b/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_multiple_file_uploads.yaml @@ -0,0 +1,56 @@ +interactions: +- request: + body: '"{\"values\": {\"file\": [{\"file_name\": \"file1.txt\", \"content\": \"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu\"}, + {\"file_name\": \"file2.txt\", \"content\": \"Q29udGVudCBleGFtcGxlIGlzIHRoaXMu\"}]}, + \"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}}"' + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate, br + Authorization: + - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3Mzc0Njk5MDAsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.VaaQtoX7c3ac2_Zf8GPvB8fAMYfmJft53c7qXNBY_lk + Connection: + - keep-alive + Content-Length: + - '638' + 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 \"attachment\": + {\n \"contentEncoding\": \"base64\",\n \"type\": \"string\"\n + \ },\n \"form_var_1\": {\n \"type\": \"string\"\n },\n + \ \"form_var_2\": {\n \"type\": \"string\"\n },\n \"static_var_1\": + {\n \"pattern\": \"^cool_pattern$\",\n \"type\": \"string\"\n + \ }\n },\n \"required\": [\n \"static_var_1\",\n \"form_var_1\",\n + \ \"form_var_2\"\n ],\n \"type\": \"object\"\n },\n \"values\": + {\n \"file\": [\n {\n \"content\": \"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu\",\n + \ \"file_name\": \"file1.txt\"\n },\n {\n \"content\": + \"Q29udGVudCBleGFtcGxlIGlzIHRoaXMu\",\n \"file_name\": \"file2.txt\"\n + \ }\n ]\n }\n },\n \"message\": \"Data received\"\n}\n" + headers: + Connection: + - close + Content-Length: + - '923' + Content-Type: + - application/json + Date: + - Tue, 21 Jan 2025 14:31:40 GMT + Server: + - Werkzeug/3.1.3 Python/3.12.8 + status: + code: 201 + message: CREATED +version: 1 diff --git a/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_submission_with_json_dump_backend.yaml b/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_no_file_upload_for_multiple_files_component.yaml similarity index 60% rename from src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_submission_with_json_dump_backend.yaml rename to src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_no_file_upload_for_multiple_files_component.yaml index d2294549fc..af4ae5be94 100644 --- a/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_submission_with_json_dump_backend.yaml +++ b/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_no_file_upload_for_multiple_files_component.yaml @@ -1,22 +1,22 @@ interactions: - request: - body: '{"values": {"file": "VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu", "firstName": "We - Are", "auth_bsn": "123456789"}, "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\": {\"file\": []}, \"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}}"' headers: Accept: - '*/*' Accept-Encoding: - gzip, deflate, br Authorization: - - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3MzU4MjY2MzksImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.8PuPAIY6PI3_g4edfqzFFbHNldYxxRIBjPuAh-p00xk + - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3Mzc0Njk5MDAsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.VaaQtoX7c3ac2_Zf8GPvB8fAMYfmJft53c7qXNBY_lk Connection: - keep-alive Content-Length: - - '494' + - '474' Content-Type: - application/json User-Agent: @@ -33,17 +33,16 @@ interactions: {\n \"pattern\": \"^cool_pattern$\",\n \"type\": \"string\"\n \ }\n },\n \"required\": [\n \"static_var_1\",\n \"form_var_1\",\n \ \"form_var_2\"\n ],\n \"type\": \"object\"\n },\n \"values\": - {\n \"auth_bsn\": \"123456789\",\n \"file\": \"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu\",\n - \ \"firstName\": \"We Are\"\n }\n },\n \"message\": \"Data received\"\n}\n" + {\n \"file\": []\n }\n },\n \"message\": \"Data received\"\n}\n" headers: Connection: - close Content-Length: - - '783' + - '691' Content-Type: - application/json Date: - - Thu, 02 Jan 2025 14:03:59 GMT + - Tue, 21 Jan 2025 14:31:40 GMT Server: - Werkzeug/3.1.3 Python/3.12.8 status: diff --git a/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_no_file_upload_for_single_file_component.yaml b/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_no_file_upload_for_single_file_component.yaml new file mode 100644 index 0000000000..55f07cc3d8 --- /dev/null +++ b/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_no_file_upload_for_single_file_component.yaml @@ -0,0 +1,51 @@ +interactions: +- request: + body: '"{\"values\": {\"file\": null}, \"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}}"' + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate, br + Authorization: + - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3Mzc0Njk5MDAsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.VaaQtoX7c3ac2_Zf8GPvB8fAMYfmJft53c7qXNBY_lk + Connection: + - keep-alive + Content-Length: + - '476' + 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 \"attachment\": + {\n \"contentEncoding\": \"base64\",\n \"type\": \"string\"\n + \ },\n \"form_var_1\": {\n \"type\": \"string\"\n },\n + \ \"form_var_2\": {\n \"type\": \"string\"\n },\n \"static_var_1\": + {\n \"pattern\": \"^cool_pattern$\",\n \"type\": \"string\"\n + \ }\n },\n \"required\": [\n \"static_var_1\",\n \"form_var_1\",\n + \ \"form_var_2\"\n ],\n \"type\": \"object\"\n },\n \"values\": + {\n \"file\": null\n }\n },\n \"message\": \"Data received\"\n}\n" + headers: + Connection: + - close + Content-Length: + - '693' + Content-Type: + - application/json + Date: + - Tue, 21 Jan 2025 14:31:40 GMT + Server: + - Werkzeug/3.1.3 Python/3.12.8 + status: + code: 201 + message: CREATED +version: 1 diff --git a/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_one_file_upload_for_multiple_files_component.yaml b/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_one_file_upload_for_multiple_files_component.yaml new file mode 100644 index 0000000000..b129859841 --- /dev/null +++ b/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_one_file_upload_for_multiple_files_component.yaml @@ -0,0 +1,54 @@ +interactions: +- request: + body: '"{\"values\": {\"file\": [{\"file_name\": \"file1.txt\", \"content\": \"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu\"}]}, + \"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}}"' + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate, br + Authorization: + - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3Mzc0Njk5MDAsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.VaaQtoX7c3ac2_Zf8GPvB8fAMYfmJft53c7qXNBY_lk + Connection: + - keep-alive + Content-Length: + - '555' + 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 \"attachment\": + {\n \"contentEncoding\": \"base64\",\n \"type\": \"string\"\n + \ },\n \"form_var_1\": {\n \"type\": \"string\"\n },\n + \ \"form_var_2\": {\n \"type\": \"string\"\n },\n \"static_var_1\": + {\n \"pattern\": \"^cool_pattern$\",\n \"type\": \"string\"\n + \ }\n },\n \"required\": [\n \"static_var_1\",\n \"form_var_1\",\n + \ \"form_var_2\"\n ],\n \"type\": \"object\"\n },\n \"values\": + {\n \"file\": [\n {\n \"content\": \"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu\",\n + \ \"file_name\": \"file1.txt\"\n }\n ]\n }\n },\n + \ \"message\": \"Data received\"\n}\n" + headers: + Connection: + - close + Content-Length: + - '810' + Content-Type: + - application/json + Date: + - Tue, 21 Jan 2025 14:31:40 GMT + Server: + - Werkzeug/3.1.3 Python/3.12.8 + status: + code: 201 + message: CREATED +version: 1 diff --git a/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_submission_happy_flow.yaml b/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_submission_happy_flow.yaml new file mode 100644 index 0000000000..7eb76f66a9 --- /dev/null +++ b/src/openforms/registrations/contrib/json_dump/tests/files/vcr_cassettes/JSONDumpBackendTests/JSONDumpBackendTests.test_submission_happy_flow.yaml @@ -0,0 +1,56 @@ +interactions: +- request: + body: '"{\"values\": {\"auth_bsn\": \"123456789\", \"firstName\": \"We Are\", + \"file\": {\"file_name\": \"test_file.txt\", \"content\": \"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu\"}}, + \"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}}"' + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate, br + Authorization: + - Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3Mzc0Njk5MDAsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.VaaQtoX7c3ac2_Zf8GPvB8fAMYfmJft53c7qXNBY_lk + Connection: + - keep-alive + Content-Length: + - '613' + 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 \"attachment\": + {\n \"contentEncoding\": \"base64\",\n \"type\": \"string\"\n + \ },\n \"form_var_1\": {\n \"type\": \"string\"\n },\n + \ \"form_var_2\": {\n \"type\": \"string\"\n },\n \"static_var_1\": + {\n \"pattern\": \"^cool_pattern$\",\n \"type\": \"string\"\n + \ }\n },\n \"required\": [\n \"static_var_1\",\n \"form_var_1\",\n + \ \"form_var_2\"\n ],\n \"type\": \"object\"\n },\n \"values\": + {\n \"auth_bsn\": \"123456789\",\n \"file\": {\n \"content\": + \"VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu\",\n \"file_name\": \"test_file.txt\"\n + \ },\n \"firstName\": \"We Are\"\n }\n },\n \"message\": \"Data + received\"\n}\n" + headers: + Connection: + - close + Content-Length: + - '850' + Content-Type: + - application/json + Date: + - Tue, 21 Jan 2025 14:31:40 GMT + Server: + - Werkzeug/3.1.3 Python/3.12.8 + status: + code: 201 + message: CREATED +version: 1 diff --git a/src/openforms/registrations/contrib/json_dump/tests/test_backend.py b/src/openforms/registrations/contrib/json_dump/tests/test_backend.py index f2dbd51a3e..c9157efc74 100644 --- a/src/openforms/registrations/contrib/json_dump/tests/test_backend.py +++ b/src/openforms/registrations/contrib/json_dump/tests/test_backend.py @@ -7,13 +7,13 @@ from requests import RequestException from zgw_consumers.test.factories import ServiceFactory -from openforms.submissions.public_references import set_submission_reference from openforms.submissions.tests.factories import ( SubmissionFactory, SubmissionFileAttachmentFactory, ) from openforms.utils.tests.vcr import OFVCRMixin +from ..config import JSONDumpOptions from ..plugin import JSONDumpRegistration VCR_TEST_FILES = Path(__file__).parent / "files" @@ -22,10 +22,14 @@ class JSONDumpBackendTests(OFVCRMixin, TestCase): VCR_TEST_FILES = VCR_TEST_FILES - def test_submission_with_json_dump_backend(self): + @classmethod + def setUpTestData(cls): + cls.service = ServiceFactory.create(api_root="http://localhost:80/") + + def test_submission_happy_flow(self): submission = SubmissionFactory.from_components( [ - {"key": "firstName", "type": "textField"}, + {"key": "firstName", "type": "textfield"}, {"key": "lastName", "type": "textfield"}, {"key": "file", "type": "file"}, ], @@ -43,32 +47,36 @@ def test_submission_with_json_dump_backend(self): ], }, bsn="123456789", + with_public_registration_reference=True, ) SubmissionFileAttachmentFactory.create( form_key="file", submission_step=submission.submissionstep_set.get(), file_name="test_file.txt", + original_name="test_file.txt", content_type="application/text", content__data=b"This is example content.", _component_configuration_path="components.2", _component_data_path="file", ) - json_form_options = dict( - service=(ServiceFactory(api_root="http://localhost:80/")), - path="json_plugin", - variables=["firstName", "file", "auth_bsn"], - ) + options: JSONDumpOptions = { + "service": self.service, + "path": "json_plugin", + "variables": ["firstName", "file", "auth_bsn"], + } json_plugin = JSONDumpRegistration("json_registration_plugin") - set_submission_reference(submission) expected_response = { # Note that `lastName` is not included here as it wasn't specified in the variables "data": { "values": { "auth_bsn": "123456789", - "file": "VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu", # Content of the attachment encoded using base64 + "file": { + "file_name": "test_file.txt", + "content": "VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu", + }, "firstName": "We Are", }, "schema": { @@ -87,36 +95,37 @@ def test_submission_with_json_dump_backend(self): "message": "Data received", } - res = json_plugin.register_submission(submission, json_form_options) - res_json = res["api_response"].json() + result = json_plugin.register_submission(submission, options) - self.assertEqual(res_json, expected_response) + self.assertEqual(result["api_response"], expected_response) with self.subTest("attachment content encoded"): - decoded_content = b64decode(res_json["data"]["values"]["file"]) + decoded_content = b64decode( + result["api_response"]["data"]["values"]["file"]["content"] + ) self.assertEqual(decoded_content, b"This is example content.") def test_exception_raised_when_service_returns_unexpected_status_code(self): submission = SubmissionFactory.from_components( [ - {"key": "firstName", "type": "textField"}, + {"key": "firstName", "type": "textfield"}, {"key": "lastName", "type": "textfield"}, ], completed=True, submitted_data={"firstName": "We Are", "lastName": "Checking"}, bsn="123456789", + with_public_registration_reference=True, ) - json_form_options = dict( - service=(ServiceFactory(api_root="http://localhost:80/")), - path="fake_endpoint", - variables=["firstName", "auth_bsn"], - ) + options: JSONDumpOptions = { + "service": self.service, + "path": "fake_endpoint", + "variables": ["firstName", "auth_bsn"], + } json_plugin = JSONDumpRegistration("json_registration_plugin") - set_submission_reference(submission) with self.assertRaises(RequestException): - json_plugin.register_submission(submission, json_form_options) + json_plugin.register_submission(submission, options) def test_multiple_file_uploads(self): submission = SubmissionFactory.from_components( @@ -138,12 +147,14 @@ def test_multiple_file_uploads(self): }, ], }, + with_public_registration_reference=True, ) SubmissionFileAttachmentFactory.create( form_key="file", submission_step=submission.submissionstep_set.get(), file_name="file1.txt", + original_name="file1.txt", content_type="application/text", content__data=b"This is example content.", _component_configuration_path="components.2", @@ -154,36 +165,130 @@ def test_multiple_file_uploads(self): form_key="file", submission_step=submission.submissionstep_set.get(), file_name="file2.txt", + original_name="file2.txt", content_type="application/text", content__data=b"Content example is this.", _component_configuration_path="components.2", _component_data_path="file", ) - json_form_options = dict( - service=(ServiceFactory(api_root="http://localhost:80/")), - path="json_plugin", - variables=["file"], + options: JSONDumpOptions = { + "service": self.service, + "path": "json_plugin", + "variables": ["file"], + } + json_plugin = JSONDumpRegistration("json_registration_plugin") + + expected_files = [ + { + "file_name": "file1.txt", + "content": "VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu", # This is example content. + }, + { + "file_name": "file2.txt", + "content": "Q29udGVudCBleGFtcGxlIGlzIHRoaXMu", # Content example is this. + }, + ] + + result = json_plugin.register_submission(submission, options) + + self.assertEqual( + result["api_response"]["data"]["values"]["file"], expected_files ) + + def test_one_file_upload_for_multiple_files_component(self): + submission = SubmissionFactory.from_components( + [{"key": "file", "type": "file", "multiple": True}], + completed=True, + submitted_data={ + "file": [ + { + "url": "some://url", + "name": "file1.txt", + "type": "application/text", + "originalName": "file1.txt", + } + ], + }, + with_public_registration_reference=True, + ) + + SubmissionFileAttachmentFactory.create( + form_key="file", + submission_step=submission.submissionstep_set.get(), + file_name="file1.txt", + original_name="file1.txt", + content_type="application/text", + content__data=b"This is example content.", + _component_configuration_path="components.2", + _component_data_path="file", + ) + + options: JSONDumpOptions = { + "service": self.service, + "path": "json_plugin", + "variables": ["file"], + } + json_plugin = JSONDumpRegistration("json_registration_plugin") + + result = json_plugin.register_submission(submission, options) + + self.assertEqual( + result["api_response"]["data"]["values"]["file"], + [ + { + "file_name": "file1.txt", + "content": "VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu", # This is example content. + } + ], + ) + + def test_no_file_upload_for_single_file_component(self): + submission = SubmissionFactory.from_components( + [{"key": "file", "type": "file"}], + completed=True, + submitted_data={ + "file": [], + }, + with_public_registration_reference=True, + ) + + options: JSONDumpOptions = { + "service": self.service, + "path": "json_plugin", + "variables": ["file"], + } json_plugin = JSONDumpRegistration("json_registration_plugin") - set_submission_reference(submission) - expected_values = { - "file": { - "file1.txt": "VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu", # This is example content. - "file2.txt": "Q29udGVudCBleGFtcGxlIGlzIHRoaXMu", # Content example is this. + result = json_plugin.register_submission(submission, options) + + self.assertEqual(result["api_response"]["data"]["values"]["file"], None) + + def test_no_file_upload_for_multiple_files_component(self): + submission = SubmissionFactory.from_components( + [{"key": "file", "type": "file", "multiple": True}], + completed=True, + submitted_data={ + "file": [], }, + with_public_registration_reference=True, + ) + + options: JSONDumpOptions = { + "service": self.service, + "path": "json_plugin", + "variables": ["file"], } + json_plugin = JSONDumpRegistration("json_registration_plugin") - res = json_plugin.register_submission(submission, json_form_options) - res_json = res["api_response"] + result = json_plugin.register_submission(submission, options) - self.assertEqual(res_json["data"]["values"], expected_values) + self.assertEqual(result["api_response"]["data"]["values"]["file"], []) def test_path_traversal_attack(self): submission = SubmissionFactory.from_components( [ - {"key": "firstName", "type": "textField"}, + {"key": "firstName", "type": "textfield"}, {"key": "lastName", "type": "textfield"}, ], completed=True, @@ -192,18 +297,18 @@ def test_path_traversal_attack(self): "lastName": "Checking", }, bsn="123456789", + with_public_registration_reference=True, ) - json_form_options = dict( - service=(ServiceFactory(api_root="http://localhost:80/")), - path="..", - variables=["firstName", "file", "auth_bsn"], - ) + options: JSONDumpOptions = { + "service": self.service, + "path": "..", + "variables": ["firstName", "file", "auth_bsn"], + } json_plugin = JSONDumpRegistration("json_registration_plugin") - set_submission_reference(submission) for path in ("..", "../foo", "foo/..", "foo/../bar"): with self.subTest(path): - json_form_options["path"] = path + options["path"] = path with self.assertRaises(SuspiciousOperation): - json_plugin.register_submission(submission, json_form_options) + json_plugin.register_submission(submission, options)