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.

148 lines
5.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright - 2015-2018 Therp BV <https://acme.com>.
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import models
  5. from odoo.tests.common import TransactionCase
  6. from ..match_algorithm import email_exact, email_domain, odoo_standard
  7. MSG_BODY = [
  8. ('1 (RFC822 {1149}',
  9. 'Return-Path: <ronald@acme.com>\r\n'
  10. 'Delivered-To: demo@yourcompany.example.com\r\n'
  11. 'Received: from localhost (localhost [127.0.0.1])\r\n'
  12. '\tby vanaheim.acme.com (Postfix) with ESMTP id 14A3183163\r\n'
  13. '\tfor <demo@yourcompany.example.com>;'
  14. ' Mon, 26 Mar 2018 16:03:52 +0200 (CEST)\r\n'
  15. 'To: Test User <demo@yourcompany.example.com>\r\n'
  16. 'From: Ronald Portier <ronald@acme.com>\r\n'
  17. 'Subject: test\r\n'
  18. 'Message-ID: <485a8041-d560-a981-5afc-d31c1f136748@acme.com>\r\n'
  19. 'Date: Mon, 26 Mar 2018 16:03:51 +0200\r\n'
  20. 'User-Agent: Mock Test\r\n'
  21. 'MIME-Version: 1.0\r\n'
  22. 'Content-Type: text/plain; charset=utf-8\r\n'
  23. 'Content-Language: en-US\r\n'
  24. 'Content-Transfer-Encoding: 7bit\r\n\r\n'
  25. 'Hallo Wereld!\r\n'),
  26. ')']
  27. class MockConnection():
  28. def select(self, path):
  29. """Mock selecting a folder."""
  30. return ('OK', )
  31. def store(self, msgid, msg_item, value):
  32. """Mock store command."""
  33. return 'OK'
  34. def fetch(self, msgid, parts):
  35. """Return RFC822 formatted message."""
  36. return ('OK', MSG_BODY)
  37. def search(self, charset, criteria):
  38. """Return some msgid's."""
  39. return ('OK', ['123 456'])
  40. class TestMatchAlgorithms(TransactionCase):
  41. def _get_base_folder(self):
  42. server_model = self.env['fetchmail.server']
  43. folder_model = self.env['fetchmail.server.folder']
  44. folder = folder_model.browse([models.NewId()])
  45. folder.model_id = self.env.ref('base.model_res_partner').id
  46. folder.model_field = 'email'
  47. folder.match_algorithm = 'EmailExact'
  48. folder.mail_field = 'to,from'
  49. folder.server_id = server_model.browse([models.NewId()])
  50. return folder
  51. def do_matching(
  52. self, match_algorithm, expected_xmlid, folder, mail_message,
  53. mail_message_org=None):
  54. matcher = match_algorithm()
  55. matches = matcher.search_matches(folder, mail_message)
  56. self.assertEqual(len(matches), 1)
  57. self.assertEqual(
  58. matches[0], self.env.ref(expected_xmlid))
  59. connection = MockConnection()
  60. matcher.handle_match(
  61. connection, matches[0], folder, mail_message, mail_message_org,
  62. None)
  63. def test_email_exact(self):
  64. mail_message = {
  65. 'subject': 'Testsubject',
  66. 'to': 'demo@yourcompany.example.com',
  67. 'from': 'someone@else.com',
  68. }
  69. folder = self._get_base_folder()
  70. folder.match_algorithm = 'EmailExact'
  71. self.do_matching(
  72. email_exact.EmailExact, 'base.user_demo_res_partner',
  73. folder, mail_message)
  74. self.assertEqual(
  75. self.env.ref('base.user_demo_res_partner').message_ids.subject,
  76. mail_message['subject'])
  77. def test_email_domain(self):
  78. mail_message = {
  79. 'subject': 'Testsubject',
  80. 'to': 'test@seagate.com',
  81. 'from': 'someone@else.com',
  82. 'attachments': [('hello.txt', 'Hello World!')]}
  83. folder = self._get_base_folder()
  84. folder.match_algorithm = 'EmailDomain'
  85. folder.use_first_match = True
  86. self.do_matching(
  87. email_domain.EmailDomain, 'base.res_partner_address_31',
  88. folder, mail_message)
  89. self.assertEqual(
  90. self.env.ref('base.res_partner_address_31').message_ids.subject,
  91. mail_message['subject'])
  92. def test_odoo_standard(self):
  93. mail_message_org = (
  94. "To: demo@yourcompany.example.com\n"
  95. "From: someone@else.com\n"
  96. "Subject: testsubject\n"
  97. "Message-Id: 42\n"
  98. "Hello world"
  99. )
  100. folder = self._get_base_folder()
  101. folder.match_algorithm = 'OdooStandard'
  102. matcher = odoo_standard.OdooStandard()
  103. matches = matcher.search_matches(folder, None)
  104. self.assertEqual(len(matches), 1)
  105. matcher.handle_match(
  106. None, matches[0], folder, None, mail_message_org, None)
  107. self.assertIn(
  108. 'Hello world',
  109. self.env['mail.message']
  110. .search([('subject', '=', 'testsubject')]).body)
  111. def test_apply_matching_exact(self):
  112. folder = self._get_base_folder()
  113. folder.match_algorithm = 'EmailExact'
  114. connection = MockConnection()
  115. msgid = "<485a8041-d560-a981-5afc-d31c1f136748@acme.com>"
  116. matcher = email_exact.EmailExact()
  117. folder.apply_matching(connection, msgid, matcher)
  118. def test_retrieve_imap_folder_domain(self):
  119. folder = self._get_base_folder()
  120. folder.match_algorithm = 'EmailDomain'
  121. connection = MockConnection()
  122. folder.retrieve_imap_folder(connection)
  123. def test_field_view_get(self):
  124. """For the moment just check execution withouth errors."""
  125. server_model = self.env['fetchmail.server']
  126. view = server_model.fields_view_get()
  127. self.assertTrue(view)
  128. self.assertIn(
  129. 'match_algorithm',
  130. view['fields']['folder_ids']['views']['form']['arch'])