test_mail_render_mixin.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import re
  4. from odoo.tests import common, tagged
  5. from odoo.tools import TEXT_URL_REGEX
  6. @tagged('-at_install', 'post_install')
  7. class TestMailRenderMixin(common.TransactionCase):
  8. @classmethod
  9. def setUpClass(cls):
  10. super().setUpClass()
  11. cls.base_url = cls.env["mail.render.mixin"].get_base_url()
  12. def setUp(self):
  13. super().setUp()
  14. r = self.patch_requests()
  15. r.side_effect = NotImplementedError
  16. def test_shorten_links(self):
  17. test_links = [
  18. '<a href="https://gitlab.com" title="title" fake="fake">test_label</a>',
  19. '<a href="https://test_542152qsdqsd.com"/>',
  20. """<a href="https://third_test_54212.com">
  21. <img src="imagesrc"/>
  22. </a>
  23. """,
  24. """<a
  25. href="https://test_strange_html.com" title="title"
  26. fake='fake'
  27. > test_strange_html_label
  28. </a>
  29. """,
  30. '<a href="https://test_escaped.com" title="title" fake="fake"> test_escaped &lt; &gt; </a>',
  31. '<a href="https://url_with_params.com?a=b&c=d">label</a>',
  32. ]
  33. self.env["mail.render.mixin"]._shorten_links("".join(test_links), {})
  34. trackers_to_find = [
  35. [("url", "=", "https://gitlab.com"), ("label", "=", "test_label")],
  36. [("url", "=", "https://test_542152qsdqsd.com")],
  37. [
  38. ("url", "=", "https://test_strange_html.com"),
  39. ("label", "=", "test_strange_html_label"),
  40. ],
  41. [
  42. ("url", "=", "https://test_escaped.com"),
  43. ("label", "=", "test_escaped < >"),
  44. ],
  45. [
  46. ("url", "=", "https://url_with_params.com?a=b&c=d"),
  47. ("label", "=", "label"),
  48. ],
  49. ]
  50. trackers_to_fail = [
  51. [("url", "=", "https://test_542152qsdqsd.com"), ("label", "ilike", "_")]
  52. ]
  53. for tracker_to_find in trackers_to_find:
  54. self.assertTrue(self.env["link.tracker"].search(tracker_to_find))
  55. for tracker_to_fail in trackers_to_fail:
  56. self.assertFalse(self.env["link.tracker"].search(tracker_to_fail))
  57. def test_shorten_links_html_skip_shorts(self):
  58. old_content = self.env["mail.render.mixin"]._shorten_links(
  59. 'This is a link: <a href="https://test_542152qsdqsd.com">old</a>', {})
  60. created_short_url_match = re.search(TEXT_URL_REGEX, old_content)
  61. self.assertIsNotNone(created_short_url_match)
  62. created_short_url = created_short_url_match[0]
  63. self.assertRegex(created_short_url, "{base_url}/r/[\\w]+".format(base_url=self.base_url))
  64. new_content = self.env["mail.render.mixin"]._shorten_links(
  65. 'Reusing this old <a href="{old_short_url}">link</a> with a new <a href="https://odoo.com">one</a>'
  66. .format(old_short_url=created_short_url),
  67. {}
  68. )
  69. expected = re.compile(
  70. 'Reusing this old <a href="{old_short_url}">link</a> with a new <a href="{base_url}/r/[\\w]+">one</a>'
  71. .format(old_short_url=created_short_url, base_url=self.base_url)
  72. )
  73. self.assertRegex(new_content, expected)
  74. def test_shorten_links_html_including_base_url(self):
  75. content = (
  76. 'This is a link: <a href="https://www.worldcommunitygrid.org">https://www.worldcommunitygrid.org</a><br/>\n'
  77. 'This is another: <a href="{base_url}/web#debug=1&more=2">{base_url}</a><br/>\n'
  78. 'And a third: <a href="{base_url}">Here</a>\n'
  79. 'And a forth: <a href="{base_url}">Here</a>\n'
  80. 'And a fifth: <a href="{base_url}">Here too</a>\n'
  81. 'And a last, more complex: <a href="https://boinc.berkeley.edu/forum_thread.php?id=14544&postid=106833">There!</a>'
  82. .format(base_url=self.base_url)
  83. )
  84. expected_pattern = re.compile(
  85. 'This is a link: <a href="{base_url}/r/[\\w]+">https://www.worldcommunitygrid.org</a><br/>\n'
  86. 'This is another: <a href="{base_url}/r/[\\w]+">{base_url}</a><br/>\n'
  87. 'And a third: <a href="{base_url}/r/([\\w]+)">Here</a>\n'
  88. 'And a forth: <a href="{base_url}/r/([\\w]+)">Here</a>\n'
  89. 'And a fifth: <a href="{base_url}/r/([\\w]+)">Here too</a>\n'
  90. 'And a last, more complex: <a href="{base_url}/r/([\\w]+)">There!</a>'
  91. .format(base_url=self.base_url)
  92. )
  93. new_content = self.env["mail.render.mixin"]._shorten_links(content, {})
  94. self.assertRegex(new_content, expected_pattern)
  95. matches = expected_pattern.search(new_content).groups()
  96. # 3rd and 4th lead to the same short_url
  97. self.assertEqual(matches[0], matches[1])
  98. # 5th has different label but should currently lead to the same link
  99. self.assertEqual(matches[1], matches[2])
  100. def test_shorten_links_text_including_base_url(self):
  101. content = (
  102. 'This is a link: https://www.worldcommunitygrid.org\n'
  103. 'This is another: {base_url}/web#debug=1&more=2\n'
  104. 'A third: {base_url}\n'
  105. 'A forth: {base_url}\n'
  106. 'And a last, with question mark: https://boinc.berkeley.edu/forum_thread.php?id=14544&postid=106833'
  107. .format(base_url=self.base_url)
  108. )
  109. expected_pattern = re.compile(
  110. 'This is a link: {base_url}/r/[\\w]+\n'
  111. 'This is another: {base_url}/r/[\\w]+\n'
  112. 'A third: {base_url}/r/([\\w]+)\n'
  113. 'A forth: {base_url}/r/([\\w]+)\n'
  114. 'And a last, with question mark: {base_url}/r/([\\w]+)'
  115. .format(base_url=self.base_url)
  116. )
  117. new_content = self.env["mail.render.mixin"]._shorten_links_text(content, {})
  118. self.assertRegex(new_content, expected_pattern)
  119. matches = expected_pattern.search(new_content).groups()
  120. # 3rd and 4th lead to the same short_url
  121. self.assertEqual(matches[0], matches[1])
  122. def test_shorten_links_text_skip_shorts(self):
  123. old_content = self.env["mail.render.mixin"]._shorten_links_text(
  124. 'This is a link: https://test_542152qsdqsd.com', {})
  125. created_short_url_match = re.search(TEXT_URL_REGEX, old_content)
  126. self.assertIsNotNone(created_short_url_match)
  127. created_short_url = created_short_url_match[0]
  128. self.assertRegex(created_short_url, "{base_url}/r/[\\w]+".format(base_url=self.base_url))
  129. new_content = self.env["mail.render.mixin"]._shorten_links_text(
  130. 'Reusing this old link {old_short_url} with a new one, https://odoo.com</a>'
  131. .format(old_short_url=created_short_url),
  132. {}
  133. )
  134. expected = re.compile(
  135. 'Reusing this old link {old_short_url} with a new one, {base_url}/r/[\\w]+'
  136. .format(old_short_url=created_short_url, base_url=self.base_url)
  137. )
  138. self.assertRegex(new_content, expected)