Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

3.2 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";
import { renderHook } from "@testing-library/react";
import { useAccountMenuShortcuts } from "@editor/components/shared/quickNav/useAccountMenuShortcuts";

const state = vi.hoisted(() => ({
  user: { id: "u1" } as { id: string } | null,
  isAnonymous: false,
  portalAccess: false,
  isAdmin: false,
  enableLogin: true,
  orgOwner: false,
  loading: false,
  accessSettled: true,
  rosterAvailable: true,
}));
vi.mock("@editor/routes/hasPortal", () => ({ HAS_PORTAL: true }));
vi.mock("@editor/auth/UseSession", () => ({
  useAuth: () => ({
    user: state.user && { ...state.user, orgOwner: state.orgOwner },
    isAdmin: state.isAdmin,
    isAnonymous: state.isAnonymous,
    portalAccess: state.portalAccess,
    loading: state.loading,
  }),
}));
vi.mock("@editor/hooks/usePortalAccess", () => ({
  usePortalAccessState: () => ({
    granted: state.portalAccess,
    settled: state.accessSettled,
  }),
}));
vi.mock("@editor/hooks/useRosterAvailable", () => ({
  useRosterAvailable: () => state.rosterAvailable,
}));
vi.mock("@editor/contexts/AppConfigContext", () => ({
  useAppConfig: () => ({
    config: { isAdmin: state.isAdmin, enableLogin: state.enableLogin },
  }),
}));

function shortcutIds() {
  return renderHook(() => useAccountMenuShortcuts()).result.current.map(
    (s) => s.id,
  );
}

describe("self-hosted account menu shortcuts", () => {
  beforeEach(() => {
    state.user = { id: "u1" };
    state.isAnonymous = false;
    state.portalAccess = false;
    state.isAdmin = false;
    state.enableLogin = true;
    state.orgOwner = false;
    state.loading = false;
    state.accessSettled = true;
    state.rosterAvailable = true;
  });

  it("gives a signed-in admin the server pages", () => {
    state.isAdmin = true;
    expect(shortcutIds()).toEqual(["users", "server"]);
  });

  it("offers billing only to the owner with processor access", () => {
    state.isAdmin = true;
    state.portalAccess = true;
    expect(shortcutIds()).not.toContain("billing");
    expect(shortcutIds()).not.toContain("plan");

    state.orgOwner = true;
    const ids = shortcutIds();
    expect(ids).toContain("billing");
    expect(ids).not.toContain("plan");
  });

  it("gives a regular user their keys", () => {
    expect(shortcutIds()).toEqual(["api-keys"]);
  });

  it("does not offer the admin roster to a regular processor member", () => {
    state.portalAccess = true;
    expect(shortcutIds()).toEqual(["api-keys"]);
  });

  it("hides the roster without a server connection", () => {
    state.isAdmin = true;
    state.rosterAvailable = false;
    expect(shortcutIds()).toEqual(["server"]);
  });

  it("waits for settled permissions before offering owner billing", () => {
    state.isAdmin = true;
    state.orgOwner = true;
    state.portalAccess = true;
    state.loading = true;
    expect(shortcutIds()).toEqual(["server"]);

    state.loading = false;
    state.accessSettled = false;
    expect(shortcutIds()).not.toContain("billing");
  });

  it("offers no admin pages with login off, where settings disables them all", () => {
    state.enableLogin = false;
    state.isAdmin = true;
    state.isAnonymous = true;
    expect(shortcutIds()).toEqual(["appearance", "hotkeys"]);
  });
});