diff --git a/automation_oca/models/automation_record_activity.py b/automation_oca/models/automation_record_activity.py index 4c06780..b08dce5 100644 --- a/automation_oca/models/automation_record_activity.py +++ b/automation_oca/models/automation_record_activity.py @@ -227,7 +227,8 @@ def _set_mail_open(self): {"mail_status": "open", "mail_opened_on": fields.Datetime.now()} ) self.child_ids.filtered( - lambda r: r.trigger_type == "mail_open" + lambda r: r.trigger_type + in ["mail_open", "mail_not_reply", "mail_not_clicked"] and not r.scheduled_date and r.state == "scheduled" )._activate() diff --git a/automation_oca/models/mail_thread.py b/automation_oca/models/mail_thread.py index 67cb5bf..1dcedfe 100644 --- a/automation_oca/models/mail_thread.py +++ b/automation_oca/models/mail_thread.py @@ -17,7 +17,7 @@ def _routing_handle_bounce(self, email_message, message_dict): bounced_msg_id = message_dict.get("bounced_msg_id") if bounced_msg_id: self.env["automation.record.activity"].search( - [("message_id", "=", bounced_msg_id)] + [("message_id", "in", bounced_msg_id)] )._set_mail_bounced() return result diff --git a/automation_oca/tests/common.py b/automation_oca/tests/common.py index 110e8a0..c08cd98 100644 --- a/automation_oca/tests/common.py +++ b/automation_oca/tests/common.py @@ -30,7 +30,7 @@ def setUpClass(cls): "name": "My template", "model_id": cls.env.ref("base.model_res_partner").id, "subject": "Subject", - "body_html": "My templae", + "body_html": 'My template with link', } ) cls.partner_01 = cls.env["res.partner"].create( diff --git a/automation_oca/tests/test_automation_mail.py b/automation_oca/tests/test_automation_mail.py index f6a9306..053385c 100644 --- a/automation_oca/tests/test_automation_mail.py +++ b/automation_oca/tests/test_automation_mail.py @@ -2,11 +2,56 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo import tools +from odoo.tests.common import HttpCase + +from odoo.addons.mail.tests.common import MockEmail from .common import AutomationTestCase +MAIL_TEMPLATE = """Return-Path: +To: {to} +cc: {cc} +Received: by mail1.openerp.com (Postfix, from userid 10002) + id 5DF9ABFB2A; Fri, 10 Aug 2012 16:16:39 +0200 (CEST) +From: {email_from} +Subject: {subject} +MIME-Version: 1.0 +Content-Type: multipart/alternative; + boundary="----=_Part_4200734_24778174.1344608186754" +Date: Fri, 10 Aug 2012 14:16:26 +0000 +Message-ID: {msg_id} +{extra} +------=_Part_4200734_24778174.1344608186754 +Content-Type: text/plain; charset=utf-8 +Content-Transfer-Encoding: quoted-printable + +I would gladly answer to your mass mailing ! + +-- +Your Dear Customer +------=_Part_4200734_24778174.1344608186754 +Content-Type: text/html; charset=utf-8 +Content-Transfer-Encoding: quoted-printable + + + + =20 + + =20 + =20 + +

I would gladly answer to your mass mailing !

+ +

--
+ Your Dear Customer +

+ + +------=_Part_4200734_24778174.1344608186754-- +""" + -class TestAutomationMail(AutomationTestCase): +class TestAutomationMail(AutomationTestCase, MockEmail, HttpCase): def test_activity_execution(self): """ We will check the execution of the tasks and that we cannot execute them again @@ -57,3 +102,168 @@ def test_bounce(self): self.env["mail.thread"]._routing_handle_bounce(False, parsed_bounce_values) self.assertEqual("bounce", record_activity.mail_status) self.assertTrue(record_child_activity.scheduled_date) + + def test_reply(self): + """ + Now we will check the execution of scheduled activities""" + with self.mock_mail_gateway(): + activity = self.create_mail_activity() + child_activity = self.create_mail_activity( + parent_id=activity.id, trigger_type="mail_reply" + ) + self.configuration.domain = "[('id', '=', %s)]" % self.partner_01.id + self.configuration.start_automation() + self.env["automation.configuration"].cron_automation() + self.env["automation.record.activity"]._cron_automation_activities() + record_activity = self.env["automation.record.activity"].search( + [("configuration_activity_id", "=", activity.id)] + ) + record_child_activity = self.env["automation.record.activity"].search( + [("configuration_activity_id", "=", child_activity.id)] + ) + self.assertEqual("sent", record_activity.mail_status) + self.assertTrue(record_child_activity) + self.assertFalse(record_child_activity.scheduled_date) + self.gateway_mail_reply_wrecord( + MAIL_TEMPLATE, self.partner_01, use_in_reply_to=True + ) + self.assertEqual("reply", record_activity.mail_status) + self.assertTrue(record_child_activity.scheduled_date) + + def test_no_reply(self): + """ + Now we will check the not reply validation. To remember: + if it is not opened, the schedule date of the child task will be false + """ + with self.mock_mail_gateway(): + activity = self.create_mail_activity() + child_activity = self.create_mail_activity( + parent_id=activity.id, trigger_type="mail_not_reply" + ) + self.configuration.domain = "[('id', '=', %s)]" % self.partner_01.id + self.configuration.start_automation() + self.env["automation.configuration"].cron_automation() + self.env["automation.record.activity"]._cron_automation_activities() + record_activity = self.env["automation.record.activity"].search( + [("configuration_activity_id", "=", activity.id)] + ) + record_child_activity = self.env["automation.record.activity"].search( + [("configuration_activity_id", "=", child_activity.id)] + ) + self.assertEqual("sent", record_activity.mail_status) + self.assertTrue(record_child_activity) + self.assertFalse(record_child_activity.scheduled_date) + self.url_open(record_activity._get_mail_tracking_url()) + self.assertEqual("open", record_activity.mail_status) + self.assertTrue(record_child_activity.scheduled_date) + self.gateway_mail_reply_wrecord( + MAIL_TEMPLATE, self.partner_01, use_in_reply_to=True + ) + self.assertEqual("reply", record_activity.mail_status) + self.env["automation.record.activity"]._cron_automation_activities() + self.assertEqual("rejected", record_child_activity.state) + + def test_open(self): + """ + Now we will check the execution of scheduled activities""" + with self.mock_mail_gateway(): + activity = self.create_mail_activity() + child_activity = self.create_mail_activity( + parent_id=activity.id, trigger_type="mail_open" + ) + self.configuration.domain = "[('id', '=', %s)]" % self.partner_01.id + self.configuration.start_automation() + self.env["automation.configuration"].cron_automation() + self.env["automation.record.activity"]._cron_automation_activities() + record_activity = self.env["automation.record.activity"].search( + [("configuration_activity_id", "=", activity.id)] + ) + record_child_activity = self.env["automation.record.activity"].search( + [("configuration_activity_id", "=", child_activity.id)] + ) + self.assertEqual("sent", record_activity.mail_status) + self.assertTrue(record_child_activity) + self.assertFalse(record_child_activity.scheduled_date) + self.url_open(record_activity._get_mail_tracking_url()) + self.assertEqual("open", record_activity.mail_status) + self.assertTrue(record_child_activity.scheduled_date) + + def test_open_wrong_code(self): + """ + We wan to ensure that the code is checked on the call + """ + with self.mock_mail_gateway(): + activity = self.create_mail_activity() + child_activity = self.create_mail_activity( + parent_id=activity.id, trigger_type="mail_open" + ) + self.configuration.domain = "[('id', '=', %s)]" % self.partner_01.id + self.configuration.start_automation() + self.env["automation.configuration"].cron_automation() + self.env["automation.record.activity"]._cron_automation_activities() + record_activity = self.env["automation.record.activity"].search( + [("configuration_activity_id", "=", activity.id)] + ) + record_child_activity = self.env["automation.record.activity"].search( + [("configuration_activity_id", "=", child_activity.id)] + ) + self.assertEqual("sent", record_activity.mail_status) + self.assertTrue(record_child_activity) + self.assertFalse(record_child_activity.scheduled_date) + self.url_open( + "/automation_oca/track/%s/INVENTED_CODE/blank.gif" % record_activity.id + ) + self.assertEqual("sent", record_activity.mail_status) + self.assertFalse(record_child_activity.scheduled_date) + + def test_no_open(self): + """ + Now we will check the not open validation when it is not opened (should be executed) + """ + with self.mock_mail_gateway(): + activity = self.create_mail_activity() + child_activity = self.create_mail_activity( + parent_id=activity.id, trigger_type="mail_not_open" + ) + self.configuration.domain = "[('id', '=', %s)]" % self.partner_01.id + self.configuration.start_automation() + self.env["automation.configuration"].cron_automation() + self.env["automation.record.activity"]._cron_automation_activities() + record_activity = self.env["automation.record.activity"].search( + [("configuration_activity_id", "=", activity.id)] + ) + record_child_activity = self.env["automation.record.activity"].search( + [("configuration_activity_id", "=", child_activity.id)] + ) + self.assertEqual("sent", record_activity.mail_status) + self.assertTrue(record_child_activity) + self.assertTrue(record_child_activity.scheduled_date) + self.env["automation.record.activity"]._cron_automation_activities() + self.assertEqual("done", record_child_activity.state) + + def test_no_open_rejected(self): + """ + Now we will check the not open validation when it was already opened + """ + with self.mock_mail_gateway(): + activity = self.create_mail_activity() + child_activity = self.create_mail_activity( + parent_id=activity.id, trigger_type="mail_not_open" + ) + self.configuration.domain = "[('id', '=', %s)]" % self.partner_01.id + self.configuration.start_automation() + self.env["automation.configuration"].cron_automation() + self.env["automation.record.activity"]._cron_automation_activities() + record_activity = self.env["automation.record.activity"].search( + [("configuration_activity_id", "=", activity.id)] + ) + record_child_activity = self.env["automation.record.activity"].search( + [("configuration_activity_id", "=", child_activity.id)] + ) + self.assertEqual("sent", record_activity.mail_status) + self.assertTrue(record_child_activity) + self.assertTrue(record_child_activity.scheduled_date) + self.url_open(record_activity._get_mail_tracking_url()) + self.assertEqual("open", record_activity.mail_status) + self.env["automation.record.activity"]._cron_automation_activities() + self.assertEqual("rejected", record_child_activity.state)