Skip to content

Commit

Permalink
fixup! Adding mail tests
Browse files Browse the repository at this point in the history
  • Loading branch information
etobella committed Mar 10, 2024
1 parent ba331b3 commit d8db046
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 4 deletions.
3 changes: 2 additions & 1 deletion automation_oca/models/automation_record_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion automation_oca/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion automation_oca/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://www.twitter.com" /> with link',
}
)
cls.partner_01 = cls.env["res.partner"].create(
Expand Down
212 changes: 211 additions & 1 deletion automation_oca/tests/test_automation_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: <[email protected]>
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>=20
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8" />
</head>=20
<body style=3D"margin: 0; padding: 0; background: #ffffff;-webkit-text-size-adjust: 100%;">=20
<p>I would gladly answer to your mass mailing !</p>
<p>--<br/>
Your Dear Customer
<p>
</body>
</html>
------=_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
Expand Down Expand Up @@ -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)

0 comments on commit d8db046

Please sign in to comment.