Add tests for userdbs app
This commit adds a set of unit tests for the userdbs app. Some tests will fail because a refactoring to signals comes with the next commit.
This commit is contained in:
parent
e96aac82fc
commit
1649e4592e
7 changed files with 713 additions and 0 deletions
0
gnuviechadmin/userdbs/tests/__init__.py
Normal file
0
gnuviechadmin/userdbs/tests/__init__.py
Normal file
0
gnuviechadmin/userdbs/tests/templatetags/__init__.py
Normal file
0
gnuviechadmin/userdbs/tests/templatetags/__init__.py
Normal file
45
gnuviechadmin/userdbs/tests/templatetags/test_userdb.py
Normal file
45
gnuviechadmin/userdbs/tests/templatetags/test_userdb.py
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
"""
|
||||||
|
This module provides tests for the functions in
|
||||||
|
:py:mod:`userdbs.templatetags.userdb`.
|
||||||
|
|
||||||
|
"""
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from unittest import TestCase
|
||||||
|
|
||||||
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
|
from userdbs.models import DB_TYPES
|
||||||
|
from userdbs.templatetags.userdb import db_type_icon_class, db_type_name
|
||||||
|
|
||||||
|
|
||||||
|
class UserdbTemplateTagTests(TestCase):
|
||||||
|
"""
|
||||||
|
Test suite for :py:mod:`userdbs.templatetags.userdb` functions.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_db_type_icon_class_unknown(self):
|
||||||
|
self.assertEqual(
|
||||||
|
db_type_icon_class({'db_type': 'unknown'}),
|
||||||
|
'icon-database')
|
||||||
|
|
||||||
|
def test_db_type_icon_class_mysql(self):
|
||||||
|
self.assertEqual(
|
||||||
|
db_type_icon_class({'db_type': DB_TYPES.mysql}),
|
||||||
|
'icon-mysql')
|
||||||
|
|
||||||
|
def test_db_type_icon_class_pgsql(self):
|
||||||
|
self.assertEqual(
|
||||||
|
db_type_icon_class({'db_type': DB_TYPES.pgsql}),
|
||||||
|
'icon-postgres')
|
||||||
|
|
||||||
|
def test_db_type_name_mysql(self):
|
||||||
|
self.assertEqual(
|
||||||
|
db_type_name({'db_type': DB_TYPES.mysql}),
|
||||||
|
_(DB_TYPES[DB_TYPES.mysql]))
|
||||||
|
|
||||||
|
def test_db_type_name_pgsql(self):
|
||||||
|
self.assertEqual(
|
||||||
|
db_type_name({'db_type': DB_TYPES.pgsql}),
|
||||||
|
_(DB_TYPES[DB_TYPES.pgsql]))
|
172
gnuviechadmin/userdbs/tests/test_admin.py
Normal file
172
gnuviechadmin/userdbs/tests/test_admin.py
Normal file
|
@ -0,0 +1,172 @@
|
||||||
|
"""
|
||||||
|
This module provides tests for :py:mod:`userdbs.admin`.
|
||||||
|
|
||||||
|
"""
|
||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from django.contrib.admin import AdminSite
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from userdbs.admin import (DatabaseUserAdmin, DatabaseUserCreationForm,
|
||||||
|
UserDatabaseAdmin, UserDatabaseCreationForm)
|
||||||
|
from userdbs.models import DB_TYPES, DatabaseUser, UserDatabase
|
||||||
|
|
||||||
|
try:
|
||||||
|
from unittest.mock import MagicMock, Mock, patch
|
||||||
|
except ImportError:
|
||||||
|
from mock import MagicMock, Mock, patch
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseUserCreationFormTest(TestCase):
|
||||||
|
|
||||||
|
@patch('userdbs.admin.DatabaseUser.objects.create_database_user')
|
||||||
|
def test_save(self, create_database_user):
|
||||||
|
create_database_user.return_value = Mock()
|
||||||
|
form = DatabaseUserCreationForm()
|
||||||
|
mockuser = Mock(name='osuser')
|
||||||
|
form.cleaned_data = {
|
||||||
|
'osuser': mockuser,
|
||||||
|
'db_type': DB_TYPES.pgsql
|
||||||
|
}
|
||||||
|
retval = form.save()
|
||||||
|
self.assertTrue(create_database_user.called_with(
|
||||||
|
osuser=mockuser, db_type=DB_TYPES.pgsql, commit=True))
|
||||||
|
self.assertEqual(retval, create_database_user.return_value)
|
||||||
|
|
||||||
|
def test_save_m2m_returns_none(self):
|
||||||
|
form = DatabaseUserCreationForm()
|
||||||
|
self.assertIsNone(form.save_m2m())
|
||||||
|
|
||||||
|
|
||||||
|
class UserDatabaseCreationFormTest(TestCase):
|
||||||
|
|
||||||
|
@patch('userdbs.admin.UserDatabase.objects.create_userdatabase')
|
||||||
|
def test_save(self, create_userdatabase):
|
||||||
|
create_userdatabase.return_value = Mock()
|
||||||
|
form = UserDatabaseCreationForm()
|
||||||
|
mockuser = Mock(name='mockuser')
|
||||||
|
form.cleaned_data = {'db_user': mockuser}
|
||||||
|
retval = form.save()
|
||||||
|
self.assertTrue(create_userdatabase.called_with(
|
||||||
|
db_user=mockuser, commit=True))
|
||||||
|
self.assertEqual(retval, create_userdatabase.return_value)
|
||||||
|
|
||||||
|
def test_save_m2m_returns_none(self):
|
||||||
|
form = UserDatabaseCreationForm()
|
||||||
|
self.assertIsNone(form.save_m2m())
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseUserAdminTest(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
site = AdminSite()
|
||||||
|
self.dbuadmin = DatabaseUserAdmin(DatabaseUser, site)
|
||||||
|
super(DatabaseUserAdminTest, self).setUp()
|
||||||
|
|
||||||
|
def test_get_form_with_instance(self):
|
||||||
|
form = self.dbuadmin.get_form(
|
||||||
|
Mock(name='request'), obj=Mock(name='dbuser'))
|
||||||
|
self.assertEqual(
|
||||||
|
form.Meta.fields,
|
||||||
|
['osuser', 'name', 'db_type']
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_get_form_without_instance(self):
|
||||||
|
form = self.dbuadmin.get_form(Mock(name='request'))
|
||||||
|
self.assertEqual(
|
||||||
|
form.Meta.fields,
|
||||||
|
['osuser', 'db_type']
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_get_readonly_fields_with_instance(self):
|
||||||
|
fields = self.dbuadmin.get_readonly_fields(
|
||||||
|
Mock(name='request'), obj=Mock(name='dbuser'))
|
||||||
|
self.assertEqual(
|
||||||
|
fields, ['osuser', 'name', 'db_type'])
|
||||||
|
|
||||||
|
def test_get_readonly_fields_without_instance(self):
|
||||||
|
fields = self.dbuadmin.get_readonly_fields(
|
||||||
|
Mock(name='request'))
|
||||||
|
self.assertEqual(fields, [])
|
||||||
|
|
||||||
|
def test_save_model_change(self):
|
||||||
|
objmock = Mock()
|
||||||
|
self.dbuadmin.save_model(Mock(name='request'), objmock, Mock(), True)
|
||||||
|
self.assertTrue(objmock.create_in_database.not_called())
|
||||||
|
|
||||||
|
def test_save_model_no_change(self):
|
||||||
|
objmock = Mock()
|
||||||
|
self.dbuadmin.save_model(Mock(name='request'), objmock, Mock(), False)
|
||||||
|
self.assertTrue(objmock.create_in_database.called_with())
|
||||||
|
|
||||||
|
def test_perform_delete_selected(self):
|
||||||
|
usermock = Mock()
|
||||||
|
selected = Mock()
|
||||||
|
selected.all.return_value = [usermock]
|
||||||
|
self.dbuadmin.perform_delete_selected(Mock(name='request'), selected)
|
||||||
|
self.assertTrue(selected.all.called_with())
|
||||||
|
self.assertTrue(usermock.delete.called_with())
|
||||||
|
|
||||||
|
def test_get_actions(self):
|
||||||
|
requestmock = MagicMock(name='request')
|
||||||
|
self.assertNotIn(
|
||||||
|
'delete_selected',
|
||||||
|
self.dbuadmin.get_actions(requestmock))
|
||||||
|
self.assertIn(
|
||||||
|
'perform_delete_selected',
|
||||||
|
self.dbuadmin.get_actions(requestmock))
|
||||||
|
|
||||||
|
|
||||||
|
class UserDatabaseAdminTest(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
site = AdminSite()
|
||||||
|
self.udbadmin = UserDatabaseAdmin(UserDatabase, site)
|
||||||
|
super(UserDatabaseAdminTest, self).setUp()
|
||||||
|
|
||||||
|
def test_get_form_with_instance(self):
|
||||||
|
form = self.udbadmin.get_form(
|
||||||
|
Mock(name='request'), obj=Mock(name='userdb'))
|
||||||
|
self.assertEqual(form.Meta.fields, ['db_name', 'db_user'])
|
||||||
|
|
||||||
|
def test_get_form_without_instance(self):
|
||||||
|
form = self.udbadmin.get_form(Mock(name='request'))
|
||||||
|
self.assertEqual(form.Meta.fields, ['db_user'])
|
||||||
|
|
||||||
|
def test_get_readonly_fields_with_instance(self):
|
||||||
|
fields = self.udbadmin.get_readonly_fields(
|
||||||
|
Mock(name='request'), obj=Mock(name='userdb'))
|
||||||
|
self.assertEqual(
|
||||||
|
fields, ['db_name', 'db_user'])
|
||||||
|
|
||||||
|
def test_get_readonly_fields_without_instance(self):
|
||||||
|
fields = self.udbadmin.get_readonly_fields(
|
||||||
|
Mock(name='request'))
|
||||||
|
self.assertEqual(fields, [])
|
||||||
|
|
||||||
|
def test_save_model_change(self):
|
||||||
|
objmock = Mock()
|
||||||
|
self.udbadmin.save_model(Mock(name='request'), objmock, Mock(), True)
|
||||||
|
self.assertTrue(objmock.create_in_database.not_called())
|
||||||
|
|
||||||
|
def test_save_model_no_change(self):
|
||||||
|
objmock = Mock()
|
||||||
|
self.udbadmin.save_model(Mock(name='request'), objmock, Mock(), False)
|
||||||
|
self.assertTrue(objmock.create_in_database.called_with())
|
||||||
|
|
||||||
|
def test_perform_delete_selected(self):
|
||||||
|
userdbmock = Mock()
|
||||||
|
selected = Mock()
|
||||||
|
selected.all.return_value = [userdbmock]
|
||||||
|
self.udbadmin.perform_delete_selected(Mock(name='request'), selected)
|
||||||
|
self.assertTrue(selected.all.called_with())
|
||||||
|
self.assertTrue(userdbmock.delete.called_with())
|
||||||
|
|
||||||
|
def test_get_actions(self):
|
||||||
|
requestmock = MagicMock(name='request')
|
||||||
|
self.assertNotIn(
|
||||||
|
'delete_selected',
|
||||||
|
self.udbadmin.get_actions(requestmock))
|
||||||
|
self.assertIn(
|
||||||
|
'perform_delete_selected',
|
||||||
|
self.udbadmin.get_actions(requestmock))
|
135
gnuviechadmin/userdbs/tests/test_forms.py
Normal file
135
gnuviechadmin/userdbs/tests/test_forms.py
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
"""
|
||||||
|
This module provides tests for :py:mod:`userdbs.forms`.
|
||||||
|
|
||||||
|
"""
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django import forms
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.core.urlresolvers import reverse
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from userdbs.forms import AddUserDatabaseForm, ChangeDatabaseUserPasswordForm
|
||||||
|
from userdbs.models import DB_TYPES
|
||||||
|
|
||||||
|
try:
|
||||||
|
from unittest.mock import MagicMock, Mock, patch
|
||||||
|
except ImportError:
|
||||||
|
from mock import MagicMock, Mock, patch
|
||||||
|
|
||||||
|
|
||||||
|
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())
|
||||||
|
self.assertEqual(ke.exception.args[0], 'hostingpackage')
|
||||||
|
|
||||||
|
def test_constructor_needs_dbtypes(self):
|
||||||
|
with self.assertRaises(KeyError) as ke:
|
||||||
|
AddUserDatabaseForm(instance=Mock(), hostingpackage=Mock())
|
||||||
|
self.assertEqual(ke.exception.args[0], 'dbtypes')
|
||||||
|
|
||||||
|
def test_constructor_one_dbtype(self):
|
||||||
|
self._setup_hostingpackage()
|
||||||
|
dbtypes = [(DB_TYPES.pgsql, DB_TYPES[DB_TYPES.pgsql])]
|
||||||
|
form = AddUserDatabaseForm(
|
||||||
|
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')
|
||||||
|
|
||||||
|
def test_constructor_multiple_dbtypes(self):
|
||||||
|
self._setup_hostingpackage()
|
||||||
|
dbtypes = [
|
||||||
|
(DB_TYPES.pgsql, DB_TYPES[DB_TYPES.pgsql]),
|
||||||
|
(DB_TYPES.mysql, DB_TYPES[DB_TYPES.mysql])
|
||||||
|
]
|
||||||
|
form = AddUserDatabaseForm(
|
||||||
|
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')
|
||||||
|
def test_save(self, create_userdatabase_with_user):
|
||||||
|
self._setup_hostingpackage()
|
||||||
|
dbtypes = [
|
||||||
|
(DB_TYPES.pgsql, DB_TYPES[DB_TYPES.pgsql]),
|
||||||
|
(DB_TYPES.mysql, DB_TYPES[DB_TYPES.mysql])
|
||||||
|
]
|
||||||
|
form = AddUserDatabaseForm(
|
||||||
|
instance=MagicMock(), hostingpackage=self.hostingpackage,
|
||||||
|
dbtypes=dbtypes)
|
||||||
|
form.cleaned_data = {
|
||||||
|
'db_type': DB_TYPES.pgsql,
|
||||||
|
'password1': 'secret',
|
||||||
|
}
|
||||||
|
form.save()
|
||||||
|
self.assertTrue(create_userdatabase_with_user.called_with(
|
||||||
|
DB_TYPES.pgsql, self.hostingpackage.osuser,
|
||||||
|
password='secret', commit=True))
|
||||||
|
|
||||||
|
|
||||||
|
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())
|
||||||
|
self.assertEqual(ke.exception.args[0], 'hostingpackage')
|
||||||
|
|
||||||
|
def test_constructor(self):
|
||||||
|
self._setup_hostingpackage()
|
||||||
|
instance = MagicMock()
|
||||||
|
instance.name = 'test'
|
||||||
|
form = ChangeDatabaseUserPasswordForm(
|
||||||
|
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')
|
||||||
|
|
||||||
|
def test_save(self):
|
||||||
|
instance = MagicMock()
|
||||||
|
instance.name = 'test'
|
||||||
|
self._setup_hostingpackage()
|
||||||
|
form = ChangeDatabaseUserPasswordForm(
|
||||||
|
instance=instance, hostingpackage=self.hostingpackage)
|
||||||
|
form.cleaned_data = {'password1': 'secret'}
|
||||||
|
form.save()
|
||||||
|
self.assertTrue(instance.set_password.called_with('secret'))
|
295
gnuviechadmin/userdbs/tests/test_models.py
Normal file
295
gnuviechadmin/userdbs/tests/test_models.py
Normal file
|
@ -0,0 +1,295 @@
|
||||||
|
"""
|
||||||
|
This module provides tests for :py:mod:`userdbs.models`.
|
||||||
|
|
||||||
|
"""
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.test.utils import override_settings
|
||||||
|
|
||||||
|
from osusers.models import User
|
||||||
|
from taskresults.models import TaskResult
|
||||||
|
from userdbs.models import DB_TYPES, DatabaseUser, UserDatabase
|
||||||
|
|
||||||
|
Customer = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(
|
||||||
|
CELERY_ALWAYS_EAGER=True,
|
||||||
|
CELERY_CACHE_BACKEND='memory',
|
||||||
|
BROKER_BACKEND='memory'
|
||||||
|
)
|
||||||
|
class TestCaseWithCeleryTasks(TestCase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseUserManagerTest(TestCaseWithCeleryTasks):
|
||||||
|
"""
|
||||||
|
Test case for :py:class:`userdbs.models.DatabaseUserManager`.
|
||||||
|
|
||||||
|
"""
|
||||||
|
def setUp(self):
|
||||||
|
self.customer = Customer.objects.create_user(username='testcustomer')
|
||||||
|
self.osuser = User.objects.create_user(customer=self.customer)
|
||||||
|
TaskResult.objects.all().delete()
|
||||||
|
|
||||||
|
def test_create_database_user_with_name(self):
|
||||||
|
dbu = DatabaseUser.objects.create_database_user(
|
||||||
|
self.osuser, DB_TYPES.pgsql, 'testname', 'secret')
|
||||||
|
self.assertEqual(dbu.name, 'testname')
|
||||||
|
self.assertEqual(dbu.osuser, self.osuser)
|
||||||
|
self.assertEqual(dbu.db_type, DB_TYPES.pgsql)
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 1)
|
||||||
|
self.assertEqual(taskres[0].creator, 'handle_dbuser_created')
|
||||||
|
self.assertEqual(taskres[0].notes, 'pgsql user creation')
|
||||||
|
|
||||||
|
def test_create_database_user_with_name_no_commit(self):
|
||||||
|
dbu = DatabaseUser.objects.create_database_user(
|
||||||
|
self.osuser, DB_TYPES.pgsql, 'testname', 'secret', False)
|
||||||
|
self.assertEqual(dbu.name, 'testname')
|
||||||
|
self.assertEqual(dbu.osuser, self.osuser)
|
||||||
|
self.assertEqual(dbu.db_type, DB_TYPES.pgsql)
|
||||||
|
self.assertFalse(TaskResult.objects.exists())
|
||||||
|
|
||||||
|
def test_create_database_user_generate_name(self):
|
||||||
|
dbu = DatabaseUser.objects.create_database_user(
|
||||||
|
self.osuser, DB_TYPES.pgsql)
|
||||||
|
self.assertEqual(dbu.name, '{user}db01'.format(
|
||||||
|
user=self.osuser.username))
|
||||||
|
self.assertEqual(dbu.osuser, self.osuser)
|
||||||
|
self.assertEqual(dbu.db_type, DB_TYPES.pgsql)
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 1)
|
||||||
|
self.assertEqual(taskres[0].creator, 'handle_dbuser_created')
|
||||||
|
self.assertEqual(taskres[0].notes, 'pgsql user creation')
|
||||||
|
|
||||||
|
def test_create_database_user_multiple_generate_name(self):
|
||||||
|
dbu = DatabaseUser.objects.create_database_user(
|
||||||
|
self.osuser, DB_TYPES.mysql)
|
||||||
|
self.assertEqual(dbu.name, '{user}db01'.format(
|
||||||
|
user=self.osuser.username))
|
||||||
|
self.assertEqual(dbu.osuser, self.osuser)
|
||||||
|
self.assertEqual(dbu.db_type, DB_TYPES.mysql)
|
||||||
|
dbu = DatabaseUser.objects.create_database_user(
|
||||||
|
self.osuser, DB_TYPES.mysql)
|
||||||
|
self.assertEqual(dbu.name, '{user}db02'.format(
|
||||||
|
user=self.osuser.username))
|
||||||
|
self.assertEqual(dbu.osuser, self.osuser)
|
||||||
|
self.assertEqual(dbu.db_type, DB_TYPES.mysql)
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 2)
|
||||||
|
self.assertEqual(taskres[0].creator, 'handle_dbuser_created')
|
||||||
|
self.assertEqual(taskres[0].notes, 'mysql user creation')
|
||||||
|
self.assertEqual(taskres[1].creator, 'handle_dbuser_created')
|
||||||
|
self.assertEqual(taskres[1].notes, 'mysql user creation')
|
||||||
|
|
||||||
|
def test_create_database_user_multiple_gap_generate_name(self):
|
||||||
|
dbu = DatabaseUser.objects.create_database_user(
|
||||||
|
self.osuser, DB_TYPES.mysql)
|
||||||
|
self.assertEqual(dbu.name, '{user}db01'.format(
|
||||||
|
user=self.osuser.username))
|
||||||
|
self.assertEqual(dbu.osuser, self.osuser)
|
||||||
|
self.assertEqual(dbu.db_type, DB_TYPES.mysql)
|
||||||
|
dbu = DatabaseUser.objects.create_database_user(
|
||||||
|
self.osuser, DB_TYPES.mysql)
|
||||||
|
self.assertEqual(dbu.name, '{user}db02'.format(
|
||||||
|
user=self.osuser.username))
|
||||||
|
self.assertEqual(dbu.osuser, self.osuser)
|
||||||
|
self.assertEqual(dbu.db_type, DB_TYPES.mysql)
|
||||||
|
DatabaseUser.objects.get(
|
||||||
|
name='{user}db01'.format(user=self.osuser.username)).delete()
|
||||||
|
dbu = DatabaseUser.objects.create_database_user(
|
||||||
|
self.osuser, DB_TYPES.mysql)
|
||||||
|
self.assertEqual(dbu.name, '{user}db01'.format(
|
||||||
|
user=self.osuser.username))
|
||||||
|
self.assertEqual(dbu.osuser, self.osuser)
|
||||||
|
self.assertEqual(dbu.db_type, DB_TYPES.mysql)
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 4)
|
||||||
|
self.assertEqual(taskres[0].creator, 'handle_dbuser_created')
|
||||||
|
self.assertEqual(taskres[0].notes, 'mysql user creation')
|
||||||
|
self.assertEqual(taskres[1].creator, 'handle_dbuser_created')
|
||||||
|
self.assertEqual(taskres[1].notes, 'mysql user creation')
|
||||||
|
self.assertEqual(taskres[2].creator, 'handle_dbuser_deleted')
|
||||||
|
self.assertEqual(taskres[2].notes, 'mysql user deletion')
|
||||||
|
self.assertEqual(taskres[3].creator, 'handle_dbuser_created')
|
||||||
|
self.assertEqual(taskres[3].notes, 'mysql user creation')
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseUserTest(TestCaseWithCeleryTasks):
|
||||||
|
"""
|
||||||
|
Test case for :py:class:`userdbs.models.DatabaseUser`.
|
||||||
|
|
||||||
|
"""
|
||||||
|
def setUp(self):
|
||||||
|
self.customer = Customer.objects.create_user(username='testcustomer')
|
||||||
|
self.osuser = User.objects.create_user(customer=self.customer)
|
||||||
|
self.dbu = DatabaseUser.objects.create_database_user(
|
||||||
|
self.osuser, DB_TYPES.pgsql)
|
||||||
|
TaskResult.objects.all().delete()
|
||||||
|
|
||||||
|
def test___str__(self):
|
||||||
|
self.assertEqual(
|
||||||
|
str(self.dbu),
|
||||||
|
'{user}db01 (PostgreSQL for {user})'.format(
|
||||||
|
user=self.osuser.username))
|
||||||
|
|
||||||
|
def test_set_password_pgsql(self):
|
||||||
|
self.dbu.set_password('secret')
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 1)
|
||||||
|
self.assertEqual(taskres[0].creator, 'handle_dbuser_password_set')
|
||||||
|
self.assertEqual(taskres[0].notes, 'pgsql password change')
|
||||||
|
|
||||||
|
def test_set_password_mysql(self):
|
||||||
|
self.dbu.db_type = DB_TYPES.mysql
|
||||||
|
self.dbu.save()
|
||||||
|
self.dbu.set_password('secret')
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 1)
|
||||||
|
self.assertEqual(taskres[0].creator, 'handle_dbuser_password_set')
|
||||||
|
self.assertEqual(taskres[0].notes, 'mysql password change')
|
||||||
|
|
||||||
|
def test_delete_no_dbs(self):
|
||||||
|
self.dbu.delete()
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 1)
|
||||||
|
self.assertEqual(taskres[0].creator, 'handle_dbuser_deleted')
|
||||||
|
self.assertEqual(taskres[0].notes, 'pgsql user deletion')
|
||||||
|
|
||||||
|
def test_delete_with_dbs(self):
|
||||||
|
db = UserDatabase.objects.create_userdatabase(self.dbu)
|
||||||
|
dbid = db.id
|
||||||
|
self.dbu.delete()
|
||||||
|
self.assertFalse(UserDatabase.objects.filter(id=dbid).exists())
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 3)
|
||||||
|
self.assertEqual(taskres[0].creator, 'handle_userdb_created')
|
||||||
|
self.assertEqual(taskres[0].notes, 'pgsql database creation')
|
||||||
|
self.assertEqual(taskres[1].creator, 'handle_userdb_deleted')
|
||||||
|
self.assertEqual(taskres[1].notes, 'pgsql database deletion')
|
||||||
|
self.assertEqual(taskres[2].creator, 'handle_dbuser_deleted')
|
||||||
|
self.assertEqual(taskres[2].notes, 'pgsql user deletion')
|
||||||
|
|
||||||
|
|
||||||
|
class UserDatabaseManagerTest(TestCaseWithCeleryTasks):
|
||||||
|
"""
|
||||||
|
Test case for :py:class:`userdbs.models.UserDatabaseManager`.
|
||||||
|
|
||||||
|
"""
|
||||||
|
def setUp(self):
|
||||||
|
self.customer = Customer.objects.create_user(username='testcustomer')
|
||||||
|
self.osuser = User.objects.create_user(customer=self.customer)
|
||||||
|
TaskResult.objects.all().delete()
|
||||||
|
|
||||||
|
def _create_database_user(self, dbtype):
|
||||||
|
self.dbu = DatabaseUser.objects.create_database_user(
|
||||||
|
self.osuser, dbtype)
|
||||||
|
TaskResult.objects.all().delete()
|
||||||
|
|
||||||
|
def test_create_userdatabase_with_user_mysql(self):
|
||||||
|
db = UserDatabase.objects.create_userdatabase_with_user(
|
||||||
|
DB_TYPES.mysql, self.osuser)
|
||||||
|
self.assertEqual(db.db_name, '{user}db01'.format(
|
||||||
|
user=self.osuser.username))
|
||||||
|
self.assertEqual(db.db_user.name, '{user}db01'.format(
|
||||||
|
user=self.osuser.username))
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 2)
|
||||||
|
self.assertEqual(taskres[0].creator, 'handle_dbuser_created')
|
||||||
|
self.assertEqual(taskres[0].notes, 'mysql user creation')
|
||||||
|
self.assertEqual(taskres[1].creator, 'handle_userdb_created')
|
||||||
|
self.assertEqual(taskres[1].notes, 'mysql database creation')
|
||||||
|
|
||||||
|
def test_create_userdatabase_with_user_pgsql(self):
|
||||||
|
db = UserDatabase.objects.create_userdatabase_with_user(
|
||||||
|
DB_TYPES.pgsql, self.osuser)
|
||||||
|
self.assertEqual(db.db_name, '{user}db01'.format(
|
||||||
|
user=self.osuser.username))
|
||||||
|
self.assertEqual(db.db_user.name, '{user}db01'.format(
|
||||||
|
user=self.osuser.username))
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 2)
|
||||||
|
self.assertEqual(taskres[0].creator, 'handle_dbuser_created')
|
||||||
|
self.assertEqual(taskres[0].notes, 'pgsql user creation')
|
||||||
|
self.assertEqual(taskres[1].creator, 'handle_userdb_created')
|
||||||
|
self.assertEqual(taskres[1].notes, 'pgsql database creation')
|
||||||
|
|
||||||
|
def test_create_userdatabase_given_name_no_new_write(self):
|
||||||
|
self._create_database_user(DB_TYPES.pgsql)
|
||||||
|
db = UserDatabase.objects.create_userdatabase(self.dbu, db_name='test')
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 1)
|
||||||
|
self.assertEqual(db.db_name, 'test')
|
||||||
|
TaskResult.objects.all().delete()
|
||||||
|
db.save()
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 0)
|
||||||
|
|
||||||
|
def test_create_userdatabase_given_name(self):
|
||||||
|
self._create_database_user(DB_TYPES.pgsql)
|
||||||
|
db = UserDatabase.objects.create_userdatabase(self.dbu, db_name='test')
|
||||||
|
self.assertEqual(db.db_name, 'test')
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 1)
|
||||||
|
self.assertEqual(db.db_name, 'test')
|
||||||
|
|
||||||
|
def test_create_userdatabase_generate_name_no_commit(self):
|
||||||
|
self._create_database_user(DB_TYPES.pgsql)
|
||||||
|
db = UserDatabase.objects.create_userdatabase(self.dbu, commit=False)
|
||||||
|
self.assertEqual(db.db_name, self.dbu.name)
|
||||||
|
self.assertFalse(TaskResult.objects.all().exists())
|
||||||
|
|
||||||
|
def test_create_userdatabase_generate_name(self):
|
||||||
|
self._create_database_user(DB_TYPES.pgsql)
|
||||||
|
db = UserDatabase.objects.create_userdatabase(self.dbu)
|
||||||
|
self.assertEqual(db.db_name, self.dbu.name)
|
||||||
|
|
||||||
|
def test_create_userdatabase_multiple_generate_name(self):
|
||||||
|
self._create_database_user(DB_TYPES.pgsql)
|
||||||
|
db = UserDatabase.objects.create_userdatabase(self.dbu)
|
||||||
|
self.assertEqual(db.db_name, self.dbu.name)
|
||||||
|
db = UserDatabase.objects.create_userdatabase(self.dbu)
|
||||||
|
self.assertEqual(db.db_name, '{user}_02'.format(user=self.dbu.name))
|
||||||
|
|
||||||
|
def test_create_userdatabase_multiple_gap_generate_name(self):
|
||||||
|
self._create_database_user(DB_TYPES.pgsql)
|
||||||
|
db = UserDatabase.objects.create_userdatabase(self.dbu)
|
||||||
|
self.assertEqual(db.db_name, self.dbu.name)
|
||||||
|
dbx = UserDatabase.objects.create_userdatabase(self.dbu)
|
||||||
|
self.assertEqual(dbx.db_name, '{user}_02'.format(user=self.dbu.name))
|
||||||
|
db = UserDatabase.objects.create_userdatabase(self.dbu)
|
||||||
|
self.assertEqual(db.db_name, '{user}_03'.format(user=self.dbu.name))
|
||||||
|
dbx.delete()
|
||||||
|
db = UserDatabase.objects.create_userdatabase(self.dbu)
|
||||||
|
self.assertEqual(db.db_name, '{user}_02'.format(user=self.dbu.name))
|
||||||
|
|
||||||
|
|
||||||
|
class UserDatabaseTest(TestCaseWithCeleryTasks):
|
||||||
|
"""
|
||||||
|
Test case for :py:class:`userdbs.models.UserDabase`.
|
||||||
|
|
||||||
|
"""
|
||||||
|
def test___str__(self):
|
||||||
|
customer = Customer.objects.create_user(username='testcustomer')
|
||||||
|
osuser = User.objects.create_user(customer=customer)
|
||||||
|
db = UserDatabase.objects.create_userdatabase_with_user(
|
||||||
|
DB_TYPES.pgsql, osuser)
|
||||||
|
self.assertEqual(
|
||||||
|
str(db),
|
||||||
|
'{user}db01 ({dbuser})'.format(
|
||||||
|
user=osuser.username, dbuser=db.db_user))
|
||||||
|
|
||||||
|
def test_delete_mysql_db(self):
|
||||||
|
customer = Customer.objects.create_user(username='testcustomer')
|
||||||
|
osuser = User.objects.create_user(customer=customer)
|
||||||
|
TaskResult.objects.all().delete()
|
||||||
|
db = UserDatabase.objects.create_userdatabase_with_user(
|
||||||
|
DB_TYPES.mysql, osuser)
|
||||||
|
db.delete()
|
||||||
|
taskres = TaskResult.objects.all()
|
||||||
|
self.assertEqual(len(taskres), 3)
|
||||||
|
self.assertEqual(taskres[2].creator, 'handle_userdb_deleted')
|
||||||
|
self.assertEqual(taskres[2].notes, 'mysql database deletion')
|
66
gnuviechadmin/userdbs/tests/test_signals.py
Normal file
66
gnuviechadmin/userdbs/tests/test_signals.py
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
"""
|
||||||
|
This module contains explicit tests for corner cases in
|
||||||
|
:py:mod:`userdbs.signals` that are not handled by the tests in
|
||||||
|
:py:mod:`userdbs.tests.test_models`.
|
||||||
|
|
||||||
|
"""
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.test.utils import override_settings
|
||||||
|
|
||||||
|
from taskresults.models import TaskResult
|
||||||
|
from userdbs.signals import (handle_dbuser_created, handle_dbuser_deleted,
|
||||||
|
handle_dbuser_password_set, handle_userdb_created,
|
||||||
|
handle_userdb_deleted)
|
||||||
|
|
||||||
|
try:
|
||||||
|
from unittest.mock import Mock
|
||||||
|
except ImportError:
|
||||||
|
from mock import Mock
|
||||||
|
|
||||||
|
|
||||||
|
@override_settings(
|
||||||
|
CELERY_ALWAYS_EAGER=True,
|
||||||
|
CELERY_CACHE_BACKEND='memory',
|
||||||
|
BROKER_BACKEND='memory'
|
||||||
|
)
|
||||||
|
class TestCaseWithCeleryTasks(TestCase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class TestWithUnknownDBType(TestCaseWithCeleryTasks):
|
||||||
|
|
||||||
|
def test_handle_dbuser_password_set_unknown(self):
|
||||||
|
instance = Mock(data={'name': 'test', 'db_type': -1})
|
||||||
|
handle_dbuser_password_set(Mock(name='sender'), instance, 'secret')
|
||||||
|
self.assertFalse(TaskResult.objects.exists())
|
||||||
|
|
||||||
|
def test_handle_dbuser_create_unknown(self):
|
||||||
|
instance = Mock(data={'name': 'test', 'db_type': -1})
|
||||||
|
handle_dbuser_created(
|
||||||
|
Mock(name='sender'), instance, True, password='secret')
|
||||||
|
self.assertFalse(TaskResult.objects.exists())
|
||||||
|
|
||||||
|
def test_handle_dbuser_deleted_unknown(self):
|
||||||
|
instance = Mock(data={'name': 'test', 'db_type': -1})
|
||||||
|
handle_dbuser_deleted(Mock(name='sender'), instance)
|
||||||
|
self.assertFalse(TaskResult.objects.exists())
|
||||||
|
|
||||||
|
def test_handle_userdb_created_unknown(self):
|
||||||
|
instance = Mock(
|
||||||
|
data={
|
||||||
|
'db_name': 'test',
|
||||||
|
'db_user': Mock(data={'name': 'test', 'db_type': -1, })
|
||||||
|
})
|
||||||
|
handle_userdb_created(Mock(name='sender'), instance, True)
|
||||||
|
self.assertFalse(TaskResult.objects.exists())
|
||||||
|
|
||||||
|
def test_handle_userdb_deleted_unknown(self):
|
||||||
|
instance = Mock(
|
||||||
|
data={
|
||||||
|
'db_name': 'test',
|
||||||
|
'db_user': Mock(data={'name': 'test', 'db_type': -1, })
|
||||||
|
})
|
||||||
|
handle_userdb_deleted(Mock(name='sender'), instance)
|
||||||
|
self.assertFalse(TaskResult.objects.exists())
|
Loading…
Reference in a new issue