Files
odoo_source/addons/link_tracker/tests/common.py
T
Thibault Delavallée 781b61405b [IMP] event_sms, link_tracker, sms, test_mail_full: improve SMS-based mailing tests
PURPOSE

Purpose is to have more tests when sending sms mailings, notably about
canceled or failed mails or sms as well as jinja and links rendering.

SPECIFICATIONS

In this commit we improve SMS Marketing mailing tests. We notably

  * add tests for void and invalid email and numbers. It allows to check they
    correctly update their trace status;
  * add tests for unsubscribe and view links embedded in sms marketing;
  * add tests to simulate a click on links sent through sms marketing and
    ensure click statistics are effectively updated;
  * ensure content of sent sms are checked;

Default content of mailings used in test is updated to ensure jinja is
correctly rendered, including links and some corner cases. This will also
helps ensuring behavior is kept when converting to QWeb.

In event we now correctly use sms gateway mock to ensure SMS finding.

In link_tracker a helper is added to check url tracking in plaintext content.

LINKS

Task ID-2508643
Followup of odoo/odoo#68874 (improve mail tests)
Prepares Task ID-27033 (support QWeb in templates)
Prepares Task ID-2377974 (clean trace and status management in mass mailing)
COM PR odoo/odoo#69461
ENT PR odoo/enterprise#17780

X-original-commit: f1182b9da9aca9e67518fc10c240f3b21895a7ab
2021-04-20 09:18:22 +00:00

92 lines
3.7 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import werkzeug
from lxml import etree
from unittest.mock import patch
from odoo.tests import common
class MockLinkTracker(common.BaseCase):
def setUp(self):
super(MockLinkTracker, self).setUp()
def _get_title_from_url(url):
return "Test_TITLE"
link_tracker_title_patch = patch('odoo.addons.link_tracker.models.link_tracker.LinkTracker._get_title_from_url', wraps=_get_title_from_url)
link_tracker_title_patch.start()
self.addCleanup(link_tracker_title_patch.stop)
def _get_href_from_anchor_id(self, body, anchor_id):
""" Parse en html body to find the href of an element given its ID. """
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([
('code', '=', short_url.split('/r/')[-1])
])
return code.link_id
def assertLinkShortenedHtml(self, body, link_info, link_params=None):
""" Find shortened links in an HTML content. Usage :
self.assertLinkShortenedHtml(
message.body,
('url0', 'http://www.odoo.com', True),
{'utm_campaign': self.utm_c.name, 'utm_medium': self.utm_m.name}
)
"""
(anchor_id, url, is_shortened) = link_info
anchor_href = self._get_href_from_anchor_id(body, anchor_id)
if is_shortened:
self.assertTrue('/r/' in anchor_href, '%s should be shortened: %s' % (anchor_id, anchor_href))
link_tracker = self._get_tracker_from_short_url(anchor_href)
self.assertEqual(url, link_tracker.url)
self.assertLinkParams(url, link_tracker, link_params=link_params)
else:
self.assertTrue('/r/' not in anchor_href, '%s should not be shortened: %s' % (anchor_id, anchor_href))
self.assertEqual(anchor_href, url)
def assertLinkShortenedText(self, body, link_info, link_params=None):
""" Find shortened links in an text content. Usage :
self.assertLinkShortenedText(
message.body,
('http://www.odoo.com', True),
{'utm_campaign': self.utm_c.name, 'utm_medium': self.utm_m.name}
)
"""
(url, is_shortened) = link_info
link_tracker = self.env['link.tracker'].search([('url', '=', url)])
if is_shortened:
self.assertEqual(len(link_tracker), 1)
self.assertIn(link_tracker.short_url, body, '%s should be shortened' % (url))
self.assertLinkParams(url, link_tracker, link_params=link_params)
else:
self.assertEqual(len(link_tracker), 0)
self.assertIn(url, body)
def assertLinkParams(self, url, link_tracker, link_params=None):
""" Usage
self.assertLinkTracker(
'http://www.example.com',
link_tracker,
{'utm_campaign': self.utm_c.name, 'utm_medium': self.utm_m.name}
)
"""
# check UTMS are correctly set on redirect URL
original_url = werkzeug.urls.url_parse(url)
redirect_url = werkzeug.urls.url_parse(link_tracker.redirected_url)
redirect_params = redirect_url.decode_query().to_dict(flat=True)
self.assertEqual(redirect_url.scheme, original_url.scheme)
self.assertEqual(redirect_url.decode_netloc(), original_url.decode_netloc())
self.assertEqual(redirect_url.path, original_url.path)
if link_params:
self.assertEqual(redirect_params, link_params)