277 lines
11 KiB
JavaScript
277 lines
11 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;
|
|
const createdBookingIds = [];
|
|
|
|
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.booking.deleteMany({ where: { id: { in: createdBookingIds } } });
|
|
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.ok(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.equal(typeof JSON.parse(count.body).count, "number");
|
|
|
|
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("admin can read and update brand and service configuration", async () => {
|
|
const login = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
|
});
|
|
const adminToken = JSON.parse(login.body).token;
|
|
assert.ok(adminToken);
|
|
|
|
const unauthorized = await request("/admin/config/brand");
|
|
assert.equal(unauthorized.statusCode, 400);
|
|
|
|
const brandBefore = await prisma.brandProfile.findUnique({ where: { id: brandId } });
|
|
assert.ok(brandBefore);
|
|
const serviceBefore = await prisma.service.findFirst({ where: { brandId, isEnabled: true } });
|
|
assert.ok(serviceBefore);
|
|
let createdServiceId;
|
|
|
|
try {
|
|
const brand = await request("/admin/config/brand", { headers: { authorization: `Bearer ${adminToken}` } });
|
|
assert.equal(brand.statusCode, 200);
|
|
assert.equal(JSON.parse(brand.body).id, brandId);
|
|
|
|
const updatedBrand = await request("/admin/config/brand", {
|
|
method: "PATCH",
|
|
headers: { authorization: `Bearer ${adminToken}` },
|
|
payload: { description: "集成测试品牌简介", contactPhone: "13800000000", templateKey: "personal" },
|
|
});
|
|
assert.equal(updatedBrand.statusCode, 200);
|
|
assert.equal(JSON.parse(updatedBrand.body).description, "集成测试品牌简介");
|
|
assert.equal(JSON.parse(updatedBrand.body).templateKey, "personal");
|
|
|
|
const updatedService = await request(`/admin/config/services/${serviceBefore.id}`, {
|
|
method: "PATCH",
|
|
headers: { authorization: `Bearer ${adminToken}` },
|
|
payload: { priceText: "集成测试价格", sortOrder: 99 },
|
|
});
|
|
assert.equal(updatedService.statusCode, 200);
|
|
assert.equal(JSON.parse(updatedService.body).priceText, "集成测试价格");
|
|
assert.equal(JSON.parse(updatedService.body).sortOrder, 99);
|
|
|
|
const listedServices = await request("/admin/config/services", { headers: { authorization: `Bearer ${adminToken}` } });
|
|
assert.equal(listedServices.statusCode, 200);
|
|
assert.ok(JSON.parse(listedServices.body).some((item) => item.id === serviceBefore.id));
|
|
|
|
const createdService = await request("/admin/config/services", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${adminToken}` },
|
|
payload: { name: "集成测试通用服务", description: "通用品牌服务", categoryKey: "consulting", config: { duration: 60 } },
|
|
});
|
|
assert.equal(createdService.statusCode, 201);
|
|
createdServiceId = JSON.parse(createdService.body).id;
|
|
assert.equal(JSON.parse(createdService.body).categoryKey, "consulting");
|
|
} finally {
|
|
await prisma.brandProfile.update({ where: { id: brandId }, data: { description: brandBefore.description, contactPhone: brandBefore.contactPhone, templateKey: brandBefore.templateKey } });
|
|
await prisma.service.update({ where: { id: serviceBefore.id }, data: { priceText: serviceBefore.priceText, sortOrder: serviceBefore.sortOrder } });
|
|
if (createdServiceId) await prisma.service.delete({ where: { id: createdServiceId } });
|
|
}
|
|
});
|
|
|
|
test("admin can manage trust content", async () => {
|
|
const login = await request("/admin/session/login", { method: "POST", payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD } });
|
|
const token = JSON.parse(login.body).token;
|
|
assert.equal((await request("/admin/config/trust-content")).statusCode, 400);
|
|
const created = await request("/admin/config/trust-content", { method: "POST", headers: { authorization: `Bearer ${token}` }, payload: { type: "profile", title: "关于我", content: "集成测试内容", summary: "测试摘要" } });
|
|
assert.equal(created.statusCode, 201);
|
|
const id = JSON.parse(created.body).id;
|
|
try {
|
|
const list = await request("/admin/config/trust-content", { headers: { authorization: `Bearer ${token}` } });
|
|
assert.equal(list.statusCode, 200);
|
|
const updated = await request(`/admin/config/trust-content/${id}`, { method: "PATCH", headers: { authorization: `Bearer ${token}` }, payload: { content: "更新后的内容" } });
|
|
assert.equal(updated.statusCode, 200);
|
|
assert.equal(JSON.parse(updated.body).content, "更新后的内容");
|
|
} finally {
|
|
const deleted = await request(`/admin/config/trust-content/${id}`, { method: "DELETE", headers: { authorization: `Bearer ${token}` } });
|
|
assert.equal(deleted.statusCode, 200);
|
|
}
|
|
});
|
|
|
|
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");
|
|
});
|
|
|
|
test("customer can create a generic inquiry without food-specific fields", async () => {
|
|
const response = await request("/customer/bookings", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${customerToken}` },
|
|
payload: {
|
|
requestType: "inquiry",
|
|
customerName: "个人品牌咨询客户",
|
|
wechatId: "inquiry-wechat",
|
|
contactPreference: "wechat",
|
|
requestedTime: "下周三 14:00",
|
|
note: "希望讨论品牌合作方案",
|
|
},
|
|
});
|
|
assert.equal(response.statusCode, 201);
|
|
const body = JSON.parse(response.body);
|
|
const booking = await prisma.booking.findUnique({ where: { bookingNo: body.bookingNo } });
|
|
assert.ok(booking);
|
|
assert.equal(booking.requestType, "inquiry");
|
|
createdBookingIds.push(booking.id);
|
|
});
|
|
|
|
test("customer can create and cancel a package booking", async () => {
|
|
const service = await prisma.service.findFirst({ where: { brandId, isEnabled: true, priceAmount: { not: null } } });
|
|
assert.ok(service);
|
|
const created = await request("/customer/bookings", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${customerToken}` },
|
|
payload: {
|
|
requestType: "package",
|
|
customerName: "集成测试客户",
|
|
wechatId: "integration-wechat",
|
|
contactPreference: "wechat",
|
|
items: [{ serviceId: service.id, quantity: 2 }],
|
|
},
|
|
});
|
|
assert.equal(created.statusCode, 201);
|
|
const createdBody = JSON.parse(created.body);
|
|
assert.equal(createdBody.estimatedAmount, service.priceAmount * 2);
|
|
const booking = await prisma.booking.findUnique({ where: { bookingNo: createdBody.bookingNo } });
|
|
assert.ok(booking);
|
|
createdBookingIds.push(booking.id);
|
|
|
|
const cancelled = await request(`/customer/bookings/${booking.id}/cancel`, {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${customerToken}` },
|
|
});
|
|
assert.equal(cancelled.statusCode, 201);
|
|
assert.equal(JSON.parse(cancelled.body).status, "cancelled");
|
|
});
|
|
|
|
test("admin can advance booking status but cannot skip the state machine", async () => {
|
|
const service = await prisma.service.findFirst({ where: { brandId, isEnabled: true, priceAmount: { not: null } } });
|
|
assert.ok(service);
|
|
const created = await request("/customer/bookings", {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${customerToken}` },
|
|
payload: {
|
|
requestType: "package",
|
|
customerName: "状态测试客户",
|
|
phone: "13800000000",
|
|
contactPreference: "phone",
|
|
items: [{ serviceId: service.id, quantity: 1 }],
|
|
},
|
|
});
|
|
const booking = await prisma.booking.findUnique({ where: { bookingNo: JSON.parse(created.body).bookingNo } });
|
|
assert.ok(booking);
|
|
createdBookingIds.push(booking.id);
|
|
|
|
const login = await request("/admin/session/login", {
|
|
method: "POST",
|
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
|
});
|
|
const adminToken = JSON.parse(login.body).token;
|
|
const contacting = await request(`/admin/bookings/${booking.id}/status`, {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${adminToken}` },
|
|
payload: { status: "contacting", adminNote: "已联系客户" },
|
|
});
|
|
assert.equal(contacting.statusCode, 201);
|
|
assert.equal(JSON.parse(contacting.body).status, "contacting");
|
|
|
|
const skipped = await request(`/admin/bookings/${booking.id}/status`, {
|
|
method: "POST",
|
|
headers: { authorization: `Bearer ${adminToken}` },
|
|
payload: { status: "completed" },
|
|
});
|
|
assert.equal(skipped.statusCode, 400);
|
|
});
|