summaryrefslogtreecommitdiff
path: root/addons/link_tracker/tests
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/link_tracker/tests
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/link_tracker/tests')
-rw-r--r--addons/link_tracker/tests/__init__.py5
-rw-r--r--addons/link_tracker/tests/common.py91
-rw-r--r--addons/link_tracker/tests/test_mail_render_mixin.py52
3 files changed, 148 insertions, 0 deletions
diff --git a/addons/link_tracker/tests/__init__.py b/addons/link_tracker/tests/__init__.py
new file mode 100644
index 00000000..e8c7d845
--- /dev/null
+++ b/addons/link_tracker/tests/__init__.py
@@ -0,0 +1,5 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from . import common
+from . import test_mail_render_mixin
diff --git a/addons/link_tracker/tests/common.py b/addons/link_tracker/tests/common.py
new file mode 100644
index 00000000..eda22291
--- /dev/null
+++ b/addons/link_tracker/tests/common.py
@@ -0,0 +1,91 @@
+# -*- 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)
diff --git a/addons/link_tracker/tests/test_mail_render_mixin.py b/addons/link_tracker/tests/test_mail_render_mixin.py
new file mode 100644
index 00000000..23a74b5a
--- /dev/null
+++ b/addons/link_tracker/tests/test_mail_render_mixin.py
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo.tests import common
+
+
+class TestMailRenderMixin(common.TransactionCase):
+ def test_shorten_links(self):
+ test_links = [
+ '<a href="https://gitlab.com" title="title" fake="fake">test_label</a>',
+ '<a href="https://test_542152qsdqsd.com"/>',
+ """<a href="https://third_test_54212.com">
+ <img src="imagesrc"/>
+ </a>
+ """,
+ """<a
+ href="https://test_strange_html.com" title="title"
+ fake='fake'
+ > test_strange_html_label
+ </a>
+ """,
+ '<a href="https://test_escaped.com" title="title" fake="fake"> test_escaped &lt; &gt; </a>',
+ '<a href="https://url_with_params.com?a=b&c=d">label</a>',
+ ]
+
+ self.env["mail.render.mixin"]._shorten_links("".join(test_links), {})
+
+ trackers_to_find = [
+ [("url", "=", "https://gitlab.com"), ("label", "=", "test_label")],
+ [("url", "=", "https://test_542152qsdqsd.com")],
+ [
+ ("url", "=", "https://test_strange_html.com"),
+ ("label", "=", "test_strange_html_label"),
+ ],
+ [
+ ("url", "=", "https://test_escaped.com"),
+ ("label", "=", "test_escaped < >"),
+ ],
+ [
+ ("url", "=", "https://url_with_params.com?a=b&c=d"),
+ ("label", "=", "label"),
+ ],
+ ]
+ trackers_to_fail = [
+ [("url", "=", "https://test_542152qsdqsd.com"), ("label", "ilike", "_")]
+ ]
+
+ for tracker_to_find in trackers_to_find:
+ self.assertTrue(self.env["link.tracker"].search(tracker_to_find))
+
+ for tracker_to_fail in trackers_to_fail:
+ self.assertFalse(self.env["link.tracker"].search(tracker_to_fail))