Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

fix(sdk-cli): Fixes CLI quickstart not passing in the proper revision in query (#66493)

Sébastien71a8b3f4c13a
diff --git a/enterprise/frontend/src/embedding-sdk-package/cli/steps/setup-permission/index.ts b/enterprise/frontend/src/embedding-sdk-package/cli/steps/setup-permission/index.ts
new file mode 100644
index 00000000000..e259f5aa4a9
--- /dev/null
+++ b/enterprise/frontend/src/embedding-sdk-package/cli/steps/setup-permission/index.ts
@@ -0,0 +1,5 @@
+export {
+ setupPermissions,
+ grantAccessToModelCollection,
+} from "./setup-permission";
+export type { GrantAccessToModelCollectionOptions } from "./setup-permission";
diff --git a/enterprise/frontend/src/embedding-sdk-package/cli/steps/setup-permission.ts b/enterprise/frontend/src/embedding-sdk-package/cli/steps/setup-permission/setup-permission.ts
similarity index 61%
rename from enterprise/frontend/src/embedding-sdk-package/cli/steps/setup-permission.ts
rename to enterprise/frontend/src/embedding-sdk-package/cli/steps/setup-permission/setup-permission.ts
index 69969af7bfa..55fb04390ca 100644
--- a/enterprise/frontend/src/embedding-sdk-package/cli/steps/setup-permission.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/cli/steps/setup-permission/setup-permission.ts
@@ -1,15 +1,15 @@
-import { SANDBOXED_GROUP_NAMES } from "../constants/config";
-import { getNoTenantMessage } from "../constants/messages";
-import type { CliStepMethod } from "../types/cli";
-import { createCollection } from "../utils/create-collection";
-import { getPermissionsForGroups } from "../utils/get-permission-groups";
-import { getSandboxedCollectionPermissions } from "../utils/get-sandboxed-collection-permissions";
-import { getTenancyIsolationSandboxes } from "../utils/get-tenancy-isolation-sandboxes";
+import { SANDBOXED_GROUP_NAMES } from "../../constants/config";
+import { getNoTenantMessage } from "../../constants/messages";
+import type { CliStepMethod } from "../../types/cli";
+import { createCollection } from "../../utils/create-collection";
+import { getPermissionsForGroups } from "../../utils/get-permission-groups";
+import { getSandboxedCollectionPermissions } from "../../utils/get-sandboxed-collection-permissions";
+import { getTenancyIsolationSandboxes } from "../../utils/get-tenancy-isolation-sandboxes";
import {
cliError,
propagateErrorResponse,
-} from "../utils/propagate-error-response";
-import { sampleTenantIdsFromTables } from "../utils/sample-tenancy-column-values";
+} from "../../utils/propagate-error-response";
+import { sampleTenantIdsFromTables } from "../../utils/sample-tenancy-column-values";
export const setupPermissions: CliStepMethod = async (state) => {
const {
@@ -104,28 +104,13 @@ export const setupPermissions: CliStepMethod = async (state) => {
}
try {
- const groups = getSandboxedCollectionPermissions({
+ await grantAccessToModelCollection({
groupIds,
collectionIds,
+ modelCollectionId,
+ instanceUrl,
+ cookie,
});
-
- // Grant access to the "Our models" collection for all customer groups.
- // This is so they can search and select their models in the entity picker.
- for (const groupId of groupIds) {
- groups[groupId][modelCollectionId] = "write";
- }
-
- // Update the permissions for sandboxed collections
- res = await fetch(`${instanceUrl}/api/collection/graph?skip-graph=true`, {
- method: "PUT",
- headers: { "content-type": "application/json", cookie },
- body: JSON.stringify({
- groups,
- revision: 0,
- }),
- });
-
- await propagateErrorResponse(res);
} catch (error) {
const message = `Failed to update collection permissions`;
@@ -155,3 +140,51 @@ export const setupPermissions: CliStepMethod = async (state) => {
return [cliError(message, error), state];
}
};
+
+export interface GrantAccessToModelCollectionOptions {
+ groupIds: number[];
+ collectionIds: number[];
+ modelCollectionId: number;
+ instanceUrl: string;
+ cookie: string;
+}
+
+export async function grantAccessToModelCollection(
+ options: GrantAccessToModelCollectionOptions,
+): Promise<void> {
+ const { groupIds, collectionIds, modelCollectionId, instanceUrl, cookie } =
+ options;
+
+ // Fetch current collection graph to get the correct revision
+ let res = await fetch(`${instanceUrl}/api/collection/graph`, {
+ headers: { cookie },
+ });
+
+ await propagateErrorResponse(res);
+
+ const currentGraph = await res.json();
+ const currentRevision = currentGraph.revision ?? 0;
+
+ const groups = getSandboxedCollectionPermissions({
+ groupIds,
+ collectionIds,
+ });
+
+ // Grant access to the "Our models" collection for all customer groups.
+ // This is so they can search and select their models in the entity picker.
+ for (const groupId of groupIds) {
+ groups[groupId][modelCollectionId] = "write";
+ }
+
+ // Update the permissions for sandboxed collections
+ res = await fetch(`${instanceUrl}/api/collection/graph?skip-graph=true`, {
+ method: "PUT",
+ headers: { "content-type": "application/json", cookie },
+ body: JSON.stringify({
+ groups,
+ revision: currentRevision,
+ }),
+ });
+
+ await propagateErrorResponse(res);
+}
diff --git a/enterprise/frontend/src/embedding-sdk-package/cli/steps/setup-permission/setup-permission.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-package/cli/steps/setup-permission/setup-permission.unit.spec.ts
new file mode 100644
index 00000000000..c6c9cfbaa86
--- /dev/null
+++ b/enterprise/frontend/src/embedding-sdk-package/cli/steps/setup-permission/setup-permission.unit.spec.ts
@@ -0,0 +1,172 @@
+import fetchMock from "fetch-mock";
+
+import { getSandboxedCollectionPermissions } from "../../utils/get-sandboxed-collection-permissions";
+import { propagateErrorResponse } from "../../utils/propagate-error-response";
+
+import { grantAccessToModelCollection } from "./setup-permission";
+
+// Mock the utility functions
+jest.mock("../../utils/get-sandboxed-collection-permissions", () => ({
+ getSandboxedCollectionPermissions: jest.fn(),
+}));
+
+jest.mock("../../utils/propagate-error-response", () => ({
+ propagateErrorResponse: jest.fn(),
+}));
+
+describe("grantAccessToModelCollection", () => {
+ const defaultOptions = {
+ groupIds: [101, 102],
+ collectionIds: [1, 2],
+ modelCollectionId: 123,
+ instanceUrl: "http://localhost:3000",
+ cookie: "test-cookie",
+ };
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ it("should fetch current revision from collection graph and pass it to permissions update", async () => {
+ const mockCurrentGraph = {
+ revision: 42,
+ groups: {},
+ };
+
+ // Mock API calls
+ fetchMock
+ .get("http://localhost:3000/api/collection/graph", mockCurrentGraph)
+ .put("http://localhost:3000/api/collection/graph?skip-graph=true", {});
+
+ // Mock utility functions
+ (
+ propagateErrorResponse as jest.MockedFunction<
+ typeof propagateErrorResponse
+ >
+ ).mockResolvedValue(undefined);
+
+ (
+ getSandboxedCollectionPermissions as jest.MockedFunction<
+ typeof getSandboxedCollectionPermissions
+ >
+ ).mockReturnValue({
+ 101: { 1: "write" },
+ 102: { 2: "write" },
+ });
+
+ await grantAccessToModelCollection(defaultOptions);
+
+ // Verify that we fetched the current collection graph
+ const graphGetCalls = fetchMock.callHistory.calls(
+ "http://localhost:3000/api/collection/graph",
+ { method: "GET" },
+ );
+ expect(graphGetCalls).toHaveLength(1);
+
+ // Verify that we passed the current revision to the permissions update
+ const collectionUpdateCalls = fetchMock.callHistory.calls(
+ "http://localhost:3000/api/collection/graph?skip-graph=true",
+ { method: "PUT" },
+ );
+ expect(collectionUpdateCalls).toHaveLength(1);
+
+ const requestBody = JSON.parse(
+ collectionUpdateCalls[0].options?.body as string,
+ );
+ expect(requestBody.revision).toBe(42); // This is the key assertion
+ expect(requestBody.groups[101][123]).toBe("write"); // modelCollectionId access
+ expect(requestBody.groups[102][123]).toBe("write"); // modelCollectionId access
+ });
+
+ it("should handle missing revision in current graph by defaulting to 0", async () => {
+ const mockCurrentGraphWithoutRevision = {
+ groups: {},
+ // No revision property
+ };
+
+ // Mock API calls
+ fetchMock
+ .get(
+ "http://localhost:3000/api/collection/graph",
+ mockCurrentGraphWithoutRevision,
+ )
+ .put("http://localhost:3000/api/collection/graph?skip-graph=true", {});
+
+ // Mock utility functions
+ (
+ propagateErrorResponse as jest.MockedFunction<
+ typeof propagateErrorResponse
+ >
+ ).mockResolvedValue(undefined);
+
+ (
+ getSandboxedCollectionPermissions as jest.MockedFunction<
+ typeof getSandboxedCollectionPermissions
+ >
+ ).mockReturnValue({
+ 101: { 1: "write" },
+ 102: { 2: "write" },
+ });
+
+ await grantAccessToModelCollection(defaultOptions);
+
+ // Verify that we passed revision 0 when it's missing from the graph
+ const collectionUpdateCalls = fetchMock.callHistory.calls(
+ "http://localhost:3000/api/collection/graph?skip-graph=true",
+ { method: "PUT" },
+ );
+ expect(collectionUpdateCalls).toHaveLength(1);
+
+ const requestBody = JSON.parse(
+ collectionUpdateCalls[0].options?.body as string,
+ );
+ expect(requestBody.revision).toBe(0); // Should default to 0 when revision is missing
+ });
+
+ it("should add modelCollectionId with write permission to all group permissions", async () => {
+ const mockCurrentGraph = {
+ revision: 10,
+ groups: {},
+ };
+
+ fetchMock
+ .get("http://localhost:3000/api/collection/graph", mockCurrentGraph)
+ .put("http://localhost:3000/api/collection/graph?skip-graph=true", {});
+
+ (
+ propagateErrorResponse as jest.MockedFunction<
+ typeof propagateErrorResponse
+ >
+ ).mockResolvedValue(undefined);
+
+ // Mock initial permissions without modelCollectionId access
+ (
+ getSandboxedCollectionPermissions as jest.MockedFunction<
+ typeof getSandboxedCollectionPermissions
+ >
+ ).mockReturnValue({
+ 101: { 1: "write", 2: "none" },
+ 102: { 1: "none", 2: "write" },
+ });
+
+ await grantAccessToModelCollection(defaultOptions);
+
+ const collectionUpdateCalls = fetchMock.callHistory.calls(
+ "http://localhost:3000/api/collection/graph?skip-graph=true",
+ { method: "PUT" },
+ );
+ const requestBody = JSON.parse(
+ collectionUpdateCalls[0].options?.body as string,
+ );
+
+ // Verify that modelCollectionId (123) was added with "write" permission for all groups
+ expect(requestBody.groups[101][123]).toBe("write");
+ expect(requestBody.groups[102][123]).toBe("write");
+
+ // Verify that existing permissions were preserved
+ expect(requestBody.groups[101][1]).toBe("write");
+ expect(requestBody.groups[101][2]).toBe("none");
+ expect(requestBody.groups[102][1]).toBe("none");
+ expect(requestBody.groups[102][2]).toBe("write");
+ });
+});