Merge branch 'feature/managemails_tests'

This commit is contained in:
Jan Dittberner 2014-06-01 18:33:59 +02:00
commit e56fdf5e80
4 changed files with 204 additions and 3 deletions

View file

@ -1,6 +1,7 @@
Changelog
=========
* :feature:`-` full test suite for managemails app
* :feature:`-` full test suite for domains app
* :feature:`-` full test suite for osusers.models
* :feature:`-` `Celery <http://www.celeryproject.com/>`_ integration for ldap

View file

@ -0,0 +1,189 @@
from django import forms
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.utils import override_settings
from django.utils.html import format_html
from django.utils.translation import ugettext as _
from django.contrib.admin import AdminSite
from mock import Mock
from osusers.models import User
from managemails.admin import (
ActivationChangeMixin,
MailboxAdmin,
MailboxChangeForm,
MailboxCreationForm,
PASSWORD_MISMATCH_ERROR,
ReadOnlyPasswordHashField,
ReadOnlyPasswordHashWidget,
)
from managemails.models import (
Mailbox,
)
class TestReadOnlyPasswordHashWidget(TestCase):
def test_render(self):
widget = ReadOnlyPasswordHashWidget()
rendered = widget.render('password', 'secret', {'class': 'test'})
self.assertEqual(
rendered,
format_html(
'<div class="test">{0}</div>',
format_html('<strong>{0}</strong>: secret ',
_('Hash'))
))
class TestReadOnlyPasswordHashField(TestCase):
def test___init__(self):
field = ReadOnlyPasswordHashField()
self.assertFalse(field.required)
def test_bound_data(self):
field = ReadOnlyPasswordHashField()
self.assertEqual(field.bound_data('new', 'old'), 'old')
def test__has_changed(self):
field = ReadOnlyPasswordHashField()
self.assertFalse(field._has_changed('new', 'old'))
class TestMailboxCreationForm(TestCase):
def test_clean_password2_same(self):
form = MailboxCreationForm()
form.cleaned_data = {'password1': 'secret', 'password2': 'secret'}
self.assertEqual(form.clean_password2(), 'secret')
def test_clean_password2_empty(self):
form = MailboxCreationForm()
form.cleaned_data = {}
self.assertIsNone(form.clean_password2())
def test_clean_password2_mismatch(self):
form = MailboxCreationForm()
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):
user = User.objects.create_user()
form = MailboxCreationForm(data={
'osuser': user.uid,
'password1': 'secret',
'password2': 'secret',
})
mailbox = form.save()
self.assertIsNotNone(mailbox)
self.assertEqual(
len(Mailbox.objects.filter(osuser=user)), 1)
@override_settings(
CELERY_ALWAYS_EAGER=True,
CELERY_CACHE_BACKEND='memory',
BROKER_BACKEND='memory'
)
def test_save_no_commit(self):
user = User.objects.create_user()
form = MailboxCreationForm(data={
'osuser': user.uid,
'password1': 'secret',
'password2': 'secret',
})
mailbox = form.save(commit=False)
self.assertIsNotNone(mailbox)
self.assertEqual(
len(Mailbox.objects.filter(osuser=user)), 0)
class TestMailboxChangeForm(TestCase):
@override_settings(
CELERY_ALWAYS_EAGER=True,
CELERY_CACHE_BACKEND='memory',
BROKER_BACKEND='memory'
)
def test_clean_password(self):
mailbox = Mailbox(username='test', osuser=User.objects.create_user())
mailbox.set_password('test')
mailbox.save()
form = MailboxChangeForm(instance=mailbox, data={'password': 'blub'})
self.assertEqual(form.clean_password(), mailbox.password)
class TestActivationChangeMixin(TestCase):
def test_activate(self):
querysetmock = Mock()
activationchange = ActivationChangeMixin()
activationchange.activate(Mock(), querysetmock)
querysetmock.update.called_with(active=True)
def test_deactivate(self):
querysetmock = Mock()
activationchange = ActivationChangeMixin()
activationchange.deactivate(Mock(), querysetmock)
querysetmock.update.called_with(active=False)
class TestMailBoxAdmin(TestCase):
def setUp(self):
site = AdminSite()
self.mbadmin = MailboxAdmin(Mailbox, site)
def test_get_fieldsets_without_object(self):
self.assertEqual(
self.mbadmin.get_fieldsets(Mock()),
self.mbadmin.add_fieldsets)
@override_settings(
CELERY_ALWAYS_EAGER=True,
CELERY_CACHE_BACKEND='memory',
BROKER_BACKEND='memory'
)
def test_get_fieldsets_with_object(self):
mailbox = Mailbox(username='test', osuser=User.objects.create_user())
mailbox.set_password('test')
mailbox.save()
self.assertEqual(
self.mbadmin.get_fieldsets(Mock(), mailbox),
self.mbadmin.fieldsets)
def test_get_form_without_object(self):
form = self.mbadmin.get_form(Mock)
self.assertEqual(
form.Meta.fields,
['username', 'password1', 'password2']
)
@override_settings(
CELERY_ALWAYS_EAGER=True,
CELERY_CACHE_BACKEND='memory',
BROKER_BACKEND='memory'
)
def test_get_form_with_object(self):
mailbox = Mailbox(username='test', osuser=User.objects.create_user())
mailbox.set_password('test')
mailbox.save()
form = self.mbadmin.get_form(Mock, mailbox)
self.assertEqual(
form.Meta.fields,
['username', 'password', 'osuser', 'active']
)
def test_admin_for_mailbox(self):
admin_url = reverse('admin:managemails_mailaddress_changelist')
self.assertIsNotNone(admin_url)
class TestMailAddressAdmin(TestCase):
def test_admin_for_mailaddress(self):
admin_url = reverse('admin:managemails_mailaddress_changelist')
self.assertIsNotNone(admin_url)

View file

@ -1,14 +1,22 @@
from django.test import TestCase
from django.test.utils import override_settings
from passlib.hash import sha512_crypt
from domains.models import MailDomain
from osusers.models import User
from managemails.models import (
MailAddress,
Mailbox,
)
@override_settings(
CELERY_ALWAYS_EAGER=True,
CELERY_CACHE_BACKEND='memory',
BROKER_BACKEND='memory'
)
class MailboxTest(TestCase):
def test_set_password(self):
user = User.objects.create_user()
@ -16,6 +24,12 @@ class MailboxTest(TestCase):
mb.set_password('test')
self.assertTrue(sha512_crypt.verify('test', mb.password))
def test___str__(self):
user = User.objects.create_user()
mb = Mailbox.objects.create(username='test', osuser=user)
mb.set_password('test')
self.assertEqual(str(mb), 'test')
class MailAddressTest(TestCase):
def test__str__(self):

View file

@ -1,3 +0,0 @@
from django.shortcuts import render
# Create your views here.