Skip to content

Commit

Permalink
✅ [#4908] Add and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorvanwijk committed Jan 17, 2025
1 parent 74d3b3b commit dc7d7d3
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
interactions:
- request:
body: '{"values": {"firstName": "We Are", "auth_bsn": "123456789"}, "schema":
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":
Expand All @@ -12,7 +12,7 @@ interactions:
Accept-Encoding:
- gzip, deflate, br
Authorization:
- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3MzU4MjY2MzksImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.8PuPAIY6PI3_g4edfqzFFbHNldYxxRIBjPuAh-p00xk
- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3MzcwMzM3OTcsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.ZE3A4AQZ8omE_KsvI_LcDSMrmBa9-AJoUiiAdjhtSqo
Connection:
- keep-alive
Content-Length:
Expand Down Expand Up @@ -45,7 +45,7 @@ interactions:
Content-Type:
- text/html; charset=utf-8
Date:
- Thu, 02 Jan 2025 14:03:59 GMT
- Thu, 16 Jan 2025 13:23:17 GMT
Server:
- Werkzeug/3.1.3 Python/3.12.8
status:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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.eyJpc3MiOiIiLCJpYXQiOjE3MzcwMzM3OTcsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.ZE3A4AQZ8omE_KsvI_LcDSMrmBa9-AJoUiiAdjhtSqo
Connection:
- keep-alive
Content-Length:
- '562'
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:
- Thu, 16 Jan 2025 13:23:17 GMT
Server:
- Werkzeug/3.1.3 Python/3.12.8
status:
code: 201
message: CREATED
version: 1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
interactions:
- request:
body: '{"values": {"file": "VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu", "firstName": "We
Are", "auth_bsn": "123456789"}, "schema": {"$schema": "https://json-schema.org/draft/2020-12/schema",
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":
Expand All @@ -12,11 +11,11 @@ interactions:
Accept-Encoding:
- gzip, deflate, br
Authorization:
- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3MzU4MjY2MzksImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.8PuPAIY6PI3_g4edfqzFFbHNldYxxRIBjPuAh-p00xk
- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIiLCJpYXQiOjE3MzcwMzM3OTcsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.ZE3A4AQZ8omE_KsvI_LcDSMrmBa9-AJoUiiAdjhtSqo
Connection:
- keep-alive
Content-Length:
- '494'
- '414'
Content-Type:
- application/json
User-Agent:
Expand All @@ -33,17 +32,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
- Thu, 16 Jan 2025 13:23:17 GMT
Server:
- Werkzeug/3.1.3 Python/3.12.8
status:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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.eyJpc3MiOiIiLCJpYXQiOjE3MzcwMzM3OTgsImNsaWVudF9pZCI6IiIsInVzZXJfaWQiOiIiLCJ1c2VyX3JlcHJlc2VudGF0aW9uIjoiIn0.auRLR2g7eX3HTcKYX6vM2j4WThn9rWImicoRL1rz0l4
Connection:
- keep-alive
Content-Length:
- '537'
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:
- Thu, 16 Jan 2025 13:23:18 GMT
Server:
- Werkzeug/3.1.3 Python/3.12.8
status:
code: 201
message: CREATED
version: 1
62 changes: 47 additions & 15 deletions src/openforms/registrations/contrib/json_dump/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.core.exceptions import SuspiciousOperation
from django.test import TestCase

from glom import Path as Path_glom, glom
from requests import RequestException
from zgw_consumers.test.factories import ServiceFactory

Expand All @@ -22,10 +23,10 @@
class JSONDumpBackendTests(OFVCRMixin, TestCase):
VCR_TEST_FILES = VCR_TEST_FILES

def test_submission_with_json_dump_backend(self):
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"},
],
Expand All @@ -49,6 +50,7 @@ def test_submission_with_json_dump_backend(self):
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",
Expand All @@ -68,7 +70,10 @@ def test_submission_with_json_dump_backend(self):
"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": {
Expand All @@ -88,18 +93,19 @@ def test_submission_with_json_dump_backend(self):
}

res = json_plugin.register_submission(submission, json_form_options)
res_json = res["api_response"].json()

self.assertEqual(res_json, expected_response)
self.assertEqual(res["api_response"], expected_response)

with self.subTest("attachment content encoded"):
decoded_content = b64decode(res_json["data"]["values"]["file"])
decoded_content = b64decode(
glom(res, "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,
Expand Down Expand Up @@ -144,6 +150,7 @@ def test_multiple_file_uploads(self):
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",
Expand All @@ -154,6 +161,7 @@ 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",
Expand All @@ -168,22 +176,46 @@ def test_multiple_file_uploads(self):
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.
expected_files = [
{
"file_name": "file1.txt",
"content": "VGhpcyBpcyBleGFtcGxlIGNvbnRlbnQu", # This is example content.
},
}
{
"file_name": "file2.txt",
"content": "Q29udGVudCBleGFtcGxlIGlzIHRoaXMu", # Content example is this.
},
]

res = json_plugin.register_submission(submission, json_form_options)

self.assertEqual(glom(res, "api_response.data.values.file"), expected_files)

def test_no_file_upload(self):
submission = SubmissionFactory.from_components(
[{"key": "file", "type": "file"}],
completed=True,
submitted_data={
"file": [],
},
)

json_form_options = dict(
service=(ServiceFactory(api_root="http://localhost:80/")),
path="json_plugin",
variables=["file"],
)
json_plugin = JSONDumpRegistration("json_registration_plugin")
set_submission_reference(submission)

res = json_plugin.register_submission(submission, json_form_options)
res_json = res["api_response"]

self.assertEqual(res_json["data"]["values"], expected_values)
self.assertEqual(glom(res, "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,
Expand Down
10 changes: 10 additions & 0 deletions src/openforms/registrations/contrib/json_dump/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.core.exceptions import ValidationError
from django.test import TestCase

from ..config import validate_path


class JSONDumpConfigTests(TestCase):
def test_validate_path(self):
with self.assertRaises(ValidationError):
validate_path("..")

0 comments on commit dc7d7d3

Please sign in to comment.