summaryrefslogtreecommitdiff
path: root/addons/test_mail/tests/test_mail_tools.py
blob: b36a1335c5d3c450cac0c62ef8f3dcc45c84de1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo.addons.test_mail.tests.common import TestMailCommon, TestRecipients
from odoo.tests import tagged, users


@tagged('mail_tools')
class TestMailTools(TestMailCommon, TestRecipients):

    @classmethod
    def setUpClass(cls):
        super(TestMailTools, cls).setUpClass()

        cls._test_email = 'alfredoastaire@test.example.com'
        cls.test_partner = cls.env['res.partner'].create({
            'country_id': cls.env.ref('base.be').id,
            'email': cls._test_email,
            'mobile': '0456001122',
            'name': 'Alfred Astaire',
            'phone': '0456334455',
        })

    @users('employee')
    def test_find_partner_from_emails(self):
        Partner = self.env['res.partner']
        test_partner = Partner.browse(self.test_partner.ids)
        self.assertEqual(test_partner.email, self._test_email)

        # test direct match
        found = Partner._mail_find_partner_from_emails([self._test_email])
        self.assertEqual(found, [test_partner])

        # test encapsulated email
        found = Partner._mail_find_partner_from_emails(['"Norbert Poiluchette" <%s>' % self._test_email])
        self.assertEqual(found, [test_partner])

        # test with wildcard "_"
        found = Partner._mail_find_partner_from_emails(['alfred_astaire@test.example.com'])
        self.assertEqual(found, [self.env['res.partner']])

        # sub-check: this search does not consider _ as a wildcard
        found = Partner._mail_search_on_partner(['alfred_astaire@test.example.com'])
        self.assertEqual(found, self.env['res.partner'])

        # test partners with encapsulated emails
        # ------------------------------------------------------------
        test_partner.sudo().write({'email': '"Alfred Mighty Power Astaire" <%s>' % self._test_email})

        # test direct match
        found = Partner._mail_find_partner_from_emails([self._test_email])
        self.assertEqual(found, [test_partner])

        # test encapsulated email
        found = Partner._mail_find_partner_from_emails(['"Norbert Poiluchette" <%s>' % self._test_email])
        self.assertEqual(found, [test_partner])

        # test with wildcard "_"
        found = Partner._mail_find_partner_from_emails(['alfred_astaire@test.example.com'])
        self.assertEqual(found, [self.env['res.partner']])

        # sub-check: this search does not consider _ as a wildcard
        found = Partner._mail_search_on_partner(['alfred_astaire@test.example.com'])
        self.assertEqual(found, self.env['res.partner'])

        # test partners with look-alike emails
        # ------------------------------------------------------------
        for email_lookalike in [
                'alfred.astaire@test.example.com',
                'alfredoastaire@example.com',
                'aalfredoastaire@test.example.com',
                'alfredoastaire@test.example.comm']:
            test_partner.sudo().write({'email': '"Alfred Astaire" <%s>' % email_lookalike})

            # test direct match
            found = Partner._mail_find_partner_from_emails([self._test_email])
            self.assertEqual(found, [self.env['res.partner']])
            # test encapsulated email
            found = Partner._mail_find_partner_from_emails(['"Norbert Poiluchette" <%s>' % self._test_email])
            self.assertEqual(found, [self.env['res.partner']])
            # test with wildcard "_"
            found = Partner._mail_find_partner_from_emails(['alfred_astaire@test.example.com'])
            self.assertEqual(found, [self.env['res.partner']])