Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

4

How can I change the user permissions during unit tests?

By
Atte Isopuro
on 2/4/16, 9:51 AM 553 views

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.

0

Jérémy Kersten (jke)

--Jérémy Kersten (jke)--
2983
| 3 2 5
Jodoigne, Belgium
--Jérémy Kersten (jke)--

Jérémy is a member of the core R&D team since september 2013. He developed several projects for OpenERP version 8 including the eCommerce, the Google Calendar synchronization and the new product variants/configurator. Now, he manages Odoo website and themes for saas.

Jérémy Kersten (jke)
On 2/8/16, 2:38 AM

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. 

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

Atte Isopuro
on 2/8/16, 8:38 AM
0

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.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/4/16, 9:51 AM
Seen: 553 times
Last updated: 4/6/16, 1:17 PM