More docs for kfet.tests.utils
This commit is contained in:
parent
7d490f0253
commit
997b63d6b6
1 changed files with 82 additions and 0 deletions
|
@ -8,6 +8,21 @@ User = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
def _create_user_and_account(user_attrs, account_attrs, perms=None):
|
def _create_user_and_account(user_attrs, account_attrs, perms=None):
|
||||||
|
"""
|
||||||
|
Create a user and its account, and assign permissions to this user.
|
||||||
|
|
||||||
|
Arguments
|
||||||
|
user_attrs (dict): User data (first name, last name, password...).
|
||||||
|
account_attrs (dict): Account data (department, kfet password...).
|
||||||
|
perms (list of str: 'app.perm'): These permissions will be assigned to
|
||||||
|
the created user. No permission are assigned by default.
|
||||||
|
|
||||||
|
If 'password' is not given in 'user_attrs', username is used as password.
|
||||||
|
|
||||||
|
If 'kfet.is_team' is in 'perms' and 'password' is not in 'account_attrs',
|
||||||
|
the account password is 'kfetpwd_<user pwd>'.
|
||||||
|
|
||||||
|
"""
|
||||||
user_pwd = user_attrs.pop('password', user_attrs['username'])
|
user_pwd = user_attrs.pop('password', user_attrs['username'])
|
||||||
user = User.objects.create(**user_attrs)
|
user = User.objects.create(**user_attrs)
|
||||||
user.set_password(user_pwd)
|
user.set_password(user_pwd)
|
||||||
|
@ -29,6 +44,27 @@ def _create_user_and_account(user_attrs, account_attrs, perms=None):
|
||||||
|
|
||||||
|
|
||||||
def create_user(username='user', trigramme='000', **kwargs):
|
def create_user(username='user', trigramme='000', **kwargs):
|
||||||
|
"""
|
||||||
|
Create a user without any permission and its kfet account.
|
||||||
|
|
||||||
|
username and trigramme are accepted as arguments (defaults to 'user' and
|
||||||
|
'000').
|
||||||
|
|
||||||
|
user_attrs, account_attrs and perms can be given as keyword arguments to
|
||||||
|
customize the user and its kfet account.
|
||||||
|
|
||||||
|
# Default values
|
||||||
|
|
||||||
|
User
|
||||||
|
* username: user
|
||||||
|
* password: user
|
||||||
|
* first_name: first
|
||||||
|
* last_name: last
|
||||||
|
* email: mail@user.net
|
||||||
|
Account
|
||||||
|
* trigramme: 000
|
||||||
|
|
||||||
|
"""
|
||||||
user_attrs = kwargs.setdefault('user_attrs', {})
|
user_attrs = kwargs.setdefault('user_attrs', {})
|
||||||
|
|
||||||
user_attrs.setdefault('username', username)
|
user_attrs.setdefault('username', username)
|
||||||
|
@ -43,6 +79,28 @@ def create_user(username='user', trigramme='000', **kwargs):
|
||||||
|
|
||||||
|
|
||||||
def create_team(username='team', trigramme='100', **kwargs):
|
def create_team(username='team', trigramme='100', **kwargs):
|
||||||
|
"""
|
||||||
|
Create a user, member of the kfet team, and its kfet account.
|
||||||
|
|
||||||
|
username and trigramme are accepted as arguments (defaults to 'team' and
|
||||||
|
'100').
|
||||||
|
|
||||||
|
user_attrs, account_attrs and perms can be given as keyword arguments to
|
||||||
|
customize the user and its kfet account.
|
||||||
|
|
||||||
|
# Default values
|
||||||
|
|
||||||
|
User
|
||||||
|
* username: team
|
||||||
|
* password: team
|
||||||
|
* first_name: team
|
||||||
|
* last_name: member
|
||||||
|
* email: mail@team.net
|
||||||
|
Account
|
||||||
|
* trigramme: 100
|
||||||
|
* kfet password: kfetpwd_team
|
||||||
|
|
||||||
|
"""
|
||||||
user_attrs = kwargs.setdefault('user_attrs', {})
|
user_attrs = kwargs.setdefault('user_attrs', {})
|
||||||
|
|
||||||
user_attrs.setdefault('username', username)
|
user_attrs.setdefault('username', username)
|
||||||
|
@ -60,6 +118,29 @@ def create_team(username='team', trigramme='100', **kwargs):
|
||||||
|
|
||||||
|
|
||||||
def create_root(username='root', trigramme='200', **kwargs):
|
def create_root(username='root', trigramme='200', **kwargs):
|
||||||
|
"""
|
||||||
|
Create a superuser and its kfet account.
|
||||||
|
|
||||||
|
username and trigramme are accepted as arguments (defaults to 'root' and
|
||||||
|
'200').
|
||||||
|
|
||||||
|
user_attrs, account_attrs and perms can be given as keyword arguments to
|
||||||
|
customize the user and its kfet account.
|
||||||
|
|
||||||
|
# Default values
|
||||||
|
|
||||||
|
User
|
||||||
|
* username: root
|
||||||
|
* password: root
|
||||||
|
* first_name: super
|
||||||
|
* last_name: user
|
||||||
|
* email: mail@root.net
|
||||||
|
* is_staff, is_superuser: True
|
||||||
|
Account
|
||||||
|
* trigramme: 200
|
||||||
|
* kfet password: kfetpwd_root
|
||||||
|
|
||||||
|
"""
|
||||||
user_attrs = kwargs.setdefault('user_attrs', {})
|
user_attrs = kwargs.setdefault('user_attrs', {})
|
||||||
|
|
||||||
user_attrs.setdefault('username', username)
|
user_attrs.setdefault('username', username)
|
||||||
|
@ -75,6 +156,7 @@ def create_root(username='root', trigramme='200', **kwargs):
|
||||||
|
|
||||||
|
|
||||||
def get_perms(*labels):
|
def get_perms(*labels):
|
||||||
|
"""Return Permission instances from a list of '<app>.<perm_codename>'."""
|
||||||
perms = {}
|
perms = {}
|
||||||
for label in set(labels):
|
for label in set(labels):
|
||||||
app_label, codename = label.split('.', 1)
|
app_label, codename = label.split('.', 1)
|
||||||
|
|
Loading…
Reference in a new issue