2015-12-07 01:22:13 +01:00
|
|
|
"""
|
|
|
|
This module provides tests for :py:mod:`userdbs.forms`.
|
|
|
|
|
|
|
|
"""
|
|
|
|
from django import forms
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.test import TestCase
|
2019-01-30 21:27:25 +01:00
|
|
|
from django.urls import reverse
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
from userdbs.forms import AddUserDatabaseForm, ChangeDatabaseUserPasswordForm
|
|
|
|
from userdbs.models import DB_TYPES
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
from unittest.mock import MagicMock, Mock, patch
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
Customer = get_user_model()
|
|
|
|
|
|
|
|
|
|
|
|
class AddUserDatabaseFormTest(TestCase):
|
|
|
|
"""
|
|
|
|
Test class for :py:class:`userdbs.forms.AddUserDatabaseForm`.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _setup_hostingpackage(self):
|
|
|
|
self.hostingpackage = Mock(id=42)
|
|
|
|
|
|
|
|
def test_constructor_needs_hostingpackage(self):
|
|
|
|
with self.assertRaises(KeyError) as ke:
|
|
|
|
AddUserDatabaseForm(instance=Mock())
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(ke.exception.args[0], "hostingpackage")
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_constructor_needs_dbtypes(self):
|
|
|
|
with self.assertRaises(KeyError) as ke:
|
|
|
|
AddUserDatabaseForm(instance=Mock(), hostingpackage=Mock())
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(ke.exception.args[0], "dbtypes")
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_constructor_one_dbtype(self):
|
|
|
|
self._setup_hostingpackage()
|
|
|
|
dbtypes = [(DB_TYPES.pgsql, DB_TYPES[DB_TYPES.pgsql])]
|
|
|
|
form = AddUserDatabaseForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=MagicMock(), hostingpackage=self.hostingpackage, dbtypes=dbtypes
|
|
|
|
)
|
|
|
|
self.assertIn("db_type", form.fields)
|
|
|
|
self.assertEqual(form.fields["db_type"].choices, dbtypes)
|
|
|
|
self.assertTrue(isinstance(form.fields["db_type"].widget, forms.HiddenInput))
|
|
|
|
self.assertTrue(hasattr(form, "helper"))
|
|
|
|
self.assertEqual(
|
|
|
|
form.helper.form_action,
|
|
|
|
reverse("add_userdatabase", kwargs={"package": self.hostingpackage.id}),
|
|
|
|
)
|
|
|
|
self.assertEqual(form.helper.inputs[0].name, "submit")
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_constructor_multiple_dbtypes(self):
|
|
|
|
self._setup_hostingpackage()
|
|
|
|
dbtypes = [
|
|
|
|
(DB_TYPES.pgsql, DB_TYPES[DB_TYPES.pgsql]),
|
2019-01-30 21:27:25 +01:00
|
|
|
(DB_TYPES.mysql, DB_TYPES[DB_TYPES.mysql]),
|
2015-12-07 01:22:13 +01:00
|
|
|
]
|
|
|
|
form = AddUserDatabaseForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=MagicMock(), hostingpackage=self.hostingpackage, dbtypes=dbtypes
|
|
|
|
)
|
|
|
|
self.assertIn("db_type", form.fields)
|
|
|
|
self.assertEqual(form.fields["db_type"].choices, dbtypes)
|
|
|
|
self.assertTrue(isinstance(form.fields["db_type"].widget, forms.RadioSelect))
|
|
|
|
self.assertTrue(hasattr(form, "helper"))
|
|
|
|
self.assertEqual(
|
|
|
|
form.helper.form_action,
|
|
|
|
reverse("add_userdatabase", kwargs={"package": self.hostingpackage.id}),
|
|
|
|
)
|
|
|
|
self.assertEqual(form.helper.inputs[0].name, "submit")
|
|
|
|
|
|
|
|
@patch("userdbs.forms.UserDatabase.objects.create_userdatabase_with_user")
|
2015-12-07 01:22:13 +01:00
|
|
|
def test_save(self, create_userdatabase_with_user):
|
|
|
|
self._setup_hostingpackage()
|
|
|
|
dbtypes = [
|
|
|
|
(DB_TYPES.pgsql, DB_TYPES[DB_TYPES.pgsql]),
|
2019-01-30 21:27:25 +01:00
|
|
|
(DB_TYPES.mysql, DB_TYPES[DB_TYPES.mysql]),
|
2015-12-07 01:22:13 +01:00
|
|
|
]
|
|
|
|
form = AddUserDatabaseForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=MagicMock(), hostingpackage=self.hostingpackage, dbtypes=dbtypes
|
|
|
|
)
|
|
|
|
form.cleaned_data = {"db_type": DB_TYPES.pgsql, "password1": "secret"}
|
2015-12-07 01:22:13 +01:00
|
|
|
form.save()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(
|
|
|
|
create_userdatabase_with_user.called_with(
|
|
|
|
DB_TYPES.pgsql,
|
|
|
|
self.hostingpackage.osuser,
|
|
|
|
password="secret",
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
)
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ChangeDatabaseUserPasswordFormTest(TestCase):
|
|
|
|
"""
|
|
|
|
Test class for :py:class:`userdbs.forms.ChangeDatabaseUserPasswordForm`.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _setup_hostingpackage(self):
|
|
|
|
self.hostingpackage = Mock(id=42)
|
|
|
|
|
|
|
|
def test_constructor_needs_hostingpackage(self):
|
|
|
|
with self.assertRaises(KeyError) as ke:
|
|
|
|
ChangeDatabaseUserPasswordForm(instance=Mock())
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(ke.exception.args[0], "hostingpackage")
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_constructor(self):
|
|
|
|
self._setup_hostingpackage()
|
|
|
|
instance = MagicMock()
|
2019-01-30 21:27:25 +01:00
|
|
|
instance.name = "test"
|
2015-12-07 01:22:13 +01:00
|
|
|
form = ChangeDatabaseUserPasswordForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance, hostingpackage=self.hostingpackage
|
|
|
|
)
|
|
|
|
self.assertIn("password1", form.fields)
|
|
|
|
self.assertIn("password2", form.fields)
|
|
|
|
self.assertTrue(hasattr(form, "helper"))
|
|
|
|
self.assertEqual(
|
|
|
|
form.helper.form_action,
|
|
|
|
reverse("change_dbuser_password", kwargs={"slug": "test", "package": 42}),
|
|
|
|
)
|
|
|
|
self.assertEqual(form.helper.inputs[0].name, "submit")
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_save(self):
|
|
|
|
instance = MagicMock()
|
2019-01-30 21:27:25 +01:00
|
|
|
instance.name = "test"
|
2015-12-07 01:22:13 +01:00
|
|
|
self._setup_hostingpackage()
|
|
|
|
form = ChangeDatabaseUserPasswordForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance, hostingpackage=self.hostingpackage
|
|
|
|
)
|
|
|
|
form.cleaned_data = {"password1": "secret"}
|
2015-12-07 01:22:13 +01:00
|
|
|
form.save()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(instance.set_password.called_with("secret"))
|