common.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import werkzeug
  4. from lxml import etree
  5. from unittest.mock import patch
  6. from odoo.tests import common
  7. class MockLinkTracker(common.BaseCase):
  8. def setUp(self):
  9. super(MockLinkTracker, self).setUp()
  10. def _get_title_from_url(url):
  11. return "Test_TITLE"
  12. link_tracker_title_patch = patch('odoo.addons.link_tracker.models.link_tracker.LinkTracker._get_title_from_url', wraps=_get_title_from_url)
  13. self.startPatcher(link_tracker_title_patch)
  14. def _get_href_from_anchor_id(self, body, anchor_id):
  15. """ Parse en html body to find the href of an element given its ID. """
  16. html = etree.fromstring(body, parser=etree.HTMLParser())
  17. return html.xpath("//*[@id='%s']" % anchor_id)[0].attrib.get('href')
  18. def _get_code_from_short_url(self, short_url):
  19. return self.env['link.tracker.code'].sudo().search([
  20. ('code', '=', short_url.split('/r/')[-1])
  21. ])
  22. def _get_tracker_from_short_url(self, short_url):
  23. return self._get_code_from_short_url(short_url).link_id
  24. def assertLinkShortenedHtml(self, body, link_info, link_params=None):
  25. """ Find shortened links in an HTML content. Usage :
  26. self.assertLinkShortenedHtml(
  27. message.body,
  28. ('url0', 'http://www.odoo.com', True),
  29. {'utm_campaign': self.utm_c.name, 'utm_medium': self.utm_m.name}
  30. )
  31. """
  32. (anchor_id, url, is_shortened) = link_info
  33. anchor_href = self._get_href_from_anchor_id(body, anchor_id)
  34. if is_shortened:
  35. self.assertTrue('/r/' in anchor_href, '%s should be shortened: %s' % (anchor_id, anchor_href))
  36. link_tracker = self._get_tracker_from_short_url(anchor_href)
  37. self.assertEqual(url, link_tracker.url)
  38. self.assertLinkParams(url, link_tracker, link_params=link_params)
  39. else:
  40. self.assertTrue('/r/' not in anchor_href, '%s should not be shortened: %s' % (anchor_id, anchor_href))
  41. self.assertEqual(anchor_href, url)
  42. def assertLinkShortenedText(self, body, link_info, link_params=None):
  43. """ Find shortened links in an text content. Usage :
  44. self.assertLinkShortenedText(
  45. message.body,
  46. ('http://www.odoo.com', True),
  47. {'utm_campaign': self.utm_c.name, 'utm_medium': self.utm_m.name}
  48. )
  49. """
  50. (url, is_shortened) = link_info
  51. link_tracker = self.env['link.tracker'].search([('url', '=', url)])
  52. if is_shortened:
  53. self.assertEqual(len(link_tracker), 1)
  54. self.assertIn(link_tracker.short_url, body, '%s should be shortened' % (url))
  55. self.assertLinkParams(url, link_tracker, link_params=link_params)
  56. else:
  57. self.assertEqual(len(link_tracker), 0)
  58. self.assertIn(url, body)
  59. def assertLinkParams(self, url, link_tracker, link_params=None):
  60. """ Usage
  61. self.assertLinkTracker(
  62. 'http://www.example.com',
  63. link_tracker,
  64. {'utm_campaign': self.utm_c.name, 'utm_medium': self.utm_m.name}
  65. )
  66. """
  67. # check UTMS are correctly set on redirect URL
  68. original_url = werkzeug.urls.url_parse(url)
  69. redirect_url = werkzeug.urls.url_parse(link_tracker.redirected_url)
  70. redirect_params = redirect_url.decode_query().to_dict(flat=True)
  71. self.assertEqual(redirect_url.scheme, original_url.scheme)
  72. self.assertEqual(redirect_url.decode_netloc(), original_url.decode_netloc())
  73. self.assertEqual(redirect_url.path, original_url.path)
  74. if link_params:
  75. self.assertEqual(redirect_params, link_params)