test: 补齐 Admin 会话边界回归

This commit is contained in:
que01 committed 2026-10-19 11:35:41 +08:00
1 parent 8e29932bc6
commit 5d88cb2040
5 files changed
+897 -718

No files matched your search

@@ -13,7 +13,7 @@
"artifacts": [
{
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log",
"sha256": "160cd02dab269fa225ff7625f2779661752f445efa97064337d5603be8d05431"
"sha256": "7cd8897ba289da75cf88cb54aa5eb42b15dfcc080ef7ba0a79499864ebcfbabb"
},
{
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/admin-editor.log",
@@ -30,7 +30,7 @@
"artifacts": [
{
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log",
"sha256": "160cd02dab269fa225ff7625f2779661752f445efa97064337d5603be8d05431"
"sha256": "7cd8897ba289da75cf88cb54aa5eb42b15dfcc080ef7ba0a79499864ebcfbabb"
}
],
"exitCode": 0
@@ -105,7 +105,7 @@
"artifacts": [
{
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log",
"sha256": "160cd02dab269fa225ff7625f2779661752f445efa97064337d5603be8d05431"
"sha256": "7cd8897ba289da75cf88cb54aa5eb42b15dfcc080ef7ba0a79499864ebcfbabb"
},
{
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/admin-editor.log",
@@ -122,7 +122,7 @@
"artifacts": [
{
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log",
"sha256": "160cd02dab269fa225ff7625f2779661752f445efa97064337d5603be8d05431"
"sha256": "7cd8897ba289da75cf88cb54aa5eb42b15dfcc080ef7ba0a79499864ebcfbabb"
}
],
"exitCode": 0
@@ -197,7 +197,7 @@
"artifacts": [
{
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log",
"sha256": "160cd02dab269fa225ff7625f2779661752f445efa97064337d5603be8d05431"
"sha256": "7cd8897ba289da75cf88cb54aa5eb42b15dfcc080ef7ba0a79499864ebcfbabb"
},
{
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/admin-editor.log",
@@ -214,7 +214,7 @@
"artifacts": [
{
"path": ".codex-tasks/template-audit-execution/raw/evidence-2026-10-19/api-integration.log",
"sha256": "160cd02dab269fa225ff7625f2779661752f445efa97064337d5603be8d05431"
"sha256": "7cd8897ba289da75cf88cb54aa5eb42b15dfcc080ef7ba0a79499864ebcfbabb"
}
],
"exitCode": 0
@@ -292,5 +292,5 @@
}
}
},
"sourceTreeHash": "2479cbced319d41ec0ab2a23d913c5104b162eb8f904cd5807b61628dc55e71e"
"sourceTreeHash": "44b16c0f22a737b6d53db2923b45957289aa50f9355b02a44e6cc72323d7b3c8"
}
File diff suppressed because it is too large. Load diff
+73
View File
@@ -2199,6 +2199,16 @@ test("editor membership can manage brand content but cannot change system settin
headers: { authorization: `Bearer ${token}` },
});
assert.equal(capabilities.statusCode, 403);
const miniAppSettings = await request("/admin/miniapp-releases/settings", {
headers: { authorization: `Bearer ${token}` },
});
assert.equal(miniAppSettings.statusCode, 200);
const miniAppUpdate = await request("/admin/miniapp-releases/settings", {
method: "PATCH",
headers: { authorization: `Bearer ${token}` },
payload: { appId: "wx-editor-denied" },
});
assert.equal(miniAppUpdate.statusCode, 403);
const visualUpdate = await request("/admin/config/brand", {
method: "PATCH",
headers: { authorization: `Bearer ${token}` },
@@ -3961,6 +3971,68 @@ test("customer logout revokes the current token", async () => {
assert.equal(after.statusCode, 401);
});
test("admin session cookie drives current session and rejects invalid credentials", async () => {
const missing = await request("/admin/session/current");
assert.equal(missing.statusCode, 401);
const invalid = await request("/admin/session/current", {
headers: { authorization: "Bearer invalid-token" },
});
assert.equal(invalid.statusCode, 401);
const customer = await request("/admin/session/current", {
headers: { authorization: `Bearer ${customerToken}` },
});
assert.equal(customer.statusCode, 403);
const username = `cookie-session-${randomUUID()}`;
const password = "cookie-password-123";
const user = await prisma.adminUser.create({
data: {
username,
passwordHash: await bcrypt.hash(password, 12),
displayName: "Cookie 会话管理员",
mustChangePassword: false,
memberships: { create: { brandId, role: "owner", isEnabled: true } },
},
});
try {
const login = await request("/admin/session/login", {
method: "POST",
payload: { username, password },
});
assert.equal(login.statusCode, 201);
assert.equal(JSON.parse(login.body).token, undefined);
const cookie = login.cookies?.find((item) => item.name === "admin_session");
assert.ok(cookie, "admin session cookie missing");
assert.equal(cookie.httpOnly, true);
assert.equal(cookie.sameSite, "Strict");
assert.equal(cookie.path, "/");
const current = await request("/admin/session/current", {
headers: { cookie: `admin_session=${cookie.value}` },
});
assert.equal(current.statusCode, 200);
assert.equal(JSON.parse(current.body).authenticated, true);
assert.equal(JSON.parse(current.body).mustChangePassword, false);
const logout = await request("/admin/session/logout", {
method: "POST",
headers: { cookie: `admin_session=${cookie.value}` },
});
assert.equal(logout.statusCode, 201);
const clearedCookie = logout.cookies?.find((item) => item.name === "admin_session");
assert.ok(clearedCookie, "clear-cookie header missing");
const after = await request("/admin/session/current", {
headers: { cookie: `admin_session=${cookie.value}` },
});
assert.equal(after.statusCode, 401);
} finally {
await prisma.brandMembership.deleteMany({ where: { adminUserId: user.id } });
await prisma.adminUser.delete({ where: { id: user.id } });
}
});
test("admin login does not auto-grant ownership to users without memberships", async () => {
const username = `no-membership-${Date.now()}`;
const user = await prisma.adminUser.create({
@@ -4064,6 +4136,7 @@ test("brand switching isolates admin configuration, offerings, and public worksp
headers: { authorization: `Bearer ${defaultToken}` },
});
assert.equal(switched.statusCode, 201);
assert.equal(JSON.parse(switched.body).token, undefined);
const doorsToken = sessionToken(switched);
const [
@@ -0,0 +1,44 @@
import { test, expect } from "@playwright/test";
const adminOrigin = process.env.ADMIN_URL || "http://localhost:3005";
const brand = {
id: "session-brand-fixture",
name: "会话恢复品牌",
slug: "session-brand",
templateKey: "food",
};
test("首次登录修改密码后直接恢复后台", async ({ page }) => {
let passwordChanged = false;
await page.setViewportSize({ width: 1440, height: 900 });
await page.route("**/api/**", async (route) => {
const path = new URL(route.request().url()).pathname.replace(/^\/api/, "");
if (path === "/admin/session/current") {
return route.fulfill({
json: {
authenticated: true,
displayName: "首次登录管理员",
role: "editor",
mustChangePassword: !passwordChanged,
platformTemplateRole: "none",
},
});
}
if (path === "/admin/session/change-password" && route.request().method() === "POST") {
passwordChanged = true;
return route.fulfill({ json: { changed: true } });
}
if (path === "/admin/config/brand") return route.fulfill({ json: brand });
if (path === "/admin/brands") return route.fulfill({ json: [] });
return route.fulfill({ json: {} });
});
await page.goto(`${adminOrigin}/admin/brand`);
await expect(page.getByText("首次登录需要修改密码")).toBeVisible();
await page.getByLabel("初始密码").fill("initial-password-123");
await page.getByLabel("新密码", { exact: true }).fill("updated-password-123");
await page.getByLabel("确认密码").fill("updated-password-123");
await page.getByRole("button", { name: "保存新密码" }).click();
await expect(page.locator(".admin-shell")).toBeVisible();
await expect(page.getByText("首次登录需要修改密码")).toBeHidden();
});
+56
View File
@@ -45,6 +45,62 @@ test("客户端登出会调用当前会话接口", async () => {
]);
});
test("Admin 会话客户端读取当前会话且登录不返回 token", async () => {
const calls: Array<{ url: string; method?: string; body?: string }> = [];
const client = new ApiClient({
baseUrl: "http://api.test",
fetcher: async (url, init) => {
calls.push({ url, method: init?.method, body: init?.body });
return url.endsWith("/session/current")
? response({
authenticated: true,
displayName: "测试管理员",
mustChangePassword: false,
role: "owner",
platformTemplateRole: "none",
})
: response({
displayName: "测试管理员",
mustChangePassword: false,
role: "owner",
platformTemplateRole: "none",
});
},
});
const current = await client.getAdminSession();
const login = await client.loginAdmin("admin", "password-123");
assert.equal(current.authenticated, true);
assert.equal("token" in login, false);
assert.deepEqual(calls, [
{ url: "http://api.test/admin/session/current", method: "GET", body: undefined },
{
url: "http://api.test/admin/session/login",
method: "POST",
body: JSON.stringify({ username: "admin", password: "password-123" }),
},
]);
});
test("品牌切换客户端不再接收 token", async () => {
const calls: Array<{ url: string; method?: string; body?: string }> = [];
const client = new ApiClient({
baseUrl: "http://api.test",
fetcher: async (url, init) => {
calls.push({ url, method: init?.method, body: init?.body });
return response({ role: "owner", brand: { id: "brand-1", name: "品牌一" } });
},
});
const switched = await client.switchAdminBrand("brand-1");
assert.equal("token" in switched, false);
assert.deepEqual(calls, [
{
url: "http://api.test/admin/brands/brand-1/switch",
method: "POST",
body: undefined,
},
]);
});
test("客户端将统一错误响应转换为 ApiClientError", async () => {
const client = new ApiClient({
baseUrl: "http://api.test",