Files
perler-lab/perler-lab-server/src/tests/download.test.ts
T

144 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect } from "vitest";
import request from "supertest";
import type Database from "better-sqlite3";
import type { Express } from "express";
import { createApp } from "../server.js";
import { createTestDatabase } from "../db/index.js";
import { generateCodes, insertCodes, getCodeStatus } from "../codes/service.js";
/** 建一个含有效兑换码的测试库。 */
function setupDb(uses = 3): { db: Database.Database; code: string } {
const db = createTestDatabase();
const codes = generateCodes(1);
insertCodes(db, codes, "体验包", uses);
return { db, code: codes[0]! };
}
/** 构造合法 generate 请求体。 */
function genRequest(code: string, overrides: Record<string, unknown> = {}): Record<string, unknown> {
return {
code,
image: "data:image/png;base64,iVBORw0KGgo=",
category: "pet",
subtype: "cat",
gridSize: 48,
colorCount: 16,
composition: "主体特写",
detailTags: [],
...overrides,
};
}
/**
* 通过真实 generate 流程签发一张下载授权(mock 模式,无需 AI 接口)。
* 返回 { grant, app, db, code }。
*/
async function issueGrant(
app: Express,
code: string,
): Promise<{ grant: string; res: request.Response }> {
const res = await request(app).post("/api/chibi/generate").send(genRequest(code));
expect(res.status).toBe(200);
expect(res.body.downloadGrant).toMatch(/^[0-9a-f]{32}$/);
return { grant: res.body.downloadGrant as string, res };
}
const FAKE_GRANT = "ab".repeat(16); // 合法长度但数据库中不存在
describe("下载授权 (download_grants) 核销", () => {
it("生成后持有效 grant 消费成功,且 grant 被标记为已使用", async () => {
const { db, code } = setupDb(3);
const app = createApp(db);
// 生成消耗 1 次,签发 grant
const { grant } = await issueGrant(app, code);
expect(getCodeStatus(db, code).remainingUses).toBe(2);
// 带 grant 消费:校验通过 + 扣减 + 置 used
const res = await request(app)
.post("/api/codes/consume")
.send({ code, fileName: "chibi.png", downloadGrant: grant });
expect(res.status).toBe(200);
expect(res.body.access.remainingUses).toBe(1);
expect(res.body.freeDownload).toBe(true);
const row = db.prepare("SELECT used FROM download_grants WHERE grant = ?").get(grant) as {
used: number;
};
expect(row.used).toBe(1);
});
it("已使用的 grant 再次消费返回 403,且不再扣减次数", async () => {
const { db, code } = setupDb(3);
const app = createApp(db);
const { grant } = await issueGrant(app, code);
const first = await request(app)
.post("/api/codes/consume")
.send({ code, fileName: "a.png", downloadGrant: grant });
expect(first.status).toBe(200);
const second = await request(app)
.post("/api/codes/consume")
.send({ code, fileName: "b.png", downloadGrant: grant });
expect(second.status).toBe(403);
expect(second.body.error).toBe("下载授权无效或已使用");
// 失败时次数保持不变(generate 1 次 + 有效消费 1 次 = 剩 1)
expect(getCodeStatus(db, code).remainingUses).toBe(1);
});
it("伪造/不存在的 grant 返回 403,且不扣减次数", async () => {
const { db, code } = setupDb(3);
const app = createApp(db);
await issueGrant(app, code); // generate 消耗 1 次
const res = await request(app)
.post("/api/codes/consume")
.send({ code, fileName: "a.png", downloadGrant: FAKE_GRANT });
expect(res.status).toBe(403);
expect(res.body.error).toBe("下载授权无效或已使用");
expect(getCodeStatus(db, code).remainingUses).toBe(2);
});
it("不带 grant 直接消费仍可用(向后兼容)", async () => {
const { db, code } = setupDb(3);
const app = createApp(db);
const res = await request(app)
.post("/api/codes/consume")
.send({ code, fileName: "a.png" });
expect(res.status).toBe(200);
expect(res.body.access.remainingUses).toBe(2);
expect(getCodeStatus(db, code).remainingUses).toBe(2);
});
it("grant 归属其他兑换码时返回 403,且双方状态不变", async () => {
const db = createTestDatabase();
const codes = generateCodes(2);
insertCodes(db, codes, "体验包", 3);
const [codeA, codeB] = codes;
const app = createApp(db);
// A 生成签发 grant(A 剩 2)
const { grant } = await issueGrant(app, codeA!);
// 用 B 的兑换码携带 A 的 grant 消费 → 403
const res = await request(app)
.post("/api/codes/consume")
.send({ code: codeB, fileName: "a.png", downloadGrant: grant });
expect(res.status).toBe(403);
expect(res.body.error).toBe("下载授权无效或已使用");
// B 未扣减,grant 未被置 used
expect(getCodeStatus(db, codeB!).remainingUses).toBe(3);
const row = db.prepare("SELECT used FROM download_grants WHERE grant = ?").get(grant) as {
used: number;
};
expect(row.used).toBe(0);
});
});