2016-01-28 16:17:50 +01:00
|
|
|
"""
|
|
|
|
This module provides tests for :py:mod:`userdbs.views`.
|
|
|
|
|
|
|
|
"""
|
2023-05-02 17:57:13 +02:00
|
|
|
from unittest.mock import MagicMock, patch
|
2016-01-28 16:17:50 +01:00
|
|
|
|
|
|
|
from django.contrib.auth import get_user_model
|
2023-05-02 17:57:13 +02:00
|
|
|
from django.test import TestCase
|
2019-01-30 21:27:25 +01:00
|
|
|
from django.urls import reverse
|
2016-01-28 16:17:50 +01:00
|
|
|
|
|
|
|
from hostingpackages.models import (
|
|
|
|
CustomerHostingPackage,
|
|
|
|
CustomerUserDatabaseOption,
|
|
|
|
HostingPackageTemplate,
|
|
|
|
UserDatabaseOption,
|
|
|
|
)
|
|
|
|
from userdbs.models import DB_TYPES, UserDatabase
|
2016-01-29 11:03:24 +01:00
|
|
|
from userdbs.views import AddUserDatabase, ChangeDatabaseUserPassword
|
2016-01-28 16:17:50 +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"
|
2016-01-28 16:17:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HostingPackageAwareTestMixin(object):
|
2019-01-30 21:27:25 +01:00
|
|
|
# noinspection PyMethodMayBeStatic
|
2016-01-28 16:17:50 +01:00
|
|
|
def _setup_hosting_package(self, customer):
|
|
|
|
template = HostingPackageTemplate.objects.create(
|
2019-01-30 21:27:25 +01:00
|
|
|
name="testpackagetemplate", mailboxcount=10, diskspace=1, diskspace_unit=0
|
|
|
|
)
|
2016-01-28 16:17:50 +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:
|
2016-01-28 16:17:50 +01:00
|
|
|
hmsettings.OSUSER_DEFAULT_GROUPS = []
|
|
|
|
package.save()
|
|
|
|
return package
|
|
|
|
|
|
|
|
|
|
|
|
class CustomerUserDatabaseOptionAwareTestMixin(object):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2019-01-30 21:27:25 +01:00
|
|
|
super(CustomerUserDatabaseOptionAwareTestMixin, self).__init__(*args, **kwargs)
|
2016-01-28 16:17:50 +01:00
|
|
|
self._templates = {}
|
|
|
|
|
|
|
|
def _setup_userdatabaseoption(self, number, dbtype):
|
|
|
|
key = "{}_{}".format(dbtype, number)
|
|
|
|
if key not in self._templates:
|
|
|
|
self._templates[key] = UserDatabaseOption.objects.create(
|
2019-01-30 21:27:25 +01:00
|
|
|
number=number, db_type=dbtype
|
|
|
|
)
|
2016-01-28 16:17:50 +01:00
|
|
|
return self._templates[key]
|
|
|
|
|
|
|
|
def _create_userdatabase_option(self, number=1, dbtype=DB_TYPES.pgsql):
|
2019-01-30 21:27:25 +01:00
|
|
|
# noinspection PyUnresolvedReferences
|
2016-01-28 16:17:50 +01:00
|
|
|
return CustomerUserDatabaseOption.objects.create(
|
|
|
|
template=self._setup_userdatabaseoption(number, dbtype),
|
2019-01-30 21:27:25 +01:00
|
|
|
number=number,
|
|
|
|
db_type=dbtype,
|
|
|
|
hosting_package=self.package,
|
|
|
|
)
|
2016-01-28 16:17:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AddUserDatabaseTest(
|
2019-01-30 21:27:25 +01:00
|
|
|
HostingPackageAwareTestMixin, CustomerUserDatabaseOptionAwareTestMixin, TestCase
|
2016-01-28 16:17:50 +01:00
|
|
|
):
|
|
|
|
def setUp(self):
|
|
|
|
self.customer = User.objects.create_user(
|
2019-01-30 21:27:25 +01:00
|
|
|
username=TEST_USER, password=TEST_PASSWORD
|
|
|
|
)
|
2016-01-28 16:17:50 +01:00
|
|
|
self.package = self._setup_hosting_package(self.customer)
|
|
|
|
|
|
|
|
def _get_url(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
return reverse("add_userdatabase", kwargs={"package": self.package.id})
|
2016-01-28 16:17:50 +01:00
|
|
|
|
|
|
|
def test_get_anonymous(self):
|
|
|
|
response = self.client.get(self._get_url())
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_get_regular_user_nodboption(self):
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.get(self._get_url())
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
|
|
|
def test_get_regular_user(self):
|
|
|
|
self._create_userdatabase_option()
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.get(self._get_url())
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_get_other_regular_user(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
User.objects.create_user("test2", password=TEST_PASSWORD)
|
|
|
|
self.client.login(username="test2", password=TEST_PASSWORD)
|
2016-01-28 16:17:50 +01:00
|
|
|
response = self.client.get(self._get_url())
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_get_staff_user_nodboption(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
User.objects.create_superuser("admin", email=TEST_EMAIL, password=TEST_PASSWORD)
|
|
|
|
self.client.login(username="admin", password=TEST_PASSWORD)
|
2016-01-28 16:17:50 +01:00
|
|
|
response = self.client.get(self._get_url())
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
|
|
|
def test_get_staff_user(self):
|
|
|
|
self._create_userdatabase_option()
|
2019-01-30 21:27:25 +01:00
|
|
|
User.objects.create_superuser("admin", email=TEST_EMAIL, password=TEST_PASSWORD)
|
|
|
|
self.client.login(username="admin", password=TEST_PASSWORD)
|
2016-01-28 16:17:50 +01:00
|
|
|
response = self.client.get(self._get_url())
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_get_regular_user_nofree_db(self):
|
|
|
|
db_option = self._create_userdatabase_option()
|
|
|
|
UserDatabase.objects.create_userdatabase_with_user(
|
2019-01-30 21:27:25 +01:00
|
|
|
db_option.db_type, self.package.osuser
|
|
|
|
)
|
2016-01-28 16:17:50 +01:00
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.get(self._get_url())
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
|
|
|
def test_get_form_kwargs(self):
|
|
|
|
db_option = self._create_userdatabase_option()
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
view = AddUserDatabase(
|
2019-01-30 21:27:25 +01:00
|
|
|
request=MagicMock(), kwargs={"package": str(self.package.pk)}
|
|
|
|
)
|
2016-01-28 16:17:50 +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"], self.package)
|
|
|
|
self.assertIn("dbtypes", the_kwargs)
|
2016-01-28 16:17:50 +01:00
|
|
|
self.assertEqual(
|
2019-01-30 21:27:25 +01:00
|
|
|
the_kwargs["dbtypes"], [(db_option.db_type, DB_TYPES[db_option.db_type])]
|
2016-01-28 16:17:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_get_template(self):
|
|
|
|
self._create_userdatabase_option()
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.get(self._get_url())
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTemplateUsed(response, "userdbs/userdatabase_create.html")
|
2016-01-28 16:17:50 +01:00
|
|
|
|
|
|
|
def test_form_valid_redirect(self):
|
|
|
|
db_option = self._create_userdatabase_option()
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.post(
|
|
|
|
self._get_url(),
|
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"db_type": db_option.db_type,
|
|
|
|
"password1": TEST_PASSWORD,
|
|
|
|
"password2": TEST_PASSWORD,
|
|
|
|
},
|
|
|
|
)
|
2016-01-28 16:17:50 +01:00
|
|
|
self.assertRedirects(response, self.package.get_absolute_url())
|
|
|
|
|
|
|
|
def test_form_valid_message(self):
|
|
|
|
db_option = self._create_userdatabase_option()
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.post(
|
2019-01-30 21:27:25 +01:00
|
|
|
self._get_url(),
|
|
|
|
follow=True,
|
2016-01-28 16:17:50 +01:00
|
|
|
data={
|
2019-01-30 21:27:25 +01:00
|
|
|
"db_type": db_option.db_type,
|
|
|
|
"password1": TEST_PASSWORD,
|
|
|
|
"password2": TEST_PASSWORD,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
db = UserDatabase.objects.filter(db_user__osuser=self.package.osuser).get()
|
|
|
|
messages = list(response.context["messages"])
|
2016-01-28 16:17:50 +01:00
|
|
|
self.assertEqual(len(messages), 1)
|
|
|
|
self.assertEqual(
|
2019-01-30 21:27:25 +01:00
|
|
|
str(messages[0]),
|
|
|
|
(
|
|
|
|
"Successfully create new {type} database {dbname} for user " "{dbuser}."
|
|
|
|
).format(type=db.db_user.db_type, dbname=db.db_name, dbuser=db.db_user),
|
|
|
|
)
|
2016-01-28 16:17:50 +01:00
|
|
|
|
|
|
|
|
2016-01-29 11:03:24 +01:00
|
|
|
class ChangeDatabaseUserPasswordTest(
|
2019-01-30 21:27:25 +01:00
|
|
|
HostingPackageAwareTestMixin, CustomerUserDatabaseOptionAwareTestMixin, TestCase
|
2016-01-29 11:03:24 +01:00
|
|
|
):
|
2016-01-28 16:17:50 +01:00
|
|
|
def setUp(self):
|
|
|
|
self.customer = User.objects.create_user(
|
2019-01-30 21:27:25 +01:00
|
|
|
username=TEST_USER, password=TEST_PASSWORD
|
|
|
|
)
|
2016-01-28 16:17:50 +01:00
|
|
|
self.package = self._setup_hosting_package(self.customer)
|
2016-01-29 11:03:24 +01:00
|
|
|
template = self._create_userdatabase_option()
|
|
|
|
database = UserDatabase.objects.create_userdatabase_with_user(
|
2019-01-30 21:27:25 +01:00
|
|
|
template.db_type, self.package.osuser
|
|
|
|
)
|
2016-01-29 11:03:24 +01:00
|
|
|
self.dbuser = database.db_user
|
2016-01-28 16:17:50 +01:00
|
|
|
|
2016-01-29 11:03:24 +01:00
|
|
|
def _get_url(self, dbuser):
|
2016-01-28 16:17:50 +01:00
|
|
|
return reverse(
|
2019-01-30 21:27:25 +01:00
|
|
|
"change_dbuser_password",
|
|
|
|
kwargs={"package": self.package.id, "slug": dbuser.name},
|
|
|
|
)
|
2016-01-29 11:03:24 +01:00
|
|
|
|
|
|
|
def test_get_anonymous(self):
|
|
|
|
response = self.client.get(self._get_url(self.dbuser))
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_get_regular_user(self):
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.get(self._get_url(self.dbuser))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_get_other_regular_user(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
User.objects.create_user("test2", password=TEST_PASSWORD)
|
|
|
|
self.client.login(username="test2", password=TEST_PASSWORD)
|
2016-01-29 11:03:24 +01:00
|
|
|
response = self.client.get(self._get_url(self.dbuser))
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_get_staff_user(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
User.objects.create_superuser("admin", email=TEST_EMAIL, password=TEST_PASSWORD)
|
|
|
|
self.client.login(username="admin", password=TEST_PASSWORD)
|
2016-01-29 11:03:24 +01:00
|
|
|
response = self.client.get(self._get_url(self.dbuser))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_get_template(self):
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.get(self._get_url(self.dbuser))
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTemplateUsed(response, "userdbs/databaseuser_setpassword.html")
|
2016-01-29 11:03:24 +01:00
|
|
|
|
|
|
|
def test_get_form_kwargs(self):
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2019-01-30 21:27:25 +01:00
|
|
|
view = ChangeDatabaseUserPassword(
|
|
|
|
request=MagicMock(),
|
|
|
|
kwargs={"package": str(self.package.pk), "slug": self.dbuser.name},
|
|
|
|
)
|
2016-01-29 11:03:24 +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"], self.package)
|
2016-01-29 11:03:24 +01:00
|
|
|
|
|
|
|
def test_get_context_data(self):
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.get(self._get_url(self.dbuser))
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("dbuser", response.context)
|
|
|
|
self.assertEqual(response.context["dbuser"], self.dbuser)
|
|
|
|
self.assertIn("hostingpackage", response.context)
|
|
|
|
self.assertEqual(response.context["hostingpackage"], self.package)
|
|
|
|
self.assertIn("customer", response.context)
|
|
|
|
self.assertEqual(response.context["customer"], self.customer)
|
2016-01-29 11:03:24 +01:00
|
|
|
|
|
|
|
def test_form_valid_redirect(self):
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
2019-01-30 21:27:25 +01:00
|
|
|
response = self.client.post(
|
|
|
|
self._get_url(self.dbuser),
|
|
|
|
data={"password1": TEST_PASSWORD, "password2": TEST_PASSWORD},
|
|
|
|
)
|
2016-01-29 11:03:24 +01:00
|
|
|
self.assertRedirects(response, self.package.get_absolute_url())
|
|
|
|
|
|
|
|
def test_form_valid_message(self):
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.post(
|
2019-01-30 21:27:25 +01:00
|
|
|
self._get_url(self.dbuser),
|
|
|
|
follow=True,
|
|
|
|
data={"password1": TEST_PASSWORD, "password2": TEST_PASSWORD},
|
|
|
|
)
|
|
|
|
messages = list(response.context["messages"])
|
2016-01-29 11:03:24 +01:00
|
|
|
self.assertEqual(len(messages), 1)
|
|
|
|
self.assertEqual(
|
|
|
|
str(messages[0]),
|
2019-01-30 21:27:25 +01:00
|
|
|
"Successfully changed password of database user {dbuser}.".format(
|
|
|
|
dbuser=self.dbuser.name
|
|
|
|
),
|
|
|
|
)
|
2016-01-28 16:17:50 +01:00
|
|
|
|
|
|
|
|
2016-01-29 10:40:14 +01:00
|
|
|
class DeleteUserDatabaseTest(
|
2019-01-30 21:27:25 +01:00
|
|
|
HostingPackageAwareTestMixin, CustomerUserDatabaseOptionAwareTestMixin, TestCase
|
2016-01-29 10:40:14 +01:00
|
|
|
):
|
2016-01-28 16:17:50 +01:00
|
|
|
def setUp(self):
|
|
|
|
self.customer = User.objects.create_user(
|
2019-01-30 21:27:25 +01:00
|
|
|
username=TEST_USER, password=TEST_PASSWORD
|
|
|
|
)
|
2016-01-28 16:17:50 +01:00
|
|
|
self.package = self._setup_hosting_package(self.customer)
|
2016-01-29 10:40:14 +01:00
|
|
|
template = self._create_userdatabase_option()
|
|
|
|
self.database = UserDatabase.objects.create_userdatabase_with_user(
|
2019-01-30 21:27:25 +01:00
|
|
|
template.db_type, self.package.osuser
|
|
|
|
)
|
2016-01-28 16:17:50 +01:00
|
|
|
|
|
|
|
def _get_url(self, userdatabase):
|
|
|
|
return reverse(
|
2019-01-30 21:27:25 +01:00
|
|
|
"delete_userdatabase",
|
|
|
|
kwargs={"package": self.package.id, "slug": userdatabase.db_name},
|
|
|
|
)
|
2016-01-29 10:40:14 +01:00
|
|
|
|
|
|
|
def test_get_anonymous(self):
|
|
|
|
response = self.client.get(self._get_url(self.database))
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_get_regular_user(self):
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.get(self._get_url(self.database))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_get_other_regular_user(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
User.objects.create_user("test2", password=TEST_PASSWORD)
|
|
|
|
self.client.login(username="test2", password=TEST_PASSWORD)
|
2016-01-29 10:40:14 +01:00
|
|
|
response = self.client.get(self._get_url(self.database))
|
|
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
|
|
|
|
def test_get_staff_user(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
User.objects.create_superuser("admin", email=TEST_EMAIL, password=TEST_PASSWORD)
|
|
|
|
self.client.login(username="admin", password=TEST_PASSWORD)
|
2016-01-29 10:40:14 +01:00
|
|
|
response = self.client.get(self._get_url(self.database))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_get_template(self):
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.get(self._get_url(self.database))
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTemplateUsed(response, "userdbs/userdatabase_confirm_delete.html")
|
2016-01-29 10:40:14 +01:00
|
|
|
|
|
|
|
def test_get_context_data(self):
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.get(self._get_url(self.database))
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("database", response.context)
|
|
|
|
self.assertEqual(response.context["database"], self.database)
|
|
|
|
self.assertIn("hostingpackage", response.context)
|
|
|
|
self.assertEqual(response.context["hostingpackage"], self.package)
|
|
|
|
self.assertIn("customer", response.context)
|
|
|
|
self.assertEqual(response.context["customer"], self.customer)
|
2016-01-29 10:40:14 +01:00
|
|
|
|
|
|
|
def test_form_valid_redirect(self):
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.post(self._get_url(self.database))
|
|
|
|
self.assertRedirects(response, self.package.get_absolute_url())
|
|
|
|
|
|
|
|
def test_form_valid_message(self):
|
|
|
|
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
|
|
|
|
response = self.client.post(self._get_url(self.database), follow=True)
|
2019-01-30 21:27:25 +01:00
|
|
|
messages = list(response.context["messages"])
|
2016-01-29 10:40:14 +01:00
|
|
|
self.assertEqual(len(messages), 1)
|
|
|
|
self.assertEqual(str(messages[0]), "Database deleted.")
|