This question has been flagged
1 Reply
6037 Views

Hello,

I used classic inheritance on 'res.users' model, to have some additional fields in user and I also have an additional method. Therefore, I would like to write a test for that method and would like to build a user only for the tests.

I was using the 'demo.xml' file since then, but I don’t think it is a good approach, since the actual users of the module will not use odoo in demonstration mode. So I’m afraid the demo.xml data won’t be accessible with the demonstration mode disabled.

So I tried this :

class TestResUsers(TransactionCase):
   
    def setUp(self, *args, **kwargs):
        super(TestResUsers, self).setUp(*args, **kwargs)
        
        self.test_user = self.env['res.users'].create({
            'name': 'test user',
        })

But I get the following error, when the tests run :


ERROR
======================================================================
ERROR: test_get_client (odoo.addons.connector_trello.tests.test_res_users.TestTrelloUsers)
Traceback (most recent call last):
`   File "/home/cyrille/odoo/addons-dev/connector_trello/tests/test_res_users.py", line 13, in setUp
`     'name': 'test user',
`   File "/opt/odoo/odoo/addons/sales_team/models/res_users.py", line 17, in create
`     user = super(ResUsers, self).create(vals)
`   File "/opt/odoo/odoo/addons/auth_signup/models/res_users.py", line 154, in create
`     user = super(ResUsers, self).create(values)
`   File "/opt/odoo/odoo/addons/mail/models/res_users.py", line 55, in create
`     raise exceptions.RedirectWarning(msg, action.id, _('Go to the configuration panel'))
` odoo.exceptions.RedirectWarning: ('You cannot create a new user from here.\n To create new user please go to configuration panel.', 65, 'Go to the configuration panel')


Apparently, I can’t create a user from the test environment. Is it an access right issue ? Is there another solution ?

Thanks.

Avatar
Discard
Author

@Yenthe Van Ginneken Indeed, it worked, thank you very much

You're welcome! Happy it helped.

Best Answer

Hi LaLibreRie,

You shouldn't directly create a user with a name from here. Instead create an user with a 'login' which you link to the res.partner record that you create while creating the user. This way the exception will not go off. Example:

self.test_user = self.env['res.users'].create({
    'login': 'testuser',
    'partner_id': self.env['res.partner'].create({
        'name': "Strawman Test User"
    }).id
})

Regards,
Yenthe

Avatar
Discard