mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-09 11:46:49 +00:00
The host pages lose their last inline scripts: the overview's charts (host_graphs.js), the New pool, New network and New interface dialogs (pool_dialog.js, network_dialog.js, interface_dialog.js), a network's and a network filter's XML editors and dialogs (network_page.js, nwfilter_page.js, nwfilters_page.js), the secrets page's labels (secrets_page.js, its texts from the view as usage_texts) and a pool's page with the ISO upload (storage_page.js). Their URLs and messages are data attributes of the elements they belong to; the upload reads its token and address from its form. No template has an inline script or an inline event handler any more.
69 lines
2.9 KiB
Python
69 lines
2.9 KiB
Python
"""Pages poll the server through static/js/poll.js: one request at a time,
|
|
none while the page is hidden. Its behaviour is checked with Node."""
|
|
|
|
import json
|
|
import shutil
|
|
import subprocess
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from django.conf import settings
|
|
|
|
BASE = Path(settings.BASE_DIR)
|
|
POLLING_SOURCES = ["static/js/instance_status.js", "static/js/instance_stats.js", "static/js/host_graphs.js"]
|
|
|
|
|
|
class PollTestCase(unittest.TestCase):
|
|
@unittest.skipUnless(shutil.which("node"), "node is not installed")
|
|
def test_poll_js(self):
|
|
result = subprocess.run(
|
|
["node", str(BASE / "webvirtcloud/tests/js/poll.test.js"), str(BASE / "static/js/poll.js")],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=60,
|
|
)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
|
|
@unittest.skipUnless(shutil.which("node"), "node is not installed")
|
|
def test_vm_states_js(self):
|
|
result = subprocess.run(
|
|
["node", str(BASE / "webvirtcloud/tests/js/vm_states.test.js"), str(BASE / "static/js/poll.js"),
|
|
str(BASE / "static/js/vm_states.js")],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=60,
|
|
)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
|
|
def test_pages_poll_through_poll_js(self):
|
|
for path in POLLING_SOURCES:
|
|
with self.subTest(path=path):
|
|
source = (BASE / path).read_text()
|
|
self.assertNotIn("setInterval", source)
|
|
self.assertIn("pollWhileVisible(", source)
|
|
|
|
@unittest.skipUnless(shutil.which("node"), "node is not installed")
|
|
def test_the_vm_page_reloads_only_when_the_state_changes(self):
|
|
# "status" is a property of the browser's window: a page variable of
|
|
# that name holds what the browser makes of it (a string, or nothing)
|
|
script = (BASE / "static/js/instance_status.js").read_text()
|
|
js = """
|
|
const vm = require("vm");
|
|
const results = {};
|
|
for (const answer of [1, 5]) {
|
|
let reloads = 0;
|
|
const page = {
|
|
pollWhileVisible: (interval, request, onData) => { onData({status: answer}); return {stop() {}}; },
|
|
$: () => ({submit() {}}),
|
|
document: {getElementById: () => ({dataset: {status: "1", statusUrl: "/instances/1/status/"}})},
|
|
};
|
|
page.window = {location: {reload: () => reloads++}};
|
|
Object.defineProperty(page, "status", {get: () => "", set: () => {}});
|
|
vm.runInNewContext(SCRIPT, vm.createContext(page));
|
|
results[answer] = reloads;
|
|
}
|
|
console.log(JSON.stringify(results));
|
|
""".replace("SCRIPT", json.dumps(script))
|
|
out = subprocess.run(["node", "-e", js], capture_output=True, text=True, timeout=30)
|
|
self.assertEqual(out.returncode, 0, out.stderr)
|
|
self.assertEqual(json.loads(out.stdout), {"1": 0, "5": 1})
|