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.

247 lines
11 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Tecnativa - Jairo Llopis
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from contextlib import contextmanager
  5. from odoo.exceptions import ValidationError
  6. from odoo.tests.common import at_install, post_install, HttpCase
  7. @at_install(False)
  8. @post_install(True)
  9. class ActivityCase(HttpCase):
  10. def setUp(self):
  11. super(ActivityCase, self).setUp()
  12. # HACK https://github.com/odoo/odoo/issues/12237
  13. # TODO Remove hack in v12
  14. self._oldenv = self.env
  15. self.env = self._oldenv(self.cursor())
  16. # HACK end
  17. self.cron = self.env.ref("privacy_consent.cron_auto_consent")
  18. self.update_opt_out = self.env.ref("privacy_consent.update_opt_out")
  19. self.mt_consent_consent_new = self.env.ref(
  20. "privacy_consent.mt_consent_consent_new")
  21. self.mt_consent_acceptance_changed = self.env.ref(
  22. "privacy_consent.mt_consent_acceptance_changed")
  23. self.mt_consent_state_changed = self.env.ref(
  24. "privacy_consent.mt_consent_state_changed")
  25. # Some partners to ask for consent
  26. self.partners = self.env["res.partner"]
  27. self.partners += self.partners.create({
  28. "name": "consent-partner-0",
  29. "email": "partner0@example.com",
  30. "notify_email": "none",
  31. "opt_out": False,
  32. })
  33. self.partners += self.partners.create({
  34. "name": "consent-partner-1",
  35. "email": "partner1@example.com",
  36. "notify_email": "always",
  37. "opt_out": True,
  38. })
  39. self.partners += self.partners.create({
  40. "name": "consent-partner-2",
  41. "email": "partner2@example.com",
  42. "opt_out": False,
  43. })
  44. # Partner without email, on purpose
  45. self.partners += self.partners.create({
  46. "name": "consent-partner-3",
  47. "opt_out": True,
  48. })
  49. # Activity without consent
  50. self.activity_noconsent = self.env["privacy.activity"].create({
  51. "name": "activity_noconsent",
  52. "description": "I'm activity 1",
  53. })
  54. # Activity with auto consent, for all partners
  55. self.activity_auto = self.env["privacy.activity"].create({
  56. "name": "activity_auto",
  57. "description": "I'm activity auto",
  58. "subject_find": True,
  59. "subject_domain": repr([("id", "in", self.partners.ids)]),
  60. "consent_required": "auto",
  61. "default_consent": True,
  62. "server_action_id": self.update_opt_out.id,
  63. })
  64. # Activity with manual consent, skipping partner 0
  65. self.activity_manual = self.env["privacy.activity"].create({
  66. "name": "activity_manual",
  67. "description": "I'm activity 3",
  68. "subject_find": True,
  69. "subject_domain": repr([("id", "in", self.partners[1:].ids)]),
  70. "consent_required": "manual",
  71. "default_consent": False,
  72. "server_action_id": self.update_opt_out.id,
  73. })
  74. # HACK https://github.com/odoo/odoo/issues/12237
  75. # TODO Remove hack in v12
  76. def tearDown(self):
  77. self.env = self._oldenv
  78. super(ActivityCase, self).tearDown()
  79. # HACK https://github.com/odoo/odoo/issues/12237
  80. # TODO Remove hack in v12
  81. @contextmanager
  82. def release_cr(self):
  83. self.env.cr.release()
  84. yield
  85. self.env.cr.acquire()
  86. def check_activity_auto_properly_sent(self):
  87. """Check emails sent by ``self.activity_auto``."""
  88. consents = self.env["privacy.consent"].search([
  89. ("activity_id", "=", self.activity_auto.id),
  90. ])
  91. # Check sent mails
  92. for consent in consents:
  93. self.assertEqual(consent.state, "sent")
  94. messages = consent.mapped("message_ids")
  95. self.assertEqual(len(messages), 4)
  96. # 2nd message notifies creation
  97. self.assertEqual(
  98. messages[2].subtype_id,
  99. self.mt_consent_consent_new,
  100. )
  101. # 3rd message notifies subject
  102. # Placeholder links should be logged
  103. self.assertTrue("/privacy/consent/accept/" in messages[1].body)
  104. self.assertTrue("/privacy/consent/reject/" in messages[1].body)
  105. # Tokenized links shouldn't be logged
  106. self.assertFalse(consent._url(True) in messages[1].body)
  107. self.assertFalse(consent._url(False) in messages[1].body)
  108. # 4th message contains the state change
  109. self.assertEqual(
  110. messages[0].subtype_id,
  111. self.mt_consent_state_changed,
  112. )
  113. # Partner's opt_out should be synced with default consent
  114. self.assertFalse(consent.partner_id.opt_out)
  115. def test_default_template(self):
  116. """We have a good mail template by default."""
  117. good = self.env.ref("privacy_consent.template_consent")
  118. self.assertEqual(
  119. self.activity_noconsent.consent_template_id,
  120. good,
  121. )
  122. self.assertEqual(
  123. self.activity_noconsent.consent_template_default_body_html,
  124. good.body_html,
  125. )
  126. self.assertEqual(
  127. self.activity_noconsent.consent_template_default_subject,
  128. good.subject,
  129. )
  130. def test_find_subject_if_consent_required(self):
  131. """If user wants to require consent, it needs subjects."""
  132. # Test the onchange helper
  133. onchange_activity1 = self.env["privacy.activity"].new(
  134. self.activity_noconsent.copy_data()[0])
  135. self.assertFalse(onchange_activity1.subject_find)
  136. onchange_activity1.consent_required = "auto"
  137. onchange_activity1._onchange_consent_required_subject_find()
  138. self.assertTrue(onchange_activity1.subject_find)
  139. # Test very dumb user that forces an error
  140. with self.assertRaises(ValidationError):
  141. self.activity_noconsent.consent_required = "manual"
  142. def test_template_required_auto(self):
  143. """Automatic consent activities need a template."""
  144. self.activity_noconsent.subject_find = True
  145. self.activity_noconsent.consent_template_id = False
  146. self.activity_noconsent.consent_required = "manual"
  147. with self.assertRaises(ValidationError):
  148. self.activity_noconsent.consent_required = "auto"
  149. def test_generate_manually(self):
  150. """Manually-generated consents work as expected."""
  151. self.partners.write({"opt_out": False})
  152. result = self.activity_manual.action_new_consents()
  153. self.assertEqual(result["res_model"], "privacy.consent")
  154. consents = self.env[result["res_model"]].search(result["domain"])
  155. self.assertEqual(consents.mapped("state"), ["draft"] * 2)
  156. self.assertEqual(consents.mapped("partner_id.opt_out"), [False] * 2)
  157. self.assertEqual(consents.mapped("accepted"), [False] * 2)
  158. self.assertEqual(consents.mapped("last_metadata"), [False] * 2)
  159. # Check sent mails
  160. messages = consents.mapped("message_ids")
  161. self.assertEqual(len(messages), 4)
  162. subtypes = messages.mapped("subtype_id")
  163. self.assertTrue(subtypes & self.mt_consent_consent_new)
  164. self.assertFalse(subtypes & self.mt_consent_acceptance_changed)
  165. self.assertFalse(subtypes & self.mt_consent_state_changed)
  166. # Send one manual request
  167. action = consents[0].action_manual_ask()
  168. self.assertEqual(action["res_model"], "mail.compose.message")
  169. composer = self.env[action["res_model"]] \
  170. .with_context(active_ids=consents[0].ids,
  171. active_model=consents._name,
  172. **action["context"]).create({})
  173. composer.onchange_template_id_wrapper()
  174. composer.send_mail()
  175. messages = consents.mapped("message_ids") - messages
  176. self.assertEqual(len(messages), 2)
  177. self.assertEqual(messages[0].subtype_id, self.mt_consent_state_changed)
  178. self.assertEqual(consents.mapped("state"), ["sent", "draft"])
  179. self.assertEqual(consents.mapped("partner_id.opt_out"), [True, False])
  180. # Placeholder links should be logged
  181. self.assertTrue("/privacy/consent/accept/" in messages[1].body)
  182. self.assertTrue("/privacy/consent/reject/" in messages[1].body)
  183. # Tokenized links shouldn't be logged
  184. accept_url = consents[0]._url(True)
  185. reject_url = consents[0]._url(False)
  186. self.assertNotIn(accept_url, messages[1].body)
  187. self.assertNotIn(reject_url, messages[1].body)
  188. # Visit tokenized accept URL
  189. with self.release_cr():
  190. result = self.url_open(accept_url).read()
  191. self.assertIn("accepted", result)
  192. self.assertIn(reject_url, result)
  193. self.assertIn(self.activity_manual.name, result)
  194. self.assertIn(self.activity_manual.description, result)
  195. consents.invalidate_cache()
  196. self.assertEqual(consents.mapped("accepted"), [True, False])
  197. self.assertTrue(consents[0].last_metadata)
  198. self.assertFalse(consents[0].partner_id.opt_out)
  199. self.assertEqual(consents.mapped("state"), ["answered", "draft"])
  200. self.assertEqual(
  201. consents[0].message_ids[0].subtype_id,
  202. self.mt_consent_acceptance_changed,
  203. )
  204. # Visit tokenized reject URL
  205. with self.release_cr():
  206. result = self.url_open(reject_url).read()
  207. self.assertIn("rejected", result)
  208. self.assertIn(accept_url, result)
  209. self.assertIn(self.activity_manual.name, result)
  210. self.assertIn(self.activity_manual.description, result)
  211. consents.invalidate_cache()
  212. self.assertEqual(consents.mapped("accepted"), [False, False])
  213. self.assertTrue(consents[0].last_metadata)
  214. self.assertTrue(consents[0].partner_id.opt_out)
  215. self.assertEqual(consents.mapped("state"), ["answered", "draft"])
  216. self.assertEqual(
  217. consents[0].message_ids[0].subtype_id,
  218. self.mt_consent_acceptance_changed,
  219. )
  220. self.assertFalse(consents[1].last_metadata)
  221. def test_generate_automatically(self):
  222. """Automatically-generated consents work as expected."""
  223. result = self.activity_auto.action_new_consents()
  224. self.assertEqual(result["res_model"], "privacy.consent")
  225. self.check_activity_auto_properly_sent()
  226. def test_generate_cron(self):
  227. """Cron-generated consents work as expected."""
  228. self.cron.method_direct_trigger()
  229. self.check_activity_auto_properly_sent()
  230. def test_mail_template_without_links(self):
  231. """Cannot create mail template without needed links."""
  232. with self.assertRaises(ValidationError):
  233. self.activity_manual.consent_template_id.body_html = "No links :("