test_link_tracker.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.addons.link_tracker.tests.common import MockLinkTracker
  4. from odoo.exceptions import UserError
  5. from odoo.tests import common, tagged
  6. @tagged('link_tracker')
  7. class TestLinkTracker(common.TransactionCase, MockLinkTracker):
  8. def setUp(self):
  9. super(TestLinkTracker, self).setUp()
  10. self._web_base_url = 'https://test.odoo.com'
  11. self.env['ir.config_parameter'].sudo().set_param('web.base.url', self._web_base_url)
  12. def test_create(self):
  13. link_trackers = self.env['link.tracker'].create([
  14. {
  15. 'url': 'odoo.com',
  16. 'title': 'Odoo',
  17. }, {
  18. 'url': 'example.com',
  19. 'title': 'Odoo',
  20. }, {
  21. 'url': 'http://test.example.com',
  22. 'title': 'Odoo',
  23. },
  24. ])
  25. self.assertEqual(
  26. link_trackers.mapped('url'),
  27. ['http://odoo.com', 'http://example.com', 'http://test.example.com'],
  28. )
  29. self.assertEqual(len(set(link_trackers.mapped('code'))), 3)
  30. def test_search_or_create(self):
  31. link_tracker_1 = self.env['link.tracker'].create({
  32. 'url': 'https://odoo.com',
  33. 'title': 'Odoo',
  34. })
  35. link_tracker_2 = self.env['link.tracker'].search_or_create({
  36. 'url': 'https://odoo.com',
  37. 'title': 'Odoo',
  38. })
  39. self.assertEqual(link_tracker_1, link_tracker_2)
  40. link_tracker_3 = self.env['link.tracker'].search_or_create({
  41. 'url': 'https://odoo.be',
  42. 'title': 'Odoo',
  43. })
  44. self.assertNotEqual(link_tracker_1, link_tracker_3)
  45. def test_constraint(self):
  46. campaign_id = self.env['utm.campaign'].search([], limit=1)
  47. self.env['link.tracker'].create({
  48. 'url': 'https://odoo.com',
  49. 'title': 'Odoo',
  50. })
  51. link_1 = self.env['link.tracker'].create({
  52. 'url': '2nd url',
  53. 'title': 'Odoo',
  54. 'campaign_id': campaign_id.id,
  55. })
  56. with self.assertRaises(UserError):
  57. self.env['link.tracker'].create({
  58. 'url': 'https://odoo.com',
  59. 'title': 'Odoo',
  60. })
  61. with self.assertRaises(UserError):
  62. self.env['link.tracker'].create({
  63. 'url': '2nd url',
  64. 'title': 'Odoo',
  65. 'campaign_id': campaign_id.id,
  66. })
  67. link_2 = self.env['link.tracker'].create({
  68. 'url': '2nd url',
  69. 'title': 'Odoo',
  70. 'campaign_id': campaign_id.id,
  71. 'medium_id': self.env['utm.medium'].search([], limit=1).id
  72. })
  73. # test in batch
  74. with self.assertRaises(UserError):
  75. # both link trackers on which we write will have the same values
  76. (link_1 | link_2).write({'campaign_id': False, 'medium_id': False})
  77. with self.assertRaises(UserError):
  78. (link_1 | link_2).write({'medium_id': False})
  79. def test_no_external_tracking(self):
  80. self.env['ir.config_parameter'].set_param('link_tracker.no_external_tracking', '1')
  81. campaign = self.env['utm.campaign'].create({'name': 'campaign'})
  82. source = self.env['utm.source'].create({'name': 'source'})
  83. medium = self.env['utm.medium'].create({'name': 'medium'})
  84. expected_utm_params = {
  85. 'utm_campaign': campaign.name,
  86. 'utm_source': source.name,
  87. 'utm_medium': medium.name,
  88. }
  89. # URL to an external website -> UTM parameters should no be added
  90. # because the system parameter "no_external_tracking" is set
  91. link = self.env['link.tracker'].create({
  92. 'url': 'http://external.com/test?a=example.com',
  93. 'campaign_id': campaign.id,
  94. 'source_id': source.id,
  95. 'medium_id': medium.id,
  96. 'title': 'Title',
  97. })
  98. self.assertLinkParams(
  99. 'http://external.com/test',
  100. link,
  101. {'a': 'example.com'}
  102. )
  103. # URL to the local website -> UTM parameters should be added since we know we handle them
  104. # even though the parameter "no_external_tracking" is enabled
  105. link.url = f'{self._web_base_url}/test?a=example.com'
  106. self.assertLinkParams(
  107. f'{self._web_base_url}/test',
  108. link,
  109. {**expected_utm_params, 'a': 'example.com'}
  110. )
  111. # Relative URL to the local website -> UTM parameters should be added since we know we handle them
  112. # even though the parameter "no_external_tracking" is enabled
  113. link.url = '/test?a=example.com'
  114. self.assertLinkParams(
  115. '/test',
  116. link,
  117. {**expected_utm_params, 'a': 'example.com'}
  118. )
  119. # Deactivate the system parameter
  120. self.env['ir.config_parameter'].set_param('link_tracker.no_external_tracking', False)
  121. # URL to an external website -> UTM parameters should be added since
  122. # the system parameter "link_tracker.no_external_tracking" is disabled
  123. link.url = 'http://external.com/test?a=example.com'
  124. self.assertLinkParams(
  125. 'http://external.com/test',
  126. link,
  127. {**expected_utm_params, 'a': 'example.com'}
  128. )