test: 补齐 Admin 会话边界回归
This commit is contained in:
1 parent
8e29932bc6
commit
5d88cb2040
5 files changed
+897
-718
No files matched your search
@@ -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();
|
||||
});
|
||||
Reference in new issue
Block a user