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.

103 lines
4.4 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2015 Therp BV (<http://therp.nl>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import models
  22. from openerp.tests.common import TransactionCase
  23. from openerp.addons.fetchmail_attach_from_folder.match_algorithm import (
  24. email_exact, email_domain, openerp_standard)
  25. class TestMatchAlgorithms(TransactionCase):
  26. def do_matching(self, match_algorithm, expected_xmlid, conf, mail_message,
  27. mail_message_org=None):
  28. matcher = match_algorithm()
  29. matches = matcher.search_matches(
  30. self.env.cr, self.env.uid, conf, mail_message, mail_message_org)
  31. self.assertEqual(len(matches), 1)
  32. self.assertEqual(
  33. matches[0], self.env.ref(expected_xmlid).id)
  34. matcher.handle_match(
  35. self.env.cr, self.env.uid, None, matches[0], conf, mail_message,
  36. mail_message_org, None)
  37. def test_email_exact(self):
  38. mail_message = {
  39. 'subject': 'Testsubject',
  40. 'to': 'demo@yourcompany.example.com',
  41. 'from': 'someone@else.com',
  42. }
  43. conf = self.env['fetchmail.server.folder'].browse([models.NewId()])
  44. conf.model_id = self.env.ref('base.model_res_partner').id
  45. conf.model_field = 'email'
  46. conf.match_algorithm = 'email_exact'
  47. conf.mail_field = 'to,from'
  48. conf.server_id = self.env['fetchmail.server'].browse([models.NewId()])
  49. self.do_matching(
  50. email_exact.email_exact, 'base.user_demo_res_partner',
  51. conf, mail_message)
  52. self.assertEqual(
  53. self.env.ref('base.user_demo_res_partner').message_ids.subject,
  54. mail_message['subject'])
  55. def test_email_domain(self):
  56. mail_message = {
  57. 'subject': 'Testsubject',
  58. 'to': 'test@seagate.com',
  59. 'from': 'someone@else.com',
  60. }
  61. conf = self.env['fetchmail.server.folder'].browse([models.NewId()])
  62. conf.model_id = self.env.ref('base.model_res_partner').id
  63. conf.model_field = 'email'
  64. conf.match_algorithm = 'email_domain'
  65. conf.mail_field = 'to,from'
  66. conf.use_first_match = True
  67. conf.server_id = self.env['fetchmail.server'].browse([models.NewId()])
  68. self.do_matching(
  69. email_domain.email_domain, 'base.res_partner_address_31',
  70. conf, mail_message)
  71. self.assertEqual(
  72. self.env.ref('base.res_partner_address_31').message_ids.subject,
  73. mail_message['subject'])
  74. def test_openerp_standard(self):
  75. mail_message_org = (
  76. "To: demo@yourcompany.example.com\n"
  77. "From: someone@else.com\n"
  78. "Subject: testsubject\n"
  79. "Message-Id: 42\n"
  80. "Hello world"
  81. )
  82. conf = self.env['fetchmail.server.folder'].browse([models.NewId()])
  83. conf.model_id = self.env.ref('base.model_res_partner').id
  84. conf.model_field = 'email'
  85. conf.match_algorithm = 'openerp_standard'
  86. conf.mail_field = 'to,from'
  87. conf.server_id = self.env['fetchmail.server'].browse([models.NewId()])
  88. matcher = openerp_standard.openerp_standard()
  89. matches = matcher.search_matches(
  90. self.env.cr, self.env.uid, conf, None, mail_message_org)
  91. self.assertEqual(len(matches), 1)
  92. matcher.handle_match(
  93. self.env.cr, self.env.uid, None, matches[0], conf, None,
  94. mail_message_org, None, None)
  95. self.assertIn(
  96. 'Hello world',
  97. self.env['mail.message']
  98. .search([('subject', '=', 'testsubject')]).body)