Skip to content

Commit

Permalink
extra deserialization tests
Browse files Browse the repository at this point in the history
context: test with extradata and showfields
  • Loading branch information
ThibautBorn committed Nov 10, 2023
1 parent 493692c commit d8606c5
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/collective/easyform/tests/testSavedDataSerializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,48 @@ def testSpecialConverterFormDataSerialization(self):
self.assertIn("me", json.dumps(obj))
self.assertIn(u"<div><b>testing is f\\u00f9n</b> says Micha\\u00ebl</div>", json.dumps(obj))

def testShowFieldsFormDataSerialization(self):
self.request = self.layer["request"]
self.createSaver()

## set showfields
extra_showfields = '\n <showFields>\n <element>topic</element>\n <element>comments</element>\n </showFields>'
idx = self.ff1.actions_model.index('<title>Saver</title>\n')
self.ff1.actions_model = self.ff1.actions_model[:idx] + extra_showfields + self.ff1.actions_model[idx:]
saver = get_actions(self.ff1)["saver"]
request = FakeRequest(
topic="test subject", replyto="[email protected]", comments="test comments"
)
saver.onSuccess(request.form, request)

obj = self.serialize(self.ff1)
self.assertIn("savedDataStorage", obj)
self.assertIn("test subject", json.dumps(obj))
self.assertNotIn("[email protected]", json.dumps(obj))
self.assertIn("test comments", json.dumps(obj))

def testExtraDataFormDataSerialization(self):
self.request = self.layer["request"]
self.createSaver()

## set extradata and showfields
extra_showfields = '\n <ExtraData>\n <element>dt</element>\n <element>HTTP_X_FORWARDED_FOR</element>\n </ExtraData>'
idx = self.ff1.actions_model.index('<title>Saver</title>\n')
self.ff1.actions_model = self.ff1.actions_model[:idx] + extra_showfields + self.ff1.actions_model[idx:]
saver = get_actions(self.ff1)["saver"]
request = FakeRequest(
topic="test subject", replyto="[email protected]", comments="test comments"
)
saver.onSuccess(request.form, request)

obj = self.serialize(self.ff1)
self.assertIn("savedDataStorage", obj)
self.assertIn("test subject", json.dumps(obj))
self.assertIn("[email protected]", json.dumps(obj))
self.assertIn("test comments", json.dumps(obj))
self.assertIn("dt", json.dumps(obj))
self.assertIn("HTTP_X_FORWARDED_FOR", json.dumps(obj))

def serialize(self, obj=None):
if obj is None:
obj = self.portal.doc1
Expand Down Expand Up @@ -152,6 +194,64 @@ def testSpecialConvertersFormDataDeserialization(self):
self.assertIn("rich", data)
self.assertEqual(u"<div><b>testing is fùn</b> says Michaël</div>", data['rich'].raw)

def testShowFieldsFormDataDeserialization(self):
BODY = ( '{"actions_model": "<model '
'xmlns:i18n=\\"http://xml.zope.org/namespaces/i18n\\" '
'xmlns:marshal=\\"http://namespaces.plone.org/supermodel/marshal\\" '
'xmlns:form=\\"http://namespaces.plone.org/supermodel/form\\" '
'xmlns:security=\\"http://namespaces.plone.org/supermodel/security\\" '
'xmlns:users=\\"http://namespaces.plone.org/supermodel/users\\" '
'xmlns:lingua=\\"http://namespaces.plone.org/supermodel/lingua\\" '
'xmlns:easyform=\\"http://namespaces.plone.org/supermodel/easyform\\" '
'xmlns=\\"http://namespaces.plone.org/supermodel/schema\\" '
'i18n:domain=\\"collective.easyform\\">\\n <schema>\\n <field '
'name=\\"saver\\" type=\\"collective.easyform.actions.SaveData\\">\\n '
'<showFields>\\n <element>topic</element>\\n <element>comments</element>\\n </showFields>\\n '
'<title>Saver</title>\\n </field>\\n </schema>\\n</model>",'
'"savedDataStorage": '
'{"saver": {"1658237759974": {"topic": "test subject", "replyto": '
'"[email protected]", "comments": "test comments", "id": 1658237759974}}}}')

self.deserialize(body=BODY, context=self.ff1)
saver = get_actions(self.ff1)["saver"]
data = saver.getSavedFormInput()[0]
self.assertIn("topic", data)
self.assertEqual("test subject", data['topic'])
self.assertNotIn("replyto", data)
self.assertIn("comments", data)
self.assertEqual("test comments", data['comments'])

def testExtraDataFormDataDeserialization(self):
BODY = ( '{"actions_model": "<model '
'xmlns:i18n=\\"http://xml.zope.org/namespaces/i18n\\" '
'xmlns:marshal=\\"http://namespaces.plone.org/supermodel/marshal\\" '
'xmlns:form=\\"http://namespaces.plone.org/supermodel/form\\" '
'xmlns:security=\\"http://namespaces.plone.org/supermodel/security\\" '
'xmlns:users=\\"http://namespaces.plone.org/supermodel/users\\" '
'xmlns:lingua=\\"http://namespaces.plone.org/supermodel/lingua\\" '
'xmlns:easyform=\\"http://namespaces.plone.org/supermodel/easyform\\" '
'xmlns=\\"http://namespaces.plone.org/supermodel/schema\\" '
'i18n:domain=\\"collective.easyform\\">\\n <schema>\\n <field '
'name=\\"saver\\" type=\\"collective.easyform.actions.SaveData\\">\\n '
'<ExtraData>\\n <element>td</element>\\n <element>HTTP_X_FORWARDED_FOR</element>\\n </ExtraData>\\n '
'<title>Saver</title>\\n </field>\\n </schema>\\n</model>",'
'"savedDataStorage": '
'{"saver": {"1658237759974": {"topic": "test subject", "replyto": '
'"[email protected]", "comments": "test comments", "id": 1658237759974,'
'"HTTP_X_FORWARDED_FOR": "", "dt": "2023/11/10 09:54:5.924947 GMT+1"}}}}')

self.deserialize(body=BODY, context=self.ff1)
saver = get_actions(self.ff1)["saver"]
data = saver.getSavedFormInput()[0]
self.assertIn("topic", data)
self.assertEqual("test subject", data['topic'])
self.assertIn("replyto", data)
self.assertEqual("[email protected]", data['replyto'])
self.assertIn("comments", data)
self.assertEqual("test comments", data['comments'])
self.assertIn("dt", data)
self.assertEqual("2023/11/10 09:54:5.924947 GMT+1", data['dt'])
self.assertIn("HTTP_X_FORWARDED_FOR", data)

def deserialize(self, body="{}", validate_all=False, context=None, create=False):
self.request = self.layer["request"]
Expand Down

0 comments on commit d8606c5

Please sign in to comment.