[IMP] link_tracker, mass_mailing(_sms): improve tests

Improve link tracker tests :

  * add missing test for trace tracking in mass mailing. We generate a trace
    and find a short link in it, in order to test the tracking in mass
    mailing;
  * add tests for side effects of mass mailing routes: update of trace status,
    clicks, ...
  * wrap base url locally instead of globally in MockTracker class to ease
    specific setup in classes;

Prepares Task-2150462 (Mass Mailing: Unsubscribe flow improvement)

closes odoo/odoo#94660

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
Thibault Delavallée
2022-06-27 16:25:55 +02:00
parent 3ca98b1271
commit 163a12e423
5 changed files with 116 additions and 32 deletions
+5 -6
View File
@@ -14,9 +14,6 @@ class MockLinkTracker(common.BaseCase):
def setUp(self):
super(MockLinkTracker, self).setUp()
self._web_base_url = self.env['mail.mail'].get_base_url()
self.env['ir.config_parameter'].sudo().set_param('web.base.url', 'https://test.odoo.com')
def _get_title_from_url(url):
return "Test_TITLE"
@@ -29,11 +26,13 @@ class MockLinkTracker(common.BaseCase):
html = etree.fromstring(body, parser=etree.HTMLParser())
return html.xpath("//*[@id='%s']" % anchor_id)[0].attrib.get('href')
def _get_tracker_from_short_url(self, short_url):
code = self.env['link.tracker.code'].sudo().search([
def _get_code_from_short_url(self, short_url):
return self.env['link.tracker.code'].sudo().search([
('code', '=', short_url.split('/r/')[-1])
])
return code.link_id
def _get_tracker_from_short_url(self, short_url):
return self._get_code_from_short_url(short_url).link_id
def assertLinkShortenedHtml(self, body, link_info, link_params=None):
""" Find shortened links in an HTML content. Usage :
+11 -4
View File
@@ -1,12 +1,19 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from .common import MockLinkTracker
from odoo.tests import common
from odoo.addons.link_tracker.tests.common import MockLinkTracker
from odoo.exceptions import UserError
from odoo.tests import common, tagged
@tagged('link_tracker')
class TestLinkTracker(common.TransactionCase, MockLinkTracker):
def setUp(self):
super(TestLinkTracker, self).setUp()
self._web_base_url = 'https://test.odoo.com'
self.env['ir.config_parameter'].sudo().set_param('web.base.url', self._web_base_url)
def test_create(self):
link_trackers = self.env['link.tracker'].create([
{
@@ -120,9 +127,9 @@ class TestLinkTracker(common.TransactionCase, MockLinkTracker):
# URL to the local website -> UTM parameters should be added since we know we handle them
# even though the parameter "no_external_tracking" is enabled
link.url = 'https://test.odoo.com/test?a=example.com'
link.url = f'{self._web_base_url}/test?a=example.com'
self.assertLinkParams(
'https://test.odoo.com/test',
f'{self._web_base_url}/test',
link,
{**expected_utm_params, 'a': 'example.com'}
)
@@ -10,6 +10,7 @@ from odoo.tools import mute_logger
@tagged('post_install', '-at_install')
class TestMailingABTesting(MassMailCommon):
def setUp(self):
super().setUp()
self.mailing_list = self._create_mailing_list_of_x_contacts(150)
@@ -1,24 +1,94 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from freezegun import freeze_time
import datetime
import werkzeug
from odoo.addons.mass_mailing.tests.common import MassMailCommon
from odoo.tests.common import HttpCase
from odoo.tests import tagged
class TestMassMailingControllers(MassMailCommon, HttpCase):
@tagged('link_tracker')
class TestMailingControllers(MassMailCommon, HttpCase):
@classmethod
def setUpClass(cls):
super(TestMailingControllers, cls).setUpClass()
cls._create_mailing_list()
cls.test_mailing = cls.env['mailing.mailing'].create({
'body_html': '<p>Hello <t t-out="object.name"/><br />Go to <a id="url" href="https://www.example.com/foo/bar?baz=qux">this link</a></p>',
'contact_list_ids': [(4, cls.mailing_list_1.id), (4, cls.mailing_list_2.id)],
'mailing_model_id': cls.env['ir.model']._get('mailing.list').id,
'mailing_type': 'mail',
'name': 'TestMailing',
'reply_to': cls.email_reply_to,
'subject': 'Test',
})
cls.test_contact = cls.mailing_list_1.contact_ids[0]
# freeze time base value
cls._reference_now = datetime.datetime(2022, 6, 14, 10, 0, 0)
def test_tracking_short_code(self):
""" Test opening short code linked to a mailing trace: should set the
trace as opened and clicked, create a click record. """
mailing = self.test_mailing.with_env(self.env)
with self.mock_mail_gateway(mail_unlink_sent=False):
mailing.action_send_mail()
mail = self._find_mail_mail_wrecord(self.test_contact)
mailing_trace = mail.mailing_trace_ids
link_tracker_code = self._get_code_from_short_url(
self._get_href_from_anchor_id(mail.body, 'url')
)
self.assertEqual(len(link_tracker_code), 1)
self.assertEqual(link_tracker_code.link_id.count, 0)
self.assertEqual(mail.state, 'sent')
self.assertEqual(len(mailing_trace), 1)
self.assertFalse(mailing_trace.links_click_datetime)
self.assertFalse(mailing_trace.open_datetime)
self.assertEqual(mailing_trace.trace_status, 'sent')
short_link_url = werkzeug.urls.url_join(
mail.get_base_url(),
f'r/{link_tracker_code.code}/m/{mailing_trace.id}'
)
with freeze_time(self._reference_now):
_response = self.url_open(short_link_url)
self.assertEqual(link_tracker_code.link_id.count, 1)
self.assertEqual(mailing_trace.links_click_datetime, self._reference_now)
self.assertEqual(mailing_trace.open_datetime, self._reference_now)
self.assertEqual(mailing_trace.trace_status, 'open')
def test_tracking_url_token(self):
self.env['ir.config_parameter'].sudo().set_param('web.base.url', self._web_base_url)
""" Test tracking of mails linked to a mailing trace: should set the
trace as opened. """
mailing = self.test_mailing.with_env(self.env)
with self.mock_mail_gateway(mail_unlink_sent=False):
mailing.action_send_mail()
mail_mail = self.env['mail.mail'].create({})
mail = self._find_mail_mail_wrecord(self.test_contact)
mailing_trace = mail.mailing_trace_ids
self.assertEqual(mail.state, 'sent')
self.assertEqual(len(mailing_trace), 1)
self.assertFalse(mailing_trace.open_datetime)
self.assertEqual(mailing_trace.trace_status, 'sent')
response = self.url_open(mail_mail._get_tracking_url())
with freeze_time(self._reference_now):
response = self.url_open(mail._get_tracking_url())
self.assertEqual(response.status_code, 200)
self.assertEqual(mail.state, 'sent')
self.assertEqual(mailing_trace.open_datetime, self._reference_now)
self.assertEqual(mailing_trace.trace_status, 'open')
base_url = mail_mail.get_base_url()
url = werkzeug.urls.url_join(base_url, 'mail/track/%s/fake_token/blank.gif' % mail_mail.id)
response = self.url_open(url)
track_url = werkzeug.urls.url_join(
mail.get_base_url(),
'mail/track/%s/fake_token/blank.gif' % mail.id
)
response = self.url_open(track_url)
self.assertEqual(response.status_code, 400)
+21 -14
View File
@@ -10,8 +10,10 @@ from odoo import exceptions
from odoo.addons.link_tracker.tests.common import MockLinkTracker
from odoo.addons.sms.models.sms_sms import SmsSms as SmsSms
from odoo.addons.test_mail_full.tests.common import TestMailFullCommon
from odoo.tests import tagged
@tagged('link_tracker')
class TestSMSPost(TestMailFullCommon, MockLinkTracker):
@classmethod
@@ -34,6 +36,11 @@ class TestSMSPost(TestMailFullCommon, MockLinkTracker):
'medium_id': cls.utm_m.id,
}
def setUp(self):
super(TestSMSPost, self).setUp()
self._web_base_url = 'https://test.odoo.com'
self.env['ir.config_parameter'].sudo().set_param('web.base.url', self._web_base_url)
def test_body_link_shorten(self):
link = 'http://www.example.com'
self.env['link.tracker'].search([('url', '=', link)]).unlink()
@@ -43,7 +50,7 @@ class TestSMSPost(TestMailFullCommon, MockLinkTracker):
link = self.env['link.tracker'].search([('url', '=', link)])
self.assertIn(link.short_url, new_body)
link = 'https://test.odoo.com/my/super_page?test[0]=42&toto=áâà#title3'
link = f'{self._web_base_url}/my/super_page?test[0]=42&toto=áâà#title3'
self.env['link.tracker'].search([('url', '=', link)]).unlink()
new_body = self.env['mail.render.mixin']._shorten_links_text('Welcome to %s !' % link, self.tracker_values)
self.assertNotIn(link, new_body)
@@ -60,14 +67,14 @@ class TestSMSPost(TestMailFullCommon, MockLinkTracker):
self.assertFalse(new_body)
def test_body_link_shorten_wshort(self):
link = 'https://test.odoo.com/r/RAOUL'
link = f'{self._web_base_url}/r/RAOUL'
self.env['link.tracker'].search([('url', '=', link)]).unlink()
new_body = self.env['mail.render.mixin']._shorten_links_text('Welcome to %s !' % link, self.tracker_values)
self.assertIn(link, new_body)
self.assertFalse(self.env['link.tracker'].search([('url', '=', link)]))
def test_body_link_shorten_wunsubscribe(self):
link = 'https://test.odoo.com/sms/3/'
link = f'{self._web_base_url}/sms/3/'
self.env['link.tracker'].search([('url', '=', link)]).unlink()
new_body = self.env['mail.render.mixin']._shorten_links_text('Welcome to %s !' % link, self.tracker_values)
self.assertIn(link, new_body)
@@ -81,39 +88,39 @@ class TestSMSPost(TestMailFullCommon, MockLinkTracker):
})
sms_0 = self.env['sms.sms'].create({
'body': 'Welcome to https://test.odoo.com',
'body': f'Welcome to {self._web_base_url}',
'number': '10',
'mailing_id': mailing.id,
})
sms_1 = self.env['sms.sms'].create({
'body': 'Welcome to https://test.odoo.com/r/RAOUL',
'body': f'Welcome to {self._web_base_url}/r/RAOUL',
'number': '11',
})
sms_2 = self.env['sms.sms'].create({
'body': 'Welcome to https://test.odoo.com/r/RAOUL',
'body': f'Welcome to {self._web_base_url}/r/RAOUL',
'number': '12',
'mailing_id': mailing.id,
})
sms_3 = self.env['sms.sms'].create({
'body': 'Welcome to https://test.odoo.com/leodagan/r/RAOUL',
'body': f'Welcome to {self._web_base_url}/leodagan/r/RAOUL',
'number': '13',
'mailing_id': mailing.id,
})
sms_4 = self.env['sms.sms'].create({
'body': 'Welcome to https://test.odoo.com/r/RAOUL\nAnd again,\n'
'https://test.odoo.com/r/RAOUL',
'body': f'Welcome to {self._web_base_url}/r/RAOUL\nAnd again,\n'
f'{self._web_base_url}/r/RAOUL',
'number': '14',
'mailing_id': mailing.id,
})
res = (sms_0 | sms_1 | sms_2 | sms_3 | sms_4)._update_body_short_links()
self.assertEqual(res[sms_0.id], 'Welcome to https://test.odoo.com')
self.assertEqual(res[sms_1.id], 'Welcome to https://test.odoo.com/r/RAOUL')
self.assertEqual(res[sms_2.id], 'Welcome to https://test.odoo.com/r/RAOUL/s/%s' % sms_2.id)
self.assertEqual(res[sms_3.id], 'Welcome to https://test.odoo.com/leodagan/r/RAOUL')
self.assertEqual(res[sms_0.id], f'Welcome to {self._web_base_url}')
self.assertEqual(res[sms_1.id], f'Welcome to {self._web_base_url}/r/RAOUL')
self.assertEqual(res[sms_2.id], f'Welcome to {self._web_base_url}/r/RAOUL/s/%s' % sms_2.id)
self.assertEqual(res[sms_3.id], f'Welcome to {self._web_base_url}/leodagan/r/RAOUL')
self.assertEqual(
res[sms_4.id],
f'Welcome to https://test.odoo.com/r/RAOUL/s/{sms_4.id}\nAnd again,\nhttps://test.odoo.com/r/RAOUL/s/{sms_4.id}')
f'Welcome to {self._web_base_url}/r/RAOUL/s/{sms_4.id}\nAnd again,\n{self._web_base_url}/r/RAOUL/s/{sms_4.id}')
def test_sms_send_batch_size(self):
self.count = 0