108 lines
3.6 KiB
JavaScript
108 lines
3.6 KiB
JavaScript
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 = {}) {
|
|
return app.getHttpAdapter().getInstance().inject({ method: "GET", url, ...options });
|
|
}
|
|
|
|
test("health and public content are available without authentication", async () => {
|
|
const health = await request("/health");
|
|
assert.equal(health.statusCode, 200);
|
|
assert.deepEqual(JSON.parse(health.body), { status: "ok", service: "personal-brand-api" });
|
|
|
|
const brand = await request("/public/brand");
|
|
assert.equal(brand.statusCode, 200);
|
|
assert.equal(JSON.parse(brand.body).name, "阿成家常菜");
|
|
|
|
const services = await request("/public/services");
|
|
assert.equal(services.statusCode, 200);
|
|
assert.equal(JSON.parse(services.body).length, 3);
|
|
});
|
|
|
|
test("admin booking endpoints reject missing or customer credentials", async () => {
|
|
const missing = await request("/admin/bookings");
|
|
assert.equal(missing.statusCode, 400);
|
|
|
|
const customer = await request("/admin/bookings", {
|
|
headers: { authorization: "Bearer invalid-token" },
|
|
});
|
|
assert.equal(customer.statusCode, 401);
|
|
});
|
|
|
|
test("seeded admin can log in and read protected booking resources", async () => {
|
|
const login = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
|
});
|
|
assert.equal(login.statusCode, 201);
|
|
const token = JSON.parse(login.body).token;
|
|
assert.ok(token);
|
|
|
|
const count = await request("/admin/bookings/unread-count", {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(count.statusCode, 200);
|
|
assert.deepEqual(JSON.parse(count.body), { count: 0 });
|
|
|
|
const tags = await request("/admin/bookings/tags", {
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
assert.equal(tags.statusCode, 200);
|
|
assert.equal(JSON.parse(tags.body).length, 6);
|
|
});
|
|
|
|
test("admin login validates credentials", async () => {
|
|
const response = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: "wrong-password" },
|
|
});
|
|
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");
|
|
});
|