1# -*- coding: utf-8 -*-
2# Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4from odoo.tests.common import TransactionCase, Form, tagged
5
6
7class TestUsers(TransactionCase):
8
9    def test_name_search(self):
10        """ Check name_search on user. """
11        User = self.env['res.users']
12
13        test_user = User.create({'name': 'Flad the Impaler', 'login': 'vlad'})
14        like_user = User.create({'name': 'Wlad the Impaler', 'login': 'vladi'})
15        other_user = User.create({'name': 'Nothing similar', 'login': 'nothing similar'})
16        all_users = test_user | like_user | other_user
17
18        res = User.name_search('vlad', operator='ilike')
19        self.assertEqual(User.browse(i[0] for i in res) & all_users, test_user)
20
21        res = User.name_search('vlad', operator='not ilike')
22        self.assertEqual(User.browse(i[0] for i in res) & all_users, all_users)
23
24        res = User.name_search('', operator='ilike')
25        self.assertEqual(User.browse(i[0] for i in res) & all_users, all_users)
26
27        res = User.name_search('', operator='not ilike')
28        self.assertEqual(User.browse(i[0] for i in res) & all_users, User)
29
30        res = User.name_search('lad', operator='ilike')
31        self.assertEqual(User.browse(i[0] for i in res) & all_users, test_user | like_user)
32
33        res = User.name_search('lad', operator='not ilike')
34        self.assertEqual(User.browse(i[0] for i in res) & all_users, other_user)
35
36    def test_user_partner(self):
37        """ Check that the user partner is well created """
38
39        User = self.env['res.users']
40        Partner = self.env['res.partner']
41        Company = self.env['res.company']
42
43        company_1 = Company.create({'name': 'company_1'})
44        company_2 = Company.create({'name': 'company_2'})
45
46        partner = Partner.create({
47            'name': 'Bob Partner',
48            'company_id': company_2.id
49        })
50
51        # case 1 : the user has no partner
52        test_user = User.create({
53            'name': 'John Smith',
54            'login': 'jsmith',
55            'company_ids': [company_1.id],
56            'company_id': company_1.id
57        })
58
59        self.assertFalse(
60            test_user.partner_id.company_id,
61            "The partner_id linked to a user should be created without any company_id")
62
63        # case 2 : the user has a partner
64        test_user = User.create({
65            'name': 'Bob Smith',
66            'login': 'bsmith',
67            'company_ids': [company_1.id],
68            'company_id': company_1.id,
69            'partner_id': partner.id
70        })
71
72        self.assertEqual(
73            test_user.partner_id.company_id,
74            company_1,
75            "If the partner_id of a user has already a company, it is replaced by the user company"
76        )
77
78
79    def test_change_user_company(self):
80        """ Check the partner company update when the user company is changed """
81
82        User = self.env['res.users']
83        Company = self.env['res.company']
84
85        test_user = User.create({'name': 'John Smith', 'login': 'jsmith'})
86        company_1 = Company.create({'name': 'company_1'})
87        company_2 = Company.create({'name': 'company_2'})
88
89        test_user.company_ids += company_1
90        test_user.company_ids += company_2
91
92        # 1: the partner has no company_id, no modification
93        test_user.write({
94            'company_id': company_1.id
95        })
96
97        self.assertFalse(
98            test_user.partner_id.company_id,
99            "On user company change, if its partner_id has no company_id,"
100            "the company_id of the partner_id shall NOT be updated")
101
102        # 2: the partner has a company_id different from the new one, update it
103        test_user.partner_id.write({
104            'company_id': company_1.id
105        })
106
107        test_user.write({
108            'company_id': company_2.id
109        })
110
111        self.assertEqual(
112            test_user.partner_id.company_id,
113            company_2,
114            "On user company change, if its partner_id has already a company_id,"
115            "the company_id of the partner_id shall be updated"
116        )
117
118@tagged('post_install', '-at_install')
119class TestUsers2(TransactionCase):
120    def test_reified_groups(self):
121        """ The groups handler doesn't use the "real" view with pseudo-fields
122        during installation, so it always works (because it uses the normal
123        groups_id field).
124        """
125        # use the specific views which has the pseudo-fields
126        f = Form(self.env['res.users'], view='base.view_users_form')
127        f.name = "bob"
128        f.login = "bob"
129        user = f.save()
130
131        self.assertIn(self.env.ref('base.group_user'), user.groups_id)
132