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.

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