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.

208 lines
9.0 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_digest_force_send(self):
  100. # when `force_send` is true, digest machinery is bypassed
  101. message = self.message_model.create({'body': 'My Body', })
  102. partner = self.partner1
  103. partner.notify_email = 'digest'
  104. # even if we have digest mode on, we find the guy
  105. domain = partner._get_notify_by_email_domain(message, force_send=True)
  106. self._assert_found(partner, domain)
  107. # when asking for digest domain we don't get digest-related leaves
  108. # as digest domain part is bypassed
  109. domain = partner._get_notify_by_email_domain(
  110. message, force_send=True, digest=True)
  111. self.assertNotIn('notify_email', [x[0] for x in domain])
  112. def test_notify_domains_none(self):
  113. message = self.message_model.create({'body': 'My Body', })
  114. partner = self.partner1
  115. partner.real_user_id.write({
  116. 'notification_type': 'inbox',
  117. })
  118. domain = partner._get_notify_by_email_domain(message)
  119. self._assert_found(partner, domain, not_found=True)
  120. domain = partner._get_notify_by_email_domain(message, digest=True)
  121. self._assert_found(partner, domain, not_found=True)
  122. def test_notify_domains_match_type_digest(self):
  123. # Test message subtype matches partner settings.
  124. # The partner can have several `user.notification.conf` records.
  125. # Each records establish notification rules by type.
  126. # If you don't have any record in it, you allow all subtypes.
  127. # Record `typeX` with `enable=True` enables notification for `typeX`.
  128. # Record `typeX` with `enable=False` disables notification for `typeX`.
  129. partner = self.partner1
  130. # enable digest
  131. partner.real_user_id.write({
  132. 'notification_type': 'email',
  133. 'digest_mode': True,
  134. })
  135. message_t1 = self.message_model.create({
  136. 'body': 'My Body',
  137. 'subtype_id': self.subtype1.id,
  138. })
  139. message_t2 = self.message_model.create({
  140. 'body': 'My Body',
  141. 'subtype_id': self.subtype2.id,
  142. })
  143. # enable subtype on partner
  144. partner.real_user_id._notify_enable_subtype(self.subtype1)
  145. domain = partner._get_notify_by_email_domain(
  146. message_t1, digest=True)
  147. # notification enabled: we find the partner.
  148. self._assert_found(partner, domain)
  149. # for subtype2 we don't have any explicit rule: we find the partner
  150. domain = partner._get_notify_by_email_domain(
  151. message_t2, digest=True)
  152. self._assert_found(partner, domain)
  153. # enable subtype2: find the partner anyway
  154. partner.real_user_id._notify_enable_subtype(self.subtype2)
  155. domain = partner._get_notify_by_email_domain(
  156. message_t2, digest=True)
  157. self._assert_found(partner, domain)
  158. # disable subtype2: we don't find the partner anymore
  159. partner.real_user_id._notify_disable_subtype(self.subtype2)
  160. domain = partner._get_notify_by_email_domain(
  161. message_t2, digest=True)
  162. self._assert_found(partner, domain, not_found=True)
  163. def test_notify_domains_match_type_always(self):
  164. message_t1 = self.message_model.create({
  165. 'body': 'My Body',
  166. 'subtype_id': self.subtype1.id,
  167. })
  168. message_t2 = self.message_model.create({
  169. 'body': 'My Body',
  170. 'subtype_id': self.subtype2.id,
  171. })
  172. # enable always
  173. partner = self.partner1
  174. partner.real_user_id.notification_type = 'email'
  175. # enable subtype on partner
  176. partner.real_user_id._notify_enable_subtype(self.subtype1)
  177. domain = partner._get_notify_by_email_domain(message_t1)
  178. # notification enabled: we find the partner.
  179. self._assert_found(partner, domain)
  180. # for subtype2 we don't have any explicit rule: we find the partner
  181. domain = partner._get_notify_by_email_domain(message_t2)
  182. self._assert_found(partner, domain)
  183. # enable subtype2: find the partner anyway
  184. partner.real_user_id._notify_enable_subtype(self.subtype2)
  185. domain = partner._get_notify_by_email_domain(message_t2)
  186. self._assert_found(partner, domain)
  187. # disable subtype2: we don't find the partner anymore
  188. partner.real_user_id._notify_disable_subtype(self.subtype2)
  189. domain = partner._get_notify_by_email_domain(message_t2)
  190. self._assert_found(partner, domain, not_found=True)