gva/gnuviechadmin/managemails/tests/test_forms.py

532 lines
20 KiB
Python
Raw Normal View History

"""
This module provides tests for :py:mod:`managemails.forms`.
"""
from unittest import skip
from unittest.mock import ANY, MagicMock, Mock, patch
from django.forms import ValidationError
from django.test import TestCase
from django.urls import reverse
import osusers.models
from domains.models import MailDomain
from managemails.forms import (
MAILBOX_OR_FORWARDS,
AddMailAddressForm,
ChangeMailboxPasswordForm,
CreateMailboxForm,
EditMailAddressForm,
MailAddressFieldMixin,
multiple_email_validator,
)
from managemails.models import MailAddress, Mailbox
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 test_constructor_needs_hostingpackage(self):
instance = MailAddress()
with self.assertRaises(KeyError):
AddMailAddressForm(instance=instance, maildomain=None)
def test_constructor_needs_maildomain(self):
instance = MagicMock()
with self.assertRaises(KeyError):
AddMailAddressForm(instance=instance, hostingpackage=MagicMock())
def test_constructor(self):
instance = MailAddress()
os_user = osusers.models.User(username="testuser")
hosting_package = MagicMock(id=42, osuser=os_user)
mail_domain = MailDomain(domain="example.org")
form = AddMailAddressForm(
instance=instance, hostingpackage=hosting_package, maildomain=mail_domain
)
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, hosting_package)
self.assertTrue(hasattr(form, "maildomain"))
self.assertEqual(form.maildomain, mail_domain)
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):
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)
form = AddMailAddressForm(
instance=instance,
hostingpackage=hosting_package,
maildomain=mail_domain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
"forwards": "test2@example.org",
},
)
self.assertTrue(form.is_valid())
self.assertEqual("test", form.clean_localpart())
def test_clean_localpart_duplicate(self):
mail_domain = MailDomain.objects.create(domain="example.org")
MailAddress.objects.create(localpart="test", domain=mail_domain)
instance = MailAddress()
osuser = osusers.models.User(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
form = AddMailAddressForm(
instance=instance,
hostingpackage=hostingpackage,
maildomain=mail_domain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
"forwards": "test2@example.org",
},
)
self.assertFalse(form.is_valid())
self.assertIn("localpart", form.errors)
def test_clean_no_mailbox_choice(self):
instance = MailAddress()
osuser = osusers.models.User(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MailDomain(domain="example.org")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hostingpackage,
maildomain=maildomain,
data={
"localpart": "test",
"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 = MailAddress()
os_user = osusers.models.User(username="testuser")
hosting_package = MagicMock(id=42, osuser=os_user)
mail_domain = MailDomain(domain="example.org")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hosting_package,
maildomain=mail_domain,
data={
"localpart": "test",
"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):
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)
form = AddMailAddressForm(
instance=instance,
hostingpackage=hosting_package,
maildomain=mail_domain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
"forwards": "test2@example.org,test3@example.org",
},
)
self.assertTrue(form.is_valid())
form.save(commit=False)
self.assertEqual(mail_domain, instance.domain)
def test_save_with_forwards_commit(self):
maildomain = MailDomain.objects.create(domain="example.org")
instance = MailAddress()
osuser = osusers.models.User(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
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.assertTrue(form.is_valid())
form.save(commit=True)
self.assertEqual(maildomain, instance.domain)
forwards = list(
instance.mailaddressforward_set.values_list("target", flat=True).order_by(
"target"
)
)
self.assertEqual(len(forwards), 2)
self.assertEqual(forwards, ["test2@example.org", "test3@example.org"])
@skip("does not work because it will create a real mailbox")
def test_save_with_mailbox_no_commit(self):
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")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hosting_package,
maildomain=mail_domain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
"mailbox": "mailbox23",
},
)
self.assertTrue(form.is_valid())
form.save(commit=False)
self.assertEqual(mail_domain, instance.domain)
@skip("does not work because it will create a real mailbox")
def test_save_with_mailbox_commit(self):
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)
form = AddMailAddressForm(
instance=instance,
hostingpackage=hosting_package,
maildomain=mail_domain,
data={
"localpart": "test",
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
"mailbox": "mailbox23",
},
)
self.assertTrue(form.is_valid())
form.save(commit=True)
self.assertEqual(mail_domain, instance.domain)
@skip("does not work because it will create a real mailbox")
def test_save_with_other_choice(self):
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")
form = AddMailAddressForm(
instance=instance,
hostingpackage=hosting_package,
maildomain=mail_domain,
data={
"localpart": "test",
"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)
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):
instance = MailAddress(id=23)
osuser = osusers.models.User(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
maildomain = MailDomain.objects.create(domain="example.org")
form = EditMailAddressForm(
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"))
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")
@skip("needs mailbox refactoring")
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)
@skip("needs mailbox refactoring")
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):
maildomain = MailDomain.objects.create(domain="example.org")
instance = MailAddress(id=23, domain=maildomain)
osuser = osusers.models.User(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
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())
form.save(commit=False)
def test_save_with_forwards_commit(self):
osuser = osusers.models.User(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
mail_domain = MailDomain.objects.create(domain="example.org")
instance = MailAddress(id=23, domain=mail_domain)
form = EditMailAddressForm(
instance=instance,
maildomain=mail_domain,
hostingpackage=hostingpackage,
data={
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.forwards,
"forwards": "test2@example.org,test3@example.org",
},
)
self.assertTrue(form.is_valid())
form.save(commit=True)
@skip("needs mailbox refactoring")
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()
@skip("needs mailbox refactoring")
def test_save_with_mailbox_commit(self):
instance = MailAddress(id=23)
osuser = osusers.models.User(username="testuser")
hostingpackage = MagicMock(id=42, osuser=osuser)
mail_domain = MailDomain.objects.create(domain="example.org")
form = EditMailAddressForm(
instance=instance,
maildomain=mail_domain,
hostingpackage=hostingpackage,
data={
"mailbox_or_forwards": MAILBOX_OR_FORWARDS.mailbox,
"mailbox": "mailbox23",
},
)
self.assertTrue(form.is_valid())
form.save(commit=True)
@skip("needs mailbox refactoring")
def test_save_with_other_choice(self):
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)
form = EditMailAddressForm(
instance=instance,
maildomain=mail_domain,
hostingpackage=hosting_package,
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()