| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
|
| 4 |
from odoo.exceptions import AccessError |
| 5 |
from odoo.addons.sale_purchase.tests.common import TestCommonSalePurchaseNoChart |
| 6 |
from odoo.tests import tagged |
| 7 |
|
| 8 |
|
| 9 |
@tagged('-at_install', 'post_install') |
| 10 |
class TestAccessRights(TestCommonSalePurchaseNoChart): |
| 11 |
|
| 12 |
@classmethod |
| 13 |
def setUpClass(cls): |
| 14 |
super().setUpClass() |
| 15 |
|
| 16 |
# Create a users |
| 17 |
group_sale_user = cls.env.ref('sales_team.group_sale_salesman') |
| 18 |
group_purchase_user = cls.env.ref('purchase.group_purchase_user') |
| 19 |
cls.user_salesperson = cls.env['res.users'].with_context(no_reset_password=True).create({ |
| 20 |
'name': 'Le Grand Jojo User', |
| 21 |
'login': 'grand.jojo', |
| 22 |
'email': '[email protected]', |
| 23 |
'group_ids': [(6, 0, [group_sale_user.id])] |
| 24 |
}) |
| 25 |
cls.user_purchaseperson = cls.env['res.users'].with_context(no_reset_password=True).create({ |
| 26 |
'name': 'Jean-Luc Fonck', |
| 27 |
'login': 'jl.fonck', |
| 28 |
'email': '[email protected]', |
| 29 |
'group_ids': [(6, 0, [group_purchase_user.id])] |
| 30 |
}) |
| 31 |
|
| 32 |
def test_access_saleperson(self): |
| 33 |
""" Check a saleperson (only) can generate a PO and a PO user can not confirm a SO """ |
| 34 |
SaleOrder = self.env['sale.order'].with_context(tracking_disable=True) |
| 35 |
|
| 36 |
sale_order = SaleOrder.with_user(self.user_salesperson).create({ |
| 37 |
'partner_id': self.partner_a.id, |
| 38 |
'user_id': self.user_salesperson.id |
| 39 |
}) |
| 40 |
|
| 41 |
sol_service_purchase = self.env['sale.order.line'].with_user(self.user_salesperson).create({ |
| 42 |
'product_id': self.service_purchase_1.id, |
| 43 |
'product_uom_qty': 4, |
| 44 |
'order_id': sale_order.id, |
| 45 |
'tax_ids': False, |
| 46 |
}) |
| 47 |
|
| 48 |
# confirming SO will create the PO even if you don't have the rights |
| 49 |
sale_order.action_confirm() |
| 50 |
sale_order._action_cancel() |
| 51 |
|
| 52 |
self.assertTrue(sale_order.name, "Saleperson can read its own SO") |
| 53 |
|
| 54 |
action = sale_order.sudo().action_view_purchase_orders() |
| 55 |
|
| 56 |
# try to access PO as sale person |
| 57 |
with self.assertRaises(AccessError): |
| 58 |
purchase_orders = self.env['purchase.order'].with_user(self.user_salesperson).browse(action['res_id']) |
| 59 |
purchase_orders.read() |
| 60 |
|
| 61 |
# try to access PO as purchase person |
| 62 |
purchase_orders = self.env['purchase.order'].with_user(self.user_purchaseperson).browse(action['res_id']) |
| 63 |
purchase_orders.read() |
| 64 |
|
| 65 |
# try to access the PO lines from the SO, as sale person |
| 66 |
with self.assertRaises(AccessError): |
| 67 |
sol_service_purchase.with_user(self.user_salesperson).purchase_line_ids.read() |