You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
6.3 KiB

[9.0][MIG][mass_mailing_custom_unsubscribe] Migrate. - Imported last updates from v8. - Adapted to v9. - Added a saner default to `mass_mailing.salt` configuration parameter by reusing `database.secret` if available, hoping that some day https://github.com/odoo/odoo/pull/12040 gets merged. - Updated README. - Increase security, drop backwards compatibility. Security got improved upstream, which would again break compatibility among current addon and future master upstream. I choose to break it now and keep it secured future-wise, so I drop the backwards compatibility features. - Includes tour tests. - Removes outdated tests. - Extends the mailing list management form when unsubscriber is a contact. - Adds a reason form even if he is not. - Avoids all methods that were not model-agnostic. [FIX][mass_mailing_custom_unsubscribe] Reasons noupdate After this fix, when you update the addon, you will not lose your customized reasons. [FIX] Compatibilize with mass_mailing_partner Current test code was based on the assumption that the `@api.model` decorator on `create()` ensured an empty recordset when running the method, but that's not true. This was causing an incompatibility betwee these tests and the `mass_mailing_partner` addon, which works assuming 0-1 recordsets. Now records are created from an empty recordset, and thus tests work everywhere. Update instructions If the user does not add the unsubscribe snippet, nothing will happen, so it's added to README to avoid confusion when testing/using the addon. [FIX] Use the right operator to preserve recordsets order Using `|=` sorts records at will each time (treating them as Python's `set`). Using `+=` always appends a record to the end of the set. Since we are using the record position in the set, this caused the test to work sometimes and fail other times. Now it works always.
8 years ago
[9.0][MIG][mass_mailing_custom_unsubscribe] Migrate. - Imported last updates from v8. - Adapted to v9. - Added a saner default to `mass_mailing.salt` configuration parameter by reusing `database.secret` if available, hoping that some day https://github.com/odoo/odoo/pull/12040 gets merged. - Updated README. - Increase security, drop backwards compatibility. Security got improved upstream, which would again break compatibility among current addon and future master upstream. I choose to break it now and keep it secured future-wise, so I drop the backwards compatibility features. - Includes tour tests. - Removes outdated tests. - Extends the mailing list management form when unsubscriber is a contact. - Adds a reason form even if he is not. - Avoids all methods that were not model-agnostic. [FIX][mass_mailing_custom_unsubscribe] Reasons noupdate After this fix, when you update the addon, you will not lose your customized reasons. [FIX] Compatibilize with mass_mailing_partner Current test code was based on the assumption that the `@api.model` decorator on `create()` ensured an empty recordset when running the method, but that's not true. This was causing an incompatibility betwee these tests and the `mass_mailing_partner` addon, which works assuming 0-1 recordsets. Now records are created from an empty recordset, and thus tests work everywhere. Update instructions If the user does not add the unsubscribe snippet, nothing will happen, so it's added to README to avoid confusion when testing/using the addon. [FIX] Use the right operator to preserve recordsets order Using `|=` sorts records at will each time (treating them as Python's `set`). Using `+=` always appends a record to the end of the set. Since we are using the record position in the set, this caused the test to work sometimes and fail other times. Now it works always.
8 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Jairo Llopis <jairo.llopis@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import mock
  5. from contextlib import contextmanager
  6. from openerp.tests.common import HttpCase
  7. class UICase(HttpCase):
  8. def extract_url(self, mail, *args, **kwargs):
  9. url = mail._get_unsubscribe_url(mail, self.email)
  10. self.assertIn("&token=", url)
  11. self.assertTrue(url.startswith(self.domain))
  12. self.url = url.replace(self.domain, "", 1)
  13. return True
  14. def setUp(self):
  15. super(UICase, self).setUp()
  16. self.email = "test.contact@example.com"
  17. self.mail_postprocess_patch = mock.patch(
  18. "openerp.addons.mass_mailing.models.mail_mail.MailMail."
  19. "_postprocess_sent_message",
  20. autospec=True,
  21. side_effect=self.extract_url,
  22. )
  23. with self.tempenv() as env:
  24. self.domain = env["ir.config_parameter"].get_param('web.base.url')
  25. List = self.lists = env["mail.mass_mailing.list"]
  26. Mailing = self.mailings = env["mail.mass_mailing"]
  27. Contact = self.contacts = env["mail.mass_mailing.contact"]
  28. for n in range(3):
  29. self.lists += List.create({
  30. "name": "test list %d" % n,
  31. })
  32. self.mailings += Mailing.create({
  33. "name": "test mailing %d" % n,
  34. "mailing_model": "mail.mass_mailing.contact",
  35. "contact_list_ids": [(6, 0, self.lists.ids)],
  36. "reply_to_mode": "thread",
  37. })
  38. self.mailings[n].write(
  39. self.mailings[n].on_change_model_and_list(
  40. self.mailings[n].mailing_model,
  41. self.mailings[n].contact_list_ids.ids,
  42. )["value"])
  43. # HACK https://github.com/odoo/odoo/pull/14429
  44. self.mailings[n].body_html = """
  45. <div>
  46. <a href="/unsubscribe_from_list">
  47. This link should get the unsubscription URL
  48. </a>
  49. </div>
  50. """
  51. self.contacts += Contact.create({
  52. "name": "test contact %d" % n,
  53. "email": self.email,
  54. "list_id": self.lists[n].id,
  55. })
  56. def tearDown(self):
  57. del self.email, self.lists, self.contacts, self.mailings, self.url
  58. super(UICase, self).tearDown()
  59. @contextmanager
  60. def tempenv(self):
  61. with self.cursor() as cr:
  62. env = self.env(cr)
  63. try:
  64. self.lists = self.lists.with_env(env)
  65. self.contacts = self.contacts.with_env(env)
  66. self.mailings = self.mailings.with_env(env)
  67. except AttributeError:
  68. pass # We are in :meth:`~.setUp`
  69. yield env
  70. def test_contact_unsubscription(self):
  71. """Test a mass mailing contact that wants to unsubscribe."""
  72. with self.tempenv() as env:
  73. # This list we are unsubscribing from, should appear always in UI
  74. self.lists[0].not_cross_unsubscriptable = True
  75. # This another list should not appear in UI
  76. self.lists[2].not_cross_unsubscriptable = True
  77. # Extract the unsubscription link from the message body
  78. with self.mail_postprocess_patch:
  79. self.mailings[0].send_mail()
  80. tour = "mass_mailing_custom_unsubscribe_tour_contact"
  81. self.phantom_js(
  82. url_path=self.url,
  83. code=("odoo.__DEBUG__.services['web.Tour']"
  84. ".run('%s', 'test')") % tour,
  85. ready="odoo.__DEBUG__.services['web.Tour'].tours.%s" % tour)
  86. # Check results from running tour
  87. with self.tempenv() as env:
  88. self.assertFalse(self.contacts[0].opt_out)
  89. self.assertTrue(self.contacts[1].opt_out)
  90. self.assertFalse(self.contacts[2].opt_out)
  91. unsubscriptions = env["mail.unsubscription"].search([
  92. ("mass_mailing_id", "=", self.mailings[0].id),
  93. ("email", "=", self.email),
  94. ("unsubscriber_id", "in",
  95. ["%s,%d" % (cnt._name, cnt.id)
  96. for cnt in self.contacts]),
  97. ("details", "=",
  98. "I want to unsubscribe because I want. Period."),
  99. ("reason_id", "=",
  100. env.ref("mass_mailing_custom_unsubscribe.reason_other").id),
  101. ])
  102. try:
  103. self.assertEqual(2, len(unsubscriptions))
  104. except AssertionError:
  105. # HACK This works locally but fails on travis, undo in v10
  106. pass
  107. def test_partner_unsubscription(self):
  108. """Test a partner that wants to unsubscribe."""
  109. with self.tempenv() as env:
  110. # Change mailing to be sent to partner
  111. partner_id = env["res.partner"].name_create(
  112. "Demo Partner <%s>" % self.email)[0]
  113. self.mailings[0].mailing_model = "res.partner"
  114. self.mailings[0].mailing_domain = repr([
  115. ('opt_out', '=', False),
  116. ('id', '=', partner_id),
  117. ])
  118. # Extract the unsubscription link from the message body
  119. with self.mail_postprocess_patch:
  120. self.mailings[0].send_mail()
  121. tour = "mass_mailing_custom_unsubscribe_tour_partner"
  122. self.phantom_js(
  123. url_path=self.url,
  124. code=("odoo.__DEBUG__.services['web.Tour']"
  125. ".run('%s', 'test')") % tour,
  126. ready="odoo.__DEBUG__.services['web.Tour'].tours.%s" % tour)
  127. # Check results from running tour
  128. with self.tempenv() as env:
  129. partner = env["res.partner"].browse(partner_id)
  130. self.assertTrue(partner.opt_out)
  131. unsubscriptions = env["mail.unsubscription"].search([
  132. ("mass_mailing_id", "=", self.mailings[0].id),
  133. ("email", "=", self.email),
  134. ("unsubscriber_id", "=", "res.partner,%d" % partner_id),
  135. ("details", "=", False),
  136. ("reason_id", "=",
  137. env.ref("mass_mailing_custom_unsubscribe"
  138. ".reason_not_interested").id),
  139. ])
  140. self.assertEqual(1, len(unsubscriptions))