test_mailing_server.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.addons.test_mass_mailing.tests.common import TestMassMailCommon
  4. from odoo.exceptions import UserError
  5. from odoo.tests import tagged
  6. from odoo.tests.common import users
  7. from odoo.tools import mute_logger
  8. @tagged('mass_mailing')
  9. class TestMassMailingServer(TestMassMailCommon):
  10. @classmethod
  11. def setUpClass(cls):
  12. super(TestMassMailingServer, cls).setUpClass()
  13. cls._init_mail_servers()
  14. cls.recipients = cls._create_mailing_test_records(model='mailing.test.optout', count=8)
  15. def test_mass_mailing_server_archived_usage_protection(self):
  16. """ Test the protection against using archived server:
  17. - servers used cannot be archived
  18. - mailing clone of a mailing with an archived server gets the default one instead
  19. """
  20. servers = self.env['ir.mail_server'].create([{
  21. 'name': 'Server 1',
  22. 'smtp_host': 'archive-test1.smtp.local',
  23. }, {
  24. 'name': 'Server 2',
  25. 'smtp_host': 'archive-test2.smtp.local',
  26. }])
  27. self.env['ir.config_parameter'].set_param('mass_mailing.mail_server_id', servers[0].id)
  28. mailing = self.env['mailing.mailing'].create({
  29. 'subject': 'Mailing',
  30. 'body_html': 'Body for <t t-out="object.name" />',
  31. 'email_from': 'specific_user@test.com',
  32. 'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
  33. })
  34. mailing_clone = mailing.copy()
  35. self.assertEqual(mailing_clone.mail_server_id.id, servers[0].id,
  36. 'The clone of a mailing inherits from the server of the copied mailing')
  37. with self.assertRaises(UserError, msg='Servers still used as default and for 2 mailings'):
  38. servers.action_archive()
  39. self.assertTrue(all(server.active for server in servers), 'All servers must be active')
  40. self.env['ir.config_parameter'].set_param('mass_mailing.mail_server_id', False)
  41. with self.assertRaises(UserError, msg='Servers still used for 2 mailings'):
  42. servers.action_archive()
  43. self.assertTrue(all(server.active for server in servers), 'All servers must be active')
  44. with self.mock_smtplib_connection():
  45. mailing.action_send_mail()
  46. with self.assertRaises(UserError, msg='Servers still used for 1 mailings'):
  47. servers.action_archive()
  48. self.assertTrue(all(server.active for server in servers), 'All servers must be active')
  49. with self.mock_smtplib_connection():
  50. mailing_clone.action_send_mail()
  51. servers.action_archive() # Servers no more used -> no error
  52. self.assertFalse(servers.filtered('active'), 'All servers must be archived')
  53. self.assertFalse(mailing.copy().mail_server_id,
  54. 'The clone of a mailing with an archived server gets the default one (none here)')
  55. servers[1].action_unarchive()
  56. self.env['ir.config_parameter'].set_param('mass_mailing.mail_server_id', servers[1].id)
  57. mailing_clone = mailing.copy()
  58. self.assertEqual(mailing_clone.mail_server_id.id, servers[1].id,
  59. 'The clone of a mailing with an archived server gets the default one')
  60. mailing_clone.action_archive()
  61. with self.assertRaises(UserError, msg='Servers still used as default'):
  62. servers.action_archive()
  63. self.assertTrue(servers[1].active)
  64. self.env['ir.config_parameter'].set_param('mass_mailing.mail_server_id', False)
  65. servers.action_archive() # Servers no more used -> no error
  66. self.assertFalse(servers.filtered('active'), 'All servers must be archived')
  67. @users('user_marketing')
  68. @mute_logger('odoo.addons.mail.models.mail_mail', 'odoo.models.unlink', 'odoo.addons.mass_mailing.models.mailing')
  69. def test_mass_mailing_server_batch(self):
  70. """Test that the right mail server is chosen to send the mailing.
  71. Test also the envelop and the SMTP headers.
  72. """
  73. # Test sending mailing in batch
  74. mailings = self.env['mailing.mailing'].create([{
  75. 'subject': 'Mailing',
  76. 'body_html': 'Body for <t t-out="object.name" />',
  77. 'email_from': 'specific_user@test.com',
  78. 'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
  79. }, {
  80. 'subject': 'Mailing',
  81. 'body_html': 'Body for <t t-out="object.name" />',
  82. 'email_from': 'unknown_name@test.com',
  83. 'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
  84. }])
  85. with self.mock_smtplib_connection():
  86. mailings.action_send_mail()
  87. self.assertEqual(self.find_mail_server_mocked.call_count, 2, 'Must be called only once per mail from')
  88. self.assert_email_sent_smtp(
  89. smtp_from='specific_user@test.com',
  90. message_from='specific_user@test.com',
  91. from_filter=self.server_user.from_filter,
  92. emails_count=8,
  93. )
  94. self.assert_email_sent_smtp(
  95. # Must use the bounce address here because the mail server
  96. # is configured for the entire domain "test.com"
  97. smtp_from=lambda x: 'bounce' in x,
  98. message_from='unknown_name@test.com',
  99. from_filter=self.server_domain.from_filter,
  100. emails_count=8,
  101. )
  102. @users('user_marketing')
  103. @mute_logger('odoo.addons.mail.models.mail_mail', 'odoo.models.unlink', 'odoo.addons.mass_mailing.models.mailing')
  104. def test_mass_mailing_server_default(self):
  105. # We do not have a mail server for this address email, so fall back to the
  106. # "notifications@domain" email.
  107. mailings = self.env['mailing.mailing'].create([{
  108. 'subject': 'Mailing',
  109. 'body_html': 'Body for <t t-out="object.name" />',
  110. 'email_from': '"Testing" <unknow_email@unknow_domain.com>',
  111. 'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
  112. }])
  113. with self.mock_smtplib_connection():
  114. mailings.action_send_mail()
  115. self.assertEqual(self.find_mail_server_mocked.call_count, 1)
  116. self.assert_email_sent_smtp(
  117. smtp_from='notifications@test.com',
  118. message_from='"Testing" <notifications@test.com>',
  119. from_filter=self.server_notification.from_filter,
  120. emails_count=8,
  121. )
  122. self.assertEqual(self.find_mail_server_mocked.call_count, 1, 'Must be called only once')
  123. @users('user_marketing')
  124. @mute_logger('odoo.addons.mail.models.mail_mail', 'odoo.models.unlink', 'odoo.addons.mass_mailing.models.mailing')
  125. def test_mass_mailing_server_forced(self):
  126. # We force a mail server on one mailing
  127. mailings = self.env['mailing.mailing'].create([{
  128. 'subject': 'Mailing',
  129. 'body_html': 'Body for <t t-out="object.name" />',
  130. 'email_from': self.server_user.from_filter,
  131. 'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
  132. }, {
  133. 'subject': 'Mailing',
  134. 'body_html': 'Body for <t t-out="object.name" />',
  135. 'email_from': 'unknow_email@unknow_domain.com',
  136. 'mailing_model_id': self.env['ir.model']._get('mailing.test.optout').id,
  137. 'mail_server_id': self.server_notification.id,
  138. }])
  139. with self.mock_smtplib_connection():
  140. mailings.action_send_mail()
  141. self.assertEqual(self.find_mail_server_mocked.call_count, 1, 'Must not be called when mail server is forced')
  142. self.assert_email_sent_smtp(
  143. smtp_from='specific_user@test.com',
  144. message_from='specific_user@test.com',
  145. from_filter=self.server_user.from_filter,
  146. emails_count=8,
  147. )
  148. self.assert_email_sent_smtp(
  149. smtp_from='unknow_email@unknow_domain.com',
  150. message_from='unknow_email@unknow_domain.com',
  151. from_filter=self.server_notification.from_filter,
  152. emails_count=8,
  153. )