Help

4

How can I change the user permissions during unit tests?

Avatar
Atte Isopuro

After a few attempts, it seems that the \instructions on how to change the user during code execution do not apply during unit tests, as I get this traceback:

2016-02-04 14:39:27,617 10921 ERROR testing openerp.addons.custom_module.tests.test_notification: ERROR

2016-02-04 14:39:27,618 10921 INFO testing openerp.addons.custom_module.tests.test_notification: ======================================================================

2016-02-04 14:39:27,618 10921 ERROR testing openerp.addons.custom_module.tests.test_notification: ERROR: test_010_process_should_notify (openerp.addons.custom_module.tests.test_notification.TestNotification)

2016-02-04 14:39:27,618 10921 ERROR testing openerp.addons.custom_module.tests.test_notification: Traceback (most recent call last):

2016-02-04 14:39:27,618 10921 ERROR testing openerp.addons.custom_module.tests.test_notification: ` File "/home/aisopuro/project/addons/custom_module/tests/test_notification.py", line 36, in setUp

2016-02-04 14:39:27,618 10921 ERROR testing openerp.addons.custom_module.tests.test_notification: ` self.sudo(self.sales_user.id).order.action_button_confirm()

2016-02-04 14:39:27,618 10921 ERROR testing openerp.addons.custom_module.tests.test_notification: ` AttributeError: 'TestNotification' object has no attribute 'sudo'

2016-02-04 14:39:27,618 10921 INFO testing openerp.addons.custom_module.tests.test_notification: Ran 1 test in 1.015s

2016-02-04 14:39:27,618 10921 ERROR testing openerp.addons.custom_module.tests.test_notification: FAILED

2016-02-04 14:39:27,618 10921 INFO testing openerp.addons.custom_module.tests.test_notification: (errors=1)

The relevant Python code is as follows:

self.sales_user = self.env['res.partner'].create({

'name': 'Krong',

'email': 'krong',

'groups_id': [(6, None, self.env.ref("base.group_sale_salesman"))]

})

self.sudo(self.sales_user.id).order.action_button_confirm()

Is there a way to change the user in v8 unit tests? I would prefer to be able to change users for short periods of time: basically I need to simulate a salesman setting up a sale order and a warehouse employee marking the resulting picking as done, to test that access rights aren't being violated.

Avatar
Discard
3 Answers
1
Best Answer

Hello, sudo take a user id and not partner id... That Will not Fix your problem, but the next one:)


You can try to maka sudo after .order : self.order.sudo().

Because self is a Testnotification ... But sudo is a function on class Model


if it doesn't work, provide more codes. 

1 Comment
Avatar
Discard
Avatar
Atte Isopuro
-

Right. *facepalm*. The part about using .sudo() on .order is also correct. Thank you.

0
Best Answer

Headline says "How can I change the user permissions". 

For example quick adding user to purchase manager group:

self.user_a.groups_id += self.env.ref('purchase.group_purchase_manager')
Switch user executing the tests
self.env.uid = self.env.ref('account_invoice_inter_company.user_company_a').id
Avatar
Discard
0
Best Answer

What about if I am testing a query ? Many multicompany entities use the user's company to filter.

As long as I see here the user is applied to the resulting objects., but these objects would be others using the new user.

Avatar
Discard