区分证书导入与部署结果并阻止重复导入

This commit is contained in:
xuans committed 2026-09-27 01:05:19 +08:00
1 parent 4798d24f4b
commit 9ac222d6db
6 files changed
+149 -7

No files matched your search

+12 -1
View File
@@ -1013,6 +1013,7 @@ export function useDashboard() {
}
async function importCertificate() {
if (importSaving.value || importResult.value) return;
importError.value = "";
importResult.value = null;
if (!importFiles.value.certificate || !importFiles.value.private_key) {
@@ -1038,11 +1039,21 @@ export function useDashboard() {
{ method: "POST", body },
);
const result = await response.json().catch(() => ({}));
if (!response.ok) {
if (!response.ok && result.imported !== true) {
throw new Error(result.error || "导入证书失败");
}
importResult.value = result;
notice.value = `域名 ${importDomain.value.name} 的证书已导入`;
if (!response.ok || result.deployment?.total > 0) {
showCertificateImport.value = false;
deploymentResult.value = {
domain: importDomain.value.name,
running: false,
certificateImported: true,
report: result.deployment || null,
error: response.ok ? "" : result.error || "证书已导入,但部署未完成。请重试部署,无需重新导入。",
};
}
await loadData();
} catch (err) {
importError.value = err.message || "导入证书失败";
@@ -163,6 +163,83 @@ test("certificateImport.submitsSelectedFilesAndResetsOnReopen", async (t) => {
assert.ok(state.importError.value);
});
test("证书已导入但部署失败时展示服务器结果并刷新状态,不能重复导入", async (t) => {
let imports = 0;
let refreshes = 0;
const report = {
total: 1, success: 0, failed: 1, status: "failed",
results: [{ server_id: "server-1", server: "验收服务器", status: "failed", message: "连接服务器失败" }],
};
const message = "证书已导入,但部署未完成。请修正服务器配置后重试部署,无需重新导入。";
const { state } = await mountComposable(t, useDashboard, async (path) => {
if (path.endsWith("/certificate/import")) {
imports++;
return Response.json({ imported: true, deployment: report, error: message }, { status: 502 });
}
if (path === "/api/v1/domains") refreshes++;
return Response.json({ items: [] });
});
const before = refreshes;
state.openCertificateImport({ id: "domain-import", name: "example.test" });
for (const kind of ["certificate", "private_key"]) {
state.selectCertificateImportFile(kind, { target: { files: [new File([kind], `${kind}.pem`)] } });
}
await state.importCertificate();
assert.equal(state.importResult.value?.imported, true);
assert.equal(state.importError.value, "");
assert.equal(state.showCertificateImport.value, false);
assert.equal(state.deploymentResult.value.certificateImported, true);
assert.equal(state.deploymentResult.value.error, message);
assert.deepEqual(state.deploymentResult.value.report, report);
assert.ok(refreshes > before);
assert.match(state.notice.value, /证书已导入/);
await state.importCertificate();
assert.equal(imports, 1);
});
test("证书未保存时保留导入表单并显示真正的导入失败", async (t) => {
const { state } = await mountComposable(t, useDashboard, async (path) => {
if (path.endsWith("/certificate/import")) {
return Response.json({ imported: false, error: "证书导入失败" }, { status: 502 });
}
return Response.json({ items: [] });
});
state.openCertificateImport({ id: "domain-import", name: "example.test" });
for (const kind of ["certificate", "private_key"]) {
state.selectCertificateImportFile(kind, { target: { files: [new File([kind], `${kind}.pem`)] } });
}
await state.importCertificate();
assert.equal(state.importResult.value, null);
assert.equal(state.deploymentResult.value, null);
assert.equal(state.showCertificateImport.value, true);
assert.equal(state.importError.value, "证书导入失败");
});
test("导入执行中阻止重复提交,成功后展示实际部署结果", async (t) => {
const pending = deferred();
let imports = 0;
const report = { total: 1, success: 1, failed: 0, status: "success", results: [{ server_id: "server-1", status: "success" }] };
const { state } = await mountComposable(t, useDashboard, async (path) => {
if (path.endsWith("/certificate/import")) {
imports++;
return pending.promise;
}
return Response.json({ items: [] });
});
state.openCertificateImport({ id: "domain-import", name: "example.test" });
for (const kind of ["certificate", "private_key"]) {
state.selectCertificateImportFile(kind, { target: { files: [new File([kind], `${kind}.pem`)] } });
}
const action = state.importCertificate();
const duplicate = state.importCertificate();
assert.equal(imports, 1);
pending.resolve(Response.json({ imported: true, deployment: report }));
await Promise.all([action, duplicate]);
assert.equal(state.showCertificateImport.value, false);
assert.equal(state.deploymentResult.value.error, "");
assert.deepEqual(state.deploymentResult.value.report, report);
});
async function setup(t, request) {
const mounted = await mountComposable(t, useDashboard, async (path) => {
if (path === "/api/v1/dns/test") return request();
+1 -1
View File
@@ -544,7 +544,7 @@ const { activeSection, activeLabel, sections, selectSection } = useDashboardNavi
</div>
<div class="modal-actions">
<button class="secondary-button" type="button" :disabled="importSaving" @click="closeCertificateImport">关闭</button>
<button class="primary-button" type="submit" :disabled="importSaving">{{ importSaving ? "导入中……" : "导入证书" }}</button>
<button class="primary-button" type="submit" :disabled="importSaving || !!importResult">{{ importSaving ? "导入中……" : importResult ? "已导入" : "导入证书" }}</button>
</div>
</form>
</section>
@@ -16,7 +16,7 @@ defineEmits(["close"]);
>
<div class="modal-heading">
<div>
<h2 id="deployment-result-title">证书部署结果</h2>
<h2 id="deployment-result-title">{{ result.certificateImported ? "证书导入与部署结果" : "证书部署结果" }}</h2>
<p class="muted">{{ result.domain }}</p>
</div>
<button
@@ -29,6 +29,7 @@ defineEmits(["close"]);
</div>
<p v-if="result.running" role="status">正在连接服务器、推送证书并校验重载,请稍候……</p>
<template v-else>
<p v-if="result.certificateImported" role="status">证书已导入并保存。</p>
<p v-if="result.error" class="form-error" role="alert">{{ result.error }}</p>
<p v-if="result.report" class="deployment-summary" role="status">
{{ deploymentSummary(result.report) }}