In my Odoo module I Am trying to build an unit tests and perform an action with limited user privileges. The matter is that in Odoo all unit tests are executed with superuser privileges. In the following example I am trying to create a Partner with limited privileges (using sudo), so I can call action_edit_cfdi_customer function. But I am getting an error in 'self.env['res.partner'].sudo(self.limited_user)'
def test_action_edit_cfdi_customer_04_access_error(self):
self.limited_user = self.env['res.users'].create({
'name': 'Limited User',
'login': 'limited_user',
'email': 'limited_user@example.com',
'groups_id': [(6, 0, [self.env.ref('base.group_user').id])]
})
self.partner_model_limited = self.env['res.partner'].sudo(self.limited_user)
sample_partner_2 = self.partner_model_limited.create({
'name': 'Empresa Prueba',
'vat': 'EKU900317310',
})
with self.assertRaises(AccessDenied) as e:
sample_partner_2.action_edit_cfdi_customer()
This is the error
> File "/home/ernesto/Programming/odoo/viixoo_admin/odoo/models.py",
> line 5880, in sudo
> assert isinstance(flag, bool) AssertionError
So, I want to know if there is any way to execute that specific test as another user with limited provileges, or there is a way to call sudo in a model, so I can execute a models method as another User.
with_user()
should be used. For example:
self.env['res.partner'].with_user(self.limited_user)