gva/gnuviechadmin/osusers/tests/test_admin.py
Jan Dittberner 79b460c4a6 repair osusers.tests.test_admin
- fix test code
- fix error in set_ldap_user_password stub
2015-02-01 00:08:04 +01:00

144 lines
4.4 KiB
Python

from django import forms
from django.contrib.admin import AdminSite
from django.test import TestCase
from django.test.utils import override_settings
from django.contrib.auth import get_user_model
from mock import Mock
from osusers.models import (
Group,
User,
)
from osusers.admin import (
GroupAdmin,
PASSWORD_MISMATCH_ERROR,
UserAdmin,
UserCreationForm,
)
Customer = get_user_model()
class CustomerTestCase(TestCase):
def setUp(self):
self.customer = Customer.objects.create_user('test')
super(CustomerTestCase, self).setUp()
class UserCreationFormTest(CustomerTestCase):
def test_clean_password2_same(self):
form = UserCreationForm()
form.cleaned_data = {
'customer': self.customer,
'password1': 'secret',
'password2': 'secret'
}
self.assertEqual(form.clean_password2(), 'secret')
def test_clean_password2_empty(self):
form = UserCreationForm()
form.cleaned_data = {}
self.assertIsNone(form.clean_password2())
def test_clean_password2_mismatch(self):
form = UserCreationForm()
form.cleaned_data = {
'customer': self.customer,
'password1': 'secretx',
'password2': 'secrety'
}
with self.assertRaises(forms.ValidationError) as cm:
form.clean_password2()
self.assertEqual(cm.exception.message, PASSWORD_MISMATCH_ERROR)
@override_settings(
CELERY_ALWAYS_EAGER=True,
CELERY_CACHE_BACKEND='memory',
BROKER_BACKEND='memory'
)
def test_save_commit(self):
form = UserCreationForm()
form.cleaned_data = {
'customer': self.customer,
'password1': 'secret',
'password2': 'secret'
}
user = form.save()
self.assertIsNotNone(user)
self.assertEqual(User.objects.get(pk=user.uid), user)
def test_save_m2m_returns_none(self):
form = UserCreationForm()
self.assertIsNone(form.save_m2m())
class UserAdminTest(CustomerTestCase):
def setUp(self):
site = AdminSite()
self.uadmin = UserAdmin(User, site)
super(UserAdminTest, self).setUp()
def test_get_form_without_object(self):
form = self.uadmin.get_form(Mock(name='request'))
self.assertEqual(
form.Meta.fields,
['customer', 'password1', 'password2']
)
@override_settings(
CELERY_ALWAYS_EAGER=True,
CELERY_CACHE_BACKEND='memory',
BROKER_BACKEND='memory'
)
def test_get_form_with_object(self):
user = User.objects.create_user(customer=self.customer)
form = self.uadmin.get_form(Mock(name='request'), user)
self.assertEqual(
form.Meta.fields,
['username', 'group', 'gecos', 'homedir', 'shell', 'customer',
'uid']
)
def test_get_inline_instances_without_object(self):
inlines = self.uadmin.get_inline_instances(Mock(name='request'))
self.assertEqual(len(inlines), 2)
@override_settings(
CELERY_ALWAYS_EAGER=True,
CELERY_CACHE_BACKEND='memory',
BROKER_BACKEND='memory'
)
def test_get_inline_instances_with_object(self):
user = User.objects.create_user(customer=self.customer)
inlines = self.uadmin.get_inline_instances(
Mock(name='request'), user)
self.assertEqual(len(inlines), len(UserAdmin.inlines))
for index in range(len(inlines)):
self.assertIsInstance(inlines[index], UserAdmin.inlines[index])
class GroupAdminTest(TestCase):
def setUp(self):
site = AdminSite()
self.gadmin = GroupAdmin(Group, site)
super(GroupAdminTest, self).setUp()
def test_get_inline_instances_without_object(self):
inlines = self.gadmin.get_inline_instances(Mock(name='request'))
self.assertEqual(inlines, [])
@override_settings(
CELERY_ALWAYS_EAGER=True,
CELERY_CACHE_BACKEND='memory',
BROKER_BACKEND='memory'
)
def test_get_inline_instances_with_object(self):
group = Group.objects.create(gid=1000, groupname='test')
inlines = self.gadmin.get_inline_instances(
Mock(name='request'), group)
self.assertEqual(len(inlines), len(GroupAdmin.inlines))
for index in range(len(inlines)):
self.assertIsInstance(inlines[index], GroupAdmin.inlines[index])