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.

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