[IMP] tests: add an helper in odoo tests in order to quickly create test users
Following great @odony review we decided to add a helper method allowing to quickly create users in tests in a one-liner. It will allow to remove a lot of duplicate code in various test classes and methods. This method basically requires a login and a comma separated list of user groups xml ids. Kwargs are supported to further customize the created user. A custom context can be given to the creation as user creation in odoo can support several options within standard addons, like mailing or signup options. Next commits will propagate the use of this helper in some test classes notably in mail, hr and mass mailing. This commit is linked to task ID 1889703 and PR #27526.
This commit is contained in:
@@ -34,6 +34,7 @@ from lxml import etree, html
|
||||
from odoo.models import BaseModel
|
||||
from odoo.osv.expression import normalize_domain
|
||||
from odoo.tools import pycompat
|
||||
from odoo.tools import single_email_re
|
||||
from odoo.tools.misc import find_in_path
|
||||
from odoo.tools.safe_eval import safe_eval
|
||||
|
||||
@@ -116,6 +117,50 @@ def post_install(flag):
|
||||
"""
|
||||
return tagged('post_install' if flag else '-post_install')
|
||||
|
||||
|
||||
def new_test_user(env, login='', groups='base.group_user', context=None, **kwargs):
|
||||
""" Helper function to create a new test user. It allows to quickly create
|
||||
users given its login and groups (being a comma separated list of xml ids).
|
||||
Kwargs are directly propagated to the create to further customize the
|
||||
created user.
|
||||
|
||||
User creation uses a potentially customized environment using the context
|
||||
parameter allowing to specify a custom context. It can be used to force a
|
||||
specific behavior and/or simplify record creation. An example is to use
|
||||
mail-related context keys in mail tests to speedup record creation.
|
||||
|
||||
Some specific fields are automatically filled to avoid issues
|
||||
|
||||
* groups_id: it is filled using groups function parameter;
|
||||
* name: "login (groups)" by default as it is required;
|
||||
* email: it is either the login (if it is a valid email) or a generated
|
||||
string 'x.x@example.com' (x being the first login letter). This is due
|
||||
to email being required for most odoo operations;
|
||||
"""
|
||||
if not login:
|
||||
raise ValueError('New users require at least a login')
|
||||
if not groups:
|
||||
raise ValueError('New users require at least user groups')
|
||||
if context is None:
|
||||
context = {}
|
||||
|
||||
groups_id = [(6, 0, [env.ref(g).id for g in groups.split(',')])]
|
||||
create_values = dict(kwargs, login=login, groups_id=groups_id)
|
||||
if not create_values.get('name'):
|
||||
create_values['name'] = '%s (%s)' % (login, groups)
|
||||
if not create_values.get('email'):
|
||||
if single_email_re.match(login):
|
||||
create_values['email'] = login
|
||||
else:
|
||||
create_values['email'] = '%s.%s@example.com' % (login[0], login[0])
|
||||
|
||||
return env['res.users'].with_context(**context).create(create_values)
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Main classes
|
||||
# ------------------------------------------------------------
|
||||
|
||||
|
||||
class TreeCase(unittest.TestCase):
|
||||
def __init__(self, methodName='runTest'):
|
||||
super(TreeCase, self).__init__(methodName)
|
||||
@@ -315,6 +360,7 @@ class BaseCase(TreeCase, MetaCase('DummyCase', (object,), {})):
|
||||
def assertItemsEqual(self, a, b, msg=None):
|
||||
self.assertCountEqual(a, b, msg=None)
|
||||
|
||||
|
||||
class TransactionCase(BaseCase):
|
||||
""" TestCase in which each test method is run in its own transaction,
|
||||
and with its own cursor. The transaction is rolled back and the cursor
|
||||
|
||||
Reference in New Issue
Block a user