2015-10-15 21:50:18 +02:00
|
|
|
"""
|
|
|
|
Tests for :py:mod:`domains.forms`.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
from unittest.mock import MagicMock, Mock, patch
|
2015-10-15 21:50:18 +02:00
|
|
|
|
|
|
|
from django.forms import ValidationError
|
|
|
|
from django.test import TestCase
|
2019-01-30 21:27:25 +01:00
|
|
|
from django.urls import reverse
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.utils.translation import gettext as _
|
2015-10-15 21:50:18 +02:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
from domains.forms import CreateHostingDomainForm, relative_domain_validator
|
2015-10-15 21:50:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RelativeDomainValidatorTest(TestCase):
|
|
|
|
def test_valid_domainname(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
relative_domain_validator("example.org")
|
2015-10-15 21:50:18 +02:00
|
|
|
|
|
|
|
def test_domain_name_too_long(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
with self.assertRaisesMessage(ValidationError, _("host name too long")):
|
|
|
|
relative_domain_validator("e" * 255)
|
2015-10-15 21:50:18 +02:00
|
|
|
|
|
|
|
def test_domain_name_part_too_long(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
with self.assertRaisesMessage(ValidationError, _("invalid domain name")):
|
|
|
|
relative_domain_validator("a" * 64 + ".org")
|
2015-10-15 21:50:18 +02:00
|
|
|
|
|
|
|
def test_domain_name_illegal_characters(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
with self.assertRaisesMessage(ValidationError, _("invalid domain name")):
|
|
|
|
relative_domain_validator("eXampl3.org")
|
2015-10-15 21:50:18 +02:00
|
|
|
|
|
|
|
def test_domain_name_starts_with_dash(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
with self.assertRaisesMessage(ValidationError, _("invalid domain name")):
|
|
|
|
relative_domain_validator("-example.org")
|
2015-10-15 21:50:18 +02:00
|
|
|
|
|
|
|
def test_domain_name_ends_with_dash(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
with self.assertRaisesMessage(ValidationError, _("invalid domain name")):
|
|
|
|
relative_domain_validator("example-.org")
|
2015-10-15 21:50:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CreateHostingDomainFormTest(TestCase):
|
|
|
|
def test_constructor_needs_hostingpackage(self):
|
|
|
|
instance = MagicMock()
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
CreateHostingDomainForm(instance)
|
|
|
|
|
|
|
|
def test_constructor(self):
|
|
|
|
hostingpackage = Mock(id=42)
|
|
|
|
instance = MagicMock()
|
|
|
|
form = CreateHostingDomainForm(instance, hostingpackage=hostingpackage)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "hosting_package"))
|
2015-10-15 21:50:18 +02: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_hosting_domain", kwargs={"package": 42}),
|
|
|
|
)
|
2015-10-15 21:50:18 +02:00
|
|
|
self.assertEqual(len(form.helper.layout.fields), 2)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(form.helper.layout.fields[1].name, "submit")
|
2015-10-15 21:50:18 +02:00
|
|
|
|
|
|
|
def test_domain_field_has_relative_domain_validator(self):
|
|
|
|
hostingpackage = Mock(id=42)
|
|
|
|
instance = MagicMock()
|
|
|
|
form = CreateHostingDomainForm(instance, hostingpackage=hostingpackage)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn(relative_domain_validator, form.fields["domain"].validators)
|
2015-10-15 21:50:18 +02:00
|
|
|
|
|
|
|
def test_clean(self):
|
|
|
|
hostingpackage = Mock(id=42)
|
|
|
|
instance = MagicMock()
|
|
|
|
form = CreateHostingDomainForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance, hostingpackage=hostingpackage, data={"domain": "example.org"}
|
|
|
|
)
|
2015-10-15 21:50:18 +02:00
|
|
|
self.assertTrue(form.is_valid())
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("hosting_package", form.cleaned_data)
|
|
|
|
self.assertEqual(hostingpackage, form.cleaned_data["hosting_package"])
|
2015-10-15 21:50:18 +02:00
|
|
|
|
|
|
|
def test_save(self):
|
|
|
|
hostingpackage = Mock(id=42)
|
|
|
|
instance = MagicMock()
|
|
|
|
form = CreateHostingDomainForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance, hostingpackage=hostingpackage, data={"domain": "example.org"}
|
|
|
|
)
|
2015-10-15 21:50:18 +02:00
|
|
|
self.assertTrue(form.is_valid())
|
2019-01-30 21:27:25 +01:00
|
|
|
with patch("domains.forms.HostingDomain") as domain:
|
2015-10-15 21:50:18 +02:00
|
|
|
form.save()
|
|
|
|
domain.objects.create_for_hosting_package.assert_called_with(
|
2019-01-30 21:27:25 +01:00
|
|
|
commit=True, **form.cleaned_data
|
|
|
|
)
|
2015-10-15 21:50:18 +02:00
|
|
|
form.save(commit=False)
|
|
|
|
domain.objects.create_for_hosting_package.assert_called_with(
|
2019-01-30 21:27:25 +01:00
|
|
|
commit=False, **form.cleaned_data
|
|
|
|
)
|
2015-10-15 21:50:18 +02:00
|
|
|
|
|
|
|
def test_save_m2m(self):
|
|
|
|
hostingpackage = Mock(id=42)
|
|
|
|
instance = MagicMock()
|
|
|
|
form = CreateHostingDomainForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance, hostingpackage=hostingpackage, data={"domain": "example.org"}
|
|
|
|
)
|
2015-10-15 21:50:18 +02:00
|
|
|
form.save_m2m()
|