# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import re
from markupsafe import Markup
from odoo.tests import common, tagged
from odoo.tools import mute_logger
from odoo.tools.mail import TEXT_URL_REGEX
@tagged('-at_install', 'post_install')
class TestMailRenderMixin(common.HttpCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.base_url = cls.env["mail.render.mixin"].get_base_url()
@mute_logger("odoo.tests.common.requests")
def test_shorten_links(self):
test_links = [
'test_label',
'',
"""
""",
"""
""",
""" test_strange_html_label
""",
' test_escaped < > ',
'label',
'',
'email label',
'THERE > there',
'Without href'
]
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://third_test_54212.com"), ("label", "=", "[media] imagesrc")],
[("url", "=", "https://fourthtesthasnolabel.com"), ("label", "=", False)],
[
("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"),
],
[("url", "=", self.base_url + '#')],
[("url", "=", "https://www.odoo.com?test=%20+3&this=that"), ("label", "=", "THERE > there")], # lxml unescaped
]
trackers_to_fail = [
[("url", "=", "https://test_542152qsdqsd.com"), ("label", "ilike", "_")],
[("url", "ilike", "%mailto:afunemail@somewhere.com")],
[("label", '=', 'Without href')]
]
for tracker_to_find in trackers_to_find:
with self.subTest(tracker_to_find=tracker_to_find):
self.assertTrue(self.env["link.tracker"].search(tracker_to_find))
for tracker_to_fail in trackers_to_fail:
with self.subTest(tracker_to_fail=tracker_to_fail):
self.assertFalse(self.env["link.tracker"].search(tracker_to_fail))
@mute_logger("odoo.tests.common.requests")
def test_shorten_links_html_different_labels(self):
# Covers multiple additions from web_editor's convert_inline.js classToStyle
content = """
There is a logo.png here,
there, and in this
and also

Nor escaped
Nor escaped blurp
Without matched label because inside tags are a pain and rare: here
Without alt, filename is used:
And here is the same: 
There is a logo.png here,
there, and in this
and also

Nor escaped
Nor escaped blurp
Without matched label because inside tags are a pain and rare: here
Without alt, filename is used:
And here is the same: 
This is a link: https://www.worldcommunitygrid.org
This is another: {self.base_url}
And a third: Here
And a forth: Here
And a fifth: Here too
And a 6th: Here2
And a 7th: Here2
And a last, more complex: There!
This is a link: https://www.worldcommunitygrid.org
This is another: {self.base_url}
And a third: Here
And a forth: Here
And a fifth: Here too
And a 6th: Here2
And a 7th: Here2
And a last, more complex: There!
A link: Link
') new_content = self.env["mail.render.mixin"]._shorten_links(content, {}) self.assertTrue(isinstance(new_content, Markup)) expected_pattern = re.compile(rf'A link: Link
') self.assertRegex(new_content, expected_pattern) @mute_logger("odoo.tests.common.requests") def test_shorten_links_html_skip_shorts(self): old_content = self.env["mail.render.mixin"]._shorten_links( 'This is a link: old', {}) created_short_url_match = re.search(TEXT_URL_REGEX, old_content) self.assertIsNotNone(created_short_url_match) created_short_url = created_short_url_match[0] self.assertRegex(created_short_url, "{base_url}/r/[\\w]+".format(base_url=self.base_url)) new_content = self.env["mail.render.mixin"]._shorten_links( f'Reusing this old link with a new one', {} ) expected = re.compile( rf'Reusing this old link with a new one' ) self.assertRegex(new_content, expected) @mute_logger("odoo.tests.common.requests") def test_shorten_links_text_including_base_url(self): content = f""" This is a link: https://www.worldcommunitygrid.org This is another: {self.base_url}/odoo?debug=1&more=2 A third: {self.base_url} A forth: {self.base_url} And a last, with question mark: https://boinc.berkeley.edu/forum_thread.php?id=14544&postid=106833""" expected_pattern = re.compile( rf""" This is a link: {self.base_url}/r/\w+ This is another: {self.base_url}/r/\w+ A third: {self.base_url}/r/(\w+) A forth: {self.base_url}/r/(\w+) And a last, with question mark: {self.base_url}/r/(\w+)""" ) new_content = self.env["mail.render.mixin"]._shorten_links_text(content, {}) self.assertRegex(new_content, expected_pattern) matches = expected_pattern.search(new_content).groups() # 3rd and 4th lead to the same short_url self.assertEqual(matches[0], matches[1]) def test_shorten_links_text_skip_shorts(self): old_content = self.env["mail.render.mixin"]._shorten_links_text( 'This is a link: https://test_542152qsdqsd.com', {}) created_short_url_match = re.search(TEXT_URL_REGEX, old_content) self.assertIsNotNone(created_short_url_match) created_short_url = created_short_url_match[0] self.assertRegex(created_short_url, rf"{self.base_url}/r/\w+") new_content = self.env["mail.render.mixin"]._shorten_links_text( f'Reusing this old link {created_short_url} with a new one, https://odoo.com', {} ) expected = re.compile(rf'Reusing this old link {created_short_url} with a new one, {self.base_url}/r/\w+') self.assertRegex(new_content, expected)