mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-09 03:36:48 +00:00
fix(admin): a default VM that is not exactly one VM is refused on the form
NEW_USER_DEFAULT_INSTANCES names VMs, and a name may repeat across hosts. A name with no VM, or with one on two hosts, made creating any user a 500. The create form now looks the names up and says which one is not exactly one VM; the user and its default VMs are saved together.
This commit is contained in:
parent
2e5ef34473
commit
addff12652
4 changed files with 47 additions and 21 deletions
|
|
@ -6,6 +6,10 @@ from django.db import connection
|
|||
from django.test import TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
|
||||
from accounts.models import UserInstance
|
||||
from computes.models import Compute
|
||||
from instances.models import Instance
|
||||
|
||||
RAW = "Raw-Passw0rd-in-the-form"
|
||||
LIMITS = {"max_instances": 1, "max_cpus": 1, "max_memory": 1024, "max_disk_size": 4}
|
||||
|
||||
|
|
@ -63,3 +67,30 @@ class UserCreateTests(TestCase):
|
|||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, "too similar to the username")
|
||||
|
||||
|
||||
class DefaultInstancesTests(TestCase):
|
||||
"""NEW_USER_DEFAULT_INSTANCES names VMs, and a name may repeat across hosts."""
|
||||
|
||||
def setUp(self):
|
||||
self.client.force_login(User.objects.create_superuser("uc-admin", "uc@example.com", "pw"))
|
||||
one, two = (Compute.objects.create(name=name, hostname="localhost", login="", password="", details="", type=4)
|
||||
for name in ("uc-one", "uc-two"))
|
||||
self.web = Instance.objects.create(compute=one, name="web", uuid="00000000-0000-0000-0000-000000000001")
|
||||
for compute, uuid in ((one, "00000000-0000-0000-0000-000000000002"), (two, "00000000-0000-0000-0000-000000000003")):
|
||||
Instance.objects.create(compute=compute, name="db", uuid=uuid)
|
||||
|
||||
def create(self):
|
||||
return self.client.post(reverse("admin:user_create"), {"username": "newbie", "password": RAW, **LIMITS})
|
||||
|
||||
@override_settings(NEW_USER_DEFAULT_INSTANCES=["web"])
|
||||
def test_the_new_user_owns_the_named_vm(self):
|
||||
self.assertRedirects(self.create(), reverse("admin:user_list"))
|
||||
self.assertEqual([o.instance for o in UserInstance.objects.filter(user__username="newbie")], [self.web])
|
||||
|
||||
def test_a_name_that_is_not_one_vm_is_refused_on_the_form(self):
|
||||
for name in ("no-such-vm", "db"):
|
||||
with self.subTest(name), override_settings(NEW_USER_DEFAULT_INSTANCES=["web", name]):
|
||||
response = self.create()
|
||||
self.assertContains(response, f"NEW_USER_DEFAULT_INSTANCES: {name} is not exactly one VM")
|
||||
self.assertFalse(User.objects.filter(username="newbie").exists())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue