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.

223 lines
8.9 KiB

  1. # Copyright 2017-2018 Camptocamp - Simone Orsi
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
  3. from odoo.tests.common import SavepointCase
  4. from odoo import exceptions
  5. class DigestCase(SavepointCase):
  6. @classmethod
  7. def setUpClass(cls):
  8. super(DigestCase, cls).setUpClass()
  9. cls.message_model = cls.env['mail.message']
  10. cls.subtype_model = cls.env['mail.message.subtype']
  11. cls.digest_model = cls.env['mail.digest']
  12. cls.conf_model = cls.env['user.notification.conf']
  13. user_model = cls.env['res.users'].with_context(
  14. no_reset_password=True, tracking_disable=True)
  15. cls.user1 = user_model.create({
  16. 'name': 'User 1',
  17. 'login': 'testuser1',
  18. 'email': 'testuser1@email.com',
  19. })
  20. cls.user2 = user_model.create({
  21. 'name': 'User 2',
  22. 'login': 'testuser2',
  23. 'email': 'testuser2@email.com',
  24. })
  25. cls.user3 = user_model.create({
  26. 'name': 'User 3',
  27. 'login': 'testuser3',
  28. 'email': 'testuser3@email.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. self.message_model.create({
  34. 'body': 'My Body 1',
  35. 'subtype_id': self.subtype1.id,
  36. })
  37. 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_user(self.user1)
  43. dig2 = self.digest_model._get_or_create_by_user(self.user1)
  44. self.assertEqual(dig1, dig2)
  45. def test_create_or_update_digest(self):
  46. partners = self.env['res.partner']
  47. partners |= self.user1.partner_id
  48. partners |= self.user2.partner_id
  49. message1 = self.message_model.create({
  50. 'body': 'My Body 1',
  51. 'subtype_id': self.subtype1.id,
  52. })
  53. message2 = self.message_model.create({
  54. 'body': 'My Body 2',
  55. 'subtype_id': self.subtype2.id,
  56. })
  57. # partner 1
  58. self.digest_model.create_or_update(self.user1.partner_id, message1)
  59. self.digest_model.create_or_update(self.user1.partner_id, message2)
  60. p1dig = self.digest_model._get_or_create_by_user(self.user1)
  61. self.assertIn(message1, p1dig.message_ids)
  62. self.assertIn(message2, p1dig.message_ids)
  63. # partner 2
  64. self.digest_model.create_or_update(self.user2.partner_id, message1)
  65. self.digest_model.create_or_update(self.user2.partner_id, message2)
  66. p2dig = self.digest_model._get_or_create_by_user(self.user2)
  67. self.assertIn(message1, p2dig.message_ids)
  68. self.assertIn(message2, p2dig.message_ids)
  69. def test_notify_user_digest(self):
  70. message = self.message_model.create({
  71. 'body': 'My Body 1',
  72. 'subtype_id': self.subtype1.id,
  73. })
  74. self.user1.digest_mode = True
  75. # notify partner
  76. self.user1.partner_id._notify(message)
  77. # we should find the message in its digest
  78. dig1 = self.digest_model._get_or_create_by_user(self.user1)
  79. self.assertIn(message, dig1.message_ids)
  80. def test_notify_partner_digest_followers(self):
  81. # subscribe a partner to the other one
  82. self.user3.partner_id.message_subscribe(
  83. partner_ids=self.user2.partner_id.ids)
  84. self.user1.digest_mode = True
  85. self.user2.digest_mode = True
  86. partners = self.user1.partner_id + self.user2.partner_id
  87. message = self.message_model.create({
  88. 'body': 'My Body 1',
  89. 'subtype_id': self.subtype1.id,
  90. 'res_id': self.user3.partner_id.id,
  91. 'model': 'res.partner',
  92. 'partner_ids': [(4, self.user1.partner_id.id)]
  93. })
  94. # notify partners
  95. partners.with_context(foo=1)._notify(message)
  96. # we should find the a digest for each partner
  97. dig1 = self.digest_model._get_by_user(self.user1)
  98. dig2 = self.digest_model._get_by_user(self.user2)
  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=True)._notify(message)
  106. # we should find the a digest for each partner
  107. self.assertTrue(self.digest_model._get_by_user(self.user1))
  108. self.assertFalse(self.digest_model._get_by_user(self.user2))
  109. def test_global_conf(self):
  110. for k in ('email', 'comment', 'notification'):
  111. self.assertIn(
  112. k, self.env['res.partner']._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(
  119. k, self.env['res.partner']._digest_enabled_message_types())
  120. self.assertNotIn(
  121. 'comment', self.env['res.partner']._digest_enabled_message_types())
  122. def test_notify_user_digest_global_disabled(self):
  123. # change global conf
  124. self.env['ir.config_parameter'].set_param(
  125. 'mail_digest.enabled_message_types',
  126. 'email,comment'
  127. )
  128. message = self.message_model.create({
  129. 'body': 'My Body 1',
  130. 'subtype_id': self.subtype1.id,
  131. # globally disabled type
  132. 'message_type': 'notification',
  133. })
  134. self.user1.digest_mode = True
  135. # notify partner
  136. self.user1.partner_id._notify(message)
  137. # we should not find any digest
  138. self.assertFalse(self.digest_model._get_by_user(self.user1))
  139. def _create_for_partner(self, partner):
  140. messages = {}
  141. for type_id in (self.subtype1.id, self.subtype2.id):
  142. for k in range(1, 3):
  143. key = '{}_{}'.format(type_id, k)
  144. messages[key] = self.message_model.create({
  145. 'subject': 'My Subject {}'.format(key),
  146. 'body': 'My Body {}'.format(key),
  147. 'subtype_id': type_id,
  148. })
  149. self.digest_model.create_or_update(partner, messages[key])
  150. return self.digest_model._get_by_user(partner.real_user_id)
  151. def test_digest_group_messages(self):
  152. dig = self._create_for_partner(self.user1.partner_id)
  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.user1.partner_id)
  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(
  165. [(4, self.user1.partner_id.id)], values['recipient_ids'])
  166. def test_digest_template(self):
  167. default = self.env.ref('mail_digest.default_digest_tmpl')
  168. dig = self._create_for_partner(self.user1.partner_id)
  169. # check default
  170. self.assertEqual(dig.digest_template_id, default)
  171. self.assertTrue(dig._get_email_values())
  172. # drop template
  173. dig.digest_template_id = False
  174. # pass a custom one: ok
  175. self.assertTrue(dig._get_email_values(template=default))
  176. # raise error if no template found
  177. with self.assertRaises(exceptions.UserError):
  178. dig._get_email_values()
  179. def test_digest_message_body_sanitize(self):
  180. dig = self._create_for_partner(self.user1.partner_id)
  181. message = self.message_model.create({
  182. 'body': '<p style="font-weight: bold">Body!</p>',
  183. 'subtype_id': self.subtype1.id,
  184. 'res_id': self.user3.partner_id.id,
  185. 'model': 'res.partner',
  186. 'partner_ids': [(4, self.user1.partner_id.id)]
  187. })
  188. body = dig.message_body(message)
  189. self.assertEqual(body, '<p>Body!</p>')
  190. def test_digest_message_body_no_sanitize(self):
  191. dig = self._create_for_partner(self.user1.partner_id)
  192. dig.sanitize_msg_body = False
  193. message = self.message_model.create({
  194. 'body': '<p style="font-weight: bold">Body!</p>',
  195. 'subtype_id': self.subtype1.id,
  196. 'res_id': self.user3.partner_id.id,
  197. 'model': 'res.partner',
  198. 'partner_ids': [(4, self.user1.partner_id.id)]
  199. })
  200. body = dig.message_body(message)
  201. self.assertEqual(
  202. # prevent fail on weird behavior:
  203. # sometimes you get a space, sometimes not :(
  204. body.replace('font-weight: bold', 'font-weight:bold'),
  205. '<p style="font-weight:bold">Body!</p>')