修复正式初始化边界与向导过期校验并补齐回归测试

This commit is contained in:
xuans committed 2026-09-16 20:18:07 +08:00
1 parent c6bfcfd9ae
commit f277e101f2
10 files changed
+343 -75

No files matched your search

+18 -39
View File
@@ -501,6 +501,7 @@ export function useSetupWizard(props, emit) {
}
function validateCurrentStep() {
verificationClock.value = Date.now();
if (currentStep.value === 1) synchronizeEmailField();
error.value = "";
resultMessage.value = "";
@@ -713,55 +714,21 @@ export function useSetupWizard(props, emit) {
}
async function submit() {
verificationClock.value = Date.now();
error.value = "";
resultMessage.value = "";
if (isBootstrap && !database.databaseVerified.value) {
error.value = "数据库验证结果无效或已过期,请重新验证。";
return;
}
if (
form.value.admin_password.length < 8 ||
form.value.admin_password.length > 12
) {
error.value = "超级管理员密码长度必须为 8 至 12 个字符。";
return;
}
if (form.value.admin_password !== form.value.confirm_password) {
error.value = "两次输入的超级管理员密码不一致。";
return;
}
if (!dnsVerificationValid.value) {
error.value = "必须先完成 DNS API 在线验证;验证结果已过期时请重新测试。";
return;
}
if (form.value.email_smtp_enabled) {
const smtpPort = Number(form.value.email_smtp_port);
if (!form.value.email_smtp_host.trim()) {
error.value = "启用 SMTP 通知后必须填写 SMTP 主机。";
return;
}
if (!Number.isInteger(smtpPort) || smtpPort < 1 || smtpPort > 65535) {
error.value = "SMTP 端口必须在 1 至 65535 之间。";
return;
}
if (!form.value.email_smtp_username.trim()) {
error.value = "启用 SMTP 通知后必须填写 SMTP 用户名。";
return;
}
if (!form.value.email_smtp_password) {
error.value = "启用 SMTP 通知后必须填写 SMTP 密码。";
return;
}
if (form.value.email_smtp_mode === "plain" && !form.value.email_smtp_allow_plaintext_auth) {
error.value = "SMTP 明文模式必须明确允许明文认证,建议改用 STARTTLS 或隐式 TLS。";
return;
}
}
try {
emailCredentialPayload();
} catch (err) {
error.value = err.message || "HTTP 通知凭据格式无效。";
return;
// 提交前重新检查全部步骤,不能依赖之前点击“下一步”时的校验结果。
for (let step = 1; step <= 4; step += 1) {
currentStep.value = step;
if (!validateCurrentStep()) return;
}
const hostname =
targetType.value === "hostname" ? form.value.web_hostname.trim() : "";
@@ -833,6 +800,18 @@ export function useSetupWizard(props, emit) {
});
const result = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(result.error || "保存初始化配置失败");
if (isBootstrap) {
let destination;
try {
destination = new URL(result?.management_url);
} catch {
throw new Error("初始化响应不完整,请检查服务器状态后再继续。");
}
if (result?.completed !== true || destination.protocol !== "https:" ||
destination.username || destination.password) {
throw new Error("初始化响应不完整,请检查服务器状态后再继续。");
}
}
completed.value = true;
resultMessage.value = isBootstrap ? `初始化配置已保存,正在切换至 ${result.management_url}。请稍候;IP 自签名证书或测试证书需要人工核对。` : result.message || "初始化配置已保存。";
emit("initialized", result);
@@ -874,7 +874,7 @@ const {
</div>
</fieldset>
<fieldset v-if="currentStep === 4" class="setup-step-card">
<fieldset v-if="currentStep === 4" class="setup-step-card" :disabled="submitting || completed">
<legend><span>04</span>证书目录与剩余设置</legend>
<div class="setup-grid two-columns">
<label>
@@ -0,0 +1,170 @@
import assert from "node:assert/strict";
import test from "node:test";
import { useSetupWizard } from "./SetupWizard.js";
import { aliyunProvider, deferred, mountComposable } from "../../test-support/mount.js";
function completion() {
return Response.json({ completed: true, management_url: "https://203.0.113.20:8443/" });
}
async function setup(t, complete = completion, driver = "sqlite") {
const submitted = [];
const emitted = [];
const mounted = await mountComposable(t, () => useSetupWizard({ status: {
mode: "bootstrap",
web_public_ip: "203.0.113.20",
server_port: 8443,
certificate_root: "/srv/scert/ssl",
} }, (...args) => emitted.push(args)), async (path, options) => {
if (path.endsWith("/providers")) return Response.json({ items: [aliyunProvider] });
if (path.endsWith("/test")) return Response.json({
verified: true,
driver,
verification_token: path.includes("database") ? "database-token" : "dns-token",
expires_at: new Date(Date.now() + 60000).toISOString(),
});
assert.equal(path, "/api/v1/bootstrap/complete");
submitted.push(JSON.parse(options.body));
return complete();
});
const { state } = mounted;
Object.assign(state.form.value, {
admin_username: "test-admin",
admin_password: "TestPass123",
confirm_password: "TestPass123",
});
state.handleEmailInput("admin@example.test");
state.dnsCredentials.value = { access_key_id: "test-key" };
state.dnsTestDomain.value = "authorized.example.test";
state.databaseForm.value.driver = driver;
await state.testDatabase();
await state.testDNS();
assert.equal(state.databaseVerified.value, true);
assert.equal(state.dnsVerificationValid.value, true);
return { ...mounted, submitted, emitted };
}
for (const driver of ["sqlite", "mysql", "postgres"]) {
test(`首次向导 ${driver} 完整流程只提交验证令牌并保留通知配置`, async (t) => {
const { state, submitted, emitted } = await setup(t, completion, driver);
Object.assign(state.form.value, {
email_http_enabled: true,
email_endpoint: "https://mail.example.test",
email_http_auth_mode: "bearer",
email_retry_count: 0,
recipients: "first@example.test,second@example.test",
email_http_credential_ref: "secret://client/not-trusted",
});
state.emailCredentials.value = JSON.stringify({ token: "test-notification-token" });
for (let step = 1; step < 4; step += 1) {
assert.equal(state.currentStep.value, step);
await state.handleSubmit();
assert.equal(submitted.length, 0);
assert.equal(state.currentStep.value, step + 1);
}
await state.handleSubmit();
assert.equal(state.error.value, "");
assert.equal(state.completed.value, true);
assert.equal(submitted.length, 1);
assert.equal(submitted[0].database_verification_token, "database-token");
assert.equal(submitted[0].dns_verification_token, "dns-token");
assert.equal(submitted[0].web_public_ip, "203.0.113.20");
assert.equal(submitted[0].notification.http_enabled, true);
assert.equal(submitted[0].notification.retry_count, 0);
assert.deepEqual(submitted[0].notification.recipients, ["first@example.test", "second@example.test"]);
assert.deepEqual(submitted[0].notification.http_credentials, { token: "test-notification-token" });
for (const key of ["dns_credential_ref", "certificate_root", "database", "confirm_password"]) {
assert.equal(Object.hasOwn(submitted[0], key), false);
}
assert.equal(Object.hasOwn(submitted[0].notification, "http_credential_ref"), false);
assert.equal(emitted.length, 1);
assert.equal(emitted[0][0], "initialized");
});
}
test("首次向导提交中和完成后禁止重复提交", async (t) => {
const pending = deferred();
const { state, submitted, emitted } = await setup(t, () => pending.promise);
state.currentStep.value = 4;
const task = state.handleSubmit();
assert.equal(state.submitting.value, true);
await state.handleSubmit();
assert.equal(submitted.length, 1);
pending.resolve(completion());
await task;
await state.handleSubmit();
assert.equal(submitted.length, 1);
assert.equal(emitted.length, 1);
assert.equal(state.submitting.value, false);
});
test("首次向导提交失败清除已领取令牌,重新验证后允许重试", async (t) => {
let attempts = 0;
const { state, submitted, emitted } = await setup(t, () => ++attempts === 1
? Response.json({ error: "证书签发失败" }, { status: 502 }) : completion());
state.currentStep.value = 4;
await state.handleSubmit();
assert.match(state.error.value, /证书签发失败/);
assert.equal(state.completed.value, false);
assert.equal(state.databaseVerification.value, null);
assert.equal(state.dnsVerification.value, null);
assert.equal(emitted.length, 0);
await state.handleSubmit();
assert.equal(submitted.length, 1);
await state.testDatabase();
await state.testDNS();
await state.handleSubmit();
assert.equal(submitted.length, 2);
assert.equal(state.completed.value, true);
});
test("首次向导验证到期即使未触发界面时钟也禁止提交", async (t) => {
const { state, submitted, advanceTime } = await setup(t);
state.currentStep.value = 4;
advanceTime(60001);
await state.handleSubmit();
assert.equal(submitted.length, 0);
assert.match(state.error.value, /过期/);
});
test("首次向导最终提交重新校验全部步骤", async (t) => {
const { state, submitted } = await setup(t);
for (const [field, value, step] of [
["server_port", 0, 1],
["email_http_headers", "[]", 2],
["propagation_polling_interval_secs", 99999, 3],
["certificate_root", "", 4],
]) {
const original = state.form.value[field];
state.form.value[field] = value;
state.currentStep.value = 4;
await state.handleSubmit();
assert.equal(submitted.length, 0);
assert.equal(state.currentStep.value, step);
assert.notEqual(state.error.value, "");
state.form.value[field] = original;
}
});
test("首次向导拒绝畸形成功响应而不是误报完成", async (t) => {
const responses = [
{},
null,
{ completed: false, management_url: "https://203.0.113.20/" },
{ completed: true, management_url: "invalid" },
{ completed: true, management_url: "http://203.0.113.20/" },
{ completed: true, management_url: "https://user:password@example.test/" },
];
const { state, emitted } = await setup(t, () => Response.json(responses.shift()));
while (responses.length) {
await state.testDatabase();
await state.testDNS();
state.currentStep.value = 4;
await state.handleSubmit();
assert.equal(state.completed.value, false);
assert.equal(emitted.length, 0);
assert.match(state.error.value, /响应不完整/);
assert.equal(state.databaseVerification.value, null);
assert.equal(state.dnsVerification.value, null);
}
});