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.

79 lines
2.8 KiB

  1. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  2. import socket
  3. from odoo.addons.test_mail.data.test_mail_data import MAIL_TEMPLATE
  4. from odoo.addons.test_mail.tests.test_mail_gateway import TestMailgateway
  5. from odoo.tools import mute_logger
  6. from email.utils import formataddr
  7. class TestFetchmailNotifyErrorToSender(TestMailgateway):
  8. def setUp(self):
  9. super(TestFetchmailNotifyErrorToSender, self).setUp()
  10. self.fetchmail_server = self.env['fetchmail.server'].create({
  11. 'name': 'Test Fetchmail Server',
  12. 'type': 'imap',
  13. 'error_notice_template_id': self.env.ref('%s.%s' % (
  14. 'fetchmail_notify_error_to_sender',
  15. 'email_template_error_notice',
  16. )).id
  17. })
  18. def format_and_process_with_context(
  19. self, template, to_email='groups@example.com, other@gmail.com',
  20. subject='Frogs', extra='',
  21. email_from='Sylvie Lelitre <test.sylvie.lelitre@agrolait.com>',
  22. cc_email='',
  23. msg_id='<1198923581.41972151344608186760.JavaMail@agrolait.com>',
  24. model=None, target_model='mail.test.simple', target_field='name',
  25. ctx=None,
  26. ):
  27. self.assertFalse(self.env[target_model].search([
  28. (target_field, '=', subject),
  29. ]))
  30. mail = template.format(
  31. to=to_email,
  32. subject=subject,
  33. cc=cc_email,
  34. extra=extra,
  35. email_from=email_from,
  36. msg_id=msg_id,
  37. )
  38. self.env['mail.thread'].with_context(ctx or {}).message_process(
  39. model,
  40. mail,
  41. )
  42. return self.env[target_model].search([(target_field, '=', subject)])
  43. @mute_logger('odoo.addons.mail.models.mail_thread', 'odoo.models')
  44. def test_message_process(self):
  45. email_from = formataddr((self.partner_1.name, self.partner_1.email))
  46. count_return_mails_before = self.env['mail.mail'].search_count([
  47. ('email_to', '=', email_from),
  48. ])
  49. with self.assertRaises(ValueError):
  50. self.format_and_process_with_context(
  51. MAIL_TEMPLATE,
  52. email_from=email_from,
  53. to_email='noone@example.com',
  54. subject='spam',
  55. extra='In-Reply-To: <12321321-openerp-%d-mail.test.simple@%s'
  56. '>' % (self.test_record.id,
  57. socket.gethostname(),
  58. ),
  59. ctx={
  60. 'fetchmail_server_id': self.fetchmail_server.id,
  61. }
  62. )
  63. count_return_mails_after = self.env['mail.mail'].search_count([
  64. ('email_to', '=', email_from),
  65. ])
  66. self.assertEqual(
  67. count_return_mails_after,
  68. count_return_mails_before + 1,
  69. )