refactor: 收口 Admin 会话与授权边界

This commit is contained in:
que01 committed 2026-10-19 10:13:13 +08:00
1 parent 0788480ce3
commit 4ef4be2071
67 files changed
+1890 -392

No files matched your search

+38 -4
View File
@@ -235,6 +235,7 @@ runtimeWorkspace.trustItems.push(...additionalFoodTrust);
test("Admin 按通用预览目标筛选字段并同步品牌草稿", async ({ page }) => {
test.setTimeout(60000);
let rejectOfferingSave = false;
let sessionAuthenticated = false;
await page.setViewportSize({ width: 1920, height: 1000 });
await page.route("http://localhost:3001/public/workspace**", (route) =>
route.fulfill({ json: runtimeWorkspace }),
@@ -243,15 +244,31 @@ test("Admin 按通用预览目标筛选字段并同步品牌草稿", async ({ pa
const request = route.request();
const url = new URL(request.url());
url.pathname = url.pathname.replace(/^\/api/, "");
if (url.pathname === "/admin/session/login")
if (url.pathname === "/admin/session/current") {
if (!sessionAuthenticated) {
return route.fulfill({ status: 401, json: { error: { message: "未登录" } } });
}
return route.fulfill({
json: {
token: "fixture-token",
authenticated: true,
displayName: "测试管理员",
role: "owner",
mustChangePassword: false,
platformTemplateRole: "none",
},
});
}
if (url.pathname === "/admin/session/login") {
sessionAuthenticated = true;
return route.fulfill({
json: {
displayName: "测试管理员",
role: "owner",
mustChangePassword: false,
platformTemplateRole: "none",
},
});
}
if (url.pathname === "/admin/brands")
return route.fulfill({ json: [{ id: brand.id, role: "owner", brand }] });
if (url.pathname === "/admin/config/brand" && request.method() === "PATCH") {
@@ -636,6 +653,7 @@ test("Admin 按通用预览目标筛选字段并同步品牌草稿", async ({ pa
test("林境模板支持首页和主理人区域点选", async ({ page }) => {
test.setTimeout(60000);
let sessionAuthenticated = false;
await page.setViewportSize({ width: 1600, height: 900 });
const editorialBrand = {
...structuredClone(brand),
@@ -730,10 +748,26 @@ test("林境模板支持首页和主理人区域点选", async ({ page }) => {
await page.route("**/api/**", (route) => {
const url = new URL(route.request().url());
const path = url.pathname.replace(/^\/api/, "");
if (path === "/admin/session/login")
if (path === "/admin/session/current") {
if (!sessionAuthenticated) {
return route.fulfill({ status: 401, json: { error: { message: "未登录" } } });
}
return route.fulfill({
json: { token: "fixture-token", displayName: "测试管理员", role: "owner" },
json: {
authenticated: true,
displayName: "测试管理员",
role: "owner",
mustChangePassword: false,
platformTemplateRole: "none",
},
});
}
if (path === "/admin/session/login") {
sessionAuthenticated = true;
return route.fulfill({
json: { displayName: "测试管理员", role: "owner", mustChangePassword: false },
});
}
if (path === "/admin/brands")
return route.fulfill({
json: [{ id: editorialBrand.id, role: "owner", brand: editorialBrand }],
@@ -94,10 +94,11 @@ test("业务编辑弹窗预览列表与详情详情同一份内容", async ({ pa
await page.route("**/api/**", async (route) => {
const url = new URL(route.request().url());
const path = url.pathname.replace(/^\/api/, "");
if (path === "/admin/session/current")
return route.fulfill({ status: 401, json: { error: { message: "未登录" } } });
if (path === "/admin/session/login")
return route.fulfill({
json: {
token: "fixture-token",
displayName: "测试管理员",
role: "owner",
mustChangePassword: false,
@@ -57,9 +57,19 @@ test("模板管理可重试预览,并切换我的、详情和禁止提交的
),
);
await page.setViewportSize({ width: 1440, height: 1000 });
await page.addInitScript(() => {
localStorage.setItem("adminToken", "fixture-token");
localStorage.setItem("adminPlatformTemplateRole", "template_admin");
await page.route("**/api/**", (route) => {
const url = new URL(route.request().url());
if (url.pathname.endsWith("/admin/session/current"))
return route.fulfill({
json: {
authenticated: true,
displayName: "模板管理员",
role: "owner",
mustChangePassword: false,
platformTemplateRole: "template_admin",
},
});
return route.fulfill({ json: {} });
});
let attempts = 0;
const writes: string[] = [];
@@ -71,6 +81,16 @@ test("模板管理可重试预览,并切换我的、详情和禁止提交的
};
await page.route("**/api/**", async (route) => {
const url = new URL(route.request().url());
if (url.pathname.endsWith("/admin/session/current"))
return route.fulfill({
json: {
authenticated: true,
displayName: "模板管理员",
role: "owner",
mustChangePassword: false,
platformTemplateRole: "template_admin",
},
});
if (route.request().method() !== "GET") writes.push(url.pathname);
if (url.pathname.endsWith("/preview")) {
attempts++;
@@ -161,9 +161,19 @@ test("导入包预览矩阵展示身份、资源和完整媒体槽位", async ({
await test.step(`${mediaCase.packageKey}/${mediaCase.industry}`, async () => {
const { preview, version } = await buildPreviewFixture(mediaCase);
assert.ok(version);
await page.addInitScript(() => {
localStorage.setItem("adminToken", "fixture-token");
localStorage.setItem("adminPlatformTemplateRole", "template_admin");
await page.route("**/api/**", (route) => {
const url = new URL(route.request().url());
if (url.pathname.endsWith("/admin/session/current"))
return route.fulfill({
json: {
authenticated: true,
displayName: "模板管理员",
role: "owner",
mustChangePassword: false,
platformTemplateRole: "template_admin",
},
});
return route.fulfill({ json: {} });
});
const emptyPreview = structuredClone(preview);
emptyPreview.workspace.offerings = [];
@@ -175,6 +185,16 @@ test("导入包预览矩阵展示身份、资源和完整媒体槽位", async ({
const writes: string[] = [];
await page.route("**/api/**", async (route) => {
const url = new URL(route.request().url());
if (url.pathname.endsWith("/admin/session/current"))
return route.fulfill({
json: {
authenticated: true,
displayName: "模板管理员",
role: "owner",
mustChangePassword: false,
platformTemplateRole: "template_admin",
},
});
if (route.request().method() !== "GET") writes.push(url.pathname);
if (url.pathname.endsWith("/preview")) {
attempts += 1;