|
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
|
import { readFile } from 'node:fs/promises';
|
|
|
|
|
import vm from 'node:vm';
|
|
|
|
|
|
|
|
|
|
const pageSource = await readFile(
|
|
|
|
|
new URL('../public/cloud/admin-object-storage.js', import.meta.url), 'utf8',
|
|
|
|
|
);
|
|
|
|
|
const requestRuntimeSource = await readFile(
|
|
|
|
|
new URL('../public/cloud/admin-request-runtime.js', import.meta.url), 'utf8',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
class ClassList {
|
|
|
|
|
constructor() { this.values = new Set(); }
|
|
|
|
|
add(...values) { values.forEach(value => this.values.add(value)); }
|
|
|
|
|
remove(...values) { values.forEach(value => this.values.delete(value)); }
|
|
|
|
|
contains(value) { return this.values.has(value); }
|
|
|
|
|
toggle(value, force) {
|
|
|
|
|
if (force === true) this.values.add(value);
|
|
|
|
|
else if (force === false) this.values.delete(value);
|
|
|
|
|
else if (this.values.has(value)) this.values.delete(value);
|
|
|
|
|
else this.values.add(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class Element {
|
|
|
|
|
constructor(value = '') {
|
|
|
|
|
this.value = value;
|
|
|
|
|
this.dataset = {};
|
|
|
|
|
this.disabled = false;
|
|
|
|
|
this.hidden = false;
|
|
|
|
|
this.required = false;
|
|
|
|
|
this.listeners = new Map();
|
|
|
|
|
this.attributes = new Map();
|
|
|
|
|
this.classList = new ClassList();
|
|
|
|
|
this.className = '';
|
|
|
|
|
this.textContent = '';
|
|
|
|
|
}
|
|
|
|
|
addEventListener(name, listener) { this.listeners.set(name, listener); }
|
|
|
|
|
setAttribute(name, value) { this.attributes.set(name, String(value)); }
|
|
|
|
|
getAttribute(name) { return this.attributes.get(name) || null; }
|
|
|
|
|
removeAttribute(name) { this.attributes.delete(name); }
|
|
|
|
|
querySelector() { return null; }
|
|
|
|
|
querySelectorAll() { return []; }
|
|
|
|
|
focus() {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const names = [
|
|
|
|
|
'backend_id', 'expected_revision', 'name', 'endpoint', 'region', 'bucket',
|
|
|
|
|
'prefix', 'path_style', 'failure_domain', 'write_priority',
|
|
|
|
|
'access_key_id', 'secret_access_key',
|
|
|
|
|
];
|
|
|
|
|
const fields = Object.fromEntries(names.map(name => [name, new Element()]));
|
|
|
|
|
const form = new Element();
|
|
|
|
|
form.dataset.canManage = '1';
|
|
|
|
|
form.elements = fields;
|
|
|
|
|
form.reset = () => Object.values(fields).forEach(field => { field.value = ''; });
|
|
|
|
|
const dialog = new Element();
|
|
|
|
|
const drawer = new Element();
|
|
|
|
|
drawer.hidden = true;
|
|
|
|
|
drawer.querySelector = selector => selector === '[role="dialog"]' ? dialog : null;
|
|
|
|
|
drawer.querySelectorAll = () => [];
|
|
|
|
|
const add = new Element();
|
|
|
|
|
const replace = new Element();
|
|
|
|
|
replace.checked = false;
|
|
|
|
|
const nodes = new Map([
|
|
|
|
|
['osConfigDrawer', drawer], ['objectStorageForm', form],
|
|
|
|
|
['osAddBackend', add], ['osDrawerTitle', new Element()],
|
|
|
|
|
['osDrawerDescription', new Element()], ['osNotice', new Element()],
|
|
|
|
|
['osSave', new Element()], ['osReplaceCredentialsWrap', new Element()],
|
|
|
|
|
['osReplaceCredentials', replace], ['osCredentialFields', new Element()],
|
|
|
|
|
['osCredentialHint', new Element()],
|
|
|
|
|
]);
|
|
|
|
|
const requests = [];
|
|
|
|
|
const documentListeners = new Map();
|
|
|
|
|
const body = new Element();
|
|
|
|
|
const context = {
|
|
|
|
|
URL, Headers, AbortController,
|
|
|
|
|
document: {
|
|
|
|
|
body,
|
|
|
|
|
activeElement: null,
|
|
|
|
|
getElementById: id => nodes.get(id) || null,
|
|
|
|
|
querySelectorAll: () => [],
|
|
|
|
|
addEventListener: (name, listener) => documentListeners.set(name, listener),
|
|
|
|
|
},
|
|
|
|
|
location: {
|
|
|
|
|
pathname: '/admin/object-storage', search: '', hash: '',
|
|
|
|
|
protocol: 'https:', hostname: 'panel.test', reload() {},
|
|
|
|
|
},
|
|
|
|
|
history: { state: null, replaceState() {} },
|
|
|
|
|
crypto: { randomUUID: () => '11111111-2222-4333-8444-555555555555' },
|
|
|
|
|
setTimeout() {}, clearTimeout() {}, console,
|
|
|
|
|
fetch(url, options) {
|
|
|
|
|
requests.push({ url, options });
|
|
|
|
|
return Promise.resolve({ ok: true, status: 201, json: async () => ({ ret: 1, msg: 'created' }) });
|
|
|
|
|
},
|
|
|
|
|
VenixAdminDangerDialog: { confirm: () => Promise.resolve(true) },
|
|
|
|
|
ADMIN_CSRF_TOKEN: 'a'.repeat(64),
|
|
|
|
|
};
|
|
|
|
|
context.window = context;
|
|
|
|
|
|
|
|
|
|
vm.runInNewContext(requestRuntimeSource, context, { filename: 'admin-request-runtime.js' });
|
|
|
|
|
vm.runInNewContext(pageSource, context, { filename: 'admin-object-storage.js' });
|
|
|
|
|
|
|
|
|
|
add.listeners.get('click')();
|
|
|
|
|
Object.assign(fields.name, { value: 'Secondary Storage' });
|
|
|
|
|
Object.assign(fields.endpoint, { value: 'https://s3-b.example.com' });
|
|
|
|
|
Object.assign(fields.region, { value: 'us-west-2' });
|
|
|
|
|
Object.assign(fields.bucket, { value: 'virt-secondary' });
|
|
|
|
|
Object.assign(fields.prefix, { value: 'virt-cloud' });
|
|
|
|
|
Object.assign(fields.path_style, { value: 'false' });
|
|
|
|
|
Object.assign(fields.failure_domain, { value: 'provider-b' });
|
|
|
|
|
Object.assign(fields.write_priority, { value: '20' });
|
|
|
|
|
Object.assign(fields.access_key_id, { value: 'TESTACCESS' });
|
|
|
|
|
Object.assign(fields.secret_access_key, { value: 'test-secret' });
|
|
|
|
|
form.listeners.get('submit')({ preventDefault() {} });
|
|
|
|
|
for (let turn = 0; turn < 10; turn += 1) await Promise.resolve();
|
|
|
|
|
|
|
|
|
|
assert.equal(requests.length, 1);
|
|
|
|
|
assert.equal(requests[0].url, '/admin/object-storage/backends');
|
|
|
|
|
assert.equal(requests[0].options.headers.get('X-Admin-CSRF-Token'), context.ADMIN_CSRF_TOKEN);
|
|
|
|
|
assert.ok(requests[0].options.headers.get('Idempotency-Key'));
|
|
|
|
|
assert.deepEqual(JSON.parse(requests[0].options.body), {
|
|
|
|
|
name: 'Secondary Storage', endpoint: 'https://s3-b.example.com',
|
|
|
|
|
region: 'us-west-2', bucket: 'virt-secondary', prefix: 'virt-cloud',
|
|
|
|
|
path_style: 'false', failure_domain: 'provider-b', write_priority: '20',
|
|
|
|
|
access_key_id: 'TESTACCESS', secret_access_key: 'test-secret',
|
|
|
|
|
});
|
|
|
|
|
assert.match(pageSource, /error\.mutationMayHaveApplied === true/);
|
|
|
|
|
assert.match(pageSource, /data-effect-unknown/);
|
|
|
|
|
assert.doesNotMatch(pageSource, /window\.confirm\(/);
|
|
|
|
|
|
|
|
|
|
console.log('Object-storage create uses protected JSON and uncertain mutations remain fenced.');
|