fix license check

This commit is contained in:
2026-08-03 15:18:28 +08:00
parent 2090597858
commit bae71f3253
11 changed files with 469 additions and 20 deletions
+151
View File
@@ -773,4 +773,155 @@ test("license creation verifies the signed payload and validates expiry in real
assert.deepEqual(await post({
type: "validateLicense", licenseId: expiredId, deviceId: line.productionLine.deviceId
}), { success: true, valid: false, status: "expired", licenseId: expiredId });
});
test("deleteLicense requires prior revocation", async () => {
const suffix = crypto.randomUUID().slice(0, 8);
const company = await post({
type: "createCompany", name: `删除许可证公司-${suffix}`, code: `del-lic-${suffix}`,
adminToken: "test-token"
});
const line = await post({
type: "createProductionLine", companyId: company.company.id,
name: "许可证线", code: "line-1", adminToken: "test-token"
});
const licenseId = crypto.randomUUID();
const payload = {
license_id: licenseId,
company_id: company.company.id,
production_line_id: line.productionLine.id,
customer: company.company.name,
device_id: line.productionLine.deviceId,
issued: "2026-08-01 10:00",
expiry: "2028-08-01 10:00",
features: "*"
};
const created = await post({
type: "createLicense", licenseId,
companyId: company.company.id,
productionLineId: line.productionLine.id,
customer: payload.customer,
issued: payload.issued,
expiry: payload.expiry,
features: payload.features,
license: signLicense(payload),
adminToken: "test-token"
});
assert.equal(created.success, true);
const activeDelete = await post({ type: "deleteLicense", licenseId, adminToken: "test-token" });
assert.equal(activeDelete.success, false);
assert.equal(activeDelete.errCode, "LICENSE_ACTIVE");
const revoked = await post({
type: "revokeLicense", licenseId, reason: "测试删除", adminToken: "test-token"
});
assert.equal(revoked.success, true);
const deleted = await post({ type: "deleteLicense", licenseId, adminToken: "test-token" });
assert.deepEqual(deleted, { success: true, deletedLicenseId: licenseId });
const missing = await post({ type: "getLicense", licenseId, adminToken: "test-token" });
assert.equal(missing.success, false);
});
test("deleteProductionLine blocks active licenses and removes revoked data", async () => {
const suffix = crypto.randomUUID().slice(0, 8);
const company = await post({
type: "createCompany", name: `删除产线公司-${suffix}`, code: `del-line-${suffix}`,
adminToken: "test-token"
});
const line = await post({
type: "createProductionLine", companyId: company.company.id,
name: "待删产线", code: "line-1", adminToken: "test-token"
});
const modelIssued = await post({
type: "issueModelUpload", deviceId: line.productionLine.deviceId,
fileName: "controller.bin", adminToken: "test-token"
});
const modelForm = new FormData();
modelForm.append("file", new Blob(["model-bytes"]), "controller.bin");
assert.equal((await fetch(modelIssued.uploadMetadata.url, { method: "POST", body: modelForm })).status, 204);
const licenseId = crypto.randomUUID();
const payload = {
license_id: licenseId,
company_id: company.company.id,
production_line_id: line.productionLine.id,
customer: company.company.name,
device_id: line.productionLine.deviceId,
issued: "2026-08-01 10:00",
expiry: "2028-08-01 10:00",
features: "*"
};
await post({
type: "createLicense", licenseId,
companyId: company.company.id,
productionLineId: line.productionLine.id,
customer: payload.customer,
issued: payload.issued,
expiry: payload.expiry,
features: payload.features,
license: signLicense(payload),
adminToken: "test-token"
});
const blocked = await post({
type: "deleteProductionLine",
companyId: company.company.id,
productionLineId: line.productionLine.id,
adminToken: "test-token"
});
assert.equal(blocked.success, false);
assert.equal(blocked.errCode, "ACTIVE_LICENSES_PRESENT");
await post({ type: "revokeLicense", licenseId, reason: "产线删除", adminToken: "test-token" });
const deleted = await post({
type: "deleteProductionLine",
companyId: company.company.id,
productionLineId: line.productionLine.id,
adminToken: "test-token"
});
assert.equal(deleted.success, true);
assert.ok(deleted.deletedFiles >= 1);
assert.ok(deleted.deletedLicenses >= 1);
const organizations = await post({ type: "listOrganizations", adminToken: "test-token" });
const listedCompany = organizations.companies.find((item) => item.id === company.company.id);
assert.ok(listedCompany);
assert.equal(listedCompany.productionLines.some((item) => item.id === line.productionLine.id), false);
const models = await post({ type: "listModels", folder: `${line.productionLine.deviceId}/model_config` });
assert.deepEqual(models.fileList, []);
});
test("deleteCompany requires no child lines and no remaining licenses", async () => {
const suffix = crypto.randomUUID().slice(0, 8);
const company = await post({
type: "createCompany", name: `删除公司-${suffix}`, code: `del-co-${suffix}`,
adminToken: "test-token"
});
const line = await post({
type: "createProductionLine", companyId: company.company.id,
name: "子产线", code: "line-1", adminToken: "test-token"
});
const blocked = await post({ type: "deleteCompany", companyId: company.company.id, adminToken: "test-token" });
assert.equal(blocked.success, false);
assert.equal(blocked.errCode, "PRODUCTION_LINES_PRESENT");
const lineDeleted = await post({
type: "deleteProductionLine",
companyId: company.company.id,
productionLineId: line.productionLine.id,
adminToken: "test-token"
});
assert.equal(lineDeleted.success, true);
const deleted = await post({ type: "deleteCompany", companyId: company.company.id, adminToken: "test-token" });
assert.deepEqual(deleted, { success: true, deletedCompanyId: company.company.id });
const organizations = await post({ type: "listOrganizations", adminToken: "test-token" });
assert.equal(organizations.companies.some((item) => item.id === company.company.id), false);
});