完成证书导入后台任务及上传回收,更新回归进度
This commit is contained in:
1 parent
09616e732d
commit
fce5a84dd4
17 files changed
+473
-15
No files matched your search
@@ -64,6 +64,7 @@ export function useDashboard() {
|
||||
const importSaving = ref(false);
|
||||
const importError = ref("");
|
||||
const importResult = ref(null);
|
||||
let importIdempotencyKey = "";
|
||||
const importFiles = ref({
|
||||
certificate: null,
|
||||
private_key: null,
|
||||
@@ -839,6 +840,8 @@ export function useDashboard() {
|
||||
|
||||
function openCertificateImport(item) {
|
||||
importDomain.value = item;
|
||||
const suffix = crypto.randomUUID ? crypto.randomUUID() : createRandomSuffix();
|
||||
importIdempotencyKey = `import-${item.id}-${suffix}`;
|
||||
importFiles.value = {
|
||||
certificate: null,
|
||||
private_key: null,
|
||||
@@ -882,9 +885,25 @@ export function useDashboard() {
|
||||
}
|
||||
const response = await fetchAdmin(
|
||||
`/api/v1/domains/${importDomain.value.id}/certificate/import`,
|
||||
{ method: "POST", body },
|
||||
{ method: "POST", body, headers: { "Idempotency-Key": importIdempotencyKey } },
|
||||
);
|
||||
const result = await response.json().catch(() => ({}));
|
||||
if (response.status === 202 && !result.task?.id) {
|
||||
throw new Error("服务未返回任务编号,请在运行概览确认是否已接收,勿重复提交");
|
||||
}
|
||||
if ((response.ok || response.status === 409 && result.replayed) && result.task?.id &&
|
||||
(response.status === 202 || result.accepted || result.replayed)) {
|
||||
importResult.value = result;
|
||||
showCertificateImport.value = false;
|
||||
notice.value = `导入任务已提交:${result.task.id},可在运行概览查看执行结果`;
|
||||
deploymentResult.value = {
|
||||
domain: importDomain.value.name, running: false, pending: true,
|
||||
taskID: result.task.id, taskStatus: result.task.status,
|
||||
taskMessage: result.task.message || "", report: null, error: "", queryError: "",
|
||||
};
|
||||
await loadData();
|
||||
return;
|
||||
}
|
||||
if (!response.ok && result.imported !== true) {
|
||||
throw new Error(result.error || "导入证书失败");
|
||||
}
|
||||
|
||||
@@ -192,6 +192,71 @@ test("certificateImport.submitsSelectedFilesAndResetsOnReopen", async (t) => {
|
||||
assert.ok(state.importError.value);
|
||||
});
|
||||
|
||||
test("导入异步接收只展示任务并等待结果,不提前宣布保存成功", async (t) => {
|
||||
let imports = 0;
|
||||
const { state } = await mountComposable(t, useDashboard, async (path) => {
|
||||
if (path.endsWith("/certificate/import")) {
|
||||
imports++;
|
||||
return Response.json({ accepted: true, task: { id: "import-task", status: "queued" } }, { status: 202 });
|
||||
}
|
||||
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.deploymentResult.value.taskID, "import-task");
|
||||
assert.equal(state.deploymentResult.value.pending, true);
|
||||
assert.equal(state.deploymentResult.value.report, null);
|
||||
assert.notEqual(state.deploymentResult.value.certificateImported, true);
|
||||
assert.equal(state.showCertificateImport.value, false);
|
||||
assert.match(state.notice.value, /导入任务已提交/);
|
||||
assert.doesNotMatch(state.notice.value, /证书已导入/);
|
||||
await state.importCertificate();
|
||||
assert.equal(imports, 1);
|
||||
});
|
||||
|
||||
test("导入响应断开后使用原幂等键找回任务,不重复执行导入", async (t) => {
|
||||
const keys = [];
|
||||
const { state } = await mountComposable(t, useDashboard, async (path, options) => {
|
||||
if (path.endsWith("/certificate/import")) {
|
||||
keys.push(options.headers["Idempotency-Key"]);
|
||||
if (keys.length === 1) throw new Error("连接断开");
|
||||
return Response.json({ replayed: true, task: { id: "existing-import", status: "running" } }, { status: 409 });
|
||||
}
|
||||
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.match(state.importError.value, /连接断开/);
|
||||
await state.importCertificate();
|
||||
assert.equal(keys.length, 2);
|
||||
assert.match(keys[0], /^import-domain-import-/);
|
||||
assert.equal(keys[0], keys[1]);
|
||||
assert.equal(state.importError.value, "");
|
||||
assert.equal(state.deploymentResult.value.taskID, "existing-import");
|
||||
assert.equal(state.deploymentResult.value.pending, true);
|
||||
});
|
||||
|
||||
test("异步导入缺少任务编号时不能显示导入成功", async (t) => {
|
||||
const { state } = await mountComposable(t, useDashboard, async (path) => {
|
||||
if (path.endsWith("/certificate/import")) return Response.json({ accepted: true }, { status: 202 });
|
||||
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.match(state.importError.value, /未返回任务编号/);
|
||||
assert.equal(state.importResult.value, null);
|
||||
assert.doesNotMatch(state.notice.value, /证书已导入/);
|
||||
});
|
||||
|
||||
test("证书已导入但部署失败时展示服务器结果并刷新状态,不能重复导入", async (t) => {
|
||||
let imports = 0;
|
||||
let refreshes = 0;
|
||||
|
||||
@@ -110,6 +110,7 @@ export function useTaskMonitor({ fetchAdmin, tasks, deploymentResult, showTaskDe
|
||||
taskStatus: snapshot.task.status,
|
||||
taskMessage: snapshot.task.message || "",
|
||||
report: snapshot.report,
|
||||
certificateImported: snapshot.report?.certificate_imported === true,
|
||||
queryError: "",
|
||||
};
|
||||
}
|
||||
@@ -130,6 +131,7 @@ export function useTaskMonitor({ fetchAdmin, tasks, deploymentResult, showTaskDe
|
||||
deploymentResult.value.queryError = message;
|
||||
if (denied) {
|
||||
deploymentResult.value.report = null;
|
||||
deploymentResult.value.certificateImported = false;
|
||||
deploymentResult.value.taskMessage = "";
|
||||
deploymentResult.value.taskStatus = "";
|
||||
}
|
||||
@@ -149,6 +151,7 @@ export function useTaskMonitor({ fetchAdmin, tasks, deploymentResult, showTaskDe
|
||||
taskDeploymentReport.value = null;
|
||||
if (deploymentResult.value) {
|
||||
deploymentResult.value.report = null;
|
||||
deploymentResult.value.certificateImported = false;
|
||||
deploymentResult.value.taskMessage = "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +91,30 @@ test("页面刷新查询完整报告并展示恢复状态", async (t) => {
|
||||
assert.match(deploymentSummary(state.taskDeploymentReport.value), /恢复/);
|
||||
});
|
||||
|
||||
test("异步导入从持久化报告恢复保存状态,不因部署失败诱导重复导入", async (t) => {
|
||||
const task = { ...initialTask, task_type: "certificate_import", status: "failed", message: "证书已导入,但部署未完成" };
|
||||
const fetchTask = taskFetcher(() => task);
|
||||
const { state } = await mountComposable(t, useDashboard, (path, options) => {
|
||||
if (path.endsWith("/attempts")) {
|
||||
return Response.json({ items: [{ id: "import-attempt", attempt_number: 1, status: "failed" }] });
|
||||
}
|
||||
if (path.endsWith("/deployment-results")) {
|
||||
return Response.json({ items: [], reports: [{
|
||||
task_id: task.id, attempt_id: "import-attempt", attempt_number: 1,
|
||||
report: { status: "failed", results: [], certificate_imported: true },
|
||||
}] });
|
||||
}
|
||||
return fetchTask(path, options);
|
||||
});
|
||||
state.deploymentResult.value = { taskID: task.id, pending: true };
|
||||
await state.refreshTaskStatus();
|
||||
assert.equal(state.deploymentResult.value.pending, false);
|
||||
assert.equal(state.deploymentResult.value.certificateImported, true);
|
||||
assert.equal(state.deploymentResult.value.taskStatus, "failed");
|
||||
await state.openTaskDetails(task);
|
||||
assert.equal(state.taskDeploymentReport.value.certificate_imported, true);
|
||||
});
|
||||
|
||||
test("异步部署接收后不误报成功或缺失结果,最终失败不被逐目标成功覆盖", async (t) => {
|
||||
let current = { ...initialTask };
|
||||
const posts = [];
|
||||
@@ -265,12 +289,14 @@ test("项目撤权后清除缓存的任务详情和部署结果", async (t) => {
|
||||
});
|
||||
await state.openTaskDetails(current);
|
||||
assert.equal(state.taskDeploymentReport.value.results.length, 1);
|
||||
state.deploymentResult.value = { taskID: current.id, pending: true, certificateImported: true };
|
||||
denied = true;
|
||||
await state.refreshTaskStatus();
|
||||
assert.equal(state.taskDetails.value, null);
|
||||
assert.equal(state.taskDeploymentReport.value, null);
|
||||
assert.deepEqual(state.taskAttempts.value, []);
|
||||
assert.match(state.taskDetailsError.value, /无权读取/);
|
||||
assert.equal(state.deploymentResult.value.certificateImported, false);
|
||||
});
|
||||
|
||||
test("任务完成后自动刷新域名证书数据,但不覆盖正在编辑的设置", async (t) => {
|
||||
|
||||
Reference in new issue
Block a user