test: 增加 API HTTP 集成测试
This commit is contained in:
1 parent
50f405932b
commit
3c59b6406b
4 files changed
+86
-3
No files matched your search
@@ -0,0 +1,72 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { after, before, test } from "node:test";
|
||||
import { createApp } from "../dist/main.js";
|
||||
|
||||
let app;
|
||||
|
||||
before(async () => {
|
||||
app = await createApp();
|
||||
await app.init();
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
Reference in new issue
Block a user