2015-10-25 18:25:28 +01:00
|
|
|
"""
|
|
|
|
Tests for :py:mod:`domains.views`.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
from unittest.mock import MagicMock, patch
|
2015-10-25 18:25:28 +01:00
|
|
|
|
|
|
|
from django.contrib.auth import get_user_model
|
2019-01-30 21:27:25 +01:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.urls import reverse
|
2015-10-25 18:25:28 +01:00
|
|
|
|
|
|
|
from domains.views import CreateHostingDomain
|
2019-01-30 21:27:25 +01:00
|
|
|
from hostingpackages.models import CustomerHostingPackage, HostingPackageTemplate
|
2015-10-25 18:25:28 +01:00
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_USER = "test"
|
|
|
|
TEST_PASSWORD = "secret"
|
|
|
|
TEST_EMAIL = "test@example.org"
|
|
|
|
TEST_NAME = "Example Tester".split()
|
2015-10-25 18:25:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CreateHostingDomainTest(TestCase):
|
|
|
|
def _setup_hosting_package(self, customer):
|
|
|
|
template = HostingPackageTemplate.objects.create(
|
2019-01-30 21:27:25 +01:00
|
|
|
name="testpackagetemplate", mailboxcount=0, diskspace=1, diskspace_unit=0
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
package = CustomerHostingPackage.objects.create_from_template(
|
2019-01-30 21:27:25 +01:00
|
|
|
customer, template, "testpackage"
|
|
|
|
)
|
|
|
|
with patch("hostingpackages.models.settings") as hmsettings:
|
2015-10-25 18:25:28 +01:00
|
|
|
hmsettings.OSUSER_DEFAULT_GROUPS = []
|
|
|
|
package.save()
|
|
|
|
return package
|
|
|
|
|
|
|
|
def test_get_anonymous(self):
|
2015-11-22 15:03:47 +01:00
|
|
|
response = self.client.get(
|
2019-01-30 21:27:25 +01:00
|
|
|
reverse("create_hosting_domain", kwargs={"package": 1})
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_get_regular_user(self):
|
|
|
|
customer = User.objects.create_user(
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_USER, email=TEST_EMAIL, password=TEST_PASSWORD
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
package = self._setup_hosting_package(customer)
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2015-11-22 15:03:47 +01:00
|
|
|
response = self.client.get(
|
2019-01-30 21:27:25 +01:00
|
|
|
reverse("create_hosting_domain", kwargs={"package": package.id})
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_get_staff_user(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
customer = User.objects.create_user("customer")
|
2015-10-25 18:25:28 +01:00
|
|
|
package = self._setup_hosting_package(customer)
|
2015-12-05 14:47:41 +01:00
|
|
|
User.objects.create_superuser(
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_USER, email=TEST_EMAIL, password=TEST_PASSWORD
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2015-11-22 15:03:47 +01:00
|
|
|
response = self.client.get(
|
2019-01-30 21:27:25 +01:00
|
|
|
reverse("create_hosting_domain", kwargs={"package": package.id})
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_get_template(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
customer = User.objects.create_user("customer")
|
2015-10-25 18:25:28 +01:00
|
|
|
package = self._setup_hosting_package(customer)
|
2015-12-05 14:47:41 +01:00
|
|
|
User.objects.create_superuser(
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_USER, email=TEST_EMAIL, password=TEST_PASSWORD
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2015-11-22 15:03:47 +01:00
|
|
|
response = self.client.get(
|
2019-01-30 21:27:25 +01:00
|
|
|
reverse("create_hosting_domain", kwargs={"package": package.id})
|
|
|
|
)
|
|
|
|
self.assertTemplateUsed(response, "domains/hostingdomain_create.html")
|
2015-10-25 18:25:28 +01:00
|
|
|
|
|
|
|
def test_get_no_package_found(self):
|
2015-12-05 14:47:41 +01:00
|
|
|
User.objects.create_superuser(
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_USER, email=TEST_EMAIL, password=TEST_PASSWORD
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2015-11-22 15:03:47 +01:00
|
|
|
response = self.client.get(
|
2019-01-30 21:27:25 +01:00
|
|
|
reverse("create_hosting_domain", kwargs={"package": 1})
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
|
|
|
def test_get_get_form_kwargs(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
customer = User.objects.create_user("customer")
|
2015-10-25 18:25:28 +01:00
|
|
|
package = self._setup_hosting_package(customer)
|
2015-12-05 14:47:41 +01:00
|
|
|
User.objects.create_superuser(
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_USER, email=TEST_EMAIL, password=TEST_PASSWORD
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
view = CreateHostingDomain(
|
2019-01-30 21:27:25 +01:00
|
|
|
request=MagicMock(), kwargs={"package": str(package.id)}
|
|
|
|
)
|
2015-11-22 15:03:47 +01:00
|
|
|
the_kwargs = view.get_form_kwargs()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("hostingpackage", the_kwargs)
|
|
|
|
self.assertEqual(the_kwargs["hostingpackage"], package)
|
2015-10-25 18:25:28 +01:00
|
|
|
|
|
|
|
def test_get_context_data_has_hosting_package(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
customer = User.objects.create_user("customer")
|
2015-10-25 18:25:28 +01:00
|
|
|
package = self._setup_hosting_package(customer)
|
2015-12-05 14:47:41 +01:00
|
|
|
User.objects.create_superuser(
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_USER, email=TEST_EMAIL, password=TEST_PASSWORD
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2015-11-22 15:03:47 +01:00
|
|
|
response = self.client.get(
|
2019-01-30 21:27:25 +01:00
|
|
|
reverse("create_hosting_domain", kwargs={"package": package.id})
|
|
|
|
)
|
|
|
|
self.assertIn("hostingpackage", response.context)
|
|
|
|
self.assertEqual(response.context["hostingpackage"], package)
|
2015-10-25 18:25:28 +01:00
|
|
|
|
|
|
|
def test_get_context_data_has_customer(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
customer = User.objects.create_user("customer")
|
2015-10-25 18:25:28 +01:00
|
|
|
package = self._setup_hosting_package(customer)
|
2015-12-05 14:47:41 +01:00
|
|
|
User.objects.create_superuser(
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_USER, email=TEST_EMAIL, password=TEST_PASSWORD
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2015-11-22 15:03:47 +01:00
|
|
|
response = self.client.get(
|
2019-01-30 21:27:25 +01:00
|
|
|
reverse("create_hosting_domain", kwargs={"package": package.id})
|
|
|
|
)
|
|
|
|
self.assertIn("customer", response.context)
|
|
|
|
self.assertEqual(response.context["customer"], customer)
|
2015-10-25 18:25:28 +01:00
|
|
|
|
|
|
|
def test_form_valid_redirect(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
customer = User.objects.create_user("customer")
|
2015-10-25 18:25:28 +01:00
|
|
|
package = self._setup_hosting_package(customer)
|
2015-12-05 14:47:41 +01:00
|
|
|
User.objects.create_superuser(
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_USER, email=TEST_EMAIL, password=TEST_PASSWORD
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2015-11-22 15:03:47 +01:00
|
|
|
response = self.client.post(
|
2019-01-30 21:27:25 +01:00
|
|
|
reverse("create_hosting_domain", kwargs={"package": package.id}),
|
|
|
|
data={"domain": "example.org"},
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.assertRedirects(response, package.get_absolute_url())
|
|
|
|
|
|
|
|
def test_form_valid_message(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
customer = User.objects.create_user("customer")
|
2015-10-25 18:25:28 +01:00
|
|
|
package = self._setup_hosting_package(customer)
|
2015-12-05 14:47:41 +01:00
|
|
|
User.objects.create_superuser(
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_USER, email=TEST_EMAIL, password=TEST_PASSWORD
|
|
|
|
)
|
2015-10-25 18:25:28 +01:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2015-11-22 15:03:47 +01:00
|
|
|
response = self.client.post(
|
2019-01-30 21:27:25 +01:00
|
|
|
reverse("create_hosting_domain", kwargs={"package": package.id}),
|
|
|
|
follow=True,
|
|
|
|
data={"domain": "example.org"},
|
|
|
|
)
|
|
|
|
messages = list(response.context["messages"])
|
2015-10-25 18:25:28 +01:00
|
|
|
self.assertEqual(len(messages), 1)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual("Successfully created domain example.org", str(messages[0]))
|