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.

147 lines
5.9 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 openerp.tests.common import TransactionCase
  5. class DigestCase(TransactionCase):
  6. def setUp(self):
  7. super(DigestCase, self).setUp()
  8. self.partner_model = self.env['res.partner']
  9. self.message_model = self.env['mail.message']
  10. self.subtype_model = self.env['mail.message.subtype']
  11. self.digest_model = self.env['mail.digest']
  12. self.conf_model = self.env['partner.notification.conf']
  13. self.partner1 = self.partner_model.with_context(
  14. tracking_disable=1).create({
  15. 'name': 'Partner 1',
  16. 'email': 'partner1@test.foo.com',
  17. })
  18. self.partner2 = self.partner_model.with_context(
  19. tracking_disable=1).create({
  20. 'name': 'Partner 2',
  21. 'email': 'partner2@test.foo.com',
  22. })
  23. self.partner3 = self.partner_model.with_context(
  24. tracking_disable=1).create({
  25. 'name': 'Partner 3',
  26. 'email': 'partner3@test.foo.com',
  27. })
  28. self.subtype1 = self.subtype_model.create({'name': 'Type 1'})
  29. self.subtype2 = self.subtype_model.create({'name': 'Type 2'})
  30. def test_get_or_create_digest(self):
  31. message1 = self.message_model.create({
  32. 'body': 'My Body 1',
  33. 'subtype_id': self.subtype1.id,
  34. })
  35. message2 = self.message_model.create({
  36. 'body': 'My Body 2',
  37. 'subtype_id': self.subtype2.id,
  38. })
  39. # 2 messages, 1 digest container
  40. dig1 = self.digest_model._get_or_create_by_partner(
  41. self.partner1, message1)
  42. dig2 = self.digest_model._get_or_create_by_partner(
  43. self.partner1, message2)
  44. self.assertEqual(dig1, dig2)
  45. def test_create_or_update_digest(self):
  46. partners = self.partner_model
  47. partners |= self.partner1
  48. partners |= self.partner2
  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.partner1, message1)
  59. self.digest_model.create_or_update(self.partner1, message2)
  60. p1dig = self.digest_model._get_or_create_by_partner(self.partner1)
  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.partner2, message1)
  65. self.digest_model.create_or_update(self.partner2, message2)
  66. p2dig = self.digest_model._get_or_create_by_partner(self.partner2)
  67. self.assertIn(message1, p2dig.message_ids)
  68. self.assertIn(message2, p2dig.message_ids)
  69. def test_notify_partner_digest(self):
  70. message = self.message_model.create({
  71. 'body': 'My Body 1',
  72. 'subtype_id': self.subtype1.id,
  73. })
  74. self.partner1.notify_email = 'digest'
  75. # notify partner
  76. self.partner1._notify(message)
  77. # we should find the message in its digest
  78. dig1 = self.digest_model._get_or_create_by_partner(
  79. self.partner1, message)
  80. self.assertIn(message, dig1.message_ids)
  81. def test_notify_partner_digest_followers(self):
  82. self.partner3.message_subscribe(self.partner2.ids)
  83. self.partner1.notify_email = 'digest'
  84. self.partner2.notify_email = 'digest'
  85. partners = self.partner1 + self.partner2
  86. message = self.message_model.create({
  87. 'body': 'My Body 1',
  88. 'subtype_id': self.subtype1.id,
  89. 'res_id': self.partner3.id,
  90. 'model': 'res.partner',
  91. 'partner_ids': [(4, self.partner1.id)]
  92. })
  93. # notify partners
  94. partners._notify(message)
  95. # we should find the a digest for each partner
  96. dig1 = self.digest_model._get_by_partner(self.partner1)
  97. dig2 = self.digest_model._get_by_partner(self.partner2)
  98. # and the message in them
  99. self.assertIn(message, dig1.message_ids)
  100. self.assertIn(message, dig2.message_ids)
  101. # now we exclude followers
  102. dig1.unlink()
  103. dig2.unlink()
  104. partners.with_context(notify_only_recipients=1)._notify(message)
  105. # we should find the a digest for each partner
  106. self.assertTrue(self.digest_model._get_by_partner(self.partner1))
  107. self.assertFalse(self.digest_model._get_by_partner(self.partner2))
  108. def _create_for_partner(self, partner):
  109. messages = {}
  110. for type_id in (self.subtype1.id, self.subtype2.id):
  111. for k in xrange(1, 3):
  112. key = '{}_{}'.format(type_id, k)
  113. messages[key] = self.message_model.create({
  114. 'subject': 'My Subject {}'.format(key),
  115. 'body': 'My Body {}'.format(key),
  116. 'subtype_id': type_id,
  117. })
  118. self.digest_model.create_or_update(
  119. partner, messages[key])
  120. return self.digest_model._get_or_create_by_partner(partner)
  121. def test_digest_group_messages(self):
  122. dig = self._create_for_partner(self.partner1)
  123. grouped = dig._message_group_by()
  124. for type_id in (self.subtype1.id, self.subtype2.id):
  125. self.assertIn(type_id, grouped)
  126. self.assertEqual(len(grouped[type_id]), 2)
  127. def test_digest_mail_values(self):
  128. dig = self._create_for_partner(self.partner1)
  129. values = dig._get_email_values()
  130. expected = ('recipient_ids', 'subject', 'body_html')
  131. for k in expected:
  132. self.assertIn(k, values)
  133. self.assertEqual(self.env.user.company_id.email, values['email_from'])
  134. self.assertEqual([(4, self.partner1.id)], values['recipient_ids'])