Add tests for userdbs.views.ChangeDatabaseUserPassword

This commit adds tests for the ChangeDatabaseUserPassword view.
This commit is contained in:
Jan Dittberner 2016-01-29 10:03:24 +00:00
parent 1f700fc06a
commit 0a0524f1f0

View file

@ -21,7 +21,7 @@ from hostingpackages.models import (
)
from userdbs.models import DB_TYPES, UserDatabase
from userdbs.views import AddUserDatabase
from userdbs.views import AddUserDatabase, ChangeDatabaseUserPassword
User = get_user_model()
@ -174,18 +174,89 @@ class AddUserDatabaseTest(
dbuser=db.db_user))
class ChangeDatabaseUserPasswordTest(HostingPackageAwareTestMixin, TestCase):
class ChangeDatabaseUserPasswordTest(
HostingPackageAwareTestMixin, CustomerUserDatabaseOptionAwareTestMixin,
TestCase
):
def setUp(self):
self.customer = User.objects.create_user(
username=TEST_USER, password=TEST_PASSWORD)
self.package = self._setup_hosting_package(self.customer)
template = self._create_userdatabase_option()
database = UserDatabase.objects.create_userdatabase_with_user(
template.db_type, self.package.osuser)
self.dbuser = database.db_user
def _get_url(self, userdatabase):
def _get_url(self, dbuser):
return reverse(
'change_dbuser_password', kwargs={
'package': self.package.id,
'slug': userdatabase.db_name})
'package': self.package.id, 'slug': dbuser.name})
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):
User.objects.create_user(
'test2', password=TEST_PASSWORD)
self.client.login(username='test2', password=TEST_PASSWORD)
response = self.client.get(self._get_url(self.dbuser))
self.assertEqual(response.status_code, 403)
def test_get_staff_user(self):
User.objects.create_superuser(
'admin', email=TEST_EMAIL, password=TEST_PASSWORD)
self.client.login(username='admin', password=TEST_PASSWORD)
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))
self.assertTemplateUsed(
response, 'userdbs/databaseuser_setpassword.html')
def test_get_form_kwargs(self):
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
view = ChangeDatabaseUserPassword(request=MagicMock(), kwargs={
'package': str(self.package.pk), 'slug': self.dbuser.name})
the_kwargs = view.get_form_kwargs()
self.assertIn('hostingpackage', the_kwargs)
self.assertEqual(the_kwargs['hostingpackage'], self.package)
def test_get_context_data(self):
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
response = self.client.get(self._get_url(self.dbuser))
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)
def test_form_valid_redirect(self):
self.client.login(username=TEST_USER, password=TEST_PASSWORD)
response = self.client.post(self._get_url(self.dbuser), data={
'password1': TEST_PASSWORD, 'password2': TEST_PASSWORD})
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.dbuser), follow=True, data={
'password1': TEST_PASSWORD, 'password2': TEST_PASSWORD})
messages = list(response.context['messages'])
self.assertEqual(len(messages), 1)
self.assertEqual(
str(messages[0]),
'Successfully changed password of database user {dbuser}.'.format(
dbuser=self.dbuser.name))
class DeleteUserDatabaseTest(