Bump object-permissions to newest version 1.4.6.
[ganeti_webmgr.git] / django_test_tools / users.py
blob42af0d27c5d19e6cfe47a053e59bd40de76cdedb
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,
16 # USA.
18 from django.contrib.auth.models import User
21 class UserTestMixin():
22 """
23 Mixin providing functions for easily creating users
24 """
26 @classmethod
27 def create_user(cls, username='tester', **kwargs):
28 user, new = User.objects.get_or_create(username=username, **kwargs)
29 user.set_password('secret')
30 user.save()
31 return user
33 def create_users(self, users, context=None):
34 """
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.
45 """
47 if context is None:
48 context = {}
50 for name in 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})
57 return context
59 def create_standard_users(self, context=None):
60 """
61 Create two common users: "unauthorized", a user who is not authorized
62 to do things, and "superuser", a superuser.
63 """
65 if context is None:
66 context = {}
68 return self.create_users([
69 'unauthorized',
70 ('superuser', {'is_superuser': True})
71 ], context)