You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
3.7 KiB
JavaScript
82 lines
3.7 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { readFile } from 'node:fs/promises';
|
|
import vm from 'node:vm';
|
|
|
|
const copyText = (key, parameters = {}) => `${key}:${JSON.stringify(parameters)}`;
|
|
|
|
{
|
|
const source = await readFile(new URL('../public/cloud/iso-images.js', import.meta.url), 'utf8');
|
|
const safeError = source.slice(source.indexOf('function isoMutationMayHaveApplied('),
|
|
source.indexOf('function setImportStatus('));
|
|
const context = vm.createContext({ t: copyText });
|
|
vm.runInContext(`${safeError}\nglobalThis.safeIsoErrorForTest = safeIsoError;`, context);
|
|
const display = context.safeIsoErrorForTest;
|
|
assert.match(display(new Error('private transport detail'), 'isos.error.import', true),
|
|
/^isos\.error\.mutation_unknown:/);
|
|
assert.match(display({ status: 500 }, 'isos.error.delete', true),
|
|
/^isos\.error\.mutation_unknown:/);
|
|
assert.match(display({ status: 422 }, 'isos.error.import', true),
|
|
/^isos\.error\.invalid_input:/);
|
|
assert.match(display({ status: 500 }, 'isos.error.load'),
|
|
/^isos\.error\.request:\{"status":500\}$/);
|
|
assert.match(display({ status: 409, mutationMayHaveApplied: true },
|
|
'isos.error.delete', true), /^isos\.error\.mutation_unknown:/);
|
|
}
|
|
|
|
{
|
|
const source = await readFile(new URL('../public/cloud/ssh-keys.js', import.meta.url), 'utf8');
|
|
const transport = source.slice(source.indexOf('class UiCopyError'),
|
|
source.indexOf('function normalizeKey('));
|
|
const context = vm.createContext({
|
|
Headers,
|
|
location: { protocol: 'https:', hostname: 'panel.example.test', assign() {} },
|
|
state: { csrfToken: 'fixture' },
|
|
byId() { return null; },
|
|
t: copyText,
|
|
fetch: async () => { throw new Error('private network failure'); },
|
|
});
|
|
vm.runInContext(`${transport}\nglobalThis.apiForTest = api; globalThis.errorTextForTest = errorText;`,
|
|
context);
|
|
|
|
await assert.rejects(context.apiForTest('/api/v1/ssh-keys', {
|
|
method: 'POST', body: { name: 'fixture', public_key: 'fixture' },
|
|
}), error => error.copyKey === 'ssh_keys.error.mutation_unknown'
|
|
&& error.mutationMayHaveApplied === true);
|
|
|
|
context.fetch = async () => ({ status: 201, ok: true, async json() {
|
|
throw new Error('invalid response');
|
|
} });
|
|
await assert.rejects(context.apiForTest('/api/v1/ssh-keys', {
|
|
method: 'POST', body: { name: 'fixture', public_key: 'fixture' },
|
|
}), error => error.copyKey === 'ssh_keys.error.mutation_unknown'
|
|
&& error.mutationMayHaveApplied === true);
|
|
|
|
context.fetch = async () => ({
|
|
status: 422, ok: false,
|
|
async json() { return { message: 'private backend validation detail' }; },
|
|
});
|
|
await assert.rejects(context.apiForTest('/api/v1/ssh-keys', {
|
|
method: 'POST', body: { name: 'fixture', public_key: 'fixture' },
|
|
}), error => error.copyKey === 'ssh_keys.error.invalid_input'
|
|
&& !String(error.message).includes('private backend'));
|
|
|
|
context.fetch = async () => ({
|
|
status: 500, ok: false,
|
|
async json() { return { detail: 'private backend exception' }; },
|
|
});
|
|
await assert.rejects(context.apiForTest('/api/v1/ssh-keys'),
|
|
error => error.copyKey === 'ssh_keys.error.request'
|
|
&& error.parameters.status === 500
|
|
&& !String(error.message).includes('private backend'));
|
|
await assert.rejects(context.apiForTest('/api/v1/ssh-keys', {
|
|
method: 'POST', body: { name: 'fixture', public_key: 'fixture' },
|
|
}), error => error.copyKey === 'ssh_keys.error.mutation_unknown'
|
|
&& error.mutationMayHaveApplied === true
|
|
&& !String(error.message).includes('private backend'));
|
|
|
|
assert.match(context.errorTextForTest(new Error('private backend exception'),
|
|
'ssh_keys.error.initialize'), /^ssh_keys\.error\.initialize:/);
|
|
}
|
|
|
|
console.log('ISO and SSH-key safe feedback behavior tests passed.');
|