Files
CIAgent/apps/web/tests/app-layout.test.tsx
T
sakshamandClaude Sonnet 5 4ee38b6241 Add DB viewer access logging, account deletion, and forced password change
Logs a distinct db_viewer_accessed event (not just the earlier
session_created "requested" event) when an admin's browser actually
completes the hand-off into Adminer. Adds a password-confirmed
account-deletion box to Settings, relying on the existing ON DELETE
CASCADE foreign keys to clean up everything the account owns. Adds an
admin-only "require password change" flag that get_current_user
enforces server-side (403 on everything except /auth/me,
/auth/change-password, /auth/logout) - meant for handing a demo
account to someone with a known sample password.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-08-05 23:41:21 -04:00

87 lines
2.6 KiB
TypeScript

import { screen, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import DashboardLayout from "@/app/(app)/layout";
import { renderWithQueryClient } from "./test-utils";
const replaceMock = vi.fn();
vi.mock("next/navigation", () => ({
useRouter: () => ({ push: vi.fn(), replace: replaceMock }),
usePathname: () => "/dashboard",
}));
function meBody(mustChangePassword: boolean) {
return {
id: "user-1",
email: "[email protected]",
display_name: "Regular User",
timezone: "America/New_York",
is_active: true,
is_admin: false,
auth_mode: "jwt",
must_change_password: mustChangePassword,
};
}
function systemStatusBody() {
return {
app_env: "development",
auth_mode: "jwt",
llm_provider: "mock",
search_provider: "mock",
sms_enabled: false,
sms_provider: "twilio",
ninjapear_configured: false,
ninjapear_credit_balance: null,
ninjapear_estimated_credits_per_company: null,
is_localhost: false,
turnstile_site_key: null,
components: [],
};
}
function mockFetchImplementation(mustChangePassword: boolean) {
return vi.fn().mockImplementation((url: string) => {
const path = url.replace("http://localhost:8000", "");
if (path === "/api/v1/auth/me") {
return Promise.resolve({ ok: true, status: 200, json: async () => meBody(mustChangePassword) });
}
if (path === "/api/v1/system/status") {
return Promise.resolve({ ok: true, status: 200, json: async () => systemStatusBody() });
}
return Promise.resolve({ ok: false, status: 404, json: async () => ({ detail: "not found" }) });
});
}
beforeEach(() => {
replaceMock.mockClear();
});
describe("DashboardLayout - must_change_password redirect", () => {
it("redirects to /change-password and withholds the page content when the flag is set", async () => {
vi.stubGlobal("fetch", mockFetchImplementation(true));
renderWithQueryClient(
<DashboardLayout>
<div>Protected dashboard content</div>
</DashboardLayout>,
);
await waitFor(() => {
expect(replaceMock).toHaveBeenCalledWith("/change-password");
});
expect(screen.queryByText(/protected dashboard content/i)).not.toBeInTheDocument();
});
it("renders normally when the flag is not set", async () => {
vi.stubGlobal("fetch", mockFetchImplementation(false));
renderWithQueryClient(
<DashboardLayout>
<div>Protected dashboard content</div>
</DashboardLayout>,
);
expect(await screen.findByText(/protected dashboard content/i)).toBeInTheDocument();
expect(replaceMock).not.toHaveBeenCalledWith("/change-password");
});
});