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:
Jan Dittberner 2015-11-28 14:07:34 +00:00
parent 03a7dc0320
commit 1cfd4327da
2 changed files with 305 additions and 3 deletions

View file

@ -85,6 +85,23 @@ class MailboxManager(models.Manager):
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
class Mailbox(ActivateAbleMixin, TimeStampedModel):
@ -176,9 +193,12 @@ class MailAddress(ActivateAbleMixin, TimeStampedModel, models.Model):
for mafwd in MailAddressForward.objects.filter(mailaddress=self):
mafwd.delete()
else:
mabox = MailAddressMailbox(mailaddress=self, mailbox=mailbox)
if commit:
self.save()
mabox = MailAddressMailbox(mailaddress=self, mailbox=mailbox)
mabox.save()
else:
mabox = MailAddressMailbox(mailaddress=self, mailbox=mailbox)
return mabox
def set_forward_addresses(self, addresses, commit=True):
@ -211,6 +231,8 @@ class MailAddress(ActivateAbleMixin, TimeStampedModel, models.Model):
mafwd.save()
retval.append(mafwd)
else:
if commit:
self.save()
for target in addresses:
mafwd = MailAddressForward(mailaddress=self, target=target)
if commit: