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.

183 lines
7.7 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. class PartnerDomainCase(SavepointCase):
  5. @classmethod
  6. def setUpClass(cls):
  7. super(PartnerDomainCase, cls).setUpClass()
  8. cls.partner_model = cls.env['res.partner']
  9. cls.message_model = cls.env['mail.message']
  10. cls.subtype_model = cls.env['mail.message.subtype']
  11. user_model = cls.env['res.users'].with_context(
  12. no_reset_password=True, tracking_disable=True)
  13. cls.user1 = user_model.create({
  14. 'name': 'User 1',
  15. 'login': 'testuser1',
  16. 'email': 'testuser1@email.com',
  17. })
  18. cls.user2 = user_model.create({
  19. 'name': 'User 2',
  20. 'login': 'testuser2',
  21. 'email': 'testuser2@email.com',
  22. })
  23. cls.user3 = user_model.create({
  24. 'name': 'User 3',
  25. 'login': 'testuser3',
  26. 'email': 'testuser3@email.com',
  27. })
  28. cls.partner1 = cls.user1.partner_id
  29. cls.partner2 = cls.user2.partner_id
  30. cls.partner3 = cls.user3.partner_id
  31. cls.subtype1 = cls.subtype_model.create({'name': 'Type 1'})
  32. cls.subtype2 = cls.subtype_model.create({'name': 'Type 2'})
  33. def _assert_found(self, partner, domain, not_found=False):
  34. if not_found:
  35. self.assertNotIn(partner, self.partner_model.search(domain))
  36. else:
  37. self.assertIn(partner, self.partner_model.search(domain))
  38. def test_notify_domains_always(self):
  39. # we don't set recipients
  40. # because we call `_get_notify_by_email_domain` directly
  41. message = self.message_model.create({'body': 'My Body', })
  42. partner = self.partner1
  43. partner.real_user_id.notification_type = 'email'
  44. domain = partner._get_notify_by_email_domain(message)
  45. self._assert_found(partner, domain)
  46. domain = partner._get_notify_by_email_domain(message, digest=True)
  47. self._assert_found(partner, domain, not_found=True)
  48. def test_notify_domains_only_recipients(self):
  49. # we don't set recipients
  50. # because we call `_get_notify_by_email_domain` directly
  51. self.partner1.real_user_id.notification_type = 'email'
  52. self.partner2.real_user_id.notification_type = 'email'
  53. partners = self.partner1 + self.partner2
  54. # followers
  55. self.partner3.message_subscribe(self.partner2.ids)
  56. # partner1 is the only recipient
  57. message = self.message_model.create({
  58. 'body': 'My Body',
  59. 'res_id': self.partner3.id,
  60. 'model': 'res.partner',
  61. 'partner_ids': [(4, self.partner1.id)]
  62. })
  63. domain = partners._get_notify_by_email_domain(message)
  64. # we find both of them since partner2 is a follower
  65. self._assert_found(self.partner1, domain)
  66. self._assert_found(self.partner2, domain)
  67. # no one here in digest mode
  68. domain = partners._get_notify_by_email_domain(message, digest=True)
  69. self._assert_found(self.partner1, domain, not_found=True)
  70. self._assert_found(self.partner2, domain, not_found=True)
  71. # include only recipients
  72. domain = partners.with_context(
  73. notify_only_recipients=1)._get_notify_by_email_domain(message)
  74. self._assert_found(self.partner1, domain)
  75. self._assert_found(self.partner2, domain, not_found=True)
  76. def test_notify_domains_digest(self):
  77. # we don't set recipients
  78. # because we call `_get_notify_by_email_domain` directly
  79. message = self.message_model.create({'body': 'My Body', })
  80. partner = self.partner1
  81. partner.real_user_id.write({
  82. 'notification_type': 'email',
  83. 'digest_mode': True,
  84. })
  85. domain = partner._get_notify_by_email_domain(message)
  86. self._assert_found(partner, domain, not_found=True)
  87. domain = partner._get_notify_by_email_domain(message, digest=True)
  88. self._assert_found(partner, domain)
  89. def test_notify_domains_none(self):
  90. message = self.message_model.create({'body': 'My Body', })
  91. partner = self.partner1
  92. partner.real_user_id.write({
  93. 'notification_type': 'inbox',
  94. })
  95. domain = partner._get_notify_by_email_domain(message)
  96. self._assert_found(partner, domain, not_found=True)
  97. domain = partner._get_notify_by_email_domain(message, digest=True)
  98. self._assert_found(partner, domain, not_found=True)
  99. def test_notify_domains_match_type_digest(self):
  100. # Test message subtype matches partner settings.
  101. # The partner can have several `user.notification.conf` records.
  102. # Each records establish notification rules by type.
  103. # If you don't have any record in it, you allow all subtypes.
  104. # Record `typeX` with `enable=True` enables notification for `typeX`.
  105. # Record `typeX` with `enable=False` disables notification for `typeX`.
  106. partner = self.partner1
  107. # enable digest
  108. partner.real_user_id.write({
  109. 'notification_type': 'email',
  110. 'digest_mode': True,
  111. })
  112. message_t1 = self.message_model.create({
  113. 'body': 'My Body',
  114. 'subtype_id': self.subtype1.id,
  115. })
  116. message_t2 = self.message_model.create({
  117. 'body': 'My Body',
  118. 'subtype_id': self.subtype2.id,
  119. })
  120. # enable subtype on partner
  121. partner.real_user_id._notify_enable_subtype(self.subtype1)
  122. domain = partner._get_notify_by_email_domain(
  123. message_t1, digest=True)
  124. # notification enabled: we find the partner.
  125. self._assert_found(partner, domain)
  126. # for subtype2 we don't have any explicit rule: we find the partner
  127. domain = partner._get_notify_by_email_domain(
  128. message_t2, digest=True)
  129. self._assert_found(partner, domain)
  130. # enable subtype2: find the partner anyway
  131. partner.real_user_id._notify_enable_subtype(self.subtype2)
  132. domain = partner._get_notify_by_email_domain(
  133. message_t2, digest=True)
  134. self._assert_found(partner, domain)
  135. # disable subtype2: we don't find the partner anymore
  136. partner.real_user_id._notify_disable_subtype(self.subtype2)
  137. domain = partner._get_notify_by_email_domain(
  138. message_t2, digest=True)
  139. self._assert_found(partner, domain, not_found=True)
  140. def test_notify_domains_match_type_always(self):
  141. message_t1 = self.message_model.create({
  142. 'body': 'My Body',
  143. 'subtype_id': self.subtype1.id,
  144. })
  145. message_t2 = self.message_model.create({
  146. 'body': 'My Body',
  147. 'subtype_id': self.subtype2.id,
  148. })
  149. # enable always
  150. partner = self.partner1
  151. partner.real_user_id.notification_type = 'email'
  152. # enable subtype on partner
  153. partner.real_user_id._notify_enable_subtype(self.subtype1)
  154. domain = partner._get_notify_by_email_domain(message_t1)
  155. # notification enabled: we find the partner.
  156. self._assert_found(partner, domain)
  157. # for subtype2 we don't have any explicit rule: we find the partner
  158. domain = partner._get_notify_by_email_domain(message_t2)
  159. self._assert_found(partner, domain)
  160. # enable subtype2: find the partner anyway
  161. partner.real_user_id._notify_enable_subtype(self.subtype2)
  162. domain = partner._get_notify_by_email_domain(message_t2)
  163. self._assert_found(partner, domain)
  164. # disable subtype2: we don't find the partner anymore
  165. partner.real_user_id._notify_disable_subtype(self.subtype2)
  166. domain = partner._get_notify_by_email_domain(message_t2)
  167. self._assert_found(partner, domain, not_found=True)