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.

87 lines
3.1 KiB

  1. # Copyright 2016 Tecnativa - Pedro M. Baeza
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo.tests import common
  4. from odoo import exceptions
  5. from ..hooks import pre_init_hook
  6. class TestMassMailingUnique(common.SavepointCase):
  7. @classmethod
  8. def setUpClass(cls):
  9. super(TestMassMailingUnique, cls).setUpClass()
  10. cls.list = cls.env['mail.mass_mailing.list'].create({
  11. 'name': 'Test list',
  12. })
  13. cls.contact1 = cls.env['mail.mass_mailing.contact'].create({
  14. 'name': 'Contact 1',
  15. 'email': 'email1@test.com',
  16. 'list_ids': [(6, 0, [cls.list.id])]
  17. })
  18. def test_init_hook_list(self):
  19. # Disable temporarily the constraint
  20. self.env.cr.execute("""
  21. ALTER TABLE mail_mass_mailing_list
  22. DROP CONSTRAINT mail_mass_mailing_list_unique_name
  23. """)
  24. self.env['mail.mass_mailing.list'].create({
  25. 'name': 'Test list',
  26. })
  27. with self.assertRaises(exceptions.ValidationError):
  28. pre_init_hook(self.env.cr)
  29. def test_add_contact_with_list(self):
  30. with self.assertRaises(exceptions.ValidationError):
  31. self.env['mail.mass_mailing.contact'].create({
  32. 'name': 'Contact 2',
  33. 'email': 'email1@test.com',
  34. 'list_ids': [(6, 0, [self.list.id])]
  35. })
  36. def test_add_contact_with_subscription(self):
  37. with self.assertRaises(exceptions.ValidationError):
  38. self.env['mail.mass_mailing.contact'].create({
  39. 'name': 'Contact 2',
  40. 'email': 'email1@test.com',
  41. 'subscription_list_ids': [
  42. (0, 0, {'list_id': self.list.id})
  43. ]
  44. })
  45. def test_add_list_with_contacts(self):
  46. contact2 = self.env['mail.mass_mailing.contact'].create({
  47. 'name': 'Contact 2',
  48. 'email': 'email1@test.com',
  49. })
  50. with self.assertRaises(exceptions.ValidationError):
  51. self.env['mail.mass_mailing.list'].create({
  52. 'name': 'Test list 2',
  53. 'contact_ids': [(6, 0, (self.contact1 | contact2).ids)]
  54. })
  55. def test_add_list_with_subscriptions(self):
  56. contact2 = self.env['mail.mass_mailing.contact'].create({
  57. 'name': 'Contact 2',
  58. 'email': 'email1@test.com',
  59. })
  60. with self.assertRaises(exceptions.ValidationError):
  61. self.env['mail.mass_mailing.list'].create({
  62. 'name': 'Test list 2',
  63. 'subscription_contact_ids': [
  64. (0, 0, {'contact_id': self.contact1.id}),
  65. (0, 0, {'contact_id': contact2.id})
  66. ]
  67. })
  68. def test_add_list_contact_rel(self):
  69. contact2 = self.env['mail.mass_mailing.contact'].create({
  70. 'name': 'Contact 2',
  71. 'email': 'email1@test.com',
  72. })
  73. with self.assertRaises(exceptions.ValidationError):
  74. self.env['mail.mass_mailing.list_contact_rel'].create({
  75. 'list_id': self.list.id,
  76. 'contact_id': contact2.id
  77. })