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.

135 lines
5.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import mock
  5. import base64
  6. from openerp.tests.common import TransactionCase
  7. from openerp.addons.mail_tracking.controllers.main import \
  8. MailTrackingController, BLANK
  9. mock_request = 'openerp.http.request'
  10. mock_send_email = ('openerp.addons.base.ir.ir_mail_server.'
  11. 'ir_mail_server.send_email')
  12. class FakeUserAgent(object):
  13. browser = 'Test browser'
  14. platform = 'Test platform'
  15. def __str__(self):
  16. """Return name"""
  17. return 'Test suite'
  18. # One test case per method
  19. class TestMailTracking(TransactionCase):
  20. # Use case : Prepare some data for current test case
  21. def setUp(self):
  22. super(TestMailTracking, self).setUp()
  23. self.sender = self.env['res.partner'].create({
  24. 'name': 'Test sender',
  25. 'email': 'sender@example.com',
  26. 'notify_email': 'always',
  27. })
  28. self.recipient = self.env['res.partner'].create({
  29. 'name': 'Test recipient',
  30. 'email': 'recipient@example.com',
  31. 'notify_email': 'always',
  32. })
  33. self.request = {
  34. 'httprequest': type('obj', (object,), {
  35. 'remote_addr': '123.123.123.123',
  36. 'user_agent': FakeUserAgent(),
  37. }),
  38. }
  39. def test_message_post(self):
  40. # This message will generate a notification for recipient
  41. message = self.env['mail.message'].create({
  42. 'subject': 'Message test',
  43. 'author_id': self.sender.id,
  44. 'email_from': self.sender.email,
  45. 'type': 'comment',
  46. 'model': 'res.partner',
  47. 'res_id': self.recipient.id,
  48. 'partner_ids': [(4, self.recipient.id)],
  49. 'body': '<p>This is a test message</p>',
  50. })
  51. # Search tracking created
  52. tracking_email = self.env['mail.tracking.email'].search([
  53. ('mail_message_id', '=', message.id),
  54. ('partner_id', '=', self.recipient.id),
  55. ])
  56. # The tracking email must be sent
  57. self.assertTrue(tracking_email)
  58. self.assertEqual(tracking_email.state, 'sent')
  59. # message_dict read by web interface
  60. message_dict = self.env['mail.message'].message_read(message.id)
  61. # First item is message content
  62. self.assertTrue(len(message_dict) > 0)
  63. message_dict = message_dict[0]
  64. self.assertTrue(len(message_dict['partner_ids']) > 0)
  65. # First partner is recipient
  66. partner_id = message_dict['partner_ids'][0][0]
  67. self.assertEqual(partner_id, self.recipient.id)
  68. status = message_dict['partner_trackings'][str(partner_id)]
  69. # Tracking status must be sent and
  70. # mail tracking must be the one search before
  71. self.assertEqual(status[0], 'sent')
  72. self.assertEqual(status[1], tracking_email.id)
  73. # And now open the email
  74. metadata = {
  75. 'ip': '127.0.0.1',
  76. 'user_agent': 'Odoo Test/1.0',
  77. 'os_family': 'linux',
  78. 'ua_family': 'odoo',
  79. }
  80. tracking_email.event_create('open', metadata)
  81. self.assertEqual(tracking_email.state, 'opened')
  82. def mail_send(self):
  83. mail = self.env['mail.mail'].create({
  84. 'subject': 'Test subject',
  85. 'email_from': 'from@domain.com',
  86. 'email_to': 'to@domain.com',
  87. 'body_html': '<p>This is a test message</p>',
  88. })
  89. mail.send()
  90. # Search tracking created
  91. tracking_email = self.env['mail.tracking.email'].search([
  92. ('mail_id', '=', mail.id),
  93. ])
  94. return mail, tracking_email
  95. def test_mail_send(self):
  96. controller = MailTrackingController()
  97. db = self.env.cr.dbname
  98. image = base64.decodestring(BLANK)
  99. mail, tracking = self.mail_send()
  100. self.assertEqual(mail.email_to, tracking.recipient)
  101. self.assertEqual(mail.email_from, tracking.sender)
  102. with mock.patch(mock_request) as mock_func:
  103. mock_func.return_value = type('obj', (object,), self.request)
  104. res = controller.mail_tracking_open(db, tracking.id)
  105. self.assertEqual(image, res.response[0])
  106. def test_smtp_error(self):
  107. with mock.patch(mock_send_email) as mock_func:
  108. mock_func.side_effect = Warning('Test error')
  109. mail, tracking = self.mail_send()
  110. self.assertEqual('error', tracking.state)
  111. self.assertEqual('Warning', tracking.error_type)
  112. self.assertEqual('Test error', tracking.error_description)
  113. def test_db(self):
  114. db = self.env.cr.dbname
  115. controller = MailTrackingController()
  116. with mock.patch(mock_request) as mock_func:
  117. mock_func.return_value = type('obj', (object,), self.request)
  118. not_found = controller.mail_tracking_all('not_found_db')
  119. self.assertEqual('NOT FOUND', not_found.response[0])
  120. none = controller.mail_tracking_all(db)
  121. self.assertEqual('NONE', none.response[0])
  122. none = controller.mail_tracking_event(db, 'open')
  123. self.assertEqual('NONE', none.response[0])