-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] tests: add an helper in odoo tests in order to quickly create t…
…est 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 odoo#27526.
- Loading branch information
1 parent
5a552f0
commit 7429c61
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
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 '[email protected]' (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.%[email protected]' % (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 @@ def shortDescription(self): | |
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 | ||
|