test(admin): admin.test.ts 32 测试覆盖 auth 守卫/用户/订单/兑换码/审核/公告 CRUD
This commit is contained in:
1 parent
c48b1c74c2
commit
6970052cfb
1 file changed
+639
@@ -0,0 +1,639 @@
|
||||
/**
|
||||
* 管理后台 API 测试 —— 看板、用户、订单、兑换码、作品、公告。
|
||||
*
|
||||
* 注意:requireAdmin 中间件使用 getDb()(全局单例),因此无法使用
|
||||
* createApp(createTestDatabase()) 注入独立库。测试改用
|
||||
* initDatabase({ dbPath: ":memory:" }) 初始化全局单例,
|
||||
* createApp() 不带参数让所有路由都走 getDb()。
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import request from "supertest";
|
||||
import type { Express } from "express";
|
||||
import { createApp } from "../server.js";
|
||||
import { initDatabase, closeDatabase } from "../db/index.js";
|
||||
import type { SqliteDb } from "../db/index.js";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 全局状态(每 describe 块 beforeEach 重置)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
let db: SqliteDb;
|
||||
let app: Express;
|
||||
|
||||
beforeEach(async () => {
|
||||
await closeDatabase();
|
||||
db = initDatabase({ dbPath: ":memory:" });
|
||||
app = createApp();
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 辅助函数
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** 注册用户并返回 token。 */
|
||||
async function registerUser(
|
||||
phone: string,
|
||||
password: string,
|
||||
): Promise<{ token: string; userId: string }> {
|
||||
const res = await request(app).post("/api/auth/register").send({ phone, password });
|
||||
return { token: res.body.token, userId: res.body.user.id };
|
||||
}
|
||||
|
||||
/** 将用户提升为管理员(直接操作数据库)。 */
|
||||
function promoteToAdmin(phone: string): void {
|
||||
db.prepare("UPDATE users SET role='admin' WHERE phone=?").run(phone);
|
||||
}
|
||||
|
||||
/** 获取管理员 token:注册 + 提升。 */
|
||||
async function getAdminToken(phone = "13800000001"): Promise<string> {
|
||||
const { token } = await registerUser(phone, "admin123");
|
||||
promoteToAdmin(phone);
|
||||
return token;
|
||||
}
|
||||
|
||||
/** 获取普通用户 token。 */
|
||||
async function getRegularToken(phone = "13800000002"): Promise<string> {
|
||||
const { token } = await registerUser(phone, "user123");
|
||||
return token;
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// 鉴权守卫
|
||||
// ===========================================================================
|
||||
|
||||
describe("Auth guard — GET /api/admin/stats", () => {
|
||||
it("无 token 返回 401", async () => {
|
||||
const res = await request(app).get("/api/admin/stats");
|
||||
expect(res.status).toBe(401);
|
||||
expect(res.body.error).toBe("请先登录");
|
||||
});
|
||||
|
||||
it("普通用户 token 返回 403", async () => {
|
||||
const token = await getRegularToken();
|
||||
const res = await request(app)
|
||||
.get("/api/admin/stats")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
expect(res.status).toBe(403);
|
||||
expect(res.body.error).toBe("无权限访问");
|
||||
});
|
||||
|
||||
it("管理员 token 返回 200", async () => {
|
||||
const token = await getAdminToken();
|
||||
const res = await request(app)
|
||||
.get("/api/admin/stats")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
});
|
||||
|
||||
// ===========================================================================
|
||||
// 看板统计
|
||||
// ===========================================================================
|
||||
|
||||
describe("GET /api/admin/stats", () => {
|
||||
it("返回正确的聚合计数", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
// 创建一个用户 -> 用户数应 > 0
|
||||
// 插入一条展示作品
|
||||
db.prepare(
|
||||
"INSERT INTO showcase_works (title, image_data, tags, source) VALUES (?, ?, ?, ?)",
|
||||
).run("测试作品", "data:image/png;base64,abc", '["test"]', "manual");
|
||||
|
||||
// 插入一条兑换码
|
||||
db.prepare(
|
||||
"INSERT INTO codes (code, package_type, total_uses, used_uses, status) VALUES (?, ?, ?, ?, ?)",
|
||||
).run("TESTCODE00000001", "体验包", 3, 0, "active");
|
||||
|
||||
const res = await request(app)
|
||||
.get("/api/admin/stats")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveProperty("users");
|
||||
expect(res.body).toHaveProperty("orders");
|
||||
expect(res.body).toHaveProperty("chibiGens");
|
||||
expect(res.body).toHaveProperty("codes");
|
||||
expect(res.body).toHaveProperty("activeCodes");
|
||||
// 至少有注册的管理员 + 1 个兑换码 + 1 个活跃码
|
||||
expect(res.body.users).toBeGreaterThanOrEqual(1);
|
||||
expect(res.body.codes).toBeGreaterThanOrEqual(1);
|
||||
expect(res.body.activeCodes).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
// ===========================================================================
|
||||
// 用户管理
|
||||
// ===========================================================================
|
||||
|
||||
describe("GET /api/admin/users", () => {
|
||||
it("返回分页用户列表", async () => {
|
||||
const token = await getAdminToken();
|
||||
// 注册一个额外用户
|
||||
await registerUser("13800000003", "pass123");
|
||||
|
||||
const res = await request(app)
|
||||
.get("/api/admin/users")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveProperty("total");
|
||||
expect(res.body).toHaveProperty("items");
|
||||
expect(Array.isArray(res.body.items)).toBe(true);
|
||||
expect(res.body.total).toBeGreaterThanOrEqual(2);
|
||||
expect(res.body.items.length).toBeGreaterThanOrEqual(1);
|
||||
expect(res.body.items[0]).toHaveProperty("id");
|
||||
expect(res.body.items[0]).toHaveProperty("phone");
|
||||
expect(res.body.items[0]).toHaveProperty("role");
|
||||
expect(res.body.items[0]).toHaveProperty("createdAt");
|
||||
});
|
||||
|
||||
it("按手机号模糊搜索", async () => {
|
||||
const token = await getAdminToken();
|
||||
await registerUser("13900000001", "pass123");
|
||||
await registerUser("13900000002", "pass456");
|
||||
|
||||
const res = await request(app)
|
||||
.get("/api/admin/users?phone=139")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.total).toBe(2);
|
||||
expect(res.body.items.every((u: { phone: string }) => u.phone.startsWith("139"))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/admin/users/:id/ban", () => {
|
||||
it("封禁用户成功", async () => {
|
||||
const token = await getAdminToken();
|
||||
const { userId } = await registerUser("13800000010", "pass123");
|
||||
|
||||
const res = await request(app)
|
||||
.post(`/api/admin/users/${userId}/ban`)
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.ok).toBe(true);
|
||||
|
||||
const row = db.prepare("SELECT role FROM users WHERE id = ?").get(userId) as {
|
||||
role: string;
|
||||
} | undefined;
|
||||
expect(row?.role).toBe("banned");
|
||||
});
|
||||
|
||||
it("封禁不存在的用户返回 404", async () => {
|
||||
const token = await getAdminToken();
|
||||
const res = await request(app)
|
||||
.post("/api/admin/users/nonexistent/ban")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body.error).toBe("用户不存在");
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/admin/users/:id/unban", () => {
|
||||
it("解封用户成功", async () => {
|
||||
const token = await getAdminToken();
|
||||
const { userId } = await registerUser("13800000020", "pass123");
|
||||
|
||||
// 先封禁
|
||||
db.prepare("UPDATE users SET role='banned' WHERE id=?").run(userId);
|
||||
// 再解封
|
||||
const res = await request(app)
|
||||
.post(`/api/admin/users/${userId}/unban`)
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.ok).toBe(true);
|
||||
|
||||
const row = db.prepare("SELECT role FROM users WHERE id = ?").get(userId) as {
|
||||
role: string;
|
||||
} | undefined;
|
||||
expect(row?.role).toBe("user");
|
||||
});
|
||||
});
|
||||
|
||||
// ===========================================================================
|
||||
// 订单
|
||||
// ===========================================================================
|
||||
|
||||
describe("GET /api/admin/orders", () => {
|
||||
it("初始为空列表", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const res = await request(app)
|
||||
.get("/api/admin/orders")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.total).toBe(0);
|
||||
expect(res.body.items).toEqual([]);
|
||||
});
|
||||
|
||||
it("插入订单后出现在列表中", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
// 先插入一条兑换码(orders 表有外键 REFERENCES codes(code))
|
||||
db.prepare(
|
||||
"INSERT INTO codes (code, package_type, total_uses, used_uses, status) VALUES (?, ?, ?, ?, ?)",
|
||||
).run("ORDCODE0000001", "体验包", 3, 0, "active");
|
||||
|
||||
db.prepare(
|
||||
"INSERT INTO orders (order_no, package_name, amount_cents, payment_method, status, access_code) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
).run("ORD20250001", "体验包", 490, "wechat", "paid", "ORDCODE0000001");
|
||||
|
||||
const res = await request(app)
|
||||
.get("/api/admin/orders")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.total).toBe(1);
|
||||
expect(res.body.items[0].orderNo).toBe("ORD20250001");
|
||||
expect(res.body.items[0].packageName).toBe("体验包");
|
||||
expect(res.body.items[0].status).toBe("paid");
|
||||
});
|
||||
});
|
||||
|
||||
// ===========================================================================
|
||||
// 兑换码
|
||||
// ===========================================================================
|
||||
|
||||
describe("GET /api/admin/codes", () => {
|
||||
it("初始为空列表", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const res = await request(app)
|
||||
.get("/api/admin/codes")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.total).toBe(0);
|
||||
expect(res.body.items).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/admin/codes/generate", () => {
|
||||
it("生成 3 个兑换码", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const res = await request(app)
|
||||
.post("/api/admin/codes/generate")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({ packageType: "体验包", count: 3 });
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveProperty("codes");
|
||||
expect(Array.isArray(res.body.codes)).toBe(true);
|
||||
expect(res.body.codes.length).toBe(3);
|
||||
// 每个码应为 16 位大写字母数字
|
||||
for (const code of res.body.codes) {
|
||||
expect(code).toMatch(/^[A-Z0-9]{16}$/);
|
||||
}
|
||||
});
|
||||
|
||||
it("无效套餐类型返回 400", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const res = await request(app)
|
||||
.post("/api/admin/codes/generate")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({ packageType: "无效包", count: 3 });
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toBe("套餐类型无效");
|
||||
});
|
||||
|
||||
it("count 为 0 返回 400", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const res = await request(app)
|
||||
.post("/api/admin/codes/generate")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({ packageType: "体验包", count: 0 });
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toBe("数量需在 1-500 之间");
|
||||
});
|
||||
|
||||
it("生成后 GET /api/admin/codes 可查询到", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
await request(app)
|
||||
.post("/api/admin/codes/generate")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({ packageType: "体验包", count: 3 });
|
||||
|
||||
const res = await request(app)
|
||||
.get("/api/admin/codes")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.total).toBe(3);
|
||||
expect(res.body.items.length).toBe(3);
|
||||
expect(res.body.items[0]).toHaveProperty("code");
|
||||
expect(res.body.items[0]).toHaveProperty("packageType");
|
||||
expect(res.body.items[0]).toHaveProperty("status");
|
||||
});
|
||||
});
|
||||
|
||||
// ===========================================================================
|
||||
// 作品展示
|
||||
// ===========================================================================
|
||||
|
||||
describe("GET /api/admin/showcase", () => {
|
||||
it("初始为空列表", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const res = await request(app)
|
||||
.get("/api/admin/showcase")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.total).toBe(0);
|
||||
expect(res.body.items).toEqual([]);
|
||||
});
|
||||
|
||||
it("插入作品后出现在列表中", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
db.prepare(
|
||||
"INSERT INTO showcase_works (title, image_data, tags, source, is_hidden) VALUES (?, ?, ?, ?, ?)",
|
||||
).run("测试作品", "data:image/png;base64,test", '["test"]', "manual", 0);
|
||||
|
||||
const res = await request(app)
|
||||
.get("/api/admin/showcase")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.total).toBe(1);
|
||||
expect(res.body.items[0].title).toBe("测试作品");
|
||||
expect(res.body.items[0].id).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/admin/showcase/:id/hide", () => {
|
||||
it("隐藏作品成功", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const { lastInsertRowid } = db.prepare(
|
||||
"INSERT INTO showcase_works (title, image_data, tags, source) VALUES (?, ?, ?, ?)",
|
||||
).run("可隐藏作品", "data:image/png;base64,test", '[]', "manual");
|
||||
|
||||
const res = await request(app)
|
||||
.post(`/api/admin/showcase/${lastInsertRowid}/hide`)
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.ok).toBe(true);
|
||||
|
||||
const row = db.prepare("SELECT is_hidden FROM showcase_works WHERE id = ?").get(lastInsertRowid) as {
|
||||
is_hidden: number;
|
||||
};
|
||||
expect(row.is_hidden).toBe(1);
|
||||
});
|
||||
|
||||
it("隐藏不存在的作品返回 404", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const res = await request(app)
|
||||
.post("/api/admin/showcase/99999/hide")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body.error).toBe("作品不存在");
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/admin/showcase/:id/show", () => {
|
||||
it("恢复作品成功", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const { lastInsertRowid } = db.prepare(
|
||||
"INSERT INTO showcase_works (title, image_data, tags, source, is_hidden) VALUES (?, ?, ?, ?, ?)",
|
||||
).run("可恢复作品", "data:image/png;base64,test", '[]', "manual", 1);
|
||||
|
||||
const res = await request(app)
|
||||
.post(`/api/admin/showcase/${lastInsertRowid}/show`)
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.ok).toBe(true);
|
||||
|
||||
const row = db.prepare("SELECT is_hidden FROM showcase_works WHERE id = ?").get(lastInsertRowid) as {
|
||||
is_hidden: number;
|
||||
};
|
||||
expect(row.is_hidden).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
// ===========================================================================
|
||||
// 公告管理(管理员 CRUD)
|
||||
// ===========================================================================
|
||||
|
||||
describe("POST /api/admin/announcements", () => {
|
||||
it("创建公告成功返回 id", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const res = await request(app)
|
||||
.post("/api/admin/announcements")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({ title: "测试公告", body: "测试内容" });
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body).toHaveProperty("id");
|
||||
expect(typeof res.body.id).toBe("number");
|
||||
});
|
||||
|
||||
it("空标题返回 400", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const res = await request(app)
|
||||
.post("/api/admin/announcements")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({});
|
||||
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.body.error).toBe("标题不能为空");
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/admin/announcements", () => {
|
||||
it("初始为空列表", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const res = await request(app)
|
||||
.get("/api/admin/announcements")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(Array.isArray(res.body)).toBe(true);
|
||||
expect(res.body.length).toBe(0);
|
||||
});
|
||||
|
||||
it("创建后返回 1 条", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
await request(app)
|
||||
.post("/api/admin/announcements")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({ title: "测试公告", body: "测试内容" });
|
||||
|
||||
const res = await request(app)
|
||||
.get("/api/admin/announcements")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.length).toBe(1);
|
||||
expect(res.body[0].title).toBe("测试公告");
|
||||
expect(res.body[0].body).toBe("测试内容");
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/admin/announcements/:id/publish", () => {
|
||||
it("发布公告成功", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const createRes = await request(app)
|
||||
.post("/api/admin/announcements")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({ title: "可发布公告", body: "内容" });
|
||||
|
||||
const { id } = createRes.body;
|
||||
|
||||
const res = await request(app)
|
||||
.post(`/api/admin/announcements/${id}/publish`)
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.ok).toBe(true);
|
||||
|
||||
const row = db.prepare("SELECT is_published FROM announcements WHERE id = ?").get(id) as {
|
||||
is_published: number;
|
||||
};
|
||||
expect(row.is_published).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/admin/announcements/:id/unpublish", () => {
|
||||
it("下架公告成功", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const createRes = await request(app)
|
||||
.post("/api/admin/announcements")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({ title: "可下架公告", body: "内容" });
|
||||
|
||||
const { id } = createRes.body;
|
||||
|
||||
// 先发布
|
||||
await request(app)
|
||||
.post(`/api/admin/announcements/${id}/publish`)
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
// 再下架
|
||||
const res = await request(app)
|
||||
.post(`/api/admin/announcements/${id}/unpublish`)
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.ok).toBe(true);
|
||||
|
||||
const row = db.prepare("SELECT is_published FROM announcements WHERE id = ?").get(id) as {
|
||||
is_published: number;
|
||||
};
|
||||
expect(row.is_published).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/admin/announcements/:id", () => {
|
||||
it("删除公告成功", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const createRes = await request(app)
|
||||
.post("/api/admin/announcements")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({ title: "可删除公告", body: "内容" });
|
||||
|
||||
const { id } = createRes.body;
|
||||
|
||||
const res = await request(app)
|
||||
.delete(`/api/admin/announcements/${id}`)
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.ok).toBe(true);
|
||||
|
||||
// 验证已删除
|
||||
const row = db.prepare("SELECT id FROM announcements WHERE id = ?").get(id);
|
||||
expect(row).toBeUndefined();
|
||||
});
|
||||
|
||||
it("删除不存在的公告返回 404", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
const res = await request(app)
|
||||
.delete("/api/admin/announcements/99999")
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.body.error).toBe("公告不存在");
|
||||
});
|
||||
});
|
||||
|
||||
// ===========================================================================
|
||||
// 公开公告(无需登录)
|
||||
// ===========================================================================
|
||||
|
||||
describe("GET /api/announcements (public)", () => {
|
||||
it("返回空列表(无已发布公告)", async () => {
|
||||
const res = await request(app).get("/api/announcements");
|
||||
expect(res.status).toBe(200);
|
||||
expect(Array.isArray(res.body)).toBe(true);
|
||||
expect(res.body.length).toBe(0);
|
||||
});
|
||||
|
||||
it("仅返回已发布公告,按 publishedAt 倒序", async () => {
|
||||
// 创建两条公告,一条发布一条不发布
|
||||
const token = await getAdminToken();
|
||||
|
||||
const create1 = await request(app)
|
||||
.post("/api/admin/announcements")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({ title: "已发布公告", body: "可见" });
|
||||
|
||||
await request(app)
|
||||
.post("/api/admin/announcements")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({ title: "未发布公告", body: "不可见" });
|
||||
|
||||
// 发布第一条
|
||||
await request(app)
|
||||
.post(`/api/admin/announcements/${create1.body.id}/publish`)
|
||||
.set("Authorization", `Bearer ${token}`);
|
||||
|
||||
// 公开接口应只返回已发布的那条
|
||||
const res = await request(app).get("/api/announcements");
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.length).toBe(1);
|
||||
expect(res.body[0].title).toBe("已发布公告");
|
||||
expect(res.body[0].body).toBe("可见");
|
||||
expect(res.body[0]).toHaveProperty("publishedAt");
|
||||
expect(res.body[0]).not.toHaveProperty("is_published");
|
||||
});
|
||||
|
||||
it("未发布公告不在公开接口中", async () => {
|
||||
const token = await getAdminToken();
|
||||
|
||||
await request(app)
|
||||
.post("/api/admin/announcements")
|
||||
.set("Authorization", `Bearer ${token}`)
|
||||
.send({ title: "仅管理员可见", body: "草稿" });
|
||||
|
||||
// 不发布,公开接口应看不到
|
||||
const res = await request(app).get("/api/announcements");
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.length).toBe(0);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user