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]

Adding tests for model hub

yuneng-jiangd016ac863bab
diff --git a/ui/litellm-dashboard/src/components/AIHub/ModelHubTable.test.tsx b/ui/litellm-dashboard/src/components/AIHub/ModelHubTable.test.tsx
index 048fc9195a8..0a5cd17e571 100644
--- a/ui/litellm-dashboard/src/components/AIHub/ModelHubTable.test.tsx
+++ b/ui/litellm-dashboard/src/components/AIHub/ModelHubTable.test.tsx
@@ -3,6 +3,11 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import { renderWithProviders, screen, waitFor } from "../../../tests/test-utils";
import ModelHubTable from "./ModelHubTable";
+const mockUseUISettings = vi.hoisted(() => vi.fn());
+const mockGetCookie = vi.hoisted(() => vi.fn());
+const mockCheckTokenValidity = vi.hoisted(() => vi.fn());
+const mockRouterReplace = vi.hoisted(() => vi.fn());
+
vi.mock("@/components/networking", () => ({
getUiConfig: vi.fn(),
modelHubPublicModelsCall: vi.fn(),
@@ -17,7 +22,7 @@ vi.mock("@/components/networking", () => ({
vi.mock("next/navigation", () => ({
useRouter: () => ({
- replace: vi.fn(),
+ replace: mockRouterReplace,
}),
}));
@@ -25,11 +30,81 @@ vi.mock("@/components/public_model_hub", () => ({
default: () => <div>Public Model Hub</div>,
}));
+vi.mock("@/app/(dashboard)/hooks/uiSettings/useUISettings", () => ({
+ useUISettings: mockUseUISettings,
+}));
+
+vi.mock("@/utils/cookieUtils", () => ({
+ getCookie: mockGetCookie,
+}));
+
+vi.mock("@/utils/jwtUtils", () => ({
+ checkTokenValidity: mockCheckTokenValidity,
+}));
+
describe("ModelHubTable", () => {
afterEach(() => {
vi.clearAllMocks();
});
+ // Reusable helper function to setup mocks for auth redirect tests
+ const setupAuthRedirectTest = (
+ requireAuth: boolean,
+ tokenValue: string | null,
+ isTokenValid: boolean
+ ) => {
+ mockUseUISettings.mockReturnValue({
+ data: {
+ values: {
+ require_auth_for_public_ai_hub: requireAuth,
+ },
+ },
+ isLoading: false,
+ });
+ mockGetCookie.mockReturnValue(tokenValue);
+ mockCheckTokenValidity.mockReturnValue(isTokenValid);
+ mockRouterReplace.mockClear();
+
+ // Setup other required mocks
+ vi.mocked(networking.getUiConfig).mockResolvedValue({
+ server_root_path: "/",
+ proxy_base_url: "http://localhost:4000",
+ auto_redirect_to_sso: false,
+ admin_ui_disabled: false,
+ });
+ vi.mocked(networking.modelHubPublicModelsCall).mockResolvedValue([]);
+ vi.mocked(networking.getUiSettings).mockResolvedValue({
+ values: {
+ require_auth_for_public_ai_hub: requireAuth,
+ },
+ });
+ };
+
+ // Reusable test function for auth redirect scenarios
+ const testAuthRedirect = (
+ requireAuth: boolean,
+ tokenValue: string | null,
+ isTokenValid: boolean,
+ shouldRedirect: boolean,
+ description: string
+ ) => {
+ it(description, async () => {
+ setupAuthRedirectTest(requireAuth, tokenValue, isTokenValid);
+
+ renderWithProviders(
+ <ModelHubTable accessToken={null} publicPage={true} premiumUser={false} userRole={null} />
+ );
+
+ await waitFor(() => {
+ if (shouldRedirect) {
+ expect(mockRouterReplace).toHaveBeenCalledWith("http://localhost:4000/ui/login");
+ } else {
+ expect(mockRouterReplace).not.toHaveBeenCalled();
+ }
+ });
+ });
+ };
+
it("should render", async () => {
vi.mocked(networking.modelHubCall).mockResolvedValue({
data: [],
@@ -44,6 +119,10 @@ describe("ModelHubTable", () => {
vi.mocked(networking.getUiSettings).mockResolvedValue({
values: {},
});
+ mockUseUISettings.mockReturnValue({
+ data: { values: {} },
+ isLoading: false,
+ });
renderWithProviders(<ModelHubTable accessToken="test-token" publicPage={false} premiumUser={false} userRole={null} />);
@@ -66,6 +145,10 @@ describe("ModelHubTable", () => {
vi.mocked(networking.getUiSettings).mockResolvedValue({
values: {},
});
+ mockUseUISettings.mockReturnValue({
+ data: { values: {} },
+ isLoading: false,
+ });
renderWithProviders(<ModelHubTable accessToken={null} publicPage={true} premiumUser={false} userRole={null} />);
@@ -79,4 +162,56 @@ describe("ModelHubTable", () => {
expect(getUiConfigCallOrder).toBeLessThan(modelHubPublicModelsCallOrder);
});
+
+ describe("authentication redirect behavior", () => {
+ // Test cases where requireAuth is true - should redirect on invalid tokens
+ testAuthRedirect(
+ true,
+ null,
+ false,
+ true,
+ "should redirect to login when requireAuth is true and there is no token"
+ );
+
+ testAuthRedirect(
+ true,
+ "expired-token",
+ false,
+ true,
+ "should redirect to login when requireAuth is true and token is expired"
+ );
+
+ testAuthRedirect(
+ true,
+ "malformed-token",
+ false,
+ true,
+ "should redirect to login when requireAuth is true and token is malformed"
+ );
+
+ // Test cases where requireAuth is false - should NOT redirect regardless of token state
+ testAuthRedirect(
+ false,
+ null,
+ false,
+ false,
+ "should not redirect when requireAuth is false and there is no token"
+ );
+
+ testAuthRedirect(
+ false,
+ "expired-token",
+ false,
+ false,
+ "should not redirect when requireAuth is false and token is expired"
+ );
+
+ testAuthRedirect(
+ false,
+ "malformed-token",
+ false,
+ false,
+ "should not redirect when requireAuth is false and token is malformed"
+ );
+ });
});