2015-12-06 15:35:23 +01:00
|
|
|
"""
|
|
|
|
This module provides tests for :py:mod:`osusers.forms`.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
from unittest.mock import MagicMock, Mock, patch
|
2015-12-06 15:35:23 +01:00
|
|
|
|
|
|
|
from django import forms
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from django.contrib.auth import get_user_model
|
2019-01-30 21:27:25 +01:00
|
|
|
from django.urls import reverse
|
2015-12-06 15:35:23 +01:00
|
|
|
|
|
|
|
from passlib.hash import sha512_crypt
|
|
|
|
|
|
|
|
from osusers.forms import (
|
|
|
|
AddSshPublicKeyForm,
|
|
|
|
ChangeOsUserPasswordForm,
|
|
|
|
DUPLICATE_SSH_PUBLIC_KEY_FOR_USER,
|
|
|
|
EditSshPublicKeyCommentForm,
|
|
|
|
INVALID_SSH_PUBLIC_KEY,
|
|
|
|
)
|
|
|
|
|
|
|
|
from osusers.models import SshPublicKey, User
|
|
|
|
|
2016-01-31 16:35:26 +01:00
|
|
|
Customer = get_user_model()
|
|
|
|
|
2015-12-06 15:35:23 +01:00
|
|
|
|
|
|
|
class AddSshPublicKeyFormTest(TestCase):
|
|
|
|
"""
|
|
|
|
Test for :py:class:`osusers.forms.AddSshPublicKeyForm`.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _setup_hostingpackage(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
customer = Customer.objects.create_user("test")
|
2015-12-06 15:35:23 +01:00
|
|
|
user = User.objects.create_user(customer=customer)
|
|
|
|
self.hostingpackage = Mock(id=42, osuser=user)
|
|
|
|
|
|
|
|
def test_constructor_needs_hostingpackage(self):
|
|
|
|
instance = MagicMock()
|
|
|
|
with self.assertRaises(KeyError) as ke:
|
|
|
|
AddSshPublicKeyForm(instance)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(ke.exception.args[0], "hostingpackage")
|
2015-12-06 15:35:23 +01:00
|
|
|
|
|
|
|
def test_constructor(self):
|
|
|
|
self._setup_hostingpackage()
|
|
|
|
instance = MagicMock()
|
2019-01-30 21:27:25 +01:00
|
|
|
form = AddSshPublicKeyForm(instance, hostingpackage=self.hostingpackage)
|
|
|
|
self.assertTrue(hasattr(form, "osuser"))
|
2015-12-06 15:35:23 +01:00
|
|
|
self.assertEqual(form.osuser, self.hostingpackage.osuser)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "helper"))
|
|
|
|
self.assertEqual(
|
|
|
|
form.helper.form_action,
|
|
|
|
reverse("add_ssh_key", kwargs={"package": self.hostingpackage.id}),
|
|
|
|
)
|
|
|
|
self.assertIn("publickeytext", form.fields)
|
|
|
|
self.assertEqual(form.helper.inputs[0].name, "submit")
|
|
|
|
|
|
|
|
@patch("osusers.forms.SshPublicKey.objects.parse_key_text")
|
|
|
|
def test_clean_publickeytext_invalid(self, parse_key_text):
|
2015-12-06 15:35:23 +01:00
|
|
|
self._setup_hostingpackage()
|
|
|
|
instance = MagicMock()
|
2019-01-30 21:27:25 +01:00
|
|
|
form = AddSshPublicKeyForm(instance, hostingpackage=self.hostingpackage)
|
|
|
|
form.cleaned_data = {"publickeytext": "a bad key"}
|
|
|
|
parse_key_text.side_effect = ValueError
|
2015-12-06 15:35:23 +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.forms.SshPublicKey.objects.parse_key_text")
|
|
|
|
def test_clean_publickeytext_valid(self, _):
|
2015-12-06 15:35:23 +01:00
|
|
|
self._setup_hostingpackage()
|
|
|
|
instance = MagicMock()
|
2019-01-30 21:27:25 +01:00
|
|
|
form = AddSshPublicKeyForm(instance, hostingpackage=self.hostingpackage)
|
|
|
|
form.cleaned_data = {"publickeytext": "good key comment"}
|
2015-12-06 15:35:23 +01:00
|
|
|
retval = form.clean_publickeytext()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(retval, "good key comment")
|
2015-12-06 15:35:23 +01:00
|
|
|
|
|
|
|
def test_clean_none(self):
|
|
|
|
self._setup_hostingpackage()
|
|
|
|
instance = MagicMock()
|
2019-01-30 21:27:25 +01:00
|
|
|
form = AddSshPublicKeyForm(instance, hostingpackage=self.hostingpackage)
|
|
|
|
form.cleaned_data = {"publickeytext": None}
|
2015-12-06 15:35:23 +01:00
|
|
|
form.clean()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIsNone(form.cleaned_data["publickeytext"])
|
2015-12-06 15:35:23 +01:00
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
@patch("osusers.forms.SshPublicKey.objects.parse_key_text")
|
|
|
|
def test_clean_fresh(self, parse_key_text):
|
2015-12-06 15:35:23 +01:00
|
|
|
self._setup_hostingpackage()
|
|
|
|
instance = MagicMock()
|
2019-01-30 21:27:25 +01:00
|
|
|
form = AddSshPublicKeyForm(instance, hostingpackage=self.hostingpackage)
|
|
|
|
sshpubkey = "good key comment"
|
|
|
|
form.cleaned_data = {"publickeytext": sshpubkey}
|
|
|
|
parse_key_text.return_value = sshpubkey.split(" ")
|
2015-12-06 15:35:23 +01:00
|
|
|
form.clean()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(form.cleaned_data["publickeytext"], "good key comment")
|
2015-12-06 15:35:23 +01:00
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
@patch("osusers.forms.SshPublicKey.objects.parse_key_text")
|
|
|
|
def test_clean_duplicate(self, parse_key_text):
|
2015-12-06 15:35:23 +01:00
|
|
|
self._setup_hostingpackage()
|
|
|
|
instance = MagicMock()
|
2019-01-30 21:27:25 +01:00
|
|
|
form = AddSshPublicKeyForm(instance, hostingpackage=self.hostingpackage)
|
2015-12-06 15:35:23 +01:00
|
|
|
SshPublicKey.objects.create(
|
2019-01-30 21:27:25 +01:00
|
|
|
user=self.hostingpackage.osuser,
|
|
|
|
algorithm="good",
|
|
|
|
data="key",
|
|
|
|
comment="comment",
|
|
|
|
)
|
|
|
|
sshpubkey = "good key comment"
|
|
|
|
form.cleaned_data = {"publickeytext": sshpubkey}
|
|
|
|
parse_key_text.return_value = sshpubkey.split(" ")
|
2015-12-06 15:35:23 +01:00
|
|
|
form.clean()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("publickeytext", form.errors)
|
|
|
|
self.assertIn(DUPLICATE_SSH_PUBLIC_KEY_FOR_USER, form.errors["publickeytext"])
|
2015-12-06 15:35:23 +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):
|
2015-12-06 15:35:23 +01:00
|
|
|
self._setup_hostingpackage()
|
|
|
|
instance = MagicMock()
|
2019-01-30 21:27:25 +01:00
|
|
|
form = AddSshPublicKeyForm(instance, hostingpackage=self.hostingpackage)
|
|
|
|
sshpubkey = "good key comment"
|
|
|
|
form.cleaned_data = {"publickeytext": sshpubkey}
|
|
|
|
parse_key_text.return_value = sshpubkey.split(" ")
|
2015-12-06 15:35:23 +01:00
|
|
|
retval = form.save()
|
|
|
|
self.assertTrue(isinstance(retval, SshPublicKey))
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(retval.algorithm, "good")
|
|
|
|
self.assertEqual(retval.data, "key")
|
|
|
|
self.assertEqual(retval.comment, "comment")
|
2015-12-06 15:35:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ChangeOsUserPasswordFormTest(TestCase):
|
|
|
|
"""
|
|
|
|
Test for :py:class:`osusers.forms.ChangeOsUserPasswordForm`.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _setup_user(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
customer = Customer.objects.create_user("test")
|
2015-12-06 15:35:23 +01:00
|
|
|
self.user = User.objects.create_user(customer=customer)
|
|
|
|
|
|
|
|
def test_constructor(self):
|
|
|
|
self._setup_user()
|
|
|
|
form = ChangeOsUserPasswordForm(instance=self.user)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "instance"))
|
2015-12-06 15:35:23 +01:00
|
|
|
self.assertEqual(form.instance, self.user)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(hasattr(form, "helper"))
|
|
|
|
self.assertEqual(
|
|
|
|
form.helper.form_action,
|
|
|
|
reverse("set_osuser_password", kwargs={"slug": self.user.username}),
|
|
|
|
)
|
|
|
|
self.assertEqual(form.helper.inputs[0].name, "submit")
|
2015-12-06 15:35:23 +01:00
|
|
|
|
|
|
|
def test_save(self):
|
|
|
|
self._setup_user()
|
|
|
|
form = ChangeOsUserPasswordForm(instance=self.user)
|
2019-01-30 21:27:25 +01:00
|
|
|
form.cleaned_data = {"password1": "test"}
|
2015-12-06 15:35:23 +01:00
|
|
|
user = form.save()
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertTrue(sha512_crypt.verify("test", user.shadow.passwd))
|
2015-12-06 15:35:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
class EditSshPublicKeyCommentFormTest(TestCase):
|
|
|
|
"""
|
|
|
|
Test for :py:class:`osusers.forms.EditSshPublicKeyCommentForm`.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _setup_hostingpackage(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
customer = Customer.objects.create_user("test")
|
2015-12-06 15:35:23 +01:00
|
|
|
user = User.objects.create_user(customer=customer)
|
|
|
|
self.hostingpackage = Mock(id=42, osuser=user)
|
|
|
|
|
|
|
|
def test_constructor_needs_hostingpackage(self):
|
|
|
|
instance = MagicMock()
|
|
|
|
with self.assertRaises(KeyError) as ke:
|
|
|
|
EditSshPublicKeyCommentForm(instance)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(ke.exception.args[0], "hostingpackage")
|
2015-12-06 15:35:23 +01:00
|
|
|
|
|
|
|
def test_constructor(self):
|
|
|
|
self._setup_hostingpackage()
|
|
|
|
instance = MagicMock(id=1)
|
|
|
|
form = EditSshPublicKeyCommentForm(
|
2019-01-30 21:27:25 +01:00
|
|
|
instance=instance, hostingpackage=self.hostingpackage
|
|
|
|
)
|
|
|
|
self.assertTrue(hasattr(form, "osuser"))
|
2015-12-06 15:35:23 +01:00
|
|
|
self.assertEqual(form.osuser, self.hostingpackage.osuser)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertIn("comment", form.fields)
|
|
|
|
self.assertTrue(hasattr(form, "helper"))
|
|
|
|
self.assertEqual(
|
|
|
|
form.helper.form_action,
|
|
|
|
reverse(
|
|
|
|
"edit_ssh_key_comment",
|
|
|
|
kwargs={"package": self.hostingpackage.id, "pk": instance.id},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertEqual(form.helper.inputs[0].name, "submit")
|