From 41000e2c0733e43e3ec619860c579b4b6911eac6 Mon Sep 17 00:00:00 2001 From: Filippo Campi Date: Fri, 9 Aug 2024 17:29:41 +0200 Subject: [PATCH] [dev] test --- .../policy/tests/test_limit_submit_form.py | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/design/plone/policy/tests/test_limit_submit_form.py diff --git a/src/design/plone/policy/tests/test_limit_submit_form.py b/src/design/plone/policy/tests/test_limit_submit_form.py new file mode 100644 index 0000000..f2e600a --- /dev/null +++ b/src/design/plone/policy/tests/test_limit_submit_form.py @@ -0,0 +1,105 @@ +from collective.volto.formsupport.testing import ( # noqa: E501, + VOLTO_FORMSUPPORT_API_FUNCTIONAL_TESTING, +) +from datetime import datetime +from io import StringIO +from plone import api +from plone.app.testing import setRoles +from plone.app.testing import SITE_OWNER_NAME +from plone.app.testing import SITE_OWNER_PASSWORD +from plone.app.testing import TEST_USER_ID +from plone.restapi.testing import RelativeSession +from Products.MailHost.interfaces import IMailHost +from zope.component import getUtility + +import csv +import transaction +import unittest + + +class TestLimitMailStore(unittest.TestCase): + layer = VOLTO_FORMSUPPORT_API_FUNCTIONAL_TESTING + + def setUp(self): + self.app = self.layer["app"] + self.portal = self.layer["portal"] + self.portal_url = self.portal.absolute_url() + setRoles(self.portal, TEST_USER_ID, ["Manager"]) + + self.mailhost = getUtility(IMailHost) + + self.api_session = RelativeSession(self.portal_url) + self.api_session.headers.update({"Accept": "application/json"}) + self.api_session.auth = (SITE_OWNER_NAME, SITE_OWNER_PASSWORD) + self.anon_api_session = RelativeSession(self.portal_url) + self.anon_api_session.headers.update({"Accept": "application/json"}) + + self.document = api.content.create( + type="Document", + title="Example context", + container=self.portal, + ) + + self.document.blocks = { + "text-id": {"@type": "text"}, + "form-id": {"@type": "form"}, + } + self.document_url = self.document.absolute_url() + transaction.commit() + + def tearDown(self): + self.api_session.close() + self.anon_api_session.close() + + # set default block + self.document.blocks = { + "text-id": {"@type": "text"}, + "form-id": {"@type": "form"}, + } + transaction.commit() + + def submit_form(self, data): + url = f"{self.document_url}/@submit-form" + response = self.api_session.post( + url, + json=data, + ) + transaction.commit() + return response + + def test_limit_submit(self): + self.document.blocks = { + "form-id": { + "@type": "form", + "store": True, + "limit": 1, + "subblocks": [ + { + "label": "Message", + "field_id": "message", + "field_type": "text", + }, + { + "label": "Name", + "field_id": "name", + "field_type": "text", + }, + ], + }, + } + transaction.commit() + + response = self.submit_form( + data={ + "from": "john@doe.com", + "data": [ + {"field_id": "message", "value": "just want to say hi"}, + {"field_id": "name", "value": "John"}, + {"field_id": "foo", "value": "skip this"}, + ], + "subject": "test subject", + "block_id": "form-id", + }, + ) + transaction.commit() + self.assertEqual(response.status_code, 200)