#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
Stop the Shared Signing user picker from listing users for guests (#8228)
Anthony Stirlingaf6268e8c396
diff --git a/frontend/public/locales/en-US/translation.toml b/frontend/public/locales/en-US/translation.tomlindex 3480a7bde5..3c9318e524 100644--- a/frontend/public/locales/en-US/translation.toml+++ b/frontend/public/locales/en-US/translation.toml@@ -2969,6 +2969,7 @@ loadError = "Failed to load users"noTeam = "No Team"noUsers = "No other users found."placeholder = "Select users..."+signInRequired = "Sign in to request signatures from other people."you = "{{name}} (You)"[certSign.device]diff --git a/frontend/src/editor/core/api/users.ts b/frontend/src/editor/core/api/users.tsindex a55049b1f8..bab85ce54d 100644--- a/frontend/src/editor/core/api/users.ts+++ b/frontend/src/editor/core/api/users.ts@@ -2,7 +2,10 @@ import apiClient from "@editor/services/apiClient";import { UserSummary } from "@editor/types/signingSession";export async function fetchUsers(): Promise<UserSummary[]> {- const response = await apiClient.get<UserSummary[]>("/api/v1/user/users");+ // UserSelector raises its own error toast.+ const response = await apiClient.get<UserSummary[]>("/api/v1/user/users", {+ suppressErrorToast: true,+ });// A proxy can answer 200 with an HTML login page; callers assume an array.return Array.isArray(response.data) ? response.data : [];}diff --git a/frontend/src/editor/core/components/shared/UserSelector.test.tsx b/frontend/src/editor/core/components/shared/UserSelector.test.tsxnew file mode 100644index 0000000000..203ebaff6f--- /dev/null+++ b/frontend/src/editor/core/components/shared/UserSelector.test.tsx@@ -0,0 +1,136 @@+import { describe, it, expect, vi, beforeEach } from "vitest";+import { render, screen, waitFor } from "@testing-library/react";+import { MantineProvider } from "@mantine/core";+import { MemoryRouter } from "react-router-dom";+import { TestQueryProvider } from "@editor/tests/utils/TestQueryProvider";+import UserSelector from "@editor/components/shared/UserSelector";+import { fetchUsers } from "@editor/api/users";+import { alert } from "@editor/components/toast";++vi.mock("@editor/api/users", () => ({ fetchUsers: vi.fn() }));+vi.mock("@editor/components/toast", () => ({ alert: vi.fn() }));+vi.mock("react-i18next", () => {+ const t = (_k: string, fallback?: string) => fallback ?? _k;+ return { useTranslation: () => ({ t }) };+});+const authState = vi.hoisted(() => ({+ user: { id: "1" } as { id: string } | null,+ isAnonymous: false,+ loading: false,+}));+vi.mock("@editor/auth/UseSession", () => ({ useAuth: () => authState }));++const mockFetch = vi.mocked(fetchUsers);++function selector() {+ return (+ <MantineProvider>+ <MemoryRouter>+ <TestQueryProvider>+ <UserSelector value={[]} onChange={() => {}} />+ </TestQueryProvider>+ </MemoryRouter>+ </MantineProvider>+ );+}++function renderSelector() {+ const result = render(selector());+ return { ...result, rerenderSelector: () => result.rerender(selector()) };+}++describe("UserSelector", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ authState.user = { id: "1" };+ authState.isAnonymous = false;+ authState.loading = false;+ mockFetch.mockResolvedValue([]);+ });++ it("does not list users for a guest", async () => {+ authState.user = { id: "guest" };+ authState.isAnonymous = true;++ renderSelector();++ expect(+ await screen.findByText(+ "Sign in to request signatures from other people.",+ ),+ ).toBeInTheDocument();+ expect(mockFetch).not.toHaveBeenCalled();+ expect(vi.mocked(alert)).not.toHaveBeenCalled();+ });++ it("lists users for a signed-in account", async () => {+ renderSelector();++ await waitFor(() => expect(mockFetch).toHaveBeenCalledTimes(1));+ expect(+ await screen.findByText("No other users found."),+ ).toBeInTheDocument();+ });++ it("waits for authentication before resolving to a guest", async () => {+ authState.user = null;+ authState.loading = true;+ const { rerenderSelector } = renderSelector();++ expect(mockFetch).not.toHaveBeenCalled();+ expect(screen.queryByText("No other users found.")).not.toBeInTheDocument();++ authState.user = { id: "guest" };+ authState.isAnonymous = true;+ authState.loading = false;+ rerenderSelector();++ expect(+ await screen.findByText(+ "Sign in to request signatures from other people.",+ ),+ ).toBeInTheDocument();+ expect(mockFetch).not.toHaveBeenCalled();+ expect(alert).not.toHaveBeenCalled();+ });++ it("loads users once authentication resolves to a signed-in account", async () => {+ authState.user = null;+ authState.loading = true;+ const { rerenderSelector } = renderSelector();+ expect(mockFetch).not.toHaveBeenCalled();++ authState.user = { id: "1" };+ authState.loading = false;+ rerenderSelector();++ await waitFor(() => expect(mockFetch).toHaveBeenCalledTimes(1));+ expect(+ await screen.findByText("No other users found."),+ ).toBeInTheDocument();+ });++ it("reports a signed-in query failure once and hides it for a guest", async () => {+ mockFetch.mockRejectedValue(new Error("Unable to load users"));+ const { rerenderSelector } = renderSelector();++ await waitFor(() => expect(alert).toHaveBeenCalledTimes(1));+ expect(alert).toHaveBeenCalledWith({+ alertType: "error",+ title: "common.error",+ body: "Failed to load users",+ });++ authState.user = { id: "guest" };+ authState.isAnonymous = true;+ rerenderSelector();++ expect(+ await screen.findByText(+ "Sign in to request signatures from other people.",+ ),+ ).toBeInTheDocument();+ expect(mockFetch).toHaveBeenCalledTimes(1);+ expect(alert).toHaveBeenCalledTimes(1);+ });+});diff --git a/frontend/src/editor/core/components/shared/UserSelector.tsx b/frontend/src/editor/core/components/shared/UserSelector.tsxindex c8b46b1219..d34b8c6fd6 100644--- a/frontend/src/editor/core/components/shared/UserSelector.tsx+++ b/frontend/src/editor/core/components/shared/UserSelector.tsx@@ -35,7 +35,7 @@ const UserSelector = ({presentation = "dropdown",}: UserSelectorProps) => {const { t } = useTranslation();- const { user } = useAuth();+ const { user, isAnonymous, loading: authLoading } = useAuth();const [stringValue, setStringValue] = useState<string[]>([]);const [search, setSearch] = useState("");@@ -44,16 +44,21 @@ const UserSelector = ({isPending: loading,error,refetch,- } = useQuery({ queryKey: qk.users(), queryFn: fetchUsers });+ } = useQuery({+ queryKey: qk.users(),+ queryFn: fetchUsers,+ // isAnonymous is false until authentication resolves.+ enabled: !authLoading && !isAnonymous,+ });useEffect(() => {- if (!error) return;+ if (authLoading || isAnonymous || !error) return;alert({alertType: "error",title: t("common.error"),body: t("certSign.collab.userSelector.loadError", "Failed to load users"),});- }, [error, t]);+ }, [authLoading, isAnonymous, error, t]);const selectData = useMemo<GroupedData[]>(() => {const usersByTeam: Record<string, SelectItem[]> = {};@@ -99,6 +104,21 @@ const UserSelector = ({setStringValue(result);}, [value]);+ if (authLoading) {+ return <Loader size="sm" />;+ }++ if (isAnonymous) {+ return (+ <Text size="sm" c="dimmed">+ {t(+ "certSign.collab.userSelector.signInRequired",+ "Sign in to request signatures from other people.",+ )}+ </Text>+ );+ }+if (loading) {return <Loader size="sm" />;}