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.

70 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 LasLabs Inc.
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. import os
  5. import threading
  6. from mock import MagicMock
  7. from email import message_from_string
  8. from odoo.tests.common import TransactionCase
  9. class TestIrMailServer(TransactionCase):
  10. def setUp(self):
  11. super(TestIrMailServer, self).setUp()
  12. self.email_from = 'derp@example.com'
  13. self.email_from_another = 'another@example.com'
  14. self.Model = self.env['ir.mail_server']
  15. self.Model.search([]).write({'smtp_from': self.email_from})
  16. message_file = os.path.join(
  17. os.path.dirname(os.path.realpath(__file__)), 'test.msg',
  18. )
  19. with open(message_file, 'r') as fh:
  20. self.message = message_from_string(fh.read())
  21. def _send_mail(self, message=None, mail_server_id=None, smtp_server=None):
  22. if message is None:
  23. message = self.message
  24. connect = MagicMock()
  25. thread = threading.currentThread()
  26. setattr(thread, 'testing', False)
  27. try:
  28. self.Model._patch_method('connect', connect)
  29. try:
  30. self.Model.send_email(message, mail_server_id, smtp_server)
  31. finally:
  32. self.Model._revert_method('connect')
  33. finally:
  34. setattr(thread, 'testing', True)
  35. send_from, send_to, message_string = connect().sendmail.call_args[0]
  36. return message_from_string(message_string)
  37. def test_send_email_injects_from_no_canonical(self):
  38. """It should inject the FROM header correctly when no canonical name.
  39. """
  40. self.message.replace_header('From', 'test@example.com')
  41. message = self._send_mail()
  42. self.assertEqual(message['From'], self.email_from)
  43. def test_send_email_injects_from_with_canonical(self):
  44. """It should inject the FROM header correctly with a canonical name.
  45. Note that there is an extra `<` in the canonical name to test for
  46. proper handling in the split.
  47. """
  48. user = 'Test < User'
  49. self.message.replace_header('From', '%s <test@example.com>' % user)
  50. message = self._send_mail()
  51. self.assertEqual(
  52. message['From'],
  53. '%s <%s>' % (user, self.email_from),
  54. )
  55. def test_send_email_injects_sender(self):
  56. """It should inject the Sender header into the email."""
  57. original_from = self.message['From']
  58. message = self._send_mail()
  59. self.assertEqual(message['Sender'], original_from)