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.

178 lines
6.8 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 TransactionCase
  6. mock_base_send = 'openerp.addons.mail.models.mail_mail.MailMail.send'
  7. mock_sendgrid_api_client = ('openerp.addons.mail_sendgrid.models.mail_mail'
  8. '.SendGridAPIClient')
  9. mock_sendgrid_send = ('openerp.addons.mail_sendgrid.models.mail_mail.'
  10. 'OdooMail.send_sendgrid')
  11. mock_config = ('openerp.addons.mail_sendgrid.models.mail_mail.'
  12. 'config')
  13. class FakeClient(object):
  14. """ Mock Sendgrid APIClient """
  15. status_code = 202
  16. body = 'ok'
  17. def __init__(self):
  18. self.client = self
  19. self.mail = self
  20. self.send = self
  21. def post(self, **kwargs):
  22. return self
  23. class FakeRequest(object):
  24. """ Simulate a Sendgrid JSON request """
  25. def __init__(self, data):
  26. self.jsonrequest = [data]
  27. class TestMailSendgrid(TransactionCase):
  28. def setUp(self):
  29. super(TestMailSendgrid, self).setUp()
  30. self.sendgrid_template = self.env['sendgrid.template'].create({
  31. 'name': 'Test Template',
  32. 'remote_id': 'a74795d7-f926-4bad-8e7a-ae95fabd70fc',
  33. 'html_content': u'<h1>Test Sendgrid</h1><%body%>{footer}'
  34. })
  35. self.mail_template = self.env['mail.template'].create({
  36. 'name': 'Test Template',
  37. 'model_id': self.env.ref('base.model_res_partner').id,
  38. 'subject': 'Test e-mail',
  39. 'body_html': u'Dear ${object.name}, hello!',
  40. 'sendgrid_template_ids': [
  41. (0, 0, {'lang': 'en_US', 'sendgrid_template_id':
  42. self.sendgrid_template.id})]
  43. })
  44. self.recipient = self.env.ref('base.partner_demo')
  45. self.mail_wizard = self.env['mail.compose.message'].create({
  46. 'template_id': self.mail_template.id,
  47. 'composition_mode': 'comment',
  48. 'model': 'res.partner',
  49. 'res_id': self.recipient.id
  50. })
  51. self.mail_wizard.onchange_template_id_wrapper()
  52. self.timestamp = u'1471021089'
  53. self.event = {
  54. 'timestamp': self.timestamp,
  55. 'sg_event_id': u"f_JoKtrLQaOXUc4thXgROg",
  56. 'email': self.recipient.email,
  57. 'odoo_db': self.env.cr.dbname,
  58. 'odoo_id': u'<xxx.xxx.xxx-openerp-xxx-res.partner@test_db>'
  59. }
  60. self.metadata = {
  61. 'ip': '127.0.0.1',
  62. 'user_agent': False,
  63. 'os_family': False,
  64. 'ua_family': False,
  65. }
  66. self.request = FakeRequest(self.event)
  67. def create_email(self, vals=None):
  68. mail_vals = self.mail_wizard.get_mail_values(self.recipient.ids)[
  69. self.recipient.id]
  70. mail_vals['recipient_ids'] = [(6, 0, self.recipient.ids)]
  71. if vals is not None:
  72. mail_vals.update(vals)
  73. return self.env['mail.mail'].create(mail_vals)
  74. def test_substitutions(self):
  75. """ Test substitutions in templates. """
  76. self.assertEqual(self.sendgrid_template.detected_keywords, "{footer}")
  77. self.mail_template.update_substitutions()
  78. substitutions = self.mail_template.substitution_ids
  79. self.assertEqual(len(substitutions), 1)
  80. self.assertEqual(substitutions.key, '{footer}')
  81. def test_create_email(self):
  82. """ Test that Sendgrid template is pushed in e-mail. """
  83. self.mail_template.update_substitutions()
  84. mail_values = self.mail_wizard.get_mail_values(self.recipient.ids)[
  85. self.recipient.id]
  86. # Test Sendgrid HTML preview
  87. self.assertEqual(
  88. self.mail_wizard.body_sendgrid,
  89. self.sendgrid_template.html_content.replace(
  90. '<%body%>', mail_values['body'])
  91. )
  92. mail = self.env['mail.mail'].create(mail_values)
  93. self.assertEqual(mail.sendgrid_template_id.id,
  94. self.sendgrid_template.id)
  95. self.assertEqual(len(mail.substitution_ids), 1)
  96. @mock.patch(mock_base_send)
  97. @mock.patch(mock_sendgrid_send)
  98. def test_send_email_default(self, mock_sendgrid, mock_email):
  99. """ Tests that sending an e-mail by default doesn't use Sendgrid,
  100. and that Sendgrid is used when system parameter is set.
  101. """
  102. self.env['ir.config_parameter'].set_param(
  103. 'mail_sendgrid.send_method', False)
  104. mock_sendgrid.return_value = True
  105. mock_email.return_value = True
  106. mail = self.create_email()
  107. mail.send()
  108. self.assertTrue(mock_email.called)
  109. self.assertFalse(mock_sendgrid.called)
  110. self.env['ir.config_parameter'].set_param(
  111. 'mail_sendgrid.send_method', 'sendgrid')
  112. # Force again computation of send_method
  113. self.env.invalidate_all()
  114. mail.send()
  115. self.assertEqual(mock_email.call_count, 1)
  116. self.assertEqual(mock_sendgrid.call_count, 1)
  117. @mock.patch(mock_sendgrid_api_client)
  118. @mock.patch(mock_config)
  119. def test_mail_tracking(self, m_config, mock_sendgrid):
  120. """ Test various tracking events. """
  121. self.env['ir.config_parameter'].set_param(
  122. 'mail_sendgrid.send_method', 'sendgrid')
  123. mail = self.create_email()
  124. mock_sendgrid.return_value = FakeClient()
  125. m_config.get.return_value = "ushuwejhfkj"
  126. mail.send()
  127. self.assertEqual(mock_sendgrid.called, True)
  128. self.assertEqual(mail.state, 'sent')
  129. mail_tracking = mail.tracking_email_ids
  130. self.assertEqual(len(mail_tracking), 1)
  131. self.assertFalse(mail_tracking.state)
  132. # Test mail processed
  133. self.event.update({
  134. 'event': u'processed',
  135. 'odoo_id': mail.message_id
  136. })
  137. response = self.env['mail.tracking.email'].event_process(
  138. self.request, self.event, self.metadata)
  139. self.assertEqual(response, 'OK')
  140. self.assertEqual(mail_tracking.state, 'sent')
  141. # Test mail delivered
  142. self.event['event'] = 'delivered'
  143. self.env['mail.tracking.email'].event_process(
  144. self.request, self.event, self.metadata)
  145. self.assertEqual(mail_tracking.state, 'delivered')
  146. self.assertEqual(mail_tracking.recipient, self.recipient.email)
  147. self.assertFalse(mail.opened)
  148. # Test mail opened
  149. self.event['event'] = 'open'
  150. self.env['mail.tracking.email'].event_process(
  151. self.request, self.event, self.metadata)
  152. self.assertEqual(mail_tracking.state, 'opened')
  153. self.assertTrue(mail.opened)
  154. # Test click e-mail
  155. self.event['event'] = 'click'
  156. self.env['mail.tracking.email'].event_process(
  157. self.request, self.event, self.metadata)
  158. self.assertEqual(mail_tracking.state, 'opened')
  159. self.assertEqual(mail.click_count, 1)