-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XML Sending and custom field mapping (#22)
* Allow sending XML * Fix serialization bug * Use custom_field_id if it's available when sending an XML * Allow storing custom_field_id in-place of a field's label * Bump dev version * Fix attachment sending * Changelog * Docs * Add test for XML sending * Add test for field renaming --------- Co-authored-by: Andrea Cecchi <[email protected]>
- Loading branch information
1 parent
cf4b239
commit 15101d6
Showing
6 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
from collective.volto.formsupport.testing import ( # noqa: E501, | ||
VOLTO_FORMSUPPORT_API_FUNCTIONAL_TESTING, | ||
) | ||
from email.parser import Parser | ||
from plone import api | ||
from plone.app.testing import setRoles | ||
from plone.app.testing import SITE_OWNER_NAME | ||
|
@@ -10,6 +11,8 @@ | |
from plone.registry.interfaces import IRegistry | ||
from plone.restapi.testing import RelativeSession | ||
from Products.MailHost.interfaces import IMailHost | ||
from six import StringIO | ||
import xml.etree.ElementTree as ET | ||
from zope.component import getUtility | ||
|
||
import transaction | ||
|
@@ -579,3 +582,37 @@ def test_send_attachment_validate_size( | |
response.json()["message"], | ||
) | ||
self.assertEqual(len(self.mailhost.messages), 0) | ||
|
||
def test_send_xml(self): | ||
self.document.blocks = { | ||
"form-id": {"@type": "form", "send": True, "attachXml": True}, | ||
} | ||
transaction.commit() | ||
|
||
form_data = [ | ||
{"label": "Message", "value": "just want to say hi"}, | ||
{"label": "Name", "value": "John"}, | ||
] | ||
|
||
response = self.submit_form( | ||
data={ | ||
"from": "[email protected]", | ||
"data": form_data, | ||
"subject": "test subject", | ||
"block_id": "form-id", | ||
}, | ||
) | ||
transaction.commit() | ||
self.assertEqual(response.status_code, 204) | ||
msg = self.mailhost.messages[0] | ||
if isinstance(msg, bytes) and bytes is not str: | ||
# Python 3 with Products.MailHost 4.10+ | ||
msg = msg.decode("utf-8") | ||
|
||
parsed_msgs = Parser().parse(StringIO(msg)) | ||
# 1st index is the XML attachment | ||
msg_contents = parsed_msgs.get_payload()[1].get_payload(decode=True) | ||
xml_tree = ET.fromstring(msg_contents) | ||
for index, field in enumerate(xml_tree): | ||
self.assertEqual(field.get("name"), form_data[index]['label']) | ||
self.assertEqual(field.text, form_data[index]['value']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -251,3 +251,63 @@ def test_export_csv(self): | |
now = datetime.utcnow().strftime("%Y-%m-%dT%H:%M") | ||
self.assertTrue(sorted_data[0][-1].startswith(now)) | ||
self.assertTrue(sorted_data[1][-1].startswith(now)) | ||
|
||
def test_data_id_mapping(self): | ||
self.document.blocks = { | ||
"form-id": { | ||
"@type": "form", | ||
"store": True, | ||
"test-field": "renamed-field", | ||
"subblocks": [ | ||
{ | ||
"field_id": "message", | ||
"label": "Message", | ||
"field_type": "text", | ||
}, | ||
{ | ||
"field_id": "test-field", | ||
"label": "Test field", | ||
"field_type": "text", | ||
}, | ||
], | ||
}, | ||
} | ||
transaction.commit() | ||
response = self.submit_form( | ||
data={ | ||
"from": "[email protected]", | ||
"data": [ | ||
{"field_id": "message", "value": "just want to say hi"}, | ||
{"field_id": "test-field", "value": "John"}, | ||
], | ||
"subject": "test subject", | ||
"block_id": "form-id", | ||
}, | ||
) | ||
|
||
response = self.submit_form( | ||
data={ | ||
"from": "[email protected]", | ||
"data": [ | ||
{"field_id": "message", "value": "bye"}, | ||
{"field_id": "test-field", "value": "Sally"}, | ||
], | ||
"subject": "test subject", | ||
"block_id": "form-id", | ||
}, | ||
) | ||
|
||
self.assertEqual(response.status_code, 204) | ||
response = self.export_csv() | ||
data = [*csv.reader(StringIO(response.text), delimiter=",")] | ||
self.assertEqual(len(data), 3) | ||
# Check that 'test-field' got renamed | ||
self.assertEqual(data[0], ["Message", "renamed-field", "date"]) | ||
sorted_data = sorted(data[1:]) | ||
self.assertEqual(sorted_data[0][:-1], ["bye", "Sally"]) | ||
self.assertEqual(sorted_data[1][:-1], ["just want to say hi", "John"]) | ||
|
||
# check date column. Skip seconds because can change during test | ||
now = datetime.utcnow().strftime("%Y-%m-%dT%H:%M") | ||
self.assertTrue(sorted_data[0][-1].startswith(now)) | ||
self.assertTrue(sorted_data[1][-1].startswith(now)) |