Commit 50eb637a authored by Nicolas Wavrant's avatar Nicolas Wavrant

erp5_interface_post: add test suite

parent 5a71ba9b
##############################################################################
#
# Copyright (c) 2002-2017 Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
##############################################################################
import time
from Products.ERP5Type.tests.ERP5TypeLiveTestCase import ERP5TypeLiveTestCase
from Products.ERP5Type.tests.Sequence import SequenceList
from Products.ZSQLCatalog.SQLCatalog import SimpleQuery
from DateTime import DateTime
class TestInterfacePost(ERP5TypeLiveTestCase):
"""
Tests the creation of "Post" documents when their related event type
is sent.
"""
default_mail_text_content = \
'Hello dear customer,\nConnect to your space online to get free money'
def getTitle(self):
return "Interface Post Test"
def afterSetUp(self):
"""
This is ran before anything, used to set the environment
"""
self._setupLetterPostExportPythonScript()
self.portal.MailHost.reset()
# Make sure to have a difference of at least 1 second between test_launch_date
# and creation_date of new documents, otherwise they won't be found by catalog
# during the fatest test sequences
self.test_launch_date = DateTime()
time.sleep(1)
self.sender = self.portal.person_module.newContent(
portal_type='Person',
title='sender',
default_email_coordinate_text='sender@nexedi.com',
)
self.recipient = self.portal.person_module.newContent(
portal_type='Person',
title='recipient',
default_email_coordinate_text='recipient@nexedi.com',
)
self.recipient_list = [self.recipient,]
def beforeTearDown(self):
self._removeLetterPostExportPythonScript()
def _setupLetterPostExportPythonScript(self):
skin_folder = self.portal.portal_skins.custom
script_id = "LetterPost_export"
script = getattr(skin_folder, script_id, None)
if script is None:
skin_folder.manage_addProduct['PythonScripts']\
.manage_addPythonScript(id=script_id)
skin_folder[script_id].write("if context.getSimulationState() == 'exportable':\n context.export()")
self.tic()
def _removeLetterPostExportPythonScript(self):
skin_folder = self.portal.portal_skins.custom
script_id = "LetterPost_export"
script = getattr(skin_folder, script_id, None)
if script is not None:
skin_folder.manage_delObjects('LetterPost_export')
def _portal_catalog(self, **kw):
result_list = self.portal.portal_catalog(**kw)
uid_list = [x.uid for x in result_list]
if len(uid_list):
return self.portal.portal_catalog(
uid=uid_list,
query=SimpleQuery(
creation_date=self.test_launch_date,
comparison_operator='>'
),
)
else:
return result_list
def stepCreateMailMessage(self, sequence=None, sequence_list=None):
mail_message = self.portal.event_module.newContent(
portal_type='Mail Message',
source_value=self.sender,
destination_value_list=self.recipient_list,
title='Promotional campaign',
text_content=self.default_mail_text_content,
)
sequence['mail_message'] = mail_message
sequence.setdefault('mail_message_list', []).append(mail_message)
def stepCreateInternetMessagePost(self, sequence=None, sequence_list=None):
internet_message_post = self.portal.internet_message_post_module.newContent(
portal_type='Internet Message Post',
reference='ref1',
)
sequence['internet_message_post'] = internet_message_post
sequence.setdefault('internet_message_post_list', []).append(internet_message_post)
def stepCreateLetterPost(self, sequence=None, sequence_list=None):
letter_post = self.portal.letter_post_module.newContent(
portal_type='Letter Post',
reference='ref1',
)
sequence['letter_post'] = letter_post
sequence.setdefault('letter_post_list', []).append(letter_post)
def stepStartMailMessage(self, sequence=None, sequence_list=None):
self.portal.portal_workflow.doActionFor(
sequence['mail_message'],
'start_action'
)
def _allowExportOfAllPostByPortalType(self, portal_type):
result_list = self._portal_catalog(portal_type=portal_type)
for result in result_list:
result.allowExport()
def stepAllowExportOfAllInternetMessagePost(self, sequence=None, sequence_list=None):
self._allowExportOfAllPostByPortalType('Internet Message Post')
def stepAllowExportOfAllLetterPost(self, sequence=None, sequence_list=None):
self._allowExportOfAllPostByPortalType('Letter Post')
def stepChangeMailMessageTextContent(self, sequence=None, sequence_list=None):
mail_message = sequence['mail_message']
mail_message.setTextContent("Hello customer,\nsome agent is trying to hack the system")
def stepCheckMailMessage(self, sequence=None, sequence_list=None):
mail_message = sequence['mail_message']
self.assertEqual(mail_message.getSimulationState(), 'started')
self.assertIn(mail_message.getTextContent(), self.default_mail_text_content)
def _checkOnlyOnePostIsExportedByPortalType(self, portal_type):
result_list = self._portal_catalog(
portal_type=portal_type,
simulation_state='exported',
)
self.assertEqual(
len(result_list),
1,
"More than 1 exportable Internet Message Post has been exported."
)
result_list = self._portal_catalog(
portal_type=portal_type,
simulation_state='unexportable',
)
self.assertGreaterEqual(
len(result_list),
1,
"No Internet Message Post's export has been cancelled after exporting one of them."
)
def stepCheckOnlyOneInternetMessagePostIsExported(self, sequence=None, sequence_list=None):
self._checkOnlyOnePostIsExportedByPortalType('Internet Message Post')
def stepCheckOnlyOneLetterPostIsExported(self, sequence=None, sequence_list=None):
self._checkOnlyOnePostIsExportedByPortalType('Letter Post')
def stepCheckInternetMessagePostCreated(self, sequence=None, sequence_list=None):
mail_message = sequence['mail_message']
internet_message_post, = mail_message.getAggregateValueList(
portal_type='Internet Message Post')
sequence['internet_message_post'] = internet_message_post
self.assertEqual(0, len(mail_message.getAggregateDocumentTitleList([])),
"Internet Message Post shouldn't be an attachment"
)
self.assertEqual(internet_message_post.getSimulationState(), 'exported')
self.assertEqual(
internet_message_post.getReference(), mail_message.getReference()
)
self.assertEqual(
mail_message.getTextContent(),
self.portal.Base_getFirstPartFromMultipartMessage(
internet_message_post.getData())
)
def stepCheckLatestMessageFromMailHost(self, sequence=None, sequence_list=None):
last_message, = self.portal.MailHost._message_list
self.assertNotEquals((), last_message)
_, _, messageText = last_message
self.assertIn(messageText, sequence['internet_message_post'].getData())
def stepCheckMailMessagePreviewDisplaysLatestInternetMessagePostData(self, sequence=None, sequence_list=None):
mail_message = sequence['mail_message']
self.assertNotEqual(mail_message.getTextContent(), self.default_mail_text_content)
self.assertIn(
self.default_mail_text_content,
mail_message.MailMessage_viewPreview.my_text_content.render(),
)
def test_onlyOneExportablePostIsExported(self):
"""
"""
sequence_list = SequenceList()
# With 3 Letter Posts, we can trigger a race condition where 2 letters
# disallow export of each other, thus nothing is exported.
sequence_string = """
stepCreateLetterPost
stepCreateLetterPost
stepCreateLetterPost
stepTic
stepAllowExportOfAllLetterPost
stepTic
stepCheckOnlyOneLetterPostIsExported
"""
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self)
def test_emailSendingIsPilotedByInternetMessagePost(self):
"""
"""
sequence_list = SequenceList()
# With 3 Letter Posts, we can trigger a race condition where 2 letters
# disallow export of each other, thus nothing is exported.
sequence_string = """
stepCreateMailMessage
stepStartMailMessage
stepTic
stepCheckMailMessage
stepCheckInternetMessagePostCreated
stepCheckLatestMessageFromMailHost
"""
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self)
def test_deliveredMailMessagePreviewDisplaysLatestInternetMessagePostData(self):
"""
"""
sequence_list = SequenceList()
sequence_string = """
stepCreateMailMessage
stepStartMailMessage
stepCheckMailMessage
stepTic
stepCheckInternetMessagePostCreated
stepCheckLatestMessageFromMailHost
stepChangeMailMessageTextContent
stepCheckMailMessagePreviewDisplaysLatestInternetMessagePostData
"""
sequence_list.addSequenceString(sequence_string)
sequence_list.play(self)
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Test Component" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_recorded_property_dict</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>default_reference</string> </key>
<value> <string>testInterfacePost</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>test.erp5.testInterfacePost</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Test Component</string> </value>
</item>
<item>
<key> <string>sid</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>text_content_error_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>version</string> </key>
<value> <string>erp5</string> </value>
</item>
<item>
<key> <string>workflow_history</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary/>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary>
<item>
<key> <string>component_validation_workflow</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAQ=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="4" aka="AAAAAAAAAAQ=">
<pickle>
<global name="WorkflowHistoryList" module="Products.ERP5Type.patches.WorkflowTool"/>
</pickle>
<pickle>
<tuple>
<none/>
<list>
<dictionary>
<item>
<key> <string>action</string> </key>
<value> <string>validate</string> </value>
</item>
<item>
<key> <string>validation_state</string> </key>
<value> <string>validated</string> </value>
</item>
</dictionary>
</list>
</tuple>
</pickle>
</record>
</ZopeData>
test.erp5.testInterfacePost
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment