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.

67 lines
2.3 KiB

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