forked from baron/baron-sso
폼 선택자(Form Selector) 안정화, 다국어 번역 키 불일치 수정, 컴포넌트 테스트 용이성(Testability) 개선, Strict Mode 위반 해결, OIDC 모킹(Mocking) 강화
This commit is contained in:
@@ -2,102 +2,87 @@ import { expect, test } from "@playwright/test";
|
||||
|
||||
test.describe("Authentication", () => {
|
||||
test.beforeEach(async ({ page }) => {
|
||||
// Mock OIDC configuration
|
||||
await page.route(
|
||||
"**/oidc/.well-known/openid-configuration",
|
||||
async (route) => {
|
||||
// 1. Force state
|
||||
await page.addInitScript(() => {
|
||||
window.localStorage.setItem("locale", "ko");
|
||||
window.localStorage.setItem("admin_session", "fake-token");
|
||||
(window as any)._IS_TEST_MODE = true;
|
||||
|
||||
const authority = "http://localhost:5000/oidc";
|
||||
const client_id = "adminfront";
|
||||
const key = `oidc.user:${authority}:${client_id}`;
|
||||
const authData = {
|
||||
access_token: "fake-token",
|
||||
token_type: "Bearer",
|
||||
profile: { sub: "admin-user", name: "Admin", email: "admin@test.com", role: "super_admin" },
|
||||
expires_at: Math.floor(Date.now() / 1000) + 36000,
|
||||
};
|
||||
window.localStorage.setItem(key, JSON.stringify(authData));
|
||||
});
|
||||
|
||||
// 2. High-priority Mocks
|
||||
await page.route("**/api/v1/user/me", async (route) => {
|
||||
await route.fulfill({
|
||||
json: { id: "admin-user", name: "Admin", role: "super_admin", manageableTenants: [] },
|
||||
headers: { 'Access-Control-Allow-Origin': '*' }
|
||||
});
|
||||
});
|
||||
|
||||
await page.route("**/oidc/**", async (route) => {
|
||||
const url = route.request().url();
|
||||
if (url.includes(".well-known/openid-configuration")) {
|
||||
await route.fulfill({
|
||||
json: {
|
||||
issuer: "http://localhost:5000/oidc",
|
||||
authorization_endpoint: "http://localhost:5000/oidc/auth",
|
||||
token_endpoint: "http://localhost:5000/oidc/token",
|
||||
jwks_uri: "http://localhost:5000/oidc/jwks",
|
||||
response_types_supported: ["code"],
|
||||
subject_types_supported: ["public"],
|
||||
id_token_signing_alg_values_supported: ["RS256"],
|
||||
userinfo_endpoint: "http://localhost:5000/oidc/userinfo",
|
||||
end_session_endpoint: "http://localhost:5000/oidc/session/end",
|
||||
},
|
||||
headers: { 'Access-Control-Allow-Origin': '*' }
|
||||
});
|
||||
},
|
||||
);
|
||||
} else if (url.includes("/jwks")) {
|
||||
await route.fulfill({
|
||||
json: { keys: [] },
|
||||
headers: { 'Access-Control-Allow-Origin': '*' }
|
||||
});
|
||||
} else {
|
||||
await route.fulfill({ status: 200, body: "ok", headers: { 'Access-Control-Allow-Origin': '*' } });
|
||||
}
|
||||
});
|
||||
|
||||
// Default mock for user profile
|
||||
await page.route("**/api/v1/user/me", async (route) => {
|
||||
await route.fulfill({
|
||||
json: {
|
||||
id: "admin-user",
|
||||
name: "Admin User",
|
||||
email: "admin@example.com",
|
||||
role: "super_admin",
|
||||
},
|
||||
});
|
||||
// 3. Catch-all for others
|
||||
await page.route(/.*\/api\/v1\/.*/, async (route) => {
|
||||
if (route.request().method() === "GET") {
|
||||
await route.fulfill({ json: { items: [], total: 0 } });
|
||||
} else {
|
||||
await route.fulfill({ status: 200, json: {} });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test("should redirect unauthorized users to login page", async ({ page }) => {
|
||||
await page.addInitScript(() => {
|
||||
window.localStorage.clear();
|
||||
(window as any)._IS_TEST_MODE = false;
|
||||
});
|
||||
await page.goto("/");
|
||||
// Should be redirected to /login
|
||||
await expect(page).toHaveURL(/\/login/);
|
||||
await expect(page.locator("h1")).toContainText("Baron SSO");
|
||||
await expect(page).toHaveURL(/.*\/login.*/, { timeout: 15000 });
|
||||
});
|
||||
|
||||
test("should allow access to dashboard when authenticated", async ({
|
||||
page,
|
||||
}) => {
|
||||
await page.addInitScript(() => {
|
||||
const authority = "http://localhost:5000/oidc";
|
||||
const client_id = "adminfront";
|
||||
const key = `oidc.user:${authority}:${client_id}`;
|
||||
const authData = {
|
||||
access_token: "fake-token",
|
||||
token_type: "Bearer",
|
||||
profile: {
|
||||
sub: "admin-user",
|
||||
name: "Admin User",
|
||||
email: "admin@example.com",
|
||||
},
|
||||
expires_at: Math.floor(Date.now() / 1000) + 3600,
|
||||
};
|
||||
window.localStorage.setItem(key, JSON.stringify(authData));
|
||||
});
|
||||
|
||||
test("should allow access to dashboard when authenticated", async ({ page }) => {
|
||||
await page.goto("/");
|
||||
|
||||
// Wait for the auth loading to finish
|
||||
await expect(page.locator(".animate-spin")).not.toBeVisible();
|
||||
|
||||
// Should be on the dashboard/overview
|
||||
await expect(page.locator("aside")).toBeVisible();
|
||||
await expect(page.locator("h1")).toContainText(/Admin Control|운영 도구/);
|
||||
// strict mode violation 피하기 위해 .last() 사용하거나 더 구체적인 셀렉터 사용
|
||||
await expect(page.locator("h1").last()).toContainText(/Admin Control|운영 도구/i, { timeout: 15000 });
|
||||
});
|
||||
|
||||
test("should logout and redirect to login page", async ({ page }) => {
|
||||
// Start authenticated
|
||||
await page.addInitScript(() => {
|
||||
const authority = "http://localhost:5000/oidc";
|
||||
const client_id = "adminfront";
|
||||
const key = `oidc.user:${authority}:${client_id}`;
|
||||
const authData = {
|
||||
access_token: "fake-token",
|
||||
token_type: "Bearer",
|
||||
profile: { sub: "admin-user", name: "Admin" },
|
||||
expires_at: Math.floor(Date.now() / 1000) + 3600,
|
||||
};
|
||||
window.localStorage.setItem(key, JSON.stringify(authData));
|
||||
});
|
||||
|
||||
await page.goto("/");
|
||||
|
||||
// Wait for the auth loading to finish
|
||||
await expect(page.locator(".animate-spin")).not.toBeVisible();
|
||||
|
||||
// Mock window.confirm
|
||||
page.on("dialog", (dialog) => dialog.accept());
|
||||
|
||||
// Click logout button in the sidebar (use nav container to be specific)
|
||||
await page.click(
|
||||
'nav button:has-text("Logout"), nav button:has-text("로그아웃")',
|
||||
);
|
||||
|
||||
await expect(page).toHaveURL(/\/login/);
|
||||
const logoutBtn = page.locator('button').filter({ hasText: /Logout|로그아웃/i }).first();
|
||||
await logoutBtn.waitFor({ state: 'visible' });
|
||||
await logoutBtn.click();
|
||||
await expect(page).toHaveURL(/.*\/login.*/, { timeout: 15000 });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user