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.

173 lines
6.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2017 Emanuel Cino - <ecino@compassion.ch>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import mock
  5. from odoo.tests.common import SavepointCase
  6. mock_sendgrid_api_client = ('odoo.addons.mail_sendgrid.models.mail_mail'
  7. '.SendGridAPIClient')
  8. mock_config = ('odoo.addons.mail_sendgrid.models.mail_mail.'
  9. 'config')
  10. class FakeClient(object):
  11. """ Mock Sendgrid APIClient """
  12. status_code = 202
  13. body = 'ok'
  14. def __init__(self):
  15. self.client = self
  16. self.mail = self
  17. self.send = self
  18. def post(self, **kwargs):
  19. return self
  20. class FakeRequest(object):
  21. """ Simulate a Sendgrid JSON request """
  22. def __init__(self, data):
  23. self.jsonrequest = [data]
  24. class TestMailSendgrid(SavepointCase):
  25. @classmethod
  26. def setUpClass(cls):
  27. super(TestMailSendgrid, cls).setUpClass()
  28. cls.sendgrid_template = cls.env['sendgrid.template'].create({
  29. 'name': 'Test Template',
  30. 'remote_id': 'a74795d7-f926-4bad-8e7a-ae95fabd70fc',
  31. 'html_content': u'<h1>Test Sendgrid</h1><%body%>{footer}'
  32. })
  33. cls.mail_template = cls.env['mail.template'].create({
  34. 'name': 'Test Template',
  35. 'model_id': cls.env.ref('base.model_res_partner').id,
  36. 'subject': 'Test e-mail',
  37. 'body_html': u'Dear ${object.name}, hello!',
  38. 'sendgrid_template_ids': [
  39. (0, 0, {'lang': 'en_US', 'sendgrid_template_id':
  40. cls.sendgrid_template.id})]
  41. })
  42. cls.recipient = cls.env.ref('base.partner_demo')
  43. cls.mass_mailing = cls.env['mail.mass_mailing'].create({
  44. 'email_from': 'admin@yourcompany.example.com',
  45. 'name': 'Test Mass Mailing Sendgrid',
  46. 'mailing_model': 'res.partner',
  47. 'mailing_domain': "[('id', '=', %d)]" % cls.recipient.id,
  48. 'email_template_id': cls.mail_template.id,
  49. 'body_html': u'Dear ${object.name}, hello!',
  50. 'reply_to_mode': 'email',
  51. 'enable_unsubscribe': True,
  52. 'unsubscribe_tag': '[unsub]'
  53. }).with_context(lang='en_US', test_mode=True)
  54. cls.timestamp = u'1471021089'
  55. cls.event = {
  56. 'timestamp': cls.timestamp,
  57. 'sg_event_id': u"f_JoKtrLQaOXUc4thXgROg",
  58. 'email': cls.recipient.email,
  59. 'odoo_db': cls.env.cr.dbname,
  60. 'odoo_id': u'<xxx.xxx.xxx-openerp-xxx-res.partner@test_db>'
  61. }
  62. cls.metadata = {
  63. 'ip': '127.0.0.1',
  64. 'user_agent': False,
  65. 'os_family': False,
  66. 'ua_family': False,
  67. }
  68. cls.request = FakeRequest(cls.event)
  69. def test_sendgrid_preview(self):
  70. """
  71. Test the preview field is getting the Sendgrid template
  72. """
  73. self.mass_mailing.html_copy = self.mass_mailing.body_html
  74. preview = self.mass_mailing.body_sendgrid
  75. self.assertIn(u'<h1>Test Sendgrid</h1>', preview)
  76. self.assertIn('hello!', preview)
  77. def test_change_language(self):
  78. """
  79. Test changing the language is changing the domain
  80. """
  81. domain = self.mass_mailing.mailing_domain
  82. self.mass_mailing.lang = self.env['res.lang'].search([], limit=1)
  83. self.mass_mailing.onchange_lang()
  84. self.assertTrue(len(self.mass_mailing.mailing_domain) > len(domain))
  85. @mock.patch(mock_sendgrid_api_client)
  86. @mock.patch(mock_config)
  87. def test_send_campaign(self, m_config, mock_sendgrid):
  88. """
  89. Test sending mass campaign with Sendgrid template
  90. and statistics update
  91. """
  92. self.env['ir.config_parameter'].set_param(
  93. 'mail_sendgrid.send_method', 'sendgrid')
  94. mock_sendgrid.return_value = FakeClient()
  95. m_config.get.return_value = 'we4iorujeriu'
  96. # Test campaign
  97. self.mass_mailing.action_test_mailing()
  98. self.env['mail.mass_mailing.test'].create({
  99. 'mass_mailing_id': self.mass_mailing.id,
  100. 'email_to': 'test@sendgrid.com'
  101. }).with_context(lang='en_US', test_mode=True).send_mail_test()
  102. self.assertTrue(mock_sendgrid.called)
  103. mock_sendgrid.reset_mock()
  104. # Send campaign
  105. emails = self.mass_mailing.send_mail()
  106. # Dont delete emails sent
  107. emails.write({'auto_delete': False})
  108. self.assertEqual(len(emails), 1)
  109. self.assertEqual(emails.state, 'outgoing')
  110. self.assertEqual(emails.sendgrid_template_id.id,
  111. self.sendgrid_template.id)
  112. emails.send()
  113. self.assertTrue(mock_sendgrid.called)
  114. self.assertEqual(emails.state, 'sent')
  115. mail_tracking = emails.tracking_email_ids
  116. self.assertEqual(len(mail_tracking), 1)
  117. self.assertFalse(mail_tracking.state)
  118. stats = self.mass_mailing.statistics_ids
  119. self.assertEqual(len(stats), 1)
  120. self.assertTrue(stats.sent)
  121. # Test delivered
  122. self.event.update({
  123. 'event': 'delivered',
  124. 'odoo_id': emails.message_id
  125. })
  126. self.env['mail.tracking.email'].event_process(
  127. self.request, self.event, self.metadata)
  128. self.assertTrue(stats.sent)
  129. # Test click e-mail
  130. self.event.update({
  131. 'event': 'click',
  132. })
  133. self.env['mail.tracking.email'].event_process(
  134. self.request, self.event, self.metadata)
  135. self.assertEqual(emails.click_count, 1)
  136. events = stats.tracking_event_ids
  137. self.assertEqual(len(events), 2)
  138. self.assertIn('delivered', events.mapped('event_type'))
  139. self.assertIn('click', events.mapped('event_type'))
  140. self.assertEqual(stats.state, 'sent')
  141. # Test reject
  142. self.event.update({
  143. 'event': 'dropped',
  144. })
  145. self.env['mail.tracking.email'].event_process(
  146. self.request, self.event, self.metadata)
  147. self.assertEqual(stats.state, 'exception')
  148. @classmethod
  149. def tearDownClass(cls):
  150. cls.env['ir.config_parameter'].set_param(
  151. 'mail_sendgrid.send_method', 'traditional')
  152. super(TestMailSendgrid, cls).tearDownClass()