Add tests for managemails.models
This commit adds tests for managemails.models to improve the test coverage of that model. There are some changes to the classes in managemails.models too: - add a method create_mailbox to MailboxManager - properly handle uncommited mailaddresses in MailAddress.set_mailbox and MailAddress.set_forward_addresses
This commit is contained in:
parent
03a7dc0320
commit
1cfd4327da
2 changed files with 305 additions and 3 deletions
|
@ -85,6 +85,23 @@ class MailboxManager(models.Manager):
|
||||||
active=True, osuser=osuser,
|
active=True, osuser=osuser,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def create_mailbox(self, osuser, password=None, commit=True):
|
||||||
|
"""
|
||||||
|
Create a new mailbox for the given operating system user.
|
||||||
|
|
||||||
|
:param osuser: a :py:class:`osuser.models.OsUser` instance
|
||||||
|
:param password: an optional password
|
||||||
|
:param commit: whether the mailbox should be commited to the database
|
||||||
|
:return: mailbox instance
|
||||||
|
:rtype: :py:class:`managemails.models.Mailbox`
|
||||||
|
|
||||||
|
"""
|
||||||
|
mailbox = self.create(
|
||||||
|
osuser=osuser, username=self.get_next_mailbox_name(osuser))
|
||||||
|
if password is not None:
|
||||||
|
mailbox.set_password(password)
|
||||||
|
return mailbox
|
||||||
|
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
@python_2_unicode_compatible
|
||||||
class Mailbox(ActivateAbleMixin, TimeStampedModel):
|
class Mailbox(ActivateAbleMixin, TimeStampedModel):
|
||||||
|
@ -176,9 +193,12 @@ class MailAddress(ActivateAbleMixin, TimeStampedModel, models.Model):
|
||||||
for mafwd in MailAddressForward.objects.filter(mailaddress=self):
|
for mafwd in MailAddressForward.objects.filter(mailaddress=self):
|
||||||
mafwd.delete()
|
mafwd.delete()
|
||||||
else:
|
else:
|
||||||
mabox = MailAddressMailbox(mailaddress=self, mailbox=mailbox)
|
|
||||||
if commit:
|
if commit:
|
||||||
|
self.save()
|
||||||
|
mabox = MailAddressMailbox(mailaddress=self, mailbox=mailbox)
|
||||||
mabox.save()
|
mabox.save()
|
||||||
|
else:
|
||||||
|
mabox = MailAddressMailbox(mailaddress=self, mailbox=mailbox)
|
||||||
return mabox
|
return mabox
|
||||||
|
|
||||||
def set_forward_addresses(self, addresses, commit=True):
|
def set_forward_addresses(self, addresses, commit=True):
|
||||||
|
@ -211,6 +231,8 @@ class MailAddress(ActivateAbleMixin, TimeStampedModel, models.Model):
|
||||||
mafwd.save()
|
mafwd.save()
|
||||||
retval.append(mafwd)
|
retval.append(mafwd)
|
||||||
else:
|
else:
|
||||||
|
if commit:
|
||||||
|
self.save()
|
||||||
for target in addresses:
|
for target in addresses:
|
||||||
mafwd = MailAddressForward(mailaddress=self, target=target)
|
mafwd = MailAddressForward(mailaddress=self, target=target)
|
||||||
if commit:
|
if commit:
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
from django.test import TestCase
|
"""
|
||||||
|
This module contains tests for :py:mod:`managemails.models`
|
||||||
|
"""
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from mock import patch
|
||||||
|
|
||||||
|
from django.test import TestCase, TransactionTestCase
|
||||||
from django.test.utils import override_settings
|
from django.test.utils import override_settings
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
|
|
||||||
|
@ -21,6 +28,7 @@ Customer = get_user_model()
|
||||||
BROKER_BACKEND='memory'
|
BROKER_BACKEND='memory'
|
||||||
)
|
)
|
||||||
class MailboxTest(TestCase):
|
class MailboxTest(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(MailboxTest, self).setUp()
|
super(MailboxTest, self).setUp()
|
||||||
self.customer = Customer.objects.create_user('test')
|
self.customer = Customer.objects.create_user('test')
|
||||||
|
@ -37,9 +45,281 @@ class MailboxTest(TestCase):
|
||||||
mb.set_password('test')
|
mb.set_password('test')
|
||||||
self.assertEqual(str(mb), 'test')
|
self.assertEqual(str(mb), 'test')
|
||||||
|
|
||||||
|
@patch('managemails.models.create_file_mailbox')
|
||||||
|
def test_save(self, create_file_mailbox_task):
|
||||||
|
user = User.objects.create_user(self.customer)
|
||||||
|
mb = Mailbox.objects.create_mailbox(user)
|
||||||
|
self.assertIsNotNone(mb.pk)
|
||||||
|
create_file_mailbox_task.delay.assert_called_with(
|
||||||
|
user.username, mb.username)
|
||||||
|
|
||||||
|
@patch('managemails.models.delete_file_mailbox')
|
||||||
|
def test_delete(self, delete_file_mailbox_task):
|
||||||
|
user = User.objects.create_user(self.customer)
|
||||||
|
mb = Mailbox.objects.create_mailbox(user)
|
||||||
|
mb.delete()
|
||||||
|
self.assertIsNone(mb.pk)
|
||||||
|
delete_file_mailbox_task.delay.assert_called_with(
|
||||||
|
user.username, mb.username)
|
||||||
|
|
||||||
|
def test_get_mailaddresses(self):
|
||||||
|
user = User.objects.create_user(self.customer)
|
||||||
|
mb = Mailbox.objects.create_mailbox(user)
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
address = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
address.set_mailbox(mb)
|
||||||
|
mailaddresses = mb.get_mailaddresses()
|
||||||
|
self.assertEqual(len(mailaddresses), 1)
|
||||||
|
self.assertIn(address, mailaddresses)
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(
|
||||||
|
CELERY_ALWAYS_EAGER=True,
|
||||||
|
CELERY_CACHE_BACKEND='memory',
|
||||||
|
BROKER_BACKEND='memory'
|
||||||
|
)
|
||||||
|
class MailAddressTest(TransactionTestCase):
|
||||||
|
|
||||||
class MailAddressTest(TestCase):
|
|
||||||
def test__str__(self):
|
def test__str__(self):
|
||||||
md = MailDomain.objects.create(domain='example.org')
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
ma = MailAddress.objects.create(localpart='test', domain=md)
|
ma = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
self.assertEqual(str(ma), 'test@example.org')
|
self.assertEqual(str(ma), 'test@example.org')
|
||||||
|
|
||||||
|
def test_set_mailbox_fresh(self):
|
||||||
|
customer = Customer.objects.create_user('test')
|
||||||
|
user = User.objects.create_user(customer)
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
mb = Mailbox.objects.create_mailbox(user)
|
||||||
|
ma.set_mailbox(mb)
|
||||||
|
self.assertIn(ma, mb.get_mailaddresses())
|
||||||
|
|
||||||
|
def test_set_mailbox_reassing(self):
|
||||||
|
customer = Customer.objects.create_user('test')
|
||||||
|
user = User.objects.create_user(customer)
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
mb = Mailbox.objects.create_mailbox(user)
|
||||||
|
ma.set_mailbox(mb)
|
||||||
|
mb2 = Mailbox.objects.create_mailbox(user)
|
||||||
|
ma.set_mailbox(mb2)
|
||||||
|
self.assertIn(ma, mb2.get_mailaddresses())
|
||||||
|
self.assertNotIn(ma, mb.get_mailaddresses())
|
||||||
|
|
||||||
|
def test_set_mailbox_with_forwards(self):
|
||||||
|
customer = Customer.objects.create_user('test')
|
||||||
|
user = User.objects.create_user(customer)
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
mb = Mailbox.objects.create_mailbox(user)
|
||||||
|
ma.set_forward_addresses(['test2@example.org'])
|
||||||
|
ma.set_mailbox(mb)
|
||||||
|
self.assertEqual(ma.mailaddressforward_set.count(), 0)
|
||||||
|
self.assertIn(ma, mb.get_mailaddresses())
|
||||||
|
|
||||||
|
def test_set_mailbox_with_unsaved_address(self):
|
||||||
|
customer = Customer.objects.create_user('test')
|
||||||
|
user = User.objects.create_user(customer)
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress(localpart='test', domain=md)
|
||||||
|
mb = Mailbox.objects.create_mailbox(user)
|
||||||
|
ma.set_mailbox(mb)
|
||||||
|
self.assertIn(ma, mb.get_mailaddresses())
|
||||||
|
|
||||||
|
def test_set_mailbox_fresh_no_commit(self):
|
||||||
|
customer = Customer.objects.create_user('test')
|
||||||
|
user = User.objects.create_user(customer)
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
mb = Mailbox.objects.create_mailbox(user)
|
||||||
|
ma.set_mailbox(mb, commit=False)
|
||||||
|
self.assertNotIn(ma, mb.get_mailaddresses())
|
||||||
|
|
||||||
|
def test_set_mailbox_with_unsaved_address_no_commit(self):
|
||||||
|
customer = Customer.objects.create_user('test')
|
||||||
|
user = User.objects.create_user(customer)
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress(localpart='test', domain=md)
|
||||||
|
mb = Mailbox.objects.create_mailbox(user)
|
||||||
|
ma.set_mailbox(mb, commit=False)
|
||||||
|
self.assertNotIn(ma, mb.get_mailaddresses())
|
||||||
|
|
||||||
|
def test_set_forward_addresses_fresh(self):
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
ma.set_forward_addresses(['test2@example.org'])
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
ma.mailaddressforward_set.all(), ['test2@example.org'],
|
||||||
|
lambda(maf): maf.target)
|
||||||
|
|
||||||
|
def test_set_forward_addresses_unsaved(self):
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress(localpart='test', domain=md)
|
||||||
|
ma.set_forward_addresses(['test2@example.org'])
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
ma.mailaddressforward_set.all(), ['test2@example.org'],
|
||||||
|
lambda(maf): maf.target)
|
||||||
|
|
||||||
|
def test_set_forward_addresses_replace_forwards(self):
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
ma.set_forward_addresses(['test2@example.org'])
|
||||||
|
ma.set_forward_addresses(['test3@example.org'])
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
ma.mailaddressforward_set.all(), ['test3@example.org'],
|
||||||
|
lambda(maf): maf.target)
|
||||||
|
|
||||||
|
def test_set_forward_addresses_add_forwards(self):
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
ma.set_forward_addresses(['test2@example.org'])
|
||||||
|
ma.set_forward_addresses(['test2@example.org', 'test3@example.org'])
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
ma.mailaddressforward_set.all(),
|
||||||
|
['test2@example.org', 'test3@example.org'],
|
||||||
|
lambda(maf): maf.target,
|
||||||
|
ordered=False)
|
||||||
|
|
||||||
|
def test_set_forward_addresses_replace_mailbox(self):
|
||||||
|
customer = Customer.objects.create_user('test')
|
||||||
|
user = User.objects.create_user(customer)
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
mb = Mailbox.objects.create_mailbox(user)
|
||||||
|
ma.set_mailbox(mb)
|
||||||
|
ma.set_forward_addresses(['test2@example.org'])
|
||||||
|
self.assertNotIn(ma, mb.get_mailaddresses())
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
ma.mailaddressforward_set.all(), ['test2@example.org'],
|
||||||
|
lambda(maf): maf.target)
|
||||||
|
|
||||||
|
def test_set_forward_addresses_fresh_no_commit(self):
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
mafwds = ma.set_forward_addresses(['test2@example.org'], commit=False)
|
||||||
|
self.assertEqual(ma.mailaddressforward_set.count(), 0)
|
||||||
|
self.assertEqual(mafwds[0].target, 'test2@example.org')
|
||||||
|
|
||||||
|
def test_set_forward_address_unsaved_no_commit(self):
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress(localpart='test', domain=md)
|
||||||
|
mafwds = ma.set_forward_addresses(['test2@example.org'], commit=False)
|
||||||
|
self.assertEqual(ma.mailaddressforward_set.count(), 0)
|
||||||
|
self.assertEqual(mafwds[0].target, 'test2@example.org')
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(
|
||||||
|
CELERY_ALWAYS_EAGER=True,
|
||||||
|
CELERY_CACHE_BACKEND='memory',
|
||||||
|
BROKER_BACKEND='memory'
|
||||||
|
)
|
||||||
|
class MailboxManagerTest(TransactionTestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(MailboxManagerTest, self).setUp()
|
||||||
|
self.customer = Customer.objects.create_user('test')
|
||||||
|
self.user = User.objects.create_user(self.customer)
|
||||||
|
|
||||||
|
def test_get_next_mailbox_name_fresh(self):
|
||||||
|
mailboxname = Mailbox.objects.get_next_mailbox_name(self.user)
|
||||||
|
self.assertEqual(mailboxname, '{}p01'.format(self.user.username))
|
||||||
|
|
||||||
|
def test_get_next_mailbox_name_second(self):
|
||||||
|
Mailbox.objects.create_mailbox(self.user)
|
||||||
|
mailboxname = Mailbox.objects.get_next_mailbox_name(self.user)
|
||||||
|
self.assertEqual(mailboxname, '{}p02'.format(self.user.username))
|
||||||
|
|
||||||
|
def test_get_next_mailbox_name_gap_detection(self):
|
||||||
|
mailboxes = [
|
||||||
|
Mailbox.objects.create_mailbox(self.user) for i in range(3)
|
||||||
|
]
|
||||||
|
mailboxes[1].delete()
|
||||||
|
mailboxname = Mailbox.objects.get_next_mailbox_name(self.user)
|
||||||
|
self.assertEqual(mailboxname, '{}p02'.format(self.user.username))
|
||||||
|
|
||||||
|
def test_unused_or_own_fresh(self):
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
address = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
mailboxes = Mailbox.objects.unused_or_own(address, self.user)
|
||||||
|
self.assertQuerysetEqual(mailboxes, [])
|
||||||
|
|
||||||
|
def test_unused_or_own_unassigned(self):
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
address = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
mailboxes = [
|
||||||
|
Mailbox.objects.create_mailbox(self.user) for i in range(2)
|
||||||
|
]
|
||||||
|
assignable = Mailbox.objects.unused_or_own(address, self.user)
|
||||||
|
self.assertQuerysetEqual(assignable, [repr(mb) for mb in mailboxes])
|
||||||
|
|
||||||
|
def test_unused_or_own_assigned(self):
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
address = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
mailboxes = [
|
||||||
|
Mailbox.objects.create_mailbox(self.user) for i in range(2)
|
||||||
|
]
|
||||||
|
address.set_mailbox(mailboxes[0])
|
||||||
|
assignable = Mailbox.objects.unused_or_own(address, self.user)
|
||||||
|
self.assertQuerysetEqual(assignable, [repr(mb) for mb in mailboxes])
|
||||||
|
|
||||||
|
def test_unused_or_own_assigned_other(self):
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
address = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
address2 = MailAddress.objects.create(localpart='test2', domain=md)
|
||||||
|
mailboxes = [
|
||||||
|
Mailbox.objects.create_mailbox(self.user) for i in range(2)
|
||||||
|
]
|
||||||
|
address2.set_mailbox(mailboxes[0])
|
||||||
|
assignable = Mailbox.objects.unused_or_own(address, self.user)
|
||||||
|
self.assertQuerysetEqual(assignable, [repr(mailboxes[1])])
|
||||||
|
|
||||||
|
def test_unused_fresh(self):
|
||||||
|
mailboxes = Mailbox.objects.unused(self.user)
|
||||||
|
self.assertQuerysetEqual(mailboxes, [])
|
||||||
|
|
||||||
|
def test_unused_unassigned(self):
|
||||||
|
mailbox = Mailbox.objects.create_mailbox(self.user)
|
||||||
|
mailboxes = Mailbox.objects.unused(self.user)
|
||||||
|
self.assertQuerysetEqual(mailboxes, [repr(mailbox)])
|
||||||
|
|
||||||
|
def test_unused_assigned(self):
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
address = MailAddress.objects.create(localpart='test', domain=md)
|
||||||
|
mailboxes = [
|
||||||
|
Mailbox.objects.create_mailbox(self.user) for i in range(2)
|
||||||
|
]
|
||||||
|
address.set_mailbox(mailboxes[0])
|
||||||
|
assignable = Mailbox.objects.unused(self.user)
|
||||||
|
self.assertQuerysetEqual(assignable, [repr(mailboxes[1])])
|
||||||
|
|
||||||
|
def test_create_mailbox_no_password(self):
|
||||||
|
mailbox = Mailbox.objects.create_mailbox(self.user)
|
||||||
|
self.assertEqual(mailbox.osuser, self.user)
|
||||||
|
self.assertEqual(mailbox.username, '{}p01'.format(self.user.username))
|
||||||
|
self.assertEqual(mailbox.password, '')
|
||||||
|
|
||||||
|
def test_create_mailbox_with_password(self):
|
||||||
|
mailbox = Mailbox.objects.create_mailbox(self.user, 'test')
|
||||||
|
self.assertEqual(mailbox.osuser, self.user)
|
||||||
|
self.assertEqual(mailbox.username, '{}p01'.format(self.user.username))
|
||||||
|
self.assertTrue(sha512_crypt.verify('test', mailbox.password))
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(
|
||||||
|
CELERY_ALWAYS_EAGER=True,
|
||||||
|
CELERY_CACHE_BACKEND='memory',
|
||||||
|
BROKER_BACKEND='memory'
|
||||||
|
)
|
||||||
|
class MailAddressMailboxTest(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(MailAddressMailboxTest, self).setUp()
|
||||||
|
self.customer = Customer.objects.create_user('test')
|
||||||
|
|
||||||
|
def test___str__(self):
|
||||||
|
user = User.objects.create_user(self.customer)
|
||||||
|
md = MailDomain.objects.create(domain='example.org')
|
||||||
|
ma = MailAddress(localpart='test', domain=md)
|
||||||
|
mb = Mailbox.objects.create_mailbox(user)
|
||||||
|
ma.set_mailbox(mb)
|
||||||
|
self.assertEqual(str(ma.mailaddressmailbox), mb.username)
|
||||||
|
|
Loading…
Reference in a new issue