fix(ui): load the owner's budget for keys opened outside the current page
Some checks failed
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled
Terraform Provider / gofmt, vet, build, test (push) Has been cancelled
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Has been cancelled
Terraform Modules / fmt, validate, test (aws) (push) Has been cancelled
Terraform Modules / fmt, validate, test (gcp) (push) Has been cancelled

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
jesus 2026-09-18 21:09:40 +00:00
parent c7df57ffcc
commit 79464d22b5
3 changed files with 125 additions and 3 deletions

View file

@ -0,0 +1,101 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { renderHook, waitFor } from "@testing-library/react";
import React, { ReactNode } from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { keyInfoV1Call, userGetInfoV2 } from "@/components/networking";
import { useKeyInfo } from "./useKeyInfo";
vi.mock("@/components/networking", () => ({
keyInfoV1Call: vi.fn(),
userGetInfoV2: vi.fn(),
}));
const mockUseAuthorized = vi.fn();
vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({
default: () => mockUseAuthorized(),
}));
const mockKeyInfoV1Call = vi.mocked(keyInfoV1Call);
const mockUserGetInfoV2 = vi.mocked(userGetInfoV2);
const createWrapper = () => {
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const wrapper = ({ children }: { children: ReactNode }) =>
React.createElement(QueryClientProvider, { client: queryClient }, children);
return { queryClient, wrapper };
};
const KEY_ID = "sk-key-1";
const ACCESS_TOKEN = "sk-access";
const OWNER = {
user_id: "user-1",
user_email: "owner@example.com",
user_alias: "Budget Owner",
user_role: "user",
spend: 0,
max_budget: 1500,
models: [],
budget_duration: "1mo",
budget_reset_at: null,
metadata: null,
created_at: null,
updated_at: null,
sso_user_id: null,
teams: [],
};
const keyInfoResponse = (info: Record<string, unknown>) => ({ info });
describe("useKeyInfo", () => {
beforeEach(() => {
mockKeyInfoV1Call.mockReset();
mockUserGetInfoV2.mockReset();
mockUseAuthorized.mockReturnValue({ accessToken: ACCESS_TOKEN });
});
it("attaches the owner's budget fields when the key has a user_id", async () => {
mockKeyInfoV1Call.mockResolvedValue(keyInfoResponse({ user_id: "user-1", key_alias: "team-key" }));
mockUserGetInfoV2.mockResolvedValue(OWNER);
const { wrapper } = createWrapper();
const { result } = renderHook(() => useKeyInfo(KEY_ID), { wrapper });
await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(mockUserGetInfoV2).toHaveBeenCalledWith(ACCESS_TOKEN, "user-1");
const expectedUser = {
user_id: "user-1",
user_email: "owner@example.com",
user_alias: "Budget Owner",
max_budget: 1500,
budget_duration: "1mo",
};
expect(result.current.data?.user).toEqual(expectedUser);
});
it("does not fetch an owner when the key has no user_id", async () => {
mockKeyInfoV1Call.mockResolvedValue(keyInfoResponse({ user_id: null, key_alias: "service-key" }));
const { wrapper } = createWrapper();
const { result } = renderHook(() => useKeyInfo(KEY_ID), { wrapper });
await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(mockUserGetInfoV2).not.toHaveBeenCalled();
expect(result.current.data?.user).toBeUndefined();
});
it("still resolves the key data when the owner lookup fails", async () => {
mockKeyInfoV1Call.mockResolvedValue(keyInfoResponse({ user_id: "user-1" }));
mockUserGetInfoV2.mockRejectedValue(new Error("403"));
const { wrapper } = createWrapper();
const { result } = renderHook(() => useKeyInfo(KEY_ID), { wrapper });
await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(result.current.data?.token).toBe(KEY_ID);
expect(result.current.data?.api_key).toBe(KEY_ID);
expect(result.current.data?.user).toBeUndefined();
});
});

View file

@ -2,10 +2,25 @@ import { useQuery, UseQueryResult } from "@tanstack/react-query";
import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized";
import { KeyResponse } from "@/components/key_team_helpers/key_list";
import { keyInfoV1Call } from "@/components/networking";
import { keyInfoV1Call, userGetInfoV2 } from "@/components/networking";
import { keyKeys } from "./useKeys";
const fetchOwner = async (accessToken: string, userId: string): Promise<KeyResponse["user"] | undefined> => {
try {
const owner = await userGetInfoV2(accessToken, userId);
return {
user_id: owner.user_id,
user_email: owner.user_email,
user_alias: owner.user_alias,
max_budget: owner.max_budget,
budget_duration: owner.budget_duration,
};
} catch {
return undefined;
}
};
export function useKeyInfo(keyId: string | null, options?: { enabled?: boolean }): UseQueryResult<KeyResponse> {
const { accessToken } = useAuthorized();
@ -14,10 +29,16 @@ export function useKeyInfo(keyId: string | null, options?: { enabled?: boolean }
queryFn: async () => {
if (!accessToken || !keyId) throw new Error("Missing access token or key id");
const keyData = await keyInfoV1Call(accessToken, keyId);
const info = keyData["info"];
const owner =
typeof info.user_id === "string" && info.user_id !== ""
? await fetchOwner(accessToken, info.user_id)
: undefined;
return {
...keyData["info"],
...info,
token: keyId,
api_key: keyId,
...(owner ? { user: owner } : {}),
};
},
enabled: Boolean(accessToken && keyId) && (options?.enabled ?? true),

View file

@ -115,7 +115,7 @@ export interface KeyResponse {
next_rotation_at?: string;
user?: {
user_id: string;
user_email: string;
user_email: string | null;
user_alias: string | null;
max_budget?: number | null;
budget_duration?: string | null;