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.

194 lines
8.3 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. # a partner w/ no user that should not be excluded
  32. # in non-digest notifications
  33. cls.partner_nouser = cls.partner_model.with_context(
  34. tracking_disable=True).create({
  35. 'name': 'No User Partner',
  36. 'email': 'nouser@test.com',
  37. })
  38. cls.subtype1 = cls.subtype_model.create({'name': 'Type 1'})
  39. cls.subtype2 = cls.subtype_model.create({'name': 'Type 2'})
  40. def _assert_found(self, partner, domain, not_found=False):
  41. if not_found:
  42. self.assertNotIn(partner, self.partner_model.search(domain))
  43. else:
  44. self.assertIn(partner, self.partner_model.search(domain))
  45. def test_notify_domains_always(self):
  46. # we don't set recipients
  47. # because we call `_get_notify_by_email_domain` directly
  48. message = self.message_model.create({'body': 'My Body', })
  49. partner = self.partner1
  50. partner.real_user_id.notification_type = 'email'
  51. domain = partner._get_notify_by_email_domain(message)
  52. self._assert_found(partner, domain)
  53. domain = partner._get_notify_by_email_domain(message, digest=True)
  54. self._assert_found(partner, domain, not_found=True)
  55. def test_notify_domains_only_recipients(self):
  56. # we don't set recipients
  57. # because we call `_get_notify_by_email_domain` directly
  58. self.partner1.real_user_id.notification_type = 'email'
  59. self.partner2.real_user_id.notification_type = 'email'
  60. partners = self.partner1 + self.partner2 + self.partner_nouser
  61. # followers
  62. self.partner3.message_subscribe(self.partner2.ids)
  63. # partner1 is the only recipient
  64. message = self.message_model.create({
  65. 'body': 'My Body',
  66. 'res_id': self.partner3.id,
  67. 'model': 'res.partner',
  68. 'partner_ids': [(4, self.partner1.id), (4, self.partner_nouser.id)]
  69. })
  70. domain = partners._get_notify_by_email_domain(message)
  71. # we find both of them since partner2 is a follower
  72. self._assert_found(self.partner1, domain)
  73. self._assert_found(self.partner2, domain)
  74. # and we find also the partner w/ no user
  75. self._assert_found(self.partner_nouser, domain)
  76. # no one here in digest mode
  77. domain = partners._get_notify_by_email_domain(message, digest=True)
  78. self._assert_found(self.partner1, domain, not_found=True)
  79. self._assert_found(self.partner2, domain, not_found=True)
  80. self._assert_found(self.partner_nouser, domain, not_found=True)
  81. # include only recipients
  82. domain = partners.with_context(
  83. notify_only_recipients=1)._get_notify_by_email_domain(message)
  84. self._assert_found(self.partner1, domain)
  85. self._assert_found(self.partner2, domain, not_found=True)
  86. def test_notify_domains_digest(self):
  87. # we don't set recipients
  88. # because we call `_get_notify_by_email_domain` directly
  89. message = self.message_model.create({'body': 'My Body', })
  90. partner = self.partner1
  91. partner.real_user_id.write({
  92. 'notification_type': 'email',
  93. 'digest_mode': True,
  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)
  99. def test_notify_domains_none(self):
  100. message = self.message_model.create({'body': 'My Body', })
  101. partner = self.partner1
  102. partner.real_user_id.write({
  103. 'notification_type': 'inbox',
  104. })
  105. domain = partner._get_notify_by_email_domain(message)
  106. self._assert_found(partner, domain, not_found=True)
  107. domain = partner._get_notify_by_email_domain(message, digest=True)
  108. self._assert_found(partner, domain, not_found=True)
  109. def test_notify_domains_match_type_digest(self):
  110. # Test message subtype matches partner settings.
  111. # The partner can have several `user.notification.conf` records.
  112. # Each records establish notification rules by type.
  113. # If you don't have any record in it, you allow all subtypes.
  114. # Record `typeX` with `enable=True` enables notification for `typeX`.
  115. # Record `typeX` with `enable=False` disables notification for `typeX`.
  116. partner = self.partner1
  117. # enable digest
  118. partner.real_user_id.write({
  119. 'notification_type': 'email',
  120. 'digest_mode': True,
  121. })
  122. message_t1 = self.message_model.create({
  123. 'body': 'My Body',
  124. 'subtype_id': self.subtype1.id,
  125. })
  126. message_t2 = self.message_model.create({
  127. 'body': 'My Body',
  128. 'subtype_id': self.subtype2.id,
  129. })
  130. # enable subtype on partner
  131. partner.real_user_id._notify_enable_subtype(self.subtype1)
  132. domain = partner._get_notify_by_email_domain(
  133. message_t1, digest=True)
  134. # notification enabled: we find the partner.
  135. self._assert_found(partner, domain)
  136. # for subtype2 we don't have any explicit rule: we find the partner
  137. domain = partner._get_notify_by_email_domain(
  138. message_t2, digest=True)
  139. self._assert_found(partner, domain)
  140. # enable subtype2: find the partner anyway
  141. partner.real_user_id._notify_enable_subtype(self.subtype2)
  142. domain = partner._get_notify_by_email_domain(
  143. message_t2, digest=True)
  144. self._assert_found(partner, domain)
  145. # disable subtype2: we don't find the partner anymore
  146. partner.real_user_id._notify_disable_subtype(self.subtype2)
  147. domain = partner._get_notify_by_email_domain(
  148. message_t2, digest=True)
  149. self._assert_found(partner, domain, not_found=True)
  150. def test_notify_domains_match_type_always(self):
  151. message_t1 = self.message_model.create({
  152. 'body': 'My Body',
  153. 'subtype_id': self.subtype1.id,
  154. })
  155. message_t2 = self.message_model.create({
  156. 'body': 'My Body',
  157. 'subtype_id': self.subtype2.id,
  158. })
  159. # enable always
  160. partner = self.partner1
  161. partner.real_user_id.notification_type = 'email'
  162. # enable subtype on partner
  163. partner.real_user_id._notify_enable_subtype(self.subtype1)
  164. domain = partner._get_notify_by_email_domain(message_t1)
  165. # notification enabled: we find the partner.
  166. self._assert_found(partner, domain)
  167. # for subtype2 we don't have any explicit rule: we find the partner
  168. domain = partner._get_notify_by_email_domain(message_t2)
  169. self._assert_found(partner, domain)
  170. # enable subtype2: find the partner anyway
  171. partner.real_user_id._notify_enable_subtype(self.subtype2)
  172. domain = partner._get_notify_by_email_domain(message_t2)
  173. self._assert_found(partner, domain)
  174. # disable subtype2: we don't find the partner anymore
  175. partner.real_user_id._notify_disable_subtype(self.subtype2)
  176. domain = partner._get_notify_by_email_domain(message_t2)
  177. self._assert_found(partner, domain, not_found=True)