1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

feat(instances): a VM's owners with their rights, and XML edits kept until saved

Settings > Users was a bold title, an unlabelled "+" and a table of
names with a trash icon. The owner chose this layout from three
rendered options: one list of owners, each with what it may do
(change settings, delete the VM, change console settings, or power and
console only), Edit rights and Remove, an empty state, and a labelled
"Add owner".

- Remove and Edit rights come back to the VM's Users tab: the return
  address carried #users unencoded, so it was lost, and Edit rights
  went to the user's account page. Both accounts views now go to a
  ?next page of this site.
- The add-owner list offers only users who are not owners yet and not
  administrators, who can use every VM anyway; a superuser owner also
  took the only place when a VM may have one owner.
- The unused del_owner view and URL are removed: the list uses the
  accounts page, which asks before removing an owner.

XML:

- An XML edit not saved yet is kept: the page no longer reloads itself
  when the VM's state changes, and the browser asks before leaving it.
  A state change lost the edit.
- The button that opens the review dialog reads "Review changes"; it
  did not save, as "Change" suggested. Closing the dialog gives focus
  back to the editor.
This commit is contained in:
catborise 2026-10-30 17:02:38 +03:00
parent a2d2915f9c
commit 270fd8cb4f
12 changed files with 125 additions and 85 deletions

View file

@ -31,7 +31,7 @@ class VMOwnerRowTests(TestCase):
self.client.post(add, {"user_id": self.carol.pk})
self.assertFalse(UserInstance.objects.filter(user=self.carol).exists())
ui = UserInstance.objects.get(user=self.bob)
self.client.post(reverse("instances:del_owner", args=[self.vm.pk]), {"userinstance": ui.pk})
self.client.post(reverse("accounts:user_instance_delete", args=[ui.pk]))
self.assertEqual(rows(), [
("VM vm: add owner bob", True),
("VM vm: add owner carol: only one owner is allowed", False),

View file

@ -3,7 +3,6 @@
import inspect
from unittest.mock import patch
from accounts.models import UserInstance
from computes.models import Compute
from django.contrib.auth import get_user_model
from django.contrib.messages import get_messages
@ -49,8 +48,6 @@ class ErrorHandlingTestCase(TestCase):
# Required fields for the views that 404 without them
DATA = {
"add_owner": lambda self: {"user_id": get_user_model().objects.get(username="err_admin").id},
"del_owner": lambda self: {"userinstance": UserInstance.objects.get_or_create(
instance=self.instance, user=get_user_model().objects.get(username="err_admin"))[0].id},
"add_network": lambda self: {"add-net-network": "net:default"},
"set_qos": lambda self: {"qos_direction": "inbound", "net-mac-0": "52:54:00:00:00:01"},
}
@ -68,13 +65,8 @@ class ErrorHandlingTestCase(TestCase):
self.assertEqual(response.status_code, 302)
self.assertTrue(response.url.startswith(self.page + "#"), response.url)
def test_owner_views_reject_missing_or_foreign_ids(self):
other = Instance.objects.create(compute=self.compute, name="other", uuid="11111111-2222-3333-4444-777777777777")
foreign = UserInstance.objects.create(instance=other, user=get_user_model().objects.get(username="err_admin"))
def test_add_owner_rejects_a_missing_user(self):
self.assertEqual(self.client.post(self.url("add_owner"), {}).status_code, 404)
self.assertEqual(self.client.post(self.url("del_owner"), {}).status_code, 404)
self.assertEqual(self.client.post(self.url("del_owner"), {"userinstance": foreign.id}).status_code, 404)
self.assertTrue(UserInstance.objects.filter(pk=foreign.id).exists())
def test_same_site_referer_is_kept(self):
with patch("instances.models.wvmInstance"):

View file

@ -3,6 +3,7 @@ until the next start. The edit forms show that definition, so a save does
not post the running VM's value back over a pending change."""
from unittest.mock import PropertyMock, patch
from urllib.parse import unquote
from computes.models import Compute
from django.contrib.auth import get_user_model
@ -150,6 +151,23 @@ class PendingEditsTestCase(TestCase):
self.assertEqual(doc.xpath("//input[@name='rng']/@checked"), ["checked"])
self.assertEqual(doc.xpath("//select[@name='on_crash']/option[@selected]/@value"), ["restart"])
def test_the_users_tab_shows_each_owner_and_offers_only_new_owners(self):
from accounts.models import UserInstance
User = get_user_model()
owner = User.objects.create_user("pe-owner")
UserInstance.objects.create(instance=self.instance, user=owner, is_change=True, is_vnc=True)
User.objects.create_user("pe-new")
User.objects.create_superuser("pe-admin2", "pe2@example.com", "x")
doc = self.page()
(row,) = doc.xpath("//div[@id='users']//li")
self.assertIn("Change settings · Change console settings", " ".join(row.text_content().split()))
# Remove and Edit rights come back to the Users tab
self.assertEqual([unquote(href.split("?next=")[1]) for href in row.xpath(".//a[contains(@class, 'btn')]/@href")],
[f"/instances/{self.instance.id}/#users"] * 2)
# not an owner yet and not an administrator, who can use every VM anyway
self.assertEqual(doc.xpath("//select[@id='add-owner-user']/option/text()"), ["pe-new"])
def test_buttons_shown_as_disabled_cannot_submit(self):
# running: the CPU resize, vCPU hot plug, template and disk removal forms wait for a shutdown;
# the title, description and video model are of the next start, saved at once

View file

@ -643,11 +643,7 @@ class InstancesTestCase(TestCase):
self.assertEqual(response.status_code, 302)
self.assertEqual(UserInstance.objects.count(), 1)
response = self.client.post(
reverse("instances:del_owner", args=[self.instance.id]),
{"userinstance": user_instance.id},
HTTP_REFERER=reverse("index"),
)
response = self.client.post(reverse("accounts:user_instance_delete", args=[user_instance.id]))
self.assertEqual(response.status_code, 302)
self.assertEqual(UserInstance.objects.count(), 0)