append tests for the participant app

This commit is contained in:
Oskar Hahn 2012-08-07 10:15:39 +02:00
parent ab33b9f05a
commit b55ceddb82
3 changed files with 102 additions and 5 deletions

View File

@ -124,7 +124,6 @@ INSTALLED_APPS = (
'django.contrib.messages',
'django.contrib.staticfiles',
'mptt',
'openslides.utils',
'openslides.poll',
'openslides.projector',
'openslides.agenda',

View File

@ -0,0 +1,87 @@
#!/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 django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User, Group
from django.db.models.query import EmptyQuerySet
from django.contrib.auth.hashers import check_password
from openslides.utils.user import get_user, Users
from openslides.participant.api import gen_username, gen_password, user2djangouser
from openslides.participant.models import Profile, DjangoGroup, DjangoUser
class ParticipantTest(TestCase):
def setUp(self):
self.user1 = User(first_name=u'Max', last_name=u'Mustermann')
self.user1.username = gen_username(self.user1.first_name, self.user1.last_name)
self.user1.save()
self.participant1 = Profile.objects.create(user=self.user1, firstpassword=gen_password())
def test_participant_user(self):
self.assertEqual(self.user1.profile, self.participant1)
self.assertEqual(self.user1, self.participant1.user)
def test_repr(self):
self.assertEqual(unicode(self.participant1), u'Max Mustermann')
def test_group(self):
self.participant1.group = u'München'
self.participant1.save()
self.assertEqual(unicode(self.participant1), u'Max Mustermann (München)')
def test_reset_password(self):
self.assertIsInstance(self.participant1.firstpassword, basestring)
self.assertEqual(len(self.participant1.firstpassword), 8)
self.user1.set_unusable_password()
self.assertFalse(self.user1.check_password(self.participant1.firstpassword))
self.participant1.reset_password()
self.assertTrue(self.user1.check_password(self.participant1.firstpassword))
def test_user_api(self):
self.assertTrue(hasattr(self.participant1, 'uid'))
self.assertEqual(self.participant1.uid, 'participant:1')
self.assertEqual(get_user('participant:1'), self.participant1)
self.assertEqual(len(Users()), 1)
class DjangoGroupTest(TestCase):
def setUp(self):
self.group1 = Group.objects.create(name='Test Group')
self.djangogroup1 = DjangoGroup.objects.create(group=self.group1)
def test_group_djangogroup(self):
self.assertEqual(self.djangogroup1.group, self.group1)
def test_user_api(self):
self.assertTrue(hasattr(self.djangogroup1, 'uid'))
self.assertEqual(self.djangogroup1.uid, 'djangogroup:1')
self.assertEqual(get_user('djangogroup:1'), self.djangogroup1)
class DjangoUserTest(TestCase):
def setUp(self):
self.user1 = User.objects.create(username='admin')
self.djangouser1 = DjangoUser.objects.get(pk=1)
def test_djangouser_user(self):
self.assertEqual(self.user1.pk, self.djangouser1.pk)
def test_has_no_profile(self):
self.assertTrue(self.djangouser1.has_no_profile())
def test_user_api(self):
self.assertTrue(hasattr(self.djangouser1, 'uid'))
self.assertEqual(self.djangouser1.uid, 'djangouser:1')
self.assertEqual(get_user('djangouser:1'), self.djangouser1)
self.assertEqual(user2djangouser(self.user1), self.djangouser1)

View File

@ -22,15 +22,26 @@ class Users(object):
self.id = id
def __iter__(self):
for receiver, users in receiv_users.send(
sender='users', user_prefix=self.user_prefix, id=self.id):
for user in users:
yield user
try:
return iter(self._cache)
except AttributeError:
return iter(self.iter_users())
def __len__(self):
return len(list(self.__iter__()))
def __getitem__(self, key):
user_list = list(self)
return user_list[key]
def iter_users(self):
self._cache = list()
for receiver, users in receiv_users.send(
sender='users', user_prefix=self.user_prefix, id=self.id):
for user in users:
self._cache.append(user)
yield user
def generate_uid(prefix, id):
if ':' in prefix: