test_access_rights.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.addons.account.tests.common import AccountTestInvoicingCommon
  4. from odoo.tests import Form, tagged
  5. from odoo.exceptions import AccessError
  6. @tagged('post_install', '-at_install')
  7. class TestPurchaseInvoice(AccountTestInvoicingCommon):
  8. @classmethod
  9. def setUpClass(cls, chart_template_ref=None):
  10. super().setUpClass(chart_template_ref=chart_template_ref)
  11. # Create a users
  12. group_purchase_user = cls.env.ref('purchase.group_purchase_user')
  13. group_employee = cls.env.ref('base.group_user')
  14. group_partner_manager = cls.env.ref('base.group_partner_manager')
  15. cls.purchase_user = cls.env['res.users'].with_context(
  16. no_reset_password=True
  17. ).create({
  18. 'name': 'Purchase user',
  19. 'login': 'purchaseUser',
  20. 'email': 'pu@odoo.com',
  21. 'groups_id': [(6, 0, [group_purchase_user.id, group_employee.id, group_partner_manager.id])],
  22. })
  23. cls.vendor = cls.env['res.partner'].create({
  24. 'name': 'Supplier',
  25. 'email': 'supplier.serv@supercompany.com',
  26. })
  27. cls.account_expense_product = cls.env['account.account'].create({
  28. 'code': 'EXPENSE.PROD111',
  29. 'name': 'Expense - Test Account',
  30. 'account_type': 'expense',
  31. })
  32. # Create category
  33. cls.product_category = cls.env['product.category'].create({
  34. 'name': 'Product Category with Expense account',
  35. 'property_account_expense_categ_id': cls.account_expense_product.id
  36. })
  37. cls.product = cls.env['product.product'].create({
  38. 'name': "Product",
  39. 'standard_price': 200.0,
  40. 'list_price': 180.0,
  41. 'type': 'service',
  42. })
  43. def test_create_purchase_order(self):
  44. """Check a purchase user can create a vendor bill from a purchase order but not post it"""
  45. purchase_order_form = Form(self.env['purchase.order'].with_user(self.purchase_user))
  46. purchase_order_form.partner_id = self.vendor
  47. with purchase_order_form.order_line.new() as line:
  48. line.name = self.product.name
  49. line.product_id = self.product
  50. line.product_qty = 4
  51. line.price_unit = 5
  52. purchase_order = purchase_order_form.save()
  53. purchase_order.button_confirm()
  54. purchase_order.order_line.qty_received = 4
  55. purchase_order.action_create_invoice()
  56. invoice = purchase_order.invoice_ids
  57. with self.assertRaises(AccessError):
  58. invoice.action_post()
  59. def test_read_purchase_order(self):
  60. """ Check that a purchase user can read all purchase order and 'in' invoices"""
  61. purchase_user_2 = self.purchase_user.copy({
  62. 'name': 'Purchase user 2',
  63. 'login': 'purchaseUser2',
  64. 'email': 'pu2@odoo.com',
  65. })
  66. purchase_order_form = Form(self.env['purchase.order'].with_user(purchase_user_2))
  67. purchase_order_form.partner_id = self.vendor
  68. with purchase_order_form.order_line.new() as line:
  69. line.name = self.product.name
  70. line.product_id = self.product
  71. line.product_qty = 4
  72. line.price_unit = 5
  73. purchase_order_user2 = purchase_order_form.save()
  74. purchase_order_user2.button_confirm()
  75. purchase_order_user2.order_line.qty_received = 4
  76. purchase_order_user2.action_create_invoice()
  77. vendor_bill_user2 = purchase_order_user2.invoice_ids
  78. # open purchase_order_user2 and vendor_bill_user2 with `self.purchase_user`
  79. purchase_order_user1 = Form(purchase_order_user2.with_user(self.purchase_user))
  80. purchase_order_user1 = purchase_order_user1.save()
  81. vendor_bill_user1 = Form(vendor_bill_user2.with_user(self.purchase_user))
  82. vendor_bill_user1 = vendor_bill_user1.save()
  83. def test_read_purchase_order_2(self):
  84. """ Check that a 2 purchase users with open the vendor bill the same
  85. way even with a 'own documents only' record rule. """
  86. # edit the account.move record rule for purchase user in order to ensure
  87. # a user can only see his own invoices
  88. rule = self.env.ref('purchase.purchase_user_account_move_rule')
  89. rule.domain_force = "['&', ('move_type', 'in', ('in_invoice', 'in_refund', 'in_receipt')), ('invoice_user_id', '=', user.id)]"
  90. # create a purchase and make a vendor bill from it as purchase user 2
  91. purchase_user_2 = self.purchase_user.copy({
  92. 'name': 'Purchase user 2',
  93. 'login': 'purchaseUser2',
  94. 'email': 'pu2@odoo.com',
  95. })
  96. purchase_order_form = Form(self.env['purchase.order'].with_user(purchase_user_2))
  97. purchase_order_form.partner_id = self.vendor
  98. with purchase_order_form.order_line.new() as line:
  99. line.name = self.product.name
  100. line.product_id = self.product
  101. line.product_qty = 4
  102. line.price_unit = 5
  103. purchase_order_user2 = purchase_order_form.save()
  104. purchase_order_user2.button_confirm()
  105. purchase_order_user2.order_line.qty_received = 4
  106. purchase_order_user2.action_create_invoice()
  107. vendor_bill_user2 = purchase_order_user2.invoice_ids
  108. # check user 1 cannot read the invoice
  109. with self.assertRaises(AccessError):
  110. Form(vendor_bill_user2.with_user(self.purchase_user))
  111. # Check that calling 'action_view_invoice' return the same action despite the record rule
  112. action_user_1 = purchase_order_user2.with_user(self.purchase_user).action_view_invoice()
  113. purchase_order_user2.invalidate_recordset()
  114. action_user_2 = purchase_order_user2.with_user(purchase_user_2).action_view_invoice()
  115. self.assertEqual(action_user_1, action_user_2)
  116. def test_double_validation(self):
  117. """Only purchase managers can approve a purchase order when double
  118. validation is enabled"""
  119. group_purchase_manager = self.env.ref('purchase.group_purchase_manager')
  120. order = self.env['purchase.order'].create({
  121. "partner_id": self.vendor.id,
  122. "order_line": [
  123. (0, 0, {
  124. 'product_id': self.product.id,
  125. 'name': f'{self.product.name} {1:05}',
  126. 'price_unit': 79.80,
  127. 'product_qty': 15.0,
  128. }),
  129. ]})
  130. company = order.sudo().company_id
  131. company.po_double_validation = 'two_step'
  132. company.po_double_validation_amount = 0
  133. self.purchase_user.write({
  134. 'company_ids': [(4, company.id)],
  135. 'company_id': company.id,
  136. 'groups_id': [(3, group_purchase_manager.id)],
  137. })
  138. order.with_user(self.purchase_user).button_confirm()
  139. self.assertEqual(order.state, 'to approve')
  140. order.with_user(self.purchase_user).button_approve()
  141. self.assertEqual(order.state, 'to approve')
  142. self.purchase_user.groups_id += group_purchase_manager
  143. order.with_user(self.purchase_user).button_approve()
  144. self.assertEqual(order.state, 'purchase')