π OpenHands: AI-Driven Development
fix(canvas): hide cloud orgs the server marks is_visible=false (#17624)
Tim O'Farrell65c768f5e46c
diff --git a/__tests__/hooks/query/use-cloud-organizations.test.tsx b/__tests__/hooks/query/use-cloud-organizations.test.tsxnew file mode 100644index 0000000000..2deae72266--- /dev/null+++ b/__tests__/hooks/query/use-cloud-organizations.test.tsx@@ -0,0 +1,167 @@+import React from "react";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { renderHook, waitFor } from "@testing-library/react";+import { beforeEach, describe, expect, it, vi } from "vitest";+import {+ __resetActiveStoreForTests,+ setRegisteredBackends,+} from "#/api/backend-registry/active-store";+import type { Backend } from "#/api/backend-registry/types";+import { ActiveBackendProvider } from "#/contexts/active-backend-context";+import { useAllCloudOrganizations } from "#/hooks/query/use-cloud-organizations";++const getCloudOrganizationsMock = vi.fn();+const getCurrentCloudApiKeyMock = vi.fn();++vi.mock("#/api/cloud/organization-service.api", () => ({+ getCloudOrganizations: (...args: unknown[]) =>+ getCloudOrganizationsMock(...args),+ getCurrentCloudApiKey: (...args: unknown[]) =>+ getCurrentCloudApiKeyMock(...args),+}));++function cloudBackend(overrides: Partial<Backend> = {}): Backend {+ return {+ id: "prod",+ name: "Production",+ host: "https://app.all-hands.dev",+ apiKey: "bearer-token",+ kind: "cloud",+ ...overrides,+ };+}++function makeWrapper() {+ const queryClient = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ function Wrapper({ children }: { children: React.ReactNode }) {+ return (+ <QueryClientProvider client={queryClient}>+ <ActiveBackendProvider>{children}</ActiveBackendProvider>+ </QueryClientProvider>+ );+ }+ return Wrapper;+}++beforeEach(() => {+ window.localStorage.clear();+ __resetActiveStoreForTests();+ getCloudOrganizationsMock.mockReset();+ getCurrentCloudApiKeyMock.mockReset();+ // Legacy/unbound key by default: the unfiltered path the visibility tests+ // care about. Key-scoping has its own coverage in the selector tests.+ getCurrentCloudApiKeyMock.mockResolvedValue({+ orgId: null,+ isLegacyKey: true,+ });+ setRegisteredBackends([cloudBackend()]);+});++describe("useAllCloudOrganizations visibility filtering", () => {+ it("hides orgs the cloud marks is_visible=false", async () => {+ getCloudOrganizationsMock.mockResolvedValue({+ items: [+ { id: "org-team", name: "Acme Inc", is_visible: true },+ { id: "org-personal", name: "Personal", is_visible: false },+ ],+ currentOrgId: "org-team",+ });++ const { result } = renderHook(() => useAllCloudOrganizations(), {+ wrapper: makeWrapper(),+ });++ await waitFor(() => {+ expect(result.current.prod?.orgs).toHaveLength(1);+ });+ expect(result.current.prod?.orgs.map((o) => o.id)).toEqual(["org-team"]);+ });++ it("keeps orgs when the backend predates is_visible (field absent)", async () => {+ // Backwards compatibility: an older app-server never sends the field,+ // and those orgs must not disappear from the selector.+ getCloudOrganizationsMock.mockResolvedValue({+ items: [+ { id: "org-1", name: "Acme Inc" },+ { id: "org-2", name: "Personal", is_personal: true },+ ],+ currentOrgId: "org-1",+ });++ const { result } = renderHook(() => useAllCloudOrganizations(), {+ wrapper: makeWrapper(),+ });++ await waitFor(() => {+ expect(result.current.prod?.orgs).toHaveLength(2);+ });+ expect(result.current.prod?.orgs.map((o) => o.id)).toEqual([+ "org-1",+ "org-2",+ ]);+ });++ it("treats an explicit is_visible=true as visible", async () => {+ getCloudOrganizationsMock.mockResolvedValue({+ items: [{ id: "org-1", name: "Acme Inc", is_visible: true }],+ currentOrgId: "org-1",+ });++ const { result } = renderHook(() => useAllCloudOrganizations(), {+ wrapper: makeWrapper(),+ });++ await waitFor(() => {+ expect(result.current.prod?.orgs).toHaveLength(1);+ });+ });++ it("hides invisible orgs on the cookie-auth path too", async () => {+ setRegisteredBackends([cloudBackend({ authMode: "cookie" })]);+ getCloudOrganizationsMock.mockResolvedValue({+ items: [+ { id: "org-team", name: "Acme Inc" },+ { id: "org-personal", name: "Personal", is_visible: false },+ ],+ currentOrgId: "org-team",+ });++ const { result } = renderHook(() => useAllCloudOrganizations(), {+ wrapper: makeWrapper(),+ });++ await waitFor(() => {+ expect(result.current.prod?.orgs).toHaveLength(1);+ });+ expect(result.current.prod?.orgs.map((o) => o.id)).toEqual(["org-team"]);+ // The cookie path short-circuits before the key lookup.+ expect(getCurrentCloudApiKeyMock).not.toHaveBeenCalled();+ });++ it("applies visibility before key scoping by org id", async () => {+ // An invisible org must stay hidden even when it is the org the API key+ // is bound to β the key scoping is an intersection, never a re-reveal.+ getCurrentCloudApiKeyMock.mockResolvedValue({+ orgId: "org-personal",+ isLegacyKey: false,+ });+ getCloudOrganizationsMock.mockResolvedValue({+ items: [+ { id: "org-team", name: "Acme Inc" },+ { id: "org-personal", name: "Personal", is_visible: false },+ ],+ currentOrgId: "org-personal",+ });++ const { result } = renderHook(() => useAllCloudOrganizations(), {+ wrapper: makeWrapper(),+ });++ await waitFor(() => {+ expect(result.current.prod?.isLoading).toBe(false);+ });+ expect(result.current.prod?.orgs).toEqual([]);+ });+});diff --git a/src/api/cloud/types.ts b/src/api/cloud/types.tsindex 2c66f5639e..270e5ec73b 100644--- a/src/api/cloud/types.ts+++ b/src/api/cloud/types.ts@@ -7,6 +7,13 @@ export interface CloudOrganization {id: string;name: string;is_personal?: boolean;+ /**+ * Whether the org should be offered in the UI. The cloud sets this per org+ * from its `HIDE_PERSONAL_WORKSPACES` policy. Optional on purpose: an+ * app-server that predates the field omits it, and an absent flag means+ * "visible" β only an explicit `false` hides an org.+ */+ is_visible?: boolean;}export interface CloudOrganizationsResponse {diff --git a/src/hooks/query/use-cloud-organizations.ts b/src/hooks/query/use-cloud-organizations.tsindex 48387c5c86..886b38a428 100644--- a/src/hooks/query/use-cloud-organizations.ts+++ b/src/hooks/query/use-cloud-organizations.ts@@ -31,13 +31,25 @@ export function useAllCloudOrganizations() {// with no binding fall through unfiltered.queryFn: async () => {const orgs = await getCloudOrganizations(backend);- if (backend.authMode === "cookie") return orgs;+ // Drop orgs the cloud marks invisible (`is_visible === false`, set+ // from its `HIDE_PERSONAL_WORKSPACES` policy). Every consumer β+ // the selector rows, the "Cloud Settings" deep link, the manage+ // modal and the synced-settings badge β reads orgs from here, so+ // filtering once keeps them from disagreeing about which orgs+ // exist. Only an explicit `false` hides an org: an app-server that+ // predates the field omits it, and those orgs stay visible.+ const visible = orgs.items.filter((o) => o.is_visible !== false);+ if (backend.authMode === "cookie") {+ return { ...orgs, items: visible };+ }const key = await getCurrentCloudApiKey(backend);- if (key.isLegacyKey || key.orgId === null) return orgs;+ if (key.isLegacyKey || key.orgId === null) {+ return { ...orgs, items: visible };+ }return {...orgs,- items: orgs.items.filter((o) => o.id === key.orgId),+ items: visible.filter((o) => o.id === key.orgId),};},staleTime: 1000 * 60 * 5,