test: 扩展客户接口集成测试

This commit is contained in:
que01 committed 2026-09-19 00:58:56 +08:00
1 parent 3c59b6406b
commit 21801212d2
2 files changed
+37 -2

No files matched your search

+35
View File
@@ -1,16 +1,26 @@
import assert from "node:assert/strict";
import { after, before, test } from "node:test";
import { PrismaClient } from "@prisma/client";
import { createApp } from "../dist/main.js";
import { signIdentity } from "../dist/auth.js";
let app;
const prisma = new PrismaClient();
const brandId = process.env.DEFAULT_BRAND_ID;
const openid = `integration-${Date.now()}`;
let customerToken;
before(async () => {
await prisma.customerUser.create({ data: { brandId, openid } });
customerToken = signIdentity({ subject: openid, brandId, role: "customer" });
app = await createApp();
await app.init();
});
after(async () => {
await app.close();
await prisma.customerUser.deleteMany({ where: { openid } });
await prisma.$disconnect();
});
async function request(url, options = {}) {
@@ -70,3 +80,28 @@ test("admin login validates credentials", async () => {
});
assert.equal(response.statusCode, 401);
});
test("customer booking endpoints require a valid customer session", async () => {
const missing = await request("/customer/bookings");
assert.equal(missing.statusCode, 400);
const list = await request("/customer/bookings", {
headers: { authorization: `Bearer ${customerToken}` },
});
assert.equal(list.statusCode, 200);
assert.deepEqual(JSON.parse(list.body), []);
});
test("customer booking DTO rejects incomplete package requests", async () => {
const response = await request("/customer/bookings", {
method: "POST",
headers: { authorization: `Bearer ${customerToken}` },
payload: {
requestType: "package",
customerName: "集成测试客户",
contactPreference: "wechat",
},
});
assert.equal(response.statusCode, 400);
assert.equal(JSON.parse(response.body).error.code, "HTTP_400");
});