New Version
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import json
|
||||
import shutil
|
||||
import subprocess
|
||||
import unittest
|
||||
|
||||
spec = importlib.util.spec_from_file_location('patch', Path(__file__).resolve().parents[1] / 'scripts/logout_patch.py')
|
||||
patch = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(patch)
|
||||
FIXTURE = """text: gettext('Logout'),
|
||||
handler: function () {
|
||||
PVE.data.ResourceStore.loadData([], false);
|
||||
me.showLogin();
|
||||
cleanup();
|
||||
},"""
|
||||
|
||||
|
||||
class LogoutTests(unittest.TestCase):
|
||||
def test_lifecycle_and_update(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
target = root / 'bundle.js'
|
||||
target.write_text(FIXTURE)
|
||||
args = dict(target=target, directory=root / 'state', enabled=True,
|
||||
version='pve-manager/9.0.0/test', realm='main.heim.lan',
|
||||
endpoint='https://kc:8443/realms/main/protocol/openid-connect/logout',
|
||||
client='PVE-SERVER', redirect='https://ts01:8006/')
|
||||
self.assertTrue(patch.apply(**args))
|
||||
self.assertFalse(patch.apply(**args))
|
||||
self.assertIn('pveIdmUser.endsWith', target.read_text())
|
||||
self.assertLess(target.read_text().index('me.showLogin();'), target.read_text().index('window.setTimeout'))
|
||||
args['client'] = 'OTHER'
|
||||
self.assertTrue(patch.apply(**args))
|
||||
self.assertTrue(patch.apply(target, root / 'state', False))
|
||||
self.assertEqual(target.read_text(), FIXTURE)
|
||||
self.assertFalse(patch.apply(target, root / 'state', False))
|
||||
patch.apply(**args)
|
||||
# Package update replaced the entire file; rollback must not downgrade it.
|
||||
target.write_text(FIXTURE + '\n// new package')
|
||||
self.assertFalse(patch.apply(target, root / 'state', False))
|
||||
self.assertTrue(patch.apply(**args))
|
||||
self.assertTrue(patch.apply(target, root / 'state', False))
|
||||
self.assertEqual(target.read_text(), FIXTURE + '\n// new package')
|
||||
|
||||
def test_unknown_code_rejected(self):
|
||||
for text in ('unknown', FIXTURE + FIXTURE):
|
||||
with self.assertRaises(ValueError):
|
||||
patch.render(text, 'realm', 'https://kc/logout', 'client', 'https://pve/')
|
||||
|
||||
def test_tamper_rejected(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
target = root / 'bundle.js'
|
||||
target.write_text(FIXTURE)
|
||||
patch.apply(target, root / 'state', True, 'pve-manager/9.1/x', 'realm',
|
||||
'https://kc/logout', 'client', 'https://pve/')
|
||||
target.write_text(target.read_text() + '// manual edit')
|
||||
with self.assertRaises(ValueError):
|
||||
patch.apply(target, root / 'state', False)
|
||||
|
||||
@unittest.skipUnless(shutil.which('node'), 'Node.js not installed')
|
||||
def test_browser_handler_order_and_realm(self):
|
||||
code = patch.render(FIXTURE, 'main.heim.lan', 'https://kc/logout', 'PVE-SERVER', 'https://ts01:8006/')
|
||||
javascript = r"""
|
||||
const vm = require('vm');
|
||||
const source = CODE;
|
||||
for (const user of ['alice@main.heim.lan', 'root@pam']) {
|
||||
const events = []; const timers = [];
|
||||
const context = {URL, gettext: x => x,
|
||||
Proxmox: {UserName: user},
|
||||
PVE: {data: {ResourceStore: {loadData: () => events.push('clear')}}},
|
||||
me: {showLogin: () => { events.push('local'); context.Proxmox.UserName = null; }},
|
||||
cleanup: () => events.push('cleanup'),
|
||||
window: {setTimeout: f => timers.push(f), location: {assign: u => events.push(u)}}};
|
||||
vm.runInNewContext('({' + source + '}).handler()', context);
|
||||
if (events.join(',') !== 'clear,local,cleanup') throw Error('Local cleanup/order failed');
|
||||
timers.forEach(f => f());
|
||||
if (user === 'root@pam' && events.length !== 3) throw Error('PAM redirected');
|
||||
if (user.startsWith('alice')) {
|
||||
const url = new URL(events[3]);
|
||||
if (url.origin !== 'https://kc' || url.searchParams.get('client_id') !== 'PVE-SERVER'
|
||||
|| url.searchParams.get('post_logout_redirect_uri') !== 'https://ts01:8006/') throw Error('Wrong redirect');
|
||||
}
|
||||
}
|
||||
""".replace('CODE', json.dumps(code))
|
||||
subprocess.run([shutil.which('node'), '-e', javascript], check=True, capture_output=True)
|
||||
|
||||
def test_wrong_version_no_write(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
target = root / 'bundle.js'
|
||||
target.write_text(FIXTURE)
|
||||
with self.assertRaises(ValueError):
|
||||
patch.apply(target, root / 'state', True, 'pve-manager/10.0/x')
|
||||
self.assertEqual(target.read_text(), FIXTURE)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -5,6 +5,7 @@ import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
spec = importlib.util.spec_from_file_location('wizard', Path(__file__).resolve().parents[1] / 'setup.py')
|
||||
wizard = importlib.util.module_from_spec(spec)
|
||||
@@ -35,6 +36,28 @@ class SetupTests(unittest.TestCase):
|
||||
self.assertEqual(target.stat().st_mode & 0o777, 0o600)
|
||||
self.assertEqual(len(list(Path(folder).iterdir())), 1)
|
||||
|
||||
def test_rollback_success_and_failure(self):
|
||||
for fail in (False, True):
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
root = Path(folder)
|
||||
(root / 'group_vars').mkdir()
|
||||
config = root / 'group_vars/all.yml'
|
||||
config.write_text('pve_logout_enabled: true\npve_realm: main.heim.lan\n')
|
||||
previous = Path.cwd()
|
||||
try:
|
||||
with patch.object(wizard, 'ROOT', root), patch.object(wizard.shutil, 'which', return_value='/test/ansible-playbook'), patch.object(wizard, 'ask', return_value='inventory.yml'), patch.object(wizard, 'yes', side_effect=[False, False, True]), patch.object(wizard.subprocess, 'run') as run:
|
||||
if fail:
|
||||
run.side_effect = subprocess.CalledProcessError(1, 'test')
|
||||
with self.assertRaises(subprocess.CalledProcessError):
|
||||
wizard.rollback_logout()
|
||||
self.assertIn('pve_logout_enabled: true', config.read_text())
|
||||
else:
|
||||
wizard.rollback_logout()
|
||||
self.assertIn('pve_logout_enabled: false', config.read_text())
|
||||
self.assertNotIn('--ask-vault-pass', run.call_args.args[0])
|
||||
finally:
|
||||
os.chdir(previous)
|
||||
|
||||
@unittest.skipUnless(shutil.which('ansible-vault'), 'ansible-vault not installed')
|
||||
def test_real_vault_roundtrip(self):
|
||||
payload = {'test': 'fake-test-value-with-umlaut-ä'}
|
||||
|
||||
Reference in New Issue
Block a user