1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-11-01 03:54:15 +00:00
webvirtcloud/accounts/tests.py

309 lines
11 KiB
Python
Raw Normal View History

2020-10-14 08:37:46 +00:00
from appsettings.settings import app_settings
from computes.models import Compute
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.shortcuts import reverse
from django.test import Client, TestCase
2020-10-14 08:37:46 +00:00
from instances.models import Instance
from instances.utils import refr
from libvirt import VIR_DOMAIN_UNDEFINE_NVRAM
from vrtManager.create import wvmCreate
from accounts.forms import UserInstanceForm, UserSSHKeyForm
from accounts.models import UserInstance, UserSSHKey
from accounts.utils import validate_ssh_key
2015-02-27 08:53:51 +00:00
class AccountsTestCase(TestCase):
2020-10-14 08:37:46 +00:00
@classmethod
def setUpClass(cls):
super().setUpClass()
# Add users for testing purposes
User = get_user_model()
cls.admin_user = User.objects.get(pk=1)
2022-11-02 05:54:35 +00:00
cls.test_user = User.objects.create_user(username="test", password="test")
2020-10-14 08:37:46 +00:00
# Add localhost compute
cls.compute = Compute(
2022-11-02 05:54:35 +00:00
name="test-compute",
hostname="localhost",
login="",
password="",
details="local",
2020-10-14 08:37:46 +00:00
type=4,
)
cls.compute.save()
cls.connection = wvmCreate(
cls.compute.hostname,
cls.compute.login,
cls.compute.password,
cls.compute.type,
)
# Add disks for testing
cls.connection.create_volume(
2022-11-02 05:54:35 +00:00
"default",
"test-volume",
2020-10-14 08:37:46 +00:00
1,
2022-11-02 05:54:35 +00:00
"qcow2",
2020-10-14 08:37:46 +00:00
False,
0,
0,
)
# XML for testing vm
2022-11-02 05:54:35 +00:00
with open("conf/test-vm.xml", "r") as f:
2020-10-14 08:37:46 +00:00
cls.xml = f.read()
# Create testing vm from XML
cls.connection._defineXML(cls.xml)
refr(cls.compute)
cls.instance = Instance.objects.get(pk=1)
@classmethod
def tearDownClass(cls):
# Destroy testing vm
cls.instance.proxy.delete_all_disks()
cls.instance.proxy.delete(VIR_DOMAIN_UNDEFINE_NVRAM)
super().tearDownClass()
def setUp(self):
2022-11-02 05:54:35 +00:00
self.client.login(username="admin", password="admin")
permission = Permission.objects.get(codename="change_password")
2020-10-14 08:37:46 +00:00
self.test_user.user_permissions.add(permission)
2022-11-02 05:54:35 +00:00
self.rsa_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC6OOdbfv27QVnSC6sKxGaHb6YFc+3gxCkyVR3cTSXE/n5BEGf8aOgBpepULWa1RZfxYHY14PlKULDygdXSdrrR2kNSwoKz/Oo4d+3EE92L7ocl1+djZbptzgWgtw1OseLwbFik+iKlIdqPsH+IUQvX7yV545ZQtAP8Qj1R+uCqkw== test@test"
self.ecdsa_key = "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJc5xpT3R0iFJYNZbmWgAiDlHquX/BcV1kVTsnBfiMsZgU3lGaqz2eb7IBcir/dxGnsVENTTmPQ6sNcxLxT9kkQ= realgecko@archlinux"
def test_profile(self):
2022-11-02 05:54:35 +00:00
response = self.client.get(reverse("accounts:profile"))
self.assertEqual(response.status_code, 200)
2022-11-02 05:54:35 +00:00
response = self.client.get(
reverse("accounts:account", args=[self.test_user.id])
)
self.assertEqual(response.status_code, 200)
2020-10-14 08:37:46 +00:00
def test_account_with_otp(self):
settings.OTP_ENABLED = True
2022-11-02 05:54:35 +00:00
response = self.client.get(
reverse("accounts:account", args=[self.test_user.id])
)
2020-10-14 08:37:46 +00:00
self.assertEqual(response.status_code, 200)
2020-10-14 08:37:46 +00:00
def test_login_logout(self):
client = Client()
2022-11-02 05:54:35 +00:00
response = client.post(
reverse("accounts:login"), {"username": "test", "password": "test"}
)
self.assertRedirects(response, reverse("accounts:profile"))
2022-11-02 05:54:35 +00:00
response = client.get(reverse("accounts:logout"))
self.assertRedirects(response, reverse("accounts:login"))
2020-06-16 09:17:21 +00:00
2020-10-14 08:37:46 +00:00
def test_change_password(self):
self.client.force_login(self.test_user)
2020-06-16 09:17:21 +00:00
2022-11-02 05:54:35 +00:00
response = self.client.get(reverse("accounts:change_password"))
2020-06-16 09:17:21 +00:00
self.assertEqual(response.status_code, 200)
2020-10-14 08:37:46 +00:00
response = self.client.post(
2022-11-02 05:54:35 +00:00
reverse("accounts:change_password"),
2020-06-16 09:17:21 +00:00
{
2022-11-02 05:54:35 +00:00
"old_password": "wrongpass",
"new_password1": "newpw",
"new_password2": "newpw",
2020-06-16 09:17:21 +00:00
},
)
self.assertEqual(response.status_code, 200)
2020-10-14 08:37:46 +00:00
response = self.client.post(
2022-11-02 05:54:35 +00:00
reverse("accounts:change_password"),
2020-06-16 09:17:21 +00:00
{
2022-11-02 05:54:35 +00:00
"old_password": "test",
"new_password1": "newpw",
"new_password2": "newpw",
2020-06-16 09:17:21 +00:00
},
)
2022-11-02 05:54:35 +00:00
self.assertRedirects(response, reverse("accounts:profile"))
2020-06-16 09:17:21 +00:00
2020-10-14 08:37:46 +00:00
self.client.logout()
2020-06-16 09:17:21 +00:00
2022-11-02 05:54:35 +00:00
logged_in = self.client.login(username="test", password="newpw")
2020-06-16 09:17:21 +00:00
self.assertTrue(logged_in)
2020-10-14 08:37:46 +00:00
def test_user_instance_create_update_delete(self):
# create
2022-11-02 05:54:35 +00:00
response = self.client.get(
reverse("accounts:user_instance_create", args=[self.test_user.id])
)
2020-10-14 08:37:46 +00:00
self.assertEqual(response.status_code, 200)
response = self.client.post(
2022-11-02 05:54:35 +00:00
reverse("accounts:user_instance_create", args=[self.test_user.id]),
2020-10-14 08:37:46 +00:00
{
2022-11-02 05:54:35 +00:00
"user": self.test_user.id,
"instance": self.instance.id,
"is_change": False,
"is_delete": False,
"is_vnc": False,
2020-10-14 08:37:46 +00:00
},
)
2022-11-02 05:54:35 +00:00
self.assertRedirects(
response, reverse("accounts:account", args=[self.test_user.id])
)
2020-10-14 08:37:46 +00:00
user_instance: UserInstance = UserInstance.objects.get(pk=1)
self.assertEqual(user_instance.user, self.test_user)
self.assertEqual(user_instance.instance, self.instance)
self.assertEqual(user_instance.is_change, False)
self.assertEqual(user_instance.is_delete, False)
self.assertEqual(user_instance.is_vnc, False)
# update
2022-11-02 05:54:35 +00:00
response = self.client.get(
reverse("accounts:user_instance_update", args=[user_instance.id])
)
2020-10-14 08:37:46 +00:00
self.assertEqual(response.status_code, 200)
response = self.client.post(
2022-11-02 05:54:35 +00:00
reverse("accounts:user_instance_update", args=[user_instance.id]),
2020-10-14 08:37:46 +00:00
{
2022-11-02 05:54:35 +00:00
"user": self.test_user.id,
"instance": self.instance.id,
"is_change": True,
"is_delete": True,
"is_vnc": True,
2020-10-14 08:37:46 +00:00
},
)
2022-11-02 05:54:35 +00:00
self.assertRedirects(
response, reverse("accounts:account", args=[self.test_user.id])
)
2020-10-14 08:37:46 +00:00
user_instance: UserInstance = UserInstance.objects.get(pk=1)
self.assertEqual(user_instance.user, self.test_user)
self.assertEqual(user_instance.instance, self.instance)
self.assertEqual(user_instance.is_change, True)
self.assertEqual(user_instance.is_delete, True)
self.assertEqual(user_instance.is_vnc, True)
# delete
2022-11-02 05:54:35 +00:00
response = self.client.get(
reverse("accounts:user_instance_delete", args=[user_instance.id])
)
2020-10-14 08:37:46 +00:00
self.assertEqual(response.status_code, 200)
2022-11-02 05:54:35 +00:00
response = self.client.post(
reverse("accounts:user_instance_delete", args=[user_instance.id])
)
self.assertRedirects(
response, reverse("accounts:account", args=[self.test_user.id])
)
2020-10-14 08:37:46 +00:00
# test 'next' redirect during deletion
2022-11-02 05:54:35 +00:00
user_instance = UserInstance.objects.create(
user=self.test_user, instance=self.instance
)
2020-10-14 08:37:46 +00:00
response = self.client.post(
2022-11-02 05:54:35 +00:00
reverse("accounts:user_instance_delete", args=[user_instance.id])
+ "?next="
+ reverse("index")
)
self.assertRedirects(response, reverse("index"))
2020-10-14 08:37:46 +00:00
def test_update_user_profile(self):
self.client.force_login(self.test_user)
2022-11-02 05:54:35 +00:00
user = get_user_model().objects.get(username="test")
self.assertEqual(user.first_name, "")
self.assertEqual(user.last_name, "")
self.assertEqual(user.email, "")
2020-10-14 08:37:46 +00:00
2022-11-02 05:54:35 +00:00
response = self.client.post(
reverse("accounts:profile"),
{
"first_name": "first name",
"last_name": "last name",
"email": "email@mail.mail",
},
)
self.assertRedirects(response, reverse("accounts:profile"))
2020-10-14 08:37:46 +00:00
2022-11-02 05:54:35 +00:00
user = get_user_model().objects.get(username="test")
self.assertEqual(user.first_name, "first name")
self.assertEqual(user.last_name, "last name")
self.assertEqual(user.email, "email@mail.mail")
2020-10-14 08:37:46 +00:00
def test_create_delete_ssh_key(self):
2022-11-02 05:54:35 +00:00
response = self.client.get(reverse("accounts:ssh_key_create"))
2020-10-14 08:37:46 +00:00
self.assertEqual(response.status_code, 200)
2022-11-02 05:54:35 +00:00
response = self.client.post(
reverse("accounts:ssh_key_create"),
{
"keyname": "keyname",
"keypublic": self.rsa_key,
},
)
self.assertRedirects(response, reverse("accounts:profile"))
2020-10-14 08:37:46 +00:00
key = UserSSHKey.objects.get(pk=1)
2022-11-02 05:54:35 +00:00
self.assertEqual(key.keyname, "keyname")
2020-10-14 08:37:46 +00:00
self.assertEqual(key.keypublic, self.rsa_key)
2022-11-02 05:54:35 +00:00
response = self.client.get(reverse("accounts:ssh_key_delete", args=[1]))
2020-10-14 08:37:46 +00:00
self.assertEqual(response.status_code, 200)
2022-11-02 05:54:35 +00:00
response = self.client.post(reverse("accounts:ssh_key_delete", args=[1]))
self.assertRedirects(response, reverse("accounts:profile"))
2020-10-14 08:37:46 +00:00
def test_validate_ssh_key(self):
2022-11-02 05:54:35 +00:00
self.assertFalse(validate_ssh_key(""))
self.assertFalse(validate_ssh_key("ssh-rsa ABBA test@test"))
self.assertFalse(validate_ssh_key("ssh-rsa AAAABwdzZGY= test@test"))
self.assertFalse(validate_ssh_key("ssh-rsa AAA test@test"))
2020-10-14 08:37:46 +00:00
# validate ecdsa key
self.assertTrue(validate_ssh_key(self.ecdsa_key))
def test_forms(self):
# raise available validation errors for maximum coverage
2022-11-02 05:54:35 +00:00
form = UserSSHKeyForm(
{"keyname": "keyname", "keypublic": self.rsa_key}, user=self.test_user
)
2020-10-14 08:37:46 +00:00
form.save()
2022-11-02 05:54:35 +00:00
form = UserSSHKeyForm(
{"keyname": "keyname", "keypublic": self.rsa_key}, user=self.test_user
)
2020-10-14 08:37:46 +00:00
self.assertFalse(form.is_valid())
2022-11-02 05:54:35 +00:00
form = UserSSHKeyForm(
{"keyname": "keyname", "keypublic": "invalid key"}, user=self.test_user
)
2020-10-14 08:37:46 +00:00
self.assertFalse(form.is_valid())
2022-11-02 05:54:35 +00:00
app_settings.ALLOW_INSTANCE_MULTIPLE_OWNER = "False"
form = UserInstanceForm(
{
"user": self.admin_user.id,
"instance": self.instance.id,
"is_change": False,
"is_delete": False,
"is_vnc": False,
}
)
2020-10-14 08:37:46 +00:00
form.save()
2022-11-02 05:54:35 +00:00
form = UserInstanceForm(
{
"user": self.test_user.id,
"instance": self.instance.id,
"is_change": False,
"is_delete": False,
"is_vnc": False,
}
)
2020-10-14 08:37:46 +00:00
self.assertFalse(form.is_valid())