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.

193 lines
7.7 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Simone Orsi <simone.orsi@camptocamp.com>
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  4. from odoo.tests.common import SavepointCase
  5. from odoo import exceptions
  6. class DigestCase(SavepointCase):
  7. @classmethod
  8. def setUpClass(cls):
  9. super(DigestCase, cls).setUpClass()
  10. cls.partner_model = cls.env['res.partner']
  11. cls.message_model = cls.env['mail.message']
  12. cls.subtype_model = cls.env['mail.message.subtype']
  13. cls.digest_model = cls.env['mail.digest']
  14. cls.conf_model = cls.env['partner.notification.conf']
  15. cls.partner1 = cls.partner_model.with_context(
  16. tracking_disable=1).create({
  17. 'name': 'Partner 1',
  18. 'email': 'partner1@test.foo.com',
  19. })
  20. cls.partner2 = cls.partner_model.with_context(
  21. tracking_disable=1).create({
  22. 'name': 'Partner 2',
  23. 'email': 'partner2@test.foo.com',
  24. })
  25. cls.partner3 = cls.partner_model.with_context(
  26. tracking_disable=1).create({
  27. 'name': 'Partner 3',
  28. 'email': 'partner3@test.foo.com',
  29. })
  30. cls.subtype1 = cls.subtype_model.create({'name': 'Type 1'})
  31. cls.subtype2 = cls.subtype_model.create({'name': 'Type 2'})
  32. def test_get_or_create_digest(self):
  33. message1 = self.message_model.create({
  34. 'body': 'My Body 1',
  35. 'subtype_id': self.subtype1.id,
  36. })
  37. message2 = self.message_model.create({
  38. 'body': 'My Body 2',
  39. 'subtype_id': self.subtype2.id,
  40. })
  41. # 2 messages, 1 digest container
  42. dig1 = self.digest_model._get_or_create_by_partner(
  43. self.partner1, message1)
  44. dig2 = self.digest_model._get_or_create_by_partner(
  45. self.partner1, message2)
  46. self.assertEqual(dig1, dig2)
  47. def test_create_or_update_digest(self):
  48. partners = self.partner_model
  49. partners |= self.partner1
  50. partners |= self.partner2
  51. message1 = self.message_model.create({
  52. 'body': 'My Body 1',
  53. 'subtype_id': self.subtype1.id,
  54. })
  55. message2 = self.message_model.create({
  56. 'body': 'My Body 2',
  57. 'subtype_id': self.subtype2.id,
  58. })
  59. # partner 1
  60. self.digest_model.create_or_update(self.partner1, message1)
  61. self.digest_model.create_or_update(self.partner1, message2)
  62. p1dig = self.digest_model._get_or_create_by_partner(self.partner1)
  63. self.assertIn(message1, p1dig.message_ids)
  64. self.assertIn(message2, p1dig.message_ids)
  65. # partner 2
  66. self.digest_model.create_or_update(self.partner2, message1)
  67. self.digest_model.create_or_update(self.partner2, message2)
  68. p2dig = self.digest_model._get_or_create_by_partner(self.partner2)
  69. self.assertIn(message1, p2dig.message_ids)
  70. self.assertIn(message2, p2dig.message_ids)
  71. def test_notify_partner_digest(self):
  72. message = self.message_model.create({
  73. 'body': 'My Body 1',
  74. 'subtype_id': self.subtype1.id,
  75. })
  76. self.partner1.notify_email = 'digest'
  77. # notify partner
  78. self.partner1._notify(message)
  79. # we should find the message in its digest
  80. dig1 = self.digest_model._get_or_create_by_partner(
  81. self.partner1, message)
  82. self.assertIn(message, dig1.message_ids)
  83. def test_notify_partner_digest_followers(self):
  84. self.partner3.message_subscribe(self.partner2.ids)
  85. self.partner1.notify_email = 'digest'
  86. self.partner2.notify_email = 'digest'
  87. partners = self.partner1 + self.partner2
  88. message = self.message_model.create({
  89. 'body': 'My Body 1',
  90. 'subtype_id': self.subtype1.id,
  91. 'res_id': self.partner3.id,
  92. 'model': 'res.partner',
  93. 'partner_ids': [(4, self.partner1.id)]
  94. })
  95. # notify partners
  96. partners._notify(message)
  97. # we should find the a digest for each partner
  98. dig1 = self.digest_model._get_by_partner(self.partner1)
  99. dig2 = self.digest_model._get_by_partner(self.partner2)
  100. # and the message in them
  101. self.assertIn(message, dig1.message_ids)
  102. self.assertIn(message, dig2.message_ids)
  103. # now we exclude followers
  104. dig1.unlink()
  105. dig2.unlink()
  106. partners.with_context(notify_only_recipients=1)._notify(message)
  107. # we should find the a digest for each partner
  108. self.assertTrue(self.digest_model._get_by_partner(self.partner1))
  109. self.assertFalse(self.digest_model._get_by_partner(self.partner2))
  110. def test_global_conf(self):
  111. for k in ('email', 'comment', 'notification'):
  112. self.assertIn(k, self.partner1._digest_enabled_message_types())
  113. self.env['ir.config_parameter'].set_param(
  114. 'mail_digest.enabled_message_types',
  115. 'email,notification'
  116. )
  117. for k in ('email', 'notification'):
  118. self.assertIn(k, self.partner1._digest_enabled_message_types())
  119. self.assertNotIn(
  120. 'comment', self.partner1._digest_enabled_message_types())
  121. def test_notify_partner_digest_global_disabled(self):
  122. # change global conf
  123. self.env['ir.config_parameter'].set_param(
  124. 'mail_digest.enabled_message_types',
  125. 'email,comment'
  126. )
  127. message = self.message_model.create({
  128. 'body': 'My Body 1',
  129. 'subtype_id': self.subtype1.id,
  130. # globally disabled type
  131. 'message_type': 'notification',
  132. })
  133. self.partner1.notify_email = 'digest'
  134. # notify partner
  135. self.partner1._notify(message)
  136. # we should not find any digest
  137. self.assertFalse(self.digest_model._get_by_partner(self.partner1))
  138. def _create_for_partner(self, partner):
  139. messages = {}
  140. for type_id in (self.subtype1.id, self.subtype2.id):
  141. for k in xrange(1, 3):
  142. key = '{}_{}'.format(type_id, k)
  143. messages[key] = self.message_model.create({
  144. 'subject': 'My Subject {}'.format(key),
  145. 'body': 'My Body {}'.format(key),
  146. 'subtype_id': type_id,
  147. })
  148. self.digest_model.create_or_update(
  149. partner, messages[key])
  150. return self.digest_model._get_or_create_by_partner(partner)
  151. def test_digest_group_messages(self):
  152. dig = self._create_for_partner(self.partner1)
  153. grouped = dig._message_group_by()
  154. for type_id in (self.subtype1.id, self.subtype2.id):
  155. self.assertIn(type_id, grouped)
  156. self.assertEqual(len(grouped[type_id]), 2)
  157. def test_digest_mail_values(self):
  158. dig = self._create_for_partner(self.partner1)
  159. values = dig._get_email_values()
  160. expected = ('recipient_ids', 'subject', 'body_html')
  161. for k in expected:
  162. self.assertIn(k, values)
  163. self.assertEqual(self.env.user.company_id.email, values['email_from'])
  164. self.assertEqual([(4, self.partner1.id)], values['recipient_ids'])
  165. def test_digest_template(self):
  166. default = self.env.ref('mail_digest.default_digest_tmpl')
  167. dig = self._create_for_partner(self.partner1)
  168. # check default
  169. self.assertEqual(dig.digest_template_id, default)
  170. self.assertTrue(dig._get_email_values())
  171. # drop template
  172. dig.digest_template_id = False
  173. # pass a custom one: ok
  174. self.assertTrue(dig._get_email_values(template=default))
  175. # raise error if no template found
  176. with self.assertRaises(exceptions.UserError):
  177. dig._get_email_values()