2014-06-01 22:20:28 +02:00
|
|
|
from django import forms
|
2014-06-01 22:01:04 +02:00
|
|
|
from django.contrib.admin import AdminSite
|
2014-06-01 22:20:28 +02:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.utils import override_settings
|
2014-06-01 22:01:04 +02:00
|
|
|
|
2015-01-31 23:55:53 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
from unittest.mock import MagicMock, Mock, patch
|
2014-06-01 22:01:04 +02:00
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
from osusers.forms import INVALID_SSH_PUBLIC_KEY, DUPLICATE_SSH_PUBLIC_KEY_FOR_USER
|
|
|
|
from osusers.models import Group, SshPublicKey, User
|
2014-06-01 22:01:04 +02:00
|
|
|
from osusers.admin import (
|
2014-06-01 22:53:38 +02:00
|
|
|
GroupAdmin,
|
2014-06-01 22:20:28 +02:00
|
|
|
PASSWORD_MISMATCH_ERROR,
|
2015-12-05 23:23:25 +01:00
|
|
|
SshPublicKeyAdmin,
|
|
|
|
SshPublicKeyCreationForm,
|
2014-06-01 22:37:43 +02:00
|
|
|
UserAdmin,
|
2014-06-01 22:20:28 +02:00
|
|
|
UserCreationForm,
|
2014-06-01 22:01:04 +02:00
|
|
|
)
|
|
|
|
|
2015-01-31 23:55:53 +01:00
|
|
|
Customer = get_user_model()
|
|
|
|
|
2014-06-01 22:01:04 +02:00
|
|
|
|
2015-01-31 23:55:53 +01:00
|
|
|
class CustomerTestCase(TestCase):
|
2014-06-01 22:01:04 +02:00
|
|
|
def setUp(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
self.customer = Customer.objects.create_user("test")
|
2015-01-31 23:55:53 +01:00
|
|
|
super(CustomerTestCase, self).setUp()
|
|
|
|
|
2014-06-01 22:20:28 +02:00
|
|
|
|
2015-01-31 23:55:53 +01:00
|
|
|
class UserCreationFormTest(CustomerTestCase):
|
2014-06-01 22:20:28 +02:00
|
|
|
def test_clean_password2_same(self):
|
|
|
|
form = UserCreationForm()
|
2015-01-31 23:55:53 +01:00
|
|
|
form.cleaned_data = {
|
2019-01-30 21:27:25 +01:00
|
|
|
"customer": self.customer,
|
|
|
|
"password1": "secret",
|
|
|
|
"password2": "secret",
|
2015-01-31 23:55:53 +01:00
|
|
|
}
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(form.clean_password2(), "secret")
|
2014-06-01 22:20:28 +02:00
|
|
|
|
|
|
|
def test_clean_password2_empty(self):
|
|
|
|
form = UserCreationForm()
|
|
|
|
form.cleaned_data = {}
|
|
|
|
self.assertIsNone(form.clean_password2())
|
|
|
|
|
|
|
|
def test_clean_password2_mismatch(self):
|
|
|
|
form = UserCreationForm()
|
2015-01-31 23:55:53 +01:00
|
|
|
form.cleaned_data = {
|
2019-01-30 21:27:25 +01:00
|
|
|
"customer": self.customer,
|
|
|
|
"password1": "secretx",
|
|
|
|
"password2": "secrety",
|
2015-01-31 23:55:53 +01:00
|
|
|
}
|
2014-06-01 22:20:28 +02:00
|
|
|
with self.assertRaises(forms.ValidationError) as cm:
|
|
|
|
form.clean_password2()
|
|
|
|
self.assertEqual(cm.exception.message, PASSWORD_MISMATCH_ERROR)
|
|
|
|
|
|
|
|
@override_settings(
|
2019-01-30 21:27:25 +01:00
|
|
|
CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", BROKER_BACKEND="memory"
|
2014-06-01 22:20:28 +02:00
|
|
|
)
|
|
|
|
def test_save_commit(self):
|
|
|
|
form = UserCreationForm()
|
2015-01-31 23:55:53 +01:00
|
|
|
form.cleaned_data = {
|
2019-01-30 21:27:25 +01:00
|
|
|
"customer": self.customer,
|
|
|
|
"password1": "secret",
|
|
|
|
"password2": "secret",
|
2015-01-31 23:55:53 +01:00
|
|
|
}
|
2014-06-01 22:20:28 +02:00
|
|
|
user = form.save()
|
|
|
|
self.assertIsNotNone(user)
|
|
|
|
self.assertEqual(User.objects.get(pk=user.uid), user)
|
2014-06-01 22:37:43 +02:00
|
|
|
|
2014-06-01 23:10:26 +02:00
|
|
|
def test_save_m2m_returns_none(self):
|
|
|
|
form = UserCreationForm()
|
|
|
|
self.assertIsNone(form.save_m2m())
|
|
|
|
|
2014-06-01 22:37:43 +02:00
|
|
|
|
2015-01-31 23:55:53 +01:00
|
|
|
class UserAdminTest(CustomerTestCase):
|
2014-06-01 22:37:43 +02:00
|
|
|
def setUp(self):
|
|
|
|
site = AdminSite()
|
|
|
|
self.uadmin = UserAdmin(User, site)
|
|
|
|
super(UserAdminTest, self).setUp()
|
|
|
|
|
|
|
|
def test_get_form_without_object(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
form = self.uadmin.get_form(Mock(name="request"))
|
|
|
|
self.assertEqual(form.Meta.fields, ["customer", "password1", "password2"])
|
2014-06-01 22:37:43 +02:00
|
|
|
|
|
|
|
@override_settings(
|
2019-01-30 21:27:25 +01:00
|
|
|
CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", BROKER_BACKEND="memory"
|
2014-06-01 22:37:43 +02:00
|
|
|
)
|
|
|
|
def test_get_form_with_object(self):
|
2015-01-31 23:55:53 +01:00
|
|
|
user = User.objects.create_user(customer=self.customer)
|
2019-01-30 21:27:25 +01:00
|
|
|
form = self.uadmin.get_form(Mock(name="request"), user)
|
2014-06-01 22:37:43 +02:00
|
|
|
self.assertEqual(
|
|
|
|
form.Meta.fields,
|
2019-01-30 21:27:25 +01:00
|
|
|
["username", "group", "gecos", "homedir", "shell", "customer", "uid"],
|
2014-06-01 22:37:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_get_inline_instances_without_object(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
inlines = self.uadmin.get_inline_instances(Mock(name="request"))
|
2015-01-22 00:19:16 +01:00
|
|
|
self.assertEqual(len(inlines), 2)
|
2014-06-01 22:37:43 +02:00
|
|
|
|
|
|
|
@override_settings(
|
2019-01-30 21:27:25 +01:00
|
|
|
CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", BROKER_BACKEND="memory"
|
2014-06-01 22:37:43 +02:00
|
|
|
)
|
|
|
|
def test_get_inline_instances_with_object(self):
|
2015-01-31 23:55:53 +01:00
|
|
|
user = User.objects.create_user(customer=self.customer)
|
2019-01-30 21:27:25 +01:00
|
|
|
inlines = self.uadmin.get_inline_instances(Mock(name="request"), user)
|
2014-06-01 22:37:43 +02:00
|
|
|
self.assertEqual(len(inlines), len(UserAdmin.inlines))
|
2014-06-01 22:53:29 +02:00
|
|
|
for index in range(len(inlines)):
|
|
|
|
self.assertIsInstance(inlines[index], UserAdmin.inlines[index])
|
2014-06-01 22:53:38 +02:00
|
|
|
|
2015-12-05 23:23:25 +01:00
|
|
|
@override_settings(
|
2019-01-30 21:27:25 +01:00
|
|
|
CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", BROKER_BACKEND="memory"
|
2015-12-05 23:23:25 +01:00
|
|
|
)
|
|
|
|
def test_perform_delete_selected(self):
|
|
|
|
user = User.objects.create_user(customer=self.customer)
|
|
|
|
self.uadmin.perform_delete_selected(
|
2019-01-30 21:27:25 +01:00
|
|
|
Mock(name="request"), User.objects.filter(uid=user.uid)
|
|
|
|
)
|
2015-12-05 23:23:25 +01:00
|
|
|
self.assertEqual(User.objects.filter(uid=user.uid).count(), 0)
|
|
|
|
|
|
|
|
def test_get_actions(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
requestmock = MagicMock(name="request")
|
|
|
|
self.assertNotIn("delete_selected", self.uadmin.get_actions(requestmock))
|
|
|
|
self.assertIn("perform_delete_selected", self.uadmin.get_actions(requestmock))
|
2015-12-05 23:23:25 +01:00
|
|
|
|
2014-06-01 22:53:38 +02:00
|
|
|
|
|
|
|
class GroupAdminTest(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
site = AdminSite()
|
|
|
|
self.gadmin = GroupAdmin(Group, site)
|
|
|
|
super(GroupAdminTest, self).setUp()
|
|
|
|
|
|
|
|
def test_get_inline_instances_without_object(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
inlines = self.gadmin.get_inline_instances(Mock(name="request"))
|
2014-06-01 22:53:38 +02:00
|
|
|
self.assertEqual(inlines, [])
|
|
|
|
|
|
|
|
@override_settings(
|
2019-01-30 21:27:25 +01:00
|
|
|
CELERY_ALWAYS_EAGER=True, CELERY_CACHE_BACKEND="memory", BROKER_BACKEND="memory"
|
2014-06-01 22:53:38 +02:00
|
|
|
)
|
|
|
|
def test_get_inline_instances_with_object(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
group = Group.objects.create(gid=1000, groupname="test")
|
|
|
|
inlines = self.gadmin.get_inline_instances(Mock(name="request"), group)
|
2014-06-01 22:53:38 +02:00
|
|
|
self.assertEqual(len(inlines), len(GroupAdmin.inlines))
|
|
|
|
for index in range(len(inlines)):
|
|
|
|
self.assertIsInstance(inlines[index], GroupAdmin.inlines[index])
|
2015-12-05 23:23:25 +01:00
|
|
|
|
|
|
|
def test_perform_delete_selected(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
group = Group.objects.create(gid=1000, groupname="test")
|
2015-12-05 23:23:25 +01:00
|
|
|
self.gadmin.perform_delete_selected(
|
2019-01-30 21:27:25 +01:00
|
|
|
Mock(name="request"), Group.objects.filter(gid=group.gid)
|
|
|
|
)
|
2015-12-05 23:23:25 +01:00
|
|
|
self.assertEqual(Group.objects.filter(gid=group.gid).count(), 0)
|
|
|
|
|
|
|
|
def test_get_actions(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
requestmock = MagicMock(name="request")
|
|
|
|
self.assertNotIn("delete_selected", self.gadmin.get_actions(requestmock))
|
|
|
|
self.assertIn("perform_delete_selected", self.gadmin.get_actions(requestmock))
|
2015-12-05 23:23:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SshPublicKeyCreationFormTest(CustomerTestCase):
|
2019-01-30 21:27:25 +01:00
|
|
|
@patch("osusers.admin.SshPublicKey.objects")
|
2015-12-05 23:23:25 +01:00
|
|
|
def test_clean_publickeytext_valid_key(self, sshpkmanager):
|
|
|
|
form = SshPublicKeyCreationForm()
|
2019-01-30 21:27:25 +01:00
|
|
|
sshpkmanager.parse_key_text = MagicMock(side_effect=ValueError)
|
|
|
|
form.cleaned_data = {"publickeytext": "wrongkey"}
|
2015-12-05 23:23:25 +01:00
|
|
|
with self.assertRaises(forms.ValidationError) as ve:
|
|
|
|
form.clean_publickeytext()
|
|
|
|
self.assertEqual(ve.exception.message, INVALID_SSH_PUBLIC_KEY)
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
@patch("osusers.admin.SshPublicKey.objects")
|
2015-12-05 23:23:25 +01:00
|
|
|
def test_clean_publickeytext_invalid_key(self, sshpkmanager):
|
|
|
|
form = SshPublicKeyCreationForm()
|
2019-01-30 21:27:25 +01:00
|
|
|
sshpkmanager.parse_key_text = MagicMock(return_value="goodkey")
|
|
|
|
form.cleaned_data = {"publickeytext": "goodkey"}
|
|
|
|
self.assertEqual(form.clean_publickeytext(), "goodkey")
|
2015-12-05 23:23:25 +01:00
|
|
|
|
|
|
|
def test_clean_missing_data(self):
|
|
|
|
form = SshPublicKeyCreationForm()
|
|
|
|
form.cleaned_data = {}
|
|
|
|
form.clean()
|
|
|
|
self.assertEqual(len(form.errors), 0)
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
@patch("osusers.admin.SshPublicKey.objects.parse_key_text")
|
|
|
|
def test_clean_once(self, parse_key_text):
|
|
|
|
parse_key_text.return_value = ("good", "key", "comment")
|
2015-12-05 23:23:25 +01:00
|
|
|
user = User.objects.create_user(customer=self.customer)
|
|
|
|
form = SshPublicKeyCreationForm()
|
2019-01-30 21:27:25 +01:00
|
|
|
form.cleaned_data = {"user": user, "publickeytext": "good key comment"}
|
2015-12-05 23:23:25 +01:00
|
|
|
form.clean()
|
|
|
|
self.assertEqual(len(form.errors), 0)
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
@patch("osusers.admin.SshPublicKey.objects.parse_key_text")
|
|
|
|
def test_clean_again(self, parse_key_text):
|
|
|
|
parse_key_text.return_value = ("good", "key", "comment")
|
2015-12-05 23:23:25 +01:00
|
|
|
user = User.objects.create_user(customer=self.customer)
|
|
|
|
SshPublicKey.objects.create(
|
2019-01-30 21:27:25 +01:00
|
|
|
user=user, algorithm="good", data="key", comment="comment"
|
|
|
|
)
|
2015-12-05 23:23:25 +01:00
|
|
|
form = SshPublicKeyCreationForm()
|
2019-01-30 21:27:25 +01:00
|
|
|
form.cleaned_data = {"user": user, "publickeytext": "good key comment"}
|
2015-12-05 23:23:25 +01:00
|
|
|
form.clean()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("publickeytext", form.errors)
|
2015-12-05 23:23:25 +01:00
|
|
|
self.assertEqual(
|
2019-01-30 21:27:25 +01:00
|
|
|
form.errors["publickeytext"], [DUPLICATE_SSH_PUBLIC_KEY_FOR_USER]
|
|
|
|
)
|
2015-12-05 23:23:25 +01:00
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
@patch("osusers.admin.SshPublicKey.objects.parse_key_text")
|
|
|
|
def test_save(self, parse_key_text):
|
|
|
|
parse_key_text.return_value = ("good", "key", "comment")
|
2015-12-05 23:23:25 +01:00
|
|
|
user = User.objects.create_user(customer=self.customer)
|
|
|
|
form = SshPublicKeyCreationForm()
|
2019-01-30 21:27:25 +01:00
|
|
|
form.cleaned_data = {"user": user, "publickeytext": "good key comment"}
|
2015-12-05 23:23:25 +01:00
|
|
|
form.instance.user = user
|
|
|
|
form.save()
|
|
|
|
self.assertTrue(
|
2019-01-30 21:27:25 +01:00
|
|
|
SshPublicKey.objects.filter(user=user, algorithm="good", data="key")
|
|
|
|
)
|
2015-12-05 23:23:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SshPublicKeyAdminTest(CustomerTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
site = AdminSite()
|
|
|
|
self.sadmin = SshPublicKeyAdmin(SshPublicKey, site)
|
|
|
|
super(SshPublicKeyAdminTest, self).setUp()
|
|
|
|
|
|
|
|
def test_get_form_no_instance(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
form = self.sadmin.get_form(request=Mock(name="request"))
|
2015-12-05 23:23:25 +01:00
|
|
|
self.assertEqual(form.Meta.model, SshPublicKey)
|
|
|
|
|
|
|
|
def test_get_form_with_instance(self):
|
|
|
|
user = User.objects.create_user(customer=self.customer)
|
|
|
|
key = SshPublicKey.objects.create(
|
2019-01-30 21:27:25 +01:00
|
|
|
user=user, algorithm="good", data="key", comment="comment"
|
|
|
|
)
|
|
|
|
form = self.sadmin.get_form(request=Mock(name="request"), obj=key)
|
2015-12-05 23:23:25 +01:00
|
|
|
self.assertEqual(form.Meta.model, SshPublicKey)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(form.Meta.fields, ["user", "comment", "algorithm", "data"])
|
2015-12-05 23:23:25 +01:00
|
|
|
|
|
|
|
def test_get_readonly_fields_no_instance(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
readonly_fields = self.sadmin.get_readonly_fields(request=Mock(name="request"))
|
2015-12-05 23:23:25 +01:00
|
|
|
self.assertEqual(readonly_fields, [])
|
|
|
|
|
|
|
|
def test_get_readonly_fields_with_instance(self):
|
|
|
|
readonly_fields = self.sadmin.get_readonly_fields(
|
2019-01-30 21:27:25 +01:00
|
|
|
request=Mock(name="request"), obj=Mock()
|
|
|
|
)
|
|
|
|
self.assertEqual(readonly_fields, ["algorithm", "data"])
|
2015-12-05 23:23:25 +01:00
|
|
|
|
|
|
|
def test_perform_delete_selected(self):
|
|
|
|
user = User.objects.create_user(customer=self.customer)
|
|
|
|
key = SshPublicKey.objects.create(
|
2019-01-30 21:27:25 +01:00
|
|
|
user=user, algorithm="good", data="key", comment="comment"
|
|
|
|
)
|
2015-12-05 23:23:25 +01:00
|
|
|
self.sadmin.perform_delete_selected(
|
2019-01-30 21:27:25 +01:00
|
|
|
Mock(name="request"), SshPublicKey.objects.filter(id=key.id)
|
|
|
|
)
|
2015-12-05 23:23:25 +01:00
|
|
|
self.assertFalse(SshPublicKey.objects.filter(id=key.id).exists())
|
|
|
|
|
|
|
|
def test_get_actions(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
requestmock = MagicMock(name="request")
|
|
|
|
self.assertNotIn("delete_selected", self.sadmin.get_actions(requestmock))
|
|
|
|
self.assertIn("perform_delete_selected", self.sadmin.get_actions(requestmock))
|