1 # Copyright (C) 2011 Oregon State University et al.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 from django
.contrib
.auth
.models
import User
21 class UserTestMixin():
23 Mixin providing functions for easily creating users
27 def create_user(cls
, username
='tester', password
='secret', **kwargs
):
28 user
, new
= User
.objects
.get_or_create(username
=username
, **kwargs
)
29 user
.set_password(password
)
33 def create_users(self
, users
, context
=None):
35 Given a list of user names, create each user.
37 The users will be placed into a dict which will be returned. To use an
38 existing dict, override the ``context`` parameter.
40 The users will also be added to this class under their given names.
42 @param users - list of user names
43 @param context - dictionary to add users too
44 @return a dictionary of the users.
51 name
, kwargs
= name
if isinstance(name
, (tuple,)) else (name
, {})
52 user
= self
.create_user(name
, **kwargs
)
53 # New behavior: Add the user directly to this class.
54 setattr(self
, name
, user
)
55 context
.update({name
: user
})
59 def create_standard_users(self
, context
=None):
61 Create two common users: "unauthorized", a user who is not authorized
62 to do things, and "superuser", a superuser.
68 return self
.create_users([
70 ('superuser', {'is_superuser': True})