Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

2.3 KB
Raw Blame
/* @vitest-environment jsdom */
import { render } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import AutoRoutersTabPanel from "./AutoRoutersTabPanel";

const panelProps = vi.fn();
vi.mock("../components/AutoRouters/AutoRoutersPanel", () => ({
  AutoRoutersPanel: (props: Record<string, unknown>) => {
    panelProps(props);
    return <div data-testid="auto-routers-panel" />;
  },
}));

const mockUseAuthorized = vi.fn();
const mockUseTeams = vi.fn().mockReturnValue({ data: [] });
const mockUseUISettings = vi.fn(() => ({ data: { values: {} } }));
vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({ default: () => mockUseAuthorized() }));
vi.mock("@/app/(dashboard)/hooks/teams/useTeams", () => ({ useTeams: () => mockUseTeams() }));
vi.mock("@/app/(dashboard)/hooks/uiSettings/useUISettings", () => ({
  useUISettings: () => mockUseUISettings(),
}));

const SESSION = { accessToken: "at", userRole: "Admin", userId: "u1", isViewOnly: false };

const lastProps = () => panelProps.mock.calls.at(-1)?.[0] as { createScope: string };

describe("AutoRoutersTabPanel", () => {
  it("honors the global creation prohibition even when member auto-router management is enabled", () => {
    mockUseAuthorized.mockReturnValue({ ...SESSION, userRole: "Internal User" });
    mockUseTeams.mockReturnValueOnce({
      data: [
        {
          team_id: "team-1",
          members_with_roles: [{ user_id: "u1", role: "user" }],
          team_member_permissions: ["/auto_router/manage"],
        },
      ],
    });
    mockUseUISettings.mockReturnValueOnce({ data: { values: { disable_model_add_for_internal_users: true } } });
    render(<AutoRoutersTabPanel />);

    expect(lastProps().createScope).toBe("forbidden");
  });

  it("grants an unscoped create to a real proxy admin", () => {
    mockUseAuthorized.mockReturnValue(SESSION);
    render(<AutoRoutersTabPanel />);
    expect(lastProps().createScope).toBe("unscoped-ok");
  });

  // The masqueraded "Admin" a proxy_admin_viewer session carries: POST /model/new 403s it,
  // so the panel must not be told it may create.
  it("withholds the create affordance from a view-only admin session", () => {
    mockUseAuthorized.mockReturnValue({ ...SESSION, isViewOnly: true });
    render(<AutoRoutersTabPanel />);
    expect(lastProps().createScope).toBe("forbidden");
  });
});