Create admin-user after syncdb

This commit is contained in:
Oskar Hahn 2013-06-16 12:00:57 +02:00
parent 2f10996933
commit 64af0b162f
12 changed files with 115 additions and 124 deletions

View File

@ -182,11 +182,10 @@ def _main(opts, database_path=None):
# Create Database if necessary # Create Database if necessary
if not database_exists() or opts.syncdb: if not database_exists() or opts.syncdb:
run_syncdb() run_syncdb()
create_or_reset_admin_user()
# Reset Admin # Reset Admin
elif opts.reset_admin: elif opts.reset_admin:
create_or_reset_admin_user() reset_admin_user()
if opts.backupdb: if opts.backupdb:
backup_database(opts.backupdb) backup_database(opts.backupdb)
@ -294,23 +293,11 @@ def run_syncdb():
execute_from_command_line(argv) execute_from_command_line(argv)
def create_or_reset_admin_user(): def reset_admin_user():
# can't be imported in global scope as it already requires # can't be imported in global scope as it already requires
# the settings module during import # the settings module during import
from openslides.participant.models import User from openslides.participant.api import create_or_reset_admin_user
try: create_or_reset_admin_user()
admin = User.objects.get(username="admin")
print("Password for user admin was reset to 'admin'")
except User.DoesNotExist:
admin = User()
admin.username = 'admin'
admin.last_name = 'Administrator'
print("Created default admin user")
admin.is_superuser = True
admin.default_password = 'admin'
admin.set_password(admin.default_password)
admin.save()
def backup_database(dest_path): def backup_database(dest_path):

View File

@ -113,3 +113,18 @@ def get_registered_group():
Returns the group 'Registered' (pk=2). Returns the group 'Registered' (pk=2).
""" """
return Group.objects.get(pk=2) return Group.objects.get(pk=2)
def create_or_reset_admin_user():
group_staff = Group.objects.get(pk=4)
try:
admin = User.objects.get(username="admin")
except User.DoesNotExist:
admin = User()
admin.username = 'admin'
admin.last_name = 'Administrator'
admin.default_password = 'admin'
admin.set_password(admin.default_password)
admin.save()
admin.groups.add(group_staff)

View File

@ -21,6 +21,7 @@ from openslides.config.signals import config_signal
from openslides.config.api import ConfigVariable, ConfigPage from openslides.config.api import ConfigVariable, ConfigPage
from .models import Group from .models import Group
from .api import create_or_reset_admin_user
@receiver(config_signal, dispatch_uid='setup_participant_config_page') @receiver(config_signal, dispatch_uid='setup_participant_config_page')
@ -62,10 +63,14 @@ def setup_participant_config_page(sender, **kwargs):
participant_sort_users_by_first_name)) participant_sort_users_by_first_name))
@receiver(post_database_setup, dispatch_uid='participant_create_builtin_groups') @receiver(post_database_setup, dispatch_uid='participant_create_builtin_groups_and_admin')
def create_builtin_groups(sender, **kwargs): def create_builtin_groups_and_admin(sender, **kwargs):
""" """
Creates the buildin groups and the admin user.
Creates the builtin groups: Anonymous, Registered, Delegates and Staff. Creates the builtin groups: Anonymous, Registered, Delegates and Staff.
Creates the builtin user: admin.
""" """
# Check whether the group pks 1 to 4 are free # Check whether the group pks 1 to 4 are free
if Group.objects.filter(pk__in=range(1, 5)).exists(): if Group.objects.filter(pk__in=range(1, 5)).exists():
@ -123,3 +128,6 @@ def create_builtin_groups(sender, **kwargs):
group_staff = Group.objects.create(name=ugettext_noop('Staff'), pk=4) group_staff = Group.objects.create(name=ugettext_noop('Staff'), pk=4)
group_staff.permissions.add(perm_7, perm_9, perm_10, perm_10a, perm_11, perm_12, perm_13, perm_14, perm_15, perm_15a, perm_16) group_staff.permissions.add(perm_7, perm_9, perm_10, perm_10a, perm_11, perm_12, perm_13, perm_14, perm_15, perm_15a, perm_16)
# Admin user
create_or_reset_admin_user()

View File

@ -24,11 +24,12 @@ class Command(_Command):
Calls Django's syncdb command but always in non-interactive mode. After Calls Django's syncdb command but always in non-interactive mode. After
this it sends our post_database_setup signal. this it sends our post_database_setup signal.
""" """
interactive = kwargs.pop('interactive') interactive = kwargs.get('interactive', False)
return_value = super(Command, self).handle_noargs(*args, interactive=False, **kwargs) kwargs['interactive'] = False
return_value = super(Command, self).handle_noargs(*args, **kwargs)
post_database_setup.send(sender=self) post_database_setup.send(sender=self)
if interactive: if interactive:
print('Interactive mode (e. g. creating a superuser) is not possibile ' print('Interactive mode (e. g. creating a superuser) is not possibile '
'via this command. To create a superuser use the --reset-admin ' 'in OpenSlides. A superuser is automaticly created.')
'option of the main script.')
return return_value return return_value

View File

@ -78,7 +78,7 @@ class ListOfSpeakerModelTests(TestCase):
class SpeakerViewTestCase(TestCase): class SpeakerViewTestCase(TestCase):
def setUp(self): def setUp(self):
# Admin # Admin
self.admin = User.objects.create_superuser('admin', 'admin@admin.admin', 'admin') self.admin = User.objects.get(pk=1)
self.admin_client = Client() self.admin_client = Client()
self.admin_client.login(username='admin', password='admin') self.admin_client.login(username='admin', password='admin')

View File

@ -101,14 +101,10 @@ class ViewTest(TestCase):
self.item2 = Item.objects.create(title='item2') self.item2 = Item.objects.create(title='item2')
self.refreshItems() self.refreshItems()
self.admin, created = User.objects.get_or_create(username='testadmin') self.admin = User.objects.get(pk=1)
self.anonym, created = User.objects.get_or_create(username='testanonym') self.anonym, created = User.objects.get_or_create(username='testanonym')
self.admin.reset_password('default')
self.anonym.reset_password('default') self.anonym.reset_password('default')
self.admin.is_superuser = True
self.admin.save()
def refreshItems(self): def refreshItems(self):
self.item1 = Item.objects.get(pk=self.item1.id) self.item1 = Item.objects.get(pk=self.item1.id)
self.item2 = Item.objects.get(pk=self.item2.id) self.item2 = Item.objects.get(pk=self.item2.id)
@ -116,7 +112,7 @@ class ViewTest(TestCase):
@property @property
def adminClient(self): def adminClient(self):
c = Client() c = Client()
c.login(username='testadmin', password='default') c.login(username='admin', password='admin')
return c return c
@property @property

View File

@ -19,7 +19,7 @@ from openslides.participant.models import User, Group
class AssignmentViewTestCase(TestCase): class AssignmentViewTestCase(TestCase):
def setUp(self): def setUp(self):
# Admin # Admin
self.admin = User.objects.create_superuser('admin', 'admin@admin.admin', 'admin') self.admin = User.objects.get(pk=1)
self.admin_client = Client() self.admin_client = Client()
self.admin_client.login(username='admin', password='admin') self.admin_client.login(username='admin', password='admin')

View File

@ -36,9 +36,7 @@ class MediafileTest(TestCase):
perm_3 = Permission.objects.get(content_type=ct, codename='can_manage') perm_3 = Permission.objects.get(content_type=ct, codename='can_manage')
# Setup three different users # Setup three different users
self.manager = User.objects.create(username='mediafile_test_manager') self.manager = User.objects.get(pk=1)
self.manager.reset_password('default')
self.manager.user_permissions.add(perm_1, perm_2, perm_3)
self.vip_user = User.objects.create(username='mediafile_test_vip_user') self.vip_user = User.objects.create(username='mediafile_test_vip_user')
self.vip_user.reset_password('default') self.vip_user.reset_password('default')
self.vip_user.user_permissions.add(perm_1, perm_2) self.vip_user.user_permissions.add(perm_1, perm_2)
@ -69,7 +67,7 @@ class MediafileTest(TestCase):
Helper function to login all three test users. Helper function to login all three test users.
""" """
client_manager = Client() client_manager = Client()
client_manager.login(username='mediafile_test_manager', password='default') client_manager.login(username='admin', password='admin')
client_vip_user = Client() client_vip_user = Client()
client_vip_user.login(username='mediafile_test_vip_user', password='default') client_vip_user.login(username='mediafile_test_vip_user', password='default')
client_normal_user = Client() client_normal_user = Client()
@ -91,7 +89,7 @@ class MediafileTest(TestCase):
clients = self.login_clients() clients = self.login_clients()
response = clients['client_manager'].get('/mediafile/new/') response = clients['client_manager'].get('/mediafile/new/')
self.assertContains(response, '---------', status_code=200) self.assertContains(response, '---------', status_code=200)
self.assertContains(response, '<option value="user:1" selected="selected">mediafile_test_manager</option>', status_code=200) self.assertContains(response, '<option value="user:1" selected="selected">Administrator</option>', status_code=200)
self.assertTemplateUsed(response, 'mediafile/mediafile_form.html') self.assertTemplateUsed(response, 'mediafile/mediafile_form.html')
response = clients['client_vip_user'].get('/mediafile/new/') response = clients['client_vip_user'].get('/mediafile/new/')
self.assertNotContains(response, '<select id="id_uploader" name="uploader">', status_code=200) self.assertNotContains(response, '<select id="id_uploader" name="uploader">', status_code=200)

View File

@ -19,7 +19,7 @@ from openslides.motion.models import Motion, State
class MotionViewTestCase(TestCase): class MotionViewTestCase(TestCase):
def setUp(self): def setUp(self):
# Admin # Admin
self.admin = User.objects.create_superuser('admin', 'admin@admin.admin', 'admin') self.admin = User.objects.get(pk=1)
self.admin_client = Client() self.admin_client = Client()
self.admin_client.login(username='admin', password='admin') self.admin_client.login(username='admin', password='admin')

View File

@ -9,7 +9,68 @@
""" """
from openslides.utils.test import TestCase from openslides.utils.test import TestCase
from openslides.participant.models import Group
from openslides.utils.person import get_person, Persons
from openslides.participant.models import User, Group
from openslides.participant.api import gen_username, gen_password
class UserTest(TestCase):
def setUp(self):
self.user1 = User()
self.user1.first_name = u'Max'
self.user1.last_name = u'Mustermann'
self.user1.username = gen_username(
self.user1.first_name, self.user1.last_name)
self.user1.default_password = gen_password()
self.user1.save()
self.django_user1 = self.user1.django_user
def test_participant_user(self):
self.assertEqual(self.django_user1.user, self.user1)
self.assertEqual(self.django_user1, self.user1.django_user)
def test_repr(self):
self.assertEqual(unicode(self.user1), 'Max Mustermann')
def test_name_suffix(self):
self.user1.structure_level = u'München'
self.user1.save()
self.assertEqual(unicode(self.user1), u'Max Mustermann (München)')
def test_reset_password(self):
self.assertIsInstance(self.user1.default_password, basestring)
self.assertEqual(len(self.user1.default_password), 8)
self.user1.set_unusable_password()
self.assertFalse(self.user1.check_password(self.user1.default_password))
self.user1.reset_password()
self.assertTrue(self.user1.check_password(self.user1.default_password))
def test_person_api(self):
self.assertTrue(hasattr(self.user1, 'person_id'))
self.assertEqual(self.user1.person_id, 'user:2')
self.assertEqual(get_person('user:2'), self.user1)
self.assertEqual(len(Persons(person_prefix_filter='user')), 2)
class GroupTest(TestCase):
def setUp(self):
self.group1 = Group.objects.create(name='Test Group')
self.django_group1 = self.group1.django_group
def test_group_group(self):
self.assertEqual(self.group1.django_group, self.django_group1)
self.assertEqual(self.group1, self.django_group1.group)
def test_person_api(self):
self.assertTrue(hasattr(self.group1, 'person_id'))
person_id = "group:%d" % self.group1.id
self.assertEqual(self.group1.person_id, person_id)
self.assertRaises(Group.DoesNotExist)
self.group1.group_as_person = True
self.group1.save()
self.assertEqual(get_person(person_id), self.group1)
class DefaultGroups(TestCase): class DefaultGroups(TestCase):

View File

@ -21,10 +21,10 @@ class GroupViews(TestCase):
Tests the detail view for groups and later also the other views. Tests the detail view for groups and later also the other views.
""" """
def setUp(self): def setUp(self):
self.user_1 = User.objects.create(last_name='chahshah7eiqueip5eiW', self.user_1 = User.objects.get(pk=1)
first_name='mi6iu2Te6ei9iohue3ex', self.user_1.first_name = 'admins_first_name'
username='mi6iu2Te6ei9iohue3ex chahshah7eiqueip5eiW', self.user_1.save()
is_superuser=True)
self.user_2 = User.objects.create(last_name='uquahx3Wohtieph9baer', self.user_2 = User.objects.create(last_name='uquahx3Wohtieph9baer',
first_name='aWei4ien6Se0vie0xeiv', first_name='aWei4ien6Se0vie0xeiv',
username='aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer') username='aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer')
@ -40,17 +40,17 @@ class GroupViews(TestCase):
def test_detail(self): def test_detail(self):
self.assertFalse(config['participant_sort_users_by_first_name']) self.assertFalse(config['participant_sort_users_by_first_name'])
response = self.client.get('/participant/group/3/') response = self.client.get('/participant/group/3/')
pattern = r'mi6iu2Te6ei9iohue3ex chahshah7eiqueip5eiW|aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer' pattern = r'admins_first_name Administrator|aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer'
match = re.findall(pattern, response.content) match = re.findall(pattern, response.content)
self.assertEqual(match[0], 'mi6iu2Te6ei9iohue3ex chahshah7eiqueip5eiW') self.assertEqual(match[0], 'admins_first_name Administrator')
self.assertEqual(match[1], 'aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer') self.assertEqual(match[1], 'aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer')
config['participant_sort_users_by_first_name'] = True config['participant_sort_users_by_first_name'] = True
self.assertTrue(config['participant_sort_users_by_first_name']) self.assertTrue(config['participant_sort_users_by_first_name'])
response = self.client.get('/participant/group/3/') response = self.client.get('/participant/group/3/')
pattern = r'mi6iu2Te6ei9iohue3ex chahshah7eiqueip5eiW|aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer' pattern = r'admins_first_name Administrator|aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer'
match = re.findall(pattern, response.content) match = re.findall(pattern, response.content)
self.assertEqual(match[1], 'mi6iu2Te6ei9iohue3ex chahshah7eiqueip5eiW') self.assertEqual(match[1], 'admins_first_name Administrator')
self.assertEqual(match[0], 'aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer') self.assertEqual(match[0], 'aWei4ien6Se0vie0xeiv uquahx3Wohtieph9baer')
@ -60,15 +60,13 @@ class LockoutProtection(TestCase):
something that removes his last permission to manage participants. something that removes his last permission to manage participants.
""" """
def setUp(self): def setUp(self):
self.user = User.objects.create(last_name='AQu9ie7ach2ek2Xoozoo', self.user = User.objects.get(pk=1)
first_name='guR3La9alah7lahsief6',
username='Iedei0eecoh1aiwahnoo')
self.user.reset_password('default')
self.user.groups.add(Group.objects.get(pk=4)) self.user.groups.add(Group.objects.get(pk=4))
self.client = Client() self.client = Client()
self.client.login(username='Iedei0eecoh1aiwahnoo', password='default') self.client.login(username='admin', password='admin')
self.assertEqual(User.objects.count(), 1) self.assertEqual(User.objects.count(), 1)
self.assertEqual(Group.objects.count(), 4) self.assertEqual(Group.objects.count(), 4)
self.assertFalse(self.user.is_superuser)
def test_delete_yourself(self): def test_delete_yourself(self):
response = self.client.get('/participant/1/del/') response = self.client.get('/participant/1/del/')

View File

@ -1,73 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.participant.tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unit test for the participant app.
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
:license: GNU GPL, see LICENSE for more details.
"""
from openslides.utils.test import TestCase
from openslides.utils.person import get_person, Persons
from openslides.participant.api import gen_username, gen_password
from openslides.participant.models import User, Group
class UserTest(TestCase):
def setUp(self):
self.user1 = User()
self.user1.first_name = u'Max'
self.user1.last_name = u'Mustermann'
self.user1.username = gen_username(
self.user1.first_name, self.user1.last_name)
self.user1.default_password = gen_password()
self.user1.save()
self.django_user1 = self.user1.django_user
def test_participant_user(self):
self.assertEqual(self.django_user1.user, self.user1)
self.assertEqual(self.django_user1, self.user1.django_user)
def test_repr(self):
self.assertEqual(unicode(self.user1), 'Max Mustermann')
def test_name_suffix(self):
self.user1.structure_level = u'München'
self.user1.save()
self.assertEqual(unicode(self.user1), u'Max Mustermann (München)')
def test_reset_password(self):
self.assertIsInstance(self.user1.default_password, basestring)
self.assertEqual(len(self.user1.default_password), 8)
self.user1.set_unusable_password()
self.assertFalse(self.user1.check_password(self.user1.default_password))
self.user1.reset_password()
self.assertTrue(self.user1.check_password(self.user1.default_password))
def test_person_api(self):
self.assertTrue(hasattr(self.user1, 'person_id'))
self.assertEqual(self.user1.person_id, 'user:1')
self.assertEqual(get_person('user:1'), self.user1)
self.assertEqual(len(Persons(person_prefix_filter='user')), 1)
class GroupTest(TestCase):
def setUp(self):
self.group1 = Group.objects.create(name='Test Group')
self.django_group1 = self.group1.django_group
def test_group_group(self):
self.assertEqual(self.group1.django_group, self.django_group1)
self.assertEqual(self.group1, self.django_group1.group)
def test_person_api(self):
self.assertTrue(hasattr(self.group1, 'person_id'))
person_id = "group:%d" % self.group1.id
self.assertEqual(self.group1.person_id, person_id)
self.assertRaises(Group.DoesNotExist)
self.group1.group_as_person = True
self.group1.save()
self.assertEqual(get_person(person_id), self.group1)