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.

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