fix tests

This commit is contained in:
Jan Dittberner 2015-02-01 20:12:23 +01:00
parent e87e4ca268
commit e913b1f771
2 changed files with 34 additions and 11 deletions

View file

@ -6,6 +6,7 @@ from django.utils.html import format_html
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.contrib.admin import AdminSite from django.contrib.admin import AdminSite
from django.contrib.auth import get_user_model
from mock import Mock from mock import Mock
@ -24,6 +25,8 @@ from managemails.models import (
Mailbox, Mailbox,
) )
Customer = get_user_model()
class ReadOnlyPasswordHashWidgetTest(TestCase): class ReadOnlyPasswordHashWidgetTest(TestCase):
def test_render(self): def test_render(self):
@ -52,7 +55,13 @@ class ReadOnlyPasswordHashFieldTest(TestCase):
self.assertFalse(field._has_changed('new', 'old')) self.assertFalse(field._has_changed('new', 'old'))
class MailboxCreationFormTest(TestCase): class CustomerTestCase(TestCase):
def setUp(self):
super(CustomerTestCase, self).setUp()
self.customer = Customer.objects.create(username='test')
class MailboxCreationFormTest(CustomerTestCase):
def test_clean_password2_same(self): def test_clean_password2_same(self):
form = MailboxCreationForm() form = MailboxCreationForm()
form.cleaned_data = {'password1': 'secret', 'password2': 'secret'} form.cleaned_data = {'password1': 'secret', 'password2': 'secret'}
@ -76,7 +85,7 @@ class MailboxCreationFormTest(TestCase):
BROKER_BACKEND='memory' BROKER_BACKEND='memory'
) )
def test_save_commit(self): def test_save_commit(self):
user = User.objects.create_user() user = User.objects.create_user(customer=self.customer)
form = MailboxCreationForm(data={ form = MailboxCreationForm(data={
'osuser': user.uid, 'osuser': user.uid,
'password1': 'secret', 'password1': 'secret',
@ -93,7 +102,7 @@ class MailboxCreationFormTest(TestCase):
BROKER_BACKEND='memory' BROKER_BACKEND='memory'
) )
def test_save_no_commit(self): def test_save_no_commit(self):
user = User.objects.create_user() user = User.objects.create_user(customer=self.customer)
form = MailboxCreationForm(data={ form = MailboxCreationForm(data={
'osuser': user.uid, 'osuser': user.uid,
'password1': 'secret', 'password1': 'secret',
@ -105,14 +114,16 @@ class MailboxCreationFormTest(TestCase):
len(Mailbox.objects.filter(osuser=user)), 0) len(Mailbox.objects.filter(osuser=user)), 0)
class MailboxChangeFormTest(TestCase): class MailboxChangeFormTest(CustomerTestCase):
@override_settings( @override_settings(
CELERY_ALWAYS_EAGER=True, CELERY_ALWAYS_EAGER=True,
CELERY_CACHE_BACKEND='memory', CELERY_CACHE_BACKEND='memory',
BROKER_BACKEND='memory' BROKER_BACKEND='memory'
) )
def test_clean_password(self): def test_clean_password(self):
mailbox = Mailbox(username='test', osuser=User.objects.create_user()) mailbox = Mailbox(
username='test',
osuser=User.objects.create_user(customer=self.customer))
mailbox.set_password('test') mailbox.set_password('test')
mailbox.save() mailbox.save()
form = MailboxChangeForm(instance=mailbox, data={'password': 'blub'}) form = MailboxChangeForm(instance=mailbox, data={'password': 'blub'})
@ -133,8 +144,9 @@ class ActivationChangeMixinTest(TestCase):
querysetmock.update.called_with(active=False) querysetmock.update.called_with(active=False)
class MailBoxAdminTest(TestCase): class MailBoxAdminTest(CustomerTestCase):
def setUp(self): def setUp(self):
super(MailBoxAdminTest, self).setUp()
site = AdminSite() site = AdminSite()
self.mbadmin = MailboxAdmin(Mailbox, site) self.mbadmin = MailboxAdmin(Mailbox, site)
@ -149,7 +161,9 @@ class MailBoxAdminTest(TestCase):
BROKER_BACKEND='memory' BROKER_BACKEND='memory'
) )
def test_get_fieldsets_with_object(self): def test_get_fieldsets_with_object(self):
mailbox = Mailbox(username='test', osuser=User.objects.create_user()) mailbox = Mailbox(
username='test',
osuser=User.objects.create_user(customer=self.customer))
mailbox.set_password('test') mailbox.set_password('test')
mailbox.save() mailbox.save()
self.assertEqual( self.assertEqual(
@ -169,13 +183,15 @@ class MailBoxAdminTest(TestCase):
BROKER_BACKEND='memory' BROKER_BACKEND='memory'
) )
def test_get_form_with_object(self): def test_get_form_with_object(self):
mailbox = Mailbox(username='test', osuser=User.objects.create_user()) mailbox = Mailbox(
username='test',
osuser=User.objects.create_user(customer=self.customer))
mailbox.set_password('test') mailbox.set_password('test')
mailbox.save() mailbox.save()
form = self.mbadmin.get_form(Mock, mailbox) form = self.mbadmin.get_form(Mock, mailbox)
self.assertEqual( self.assertEqual(
form.Meta.fields, form.Meta.fields,
['username', 'password', 'osuser', 'active'] ['osuser', 'username', 'password', 'active']
) )
def test_admin_for_mailbox(self): def test_admin_for_mailbox(self):

View file

@ -1,5 +1,6 @@
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings from django.test.utils import override_settings
from django.contrib.auth import get_user_model
from passlib.hash import sha512_crypt from passlib.hash import sha512_crypt
@ -11,6 +12,8 @@ from managemails.models import (
Mailbox, Mailbox,
) )
Customer = get_user_model()
@override_settings( @override_settings(
CELERY_ALWAYS_EAGER=True, CELERY_ALWAYS_EAGER=True,
@ -18,14 +21,18 @@ from managemails.models import (
BROKER_BACKEND='memory' BROKER_BACKEND='memory'
) )
class MailboxTest(TestCase): class MailboxTest(TestCase):
def setUp(self):
super(MailboxTest, self).setUp()
self.customer = Customer.objects.create_user('test')
def test_set_password(self): def test_set_password(self):
user = User.objects.create_user() user = User.objects.create_user(self.customer)
mb = Mailbox.objects.create(username='test', osuser=user) mb = Mailbox.objects.create(username='test', osuser=user)
mb.set_password('test') mb.set_password('test')
self.assertTrue(sha512_crypt.verify('test', mb.password)) self.assertTrue(sha512_crypt.verify('test', mb.password))
def test___str__(self): def test___str__(self):
user = User.objects.create_user() user = User.objects.create_user(self.customer)
mb = Mailbox.objects.create(username='test', osuser=user) mb = Mailbox.objects.create(username='test', osuser=user)
mb.set_password('test') mb.set_password('test')
self.assertEqual(str(mb), 'test') self.assertEqual(str(mb), 'test')