From 7f4864353181cf41fcd894612c5db761ad29b22b Mon Sep 17 00:00:00 2001
From: Nicolas Wavrant <nicolas.wavrant@nexedi.com>
Date: Thu, 17 May 2018 10:13:57 +0000
Subject: [PATCH] erp5_interface_post: test that Entity_sendEmail can retry on
 failure

But only one mail will be sent out by MailHost
---
 .../test.erp5.testInterfacePost.py            | 45 +++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/bt5/erp5_interface_post/TestTemplateItem/portal_components/test.erp5.testInterfacePost.py b/bt5/erp5_interface_post/TestTemplateItem/portal_components/test.erp5.testInterfacePost.py
index 3503f1262d..9a93225a28 100644
--- a/bt5/erp5_interface_post/TestTemplateItem/portal_components/test.erp5.testInterfacePost.py
+++ b/bt5/erp5_interface_post/TestTemplateItem/portal_components/test.erp5.testInterfacePost.py
@@ -28,6 +28,7 @@ import email
 import time
 
 from Products.ERP5Type.tests.ERP5TypeLiveTestCase import ERP5TypeTestCase
+from Products.ERP5Type.tests.utils import createZODBPythonScript, removeZODBPythonScript
 from Products.ERP5Type.tests.Sequence import SequenceList
 from Products.ZSQLCatalog.SQLCatalog import SimpleQuery
 from DateTime import DateTime
@@ -81,6 +82,14 @@ class TestInterfacePost(ERP5TypeTestCase):
       module = getattr(self.portal, module_id)
       module.manage_delObjects(list(module.objectIds()))
 
+    custom_skin = self.portal.portal_skins.custom
+    if 'Entity_sendEmail' in custom_skin.objectIds():
+      removeZODBPythonScript(
+        custom_skin,
+        'Entity_sendEmail',
+      )
+      self.commit()
+
   def _portal_catalog(self, **kw):
     result_list = self.portal.portal_catalog(**kw)
     uid_list = [x.uid for x in result_list]
@@ -333,6 +342,21 @@ class TestInterfacePost(ERP5TypeTestCase):
     pdf_document, = pdf_document_list
     self.assertEqual(2, int(pdf_document.getContentInformation()['Pages']))
 
+
+  def stepMakeEntitySendEmailFailOnce(self, sequence=None):
+    createZODBPythonScript(
+      self.portal.portal_skins.custom,
+      'Entity_sendEmail',
+      self.portal.Entity_sendEmail.params(),
+      """portal = context.getPortalObject()
+for activity in portal.portal_activities.getMessageList():
+  if activity.method_id == script.id:
+    if activity.retry == 0:
+      raise ValueError('Failure on purpose')
+    else:
+      return context.skinSuper('custom', script.id)(%s)""" % (self.portal.Entity_sendEmail.params(),)
+    )
+
   def test_emailSendingIsPilotedByInternetMessagePost(self):
     """
     """
@@ -392,6 +416,27 @@ class TestInterfacePost(ERP5TypeTestCase):
     sequence_list.addSequenceString(sequence_string)
     sequence_list.play(self)
 
+  def test_Entity_sendEmailCanRaiseOnceWithoutSpammingRecipient(self):
+    """
+    Entity_sendEmail used to be launched in an activity with retry_max=0 and
+    retry_conflict=False. But now that it creates Internet Message Posts, it
+    should be able to retry on ConflictError. We should also make sure that
+    in this case the mail isn't sent (as MailHost isn't transactional)
+    """
+    sequence_list = SequenceList()
+    sequence_string = """
+      stepMakeEntitySendEmailFailOnce
+      stepCreateMailMessage
+      stepStartMailMessage
+      stepCheckMailMessage
+      stepTic
+      stepCheckInternetMessagePostCreated
+      stepCheckOnlyOneMessageHasBeenSentFromMailHost
+      stepCheckLatestMessageListFromMailHost
+    """
+    sequence_list.addSequenceString(sequence_string)
+    sequence_list.play(self)
+
   def test_ingestedInternetMessagePostCreateMailMessageWithCausality(self):
     sequence_list = SequenceList()
     sequence_string = """
-- 
2.30.9