2015-11-22 19:41:45 +01:00
|
|
|
"""
|
|
|
|
This module provides tests for :py:mod:`managemails.forms`.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
from unittest import skip
|
|
|
|
from unittest.mock import ANY, MagicMock, Mock, patch
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
from django.forms import ValidationError
|
|
|
|
from django.test import TestCase
|
2019-01-30 21:27:25 +01:00
|
|
|
from django.urls import reverse
|
2015-11-22 19:41:45 +01:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
import osusers.models
|
|
|
|
from domains.models import MailDomain
|
2015-11-22 19:41:45 +01:00
|
|
|
from managemails.forms import (
|
2023-02-18 22:46:48 +01:00
|
|
|
MAILBOX_OR_FORWARDS,
|
2015-11-22 19:41:45 +01:00
|
|
|
AddMailAddressForm,
|
|
|
|
ChangeMailboxPasswordForm,
|
|
|
|
CreateMailboxForm,
|
|
|
|
EditMailAddressForm,
|
|
|
|
MailAddressFieldMixin,
|
|
|
|
multiple_email_validator,
|
|
|
|
)
|
2023-02-18 22:46:48 +01:00
|
|
|
from managemails.models import MailAddress, Mailbox
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CreateMailboxFormTest(TestCase):
|
|
|
|
def test_constructor_needs_hostingpackage(self):
|
|
|
|
instance = MagicMock()
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
CreateMailboxForm(instance)
|
|
|
|
|
|
|
|
def test_constructor(self):
|
|
|
|
hostingpackage = Mock(id=42)
|
|
|
|
instance = MagicMock()
|
|
|
|
form = CreateMailboxForm(instance, hostingpackage=hostingpackage)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "hosting_package"))
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertEqual(form.hosting_package, hostingpackage)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "helper"))
|
|
|
|
self.assertEqual(
|
|
|
|
form.helper.form_action, reverse("create_mailbox", kwargs={"package": 42})
|
|
|
|
)
|
|
|
|
self.assertIn("password1", form.fields)
|
|
|
|
self.assertIn("password2", form.fields)
|
|
|
|
self.assertEqual(form.helper.inputs[0].name, "submit")
|
|
|
|
|
|
|
|
@patch("managemails.forms.Mailbox.objects")
|
2015-11-22 19:41:45 +01:00
|
|
|
def test_save(self, mailbox_objects):
|
|
|
|
osuser = MagicMock()
|
|
|
|
hostingpackage = Mock(id=42, osuser=osuser)
|
|
|
|
instance = MagicMock()
|
|
|
|
form = CreateMailboxForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
|
|
|
hostingpackage=hostingpackage,
|
|
|
|
data={"password1": "secret", "password2": "secret"},
|
|
|
|
)
|
|
|
|
mailbox_objects.get_next_mailbox_name.return_value = "mailbox23"
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
form.save(commit=False)
|
|
|
|
self.assertEqual(osuser, form.instance.osuser)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual("mailbox23", form.instance.username)
|
|
|
|
instance.set_password.assert_called_with("secret")
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ChangeMailboxPasswordFormTest(TestCase):
|
|
|
|
def test_constructor_needs_hostingpackage(self):
|
|
|
|
instance = MagicMock()
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
ChangeMailboxPasswordForm(instance)
|
|
|
|
|
|
|
|
def test_constructor(self):
|
|
|
|
hostingpackage = Mock(id=42)
|
2019-01-30 21:27:25 +01:00
|
|
|
instance = MagicMock(username="testuser")
|
2015-11-22 19:41:45 +01:00
|
|
|
form = ChangeMailboxPasswordForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance, hostingpackage=hostingpackage
|
|
|
|
)
|
|
|
|
self.assertTrue(hasattr(form, "hosting_package"))
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertEqual(form.hosting_package, hostingpackage)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "helper"))
|
|
|
|
self.assertEqual(
|
|
|
|
form.helper.form_action,
|
|
|
|
reverse(
|
|
|
|
"change_mailbox_password", kwargs={"package": 42, "slug": "testuser"}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertIn("password1", form.fields)
|
|
|
|
self.assertIn("password2", form.fields)
|
|
|
|
self.assertEqual(form.helper.inputs[0].name, "submit")
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_save(self):
|
|
|
|
hostingpackage = Mock(id=42)
|
2019-01-30 21:27:25 +01:00
|
|
|
instance = MagicMock(username="testuser")
|
2015-11-22 19:41:45 +01:00
|
|
|
form = ChangeMailboxPasswordForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
|
|
|
hostingpackage=hostingpackage,
|
|
|
|
data={"password1": "newsecret", "password2": "newsecret"},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
form.save(commit=False)
|
2019-01-30 21:27:25 +01:00
|
|
|
instance.set_password.assert_called_with("newsecret")
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MultipleEmailValidatorTest(TestCase):
|
|
|
|
def test_valid_single_address(self):
|
|
|
|
self.assertEqual(
|
2019-01-30 21:27:25 +01:00
|
|
|
"test@example.org", multiple_email_validator("test@example.org")
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_valid_multiple_addresses(self):
|
|
|
|
self.assertEqual(
|
2019-01-30 21:27:25 +01:00
|
|
|
"test1@example.org,test2@example.org",
|
|
|
|
multiple_email_validator("test1@example.org,test2@example.org"),
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_empty(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual("", multiple_email_validator(""))
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_none(self):
|
|
|
|
self.assertIsNone(multiple_email_validator(None))
|
|
|
|
|
|
|
|
def test_invalid_single_address(self):
|
|
|
|
with self.assertRaises(ValidationError):
|
2019-01-30 21:27:25 +01:00
|
|
|
multiple_email_validator("no@ddress")
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_invalid_multiple_addresses(self):
|
|
|
|
with self.assertRaises(ValidationError):
|
2019-01-30 21:27:25 +01:00
|
|
|
multiple_email_validator("test1@example.org,no@ddress")
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MailAddressFieldMixinTest(TestCase):
|
|
|
|
def test_fields_defined(self):
|
|
|
|
form = MailAddressFieldMixin()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("mailbox_or_forwards", form.fields)
|
|
|
|
self.assertIn("mailbox", form.fields)
|
|
|
|
self.assertIn("forwards", form.fields)
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AddMailAddressFormTest(TestCase):
|
|
|
|
def test_constructor_needs_hostingpackage(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
instance = MailAddress()
|
2015-11-22 19:41:45 +01:00
|
|
|
with self.assertRaises(KeyError):
|
2023-02-18 22:46:48 +01:00
|
|
|
AddMailAddressForm(instance=instance, maildomain=None)
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_constructor_needs_maildomain(self):
|
|
|
|
instance = MagicMock()
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
AddMailAddressForm(instance=instance, hostingpackage=MagicMock())
|
|
|
|
|
|
|
|
def test_constructor(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
instance = MailAddress()
|
|
|
|
os_user = osusers.models.User(username="testuser")
|
|
|
|
hosting_package = MagicMock(id=42, osuser=os_user)
|
|
|
|
mail_domain = MailDomain(domain="example.org")
|
2015-11-22 19:41:45 +01:00
|
|
|
form = AddMailAddressForm(
|
2023-02-18 22:46:48 +01:00
|
|
|
instance=instance, hostingpackage=hosting_package, maildomain=mail_domain
|
2019-01-30 21:27:25 +01:00
|
|
|
)
|
|
|
|
self.assertIn("mailbox_or_forwards", form.fields)
|
|
|
|
self.assertIn("mailbox", form.fields)
|
|
|
|
self.assertIn("forwards", form.fields)
|
|
|
|
self.assertTrue(hasattr(form, "hosting_package"))
|
2023-02-18 22:46:48 +01:00
|
|
|
self.assertEqual(form.hosting_package, hosting_package)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "maildomain"))
|
2023-02-18 22:46:48 +01:00
|
|
|
self.assertEqual(form.maildomain, mail_domain)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "helper"))
|
|
|
|
self.assertEqual(
|
|
|
|
form.helper.form_action,
|
|
|
|
reverse("add_mailaddress", kwargs={"package": 42, "domain": "example.org"}),
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertEqual(len(form.helper.layout), 2)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(form.helper.layout[1].name, "submit")
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_clean_localpart_valid(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
mail_domain = MailDomain.objects.create(domain="example.org")
|
|
|
|
|
|
|
|
instance = MailAddress()
|
|
|
|
os_user = osusers.models.User(username="testuser")
|
|
|
|
hosting_package = MagicMock(id=42, osuser=os_user)
|
2015-11-22 19:41:45 +01:00
|
|
|
form = AddMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
2023-02-18 22:46:48 +01:00
|
|
|
hostingpackage=hosting_package,
|
|
|
|
maildomain=mail_domain,
|
2015-11-22 19:41:45 +01:00
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"localpart": "test",
|
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
|
|
|
|
"forwards": "test2@example.org",
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual("test", form.clean_localpart())
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_clean_localpart_duplicate(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
mail_domain = MailDomain.objects.create(domain="example.org")
|
|
|
|
|
|
|
|
MailAddress.objects.create(localpart="test", domain=mail_domain)
|
|
|
|
|
|
|
|
instance = MailAddress()
|
|
|
|
osuser = osusers.models.User(username="testuser")
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage = MagicMock(id=42, osuser=osuser)
|
|
|
|
form = AddMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
|
|
|
hostingpackage=hostingpackage,
|
2023-02-18 22:46:48 +01:00
|
|
|
maildomain=mail_domain,
|
2015-11-22 19:41:45 +01:00
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"localpart": "test",
|
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
|
|
|
|
"forwards": "test2@example.org",
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertFalse(form.is_valid())
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("localpart", form.errors)
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_clean_no_mailbox_choice(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
instance = MailAddress()
|
|
|
|
osuser = osusers.models.User(username="testuser")
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage = MagicMock(id=42, osuser=osuser)
|
2023-02-18 22:46:48 +01:00
|
|
|
maildomain = MailDomain(domain="example.org")
|
2015-11-22 19:41:45 +01:00
|
|
|
form = AddMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
|
|
|
hostingpackage=hostingpackage,
|
2015-11-22 19:41:45 +01:00
|
|
|
maildomain=maildomain,
|
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"localpart": "test",
|
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertFalse(form.is_valid())
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("mailbox", form.errors)
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_clean_no_forward_address_choice(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
instance = MailAddress()
|
|
|
|
os_user = osusers.models.User(username="testuser")
|
|
|
|
hosting_package = MagicMock(id=42, osuser=os_user)
|
|
|
|
mail_domain = MailDomain(domain="example.org")
|
2015-11-22 19:41:45 +01:00
|
|
|
form = AddMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
2023-02-18 22:46:48 +01:00
|
|
|
hostingpackage=hosting_package,
|
|
|
|
maildomain=mail_domain,
|
2015-11-22 19:41:45 +01:00
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"localpart": "test",
|
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertFalse(form.is_valid())
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("forwards", form.errors)
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_save_with_forwards_no_commit(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
mail_domain = MailDomain.objects.create(domain="example.org")
|
|
|
|
|
|
|
|
instance = MailAddress()
|
|
|
|
os_user = osusers.models.User(username="testuser")
|
|
|
|
hosting_package = MagicMock(id=42, osuser=os_user)
|
2015-11-22 19:41:45 +01:00
|
|
|
form = AddMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
2023-02-18 22:46:48 +01:00
|
|
|
hostingpackage=hosting_package,
|
|
|
|
maildomain=mail_domain,
|
2015-11-22 19:41:45 +01:00
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"localpart": "test",
|
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
|
|
|
|
"forwards": "test2@example.org,test3@example.org",
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
form.save(commit=False)
|
2023-02-18 22:46:48 +01:00
|
|
|
self.assertEqual(mail_domain, instance.domain)
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_save_with_forwards_commit(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
maildomain = MailDomain.objects.create(domain="example.org")
|
|
|
|
|
|
|
|
instance = MailAddress()
|
|
|
|
osuser = osusers.models.User(username="testuser")
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage = MagicMock(id=42, osuser=osuser)
|
|
|
|
form = AddMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
|
|
|
hostingpackage=hostingpackage,
|
2015-11-22 19:41:45 +01:00
|
|
|
maildomain=maildomain,
|
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"localpart": "test",
|
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
|
|
|
|
"forwards": "test2@example.org,test3@example.org",
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
form.save(commit=True)
|
|
|
|
self.assertEqual(maildomain, instance.domain)
|
2023-02-18 22:46:48 +01:00
|
|
|
forwards = list(
|
|
|
|
instance.mailaddressforward_set.values_list("target", flat=True).order_by(
|
|
|
|
"target"
|
|
|
|
)
|
2019-01-30 21:27:25 +01:00
|
|
|
)
|
2023-02-18 22:46:48 +01:00
|
|
|
self.assertEqual(len(forwards), 2)
|
|
|
|
self.assertEqual(forwards, ["test2@example.org", "test3@example.org"])
|
2015-11-22 19:41:45 +01:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
@skip("does not work because it will create a real mailbox")
|
2015-11-22 19:41:45 +01:00
|
|
|
def test_save_with_mailbox_no_commit(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
instance = MailAddress()
|
|
|
|
|
|
|
|
os_user = osusers.models.User(username="testuser")
|
|
|
|
hosting_package = MagicMock(id=42, osuser=os_user)
|
|
|
|
|
|
|
|
mail_domain = MailDomain.objects.create(domain="example.org")
|
|
|
|
mail_box = Mailbox.objects.create(osuser=os_user, username="mailbox23")
|
|
|
|
mail_box.set_password("test")
|
|
|
|
|
2015-11-22 19:41:45 +01:00
|
|
|
form = AddMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
2023-02-18 22:46:48 +01:00
|
|
|
hostingpackage=hosting_package,
|
|
|
|
maildomain=mail_domain,
|
2015-11-22 19:41:45 +01:00
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"localpart": "test",
|
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
|
|
|
|
"mailbox": "mailbox23",
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
form.save(commit=False)
|
2023-02-18 22:46:48 +01:00
|
|
|
self.assertEqual(mail_domain, instance.domain)
|
2015-11-22 19:41:45 +01:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
@skip("does not work because it will create a real mailbox")
|
2015-11-22 19:41:45 +01:00
|
|
|
def test_save_with_mailbox_commit(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
mail_domain = MailDomain.objects.create(domain="example.org")
|
|
|
|
|
|
|
|
instance = MailAddress()
|
|
|
|
os_user = osusers.models.User(username="testuser")
|
|
|
|
|
|
|
|
mail_box = Mailbox.objects.create(osuser=os_user, username="mailbox23")
|
|
|
|
mail_box.set_password("test")
|
|
|
|
|
|
|
|
hosting_package = MagicMock(id=42, osuser=os_user)
|
|
|
|
|
2015-11-22 19:41:45 +01:00
|
|
|
form = AddMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
2023-02-18 22:46:48 +01:00
|
|
|
hostingpackage=hosting_package,
|
|
|
|
maildomain=mail_domain,
|
2015-11-22 19:41:45 +01:00
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"localpart": "test",
|
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
|
|
|
|
"mailbox": "mailbox23",
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
form.save(commit=True)
|
2023-02-18 22:46:48 +01:00
|
|
|
self.assertEqual(mail_domain, instance.domain)
|
2015-11-22 19:41:45 +01:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
@skip("does not work because it will create a real mailbox")
|
2015-11-22 19:41:45 +01:00
|
|
|
def test_save_with_other_choice(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
mail_domain = MailDomain.objects.create(domain="example.org")
|
|
|
|
|
|
|
|
instance = MailAddress()
|
|
|
|
os_user = osusers.models.User(username="testuser")
|
|
|
|
hosting_package = MagicMock(id=42, osuser=os_user)
|
|
|
|
|
|
|
|
mail_box = Mailbox.objects.create(osuser=os_user, username="mailbox23")
|
|
|
|
mail_box.set_password("test")
|
|
|
|
|
2015-11-22 19:41:45 +01:00
|
|
|
form = AddMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
2023-02-18 22:46:48 +01:00
|
|
|
hostingpackage=hosting_package,
|
|
|
|
maildomain=mail_domain,
|
2015-11-22 19:41:45 +01:00
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"localpart": "test",
|
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
|
|
|
|
"mailbox": "mailbox23",
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
2019-01-30 21:27:25 +01:00
|
|
|
form.cleaned_data["mailbox_or_forwards"] = -1
|
2015-11-22 19:41:45 +01:00
|
|
|
form.save(commit=True)
|
|
|
|
|
|
|
|
|
|
|
|
class EditMailAddressFormTest(TestCase):
|
|
|
|
def test_constructor_needs_hostingpackage(self):
|
|
|
|
instance = MagicMock()
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
EditMailAddressForm(instance=instance, maildomain=MagicMock())
|
|
|
|
|
|
|
|
def test_constructor_needs_maildomain(self):
|
|
|
|
instance = MagicMock()
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
EditMailAddressForm(instance=instance, hostingpackage=MagicMock())
|
|
|
|
|
|
|
|
def test_constructor(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
instance = MailAddress(id=23)
|
|
|
|
osuser = osusers.models.User(username="testuser")
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage = MagicMock(id=42, osuser=osuser)
|
2023-02-18 22:46:48 +01:00
|
|
|
maildomain = MailDomain.objects.create(domain="example.org")
|
2015-11-22 19:41:45 +01:00
|
|
|
form = EditMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance, maildomain=maildomain, hostingpackage=hostingpackage
|
|
|
|
)
|
|
|
|
self.assertIn("mailbox_or_forwards", form.fields)
|
|
|
|
self.assertIn("mailbox", form.fields)
|
|
|
|
self.assertIn("forwards", form.fields)
|
|
|
|
self.assertTrue(hasattr(form, "hosting_package"))
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertEqual(form.hosting_package, hostingpackage)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "maildomain"))
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertEqual(form.maildomain, maildomain)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "helper"))
|
|
|
|
self.assertEqual(
|
|
|
|
form.helper.form_action,
|
|
|
|
reverse(
|
|
|
|
"edit_mailaddress",
|
|
|
|
kwargs={"package": 42, "domain": "example.org", "pk": 23},
|
|
|
|
),
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertEqual(len(form.helper.layout), 2)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(form.helper.layout[1].name, "submit")
|
2015-11-22 19:41:45 +01:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
@skip("needs mailbox refactoring")
|
2015-11-22 19:41:45 +01:00
|
|
|
def test_clean_no_mailbox_choice(self):
|
|
|
|
instance = MagicMock(id=23)
|
2019-01-30 21:27:25 +01:00
|
|
|
osuser = Mock(username="testuser")
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage = MagicMock(id=42, osuser=osuser)
|
2019-01-30 21:27:25 +01:00
|
|
|
maildomain = MagicMock(domain="example.org")
|
2015-11-22 19:41:45 +01:00
|
|
|
form = EditMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
|
|
|
maildomain=maildomain,
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage=hostingpackage,
|
2019-01-30 21:27:25 +01:00
|
|
|
data={"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertFalse(form.is_valid())
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("mailbox", form.errors)
|
2015-11-22 19:41:45 +01:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
@skip("needs mailbox refactoring")
|
2015-11-22 19:41:45 +01:00
|
|
|
def test_clean_no_forward_address_choice(self):
|
|
|
|
instance = MagicMock(id=23)
|
2019-01-30 21:27:25 +01:00
|
|
|
osuser = Mock(username="testuser")
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage = MagicMock(id=42, osuser=osuser)
|
2019-01-30 21:27:25 +01:00
|
|
|
maildomain = MagicMock(domain="example.org")
|
2015-11-22 19:41:45 +01:00
|
|
|
form = EditMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
|
|
|
maildomain=maildomain,
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage=hostingpackage,
|
2019-01-30 21:27:25 +01:00
|
|
|
data={"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertFalse(form.is_valid())
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("forwards", form.errors)
|
2015-11-22 19:41:45 +01:00
|
|
|
|
|
|
|
def test_save_with_forwards_no_commit(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
maildomain = MailDomain.objects.create(domain="example.org")
|
|
|
|
instance = MailAddress(id=23, domain=maildomain)
|
|
|
|
osuser = osusers.models.User(username="testuser")
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage = MagicMock(id=42, osuser=osuser)
|
|
|
|
form = EditMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
|
|
|
maildomain=maildomain,
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage=hostingpackage,
|
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
|
|
|
|
"forwards": "test2@example.org,test3@example.org",
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
form.save(commit=False)
|
|
|
|
|
|
|
|
def test_save_with_forwards_commit(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
osuser = osusers.models.User(username="testuser")
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage = MagicMock(id=42, osuser=osuser)
|
2023-02-18 22:46:48 +01:00
|
|
|
mail_domain = MailDomain.objects.create(domain="example.org")
|
|
|
|
instance = MailAddress(id=23, domain=mail_domain)
|
|
|
|
|
2015-11-22 19:41:45 +01:00
|
|
|
form = EditMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
2023-02-18 22:46:48 +01:00
|
|
|
maildomain=mail_domain,
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage=hostingpackage,
|
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
|
|
|
|
"forwards": "test2@example.org,test3@example.org",
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
form.save(commit=True)
|
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
@skip("needs mailbox refactoring")
|
2015-11-22 19:41:45 +01:00
|
|
|
def test_save_with_mailbox_no_commit(self):
|
|
|
|
instance = MagicMock(id=23)
|
2019-01-30 21:27:25 +01:00
|
|
|
osuser = Mock(username="testuser")
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage = MagicMock(id=42, osuser=osuser)
|
2019-01-30 21:27:25 +01:00
|
|
|
maildomain = MagicMock(domain="example.org")
|
2015-11-22 19:41:45 +01:00
|
|
|
form = EditMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
|
|
|
maildomain=maildomain,
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage=hostingpackage,
|
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
|
|
|
|
"mailbox": "mailbox23",
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
2019-01-30 21:27:25 +01:00
|
|
|
mailbox = MagicMock(osuser=osuser, username="testuserp01")
|
2015-11-22 19:41:45 +01:00
|
|
|
instance.set_mailbox.return_value = mailbox
|
|
|
|
form.save(commit=False)
|
|
|
|
instance.set_mailbox.assert_called_with(ANY, False)
|
|
|
|
mailbox.save.assert_not_called()
|
|
|
|
instance.save.assert_not_called()
|
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
@skip("needs mailbox refactoring")
|
2015-11-22 19:41:45 +01:00
|
|
|
def test_save_with_mailbox_commit(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
instance = MailAddress(id=23)
|
|
|
|
osuser = osusers.models.User(username="testuser")
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage = MagicMock(id=42, osuser=osuser)
|
2023-02-18 22:46:48 +01:00
|
|
|
mail_domain = MailDomain.objects.create(domain="example.org")
|
2015-11-22 19:41:45 +01:00
|
|
|
form = EditMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
2023-02-18 22:46:48 +01:00
|
|
|
maildomain=mail_domain,
|
2015-11-22 19:41:45 +01:00
|
|
|
hostingpackage=hostingpackage,
|
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
|
|
|
|
"mailbox": "mailbox23",
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
form.save(commit=True)
|
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
@skip("needs mailbox refactoring")
|
2015-11-22 19:41:45 +01:00
|
|
|
def test_save_with_other_choice(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
mail_domain = MailDomain.objects.create(domain="example.org")
|
|
|
|
instance = MailAddress(id=23, domain=mail_domain)
|
|
|
|
os_user = osusers.models.User(username="testuser")
|
|
|
|
hosting_package = MagicMock(id=42, osuser=os_user)
|
2015-11-22 19:41:45 +01:00
|
|
|
form = EditMailAddressForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance,
|
2023-02-18 22:46:48 +01:00
|
|
|
maildomain=mail_domain,
|
|
|
|
hostingpackage=hosting_package,
|
2015-11-22 19:41:45 +01:00
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
|
|
|
|
"mailbox": "mailbox23",
|
|
|
|
},
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
self.assertTrue(form.is_valid())
|
2019-01-30 21:27:25 +01:00
|
|
|
form.cleaned_data["mailbox_or_forwards"] = -1
|
2015-11-22 19:41:45 +01:00
|
|
|
form.save(commit=True)
|
|
|
|
instance.set_mailbox.assert_not_called()
|
|
|
|
instance.save.assert_called_with()
|