1
0
forked from baron/baron-sso
Files
baron-sso/adminfront/tests/audit.spec.ts

192 lines
6.8 KiB
TypeScript

import { expect, test } from "@playwright/test";
test.describe("Audit Logs Management", () => {
test.beforeEach(async ({ page }) => {
await page.addInitScript(() => {
const authority = "http://localhost:5000/oidc";
const client_id = "adminfront";
const key = `oidc.user:${authority}:${client_id}`;
const authData = {
id_token: "fake-id-token",
access_token: "fake-token",
token_type: "Bearer",
scope: "openid profile email",
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));
window.localStorage.setItem("admin_session", "fake-token");
window.localStorage.setItem("locale", "ko");
window.localStorage.setItem("oidc.state", "dummy");
(
window as Window & typeof globalThis & { _IS_TEST_MODE?: boolean }
)._IS_TEST_MODE = true;
});
await page.route("**/oidc/**", async (route) => {
if (route.request().url().includes("/.well-known/openid-configuration")) {
return route.fulfill({
json: {
issuer: "http://localhost:5000/oidc",
authorization_endpoint: "http://localhost:5000/oidc/auth",
token_endpoint: "http://localhost:5000/oidc/token",
userinfo_endpoint: "http://localhost:5000/oidc/userinfo",
jwks_uri: "http://localhost:5000/oidc/jwks",
},
});
}
await route.fulfill({ json: { issuer: "http://localhost:5000/oidc" } });
});
await page.route(/.*\/api\/v1\/audit.*/, async (route) => {
const url = route.request().url();
const urlObj = new URL(url);
const cursor = urlObj.searchParams.get("cursor");
const offset = cursor ? 20 : 0;
return route.fulfill({
json: {
items: generateMockLogs(20, offset),
next_cursor: offset === 0 ? "fake-cursor" : null,
total: 40,
},
headers: { "Access-Control-Allow-Origin": "*" },
});
});
await page.route(/.*\/api\/v1\/user\/me/, async (route) => {
return route.fulfill({
json: {
id: "admin-user",
name: "Admin",
role: "super_admin",
manageableTenants: [],
},
headers: { "Access-Control-Allow-Origin": "*" },
});
});
});
const generateMockLogs = (count: number, offset = 0) => {
return Array.from({ length: count }, (_, i) => {
const id = offset + i;
return {
event_id: `evt-${id}`,
timestamp: new Date(Date.now() - id * 10000).toISOString(),
user_id: id % 2 === 0 ? "user-even" : "user-odd",
event_type: "API_REQUEST",
status: id % 5 === 0 ? "failure" : "success",
ip_address: "192.168.1.1",
user_agent: "Playwright",
details: JSON.stringify({
action: id % 3 === 0 ? "CREATE_TENANT" : "ROTATE_SECRET",
method: "POST",
path: `/v1/admin/tenants`,
}),
};
});
};
test("should load initial logs and display correctly", async ({ page }) => {
const responsePromise = page.waitForResponse(/.*\/api\/v1\/audit.*/);
await page.goto("/audit-logs");
await responsePromise;
// Check header
await expect(page.getByText(/감사 로그|Audit Logs/i).first()).toBeVisible({
timeout: 20000,
});
// Wait for the table to have at least one row
const firstRow = page.locator("tbody tr").first();
await expect(firstRow).toBeVisible({ timeout: 15000 });
// Check if table rows are rendered
await expect(page.locator("tbody tr")).toHaveCount(20);
// Check specific data visible in the row
await expect(page.locator("tbody")).toContainText("user-even");
await expect(page.locator("tbody")).toContainText("CREATE_TENANT");
});
test("should load more logs on scroll (infinite scroll)", async ({
page,
}) => {
const responsePromise = page.waitForResponse(/.*\/api\/v1\/audit.*/);
await page.goto("/audit-logs");
await responsePromise;
await expect(page.locator("tbody tr").first()).toBeVisible({ timeout: 15000 });
await expect(page.locator("tbody tr")).toHaveCount(20);
const loadMoreBtn = page.getByRole("button", {
name: /더 보기|Load more/i,
});
await expect(loadMoreBtn).toBeVisible({ timeout: 10000 });
await expect(loadMoreBtn).toBeEnabled();
const nextResponsePromise = page.waitForResponse(/.*\/api\/v1\/audit.*/);
await loadMoreBtn.click();
await nextResponsePromise;
// Wait for the next page to load (should reach 40)
await expect(page.locator("tbody tr")).toHaveCount(40, { timeout: 15000 });
await expect(page.locator("tbody")).toContainText("user-even");
});
test("should filter logs by Action and User ID locally", async ({ page }) => {
const responsePromise = page.waitForResponse(/.*\/api\/v1\/audit.*/);
await page.goto("/audit-logs");
await responsePromise;
await expect(page.locator("tbody tr").first()).toBeVisible({ timeout: 15000 });
await expect(page.locator("tbody tr")).toHaveCount(20);
// Search by User ID
const userIdInput = page.getByTestId("audit-search-user-id");
await userIdInput.fill("user-even");
// Playwright expect will retry, so it will wait for deferred value
await expect(page.locator("tbody tr")).toHaveCount(10, { timeout: 15000 });
await expect(page.locator("tbody")).not.toContainText("user-odd");
// Clear User ID
await userIdInput.clear();
await userIdInput.press("Enter");
await expect(page.locator("tbody tr")).toHaveCount(20, { timeout: 15000 });
// Search by Action
const actionInput = page.getByTestId("audit-search-action");
await actionInput.fill("ROTATE_SECRET");
// Check that we only see ROTATE_SECRET (20 - 7 = 13)
await expect(page.locator("tbody tr")).toHaveCount(13, { timeout: 15000 });
await expect(page.locator("tbody")).not.toContainText("CREATE_TENANT");
});
test("should filter logs by Status locally", async ({ page }) => {
const responsePromise = page.waitForResponse(/.*\/api\/v1\/audit.*/);
await page.goto("/audit-logs");
await responsePromise;
await expect(page.locator("tbody tr").first()).toBeVisible({ timeout: 15000 });
await expect(page.locator("tbody tr")).toHaveCount(20);
// Select "Failure" status
await page.getByTestId("audit-filter-status").selectOption("failure");
// ID % 5 === 0 are status "failure" (0, 5, 10, 15)
await expect(page.locator("tbody tr")).toHaveCount(4, { timeout: 15000 });
// Select "Success" status
await page.getByTestId("audit-filter-status").selectOption("success");
await expect(page.locator("tbody tr")).toHaveCount(16, { timeout: 15000 });
});
});