HXOPS 打开带 token 的地址时,前端只把令牌交给后端,后端向 Casdoor userinfo 校验。D20 接受该实例上任意仍然有效的访问令牌。 Co-authored-by: Cursor <cursoragent@cursor.com>
170 lines
6.1 KiB
TypeScript
170 lines
6.1 KiB
TypeScript
import { expect, test } from "@playwright/test";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const evidenceDir = path.resolve(
|
|
path.dirname(fileURLToPath(import.meta.url)),
|
|
"../../../../docs/evidence/M02/ui",
|
|
);
|
|
|
|
function envelope(data: unknown) {
|
|
return JSON.stringify({ success: true, data, request_id: "pw-login" });
|
|
}
|
|
|
|
test.describe("login shell", () => {
|
|
test("sign in as an admin, see the shell, then sign out", async ({ page }) => {
|
|
await page.route("**/auth/me", async (route) => {
|
|
await route.fulfill({
|
|
status: 401,
|
|
contentType: "application/json",
|
|
body: JSON.stringify({
|
|
success: false,
|
|
error: { code: 41000, message: "Unauthorized", details: {}, retryable: false },
|
|
request_id: "pw-anon",
|
|
}),
|
|
});
|
|
});
|
|
await page.route("**/auth/login", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
contentType: "application/json",
|
|
body: envelope({
|
|
subject: "casdoor-subject-1",
|
|
display_name: "高昊宇",
|
|
roles: ["watchman-admin"],
|
|
}),
|
|
});
|
|
});
|
|
await page.route("**/health", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
contentType: "application/json",
|
|
body: envelope({ status: "ok", app: "watchman", version: "0.1.0" }),
|
|
});
|
|
});
|
|
await page.route("**/auth/logout", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
contentType: "application/json",
|
|
body: envelope({ logged_out: true }),
|
|
});
|
|
});
|
|
|
|
await page.goto("/");
|
|
await expect(page.getByTestId("login-submit")).toBeVisible();
|
|
await page.getByTestId("login-username").locator("input").fill("26110002");
|
|
await page.getByTestId("login-password").locator("input").fill("simulated-password");
|
|
await page.getByTestId("login-submit").click();
|
|
|
|
await expect(page.getByTestId("shell-welcome")).toContainText("已登录:高昊宇");
|
|
await expect(page.getByTestId("health-success")).toBeVisible();
|
|
await expect(page.getByTestId("health-tag")).toContainText("健康检查成功");
|
|
await page.screenshot({ path: path.join(evidenceDir, "login-success.png"), fullPage: true });
|
|
|
|
await page.getByTestId("logout").click();
|
|
await expect(page.getByTestId("login-submit")).toBeVisible();
|
|
await expect(page.getByTestId("shell-welcome")).toHaveCount(0);
|
|
});
|
|
|
|
test("return from HXOPS with a one-time assertion signs in", async ({ page }) => {
|
|
await page.route("**/auth/me", async (route) => {
|
|
await route.fulfill({
|
|
status: 401,
|
|
contentType: "application/json",
|
|
body: JSON.stringify({
|
|
success: false,
|
|
error: { code: 41000, message: "Unauthorized", details: {}, retryable: false },
|
|
request_id: "pw-anon",
|
|
}),
|
|
});
|
|
});
|
|
let postedAssertion = "";
|
|
await page.route("**/auth/token-login", async (route) => {
|
|
postedAssertion = route.request().postData() ?? "";
|
|
await route.fulfill({
|
|
status: 200,
|
|
contentType: "application/json",
|
|
body: envelope({
|
|
subject: "casdoor-subject-1",
|
|
display_name: "高昊宇",
|
|
roles: ["watchman-admin"],
|
|
access_token: "session-token",
|
|
token_type: "Bearer",
|
|
expires_in: 28800,
|
|
}),
|
|
});
|
|
});
|
|
await page.route("**/health", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
contentType: "application/json",
|
|
body: envelope({ status: "ok", app: "watchman", version: "0.1.0" }),
|
|
});
|
|
});
|
|
|
|
await page.goto("/?token=one-time-token");
|
|
await expect(page.getByTestId("hxops-entry")).toHaveCount(0);
|
|
await expect(page.getByTestId("shell-welcome")).toContainText("已登录:高昊宇");
|
|
await expect(page.getByTestId("health-success")).toBeVisible();
|
|
expect(postedAssertion).toContain("one-time-token");
|
|
await expect(page).not.toHaveURL(/token=/);
|
|
await page.screenshot({ path: path.join(evidenceDir, "hxops-return.png"), fullPage: true });
|
|
});
|
|
|
|
test("failed sign-in stays on the form", async ({ page }) => {
|
|
await page.route("**/auth/me", async (route) => {
|
|
await route.fulfill({
|
|
status: 401,
|
|
contentType: "application/json",
|
|
body: JSON.stringify({
|
|
success: false,
|
|
error: { code: 41000, message: "Unauthorized", details: {}, retryable: false },
|
|
request_id: "pw-anon",
|
|
}),
|
|
});
|
|
});
|
|
await page.route("**/auth/login", async (route) => {
|
|
await route.fulfill({
|
|
status: 401,
|
|
contentType: "application/json",
|
|
body: JSON.stringify({
|
|
success: false,
|
|
error: { code: 41000, message: "Invalid username or password", details: {}, retryable: false },
|
|
request_id: "pw-bad",
|
|
}),
|
|
});
|
|
});
|
|
|
|
await page.goto("/");
|
|
await expect(page.getByTestId("login-submit")).toBeVisible();
|
|
await page.getByTestId("login-username").locator("input").fill("26110002");
|
|
await page.getByTestId("login-password").locator("input").fill("wrong-password");
|
|
await page.getByTestId("login-submit").click();
|
|
await expect(page.getByTestId("login-error")).toContainText("登录失败");
|
|
await page.screenshot({ path: path.join(evidenceDir, "login-failed.png"), fullPage: true });
|
|
});
|
|
|
|
test("a session without a Watchman role can sign out", async ({ page }) => {
|
|
await page.route("**/auth/me", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
contentType: "application/json",
|
|
body: envelope({ subject: "user-1", display_name: "No Role", roles: [] }),
|
|
});
|
|
});
|
|
await page.route("**/auth/logout", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
contentType: "application/json",
|
|
body: envelope({ logged_out: true }),
|
|
});
|
|
});
|
|
|
|
await page.goto("/");
|
|
await expect(page.getByTestId("forbidden")).toBeVisible();
|
|
await page.screenshot({ path: path.join(evidenceDir, "login-forbidden.png"), fullPage: true });
|
|
await page.getByTestId("logout").click();
|
|
await expect(page.getByTestId("login-submit")).toBeVisible();
|
|
});
|
|
});
|