gva/gnuviechadmin/managemails/tests/test_forms.py

600 lines
23 KiB
Python
Raw Normal View History

"""
This module provides tests for :py:mod:`managemails.forms`.
"""
from unittest.mock import MagicMock, Mock, patch, ANY
from django.forms import ValidationError
from django.test import TestCase
from django.urls import reverse
from managemails.forms import (
AddMailAddressForm,
ChangeMailboxPasswordForm,
CreateMailboxForm,
EditMailAddressForm,
MAILBOX_OR_FORWARDS,
MailAddressFieldMixin,
multiple_email_validator,
)
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)
self.assertTrue(hasattr(form, "hosting_package"))
self.assertEqual(form.hosting_package, hostingpackage)
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")
def test_save(self, mailbox_objects):
osuser = MagicMock()
hostingpackage = Mock(id=42, osuser=osuser)
instance = MagicMock()
form = CreateMailboxForm(
instance=instance,
hostingpackage=hostingpackage,
data={"password1": "secret", "password2": "secret"},
)
mailbox_objects.get_next_mailbox_name.return_value = "mailbox23"
self.assertTrue(form.is_valid())
form.save(commit=False)
self.assertEqual(osuser, form.instance.osuser)
self.assertEqual("mailbox23", form.instance.username)
instance.set_password.assert_called_with("secret")
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)
instance = MagicMock(username="testuser")
form = ChangeMailboxPasswordForm(
instance=instance, hostingpackage=hostingpackage
)
self.assertTrue(hasattr(form, "hosting_package"))
self.assertEqual(form.hosting_package, hostingpackage)
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")
def test_save(self):
hostingpackage = Mock(id=42)
instance = MagicMock(username="testuser")
form = ChangeMailboxPasswordForm(
instance=instance,
hostingpackage=hostingpackage,
data={"password1": "newsecret", "password2": "newsecret"},
)
self.assertTrue(form.is_valid())
form.save(commit=False)
instance.set_password.assert_called_with("newsecret")
class MultipleEmailValidatorTest(TestCase):
def test_valid_single_address(self):
self.assertEqual(
"test@example.org", multiple_email_validator("test@example.org")
)
def test_valid_multiple_addresses(self):
self.assertEqual(
"test1@example.org,test2@example.org",
multiple_email_validator("test1@example.org,test2@example.org"),
)
def test_empty(self):
self.assertEqual("", multiple_email_validator(""))
def test_none(self):
self.assertIsNone(multiple_email_validator(None))
def test_invalid_single_address(self):
with self.assertRaises(ValidationError):
multiple_email_validator("no@ddress")
def test_invalid_multiple_addresses(self):
with self.assertRaises(ValidationError):
multiple_email_validator("test1@example.org,no@ddress")
class MailAddressFieldMixinTest(TestCase):
def test_fields_defined(self):
form = MailAddressFieldMixin()
self.assertIn("mailbox_or_forwards", form.fields)
self.assertIn("mailbox", form.fields)
self.assertIn("forwards", form.fields)
class AddMailAddressFormTest(TestCase):
def setUp(self):
self.patcher1 = patch("managemails.forms.Mailbox.objects")
self.patcher2 = patch("managemails.forms.MailAddress.objects")
self.mailbox_objects = self.patcher1.start()
self.mailaddress_objects = self.patcher2.start()
def tearDown(self):
self.patcher2.stop()
self.patcher1.stop()
def test_constructor_needs_hostingpackage(self):
instance = MagicMock()
with self.assertRaises(KeyError):
AddMailAddressForm(instance=instance, maildomain=MagicMock())
def test_constructor_needs_maildomain(self):
instance = MagicMock()
with self.assertRaises(KeyError):
AddMailAddressForm(instance=instance, hostingpackage=MagicMock())
def test_constructor(self):
instance = MagicMock()
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = AddMailAddressForm(
instance=instance, hostingpackage=hostingpackage, maildomain=maildomain
)
self.mailbox_objects.unused.assert_called_with(osuser=osuser)
self.assertIn("mailbox_or_forwards", form.fields)
self.assertIn("mailbox", form.fields)
self.assertIn("forwards", form.fields)
self.assertTrue(hasattr(form, "hosting_package"))
self.assertEqual(form.hosting_package, hostingpackage)
self.assertTrue(hasattr(form, "maildomain"))
self.assertEqual(form.maildomain, maildomain)
self.assertTrue(hasattr(form, "helper"))
self.assertEqual(
form.helper.form_action,
reverse("add_mailaddress", kwargs={"package": 42, "domain": "example.org"}),
)
self.assertEqual(len(form.helper.layout), 2)
self.assertEqual(form.helper.layout[1].name, "submit")
def test_clean_localpart_valid(self):
instance = MagicMock()
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hostingpackage,
maildomain=maildomain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
"forwards": "test2@example.org",
},
)
self.mailaddress_objects.filter(
domain=maildomain, localpart="test"
).exists.return_value = False
self.assertTrue(form.is_valid())
self.assertEqual("test", form.clean_localpart())
def test_clean_localpart_duplicate(self):
instance = MagicMock()
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hostingpackage,
maildomain=maildomain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
"forwards": "test2@example.org",
},
)
self.mailaddress_objects.filter(
domain=maildomain, localpart="test"
).exists.return_value = True
self.assertFalse(form.is_valid())
self.assertIn("localpart", form.errors)
def test_clean_no_mailbox_choice(self):
instance = MagicMock()
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hostingpackage,
maildomain=maildomain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
},
)
self.mailaddress_objects.filter(
domain=maildomain, localpart="test"
).exists.return_value = False
self.assertFalse(form.is_valid())
self.assertIn("mailbox", form.errors)
def test_clean_no_forward_address_choice(self):
instance = MagicMock()
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hostingpackage,
maildomain=maildomain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
},
)
self.mailaddress_objects.filter(
domain=maildomain, localpart="test"
).exists.return_value = False
self.assertFalse(form.is_valid())
self.assertIn("forwards", form.errors)
def test_save_with_forwards_no_commit(self):
instance = MagicMock()
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hostingpackage,
maildomain=maildomain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
"forwards": "test2@example.org,test3@example.org",
},
)
self.mailaddress_objects.filter(
domain=maildomain, localpart="test"
).exists.return_value = False
self.assertTrue(form.is_valid())
address1 = MagicMock(mailaddress="test2@example.org")
address2 = MagicMock(mailaddress="test3@example.org")
instance.set_forward_addresses.return_value = [address1, address2]
form.save(commit=False)
self.assertEqual(maildomain, instance.domain)
instance.set_forward_addresses.assert_called_with(
["test2@example.org", "test3@example.org"], commit=False
)
address1.save.assert_not_called()
address2.save.assert_not_called()
instance.save.assert_not_called()
def test_save_with_forwards_commit(self):
instance = MagicMock()
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hostingpackage,
maildomain=maildomain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
"forwards": "test2@example.org,test3@example.org",
},
)
self.mailaddress_objects.filter(
domain=maildomain, localpart="test"
).exists.return_value = False
self.assertTrue(form.is_valid())
address1 = MagicMock(mailaddress="test2@example.org")
address2 = MagicMock(mailaddress="test3@example.org")
instance.set_forward_addresses.return_value = [address1, address2]
form.save(commit=True)
self.assertEqual(maildomain, instance.domain)
instance.set_forward_addresses.assert_called_with(
["test2@example.org", "test3@example.org"], commit=False
)
address1.save.assert_called_with()
address2.save.assert_called_with()
instance.save.assert_called_with()
def test_save_with_mailbox_no_commit(self):
instance = MagicMock()
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hostingpackage,
maildomain=maildomain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
"mailbox": "mailbox23",
},
)
self.mailaddress_objects.filter(
domain=maildomain, localpart="test"
).exists.return_value = False
self.assertTrue(form.is_valid())
mailbox = MagicMock(osuser=osuser, username="testuserp01")
instance.set_mailbox.return_value = mailbox
form.save(commit=False)
self.assertEqual(maildomain, instance.domain)
instance.set_mailbox.assert_called_with(ANY, commit=False)
mailbox.save.assert_not_called()
instance.save.assert_not_called()
def test_save_with_mailbox_commit(self):
instance = MagicMock()
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hostingpackage,
maildomain=maildomain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
"mailbox": "mailbox23",
},
)
self.mailaddress_objects.filter(
domain=maildomain, localpart="test"
).exists.return_value = False
self.assertTrue(form.is_valid())
mailbox = MagicMock(osuser=osuser, username="testuserp01")
instance.set_mailbox.return_value = mailbox
form.save(commit=True)
self.assertEqual(maildomain, instance.domain)
instance.set_mailbox.assert_called_with(ANY, commit=False)
instance.set_mailbox.return_value.save.assert_called_with()
mailbox.save.assert_called_with()
instance.save.assert_called_with()
def test_save_with_other_choice(self):
instance = MagicMock()
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hostingpackage,
maildomain=maildomain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
"mailbox": "mailbox23",
},
)
self.mailaddress_objects.filter(
domain=maildomain, localpart="test"
).exists.return_value = False
self.assertTrue(form.is_valid())
form.cleaned_data["mailbox_or_forwards"] = -1
address1 = MagicMock(mailaddress="test2@example.org")
address2 = MagicMock(mailaddress="test3@example.org")
instance.set_forward_addresses.return_value = [address1, address2]
mailbox = MagicMock(osuser=osuser, username="testuserp01")
instance.set_mailbox.return_value = mailbox
form.save(commit=True)
instance.set_mailbox.assert_not_called()
instance.set_forward_addresses.assert_not_called()
address1.save.assert_not_called()
address2.save.assert_not_called()
mailbox.save.assert_not_called()
instance.save.assert_called_with()
class EditMailAddressFormTest(TestCase):
def setUp(self):
self.patcher1 = patch("managemails.forms.Mailbox.objects")
self.patcher2 = patch("managemails.forms.MailAddress.objects")
self.mailbox_objects = self.patcher1.start()
self.mailaddress_objects = self.patcher2.start()
def tearDown(self):
self.patcher2.stop()
self.patcher1.stop()
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):
instance = MagicMock(id=23)
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = EditMailAddressForm(
instance=instance, maildomain=maildomain, hostingpackage=hostingpackage
)
self.mailbox_objects.unused_or_own.assert_called_with(instance, osuser)
self.assertIn("mailbox_or_forwards", form.fields)
self.assertIn("mailbox", form.fields)
self.assertIn("forwards", form.fields)
self.assertTrue(hasattr(form, "hosting_package"))
self.assertEqual(form.hosting_package, hostingpackage)
self.assertTrue(hasattr(form, "maildomain"))
self.assertEqual(form.maildomain, maildomain)
self.assertTrue(hasattr(form, "helper"))
self.assertEqual(
form.helper.form_action,
reverse(
"edit_mailaddress",
kwargs={"package": 42, "domain": "example.org", "pk": 23},
),
)
self.assertEqual(len(form.helper.layout), 2)
self.assertEqual(form.helper.layout[1].name, "submit")
def test_clean_no_mailbox_choice(self):
instance = MagicMock(id=23)
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = EditMailAddressForm(
instance=instance,
maildomain=maildomain,
hostingpackage=hostingpackage,
data={"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox},
)
self.assertFalse(form.is_valid())
self.assertIn("mailbox", form.errors)
def test_clean_no_forward_address_choice(self):
instance = MagicMock(id=23)
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = EditMailAddressForm(
instance=instance,
maildomain=maildomain,
hostingpackage=hostingpackage,
data={"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards},
)
self.assertFalse(form.is_valid())
self.assertIn("forwards", form.errors)
def test_save_with_forwards_no_commit(self):
instance = MagicMock(id=23)
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = EditMailAddressForm(
instance=instance,
maildomain=maildomain,
hostingpackage=hostingpackage,
data={
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
"forwards": "test2@example.org,test3@example.org",
},
)
self.assertTrue(form.is_valid())
address1 = MagicMock(mailaddress="test2@example.org")
address2 = MagicMock(mailaddress="test3@example.org")
instance.set_forward_addresses.return_value = [address1, address2]
form.save(commit=False)
instance.set_forward_addresses.assert_called_with(
["test2@example.org", "test3@example.org"], False
)
address1.save.assert_not_called()
address2.save.assert_not_called()
instance.save.assert_not_called()
def test_save_with_forwards_commit(self):
instance = MagicMock(id=23)
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = EditMailAddressForm(
instance=instance,
maildomain=maildomain,
hostingpackage=hostingpackage,
data={
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
"forwards": "test2@example.org,test3@example.org",
},
)
self.assertTrue(form.is_valid())
address1 = MagicMock(mailaddress="test2@example.org")
address2 = MagicMock(mailaddress="test3@example.org")
instance.set_forward_addresses.return_value = [address1, address2]
form.save(commit=True)
instance.set_forward_addresses.assert_called_with(
["test2@example.org", "test3@example.org"], True
)
instance.save.assert_called_with()
def test_save_with_mailbox_no_commit(self):
instance = MagicMock(id=23)
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = EditMailAddressForm(
instance=instance,
maildomain=maildomain,
hostingpackage=hostingpackage,
data={
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
"mailbox": "mailbox23",
},
)
self.assertTrue(form.is_valid())
mailbox = MagicMock(osuser=osuser, username="testuserp01")
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()
def test_save_with_mailbox_commit(self):
instance = MagicMock(id=23)
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = EditMailAddressForm(
instance=instance,
maildomain=maildomain,
hostingpackage=hostingpackage,
data={
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
"mailbox": "mailbox23",
},
)
self.assertTrue(form.is_valid())
mailbox = MagicMock(osuser=osuser, username="testuserp01")
instance.set_mailbox.return_value = mailbox
self.mailbox_objects.unused_or_own.get.return_value = mailbox
form.save(commit=True)
instance.set_mailbox.assert_called_with(ANY, True)
instance.save.assert_called_with()
def test_save_with_other_choice(self):
instance = MagicMock(id=23)
osuser = Mock(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MagicMock(domain="example.org")
form = EditMailAddressForm(
instance=instance,
maildomain=maildomain,
hostingpackage=hostingpackage,
data={
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
"mailbox": "mailbox23",
},
)
self.assertTrue(form.is_valid())
form.cleaned_data["mailbox_or_forwards"] = -1
form.save(commit=True)
instance.set_mailbox.assert_not_called()
instance.save.assert_called_with()