2014-06-01 22:20:28 +02:00
|
|
|
from django import forms
|
2014-06-01 22:01:04 +02:00
|
|
|
from django.contrib.admin import AdminSite
|
2014-06-01 22:20:28 +02:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.utils import override_settings
|
2014-06-01 22:01:04 +02:00
|
|
|
|
|
|
|
from mock import patch, Mock
|
|
|
|
|
|
|
|
from osusers.models import (
|
2014-06-01 23:05:58 +02:00
|
|
|
DeleteTaskResult,
|
2014-06-01 22:53:38 +02:00
|
|
|
Group,
|
2014-06-01 22:01:04 +02:00
|
|
|
User,
|
|
|
|
)
|
|
|
|
from osusers.admin import (
|
2014-06-01 23:05:58 +02:00
|
|
|
DeleteTaskResultAdmin,
|
2014-06-01 22:53:38 +02:00
|
|
|
GroupAdmin,
|
2014-06-01 22:20:28 +02:00
|
|
|
PASSWORD_MISMATCH_ERROR,
|
2014-06-01 22:37:43 +02:00
|
|
|
UserAdmin,
|
2014-06-01 22:20:28 +02:00
|
|
|
UserCreationForm,
|
2014-06-01 22:01:04 +02:00
|
|
|
UserTaskResultInline,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TaskResultInlineTest(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.site = AdminSite()
|
|
|
|
super(TaskResultInlineTest, self).setUp()
|
|
|
|
|
|
|
|
def test_get_queryset_calls_update_taskstatus(self):
|
|
|
|
with patch('osusers.admin.admin.TabularInline.get_queryset') as mock:
|
|
|
|
entrymock = Mock(name='entry')
|
|
|
|
mock.return_value = [entrymock]
|
|
|
|
requestmock = Mock(name='request')
|
|
|
|
UserTaskResultInline(User, self.site).get_queryset(requestmock)
|
2014-06-01 23:05:28 +02:00
|
|
|
entrymock.update_taskstatus.assert_calledwith()
|
2014-06-01 22:01:04 +02:00
|
|
|
|
|
|
|
def test_has_add_permissions_returns_false(self):
|
|
|
|
self.assertFalse(
|
|
|
|
UserTaskResultInline(User, self.site).has_add_permission(
|
|
|
|
self, Mock(name='request'))
|
|
|
|
)
|
2014-06-01 22:20:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
class UserCreationFormTest(TestCase):
|
|
|
|
def test_clean_password2_same(self):
|
|
|
|
form = UserCreationForm()
|
|
|
|
form.cleaned_data = {'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 = {'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 = {'password1': 'secret', 'password2': 'secret'}
|
|
|
|
user = form.save()
|
|
|
|
self.assertIsNotNone(user)
|
|
|
|
self.assertEqual(User.objects.get(pk=user.uid), user)
|
2014-06-01 22:37:43 +02:00
|
|
|
|
2014-06-01 23:10:26 +02:00
|
|
|
def test_save_m2m_returns_none(self):
|
|
|
|
form = UserCreationForm()
|
|
|
|
self.assertIsNone(form.save_m2m())
|
|
|
|
|
2014-06-01 22:37:43 +02:00
|
|
|
|
|
|
|
class UserAdminTest(TestCase):
|
|
|
|
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,
|
|
|
|
['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()
|
|
|
|
form = self.uadmin.get_form(Mock(name='request'), user)
|
|
|
|
self.assertEqual(
|
|
|
|
form.Meta.fields,
|
|
|
|
['username', 'group', 'gecos', 'homedir', 'shell', 'uid']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_get_inline_instances_without_object(self):
|
|
|
|
inlines = self.uadmin.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):
|
|
|
|
user = User.objects.create_user()
|
|
|
|
inlines = self.uadmin.get_inline_instances(
|
|
|
|
Mock(name='request'), user)
|
|
|
|
self.assertEqual(len(inlines), len(UserAdmin.inlines))
|
2014-06-01 22:53:29 +02:00
|
|
|
for index in range(len(inlines)):
|
|
|
|
self.assertIsInstance(inlines[index], UserAdmin.inlines[index])
|
2014-06-01 22:53:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
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])
|
2014-06-01 23:05:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteTaskResultAdminTest(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
site = AdminSite()
|
|
|
|
self.dtradmin = DeleteTaskResultAdmin(DeleteTaskResult, site)
|
|
|
|
super(DeleteTaskResultAdminTest, self).setUp()
|
|
|
|
|
|
|
|
def test_has_add_permission_returns_false_without_object(self):
|
|
|
|
self.assertFalse(
|
|
|
|
self.dtradmin.has_add_permission(Mock(name='request')))
|
|
|
|
|
|
|
|
def test_has_add_permission_returns_false_with_object(self):
|
|
|
|
self.assertFalse(
|
|
|
|
self.dtradmin.has_add_permission(Mock(name='request'),
|
|
|
|
Mock(name='test')))
|
|
|
|
|
|
|
|
def test_get_queryset_calls_update_taskstatus(self):
|
|
|
|
with patch('osusers.admin.admin.ModelAdmin.get_queryset') as mock:
|
|
|
|
entrymock = Mock(name='entry')
|
|
|
|
mock.return_value = [entrymock]
|
|
|
|
requestmock = Mock(name='request')
|
|
|
|
self.dtradmin.get_queryset(requestmock)
|
|
|
|
entrymock.update_taskstatus.assert_calledwith()
|