-
Notifications
You must be signed in to change notification settings - Fork 91
ENG-3001: Add OAuth API clients list page #7747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f111dcc
32e463d
3c0d92e
62895ce
00a3209
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| type: Added | ||
| description: Added OAuth API clients list page with paginated table and nav entry | ||
| pr: 7747 | ||
| labels: [] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { render, screen } from "@testing-library/react"; | ||
|
|
||
| import OAuthClientsList from "./OAuthClientsTable"; | ||
|
|
||
| // --- Module mocks --- | ||
|
|
||
| const mockUseHasPermission = jest.fn(); | ||
| jest.mock("~/features/common/Restrict", () => ({ | ||
| useHasPermission: () => mockUseHasPermission(), | ||
| })); | ||
|
|
||
| const mockUseListOAuthClientsQuery = jest.fn(); | ||
| jest.mock("./oauth-clients.slice", () => ({ | ||
| useListOAuthClientsQuery: () => mockUseListOAuthClientsQuery(), | ||
| })); | ||
|
|
||
| // LinkCell uses NextLink which doesn't work in jsdom (NodeList.includes bug) | ||
| jest.mock("~/features/common/table/cells/LinkCell", () => ({ | ||
| LinkCell: ({ href, children }: any) => | ||
| href ? <a href={href}>{children}</a> : <span>{children}</span>, | ||
| })); | ||
|
|
||
| // Tooltip mock: real Tooltip only renders content in a portal on hover, | ||
| // so we use a lightweight stand-in that exposes the title as a DOM attribute | ||
| // for static assertions. | ||
| const MockTooltip = ({ title, children }: any) => ( | ||
| <span data-tooltip={title}>{children}</span> | ||
| ); | ||
| MockTooltip.displayName = "MockTooltip"; | ||
|
|
||
| jest.mock( | ||
| "fidesui", | ||
| () => | ||
| new Proxy(jest.requireActual("fidesui"), { | ||
| get(target, prop) { | ||
| if (prop === "Tooltip") { | ||
| return MockTooltip; | ||
| } | ||
| return target[prop as keyof typeof target]; | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| // --- Helpers --- | ||
|
|
||
| const makeClient = (overrides = {}) => ({ | ||
| client_id: "abc123", | ||
| name: "My Client", | ||
| description: "A test client", | ||
| scopes: ["client:create", "client:read"], | ||
| ...overrides, | ||
| }); | ||
|
|
||
| const renderList = () => render(<OAuthClientsList />); | ||
|
|
||
| // --- Tests --- | ||
|
|
||
| describe("OAuthClientsList", () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| Object.defineProperty(navigator, "clipboard", { | ||
| value: { writeText: jest.fn() }, | ||
| configurable: true, | ||
| }); | ||
| mockUseHasPermission.mockReturnValue(true); | ||
| mockUseListOAuthClientsQuery.mockReturnValue({ | ||
| data: { items: [makeClient()], total: 1, page: 1, size: 25 }, | ||
| isLoading: false, | ||
| }); | ||
| }); | ||
|
|
||
| describe("list item rendering", () => { | ||
| it("renders the client name", () => { | ||
| renderList(); | ||
| expect(screen.getByText("My Client")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders the client ID in monospace", () => { | ||
| renderList(); | ||
| expect(screen.getByText("abc123")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders the scope count tag", () => { | ||
| renderList(); | ||
| expect(screen.getByText("2 scopes")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders the description when present", () => { | ||
| renderList(); | ||
| expect(screen.getByText("A test client")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("does not render a description section when absent", () => { | ||
| mockUseListOAuthClientsQuery.mockReturnValue({ | ||
| data: { items: [makeClient({ description: null })], total: 1 }, | ||
| isLoading: false, | ||
| }); | ||
| renderList(); | ||
| expect(screen.queryByText("A test client")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders a copy button for the client ID", () => { | ||
| renderList(); | ||
| expect(screen.getByTestId("clipboard-btn")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("shows 'Unnamed' when client has no name", () => { | ||
| mockUseListOAuthClientsQuery.mockReturnValue({ | ||
| data: { items: [makeClient({ name: null })], total: 1 }, | ||
| isLoading: false, | ||
| }); | ||
| renderList(); | ||
| expect(screen.getByText("Unnamed")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("empty state", () => { | ||
| it("renders empty state text when there are no clients", () => { | ||
| mockUseListOAuthClientsQuery.mockReturnValue({ | ||
| data: { items: [], total: 0 }, | ||
| isLoading: false, | ||
| }); | ||
| renderList(); | ||
| expect(screen.getByText(/No API clients yet/)).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("loading state", () => { | ||
| it("renders without crashing while loading", () => { | ||
| mockUseListOAuthClientsQuery.mockReturnValue({ | ||
| data: undefined, | ||
| isLoading: true, | ||
| }); | ||
| expect(() => renderList()).not.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("client name link — with CLIENT_UPDATE permission", () => { | ||
| it("renders the name as a link to the detail page", () => { | ||
| mockUseHasPermission.mockReturnValue(true); | ||
| renderList(); | ||
| const link = screen.getByText("My Client").closest("a"); | ||
| expect(link).toHaveAttribute("href", "/api-clients/abc123"); | ||
| }); | ||
|
|
||
| it("does not show a permission tooltip", () => { | ||
| mockUseHasPermission.mockReturnValue(true); | ||
| renderList(); | ||
| const tooltip = screen.getByText("My Client").closest("[data-tooltip]"); | ||
| expect(tooltip?.getAttribute("data-tooltip")).toBeFalsy(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("client name — without CLIENT_UPDATE permission", () => { | ||
| beforeEach(() => mockUseHasPermission.mockReturnValue(false)); | ||
|
|
||
| it("renders the name as plain text (no link)", () => { | ||
| renderList(); | ||
| expect(screen.getByText("My Client").closest("a")).toBeNull(); | ||
| }); | ||
|
|
||
| it("shows a tooltip explaining the permission requirement", () => { | ||
| renderList(); | ||
| const tooltip = screen.getByText("My Client").closest("[data-tooltip]"); | ||
| expect(tooltip?.getAttribute("data-tooltip")).toMatch(/permission/i); | ||
| }); | ||
| }); | ||
|
|
||
| describe("pagination", () => { | ||
| it("always renders the pagination component", () => { | ||
| renderList(); | ||
| expect(screen.getByText(/Total 1 items/)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("shows the total item count", () => { | ||
| mockUseListOAuthClientsQuery.mockReturnValue({ | ||
| data: { items: [makeClient()], total: 42 }, | ||
| isLoading: false, | ||
| }); | ||
| renderList(); | ||
| expect(screen.getByText(/Total 42 items/)).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,141 @@ | ||||||
| import { Flex, List, Pagination, Tag, Tooltip, Typography } from "fidesui"; | ||||||
| import { useState } from "react"; | ||||||
|
|
||||||
| import ClipboardButton from "~/features/common/ClipboardButton"; | ||||||
| import { API_CLIENTS_ROUTE } from "~/features/common/nav/routes"; | ||||||
| import { useHasPermission } from "~/features/common/Restrict"; | ||||||
| import { LinkCell } from "~/features/common/table/cells/LinkCell"; | ||||||
| import { | ||||||
| DEFAULT_PAGE_SIZE, | ||||||
| DEFAULT_PAGE_SIZES, | ||||||
| } from "~/features/common/table/constants"; | ||||||
| import { ClientResponse, ScopeRegistryEnum } from "~/types/api"; | ||||||
|
|
||||||
| import { useListOAuthClientsQuery } from "./oauth-clients.slice"; | ||||||
|
|
||||||
| const { Text } = Typography; | ||||||
|
|
||||||
| const ClientListItem = ({ | ||||||
| client, | ||||||
| canUpdate, | ||||||
| }: { | ||||||
| client: ClientResponse; | ||||||
| canUpdate: boolean; | ||||||
| }) => { | ||||||
| return ( | ||||||
| <List.Item data-testid={`client-list-item-${client.client_id}`}> | ||||||
| <List.Item.Meta | ||||||
| title={ | ||||||
| <Flex align="center" gap={8}> | ||||||
| <Tooltip | ||||||
| title={ | ||||||
| !canUpdate | ||||||
| ? "You don't have permission to edit API clients." | ||||||
| : undefined | ||||||
| } | ||||||
| > | ||||||
| <span> | ||||||
| <LinkCell | ||||||
| href={ | ||||||
| canUpdate | ||||||
| ? `${API_CLIENTS_ROUTE}/${client.client_id}` | ||||||
| : undefined | ||||||
| } | ||||||
| > | ||||||
| {client.name ?? "Unnamed"} | ||||||
| </LinkCell> | ||||||
| </span> | ||||||
| </Tooltip> | ||||||
| <Tag>{client.scopes.length} scopes</Tag> | ||||||
| </Flex> | ||||||
| } | ||||||
| description={ | ||||||
| <Flex vertical gap={4}> | ||||||
| <Flex align="center" gap={4}> | ||||||
| <Text className="font-mono text-xs text-gray-400"> | ||||||
| {client.client_id} | ||||||
| </Text> | ||||||
| <ClipboardButton copyText={client.client_id} size="small" /> | ||||||
| </Flex> | ||||||
| {client.description && ( | ||||||
| <Text className="text-sm text-gray-700"> | ||||||
| {client.description} | ||||||
| </Text> | ||||||
| )} | ||||||
| </Flex> | ||||||
| } | ||||||
| /> | ||||||
| </List.Item> | ||||||
| ); | ||||||
| }; | ||||||
|
|
||||||
| const useOAuthClientsList = () => { | ||||||
| const [page, setPage] = useState(1); | ||||||
| const [pageSize, setPageSize] = useState(DEFAULT_PAGE_SIZE); | ||||||
|
|
||||||
| const { data, isLoading, error } = useListOAuthClientsQuery({ | ||||||
| page, | ||||||
| size: pageSize, | ||||||
| }); | ||||||
|
|
||||||
| return { | ||||||
| data: data?.items ?? [], | ||||||
| total: data?.total ?? 0, | ||||||
| isLoading, | ||||||
| error, | ||||||
| page, | ||||||
| pageSize, | ||||||
| setPage, | ||||||
| setPageSize, | ||||||
| }; | ||||||
| }; | ||||||
|
Comment on lines
+72
to
+91
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The Consider using import { useAntPagination } from "~/features/common/pagination/useAntPagination";
const useOAuthClientsList = () => {
const { pageIndex, pageSize, paginationProps } = useAntPagination();
const { data, isLoading, error } = useListOAuthClientsQuery({
page: pageIndex,
size: pageSize,
});
return {
data: data?.items ?? [],
total: data?.total ?? 0,
isLoading,
error,
paginationProps,
};
};Rule Used: Use the existing Learnt From Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||
|
|
||||||
| const OAuthClientsList = () => { | ||||||
| const { data, total, isLoading, page, pageSize, setPage, setPageSize } = | ||||||
| useOAuthClientsList(); | ||||||
| const canUpdate = useHasPermission([ScopeRegistryEnum.CLIENT_UPDATE]); | ||||||
|
|
||||||
| return ( | ||||||
| <div> | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The outer
Suggested change
Rule Used: Avoid using Learnt From Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||
| <List | ||||||
| loading={isLoading} | ||||||
| itemLayout="horizontal" | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: surface API errors to the user
Consider adding a simple error banner or inline message, e.g.: const { data, total, isLoading, error, page, pageSize, setPage, setPageSize } =
useOAuthClientsList();
if (error) {
return <Alert type="error" message="Failed to load API clients." />;
} |
||||||
| dataSource={data} | ||||||
| rowKey={(client) => client.client_id} | ||||||
| locale={{ | ||||||
| emptyText: ( | ||||||
| <div className="px-4 py-8 text-center"> | ||||||
| <Typography.Paragraph type="secondary"> | ||||||
| No API clients yet. Click "Create API client" to get | ||||||
| started. | ||||||
| </Typography.Paragraph> | ||||||
| </div> | ||||||
| ), | ||||||
| }} | ||||||
| renderItem={(client) => ( | ||||||
| <ClientListItem client={client} canUpdate={canUpdate} /> | ||||||
| )} | ||||||
| /> | ||||||
| <Flex justify="end" className="mt-4"> | ||||||
| <Pagination | ||||||
tvandort marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| current={page} | ||||||
| total={total} | ||||||
| pageSize={pageSize} | ||||||
| onChange={(newPage, newPageSize) => { | ||||||
| if (newPageSize !== pageSize) { | ||||||
| setPageSize(newPageSize); | ||||||
| setPage(1); | ||||||
| } else { | ||||||
| setPage(newPage); | ||||||
| } | ||||||
| }} | ||||||
| showSizeChanger | ||||||
| pageSizeOptions={DEFAULT_PAGE_SIZES} | ||||||
| showTotal={(totalItems) => `Total ${totalItems} items`} | ||||||
| /> | ||||||
| </Flex> | ||||||
| </div> | ||||||
| ); | ||||||
| }; | ||||||
|
|
||||||
| export default OAuthClientsList; | ||||||
Uh oh!
There was an error while loading. Please reload this page.