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.

90 lines
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Tecnativa - Jairo Llopis
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  4. from mock import patch
  5. from odoo.exceptions import ValidationError
  6. from odoo.tests.common import TransactionCase
  7. class DynamicListCase(TransactionCase):
  8. def setUp(self):
  9. super(DynamicListCase, self).setUp()
  10. self.tag = self.env["res.partner.category"].create({
  11. "name": "testing tag",
  12. })
  13. self.partners = self.env["res.partner"]
  14. for number in range(5):
  15. self.partners |= self.partners.create({
  16. "name": "partner %d" % number,
  17. "category_id": [(4, self.tag.id, False)],
  18. "email": "%d@example.com" % number,
  19. })
  20. self.list = self.env["mail.mass_mailing.list"].create({
  21. "name": "test list",
  22. "dynamic": True,
  23. "sync_domain": repr([("category_id", "in", self.tag.ids)]),
  24. })
  25. self.mail = self.env["mail.mass_mailing"].create({
  26. "name": "test mass mailing",
  27. "contact_list_ids": [(4, self.list.id, False)],
  28. })
  29. self.mail._onchange_model_and_list()
  30. def test_list_sync(self):
  31. """List is synced correctly."""
  32. Contact = self.env["mail.mass_mailing.contact"]
  33. # Partner 0 is not categorized
  34. self.partners[0].category_id = False
  35. # Partner 1 has no email
  36. self.partners[1].email = False
  37. # Set list as unsynced
  38. self.list.dynamic = False
  39. # Create contact for partner 0 in unsynced list
  40. contact0 = Contact.create({
  41. "list_id": self.list.id,
  42. "partner_id": self.partners[0].id,
  43. })
  44. self.assertEqual(self.list.contact_nbr, 1)
  45. # Set list as add-synced
  46. self.list.dynamic = True
  47. self.list.action_sync()
  48. self.assertEqual(self.list.contact_nbr, 4)
  49. self.assertTrue(contact0.exists())
  50. # Set list as full-synced
  51. self.list.sync_method = "full"
  52. self.list.action_sync()
  53. self.assertEqual(self.list.contact_nbr, 3)
  54. self.assertFalse(contact0.exists())
  55. # Cannot add or edit contacts in fully synced lists
  56. with self.assertRaises(ValidationError):
  57. Contact.create({
  58. "list_id": self.list.id,
  59. "partner_id": self.partners[0].id,
  60. })
  61. contact1 = Contact.search([
  62. ("list_id", "=", self.list.id),
  63. ], limit=1)
  64. with self.assertRaises(ValidationError):
  65. contact1.name = "other"
  66. with self.assertRaises(ValidationError):
  67. contact1.email = "other@example.com"
  68. with self.assertRaises(ValidationError):
  69. contact1.partner_id = self.partners[0]
  70. def test_sync_when_sending_mail(self):
  71. """Dynamic list is synced before mailing to it."""
  72. self.list.action_sync()
  73. self.assertEqual(self.list.contact_nbr, 5)
  74. # Create a new partner
  75. self.partners.create({
  76. "name": "extra partner",
  77. "category_id": [(4, self.tag.id, False)],
  78. "email": "extra@example.com",
  79. })
  80. # Before sending the mail, the list is updated
  81. with patch("odoo.addons.base.ir.ir_mail_server"
  82. ".IrMailServer.send_email") as send_email:
  83. self.mail.send_mail()
  84. self.assertEqual(6, send_email.call_count)
  85. self.assertEqual(6, self.list.contact_nbr)