2015-12-07 01:22:13 +01:00
|
|
|
"""
|
|
|
|
This module provides tests for :py:mod:`userdbs.admin`.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
from unittest.mock import MagicMock, Mock, patch
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
from django.contrib.admin import AdminSite
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
from userdbs.admin import (
|
|
|
|
DatabaseUserAdmin,
|
|
|
|
DatabaseUserCreationForm,
|
|
|
|
UserDatabaseAdmin,
|
|
|
|
UserDatabaseCreationForm,
|
|
|
|
)
|
2015-12-07 01:22:13 +01:00
|
|
|
from userdbs.models import DB_TYPES, DatabaseUser, UserDatabase
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseUserCreationFormTest(TestCase):
|
2019-01-30 21:27:25 +01:00
|
|
|
@patch("userdbs.admin.DatabaseUser.objects.create_database_user")
|
2015-12-07 01:22:13 +01:00
|
|
|
def test_save(self, create_database_user):
|
|
|
|
create_database_user.return_value = Mock()
|
|
|
|
form = DatabaseUserCreationForm()
|
2019-01-30 21:27:25 +01:00
|
|
|
mockuser = Mock(name="osuser")
|
|
|
|
form.cleaned_data = {"osuser": mockuser, "db_type": DB_TYPES.pgsql}
|
2015-12-07 01:22:13 +01:00
|
|
|
retval = form.save()
|
2024-08-23 17:57:40 +02:00
|
|
|
create_database_user.assert_called_with(
|
|
|
|
osuser=mockuser, db_type=DB_TYPES.pgsql, commit=True
|
2019-01-30 21:27:25 +01:00
|
|
|
)
|
2015-12-07 01:22:13 +01:00
|
|
|
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):
|
2019-01-30 21:27:25 +01:00
|
|
|
@patch("userdbs.admin.UserDatabase.objects.create_userdatabase")
|
2015-12-07 01:22:13 +01:00
|
|
|
def test_save(self, create_userdatabase):
|
|
|
|
create_userdatabase.return_value = Mock()
|
|
|
|
form = UserDatabaseCreationForm()
|
2019-01-30 21:27:25 +01:00
|
|
|
mockuser = Mock(name="mockuser")
|
|
|
|
form.cleaned_data = {"db_user": mockuser}
|
2015-12-07 01:22:13 +01:00
|
|
|
retval = form.save()
|
2024-08-23 17:57:40 +02:00
|
|
|
create_userdatabase.assert_called_with(db_user=mockuser, commit=True)
|
2015-12-07 01:22:13 +01:00
|
|
|
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):
|
2019-01-30 21:27:25 +01:00
|
|
|
form = self.dbuadmin.get_form(Mock(name="request"), obj=Mock(name="dbuser"))
|
|
|
|
self.assertEqual(form.Meta.fields, ["osuser", "name", "db_type"])
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_get_form_without_instance(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
form = self.dbuadmin.get_form(Mock(name="request"))
|
|
|
|
self.assertEqual(form.Meta.fields, ["osuser", "db_type"])
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_get_readonly_fields_with_instance(self):
|
|
|
|
fields = self.dbuadmin.get_readonly_fields(
|
2019-01-30 21:27:25 +01:00
|
|
|
Mock(name="request"), obj=Mock(name="dbuser")
|
|
|
|
)
|
|
|
|
self.assertEqual(fields, ["osuser", "name", "db_type"])
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_get_readonly_fields_without_instance(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
fields = self.dbuadmin.get_readonly_fields(Mock(name="request"))
|
2015-12-07 01:22:13 +01:00
|
|
|
self.assertEqual(fields, [])
|
|
|
|
|
|
|
|
def test_save_model_change(self):
|
|
|
|
objmock = Mock()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.dbuadmin.save_model(Mock(name="request"), objmock, Mock(), True)
|
2024-08-23 17:57:40 +02:00
|
|
|
objmock.create_in_database.assert_not_called()
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_save_model_no_change(self):
|
|
|
|
objmock = Mock()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.dbuadmin.save_model(Mock(name="request"), objmock, Mock(), False)
|
2024-08-23 17:57:40 +02:00
|
|
|
objmock.create_in_database.assert_called_with()
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_perform_delete_selected(self):
|
|
|
|
usermock = Mock()
|
|
|
|
selected = Mock()
|
|
|
|
selected.all.return_value = [usermock]
|
2019-01-30 21:27:25 +01:00
|
|
|
self.dbuadmin.perform_delete_selected(Mock(name="request"), selected)
|
2024-08-23 17:57:40 +02:00
|
|
|
selected.all.assert_called_with()
|
|
|
|
usermock.delete.assert_called_with()
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_get_actions(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
requestmock = MagicMock(name="request")
|
|
|
|
self.assertNotIn("delete_selected", self.dbuadmin.get_actions(requestmock))
|
|
|
|
self.assertIn("perform_delete_selected", self.dbuadmin.get_actions(requestmock))
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserDatabaseAdminTest(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
site = AdminSite()
|
|
|
|
self.udbadmin = UserDatabaseAdmin(UserDatabase, site)
|
|
|
|
super(UserDatabaseAdminTest, self).setUp()
|
|
|
|
|
|
|
|
def test_get_form_with_instance(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
form = self.udbadmin.get_form(Mock(name="request"), obj=Mock(name="userdb"))
|
|
|
|
self.assertEqual(form.Meta.fields, ["db_name", "db_user"])
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_get_form_without_instance(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
form = self.udbadmin.get_form(Mock(name="request"))
|
|
|
|
self.assertEqual(form.Meta.fields, ["db_user"])
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_get_readonly_fields_with_instance(self):
|
|
|
|
fields = self.udbadmin.get_readonly_fields(
|
2019-01-30 21:27:25 +01:00
|
|
|
Mock(name="request"), obj=Mock(name="userdb")
|
|
|
|
)
|
|
|
|
self.assertEqual(fields, ["db_name", "db_user"])
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_get_readonly_fields_without_instance(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
fields = self.udbadmin.get_readonly_fields(Mock(name="request"))
|
2015-12-07 01:22:13 +01:00
|
|
|
self.assertEqual(fields, [])
|
|
|
|
|
|
|
|
def test_save_model_change(self):
|
|
|
|
objmock = Mock()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.udbadmin.save_model(Mock(name="request"), objmock, Mock(), True)
|
2024-08-23 17:57:40 +02:00
|
|
|
objmock.create_in_database.assert_not_called()
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_save_model_no_change(self):
|
|
|
|
objmock = Mock()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.udbadmin.save_model(Mock(name="request"), objmock, Mock(), False)
|
2024-08-23 17:57:40 +02:00
|
|
|
objmock.create_in_database.assert_called_with()
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_perform_delete_selected(self):
|
|
|
|
userdbmock = Mock()
|
|
|
|
selected = Mock()
|
|
|
|
selected.all.return_value = [userdbmock]
|
2019-01-30 21:27:25 +01:00
|
|
|
self.udbadmin.perform_delete_selected(Mock(name="request"), selected)
|
2024-08-23 17:57:40 +02:00
|
|
|
selected.all.assert_called_with()
|
|
|
|
userdbmock.delete.assert_called_with()
|
2015-12-07 01:22:13 +01:00
|
|
|
|
|
|
|
def test_get_actions(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
requestmock = MagicMock(name="request")
|
|
|
|
self.assertNotIn("delete_selected", self.udbadmin.get_actions(requestmock))
|
|
|
|
self.assertIn("perform_delete_selected", self.udbadmin.get_actions(requestmock))
|