The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Create a data app's collection in the API and use it in E2E tests (#85546)
Alexander Polyankinca2e28641d2a
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 941075ec692..05efd1330df 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -3845,7 +3845,6 @@permissionspremium-featuresremote-sync- requestsearchsettingsutildiff --git a/e2e/support/assets/data-apps/.gitignore b/e2e/support/assets/data-apps/.gitignoreindex 875a192f532..69917537918 100644--- a/e2e/support/assets/data-apps/.gitignore+++ b/e2e/support/assets/data-apps/.gitignore@@ -4,3 +4,5 @@/*/*!/*/src!/*/data_app.yaml+# The app's typed schema, fetched from Metabase before each build.+/*/src/metabase.tsdiff --git a/e2e/support/assets/data-apps/isolation/src/utils.ts b/e2e/support/assets/data-apps/isolation/src/utils.tsindex 772af03cd99..70d24c2f28a 100644--- a/e2e/support/assets/data-apps/isolation/src/utils.ts+++ b/e2e/support/assets/data-apps/isolation/src/utils.ts@@ -1,16 +1,8 @@import type { IsolationTestEnv, Report } from "./types";-export const getEnv = (): IsolationTestEnv => {- const env = (- globalThis as { __METABASE_DATA_APP_TEST_ENV__?: IsolationTestEnv }- ).__METABASE_DATA_APP_TEST_ENV__;-- if (!env) {- throw new Error("isolation fixture: missing test env");- }-- return env;-};+export const getEnv = (): IsolationTestEnv => ({+ instanceUrl: window.location.origin,+});export const describeError = (err: unknown): string =>(err as { message?: string })?.message ?? String(err);diff --git a/e2e/support/assets/data-apps/kitchen-sink/src/App.tsx b/e2e/support/assets/data-apps/kitchen-sink/src/App.tsxindex 701913c043c..65835fb4b61 100644--- a/e2e/support/assets/data-apps/kitchen-sink/src/App.tsx+++ b/e2e/support/assets/data-apps/kitchen-sink/src/App.tsx@@ -6,7 +6,6 @@ import {copy,DataAppLink,DataAppRouter,- defineAction,defineQuery,useAction,useDataAppLocation,@@ -15,7 +14,7 @@ import {} from "@metabase/embedding-sdk-react/data-app";import { type ComponentType, useEffect, useState } from "react";-import { getTestEnv } from "./test-env";+import { actions, queries } from "./metabase";// The sandbox throws host-realm errors, whose `instanceof Error` can be false in// the guest; read `.message` defensively.@@ -24,11 +23,17 @@ const describeError = (err: unknown): string =>const PROBE_COLOR = "rgb(0, 128, 0)";+const MISSING_QUERY = defineQuery({+ source: { type: "card", entityId: "missingQuestionEntity" },+});++const SANDBOX_ALLOWED_URL = "https://allowed.data-app.test/ping";+const SANDBOX_BLOCKED_URL = "https://blocked.data-app.test/ping";+function Overview() {- const { scalarQuery, questionQuery } = getTestEnv();- const ordersCount = useMetabaseQuery(defineQuery(scalarQuery));+ const ordersCount = useMetabaseQuery(queries.ordersCount);const totalOrders = ordersCount.data?.rawRows?.[0]?.[0];- const ordersQuery = useMetabaseQueryObject(defineQuery(questionQuery));+ const ordersQuery = useMetabaseQueryObject(queries.orders);return (<div data-testid="data-app-content" style={{ padding: 24 }}>@@ -86,9 +91,8 @@ function Details() {}function QueryStates() {- const { scalarQuery, errorQuery } = getTestEnv();- const broken = useMetabaseQuery(defineQuery(errorQuery!));- const working = useMetabaseQuery(defineQuery(scalarQuery));+ const broken = useMetabaseQuery(MISSING_QUERY);+ const working = useMetabaseQuery(queries.ordersCount);const count = working.data?.rawRows?.[0]?.[0];return (@@ -115,17 +119,14 @@ function QueryStates() {}function DownloadQuestionPage() {- const { downloadQuestionId } = getTestEnv();-return (<div data-testid="data-app-download-question" style={{ padding: 24 }}><h1>Download question</h1><div style={{ height: 360 }}>- {downloadQuestionId != null ? (- <StaticQuestion questionId={downloadQuestionId} withDownloads />- ) : (- <div data-testid="download-question-loading">…</div>- )}+ <StaticQuestion+ questionId={queries.ordersByYear.source.entityId}+ withDownloads+ /></div></div>);@@ -134,8 +135,7 @@ function DownloadQuestionPage() {// An InteractiveQuestion on a single-column (count) query, so a spec can switch it// to a custom viz — the example `demo-viz` requires exactly one result column.function CustomVizPage() {- const { scalarQuery } = getTestEnv();- const q = useMetabaseQueryObject(defineQuery(scalarQuery));+ const q = useMetabaseQueryObject(queries.ordersCount);return (<div data-testid="data-app-custom-viz" style={{ padding: 24 }}>@@ -152,8 +152,7 @@ function CustomVizPage() {}function StaticQuestionPage() {- const { questionQuery } = getTestEnv();- const q = useMetabaseQueryObject(defineQuery(questionQuery));+ const q = useMetabaseQueryObject(queries.orders);return (<div data-testid="data-app-static-question" style={{ padding: 24 }}>@@ -169,46 +168,15 @@ function StaticQuestionPage() {);}-// Data apps may not run native SQL: the backend rejects native queries from the-// `data-app` client. The `card` prop takes a raw query with no native/structured-// check, so this page hand-builds a native query to prove the backend refuses it.-function NativeQueryPage() {- const { nativeQuery } = getTestEnv();+type AddTeamParams = { team_name: string; score: number };- if (!nativeQuery) {- return <div data-testid="data-app-native-query">no-native-query-env</div>;- }-- return (- <div data-testid="data-app-native-query" style={{ padding: 24 }}>- <h1>Native query</h1>- <div style={{ height: 360 }}>- <StaticQuestion- card={{- query: {- type: "native",- database: nativeQuery.database,- native: { query: nativeQuery.query },- },- }}- />- </div>- </div>- );-}--function Actions() {- const { actionEntityId, actionParams } = getTestEnv();- const action = useAction(- actionEntityId- ? defineAction({ action: { id: actionEntityId, parameters: [] } })- : null,- );+function Actions({ params }: { params: AddTeamParams }) {+ const action = useAction(actions.addTeam);const [output, setOutput] = useState("idle");const onExecute = async () => {try {- const res = await action.execute(actionParams ?? {});+ const res = await action.execute(params);setOutput(res ? "returned-result" : "returned-null");} catch {setOutput("threw");@@ -242,6 +210,14 @@ function Actions() {);}+function NewTeamActions() {+ return <Actions params={{ team_name: "Data App FC", score: 7 }} />;+}++function ExistingTeamActions() {+ return <Actions params={{ team_name: "Amorous Aardvarks", score: 1 }} />;+}+function Clipboard() {const [status, setStatus] = useState("idle");@@ -303,7 +279,6 @@ const BLOCKED_PROBES: Probe[] = [];function Sandbox() {- const { sandbox } = getTestEnv();const [probeResults, setProbeResults] = useState<Record<string, string>>({});const [innerHtml, setInnerHtml] = useState("pending");const [blockedFetch, setBlockedFetch] = useState("pending");@@ -332,38 +307,32 @@ function Sandbox() {setInnerHtml(`threw: ${describeError(err)}`);}- if (sandbox) {- fetch(sandbox.blockedUrl)- .then((res) => setBlockedFetch(`ok: ${res.status}`))- .catch((err) => setBlockedFetch(`blocked: ${describeError(err)}`));-- fetch(sandbox.allowedUrl)- .then((res) => setAllowedFetch(`ok: ${res.status}`))- .catch((err) => setAllowedFetch(`blocked: ${describeError(err)}`));-- if (sandbox.xhrBlockedUrl) {- try {- const xhr = new XMLHttpRequest();- xhr.open("GET", sandbox.xhrBlockedUrl);- setBlockedXhr("not blocked");- } catch (err) {- setBlockedXhr(`blocked: ${describeError(err)}`);- }- }+ fetch(SANDBOX_BLOCKED_URL)+ .then((res) => setBlockedFetch(`ok: ${res.status}`))+ .catch((err) => setBlockedFetch(`blocked: ${describeError(err)}`));- if (sandbox.xhrAllowedUrl) {- const xhr = new XMLHttpRequest();- xhr.addEventListener("load", () => setAllowedXhr(`ok: ${xhr.status}`));- xhr.addEventListener("error", () => setAllowedXhr("blocked: error"));- try {- xhr.open("GET", sandbox.xhrAllowedUrl);- xhr.send();- } catch (err) {- setAllowedXhr(`blocked: ${describeError(err)}`);- }- }+ fetch(SANDBOX_ALLOWED_URL)+ .then((res) => setAllowedFetch(`ok: ${res.status}`))+ .catch((err) => setAllowedFetch(`blocked: ${describeError(err)}`));++ try {+ const xhr = new XMLHttpRequest();+ xhr.open("GET", SANDBOX_BLOCKED_URL);+ setBlockedXhr("not blocked");+ } catch (err) {+ setBlockedXhr(`blocked: ${describeError(err)}`);+ }++ const xhr = new XMLHttpRequest();+ xhr.addEventListener("load", () => setAllowedXhr(`ok: ${xhr.status}`));+ xhr.addEventListener("error", () => setAllowedXhr("blocked: error"));+ try {+ xhr.open("GET", SANDBOX_ALLOWED_URL);+ xhr.send();+ } catch (err) {+ setAllowedXhr(`blocked: ${describeError(err)}`);}- }, [sandbox]);+ }, []);return (<div data-testid="data-app-sandbox" style={{ padding: 24 }}>@@ -425,10 +394,10 @@ const ROUTES: Record<string, ComponentType> = {"/isolation": Isolation,"/query-states": QueryStates,"/static-question": StaticQuestionPage,- "/native-query": NativeQueryPage,"/download-question": DownloadQuestionPage,"/custom-viz": CustomVizPage,- "/actions": Actions,+ "/actions": NewTeamActions,+ "/actions-existing-team": ExistingTeamActions,"/clipboard": Clipboard,"/missing-question": MissingQuestion,"/throw": ThrowingPage,diff --git a/e2e/support/assets/data-apps/kitchen-sink/src/index.tsx b/e2e/support/assets/data-apps/kitchen-sink/src/index.tsxindex 673040288e4..e59be79c863 100644--- a/e2e/support/assets/data-apps/kitchen-sink/src/index.tsx+++ b/e2e/support/assets/data-apps/kitchen-sink/src/index.tsx@@ -1,12 +1,12 @@import type { DataAppFactory } from "@metabase/embedding-sdk-react/data-app";import App from "./App";-import { getTestEnv } from "./test-env";const factory: DataAppFactory = () => ({component: App,providerProps: {- allowedCustomVisualizations: getTestEnv().allowedCustomVisualizations,+ // The SDK loads no custom viz outside this allowlist; data-apps/custom-viz.cy.spec.ts uses demo-viz.+ allowedCustomVisualizations: ["custom:demo-viz"],},});diff --git a/e2e/support/assets/data-apps/kitchen-sink/src/test-env.ts b/e2e/support/assets/data-apps/kitchen-sink/src/test-env.tsdeleted file mode 100644index c0004477b30..00000000000--- a/e2e/support/assets/data-apps/kitchen-sink/src/test-env.ts+++ /dev/null@@ -1,20 +0,0 @@-// The type is the tests' contract and lives with them; this file is only how the-// app reads it back inside the sandbox realm.-import type { DataAppTestEnv } from "../../../../helpers/data-app-test-env";--declare global {- // eslint-disable-next-line no-var -- `var` (not `const`) so `globalThis.x` typechecks- var __METABASE_DATA_APP_TEST_ENV__: DataAppTestEnv | undefined;-}--export function getTestEnv(): DataAppTestEnv {- const env = globalThis.__METABASE_DATA_APP_TEST_ENV__;-- if (!env) {- throw new Error(- "data-app test env was not injected — pass `testEnv` to H.mockDataApp",- );- }-- return env;-}diff --git a/e2e/support/assets/data-apps/synced-actions-app/src/App.tsx b/e2e/support/assets/data-apps/synced-actions-app/src/App.tsxindex 747e0b041d3..af2904f0438 100644--- a/e2e/support/assets/data-apps/synced-actions-app/src/App.tsx+++ b/e2e/support/assets/data-apps/synced-actions-app/src/App.tsx@@ -1,15 +1,13 @@import { useAction } from "@metabase/embedding-sdk-react/data-app";import { useState } from "react";-import { CreateScore } from "../actions/orders.action";+import { actions } from "./metabase";/**- * Passes a typed-schema definition to `useAction`, which executes the action in- * the app's collection by its entity ID. The spec that drives this app writes- * `actions/orders.action.ts` after creating the action.+ * Executes the action that the app's typed schema defines as `addTeam`.*/export default function App() {- const action = useAction(CreateScore);+ const action = useAction(actions.addTeam);const [output, setOutput] = useState("idle");const onExecute = async () => {diff --git a/e2e/support/assets/data-apps/synced-app/src/App.tsx b/e2e/support/assets/data-apps/synced-app/src/App.tsxindex 12b8ef6d458..09c5a7eb94e 100644--- a/e2e/support/assets/data-apps/synced-app/src/App.tsx+++ b/e2e/support/assets/data-apps/synced-app/src/App.tsx@@ -1,14 +1,12 @@import { useMetabaseQuery } from "@metabase/embedding-sdk-react/data-app";-import { OrdersCount } from "../queries/orders.query";+import { queries } from "./metabase";/**- * Runs the saved question its typed-schema definition names. The spec that- * drives this app writes `queries/orders.query.ts` and the app's collection files- * before publishing it.+ * Runs the saved question that the app's typed schema defines as `ordersCount`.*/export default function App() {- const orders = useMetabaseQuery(OrdersCount);+ const orders = useMetabaseQuery(queries.ordersCount);const rows = orders.data?.rawRows;const total = rows?.[0]?.[1];diff --git a/e2e/support/helpers/api/createAction.ts b/e2e/support/helpers/api/createAction.tsnew file mode 100644index 00000000000..255be1c880b--- /dev/null+++ b/e2e/support/helpers/api/createAction.ts@@ -0,0 +1,49 @@+import type {+ ActionFormSettings,+ CardId,+ CollectionId,+ ImplicitActionKind,+ NativeDatasetQuery,+ OpaqueDatasetQuery,+ ParameterId,+ ParameterTarget,+ TableId,+ WritebackAction,+ WritebackParameter,+} from "metabase-types/api";++type BaseCreateActionOpts = {+ name?: string;+ description?: string | null;+ collection_id?: CollectionId | null;+ model_id?: CardId | null;+ parameters?: WritebackParameter[];+ parameter_mappings?: Record<ParameterId, ParameterTarget>;+ visualization_settings?: ActionFormSettings;+};++type CreateQueryActionOpts = BaseCreateActionOpts & {+ type: "query";+ dataset_query: NativeDatasetQuery | OpaqueDatasetQuery;+};++type CreateImplicitActionOpts = BaseCreateActionOpts & {+ type: "implicit";+ kind: ImplicitActionKind;+ table_id?: TableId;+};++export type CreateActionOpts = CreateQueryActionOpts | CreateImplicitActionOpts;++const DEFAULT_ACTION_NAME = "Action";++export function createAction(+ opts: CreateActionOpts,+): Cypress.Chainable<WritebackAction> {+ return cy+ .request<WritebackAction>("POST", "/api/action", {+ name: DEFAULT_ACTION_NAME,+ ...opts,+ })+ .then(({ body }) => body);+}diff --git a/e2e/support/helpers/api/createLibrary.ts b/e2e/support/helpers/api/createLibrary.tsindex c571630a2c5..773869f5b1d 100644--- a/e2e/support/helpers/api/createLibrary.ts+++ b/e2e/support/helpers/api/createLibrary.ts@@ -1,41 +1,42 @@-import type { Collection } from "metabase-types/api";+import type {+ Collection,+ CollectionId,+ CollectionType,+ ListCollectionItemsResponse,+} from "metabase-types/api";-import { retryRequest } from "../e2e-request-helpers";--type LibraryResponse = Collection & {- effective_children?: Collection[];-};--const hasLibraryRootCollections = (collections: Collection[]) => {- const libraryCollection = collections.find(({ type }) => type === "library");-- return Boolean(- libraryCollection?.children?.some(({ type }) => type === "library-data") &&- libraryCollection?.children?.some(({ type }) => type === "library-metrics"),- );+export type LibraryCollectionIds = {+ libraryCollectionId: CollectionId;+ dataCollectionId: CollectionId;+ metricsCollectionId: CollectionId;+ dashboardsCollectionId: CollectionId;};-export const createLibrary = (): Cypress.Chainable<- Cypress.Response<LibraryResponse>-> => {- cy.log("Initialize library");-+export function createLibrary(): Cypress.Chainable<LibraryCollectionIds> {return cy- .request("POST", "/api/ee/library")- .then(() => cy.request<LibraryResponse>("GET", "/api/ee/library"))- .then((response) => {- return retryRequest(- () =>- cy.request<Collection[]>({- method: "GET",- url: "/api/collection/tree",- qs: {- "exclude-other-user-collections": true,- "exclude-archived": true,- "include-library": true,- },- }),- ({ body }) => hasLibraryRootCollections(body),- ).then(() => response);- });-};+ .request<Collection>("POST", "/api/ee/library")+ .then(({ body: library }) =>+ cy+ .request<ListCollectionItemsResponse>({+ method: "GET",+ url: `/api/collection/${library.id}/items`,+ qs: { models: "collection" },+ })+ .then(({ body: { data: children } }) => {+ const childId = (type: CollectionType): CollectionId => {+ const child = children.find((item) => item.type === type);+ if (child == null) {+ throw new Error(`The Library has no ${type} collection`);+ }+ return child.id;+ };++ return {+ libraryCollectionId: library.id,+ dataCollectionId: childId("library-data"),+ metricsCollectionId: childId("library-metrics"),+ dashboardsCollectionId: childId("library-dashboards"),+ };+ }),+ );+}diff --git a/e2e/support/helpers/api/index.ts b/e2e/support/helpers/api/index.tsindex c6e25c84b46..94255043664 100644--- a/e2e/support/helpers/api/index.ts+++ b/e2e/support/helpers/api/index.ts@@ -4,6 +4,7 @@ export { archiveCollection } from "./archiveCollection";export { archiveDashboard } from "./archiveDashboard";export { archiveDocument } from "./archiveDocument";export { archiveQuestion } from "./archiveQuestion";+export { createAction } from "./createAction";export { createApiKey } from "./createApiKey";export { createCard } from "./createCard";export { createCollection } from "./createCollection";@@ -14,6 +15,7 @@ export { createDashboardWithQuestions } from "./createDashboardWithQuestions";export { createDashboardWithTabs } from "./createDashboardWithTabs";export { createDocument } from "./createDocument";export { createLibrary } from "./createLibrary";+export type { LibraryCollectionIds } from "./createLibrary";export { createModerationReview } from "./createModerationReview";export { createNativeQuestion } from "./createNativeQuestion";export type { NativeQuestionDetails } from "./createNativeQuestion";diff --git a/e2e/support/helpers/data-app-test-env.ts b/e2e/support/helpers/data-app-test-env.tsdeleted file mode 100644index 3b529682411..00000000000--- a/e2e/support/helpers/data-app-test-env.ts+++ /dev/null@@ -1,60 +0,0 @@-/**- * The config a spec injects into a data-app fixture (via `H.mockDataApp`'s- * `testEnv`), so a fixture doesn't hard-code values that track the Cypress- * snapshot — sample-DB ids, URLs, the id of an action the spec created.- *- * It lives with the tests rather than inside a fixture because it's the contract- * between the two, and the fixtures are standalone SDK apps outside this TS- * program (see `exclude` in `e2e/tsconfig.json`). A fixture reads it back at- * runtime through its own `getTestEnv()`.- */-export type DataAppTestEnv = {- /** Every query the fixture renders is a saved question, named by entity ID. */- scalarQuery: { source: CardSource };- questionQuery: { source: CardSource };- /**- * `/download-question` page: the id of a saved CHART question (so a `.png`- * export is offered). Passed in because the id tracks the Cypress snapshot.- */- downloadQuestionId?: number;- /**- * `custom:`-prefixed plugin identifiers the app allows the SDK to load (e.g.- * `["custom:demo-viz"]`). The fixture forwards this to the SDK provider's- * `allowedCustomVisualizations`, which hard-gates custom-viz loading.- */- allowedCustomVisualizations?: `custom:${string}`[];- /**- * `/sandboxing` page: URLs it fetches to probe the sandbox's `allowed_hosts`- * gate. `allowedUrl` is expected to be in `allowed_hosts`, `blockedUrl` is not.- * `xhr*Url` drive the same gate through `XMLHttpRequest`.- */- sandbox?: {- allowedUrl: string;- blockedUrl: string;- xhrAllowedUrl?: string;- xhrBlockedUrl?: string;- };- /**- * `/query-states` page: a deliberately invalid query (a card that does not- * exist), so the hook resolves to an `error` state; the page also exercises- * `refetch`.- */- errorQuery?: { source: CardSource };- /**- * `/actions` page: the entity ID of the action the spec creates and- * `useAction` executes, so it can't be hard-coded in the app. Left out to- * exercise the "no action" path, where the hook must not request anything.- */- actionEntityId?: string;- /** `/actions` page: the parameters the page passes to `execute()`. */- actionParams?: Record<string, string | number>;- /**- * `/native-query` page: a hand-built native query the app renders through- * `<StaticQuestion card={{ query }}>`. Data apps aren't allowed to run native- * SQL (the backend rejects it for the `data-app` client), so the page exists to- * assert that rejection.- */- nativeQuery?: { database: number; query: string };-};--type CardSource = { type: "card"; entityId: string };diff --git a/e2e/support/helpers/e2e-action-helpers.js b/e2e/support/helpers/e2e-action-helpers.jsindex 3123d36b92e..4a01565b595 100644--- a/e2e/support/helpers/e2e-action-helpers.js+++ b/e2e/support/helpers/e2e-action-helpers.js@@ -1,5 +1,6 @@import { capitalize } from "inflection";+import { createAction } from "./api";import {commandPalette,commandPaletteButton,@@ -18,14 +19,6 @@ export function setActionsEnabledForDB(dbId, enabled = true) {export function fillActionQuery(query) {NativeEditor.type(query);}-/**- *- * @param {import("metabase/actions/types").CreateQueryActionParams} actionDetails- */-export function createAction(actionDetails) {- return cy.request("POST", "/api/action", actionDetails);-}-/*** create a single implicit action of the given kind for the given model*diff --git a/e2e/support/helpers/e2e-data-app-helpers.ts b/e2e/support/helpers/e2e-data-app-helpers.tsindex 1c6e9fb1827..61add151e13 100644--- a/e2e/support/helpers/e2e-data-app-helpers.ts+++ b/e2e/support/helpers/e2e-data-app-helpers.ts@@ -1,20 +1,17 @@import yaml from "js-yaml";-import { USERS, USER_GROUPS, WRITABLE_DB_ID } from "e2e/support/cypress_data";+import { USERS, USER_GROUPS } from "e2e/support/cypress_data";import * as Urls from "metabase/urls/data-apps";import type {- Collection,- CollectionId,- CollectionPermission,- CollectionPermissionsGraph,+ CreateDataAppRequest,DataApp,- Group,+ DataAppGroup,+ GroupId,GroupInfo,- WritebackAction,+ TableId,} from "metabase-types/api";-import { createTestNativeQuery, updateSetting } from "./api";-import type { DataAppTestEnv } from "./data-app-test-env";+import { createLibrary, publishTables, updateSetting } from "./api";import { getIframeBody } from "./e2e-embedding-helpers";import {LOCAL_GIT_PATH,@@ -24,6 +21,8 @@ import {setupGitSync,} from "./e2e-remote-sync-helpers";+const DATA_APP_FIXTURES_PATH = "e2e/support/assets/data-apps";+export const DATA_APP_NAME = "kitchen-sink";export const DATA_APP_DISPLAY_NAME = "Kitchen Sink";@@ -32,113 +31,50 @@ export const visitDataAppRoute = (route: string) => {return cy.visit(`/apps/${DATA_APP_NAME}/${route}`);};-export const fakeDataApp = (overrides: Partial<DataApp> = {}): DataApp => ({- id: 1,- entity_id: "e2eFakeDataAppEntityI",- name: DATA_APP_NAME,- display_name: DATA_APP_DISPLAY_NAME,- description: null,- version: 1,- outdated: false,- bundle_path: "dist/index.js",- enabled: true,- resource_collection_id: 1,- allowed_hosts: [],- bundle_hash: "e2e-bundle-hash",- created_at: "2024-01-01T00:00:00Z",- updated_at: "2024-01-01T00:00:00Z",- ...overrides,-});--type MockDataAppOptions<TestEnv> = {- /** Display name (iframe title + admin list); defaults to the fixture dir name. */- displayName?: string;- /** `allowed_hosts` served in the bundle response header. */- allowedHosts?: string[];- /**- * Config a fixture reads at runtime, rather than hard-coding ids that track the- * Cypress snapshot.- */- testEnv?: TestEnv;- /** Delays the bundle response, so a loading assertion has a window to catch. */- bundleDelayMs?: number;-};--export const mockDataApp = <TestEnv = DataAppTestEnv>(- appName: string,- options: MockDataAppOptions<TestEnv> = {},-) => {- const slug = appName;- const displayName = options.displayName ?? appName;- const allowedHosts = options.allowedHosts ?? [];-- // the entrypoint checks for app access before serving its HTML- // we need to have a real app in the database - mocking the endpoint isn't enough- cy.request({- url: `/api/ee/apps/${slug}`,- failOnStatusCode: false,- }).then(({ status }) => {- if (status === 200) {- return;- }-- expect(status).to.equal(404);-- cy.log("register the data app");- cy.request("POST", "/api/ee/apps", {- name: slug,- display_name: displayName,- bundle_path: "dist/index.js",- allowed_hosts: allowedHosts,- enabled: true,- });-- cy.log("assign the data app to all users group");- cy.request("POST", `/api/ee/apps/${slug}/groups`, {- group_ids: [USER_GROUPS.ALL_USERS_GROUP],- });- });+export function createDataApp(+ details: CreateDataAppRequest,+): Cypress.Chainable<DataApp> {+ return cy+ .request<DataApp>("POST", "/api/ee/apps", details)+ .then(({ body }) => body);+}- // Prelude runs in the sandbox realm before the bundle's factory, so the app- // can read the injected config as a global (see MockDataAppOptions.testEnv).- const prelude =- options.testEnv !== undefined- ? `globalThis.__METABASE_DATA_APP_TEST_ENV__ = ${JSON.stringify(options.testEnv)};\n`- : "";-- return cy.task<string>("buildDataApp", { appName }).then((bundleCode) => {- const app = fakeDataApp({- name: slug,- display_name: displayName,- allowed_hosts: allowedHosts,- });+export function addDataAppGroups(+ app: Pick<DataApp, "name">,+ groupIds: GroupId[],+): Cypress.Chainable<DataAppGroup[]> {+ return cy+ .request<DataAppGroup[]>("POST", `/api/ee/apps/${app.name}/groups`, {+ group_ids: groupIds,+ })+ .then(({ body }) => body);+}- cy.intercept("GET", "/api/ee/apps/repo-status", {- configured: true,- });- cy.intercept("GET", "/api/ee/apps", [app]);- cy.intercept({ method: "GET", pathname: `/api/ee/apps/${slug}` }, app);- cy.intercept(- { method: "GET", pathname: `/api/ee/apps/${slug}/bundle` },- {- statusCode: 200,- headers: {- // Match the REAL bundle endpoint (`data_apps/api.clj`), which serves- // `application/javascript` + `nosniff`. The runtime fetch-and-evals the- // bundle, so the type is irrelevant there — but a stricter mock (e.g.- // `text/plain`) would diverge from production, so keep it aligned.- "content-type": "application/javascript",- "X-Content-Type-Options": "nosniff",- "X-Metabase-Data-App-Allowed-Hosts": JSON.stringify(allowedHosts),+/**+ * Writes `app`'s typed schema to `src/metabase.ts` of the fixture directory named+ * `app.name`, builds that fixture, and uploads the build as the app's bundle.+ */+export function uploadDataAppBundle(+ app: Pick<DataApp, "name">,+): Cypress.Chainable<DataApp> {+ return cy+ .request<string>(`/api/ee/apps/${app.name}/typed-schema`)+ .then(({ body: typedSchema }) =>+ cy.task("writeDataAppFiles", {+ files: {+ [`${DATA_APP_FIXTURES_PATH}/${app.name}/src/metabase.ts`]:+ typedSchema,},- body: prelude + bundleCode,- ...(options.bundleDelayMs ? { delay: options.bundleDelayMs } : {}),- },- );-- return cy.wrap({ slug, displayName }, { log: false });- });-};+ }),+ )+ .then(() => cy.task<string>("buildDataApp", { appName: app.name }))+ .then((bundle) =>+ cy.request<DataApp>("PUT", `/api/ee/apps/${app.name}/bundle`, {+ bundle,+ }),+ )+ .then(({ body }) => body);+}/*** The two rejections endpoint scope enforcement can emit. `scope_not_permitted` comes from@@ -212,113 +148,6 @@ export function dataAppIframe(displayName: string) {return getIframeBody(`iframe[title="${displayName}"]`);}-/**- * Sets every non-admin group's access to a collection through the permission graph of- * the collection's namespace: an app's own collection lives in `data-apps`, whose graph- * is separate from the default one. A namespace graph lists only the groups holding a- * grant in it, so the groups come from the groups API, not from the graph.- */-export function setDataAppCollectionAccess(- collectionId: CollectionId,- access: CollectionPermission,-) {- return cy- .request<Collection>("GET", `/api/collection/${collectionId}`)- .then(({ body: collection }) => {- const namespace = collection.namespace ?? undefined;- const graphUrl =- namespace === undefined- ? "/api/collection/graph"- : `/api/collection/graph?namespace=${namespace}`;-- cy.request<Group[]>("GET", "/api/permissions/group").then(- ({ body: allGroups }) => {- cy.request<CollectionPermissionsGraph>("GET", graphUrl).then(- ({ body: graph }) => {- const groups = Object.fromEntries(- allGroups- .filter((group) => group.id !== USER_GROUPS.ADMIN_GROUP)- .map((group) => [- group.id,- { ...graph.groups[group.id], [collectionId]: access },- ]),- );-- cy.request("PUT", "/api/collection/graph", {- ...graph,- ...(namespace === undefined ? {} : { namespace }),- groups,- });- },- );- },- );- });-}--/** Creates a data actions folder the non-admin groups hold `access` to. */-export function createDataAppCollection({- name,- access,-}: {- name: string;- access: CollectionPermission;-}) {- return cy- .request<Collection>("POST", "/api/collection", {- name,- namespace: "data-actions",- })- .then(({ body: collection }) => {- setDataAppCollectionAccess(collection.id, access);- return cy.wrap(collection, { log: false });- });-}--/** Creates a query action without a model that inserts a team into `scoreboard_actions`. */-export function createDataAppScoreboardAction({- name = "Add team",- collectionId = null,-}: { name?: string; collectionId?: CollectionId | null } = {}) {- return createTestNativeQuery({- database: WRITABLE_DB_ID,- query:- "INSERT INTO scoreboard_actions (team_name, score) VALUES ({{team_name}}, {{score}})",- templateTags: {- team_name: { type: "text", "display-name": "Team name", required: true },- score: { type: "number", "display-name": "Score", required: true },- },- }).then((datasetQuery) =>- cy- .request<WritebackAction>("POST", "/api/action", {- name,- type: "query",- database_id: WRITABLE_DB_ID,- collection_id: collectionId,- dataset_query: datasetQuery,- parameters: [- {- id: "team_name",- slug: "team_name",- name: "Team name",- type: "string/=",- target: ["variable", ["template-tag", "team_name"]],- required: true,- },- {- id: "score",- slug: "score",- name: "Score",- type: "number/=",- target: ["variable", ["template-tag", "score"]],- required: true,- },- ],- })- .then(({ body: action }) => cy.wrap(action, { log: false })),- );-}-/*** A table as serialized YAML references it: database, schema, and table name.* The schema is null for a database without schemas.@@ -440,24 +269,6 @@ export function writeDataAppResources(});}-/** Declares one `defineAction` per action, as the app's typed schema does, by the action's entity ID. */-export function declareDataAppActions(- appRoot: string,- actions: Array<{ exportName: string; actionEntityId: string }>,-) {- return cy.task("writeDataAppFiles", {- files: {- [`${appRoot}/actions/orders.action.ts`]: [- 'import { defineAction } from "@metabase/embedding-sdk-react/data-app";',- ...actions.map(- ({ exportName, actionEntityId }) =>- `export const ${exportName} = defineAction({ action: { id: "${actionEntityId}", parameters: [] } });`,- ),- ].join("\n"),- },- });-}-export const copySyncedDataAppsFixture = () =>cy.task("copyDirectory", {source: `${Cypress.config("projectRoot")}/e2e/support/assets/example_synced_data_apps`,@@ -501,9 +312,10 @@ const isSyncedDataApp = (app: DataApp): app is SyncedDataApp =>/*** Writes the app's manifest into the sync repository as `data_apps/<slug>`, makes* the repository's `collections/data_apps/` what the host app holds, and- * commits them, as an author does. The bundle is a placeholder, since specs- * serve the built one through `mockDataApp`. Pass `initializeRepo: false` to- * write into the repository an earlier call set up.+ * commits them, as an author does. The bundle is a placeholder that every pull+ * loads again, so a spec uploads the built one with `uploadDataAppBundle` after+ * its last pull. Pass `initializeRepo: false` to write into the repository an+ * earlier call set up.*/function commitDataApp(appRoot: string,@@ -528,7 +340,7 @@ function commitDataApp(cy.task("writeDataAppFiles", {files: {[`${appDir}/data_app.yaml`]: manifest,- [`${appDir}/dist/index.js`]: "// served by the spec",+ [`${appDir}/dist/index.js`]: "// uploaded by the spec",},}),);@@ -563,19 +375,17 @@ export function publishDataApp(});}-/** Create a group and assign it to a data app by slug. */-export function assignTestGroupToDataApp(slug: string) {- cy.request<GroupInfo>("POST", "/api/permissions/group", {- name: `Data app test group: ${slug}`,- })- .its("body")- .as("dataAppTestGroup");-- cy.get<GroupInfo>("@dataAppTestGroup").then(({ id }) =>- cy.request("POST", `/api/ee/apps/${slug}/groups`, { group_ids: [id] }),- );-- return cy.get<GroupInfo>("@dataAppTestGroup").its("id");+/** Creates a group and adds it to `app`'s groups, yielding the group's ID. */+export function assignTestGroupToDataApp(+ app: Pick<DataApp, "name">,+): Cypress.Chainable<GroupId> {+ return cy+ .request<GroupInfo>("POST", "/api/permissions/group", {+ name: `Data app test group: ${app.name}`,+ })+ .then(({ body: group }) =>+ addDataAppGroups(app, [group.id]).then(() => group.id),+ );}const DATA_APP_DEV_HOST_APP_DIR =@@ -596,12 +406,29 @@ export function visitDataAppDevApp(clientHost: string) {}).should("exist");}-export function setUpDataAppDevServer(clientHost: string) {+const DATA_APP_DEV_SLUG = "vite-6-data-app-host-app";++type DataAppDevServerOpts = {+ clientHost: string;+ tableId: TableId;+};++export function setUpDataAppDevServer({+ clientHost,+ tableId,+}: DataAppDevServerOpts) {const mbUrl = Cypress.config("baseUrl");if (!mbUrl) {throw new Error("baseUrl must be set for the data-app dev-server suite");}+ createLibrary();+ publishTables({ table_ids: [tableId] });+ createDataApp({+ name: DATA_APP_DEV_SLUG,+ allowed_hosts: ["https://allowed.data-app.test"],+ });+cy.task("removeDataAppPaths", { paths: [DATA_APP_DEV_ENV_PATH] });waitForDataAppDevServerEnv(clientHost, mbUrl, { expectPresent: false });diff --git a/e2e/support/helpers/index.ts b/e2e/support/helpers/index.tsindex 0426b42c5ce..87b2cb3d5d4 100644--- a/e2e/support/helpers/index.ts+++ b/e2e/support/helpers/index.ts@@ -14,7 +14,6 @@ export * from "./e2e-comment-helpers";export * from "./e2e-content-diagnostics-helpers";export * from "./e2e-custom-column-helpers";export * from "./e2e-custom-viz-helpers";-export * from "./data-app-test-env";export * from "./e2e-data-app-helpers";export * from "./e2e-dashboard-helpers";export * from "./e2e-dashboard-visualizer-helpers";diff --git a/e2e/support/test-library-data.ts b/e2e/support/test-library-data.tsindex 453d486e504..e945e7d730d 100644--- a/e2e/support/test-library-data.ts+++ b/e2e/support/test-library-data.ts@@ -1,8 +1,10 @@const { H } = cy;import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";-import type { StructuredQuestionDetails } from "e2e/support/helpers/api";-import type { Collection } from "metabase-types/api";+import type {+ LibraryCollectionIds,+ StructuredQuestionDetails,+} from "e2e/support/helpers/api";const { ORDERS_ID } = SAMPLE_DATABASE;@@ -10,10 +12,6 @@ type StructuredQuestionDetailsWithName = StructuredQuestionDetails & {name: string;};-type LibraryResponse = Collection & {- effective_children?: Collection[];-};-export const TRUSTED_ORDERS_METRIC: StructuredQuestionDetailsWithName = {name: "Trusted Orders Metric",type: "metric",@@ -24,28 +22,27 @@ export const TRUSTED_ORDERS_METRIC: StructuredQuestionDetailsWithName = {};export function createLibraryWithItems() {- H.createLibrary().then((response: Cypress.Response<LibraryResponse>) => {- const body = response.body;- const metricsCollection = body.effective_children?.find(- (child) => child.name === "Metrics",- );-+ H.createLibrary().then(({ metricsCollectionId }) => {H.publishTables({ table_ids: [ORDERS_ID] });H.createQuestion({...TRUSTED_ORDERS_METRIC,}).then(({ body: card }) => {cy.request("PUT", `/api/card/${card.id}`, {- collection_id: metricsCollection?.id,+ collection_id: metricsCollectionId,});// Expose ids so specs can navigate/verify directly instead of clicking// through the lazily-rendered library tree, which is a flake source.cy.wrap(card.id).as("trustedMetricId");- cy.wrap(metricsCollection?.id).as("metricsCollectionId");+ cy.wrap(metricsCollectionId).as("metricsCollectionId");});});}-export function createLibraryWithTable() {- H.createLibrary();- H.publishTables({ table_ids: [ORDERS_ID] });+export function createLibraryWithTable(): Cypress.Chainable<LibraryCollectionIds> {+ return H.createLibrary().then((ids) =>+ H.publishTables({+ table_ids: [ORDERS_ID],+ collection_id: ids.dataCollectionId,+ }).then(() => ids),+ );}diff --git a/e2e/test-host-app/data-apps/data-app-dev-diagnostics.cy.spec.ts b/e2e/test-host-app/data-apps/data-app-dev-diagnostics.cy.spec.tsindex fda8e35f0ef..a911c79c02a 100644--- a/e2e/test-host-app/data-apps/data-app-dev-diagnostics.cy.spec.ts+++ b/e2e/test-host-app/data-apps/data-app-dev-diagnostics.cy.spec.ts@@ -1,3 +1,4 @@+import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";import {DATA_APP_DEV_DIAGNOSTICS_PATH,devToolbarPanel,@@ -19,12 +20,14 @@ const TIMEOUT_MS = 40000;const CLIENT_PORT = Cypress.expose("CLIENT_PORT");const CLIENT_HOST = `http://localhost:${CLIENT_PORT}`;+const { ORDERS_ID } = SAMPLE_DATABASE;+const DIAGNOSTICS_URL = `${CLIENT_HOST}${DATA_APP_DEV_DIAGNOSTICS_PATH}`;describe("Embedding SDK: data-app dev diagnostics", () => {before(() => {signInAsAdminAndEnableEmbeddingSdk();- setUpDataAppDevServer(CLIENT_HOST);+ setUpDataAppDevServer({ clientHost: CLIENT_HOST, tableId: ORDERS_ID });});after(() => {diff --git a/e2e/test-host-app/data-apps/data-app-dev.cy.spec.ts b/e2e/test-host-app/data-apps/data-app-dev.cy.spec.tsindex cd74046833a..2a991c87e3b 100644--- a/e2e/test-host-app/data-apps/data-app-dev.cy.spec.ts+++ b/e2e/test-host-app/data-apps/data-app-dev.cy.spec.ts@@ -1,3 +1,4 @@+import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";import {setUpDataAppDevServer,tearDownDataAppDevServer,@@ -9,10 +10,12 @@ const TIMEOUT_MS = 40000;const CLIENT_PORT = Cypress.expose("CLIENT_PORT");const CLIENT_HOST = `http://localhost:${CLIENT_PORT}`;+const { ORDERS_ID } = SAMPLE_DATABASE;+describe("Embedding SDK: data-app dev server", () => {before(() => {signInAsAdminAndEnableEmbeddingSdk();- setUpDataAppDevServer(CLIENT_HOST);+ setUpDataAppDevServer({ clientHost: CLIENT_HOST, tableId: ORDERS_ID });});after(() => {diff --git a/e2e/test/scenarios/actions/actions-on-dashboards.cy.spec.js b/e2e/test/scenarios/actions/actions-on-dashboards.cy.spec.jsindex 311383d6941..d2991c32ee0 100644--- a/e2e/test/scenarios/actions/actions-on-dashboards.cy.spec.js+++ b/e2e/test/scenarios/actions/actions-on-dashboards.cy.spec.js@@ -372,7 +372,7 @@ const MODEL_NAME = "Test Action Model";model_id: id,});})- .then(({ body: action }) => {+ .then((action) => {H.createDashboard(dashboardDetails).then(({ body: dashboard }) => {H.updateDashboardCards({diff --git a/e2e/test/scenarios/actions/actions-reproductions.cy.spec.js b/e2e/test/scenarios/actions/actions-reproductions.cy.spec.jsindex 8b985971f24..27863641a7b 100644--- a/e2e/test/scenarios/actions/actions-reproductions.cy.spec.js+++ b/e2e/test/scenarios/actions/actions-reproductions.cy.spec.js@@ -152,7 +152,7 @@ describe("Issue 32974", { tags: ["@external", "@actions"] }, () => {cy.get("@modelId").then((modelId) => {H.createAction({ ...QUERY_ACTION, model_id: modelId }).then(- ({ body: { id: actionId } }) => {+ ({ id: actionId }) => {cy.wrap(actionId).as("actionId");H.createDashboard(DASHBOARD_DETAILS).then(diff --git a/e2e/test/scenarios/admin/databases/database-writable-connection.cy.spec.ts b/e2e/test/scenarios/admin/databases/database-writable-connection.cy.spec.tsindex 1d15ecf0e20..b38e3f09adc 100644--- a/e2e/test/scenarios/admin/databases/database-writable-connection.cy.spec.ts+++ b/e2e/test/scenarios/admin/databases/database-writable-connection.cy.spec.ts@@ -303,24 +303,21 @@ function createModel() {}function createModelWithAction() {- return createModel()- .then((model) =>- H.createAction({- type: "query",- name: "Delete row",- database_id: WRITABLE_DB_ID,- model_id: model.id,- parameters: [],- dataset_query: {- database: WRITABLE_DB_ID,- type: "native",- native: {- query: `DELETE FROM ${ORDERS_TABLE_NAME} WHERE id = 1`,- },+ return createModel().then((model) =>+ H.createAction({+ type: "query",+ name: "Delete row",+ model_id: model.id,+ parameters: [],+ dataset_query: {+ database: WRITABLE_DB_ID,+ type: "native",+ native: {+ query: `DELETE FROM ${ORDERS_TABLE_NAME} WHERE id = 1`,},- }),- )- .then(({ body: action }) => action);+ },+ }),+ );}function runAction(actionId: number) {diff --git a/e2e/test/scenarios/admin/remote-sync-setup.cy.spec.ts b/e2e/test/scenarios/admin/remote-sync-setup.cy.spec.tsindex d5f9a5235e0..fb5ee06bf8d 100644--- a/e2e/test/scenarios/admin/remote-sync-setup.cy.spec.ts+++ b/e2e/test/scenarios/admin/remote-sync-setup.cy.spec.ts@@ -85,8 +85,10 @@ describe("Remote Sync first-time setup", () => {it("opens branch picker after push while collections to sync are hidden", () => {H.updateSetting("remote-sync-custom-collections-enabled", false);- H.createLibrary().then(({ body: library }) => {- H.configureGit("read-write", LOCAL_GIT_URL, { [library.id]: true });+ H.createLibrary().then(({ libraryCollectionId }) => {+ H.configureGit("read-write", LOCAL_GIT_URL, {+ [libraryCollectionId]: true,+ });});H.createBranchViaSettings("first-branch");diff --git a/e2e/test/scenarios/admin/remote-sync.cy.spec.ts b/e2e/test/scenarios/admin/remote-sync.cy.spec.tsindex 64b4e8621aa..1f01dc2c51c 100644--- a/e2e/test/scenarios/admin/remote-sync.cy.spec.ts+++ b/e2e/test/scenarios/admin/remote-sync.cy.spec.ts@@ -1190,8 +1190,10 @@ describe("Remote Sync", () => {it("flags a new term on the Glossary tab and pushes it to the repository", () => {// Glossary entries ride with the Library, so sync the Library rather than a plain collection.- H.createLibrary().then(({ body: library }) => {- H.configureGit("read-write", LOCAL_GIT_URL, { [library.id]: true });+ H.createLibrary().then(({ libraryCollectionId }) => {+ H.configureGit("read-write", LOCAL_GIT_URL, {+ [libraryCollectionId]: true,+ });});visitDataStudioGlossary();diff --git a/e2e/test/scenarios/dashboard/dashboard-back-navigation.cy.spec.js b/e2e/test/scenarios/dashboard/dashboard-back-navigation.cy.spec.jsindex b6eac2691df..b43e3fffd07 100644--- a/e2e/test/scenarios/dashboard/dashboard-back-navigation.cy.spec.js+++ b/e2e/test/scenarios/dashboard/dashboard-back-navigation.cy.spec.js@@ -352,7 +352,7 @@ const createDashboardWithCards = () => {H.createQuestion(questionDetails).then(({ body: { id: question_id } }) => {H.createQuestion(modelDetails).then(({ body: { id: model_id } }) => {H.createAction({ ...actionDetails, model_id }).then(- ({ body: { id: action_id } }) => {+ ({ id: action_id }) => {cy.request("PUT", `/api/dashboard/${dashboard_id}`, {dashcards: [{ id: -1, card_id: question_id, ...questionDashcardDetails },diff --git a/e2e/test/scenarios/data-apps/actions.cy.spec.ts b/e2e/test/scenarios/data-apps/actions.cy.spec.tsindex 12ae16a6629..6ef2cef343e 100644--- a/e2e/test/scenarios/data-apps/actions.cy.spec.ts+++ b/e2e/test/scenarios/data-apps/actions.cy.spec.ts@@ -1,20 +1,15 @@import { WRITABLE_DB_ID } from "e2e/support/cypress_data";import {DATA_APP_DISPLAY_NAME as APP_DISPLAY_NAME,- DATA_APP_NAME as APP_NAME,- createDataAppCollection,- createDataAppScoreboardAction,visitDataAppRoute as visitAppRoute,} from "e2e/support/helpers";-import type { CollectionPermission } from "metabase-types/api";-import { DATA_APP_TEST_ENV as TEST_ENV } from "./helpers";+import { createKitchenSinkDataApp } from "./helpers";const { H } = cy;const TEST_TABLE = "scoreboard_actions";const EXISTING_TEAM = "Amorous Aardvarks";-const RESOURCE_COLLECTION = "Data App resources";describe("scenarios > data apps > actions (useAction)",@@ -30,18 +25,6 @@ describe(H.setActionsEnabledForDB(WRITABLE_DB_ID);});- const setupActionsApp = (actionParams: Record<string, string | number>) =>- createDataAppScoreboardAction().then((action) => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: {- ...TEST_ENV,- actionEntityId: action.entity_id,- actionParams,- },- });- });-it("executes the action, exposing isExecuting and result, and resets", () => {cy.intercept("POST", "/api/action/*/execute", (req) => {req.on("response", (res) => {@@ -49,7 +32,7 @@ describe(});});- setupActionsApp({ team_name: "Data App FC", score: 7 });+ createKitchenSinkDataApp({}, { withAddTeamAction: true });visitAppRoute("actions");H.dataAppIframe(APP_DISPLAY_NAME).within(() => {@@ -78,9 +61,9 @@ describe(it("surfaces an error when the action fails", () => {// `team_name` is unique, so creating a team that already exists fails in the// database — a real rejection, not a stubbed one.- setupActionsApp({ team_name: EXISTING_TEAM, score: 1 });+ createKitchenSinkDataApp({}, { withAddTeamAction: true });- visitAppRoute("actions");+ visitAppRoute("actions-existing-team");H.dataAppIframe(APP_DISPLAY_NAME).within(() => {cy.findByTestId("action-execute").click();@@ -100,51 +83,9 @@ describe(});});- describe("collection permissions", () => {- const setupRestrictedApp = (access: CollectionPermission) =>- createDataAppCollection({ name: RESOURCE_COLLECTION, access }).then(- (collection) =>- createDataAppScoreboardAction({ collectionId: collection.id }).then(- (action) => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: {- ...TEST_ENV,- actionEntityId: action.entity_id,- actionParams: { team_name: "Data App FC", score: 7 },- },- });- },- ),- );-- const assertNothingWritten = () =>- H.queryWritableDB(- `SELECT team_name FROM ${TEST_TABLE} WHERE team_name = 'Data App FC'`,- ).then(({ rows }) => {- expect(rows).to.have.length(0);- });-- it("refuses the action when the user cannot read its collection", () => {- setupRestrictedApp("none");- cy.signInAsNormalUser();-- visitAppRoute("actions");- H.dataAppIframe(APP_DISPLAY_NAME).within(() => {- cy.findByTestId("action-execute").click();-- cy.findByTestId("action-error", { timeout: 30000 }).should(- "have.text",- "has-error",- );- cy.findByTestId("action-result").should("have.text", "no-result");- });-- assertNothingWritten();- });-- it("executes the action when the user has view access to its collection", () => {- setupRestrictedApp("read");+ describe("non-admin access", () => {+ it("executes action for non-admin member of app's group", () => {+ createKitchenSinkDataApp({}, { withAddTeamAction: true });cy.signInAsNormalUser();visitAppRoute("actions");diff --git a/e2e/test/scenarios/data-apps/backend_scope.cy.spec.ts b/e2e/test/scenarios/data-apps/backend_scope.cy.spec.tsindex 1ae11773849..41fa2e1f472 100644--- a/e2e/test/scenarios/data-apps/backend_scope.cy.spec.ts+++ b/e2e/test/scenarios/data-apps/backend_scope.cy.spec.ts@@ -3,7 +3,7 @@ import {DATA_APP_NAME as APP_NAME,} from "e2e/support/helpers";-import { DATA_APP_TEST_ENV as TEST_ENV } from "./helpers";+import { createKitchenSinkDataApp } from "./helpers";const { H } = cy;@@ -24,10 +24,7 @@ describe("scenarios > data apps > backend scope", () => {* exercise the surface.*/it("serves the whole InteractiveQuestion surface without refusing a request for scope", () => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: TEST_ENV,- });+ createKitchenSinkDataApp();H.openDataApp(APP_NAME);H.dataAppIframe(APP_DISPLAY_NAME).within(() => {diff --git a/e2e/test/scenarios/data-apps/custom-viz.cy.spec.ts b/e2e/test/scenarios/data-apps/custom-viz.cy.spec.tsindex 70e9fd199c1..073bd3735bd 100644--- a/e2e/test/scenarios/data-apps/custom-viz.cy.spec.ts+++ b/e2e/test/scenarios/data-apps/custom-viz.cy.spec.ts@@ -1,10 +1,9 @@import {DATA_APP_DISPLAY_NAME as APP_DISPLAY_NAME,- DATA_APP_NAME as APP_NAME,visitDataAppRoute as visitAppRoute,} from "e2e/support/helpers";-import { DATA_APP_TEST_ENV as TEST_ENV } from "./helpers";+import { createKitchenSinkDataApp } from "./helpers";const { H } = cy;@@ -21,15 +20,7 @@ describe("scenarios > data apps > custom visualizations", () => {});it("renders a custom visualization inside a data app", () => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: {- ...TEST_ENV,- // Opt the SDK into loading the custom viz inside the data app; without this- // allowlist the SDK never surfaces or loads the plugin.- allowedCustomVisualizations: [H.CUSTOM_VIZ_DISPLAY], // "custom:demo-viz"- },- });+ createKitchenSinkDataApp();visitAppRoute("custom-viz");diff --git a/e2e/test/scenarios/data-apps/data-studio.cy.spec.ts b/e2e/test/scenarios/data-apps/data-studio.cy.spec.tsindex d029ab6c407..4aa24ccfe4e 100644--- a/e2e/test/scenarios/data-apps/data-studio.cy.spec.ts+++ b/e2e/test/scenarios/data-apps/data-studio.cy.spec.ts@@ -4,7 +4,7 @@ import {} from "e2e/support/helpers";import type { DataApp } from "metabase-types/api";-import { DATA_APP_TEST_ENV as TEST_ENV } from "./helpers";+import { createKitchenSinkDataApp } from "./helpers";const { H } = cy;@@ -25,10 +25,7 @@ describe("scenarios > data apps > data studio", () => {});it("lists a data app and previews it on the app's overview", () => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: TEST_ENV,- });+ createKitchenSinkDataApp();cy.intercept("POST", "/api/dataset").as("dataAppQuery");diff --git a/e2e/test/scenarios/data-apps/helpers/index.ts b/e2e/test/scenarios/data-apps/helpers/index.tsindex b60a78d640d..6b0b9ca39e2 100644--- a/e2e/test/scenarios/data-apps/helpers/index.ts+++ b/e2e/test/scenarios/data-apps/helpers/index.ts@@ -1,15 +1,128 @@import {- ORDERS_COUNT_QUESTION_ENTITY_ID,- ORDERS_QUESTION_ENTITY_ID,-} from "e2e/support/cypress_sample_instance_data";-import type { DataAppTestEnv } from "e2e/support/helpers";--/** The `testEnv` the fixture's Overview page reads (Orders count + question). */-export const DATA_APP_TEST_ENV: DataAppTestEnv = {- scalarQuery: {- source: { type: "card", entityId: ORDERS_COUNT_QUESTION_ENTITY_ID },- },- questionQuery: {- source: { type: "card", entityId: ORDERS_QUESTION_ENTITY_ID },- },+ SAMPLE_DB_ID,+ USER_GROUPS,+ WRITABLE_DB_ID,+} from "e2e/support/cypress_data";+import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";+import type {+ Card,+ CollectionId,+ DataApp,+ TestStageSpec,+ VisualizationDisplay,+ WritebackAction,+} from "metabase-types/api";++const { H } = cy;++const { ORDERS_ID } = SAMPLE_DATABASE;++const COUNT = { type: "operator", operator: "count" } as const;++function createOrdersCard(+ collectionId: CollectionId,+ name: string,+ display: VisualizationDisplay,+ stage: TestStageSpec = {},+): Cypress.Chainable<Card> {+ return H.createTestQuery({+ database: SAMPLE_DB_ID,+ stages: [{ source: { type: "table", id: ORDERS_ID }, ...stage }],+ }).then((dataset_query) =>+ H.createCard({+ name,+ display,+ dataset_query,+ collection_id: collectionId,+ }),+ );+}++/** Creates the questions the kitchen-sink app reads, in `app`'s collection. */+function createKitchenSinkQuestions(app: DataApp) {+ createOrdersCard(app.resource_collection_id, "Orders", "table");+ createOrdersCard(app.resource_collection_id, "Orders count", "scalar", {+ aggregations: [COUNT],+ });+ createOrdersCard(app.resource_collection_id, "Orders by year", "line", {+ aggregations: [COUNT],+ breakouts: [{ type: "column", name: "CREATED_AT", unit: "year" }],+ });+}++type KitchenSinkDataAppDetails = {+ display_name?: string;+ description?: string | null;+ allowed_hosts?: string[];+ enabled?: boolean;};++type KitchenSinkDataAppOpts = {+ withAddTeamAction?: boolean;+};++/**+ * Creates the Library, publishes ORDERS to it, and creates the kitchen-sink app,+ * readable by All Users, with its questions, the "Add team" action when asked,+ * and its bundle built against them.+ */+export function createKitchenSinkDataApp(+ details: KitchenSinkDataAppDetails = {},+ { withAddTeamAction = false }: KitchenSinkDataAppOpts = {},+): Cypress.Chainable<DataApp> {+ H.createLibrary();+ H.publishTables({ table_ids: [ORDERS_ID] });++ return H.createDataApp({+ name: H.DATA_APP_NAME,+ display_name: H.DATA_APP_DISPLAY_NAME,+ ...details,+ }).then((app) => {+ H.addDataAppGroups(app, [USER_GROUPS.ALL_USERS_GROUP]);+ createKitchenSinkQuestions(app);+ if (withAddTeamAction) {+ createAddTeamAction(app.resource_collection_id);+ }+ return H.uploadDataAppBundle(app);+ });+}++/** Creates the model-less "Add team" action, which inserts a team into `scoreboard_actions`. */+export function createAddTeamAction(+ collectionId: CollectionId,+): Cypress.Chainable<WritebackAction> {+ return H.createTestNativeQuery({+ database: WRITABLE_DB_ID,+ query:+ "INSERT INTO scoreboard_actions (team_name, score) VALUES ({{team_name}}, {{score}})",+ templateTags: {+ team_name: { type: "text", "display-name": "Team name", required: true },+ score: { type: "number", "display-name": "Score", required: true },+ },+ }).then((dataset_query) =>+ H.createAction({+ name: "Add team",+ type: "query",+ collection_id: collectionId,+ dataset_query,+ parameters: [+ {+ id: "team_name",+ slug: "team_name",+ name: "Team name",+ type: "string/=",+ target: ["variable", ["template-tag", "team_name"]],+ required: true,+ },+ {+ id: "score",+ slug: "score",+ name: "Score",+ type: "number/=",+ target: ["variable", ["template-tag", "score"]],+ required: true,+ },+ ],+ }),+ );+}diff --git a/e2e/test/scenarios/data-apps/resources-production-actions.cy.spec.ts b/e2e/test/scenarios/data-apps/resources-production-actions.cy.spec.tsindex 694f811f69f..ed1367a43b4 100644--- a/e2e/test/scenarios/data-apps/resources-production-actions.cy.spec.ts+++ b/e2e/test/scenarios/data-apps/resources-production-actions.cy.spec.ts@@ -1,5 +1,7 @@import { USERS, WRITABLE_DB_ID } from "e2e/support/cypress_data";+import { createAddTeamAction } from "./helpers";+const { H } = cy;const TEST_TABLE = "scoreboard_actions";@@ -19,7 +21,7 @@ describe(() => {const removeTestFiles = () =>cy.task("removeDataAppPaths", {- paths: [`${APP_ROOT()}/actions`, `${APP_ROOT()}/collections`],+ paths: [`${APP_ROOT()}/collections`],});beforeEach(() => {@@ -39,8 +41,8 @@ describe(});/**- * Publishes the app, creates an action in its collection, and declares it- * by its entity ID, as the app's typed schema does.+ * Publishes the app, creates an action in its collection, and uploads the+ * app's bundle built against its typed schema.*/const publishApp = () => {H.writeDataAppResources(APP_ROOT(), {@@ -51,27 +53,22 @@ describe(});return H.publishDataApp(APP_ROOT(), APP_SLUG).then((app) =>- H.createDataAppScoreboardAction({- collectionId: app.resource_collection_id,- }).then((action) => {- H.declareDataAppActions(APP_ROOT(), [- { exportName: "CreateScore", actionEntityId: action.entity_id },- ]);-- return cy.wrap({ app, action }, { log: false });- }),+ createAddTeamAction(app.resource_collection_id).then((action) =>+ H.uploadDataAppBundle(app).then(() =>+ cy.wrap({ app, action }, { log: false }),+ ),+ ),);};it("executes the action in the app's collection for a member of the app's group", () => {publishApp().then(({ app, action }) => {- H.assignTestGroupToDataApp(app.name).then((groupId) => {+ H.assignTestGroupToDataApp(app).then((groupId) => {H.addUserToGroup(groupId, USERS.normal.email);});cy.signInAsNormalUser();cy.intercept("POST", "/api/action/*/execute").as("execute");- H.mockDataApp(APP_SLUG, { displayName: APP_DISPLAY_NAME });cy.visit(`/apps/${APP_SLUG}`);H.dataAppIframe(APP_DISPLAY_NAME).within(() => {diff --git a/e2e/test/scenarios/data-apps/resources-production.cy.spec.ts b/e2e/test/scenarios/data-apps/resources-production.cy.spec.tsindex 13af64e3b2e..aaabb742338 100644--- a/e2e/test/scenarios/data-apps/resources-production.cy.spec.ts+++ b/e2e/test/scenarios/data-apps/resources-production.cy.spec.ts@@ -1,4 +1,5 @@import { USERS } from "e2e/support/cypress_data";+import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";import type { PortableTable } from "e2e/support/helpers";import type { Card } from "metabase-types/api";@@ -9,26 +10,16 @@ const APP_DISPLAY_NAME = "Synced App";/** The fixture's `data_app.yaml` names this collection. */const COLLECTION = "syncedAppCollection01";-const QUESTION = "syncedAppOrdersCount1";+const QUESTION_ENTITY_ID = "syncedAppOrdersCount1";const ORDERS_TABLE: PortableTable = ["Sample Database", "PUBLIC", "ORDERS"];const APP_ROOT = () =>`${Cypress.config("projectRoot")}/e2e/support/assets/data-apps/${APP_SLUG}`;-/** The definition the app's typed schema holds for its saved question. */-const DECLARATION = [- 'import { defineQuery } from "@metabase/embedding-sdk-react/data-app";',- "",- "export const OrdersCount = defineQuery({",- ` source: { type: "card", entityId: "${QUESTION}" },`,- "});",- "",-].join("\n");--/** The saved question an author writes for `OrdersCount`. */+/** The saved question an author writes, which the app's typed schema defines as `ordersCount`. */const ORDERS_COUNT_QUESTION = H.dataAppRepresentations.card({- entityId: QUESTION,+ entityId: QUESTION_ENTITY_ID,name: "OrdersCount",type: "question",collection: COLLECTION,@@ -47,23 +38,22 @@ const ORDERS_COUNT_QUESTION = H.dataAppRepresentations.card({/*** An app imported from a repository runs the saved question its typed schema- * names, which the pull loaded into the app's collection.+ * defines, which the pull loaded into the app's collection.*/describe("scenarios > data apps > resources from a repository", () => {const removeTestFiles = () =>cy.task("removeDataAppPaths", {- paths: [`${APP_ROOT()}/queries`, `${APP_ROOT()}/collections`],+ paths: [`${APP_ROOT()}/collections`],});beforeEach(() => {H.restore();cy.signInAsAdmin();H.activateToken("bleeding-edge");+ H.createLibrary();+ H.publishTables({ table_ids: [SAMPLE_DATABASE.ORDERS_ID] });removeTestFiles();- cy.task("writeDataAppFiles", {- files: { [`${APP_ROOT()}/queries/orders.query.ts`]: DECLARATION },- });H.writeDataAppResources(APP_ROOT(), {collection: H.dataAppRepresentations.collection(COLLECTION,@@ -77,20 +67,21 @@ describe("scenarios > data apps > resources from a repository", () => {removeTestFiles();});- /** Publishes the fixture and yields the app with the ID of the card it must now run. */+ /** Publishes the fixture, uploads its bundle, and yields the app with the ID of the card it must now run. */const publishApp = () =>H.publishDataApp(APP_ROOT(), APP_SLUG).then((app) =>- cy- .request<Card>(`/api/card/${QUESTION}`)- .then(({ body: card }) =>- cy.wrap({ app, cardId: card.id }, { log: false }),- ),+ H.uploadDataAppBundle(app).then(() =>+ cy+ .request<Card>(`/api/card/${QUESTION_ENTITY_ID}`)+ .then(({ body: card }) =>+ cy.wrap({ app, cardId: card.id }, { log: false }),+ ),+ ),);it("runs the saved question its definition names", () => {publishApp().then(({ cardId }) => {cy.intercept("POST", "/api/dataset").as("dataset");- H.mockDataApp(APP_SLUG, { displayName: APP_DISPLAY_NAME });cy.visit(`/apps/${APP_SLUG}`);H.dataAppIframe(APP_DISPLAY_NAME).within(() => {@@ -110,12 +101,11 @@ describe("scenarios > data apps > resources from a repository", () => {it("serves the app to a member of its permission group", () => {publishApp().then(({ app }) => {- H.assignTestGroupToDataApp(app.name).then((groupId) => {+ H.assignTestGroupToDataApp(app).then((groupId) => {H.addUserToGroup(groupId, USERS.normal.email);});cy.signInAsNormalUser();- H.mockDataApp(APP_SLUG, { displayName: APP_DISPLAY_NAME });cy.visit(`/apps/${APP_SLUG}`);H.dataAppIframe(APP_DISPLAY_NAME).within(() => {diff --git a/e2e/test/scenarios/data-apps/sandbox.cy.spec.ts b/e2e/test/scenarios/data-apps/sandbox.cy.spec.tsindex 068dd43a036..9a785ca50e5 100644--- a/e2e/test/scenarios/data-apps/sandbox.cy.spec.ts+++ b/e2e/test/scenarios/data-apps/sandbox.cy.spec.ts@@ -4,7 +4,7 @@ import {visitDataAppRoute as visitAppRoute,} from "e2e/support/helpers";-import { DATA_APP_TEST_ENV as TEST_ENV } from "./helpers";+import { createKitchenSinkDataApp } from "./helpers";const { H } = cy;@@ -19,7 +19,6 @@ describe("scenarios > data apps > sandbox & isolation", () => {describe("sandboxing", () => {const ALLOWED_ORIGIN = "https://allowed.data-app.test";const ALLOWED_URL = `${ALLOWED_ORIGIN}/ping`;- const BLOCKED_URL = "https://blocked.data-app.test/ping";// Method/constructor calls the sandbox distortion replaces with a throwing// shim. (Getter-only reads like `localStorage` aren't intercepted, so they@@ -44,19 +43,7 @@ describe("scenarios > data apps > sandbox & isolation", () => {body: "pong",});- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- allowedHosts: [ALLOWED_ORIGIN],- testEnv: {- ...TEST_ENV,- sandbox: {- allowedUrl: ALLOWED_URL,- blockedUrl: BLOCKED_URL,- xhrAllowedUrl: ALLOWED_URL,- xhrBlockedUrl: BLOCKED_URL,- },- },- });+ createKitchenSinkDataApp({ allowed_hosts: [ALLOWED_ORIGIN] });visitAppRoute("sandboxing");H.dataAppIframe(APP_DISPLAY_NAME).within(() => {@@ -95,10 +82,7 @@ describe("scenarios > data apps > sandbox & isolation", () => {describe("isolation", () => {it("serves the embed document with locked-down headers, renders the app in a sandboxed iframe, and keeps the app's CSS and JS globals inside it", () => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: TEST_ENV,- });+ createKitchenSinkDataApp();cy.request({url: `/embed/apps/${APP_NAME}`,diff --git a/e2e/test/scenarios/data-apps/sandboxing_isolation.cy.spec.ts b/e2e/test/scenarios/data-apps/sandboxing_isolation.cy.spec.tsindex 795fa56c458..2683bfa579e 100644--- a/e2e/test/scenarios/data-apps/sandboxing_isolation.cy.spec.ts+++ b/e2e/test/scenarios/data-apps/sandboxing_isolation.cy.spec.ts@@ -3,8 +3,6 @@ const { H } = cy;const APP_NAME = "isolation";const APP_DISPLAY_NAME = "Isolation";-type IsolationTestEnv = { instanceUrl: string };-describe("scenarios > data apps > sandbox isolation", () => {beforeEach(() => {H.restore();@@ -12,11 +10,15 @@ describe("scenarios > data apps > sandbox isolation", () => {H.activateToken("bleeding-edge");});- const setup = () => {- const instanceUrl = Cypress.config("baseUrl") ?? "";- const testEnv: IsolationTestEnv = { instanceUrl };+ const createIsolationDataApp = (allowedHosts: string[] = []) =>+ H.createDataApp({+ name: APP_NAME,+ display_name: APP_DISPLAY_NAME,+ allowed_hosts: allowedHosts,+ }).then(H.uploadDataAppBundle);- H.mockDataApp(APP_NAME, { displayName: APP_DISPLAY_NAME, testEnv });+ const setup = () => {+ createIsolationDataApp();H.openDataApp(APP_NAME);H.dataAppIframe(APP_DISPLAY_NAME).within(() => {cy.findByTestId("isolation-result", { timeout: 30000 }).should(@@ -174,10 +176,7 @@ describe("scenarios > data apps > sandbox isolation", () => {* the guard's own message instead, and open the app again for each probe.*/it("gates host-React iframes: about:blank, Metabase itself and srcdoc", () => {- const instanceUrl = Cypress.config("baseUrl") ?? "";- const testEnv: IsolationTestEnv = { instanceUrl };-- H.mockDataApp(APP_NAME, { displayName: APP_DISPLAY_NAME, testEnv });+ createIsolationDataApp();["react-about-blank", "react-src", "react-srcdoc"].forEach((probeId) => {cy.log(probeId);@@ -223,11 +222,7 @@ describe("scenarios > data apps > sandbox isolation", () => {});}).as("allowedHostRedirect");- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: { instanceUrl },- allowedHosts: ["http://localhost:4444"],- });+ createIsolationDataApp(["http://localhost:4444"]);H.openDataApp(APP_NAME);H.dataAppIframe(APP_DISPLAY_NAME).within(() => {cy.findByTestId("isolation-result", { timeout: 30000 }).should(diff --git a/e2e/test/scenarios/data-apps/sdk.cy.spec.ts b/e2e/test/scenarios/data-apps/sdk.cy.spec.tsindex f1dbab4e84d..202d67966e3 100644--- a/e2e/test/scenarios/data-apps/sdk.cy.spec.ts+++ b/e2e/test/scenarios/data-apps/sdk.cy.spec.ts@@ -1,11 +1,9 @@-import { ORDERS_BY_YEAR_QUESTION_ID } from "e2e/support/cypress_sample_instance_data";import {DATA_APP_DISPLAY_NAME as APP_DISPLAY_NAME,- DATA_APP_NAME as APP_NAME,visitDataAppRoute as visitAppRoute,} from "e2e/support/helpers";-import { DATA_APP_TEST_ENV as TEST_ENV } from "./helpers";+import { createKitchenSinkDataApp } from "./helpers";const { H } = cy;@@ -18,21 +16,9 @@ describe("scenarios > data apps > SDK runtime", () => {});describe("query hooks & question components", () => {- const setupQueryStatesApp = () =>- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: {- ...TEST_ENV,- // A card that doesn't exist, so the query resolves to an error.- errorQuery: {- source: { type: "card", entityId: "missingQuestionEntity" },- },- },- });-it("surfaces the useMetabaseQuery error state, and re-runs the query when the app calls refetch", () => {cy.intercept("POST", "/api/dataset").as("datasetQuery");- setupQueryStatesApp();+ createKitchenSinkDataApp();visitAppRoute("query-states");H.dataAppIframe(APP_DISPLAY_NAME).within(() => {@@ -62,10 +48,7 @@ describe("scenarios > data apps > SDK runtime", () => {});it("renders a StaticQuestion from useMetabaseQueryObject", () => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: TEST_ENV,- });+ createKitchenSinkDataApp();visitAppRoute("static-question");H.dataAppIframe(APP_DISPLAY_NAME).within(() => {@@ -97,10 +80,7 @@ describe("scenarios > data apps > SDK runtime", () => {describe("clipboard (copy)", () => {it("writes text to the clipboard from a user click", () => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: TEST_ENV,- });+ createKitchenSinkDataApp();visitAppRoute("clipboard");@@ -135,10 +115,7 @@ describe("scenarios > data apps > SDK runtime", () => {describe("error component", () => {it("shows the default neutral error state for a missing question", () => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: TEST_ENV,- });+ createKitchenSinkDataApp();visitAppRoute("missing-question");H.dataAppIframe(APP_DISPLAY_NAME).within(() => {@@ -150,7 +127,9 @@ describe("scenarios > data apps > SDK runtime", () => {const CUSTOM_APP = "custom-error-component";const CUSTOM_DISPLAY = "Custom Error App";- H.mockDataApp(CUSTOM_APP, { displayName: CUSTOM_DISPLAY });+ H.createDataApp({ name: CUSTOM_APP, display_name: CUSTOM_DISPLAY }).then(+ H.uploadDataAppBundle,+ );H.openDataApp(CUSTOM_APP);H.dataAppIframe(CUSTOM_DISPLAY).within(() => {@@ -167,13 +146,7 @@ describe("scenarios > data apps > SDK runtime", () => {});it("downloads a chart question as PNG", () => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: {- ...TEST_ENV,- downloadQuestionId: ORDERS_BY_YEAR_QUESTION_ID,- },- });+ createKitchenSinkDataApp();visitAppRoute("download-question");diff --git a/e2e/test/scenarios/data-apps/viewing.cy.spec.ts b/e2e/test/scenarios/data-apps/viewing.cy.spec.tsindex 647e5e78f37..b8ea9bb6192 100644--- a/e2e/test/scenarios/data-apps/viewing.cy.spec.ts+++ b/e2e/test/scenarios/data-apps/viewing.cy.spec.ts@@ -10,7 +10,7 @@ import {type GroupPermissions,} from "metabase-types/api";-import { DATA_APP_TEST_ENV as TEST_ENV } from "./helpers";+import { createKitchenSinkDataApp } from "./helpers";const { H } = cy;@@ -31,10 +31,7 @@ describe("scenarios > data apps > viewing & routing", () => {describe("viewing permissions", () => {it("lets a non-admin open a data app by direct URL", () => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: TEST_ENV,- });+ createKitchenSinkDataApp();// A normal (non-admin) user has data access, so the app opens and renders// its data — the admin gate is only on *managing* apps, not viewing them.@@ -56,10 +53,7 @@ describe("scenarios > data apps > viewing & routing", () => {[USER_GROUPS.COLLECTION_GROUP]: BLOCKED_PERMISSION,});- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: TEST_ENV,- });+ createKitchenSinkDataApp();cy.signIn("nodata");H.openDataApp(APP_NAME);@@ -79,8 +73,8 @@ describe("scenarios > data apps > viewing & routing", () => {});it("shows a not-found state for a disabled or missing app", () => {- // No app with this slug exists, so the metadata endpoint really 404s and the- // host renders a not-found state rather than a broken iframe — no mock needed.+ // No app with this slug exists, so the metadata endpoint 404s and the host+ // renders a not-found state rather than a broken iframe.H.openDataApp("does-not-exist");H.main().findByText("Data app not found").should("be.visible");});@@ -88,11 +82,15 @@ describe("scenarios > data apps > viewing & routing", () => {describe("loading state", () => {it("keeps the loader up until the app's bundle has actually rendered", () => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: TEST_ENV,- bundleDelayMs: 2000,- });+ createKitchenSinkDataApp();+ cy.intercept(+ { method: "GET", pathname: `/api/ee/apps/${APP_NAME}/bundle` },+ (req) => {+ req.on("response", (res) => {+ res.setDelay(2000);+ });+ },+ );H.openDataApp(APP_NAME);@@ -111,10 +109,7 @@ describe("scenarios > data apps > viewing & routing", () => {describe("internal routing", () => {it("starts on a deep-linked sub-route, and mirrors link and imperative route changes into the parent URL", () => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: TEST_ENV,- });+ createKitchenSinkDataApp();cy.log("deep-link directly to a sub-route");visitAppRoute("details");@@ -160,42 +155,9 @@ describe("scenarios > data apps > viewing & routing", () => {});});- describe("outdated apps", () => {- it("tells a non-admin why an outdated app won't open", () => {- H.mockDataApp(APP_NAME, { displayName: APP_DISPLAY_NAME });-- // The metadata endpoint refuses an outdated app to non-admins with this- // exact shape (see `check-not-outdated` in data_apps/api.clj).- cy.intercept(- { method: "GET", pathname: `/api/ee/apps/${APP_NAME}` },- {- statusCode: 409,- body: {- "error-code": "data-app-outdated",- message: "This app was built for version 1 of data apps.",- },- },- );-- cy.signInAsNormalUser();- H.openDataApp(APP_NAME);-- H.main().within(() => {- cy.findByText("This data app is outdated").should("be.visible");- cy.findByText("This app was built for version 1 of data apps.").should(- "be.visible",- );- });- cy.get("iframe").should("not.exist");- });- });-describe("host error / not-ready screens", () => {it("shows a themed host error screen when the bundle throws while rendering", () => {- H.mockDataApp(APP_NAME, {- displayName: APP_DISPLAY_NAME,- testEnv: TEST_ENV,- });+ createKitchenSinkDataApp();visitAppRoute("throw");// The throw is caught in the iframe and reported to the parent, whichdiff --git a/e2e/test/scenarios/data-studio/data-studio-dashboards.cy.spec.ts b/e2e/test/scenarios/data-studio/data-studio-dashboards.cy.spec.tsindex 84a98b090ea..55195f51b46 100644--- a/e2e/test/scenarios/data-studio/data-studio-dashboards.cy.spec.ts+++ b/e2e/test/scenarios/data-studio/data-studio-dashboards.cy.spec.ts@@ -12,14 +12,7 @@ const dashboardsPage = () => cy.findByTestId("library-page");const dashboardHeader = () => cy.findByTestId("dashboard-pane-header");function createLibraryDashboard() {- H.createLibrary().then(({ body: library }) => {- const dashboardsCollection = library.effective_children?.find(- ({ type }) => type === "library-dashboards",- );- if (dashboardsCollection == null) {- throw new Error("The Library has no Dashboards collection");- }-+ H.createLibrary().then(({ dashboardsCollectionId }) => {H.createQuestionAndDashboard({questionDetails: {name: QUESTION_NAME,@@ -27,7 +20,7 @@ function createLibraryDashboard() {},dashboardDetails: {name: DASHBOARD_NAME,- collection_id: dashboardsCollection.id,+ collection_id: dashboardsCollectionId,},}).then(({ body: dashcard }) => {cy.wrap(dashcard.dashboard_id).as("dashboardId");diff --git a/e2e/test/scenarios/data-studio/data-studio-library.cy.spec.ts b/e2e/test/scenarios/data-studio/data-studio-library.cy.spec.tsindex 39b70689910..9c6f4a206ca 100644--- a/e2e/test/scenarios/data-studio/data-studio-library.cy.spec.ts+++ b/e2e/test/scenarios/data-studio/data-studio-library.cy.spec.ts@@ -1,6 +1,7 @@const { H } = cy;import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";+import type { LibraryCollectionIds } from "e2e/support/helpers/api";import {TRUSTED_ORDERS_METRIC,createLibraryWithTable,@@ -9,15 +10,6 @@ import type { Collection } from "metabase-types/api";const { ORDERS_ID, PRODUCTS_ID, PEOPLE_ID, REVIEWS_ID } = SAMPLE_DATABASE;-type LibraryResponse = Collection & {- effective_children?: Collection[];-};--type LibraryRootCollections = {- dataCollection: Collection;- metricCollection: Collection;-};-describe("scenarios > data studio > library", () => {beforeEach(() => {H.restore();@@ -92,7 +84,7 @@ describe("scenarios > data studio > library", () => {});it("should show the library in the data picker and build the path from a value", () => {- createLibraryWithTable();+ createLibraryWithTable().as("library");cy.log("Library is visible when only the Data collection has items");H.startNewQuestion();@@ -103,10 +95,10 @@ describe("scenarios > data studio > library", () => {H.entityPickerModalItem(2, "Orders").should("exist");cy.log("Add a metric to the Metrics collection");- getLibraryRootCollections().then(({ metricCollection }) => {+ cy.get<LibraryCollectionIds>("@library").then(({ metricsCollectionId }) => {H.createQuestion({ ...TRUSTED_ORDERS_METRIC }).then(({ body: card }) => {cy.request("PUT", `/api/card/${card.id}`, {- collection_id: metricCollection.id,+ collection_id: metricsCollectionId,});});});@@ -145,30 +137,29 @@ describe("scenarios > data studio > library", () => {describe("Library collection management", () => {it("should create, edit, move, and archive library collections and move a published table", () => {- H.createLibrary();H.createCollection({ name: "Outside Library" });- getLibraryRootCollections().then(({ dataCollection }) => {+ H.createLibrary().then(({ dataCollectionId }) => {createLibraryCollection({name: "Collection Before Edit",description: "Original description",- parent_id: dataCollection.id,+ parent_id: dataCollectionId,});createLibraryCollection({name: "Destination Collection",- parent_id: dataCollection.id,+ parent_id: dataCollectionId,});createLibraryCollection({name: "Collection To Move",- parent_id: dataCollection.id,+ parent_id: dataCollectionId,});H.publishTables({ table_ids: [ORDERS_ID] });createLibraryCollection({name: "Table Destination Collection",- parent_id: dataCollection.id,+ parent_id: dataCollectionId,});createLibraryCollection({name: "Collection To Archive",- parent_id: dataCollection.id,+ parent_id: dataCollectionId,});});@@ -467,37 +458,34 @@ describe("scenarios > data studio > library", () => {describe("bulk actions", () => {it("selects, moves, and unpublishes library items in bulk", () => {- H.createLibrary();- getLibraryRootCollections().then(- ({ dataCollection, metricCollection }) => {- H.publishTables({- table_ids: [ORDERS_ID, PRODUCTS_ID, PEOPLE_ID, REVIEWS_ID],- });- H.createQuestion({ ...TRUSTED_ORDERS_METRIC }).then(- ({ body: card }) => {- cy.request("PUT", `/api/card/${card.id}`, {- collection_id: metricCollection.id,- });- },- );- createLibraryCollection({- name: "Folder A",- parent_id: dataCollection.id,- });- createLibraryCollection({- name: "Folder B",- parent_id: dataCollection.id,- });- createLibraryCollection({- name: "Folder C",- parent_id: dataCollection.id,- });- createLibraryCollection({- name: "Folder D",- parent_id: dataCollection.id,- });- },- );+ H.createLibrary().then(({ dataCollectionId, metricsCollectionId }) => {+ H.publishTables({+ table_ids: [ORDERS_ID, PRODUCTS_ID, PEOPLE_ID, REVIEWS_ID],+ });+ H.createQuestion({ ...TRUSTED_ORDERS_METRIC }).then(+ ({ body: card }) => {+ cy.request("PUT", `/api/card/${card.id}`, {+ collection_id: metricsCollectionId,+ });+ },+ );+ createLibraryCollection({+ name: "Folder A",+ parent_id: dataCollectionId,+ });+ createLibraryCollection({+ name: "Folder B",+ parent_id: dataCollectionId,+ });+ createLibraryCollection({+ name: "Folder C",+ parent_id: dataCollectionId,+ });+ createLibraryCollection({+ name: "Folder D",+ parent_id: dataCollectionId,+ });+ });H.DataStudio.Library.visit();cy.log("Section-root checkbox selects every item in the section");@@ -608,11 +596,10 @@ describe("scenarios > data studio > library", () => {});it("subsumes a collection's descendants so moving the parent preserves nesting", () => {- H.createLibrary();- getLibraryRootCollections().then(({ dataCollection }) => {+ H.createLibrary().then(({ dataCollectionId }) => {createLibraryCollection({name: "Parent Folder",- parent_id: dataCollection.id,+ parent_id: dataCollectionId,}).then((parent) => {H.publishTables({ table_ids: [ORDERS_ID], collection_id: parent.id });createLibraryCollection({@@ -622,7 +609,7 @@ describe("scenarios > data studio > library", () => {});createLibraryCollection({name: "Target Folder",- parent_id: dataCollection.id,+ parent_id: dataCollectionId,});});H.DataStudio.Library.visit();@@ -702,29 +689,6 @@ describe("scenarios > data studio > library", () => {});});-function getLibraryRootCollections(): Cypress.Chainable<LibraryRootCollections> {- return cy- .request<LibraryResponse>("GET", "/api/ee/library")- .then(({ body }) => {- const dataCollection = body.effective_children?.find(- (collection) => collection.type === "library-data",- );- const metricCollection = body.effective_children?.find(- (collection) => collection.type === "library-metrics",- );-- expect(dataCollection, "Data collection").to.exist;- expect(metricCollection, "Metrics collection").to.exist;-- return {- // Unjustified type cast. FIXME- dataCollection: dataCollection as Collection,- // Unjustified type cast. FIXME- metricCollection: metricCollection as Collection,- };- });-}-function createLibraryCollection({name,description = null,diff --git a/e2e/test/scenarios/monitor/ai-auditing/data-apps-analytics.cy.spec.ts b/e2e/test/scenarios/monitor/ai-auditing/data-apps-analytics.cy.spec.tsindex 6982d0a8daf..17447337a73 100644--- a/e2e/test/scenarios/monitor/ai-auditing/data-apps-analytics.cy.spec.ts+++ b/e2e/test/scenarios/monitor/ai-auditing/data-apps-analytics.cy.spec.ts@@ -1,5 +1,7 @@import { USERS, WRITABLE_DB_ID } from "e2e/support/cypress_data";+import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";import type { PortableTable } from "e2e/support/helpers";+import { createAddTeamAction } from "e2e/test/scenarios/data-apps/helpers";const { H } = cy;@@ -31,9 +33,7 @@ const ADMIN_NAME = `${USERS.admin.first_name} ${USERS.admin.last_name}`;function removeTestFiles() {cy.task("removeDataAppPaths", {paths: [- `${appRoot(QUESTION_APP.slug)}/queries`,`${appRoot(QUESTION_APP.slug)}/collections`,- `${appRoot(ACTIONS_APP.slug)}/actions`,`${appRoot(ACTIONS_APP.slug)}/collections`,],});@@ -60,18 +60,6 @@ function writeQuestionAppResources(root: string) {function publishQuestionApp() {const root = appRoot(QUESTION_APP.slug);- cy.task("writeDataAppFiles", {- files: {- [`${root}/queries/orders.query.ts`]: [- 'import { defineQuery } from "@metabase/embedding-sdk-react/data-app";',- "",- "export const OrdersCount = defineQuery({",- ` source: { type: "card", entityId: "${QUESTION_APP.question}" },`,- "});",- "",- ].join("\n"),- },- });writeQuestionAppResources(root);return H.publishDataApp(root, QUESTION_APP.slug);}@@ -92,13 +80,7 @@ function publishActionsApp() {return H.publishDataApp(root, ACTIONS_APP.slug, {initializeRepo: false,}).then((app) =>- H.createDataAppScoreboardAction({- collectionId: app.resource_collection_id,- }).then((action) =>- H.declareDataAppActions(root, [- { exportName: "CreateScore", actionEntityId: action.entity_id },- ]),- ),+ createAddTeamAction(app.resource_collection_id).then(() => app),);}@@ -129,6 +111,8 @@ describe(H.restore("postgres-writable");cy.signInAsAdmin();H.activateToken("bleeding-edge");+ H.createLibrary();+ H.publishTables({ table_ids: [SAMPLE_DATABASE.ORDERS_ID] });H.resetTestTable({ type: "postgres", table: SCOREBOARD_TABLE });H.resyncDatabase({ dbId: WRITABLE_DB_ID, tableName: SCOREBOARD_TABLE });@@ -142,14 +126,18 @@ describe(});it("lists the questions and actions published data apps run, and narrows the log by entity type and app", () => {- publishQuestionApp();- publishActionsApp();+ cy.log(+ "Publish both apps, then upload their bundles, since each pull reloads every app's placeholder bundle",+ );+ publishQuestionApp().then((questionApp) =>+ publishActionsApp().then((actionsApp) => {+ H.uploadDataAppBundle(questionApp);+ H.uploadDataAppBundle(actionsApp);+ }),+ );cy.log("Read the question in one app");cy.intercept("POST", "/api/dataset").as("appQuery");- H.mockDataApp(QUESTION_APP.slug, {- displayName: QUESTION_APP.displayName,- });cy.visit(`/apps/${QUESTION_APP.slug}`);H.dataAppIframe(QUESTION_APP.displayName).findByTestId("synced-app-total", { timeout: 30000 })@@ -160,9 +148,6 @@ describe(cy.log("Run the action in the other");cy.intercept("POST", "/api/action/*/execute").as("execute");- H.mockDataApp(ACTIONS_APP.slug, {- displayName: ACTIONS_APP.displayName,- });cy.visit(`/apps/${ACTIONS_APP.slug}`);H.dataAppIframe(ACTIONS_APP.displayName).within(() => {cy.findByTestId("action-execute", { timeout: 30000 }).click();@@ -170,13 +155,6 @@ describe(});cy.wait("@execute");- cy.log(- "The page names the apps from the installed list, which the app mocks replaced",- );- cy.intercept("GET", "/api/ee/apps", (req) => {- req.continue();- });-cy.visit(DATA_APPS_ANALYTICS_PATH);cy.findByTestId("monitor-nav").findByRole("link", { name: "Data app analytics" })diff --git a/e2e/test/scenarios/sharing/public-sharing.cy.spec.js b/e2e/test/scenarios/sharing/public-sharing.cy.spec.jsindex c978dbac728..adce8b4d950 100644--- a/e2e/test/scenarios/sharing/public-sharing.cy.spec.js+++ b/e2e/test/scenarios/sharing/public-sharing.cy.spec.js@@ -140,9 +140,8 @@ describe("scenarios > admin > settings > public sharing", () => {...DEFAULT_ACTION_DETAILS,name: expectedActionName,model_id: modelId,- }).then(({ body }) => {- const actionId = body.id;- cy.wrap(actionId).as("actionId");+ }).then((action) => {+ cy.wrap(action.id).as("actionId");});});diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/apps.clj b/enterprise/backend/src/metabase_enterprise/data_apps/apps.cljindex 0f72cf61179..59e8851dc91 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/apps.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/apps.clj@@ -4,6 +4,7 @@[clojure.string :as str][metabase-enterprise.data-apps.db :as data-apps.db][metabase.api.common :as api]+ [metabase.collections.core :as collections][metabase.settings.core :as setting][metabase.util.i18n :refer [tru]][toucan2.core :as t2]))@@ -20,10 +21,18 @@(when-not (str/blank? url)url)))+(defn collection-name+ "The name of the resource collection the API creates for the data app with `slug`."+ [slug]+ (format "Data App: %s" slug))+(defn create-app!- "Create the data app `row` and return its ID. Throws a 409 when a data app already has the slug."+ "Create the data app `row` with a new root collection of the `data-apps` namespace as its resource collection, and+ return the app's ID; throws a 409 when a data app already has the slug."[{slug :name :as row}](t2/with-transaction [_conn](api/check (not (data-apps.db/data-app-exists? slug))[409 (tru "A data app with this slug already exists.")])- (data-apps.db/insert-data-app-returning-pk! row)))+ (let [collection (collections/create-collection! {:name (collection-name slug)+ :namespace (name collections/data-apps-ns)})]+ (data-apps.db/insert-data-app-returning-pk! (assoc row :resource_collection_id (:id collection))))))diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/db.clj b/enterprise/backend/src/metabase_enterprise/data_apps/db.cljindex 9659e39cdb0..3c0094d21f6 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/db.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/db.clj@@ -106,11 +106,6 @@[collection-id](t2/select-one :model/Collection :id collection-id))-(defn update-resource-collection!- "Apply `changes` to the resource collection with `collection-id`."- [collection-id changes]- (t2/update! :model/Collection :id collection-id changes))-(defn delete-resource-collection!"Delete the resource collection with `collection-id`."[collection-id]diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj b/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.cljindex f82141e0aee..9dcf4048b79 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/models/data_app.clj@@ -2,7 +2,6 @@(:require[metabase-enterprise.data-apps.config :as data-app.config][metabase-enterprise.data-apps.db :as data-apps.db]- [metabase-enterprise.data-apps.resources :as data-app.resources][metabase-enterprise.data-apps.schema :as data-apps.schema][metabase.api.common :as api][metabase.events.core :as events]@@ -66,7 +65,7 @@(contains? row :bundle) (assoc :bundle_hash (some-> ^bytes (:bundle row) bytes-hash))))(defn- prepare- "The DataApp `row` normalized and validated against `schema`, with the hash of any bundle it sets."+ "The DataApp `row` normalized and validated against `schema`."[schema row](->> row(lib/normalize schema)@@ -74,9 +73,9 @@with-bundle-hash))(defn- check-resource-collection- "Throws unless the Collection with `collection-id` can be the resource collection of the data app `app-id`: a- top-level collection of data apps, not archived, official, typed or the sample, that no other app has; remote sync may- sync it."+ "Throws unless the Collection with `collection-id` can be the resource collection of the data app `app-id`, nil for a+ new app: a top-level collection of data apps, not archived, official, typed or the sample, that no other app has;+ remote sync may sync it."[collection-id app-id](let [collection (data-apps.db/resource-collection collection-id)](when-not (and collection@@ -92,24 +91,24 @@(t2/define-before-insert :model/DataApp[data-app]- ;; Every app owns a resource collection from its first moment: one it is given (an import names the collection- ;; its manifest references) or one created here, so the column is never null.(let [row (prepare ::data-apps.schema/data-app.insert data-app)]- (some-> (:resource_collection_id row) (check-resource-collection nil))- (cond-> row- (nil? (:resource_collection_id row))- (assoc :resource_collection_id (:id (data-app.resources/create-resource-collection! row))))))+ (when-not (:resource_collection_id row)+ (throw (ex-info (tru "A data app needs a collection of its own.")+ {:status-code 400})))+ (check-resource-collection (:resource_collection_id row) nil)+ row))(t2/define-before-update :model/DataApp[data-app](let [changes (t2/changes data-app)]- ;; A missing collection (the column is nullable, so the row survives its collection's deletion) may be set- ;; again; one the app has is never replaced or cleared.- (when (and (contains? changes :resource_collection_id)- (some? (:resource_collection_id (t2/original data-app))))- (throw (ex-info (tru "A data app''s resource collection cannot be changed.")+ (when (contains? changes :resource_collection_id)+ (when (some? (:resource_collection_id (t2/original data-app)))+ (throw (ex-info (tru "A data app''s resource collection cannot be changed.")+ {:status-code 400, :data-app-id (:id data-app)})))+ (check-resource-collection (:resource_collection_id changes) (:id data-app)))+ (when (contains? changes :bundle_hash)+ (throw (ex-info (tru "A data app''s bundle hash is computed from its bundle."){:status-code 400, :data-app-id (:id data-app)})))- (some-> (:resource_collection_id changes) (check-resource-collection (:id data-app)))(merge data-app (some->> changes (prepare ::data-apps.schema/data-app.update)))));; Reads always see `allowed_hosts` as a vector, never nil — a row synced before@@ -139,15 +138,11 @@[_model _instance]api/*is-superuser?*)-(t2/define-after-insert :model/DataApp- [app]- (merge app (data-app.resources/ensure-resources! app)))-;; The collection goes first, while the row still references it: the reference is nullable so the database can clear;; it, and the collection's own hooks delete what it holds and the grants on it.(t2/define-before-delete :model/DataApp[app]- (data-app.resources/delete-resources! app)+ (some-> (:resource_collection_id app) data-apps.db/delete-resource-collection!)(search/delete! :model/DataApp [(str (:id app))]))(search/define-spec "data-app"@@ -237,10 +232,7 @@(throw (ex-info (tru "A data app named \"{0}\" already exists on this instance. Delete it, or give the app in the repository another slug."(:slug ingested)){:status-code 400})))- (let [app (serdes/default-load-one! ingested maybe-local)]- (when maybe-local- (data-app.resources/ensure-resources! app))- app))+ (serdes/default-load-one! ingested maybe-local))(defenterprise data-app-collection-ids"The resource collections of the data apps, which hold the copies an app runs."diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/resources.clj b/enterprise/backend/src/metabase_enterprise/data_apps/resources.cljdeleted file mode 100644index 25b4923cc57..00000000000--- a/enterprise/backend/src/metabase_enterprise/data_apps/resources.clj+++ /dev/null@@ -1,62 +0,0 @@-(ns metabase-enterprise.data-apps.resources- "Lifecycle for the resource collection owned by a data app."- (:require- [metabase-enterprise.data-apps.db :as data-apps.db]- [metabase.collections.core :as collection]- [metabase.request.core :as request]))--(set! *warn-on-reflection* true)--(defn- resource-name [app]- (format "Data App: %s" (:name app)))--(defn create-resource-collection!- "Create the resource collection of `app`, a root collection in the `data-apps` namespace, as the app is inserted.- Created like any collection, so its events are published; as admin, since an import has no user bound."- [app]- (request/as-admin- (collection/create-collection! {:name (resource-name app)- :namespace (name collection/data-apps-ns)})))--(defn- restore-trashed-collection!- "Bring `collection` back out of the trash, with everything archived alongside it.-- Trashing it archives every copy inside, and an app is served from those copies,- so a sync that left it there would report success over an app whose viewers see- nothing. It goes back to the root, where the app created it: restoring in place- fails outright when an ancestor is still in the trash.-- The restore runs with full permissions because a repository import reaches this- from a scheduled task, where no user is bound; the collection is the app's own,- so there is no one else's decision to weigh."- [collection]- (when (:archived collection)- (request/as-admin- (collection/archive-or-unarchive-collection! collection- {:archived false, :parent_id nil}))))--(defn- resource-collection!- "The resource collection of `app`, created again if the app's was deleted out from under it: the reference is- nullable so that deleting the app can delete the collection first, which also lets the collection go alone."- [app]- (or (some->> (:resource_collection_id app)- (data-apps.db/resource-collection))- (let [collection (create-resource-collection! app)]- (data-apps.db/update-data-app! (:id app) {:resource_collection_id (:id collection)})- collection)))--(defn ensure-resources!- "Create, rename, or restore the resource collection of `app`, and return `{:resource_collection_id id}`."- [app]- (let [app (data-apps.db/data-app (:id app))- collection (resource-collection! app)]- (data-apps.db/update-resource-collection! (:id collection)- {:name (resource-name app)})- (restore-trashed-collection! collection)- {:resource_collection_id (:id collection)}))--(defn delete-resources!- "Delete the app collection."- [{:keys [resource_collection_id]}]- (when resource_collection_id- (data-apps.db/delete-resource-collection! resource_collection_id)))diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj b/enterprise/backend/src/metabase_enterprise/data_apps/schema.cljindex b81b6477d24..8c7ae3b2392 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/schema.clj@@ -95,10 +95,12 @@[:merge::data-app.update[:map {:closed true}- [:id ms/PositiveInt]]])+ [:id ms/PositiveInt]+ [:bundle_hash {:optional true} [:maybe :string]]]])(mr/def ::data-app.update- "What an update (or insert) of a DataApp accepts: every column of `:data_app` except `id`, all optional."+ "What an update (or insert) of a DataApp accepts: every column of `:data_app` except `id` and the `bundle_hash` the+ model computes, all optional."[:map {:closed true}[:entity_id {:optional true} [:maybe :string]][:name {:optional true} ::slug]@@ -106,7 +108,6 @@[:allowed_hosts {:optional true} ::allowed-hosts][:bundle_path {:optional true} ::bundle-path][:bundle {:optional true} [:maybe ::bundle]]- [:bundle_hash {:optional true} [:maybe :string]][:enabled {:optional true} [:maybe :boolean]][:created_at {:optional true} [:maybe ms/TemporalInstant]][:updated_at {:optional true} [:maybe ms/TemporalInstant]]diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/api_test.cljindex 07ef654bb96..58b059ebc08 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj@@ -5,9 +5,8 @@[metabase-enterprise.data-apps.apps :as data-apps.apps][metabase-enterprise.data-apps.config :as data-app.config][metabase-enterprise.data-apps.group-access :as group-access]- [metabase-enterprise.data-apps.resources :as data-app.resources][metabase-enterprise.data-apps.test-util :as data-apps.tu]- [metabase.actions.core :as actions]+ [metabase.events.core :as events][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata][metabase.permissions.core :as perms]@@ -19,12 +18,9 @@;;; ---------------------------------------------- Helpers -----------------------------------------------(defn- create-app! []- (t2/insert! :model/DataApp- :name "demo"- :display_name "Demo"- :bundle_path "index.js"- :bundle (.getBytes "BUNDLE" "UTF-8")))+(def ^:private app-bundle+ "The bundle of the apps these tests serve."+ (.getBytes "BUNDLE" "UTF-8"))(def ^:private app-request{:name "demo"@@ -41,196 +37,167 @@;; a thread-local `binding`).(mt/test-helpers-set-global-values!(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (create-app!)- (let [app (t2/select-one :model/DataApp :name "demo")- permission_group_id (:id (t2/insert-returning-instance! :model/PermissionsGroup {:name "Finches"}))]- (testing "a user who cannot read the app's collection cannot open the app"- (mt/user-http-request :rasta :get 403 "ee/apps/demo"))- (testing "a member can open a data app"- (group-access/add-groups! app [permission_group_id])- (perms/add-user-to-group! (mt/user->id :rasta) permission_group_id)- (is (str/includes?- (str (mt/user-real-request :rasta :get 200 "ee/apps/demo/bundle"))- "BUNDLE"))))+ (mt/with-temp [:model/DataApp app {:bundle app-bundle}+ :model/PermissionsGroup {group-id :id} {}]+ (testing "a user who cannot read the app's collection cannot open the app"+ (mt/user-http-request :rasta :get 403 (data-apps.tu/app-url app)))+ (testing "a member can open a data app"+ (group-access/add-groups! app [group-id])+ (perms/add-user-to-group! (mt/user->id :rasta) group-id)+ (is (str/includes? (str (mt/user-real-request :rasta :get 200 (data-apps.tu/app-url app "bundle")))+ "BUNDLE")))(testing "a non-superuser is still forbidden from managing data apps"(is (= "You don't have permissions to do that."(mt/user-http-request :rasta :get 403 "ee/apps/repo-status")))(is (= "You don't have permissions to do that."- (mt/user-http-request :rasta :put 403 "ee/apps/demo" {:enabled false}))))))))+ (mt/user-http-request :rasta :put 403 (data-apps.tu/app-url app) {:enabled false}))))))))(deftest superuser-can-manage-and-view-test(mt/test-helpers-set-global-values!(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (create-app!)- (let [app (t2/select-one :model/DataApp :name "demo")- {:keys [resource_collection_id]} app]- (is (not (some #(= resource_collection_id (:id %))- (mt/user-http-request :rasta :get 200 "collection")))))+ (mt/with-temp [:model/DataApp app {:bundle app-bundle}]+ (is (not (some #(= (:resource_collection_id app) (:id %))+ (mt/user-http-request :rasta :get 200 "collection"))))(testing "a superuser can list, read metadata, and serve the bundle"- (is (=? [{:name "demo" :display_name "Demo"}]+ (is (=? [{:name (:name app)}](mt/user-http-request :crowberto :get 200 "ee/apps")))- (is (=? {:name "demo"- :resource_collection_id pos-int?}- (mt/user-http-request :crowberto :get 200 "ee/apps/demo")))- (is (str/includes?- (str (mt/user-real-request :crowberto :get 200 "ee/apps/demo/bundle"))- "BUNDLE")))))))+ (is (=? {:name (:name app) :resource_collection_id (:resource_collection_id app)}+ (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app))))+ (is (str/includes? (str (mt/user-real-request :crowberto :get 200 (data-apps.tu/app-url app "bundle")))+ "BUNDLE")))))))(deftest read-only-remote-sync-blocks-data-app-changes-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup :model/RemoteSyncObject]- (create-app!)- (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"- remote-sync-type :read-only]- (testing "a read-only instance refuses to create, change, or delete an app"- (mt/user-http-request :crowberto :post 403 "ee/apps" (assoc app-request :name "other"))- (is (not (t2/exists? :model/DataApp :name "other")))- (mt/user-http-request :crowberto :put 403 "ee/apps/demo" {:display_name "Renamed"})- (mt/user-http-request :crowberto :put 403 "ee/apps/demo" {:enabled false :display_name "Renamed"})- (mt/user-http-request :crowberto :delete 403 "ee/apps/demo")- (is (=? {:display_name "Demo" :enabled true} (t2/select-one :model/DataApp :name "demo"))))- (testing "enabling and disabling stays allowed"- (is (=? {:enabled false}- (mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:enabled false})))))- (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"- remote-sync-type :read-write]- (testing "a read-write instance allows changes"- (is (=? {:display_name "Renamed"}- (mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:display_name "Renamed"}))))))))+ (mt/with-model-cleanup [:model/RemoteSyncObject]+ (mt/with-temp [:model/DataApp app {}]+ (let [url (data-apps.tu/app-url app)]+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-type :read-only]+ (testing "a read-only instance refuses to create, change, or delete an app"+ (mt/user-http-request :crowberto :post 403 "ee/apps" (assoc app-request :name "other"))+ (is (not (t2/exists? :model/DataApp :name "other")))+ (mt/user-http-request :crowberto :put 403 url {:display_name "Renamed"})+ (mt/user-http-request :crowberto :put 403 url {:enabled false :display_name "Renamed"})+ (mt/user-http-request :crowberto :delete 403 url)+ (is (=? {:display_name (:display_name app) :enabled true} (t2/select-one :model/DataApp :name (:name app)))))+ (testing "enabling and disabling stays allowed"+ (is (=? {:enabled false}+ (mt/user-http-request :crowberto :put 200 url {:enabled false})))))+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-type :read-write]+ (testing "a read-write instance allows changes"+ (is (=? {:display_name "Renamed"}+ (mt/user-http-request :crowberto :put 200 url {:display_name "Renamed"}))))))))))(deftest enabling-a-data-app-is-not-a-synced-change-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup :model/RemoteSyncObject]- (create-app!)- (let [app-id (t2/select-one-pk :model/DataApp :name "demo")]- (mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:enabled false})- (is (not (t2/exists? :model/RemoteSyncObject :model_type "DataApp" :model_id app-id)))- (mt/user-http-request :crowberto :put 200 "ee/apps/demo" {:display_name "Renamed"})- (is (=? {:model_name "demo" :status "update"}- (t2/select-one :model/RemoteSyncObject :model_type "DataApp" :model_id app-id)))))))+ (mt/with-model-cleanup [:model/RemoteSyncObject]+ (mt/with-temp [:model/DataApp app {}]+ (mt/user-http-request :crowberto :put 200 (data-apps.tu/app-url app) {:enabled false})+ (is (not (t2/exists? :model/RemoteSyncObject :model_type "DataApp" :model_id (:id app))))+ (mt/user-http-request :crowberto :put 200 (data-apps.tu/app-url app) {:display_name "Renamed"})+ (is (=? {:model_name (:name app) :status "update"}+ (t2/select-one :model/RemoteSyncObject :model_type "DataApp" :model_id (:id app))))))))(deftest data-app-write-endpoints-require-feature-token-test(mt/with-premium-features #{}(mt/user-http-request :crowberto :post 402 "ee/apps" {:name "demo"})))+(defn- update-venues-query+ "A native query that renames every venue."+ []+ (lib/native-query (mt/metadata-provider) "UPDATE venues SET name = 'x'"))+(deftest group-with-access-reaches-only-copied-actions-test(testing "an action is reachable exactly when it lives in the data app collection"(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (mt/with-non-admin-groups-no-root-collection-perms- (let [{:keys [resource_collection_id] :as app}- (first (t2/insert-returning-instances! :model/DataApp- {:name "action-perms-app"- :display_name "Action perms app"- :bundle_path "data_apps/action-perms-app/index.js"}))- permission_group_id (:id (t2/insert-returning-instance! :model/PermissionsGroup {:name "Action readers"}))- _ (group-access/add-groups! app [permission_group_id])]- (perms/add-user-to-group! (mt/user->id :rasta) permission_group_id)- ;; an action on no model lives in a data actions collection, as a data app runs them- (mt/with-temp [:model/Collection {source-collection-id :id} {:namespace :data-actions}]- ;; A query action on no model, as a data app runs them, and its copy in the app's own collection.- (let [action {:name "Rename venue"- :type :query- :database_id (mt/id)- :dataset_query (lib/native-query (mt/metadata-provider) "UPDATE venues SET name = 'x'")}- source-id (actions/insert! (assoc action :collection_id source-collection-id))- copied-id (actions/insert! (assoc action :collection_id resource_collection_id))]- (testing "the group with access reads the action copied into the app collection"- (is (=? {:id copied-id}- (mt/user-http-request :rasta :get 200 (str "action/" copied-id)))))- (testing "the same group cannot read the source action it was copied from"- (is (= "You don't have permissions to do that."- (mt/user-http-request :rasta :get 403 (str "action/" source-id)))))- (testing "a superuser still reads both"- (is (=? {:id source-id}- (mt/user-http-request :crowberto :get 200 (str "action/" source-id)))))))))))))+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-temp [:model/DataApp {app-collection-id :resource_collection_id :as app} {}+ :model/PermissionsGroup {group-id :id} {}+ :model/Collection {source-collection-id :id} {:namespace :data-actions}+ :model/Action {source-id :id} {:type :query+ :collection_id source-collection-id}+ :model/QueryAction _ {:action_id source-id+ :database_id (mt/id)+ :dataset_query (update-venues-query)}+ :model/Action {copied-id :id} {:type :query+ :collection_id app-collection-id}+ :model/QueryAction _ {:action_id copied-id+ :database_id (mt/id)+ :dataset_query (update-venues-query)}]+ (group-access/add-groups! app [group-id])+ (perms/add-user-to-group! (mt/user->id :rasta) group-id)+ (testing "the group with access reads the action copied into the app collection"+ (is (=? {:id copied-id}+ (mt/user-http-request :rasta :get 200 (str "action/" copied-id)))))+ (testing "the same group cannot read the source action it was copied from"+ (is (= "You don't have permissions to do that."+ (mt/user-http-request :rasta :get 403 (str "action/" source-id)))))+ (testing "a superuser still reads both"+ (is (=? {:id source-id}+ (mt/user-http-request :crowberto :get 200 (str "action/" source-id))))))))))(deftest outdated-apps-are-hidden-from-users-and-badged-for-admins-test(mt/test-helpers-set-global-values!(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (t2/insert! :model/DataApp :name "old" :display_name "Old" :bundle_path "data_apps/old/index.js"- :bundle (.getBytes "BUNDLE" "UTF-8") :bundle_hash "abc123" :version 1)- (t2/insert! :model/DataApp :name "current" :display_name "Current" :bundle_path "data_apps/current/index.js"- :bundle (.getBytes "BUNDLE" "UTF-8") :bundle_hash "def456" :version 2)- (let [group-id (:id (t2/insert-returning-instance! :model/PermissionsGroup {:name "Data app test group"}))]- (perms/add-user-to-group! (mt/user->id :rasta) group-id)- (doseq [slug ["old" "current"]- :let [app (t2/select-one :model/DataApp :name slug)]]- (group-access/add-groups! app [group-id])))+ (mt/with-temp [:model/DataApp old {:version 1}+ :model/DataApp current {:bundle app-bundle :version 2}+ :model/PermissionsGroup {group-id :id} {}]+ (perms/add-user-to-group! (mt/user->id :rasta) group-id)+ (doseq [app [old current]]+ (group-access/add-groups! app [group-id]))(with-redefs [data-app.config/supported-app-version 2](testing "a regular user is never told about the outdated app in a list"- (is (= [{:id (t2/select-one-pk :model/DataApp :name "current") :name "current" :display_name "Current"- :enabled true :outdated false}]+ (is (= [{:id (:id current) :name (:name current) :display_name (:display_name current) :enabled true :outdated false}](mt/user-http-request :rasta :get 200 "ee/apps"))))(testing "for a regular user, opening an outdated app is a 409 that says what to do"- (doseq [url ["ee/apps/old" "ee/apps/old/bundle"]]+ (doseq [url [(data-apps.tu/app-url old) (data-apps.tu/app-url old "bundle")]](is (=? {:error-code "data-app-outdated":message #"This app was built for version 1 of data apps.*"}(mt/user-http-request :rasta :get 409 url)))))(testing "the current app still opens"- (is (= {:id (t2/select-one-pk :model/DataApp :name "current") :name "current" :display_name "Current"- :enabled true :outdated false}- (mt/user-http-request :rasta :get 200 "ee/apps/current")))- (is (str/includes?- (str (mt/user-real-request :crowberto :get 200 "ee/apps/current/bundle"))- "BUNDLE")))+ (is (= {:id (:id current) :name (:name current) :display_name (:display_name current) :enabled true :outdated false}+ (mt/user-http-request :rasta :get 200 (data-apps.tu/app-url current))))+ (is (str/includes? (str (mt/user-real-request :crowberto :get 200 (data-apps.tu/app-url current "bundle")))+ "BUNDLE")))(testing "an admin sees the outdated app flagged, and can still read it to manage its groups"- (is (=? [{:name "current" :version 2 :outdated false}- {:name "old" :version 1 :outdated true}]- (mt/user-http-request :crowberto :get 200 "ee/apps")))- (is (=? {:name "old" :version 1 :outdated true}- (mt/user-http-request :crowberto :get 200 "ee/apps/old"))))+ (is (=? #{{:name (:name current) :version 2 :outdated false}+ {:name (:name old) :version 1 :outdated true}}+ (set (map #(select-keys % [:name :version :outdated])+ (mt/user-http-request :crowberto :get 200 "ee/apps")))))+ (is (=? {:name (:name old) :version 1 :outdated true}+ (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url old)))))(testing "nobody gets an outdated bundle"(is (=? {:error-code "data-app-outdated"}- (mt/user-http-request :crowberto :get 409 "ee/apps/old/bundle"))))+ (mt/user-http-request :crowberto :get 409 (data-apps.tu/app-url old "bundle")))))(testing "a management response carries the flag too"- (is (=? {:name "old" :outdated true}- (mt/user-http-request :crowberto :put 200 "ee/apps/old" {:enabled false})))))))))+ (is (=? {:name (:name old) :outdated true}+ (mt/user-http-request :crowberto :put 200 (data-apps.tu/app-url old) {:enabled false})))))))))(deftest bundle-includes-allowed-hosts-header-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (t2/insert! :model/DataApp- :name "demo"- :display_name "Demo"- :bundle_path "data_apps/demo/index.js"- :bundle (.getBytes "BUNDLE" "UTF-8")- :bundle_hash "abc123"- :allowed_hosts ["https://api.example.com"])- ;; publish the app so its bundle is served (crowberto, as a superuser, can read the collection)- (data-app.resources/ensure-resources! (t2/select-one :model/DataApp :name "demo"))+ (mt/with-temp [:model/DataApp app {:bundle app-bundle :allowed_hosts ["https://api.example.com"]}](testing "the bundle response includes only the app's configured allowed hosts"- (let [resp (mt/user-http-request-full-response :crowberto :get 200 "ee/apps/demo/bundle")]+ (let [resp (mt/user-http-request-full-response :crowberto :get 200 (data-apps.tu/app-url app "bundle"))](is (= (json/encode ["https://api.example.com"])(get-in resp [:headers "X-Metabase-Data-App-Allowed-Hosts"]))))))))(deftest list-includes-allowed-hosts-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (t2/insert! :model/DataApp- :name "withhosts" :display_name "With"- :bundle_path "data_apps/withhosts/index.js"- :allowed_hosts ["https://api.example.com"])- ;; inserted without the column → stored NULL, exercising the read coercion- (t2/insert! :model/DataApp- :name "nohosts" :display_name "No"- :bundle_path "data_apps/nohosts/index.js")- (testing "the list endpoint returns allowed_hosts, always a list (NULL → [])"+ (mt/with-temp [:model/DataApp with-hosts {:allowed_hosts ["https://api.example.com"]}+ :model/DataApp without-hosts {}]+ (testing "the list endpoint returns allowed_hosts, always a list, also for an app stored with NULL (→ [])"(let [by-name (->> (mt/user-http-request :crowberto :get 200 "ee/apps")(into {} (map (juxt :name identity))))]- (is (= ["https://api.example.com"]- (get-in by-name ["withhosts" :allowed_hosts])))- (is (= [] (get-in by-name ["nohosts" :allowed_hosts]))))))))+ (is (= ["https://api.example.com"] (get-in by-name [(:name with-hosts) :allowed_hosts])))+ (is (= [] (get-in by-name [(:name without-hosts) :allowed_hosts]))))))));;; ------------------------------------------------- Create & update -------------------------------------------------(deftest create-endpoint-test(mt/test-helpers-set-global-values!(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/with-model-cleanup [:model/DataApp :model/Collection](testing "a non-superuser cannot create an app"(is (= "You don't have permissions to do that."(mt/user-http-request :rasta :post 403 "ee/apps" app-request))))@@ -245,7 +212,12 @@(mt/user-http-request :crowberto :post 200 "ee/apps"(assoc app-request :bundle_path "./dist/index.js" :bundle "BUNDLE"))))(testing "with an empty bundle, whatever the request carries"- (is (= 0 (alength ^bytes (t2/select-one-fn :bundle :model/DataApp :name "demo"))))))+ (is (= 0 (alength ^bytes (t2/select-one-fn :bundle :model/DataApp :name "demo")))))+ (testing "with its own collection in the data-apps namespace, which no group but Administrators reads"+ (let [collection-id (t2/select-one-fn :resource_collection_id :model/DataApp :name "demo")]+ (is (=? {:name "Data App: demo" :namespace :data-apps :location "/"}+ (t2/select-one :model/Collection :id collection-id)))+ (is (= #{} (perms/collection-read-access-group-ids collection-id))))))(testing "a taken slug is refused"(is (= "A data app with this slug already exists."(mt/user-http-request :crowberto :post 409 "ee/apps" app-request))))@@ -258,10 +230,22 @@(mt/user-http-request :crowberto :post 400 "ee/apps" (merge app-request {:name "other"} bad))(is (not (t2/exists? :model/DataApp :name "other")) (str "should refuse: " (pr-str bad)))))))))+(deftest create-endpoint-publishes-collection-creation-test+ (testing "the app's collection is created the way any collection is, so what listens to collection events (remote+ sync, the activity log) sees it"+ (mt/with-premium-features #{:data-apps}+ (mt/with-model-cleanup [:model/DataApp :model/Collection]+ (let [created (atom [])]+ (mt/with-dynamic-fn-redefs [events/publish-event! (fn [topic {:keys [object]}]+ (when (= :event/collection-create topic)+ (swap! created conj (:id object))))]+ (mt/user-http-request :crowberto :post 200 "ee/apps" app-request))+ (is (= [(t2/select-one-fn :resource_collection_id :model/DataApp :name "demo")] @created)))))))+(deftest update-endpoint-test(mt/test-helpers-set-global-values!(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/with-model-cleanup [:model/DataApp :model/Collection](mt/user-http-request :crowberto :post 200 "ee/apps" app-request)(let [bundle-hash #(t2/select-one-fn :bundle_hash :model/DataApp :name "demo")](testing "a non-superuser cannot update an app"@@ -285,18 +269,17 @@(deftest delete-endpoint-requires-a-superuser-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection]- (create-app!)- (mt/with-temp [:model/PermissionsGroup {group-id :id} {}- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta), :group_id group-id}]- (let [app (t2/select-one :model/DataApp :name "demo")]- (group-access/add-groups! app [group-id]))- (testing "a user with access can open the app"- (mt/user-http-request :rasta :get 200 "ee/apps/demo"))- (testing "a user with access cannot delete the app"- (is (= "You don't have permissions to do that."- (mt/user-http-request :rasta :delete 403 "ee/apps/demo")))- (is (t2/exists? :model/DataApp :name "demo")))))))+ (mt/with-temp [:model/DataApp app {}+ :model/PermissionsGroup {group-id :id} {}+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta)+ :group_id group-id}]+ (group-access/add-groups! app [group-id])+ (testing "a user with access can open the app"+ (mt/user-http-request :rasta :get 200 (data-apps.tu/app-url app)))+ (testing "a user with access cannot delete the app"+ (is (= "You don't have permissions to do that."+ (mt/user-http-request :rasta :delete 403 (data-apps.tu/app-url app))))+ (is (t2/exists? :model/DataApp :id (:id app)))))))(deftest delete-endpoint-404s-for-a-missing-app-test(mt/with-premium-features #{:data-apps}@@ -304,28 +287,26 @@(deftest delete-endpoint-preserves-groups-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (create-app!)- (mt/with-temp [:model/PermissionsGroup group {}]- (let [app (t2/select-one :model/DataApp :name "demo")- {collection-id :resource_collection_id} app]- (data-apps.tu/with-published-tables! [(mt/id :venues)]- (mt/with-temp [:model/Card {card-id :id} {:collection_id collection-id :dataset_query (data-apps.tu/venues-query)}]- (group-access/add-groups! app [(:id group)])- (mt/user-http-request :crowberto :delete 204 "ee/apps/demo")- (is (not (t2/exists? :model/DataApp :id (:id app))))- (is (not (t2/exists? :model/Collection :id collection-id)))- (is (not (t2/exists? :model/Card :id card-id)))- (is (not (t2/exists? :model/Permissions :group_id (:id group)- :object (perms/collection-read-path collection-id))))- (is (t2/exists? :model/PermissionsGroup :id (:id group))))))))))+ (data-apps.tu/with-published-tables! [(mt/id :venues)]+ (mt/with-temp [:model/DataApp app {}+ :model/PermissionsGroup {group-id :id} {}+ :model/Card {card-id :id} {:collection_id (:resource_collection_id app)+ :dataset_query (data-apps.tu/venues-query)}]+ (group-access/add-groups! app [group-id])+ (mt/user-http-request :crowberto :delete 204 (data-apps.tu/app-url app))+ (is (not (t2/exists? :model/DataApp :id (:id app))))+ (is (not (t2/exists? :model/Collection :id (:resource_collection_id app))))+ (is (not (t2/exists? :model/Card :id card-id)))+ (is (not (t2/exists? :model/Permissions :group_id group-id+ :object (perms/collection-read-path (:resource_collection_id app)))))+ (is (t2/exists? :model/PermissionsGroup :id group-id))))));;; ----------------------------------------------------- API -----------------------------------------------------(deftest list-and-bundle-endpoints-test(mt/test-helpers-set-global-values!(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/with-model-cleanup [:model/DataApp :model/Collection](mt/user-http-request :crowberto :post 200 "ee/apps" app-request)(mt/user-http-request :crowberto :put 200 "ee/apps/demo/bundle" {:bundle "DEMOBUNDLE"})(testing "GET / lists the apps"@@ -351,7 +332,7 @@(deftest enable-disable-endpoint-test(mt/test-helpers-set-global-values!(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/with-model-cleanup [:model/DataApp :model/Collection](mt/user-http-request :crowberto :post 200 "ee/apps" app-request)(mt/user-http-request :crowberto :put 200 "ee/apps/demo/bundle" {:bundle "DEMOBUNDLE"})(testing "PUT /:slug can disable an app"@@ -399,14 +380,13 @@;; routed as a data app named "sandbox-host" and 404.(testing "the route is not shadowed by the /:slug route"(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (create-app!)+ (mt/with-temp [:model/DataApp _ {}](is (= 200 (:status (mt/user-http-request-full-response:crowberto :get 200 "ee/apps/sandbox-host"))))))))(deftest create-defaults-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/with-model-cleanup [:model/DataApp :model/Collection](testing "app created from its slug alone is enabled, with an empty bundle and its collection"(is (=? {:name "demo":display_name "demo"@@ -420,7 +400,7 @@(deftest replace-bundle-endpoint-test(mt/test-helpers-set-global-values!(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/with-model-cleanup [:model/DataApp :model/Collection](mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo" :enabled true})(testing "a non-superuser cannot replace the bundle"(mt/user-http-request :rasta :put 403 "ee/apps/demo/bundle" {:bundle "MINE"}))@@ -437,7 +417,7 @@(mt/with-premium-features #{:data-apps}(data-apps.tu/with-published-tables! [(mt/id :venues)](mt/with-actions-enabled- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup :model/Card :model/Action]+ (mt/with-model-cleanup [:model/DataApp :model/Collection :model/Card :model/Action](let [{collection-id :resource_collection_id} (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo"})query (lib/query (mt/metadata-provider) (lib.metadata/table (mt/metadata-provider) (mt/id :venues)))](testing "an admin creates a question in the app's collection"@@ -469,7 +449,7 @@(mt/with-premium-features #{:data-apps}(data-apps.tu/with-published-tables! [(mt/id :venues)](mt/with-actions-enabled- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup :model/Card :model/Action]+ (mt/with-model-cleanup [:model/DataApp :model/Collection :model/Card :model/Action](let [{collection-id :resource_collection_id} (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo"})mp (mt/metadata-provider)query (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))@@ -522,7 +502,7 @@(deftest disabled-apps-are-listed-only-to-superusers-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/with-model-cleanup [:model/DataApp :model/Collection](let [{:keys [id]} (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo" :enabled false})](group-access/add-groups! (t2/select-one :model/DataApp :id id) [(:id (perms/all-users-group))])(testing "a user with access doesn't see the disabled app"@@ -535,7 +515,7 @@(deftest apps-are-listed-only-to-users-who-can-read-their-collection-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/with-model-cleanup [:model/DataApp :model/Collection](mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo"})(testing "a user without a grant on the app's collection doesn't see the app"(is (not-any? #(= "demo" (:name %)) (mt/user-http-request :rasta :get 200 "ee/apps"))))@@ -546,7 +526,7 @@(deftest typed-schema-keys-are-the-same-on-every-instance-test(mt/with-premium-features #{:data-apps}(data-apps.tu/with-published-tables! [(mt/id :venues)]- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup :model/Card]+ (mt/with-model-cleanup [:model/DataApp :model/Collection :model/Card](let [{collection-id :resource_collection_id} (mt/user-http-request :crowberto :post 200 "ee/apps" {:name "demo"})mp (mt/metadata-provider)query (lib/query mp (lib.metadata/table mp (mt/id :venues)))diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/apps_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/apps_test.cljindex bc0d2f9bec7..31f269c2fc6 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/apps_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/apps_test.clj@@ -2,18 +2,15 @@(:require[clojure.test :refer :all][metabase-enterprise.data-apps.apps :as data-apps.apps]- [metabase.test :as mt]))+ [metabase.test :as mt]+ [toucan2.core :as t2]))(set! *warn-on-reflection* true)-(def ^:private app-row- {:name "sales"- :display_name "Sales"- :bundle_path "dist/index.js"- :bundle (.getBytes "BUNDLE" "UTF-8")})-(deftest create-app-refuses-a-taken-slug-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (data-apps.apps/create-app! app-row)- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"already exists"- (data-apps.apps/create-app! app-row)))))+ (mt/with-temp [:model/DataApp {app-name :name} {}]+ (let [collection-count (t2/count :model/Collection)]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"already exists"+ (data-apps.apps/create-app! {:name app-name :display_name "Sales" :bundle_path "dist/index.js"})))+ (testing "and creates no collection for it"+ (is (= collection-count (t2/count :model/Collection)))))))diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/csp_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/csp_test.cljindex c3379b7bea9..ad21b1d8a59 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/csp_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/csp_test.clj@@ -26,18 +26,14 @@(deftest data-app-connect-src-allowed-hosts-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp _ {:name "boba"- :display_name "Boba"- :bundle_path "data_apps/boba/dist/index.js"- :enabled true- :allowed_hosts ["https://api.example.com" "https://*.acme.com"]}]+ (mt/with-temp [:model/DataApp {app-name :name} {:allowed_hosts ["https://api.example.com" "https://*.acme.com"]}](testing "the data-app iframe document's connect-src includes the app's allowed_hosts (and keeps 'self')"- (let [cs (connect-src-for! "/embed/apps/boba")]+ (let [cs (connect-src-for! (str "/embed/apps/" app-name))](is (str/includes? cs "https://api.example.com"))(is (str/includes? cs "https://*.acme.com"))(is (str/includes? cs "'self'"))))(testing "a deeper sub-route of the same app gets them too"- (is (str/includes? (connect-src-for! "/embed/apps/boba/sub/route")+ (is (str/includes? (connect-src-for! (str "/embed/apps/" app-name "/sub/route"))"https://api.example.com")))(testing "the main app's connect-src does NOT include them"(is (not (str/includes? (connect-src-for! "/") "https://api.example.com"))))@@ -45,21 +41,17 @@(is (not (str/includes? (connect-src-for! "/embed/apps/nope")"api.example.com"))))(testing "a signed-out visitor never gets them, even for a real app"- (is (not (str/includes? (connect-src-for! "/embed/apps/boba" {})+ (is (not (str/includes? (connect-src-for! (str "/embed/apps/" app-name) {})"api.example.com")))))))(deftest data-app-connect-src-hosts-fn-test- (mt/with-temp [:model/DataApp _ {:name "boba"- :display_name "Boba"- :bundle_path "x"- :enabled true- :allowed_hosts ["https://api.example.com"]}]+ (mt/with-temp [:model/DataApp {app-id :id app-name :name} {:allowed_hosts ["https://api.example.com"]}](testing "with the :data-apps feature the EE impl returns the app's allowed_hosts"(mt/with-premium-features #{:data-apps}- (is (= ["https://api.example.com"] (mw.security/data-app-connect-src-hosts "boba")))+ (is (= ["https://api.example.com"] (mw.security/data-app-connect-src-hosts app-name)))(testing "including a disabled app's, whose document only superusers reach"- (t2/update! :model/DataApp {:name "boba"} {:enabled false})- (is (= ["https://api.example.com"] (mw.security/data-app-connect-src-hosts "boba"))))))+ (t2/update! :model/DataApp app-id {:enabled false})+ (is (= ["https://api.example.com"] (mw.security/data-app-connect-src-hosts app-name))))))(testing "without the feature it falls back to []"(mt/with-premium-features #{}- (is (= [] (mw.security/data-app-connect-src-hosts "boba")))))))+ (is (= [] (mw.security/data-app-connect-src-hosts app-name)))))))diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.cljindex f529e937cfd..7d8bad3971b 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_test.clj@@ -2,6 +2,7 @@(:require[clojure.test :refer :all][metabase-enterprise.data-apps.group-access :as group-access]+ [metabase-enterprise.data-apps.test-util :as data-apps.tu][metabase.api.macros :as api.macros][metabase.app-db.core :as mdb][metabase.collections.core :as collection]@@ -20,8 +21,8 @@(deftest visible-data-app-filter-select-outside-toucan-test(testing "the read rule query works in an app-DB query compiled outside Toucan, as the search index's queries are"(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp {app-id :id} {:name "birds" :display_name "Birds" :bundle_path "birds.js"}- :model/DataApp _ {:name "owls" :display_name "Owls" :bundle_path "owls.js"}+ (mt/with-temp [:model/DataApp {app-id :id} {}+ :model/DataApp _ {}:model/PermissionsGroup {group-id :id} {}:model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}](group-access/add-groups! (t2/select-one :model/DataApp :id app-id) [group-id])@@ -33,7 +34,7 @@(deftest added-groups-authorize-access-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp app {}:model/PermissionsGroup group {}](mt/with-current-user (mt/user->id :rasta)(is (not (mi/can-read? app))))@@ -52,10 +53,9 @@(deftest app-list-filters-readable-apps-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}- :model/DataApp disabled {:name "disabled" :display_name "Disabled" :bundle_path "disabled.js"- :enabled false}- :model/DataApp _ {:name "hidden" :display_name "Hidden" :bundle_path "hidden.js"}+ (mt/with-temp [:model/DataApp app {}+ :model/DataApp disabled {:enabled false}+ :model/DataApp _ {}:model/PermissionsGroup finches {}:model/PermissionsGroup owls {}](group-access/add-groups! app [(:id finches) (:id owls)])@@ -66,7 +66,7 @@(mt/with-current-user (mt/user->id :rasta)(list-apps {} {})(testing "a disabled app isn't listed to a non-superuser"- (is (= [{:id (:id app) :name "birds" :display_name "Birds" :enabled true :outdated false}]+ (is (= [{:id (:id app) :name (:name app) :display_name (:display_name app) :enabled true :outdated false}](list-apps {} {})))))))))(deftest read-access-agrees-across-entry-points-test@@ -74,8 +74,7 @@(mt/with-temporary-setting-values [use-tenants true](mt/with-temp [:model/Tenant tenant {:name "Tenant"}:model/User tenant-user {:tenant_id (:id tenant)}- :model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"- :bundle (.getBytes "BUNDLE" "UTF-8")}+ :model/DataApp app {:bundle (.getBytes "BUNDLE" "UTF-8")}:model/PermissionsGroup group {}](group-access/add-groups! app [(:id group)])(perms/add-user-to-group! (mt/user->id :rasta) (:id group))@@ -84,10 +83,10 @@[(mt/user->id :lucky) false false][(:id tenant-user) false false]]](testing (str "read access for user " user-id)- (is (= (if allowed? ["birds"] [])+ (is (= (if allowed? [(:name app)] [])(mapv :name (mt/user-http-request user-id :get 200 "ee/apps"))))- (mt/user-http-request user-id :get (if allowed? 200 403) "ee/apps/birds")- (mt/user-http-request user-id :get (if allowed? 200 403) "ee/apps/birds/bundle")+ (mt/user-http-request user-id :get (if allowed? 200 403) (data-apps.tu/app-url app))+ (mt/user-http-request user-id :get (if allowed? 200 403) (data-apps.tu/app-url app "bundle"))(mt/with-current-user user-id(is (= allowed? (boolean (mi/can-read? app)))))(is (= allowed? (contains? (t2/select-pks-set :model/DataApp@@ -97,40 +96,40 @@(deftest group-api-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp app {}:model/PermissionsGroup finches {:name "Finches"}:model/PermissionsGroup owls {:name "Owls"}:model/PermissionsGroup tenant {:is_tenant_group true}]- (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))+ (is (= [] (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "groups"))))(doseq [ids [[(:id finches) (:id (perms/admin-group))][(:id finches) (:id tenant)][](vec (range 1 102))]]- (mt/user-http-request :crowberto :post 400 "ee/apps/birds/groups" {:group_ids ids})+ (mt/user-http-request :crowberto :post 400 (data-apps.tu/app-url app "groups") {:group_ids ids})(is (empty? (access-group-ids app))))- (mt/user-http-request :crowberto :post 404 "ee/apps/birds/groups" {:group_ids [(:id finches) Integer/MAX_VALUE]})+ (mt/user-http-request :crowberto :post 404 (data-apps.tu/app-url app "groups") {:group_ids [(:id finches) Integer/MAX_VALUE]})(is (=? [{:id (:id finches) :name "Finches" :member_count 0}{:id (:id owls) :name "Owls" :member_count 0}]- (mt/user-http-request :crowberto :post 200 "ee/apps/birds/groups"+ (mt/user-http-request :crowberto :post 200 (data-apps.tu/app-url app "groups"){:group_ids [(:id finches) (:id finches) (:id owls)]})))(is (= #{(:id finches) (:id owls)} (access-group-ids app)))(is (= #{(perms/collection-read-path (:resource_collection_id app))}(t2/select-fn-set :object :model/Permissions :group_id (:id finches))))- (mt/user-http-request :crowberto :post 400 "ee/apps/birds/groups" {:group_ids [(:id finches)]})- (mt/user-http-request :crowberto :delete 204 (str "ee/apps/birds/groups/" (:id finches)))- (is (= [(:id owls)] (mapv :id (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups"))))+ (mt/user-http-request :crowberto :post 400 (data-apps.tu/app-url app "groups") {:group_ids [(:id finches)]})+ (mt/user-http-request :crowberto :delete 204 (data-apps.tu/app-url app "groups" (:id finches)))+ (is (= [(:id owls)] (mapv :id (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "groups")))))(doseq [[method path body] [[:get "groups" nil][:post "groups" {:group_ids [(:id finches)]}][:delete (str "groups/" (:id owls)) nil]]]- (apply mt/user-http-request :rasta method 403 (str "ee/apps/birds/" path) (when body [body]))))))+ (apply mt/user-http-request :rasta method 403 (data-apps.tu/app-url app path) (when body [body]))))))(deftest group-api-ignores-unknown-fields-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp app {}:model/PermissionsGroup group {}:model/PermissionsGroup ignored-group {}](is (=? [{:id (:id group)}]- (mt/user-http-request :crowberto :post 200 "ee/apps/birds/groups"+ (mt/user-http-request :crowberto :post 200 (data-apps.tu/app-url app "groups"){:group_ids [(:id group)]:user_ids [(mt/user->id :rasta)]:permission_group_ids [(:id ignored-group)]})))@@ -140,27 +139,27 @@(deftest group-access-without-collection-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp app {}:model/PermissionsGroup group {}](t2/delete! :model/Collection :id (:resource_collection_id app))(testing "an app without a collection lists no groups, and grants none without creating one"- (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))- (mt/user-http-request :crowberto :post 404 "ee/apps/birds/groups" {:group_ids [(:id group)]})- (mt/user-http-request :crowberto :delete 404 (str "ee/apps/birds/groups/" (:id group)))+ (is (= [] (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "groups"))))+ (mt/user-http-request :crowberto :post 404 (data-apps.tu/app-url app "groups") {:group_ids [(:id group)]})+ (mt/user-http-request :crowberto :delete 404 (data-apps.tu/app-url app "groups" (:id group)))(is (nil? (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))))))))(deftest group-with-access-display-name-test(mt/with-premium-features #{:data-apps :tenants}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}]+ (mt/with-temp [:model/DataApp app {}](group-access/add-groups! app [(:id (perms/all-users-group))])(doseq [[tenants? expected-name] [[false "All Users"] [true "All internal users"]]](mt/with-temporary-setting-values [use-tenants tenants?](is (= [expected-name]- (mapv :name (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))))))))+ (mapv :name (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "groups"))))))))))(deftest remove-group-with-trashed-collection-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp app {}:model/PermissionsGroup group {}](group-access/add-groups! app [(:id group)])(perms/add-user-to-group! (mt/user->id :rasta) (:id group))@@ -169,7 +168,7 @@(mt/with-dynamic-fn-redefs [perms/data-app-collection? (constantly false)](collection/archive-or-unarchive-collection!(t2/select-one :model/Collection :id collection-id) {:archived true})))- (mt/user-http-request :crowberto :delete 204 (str "ee/apps/birds/groups/" (:id group)))+ (mt/user-http-request :crowberto :delete 204 (data-apps.tu/app-url app "groups" (:id group)))(is (empty? (access-group-ids app)))(is (empty? (t2/select :model/Permissions :group_id (:id group):object [:in [(perms/collection-read-path collection-id)@@ -179,7 +178,7 @@(is (not (mi/can-read? app))))))))(deftest add-groups-rolls-back-grant-failure-test- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp app {}:model/PermissionsGroup group {}:model/PermissionsGroup failing-group {}](let [grant! (mt/original-fn #'perms/grant-collection-read-permissions!)]@@ -193,7 +192,7 @@(is (empty? (access-group-ids app))))))(deftest group-access-preserves-data-permissions-test- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp app {}:model/PermissionsGroup group {}](perms/set-database-permission! (:id group) (mt/id) :perms/create-queries :query-builder-and-native)(let [before (t2/select :model/DataPermissions :group_id (:id group))]@@ -205,7 +204,7 @@(is (= before (t2/select :model/DataPermissions :group_id (:id group))))))))(deftest group-deletion-cascades-test- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp app {}:model/PermissionsGroup group {}](group-access/add-groups! app [(:id group)])(t2/delete! :model/PermissionsGroup :id (:id group))@@ -214,14 +213,14 @@(deftest all-users-access-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}]+ (mt/with-temp [:model/DataApp app {}](group-access/add-groups! app [(:id (perms/all-users-group))])(mt/with-current-user (mt/user->id :rasta)(is (mi/can-read? app))))))(deftest disabled-app-access-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js" :enabled false}]+ (mt/with-temp [:model/DataApp app {:enabled false}](group-access/add-groups! app [(:id (perms/all-users-group))])(testing "a superuser reaches a disabled app"(mt/with-current-user (mt/user->id :crowberto)@@ -232,82 +231,82 @@(deftest group-endpoints-require-feature-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp app {}:model/PermissionsGroup group {}](group-access/add-groups! app [(:id group)])(let [collection-id (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))](mt/with-premium-features #{}(mt/user-http-request :crowberto :get 402 "ee/apps")- (mt/user-http-request :crowberto :get 402 "ee/apps/birds/groups")- (mt/user-http-request :crowberto :post 402 "ee/apps/birds/groups" {:group_ids [(:id group)]})- (mt/user-http-request :crowberto :delete 402 (str "ee/apps/birds/groups/" (:id group)))+ (mt/user-http-request :crowberto :get 402 (data-apps.tu/app-url app "groups"))+ (mt/user-http-request :crowberto :post 402 (data-apps.tu/app-url app "groups") {:group_ids [(:id group)]})+ (mt/user-http-request :crowberto :delete 402 (data-apps.tu/app-url app "groups" (:id group)))(is (= #{(:id group)} (access-group-ids app)))(is (t2/exists? :model/Permissions :group_id (:id group):object (perms/collection-read-path collection-id))))))))(deftest remove-group-after-it-becomes-ineligible-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp app {}:model/PermissionsGroup group {}](group-access/add-groups! app [(:id group)])(t2/update! :model/PermissionsGroup (:id group) {:is_tenant_group true})- (mt/user-http-request :rasta :delete 403 (str "ee/apps/birds/groups/" (:id group)))+ (mt/user-http-request :rasta :delete 403 (data-apps.tu/app-url app "groups" (:id group)))(is (= [(:id group)] (mapv :id (group-access/assigned-groups app))))- (mt/user-http-request :crowberto :delete 204 (str "ee/apps/birds/groups/" (:id group)))- (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))+ (mt/user-http-request :crowberto :delete 204 (data-apps.tu/app-url app "groups" (:id group)))+ (is (= [] (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "groups"))))(let [collection-id (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))](is (not (t2/exists? :model/Permissions :group_id (:id group):object (perms/collection-read-path collection-id))))))))(deftest admin-group-is-never-listed-or-revoked-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}]+ (mt/with-temp [:model/DataApp app {}](let [admin-id (:id (perms/admin-group))permissions (t2/select :model/Permissions :group_id admin-id)]- (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))- (mt/user-http-request :crowberto :delete 404 (str "ee/apps/birds/groups/" admin-id))+ (is (= [] (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "groups"))))+ (mt/user-http-request :crowberto :delete 404 (data-apps.tu/app-url app "groups" admin-id))(is (= permissions (t2/select :model/Permissions :group_id admin-id)))(mt/with-current-user (mt/user->id :crowberto)(is (mi/can-read? app)))))))(deftest remove-group-requires-matching-app-and-group-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}- :model/DataApp _ {:name "wrens" :display_name "Wrens" :bundle_path "wrens.js"}+ (mt/with-temp [:model/DataApp app {}+ :model/DataApp _ {}:model/PermissionsGroup group {}](group-access/add-groups! app [(:id group)])(doseq [[path status] [[(str "ee/apps/wrens/groups/" (:id group)) 404][(str "ee/apps/missing/groups/" (:id group)) 404]- ["ee/apps/birds/groups/2147483647" 404]- ["ee/apps/birds/groups/0" 400]- ["ee/apps/birds/groups/not-an-id" 400]]]+ [(data-apps.tu/app-url app "groups/2147483647") 404]+ [(data-apps.tu/app-url app "groups/0") 400]+ [(data-apps.tu/app-url app "groups/not-an-id") 400]]](mt/user-http-request :crowberto :delete status path))(is (= [(:id group)]- (mapv :id (mt/user-http-request :crowberto :get 200 "ee/apps/birds/groups")))))))+ (mapv :id (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "groups"))))))))(deftest collection-grants-are-the-app-access-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}]+ (mt/with-temp [:model/DataApp app {}](let [group-id (:id (perms/all-users-group))collection-id (:resource_collection_id app)](testing "a grant made on the collection is listed and admits the group's members"(perms/grant-collection-read-permissions! group-id collection-id)(is (= #{group-id} (access-group-ids app)))- (is (= {:id (:id app) :name "birds" :display_name "Birds" :enabled true :outdated false}- (mt/user-http-request :rasta :get 200 "ee/apps/birds"))))))))+ (is (= {:id (:id app) :name (:name app) :display_name (:display_name app) :enabled true :outdated false}+ (mt/user-http-request :rasta :get 200 (data-apps.tu/app-url app)))))))))(deftest tenant-users-cannot-inherit-app-access-test(mt/with-premium-features #{:data-apps :tenants}(mt/with-temp [:model/Tenant tenant {:name "Tenant"}:model/User user {:tenant_id (:id tenant)}- :model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}]+ :model/DataApp app {}](group-access/add-groups! app [(:id (perms/all-users-group))])(mt/with-current-user (:id user)(is (not (mi/can-read? app)))))))(deftest sso-membership-controls-app-access-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp app {}:model/PermissionsGroup group {}:model/User user {}](group-access/add-groups! app [(:id group)])diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.cljindex 5f5765b7d0d..0d8d52f68c2 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/group_access_warnings_test.clj@@ -18,7 +18,7 @@`table-ids`."[table-ids f](data-apps.tu/with-published-tables! [(mt/id :venues)]- (mt/with-temp [:model/DataApp app {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ (mt/with-temp [:model/DataApp app {}:model/Card card {:collection_id (:resource_collection_id app) :dataset_query (data-apps.tu/venues-query)}](mt/with-model-cleanup [:model/Dependency](when (seq table-ids)@@ -60,7 +60,7 @@(lib/aggregate venues (lib/distinct (some #(when (= (mt/id :categories :name) (:id %)) %)(lib/visible-columns venues)))))}:model/DataApp {collection-id :resource_collection_id :as app}- {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ {}:model/Card _ {:collection_id collection-id:dataset_query (lib/query mp (lib.metadata/table mp (mt/id :orders)))}:model/PermissionsGroup group {}]@@ -88,7 +88,7 @@"UPDATE VENUES v SET NAME = 'x' WHERE v.ID IN (SELECT v2.ID FROM VENUES v2 [[JOIN CHECKINS c ON c.VENUE_ID = v2.ID AND c.ID = {{id}}]])"]](testing sql(mt/with-temp [:model/DataApp {collection-id :resource_collection_id :as app}- {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ {}:model/PermissionsGroup group {}](do-with-query-action!collection-id sql@@ -107,7 +107,7 @@(mt/with-model-cleanup [:model/Dependency :model/DependencyStatus :model/AnalysisFinding](mt/with-temp [:model/NativeQuerySnippet snippet {:name "birds source" :content "VENUES"}:model/DataApp {collection-id :resource_collection_id :as app}- {:name "birds" :display_name "Birds" :bundle_path "birds.js"}+ {}:model/PermissionsGroup group {}](do-with-query-action!collection-id "DELETE FROM {{snippet: birds source}} WHERE ID = 1"@@ -149,10 +149,10 @@(deftest group-permission-warnings-ignore-groups-without-access-test(mt/with-premium-features #{:data-apps :advanced-permissions :sandboxes}(mt/with-no-data-perms-for-all-users!- (with-app-reading! [_app [(mt/id :venues) (mt/id :orders)]]+ (with-app-reading! [app [(mt/id :venues) (mt/id :orders)]](mt/with-temp [:model/PermissionsGroup group {}](perms/set-database-permission! (:id group) (mt/id) :perms/view-data :blocked)- (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))+ (is (= [] (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "group-permission-warnings")))))))))(deftest group-permission-warnings-include-inherited-access-test(mt/with-premium-features #{:data-apps :advanced-permissions :sandboxes}@@ -164,7 +164,7 @@(perms/set-table-permission! (perms/all-users-group) (mt/id :venues) :perms/view-data :unrestricted)(is (=? [{:group_id (:id group):missing_tables [{:id (mt/id :orders) :name "Orders" :database_id (mt/id)}]}]- (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))+ (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "group-permission-warnings")))))))))(deftest group-permission-warnings-preserve-permissions-test(mt/with-premium-features #{:data-apps :advanced-permissions :sandboxes}@@ -174,7 +174,7 @@(perms/set-database-permission! (:id group) (mt/id) :perms/view-data :blocked)(group-access/add-groups! app [(:id group)])(let [before (t2/select :model/DataPermissions)]- (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings")+ (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "group-permission-warnings"))(is (= before (t2/select :model/DataPermissions)))))))))(deftest group-permission-warnings-include-direct-access-test@@ -186,7 +186,7 @@(perms/set-database-permission! (:id group) (mt/id) :perms/view-data :blocked)(perms/set-table-permission! (:id group) (mt/id :venues) :perms/view-data :unrestricted)(perms/set-table-permission! (:id group) (mt/id :orders) :perms/view-data :unrestricted)- (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))+ (is (= [] (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "group-permission-warnings")))))))))(deftest group-permission-warnings-include-sandbox-access-test(mt/with-premium-features #{:data-apps :advanced-permissions :sandboxes}@@ -200,13 +200,13 @@{"PUBLIC" {(mt/id :venues) :unrestricted(mt/id :orders) :sandboxed}})(assoc :sandboxes [{:group_id (:id group) :table_id (mt/id :orders)}])))- (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings")))+ (is (= [] (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "group-permission-warnings"))))(mt/user-http-request :crowberto :put 200 "permissions/graph"(assoc-in (data-perms.graph/api-graph)[:groups (:id group) (mt/id) :view-data "PUBLIC" (mt/id :orders)]:blocked))(is (=? [{:group_id (:id group) :missing_tables [{:id (mt/id :orders)}]}]- (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))+ (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "group-permission-warnings")))))))))(deftest group-permission-warnings-require-admin-test(mt/with-premium-features #{:data-apps :advanced-permissions :sandboxes}@@ -214,7 +214,7 @@(with-app-reading! [app [(mt/id :venues) (mt/id :orders)]](mt/with-temp [:model/PermissionsGroup group {}](group-access/add-groups! app [(:id group)])- (mt/user-http-request :rasta :get 403 "ee/apps/birds/group-permission-warnings"))))))+ (mt/user-http-request :rasta :get 403 (data-apps.tu/app-url app "group-permission-warnings")))))))(deftest group-permission-warnings-without-dependencies-test(mt/with-premium-features #{:data-apps :advanced-permissions :sandboxes}@@ -222,7 +222,7 @@(with-app-reading! [app []](mt/with-temp [:model/PermissionsGroup group {}](group-access/add-groups! app [(:id group)])- (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))+ (is (= [] (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "group-permission-warnings")))))))))(deftest group-permission-warnings-missing-app-test(mt/with-premium-features #{:data-apps}@@ -253,13 +253,13 @@(assoc-in [:groups (:id group) (mt/id) :view-data] :impersonated)(assoc :impersonations [{:group_id (:id group) :db_id (mt/id):attribute "role"}])))- (is (= [] (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings")))+ (is (= [] (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "group-permission-warnings"))))(mt/user-http-request :crowberto :put 200 "permissions/graph"(assoc-in (data-perms.graph/api-graph)[:groups (:id group) (mt/id) :view-data]:blocked))(is (=? [{:group_id (:id group) :missing_tables [{:id (mt/id :venues)}]}]- (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))+ (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "group-permission-warnings")))))))))(deftest group-permission-warning-lookups-are-batched-test(mt/with-no-data-perms-for-all-users!@@ -280,8 +280,8 @@(mt/with-temp [:model/PermissionsGroup group {}](group-access/add-groups! app [(:id group)])(mt/with-premium-features #{}- (mt/user-http-request :crowberto :get 402 "ee/apps/birds/group-permission-warnings")- (mt/user-http-request :rasta :get 402 "ee/apps/birds/group-permission-warnings")))))+ (mt/user-http-request :crowberto :get 402 (data-apps.tu/app-url app "group-permission-warnings"))+ (mt/user-http-request :rasta :get 402 (data-apps.tu/app-url app "group-permission-warnings"))))))(deftest group-permission-warning-lookups-exclude-unrelated-permissions-test(mt/with-temp [:model/Database other-db {}@@ -331,4 +331,4 @@:database_id (:id aviary) :database_name "Aviary"}{:id (:id albatrosses) :name "Albatrosses" :schema nil:database_id (:id zoo) :database_name "Zoo"}]}]- (mt/user-http-request :crowberto :get 200 "ee/apps/birds/group-permission-warnings"))))))))+ (mt/user-http-request :crowberto :get 200 (data-apps.tu/app-url app "group-permission-warnings")))))))))diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/invariants_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/invariants_test.cljindex 98b04f1076c..30985d3640b 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/invariants_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/invariants_test.clj@@ -5,6 +5,7 @@[clojure.string :as str][clojure.test :refer :all][metabase-enterprise.data-apps.test-util :as data-apps.tu]+ [metabase.collections.models.collection :as collection][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata][metabase.test :as mt]@@ -16,8 +17,7 @@"Call `f` with the id of a data app's resource collection; deleting the app afterwards deletes what it holds."[f](mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp {collection-id :resource_collection_id}- {:name "birds" :display_name "Birds" :bundle_path "birds.js"}]+ (mt/with-temp [:model/DataApp {collection-id :resource_collection_id} {}](f collection-id))))(defn- reading-card@@ -219,16 +219,22 @@(testing "a remote-synced one is fine, as remote sync marks an app's collection"(is (pos-int? (app! "synced" synced-id))))(testing "a top-level collection of data apps is fine"- (let [app-id (app! "accepted" root-id)]- (testing "but not for a second app"- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"already belongs to another data app"- (app! "second" root-id))))- (testing "an app whose collection is gone is given one the same way"- (t2/query-one {:update :data_app :set {:resource_collection_id nil} :where [:= :id app-id]})- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"must be a top-level collection of data apps"- (t2/update! :model/DataApp app-id {:resource_collection_id default-id})))- (t2/update! :model/DataApp app-id {:resource_collection_id root-id})- (is (= root-id (t2/select-one-fn :resource_collection_id :model/DataApp :id app-id))))))))))))+ (is (pos-int? (app! "accepted" root-id)))+ (testing "but not for a second app"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"already belongs to another data app"+ (app! "second" root-id)))))))))))++(deftest app-collection-stays-at-namespace-root-test+ (testing "an app's collection lives in the data-apps namespace, so it can't be filed under a regular collection,+ where trashing the ancestor would take the app's copies with it"+ (mt/with-temp [:model/Collection {ancestor-id :id} {}+ :model/DataApp {collection-id :resource_collection_id} {}]+ (mt/with-test-user :crowberto+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"cannot move a data app's collection"+ (collection/move-collection!+ (t2/select-one :model/Collection :id collection-id)+ (collection/children-location (t2/select-one :model/Collection :id ancestor-id))))))+ (is (= "/" (t2/select-one-fn :location :model/Collection :id collection-id))))))(deftest content-of-data-apps-namespace-is-checked-before-its-app-loads-test(testing "a load may write a collection's content before the app that owns it, so the namespace decides"diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/models/data_app_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/models/data_app_test.cljindex c05c867dee6..ef9f95de157 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/models/data_app_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/models/data_app_test.clj@@ -6,30 +6,22 @@[metabase-enterprise.data-apps.db :as data-apps.db][metabase-enterprise.data-apps.models.data-app][metabase.api.common :as api]- [metabase.events.core :as events][metabase.models.interface :as mi]- [metabase.permissions.core :as perms][metabase.test :as mt][metabase.util.json :as json][toucan2.core :as t2]))(set! *warn-on-reflection* true)-(defn- insert-app! [& {:as extra}]- (t2/insert-returning-pk!- :model/DataApp- (merge {:name "m"- :display_name "M"- :bundle_path "index.js"- :bundle (.getBytes "BUNDLEBYTES" "UTF-8")}- extra)))+(def ^:private bundle-bytes+ "The bundle of the apps these tests create."+ (.getBytes "BUNDLEBYTES" "UTF-8"))(deftest bundle-hash-follows-the-bundle-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (let [id (insert-app! :bundle_hash "stale")- hash #(t2/select-one-fn :bundle_hash :model/DataApp id)+ (mt/with-temp [:model/DataApp {id :id} {:bundle bundle-bytes}]+ (let [hash #(t2/select-one-fn :bundle_hash :model/DataApp id)v1 (hash)]- (is (= 64 (count v1)) "the hash is computed from the bundle, not taken from the row")+ (is (= 64 (count v1)) "the insert computes the hash from the bundle")(t2/update! :model/DataApp id {:display_name "Renamed"})(is (= v1 (hash)) "a change that leaves the bundle alone keeps its hash")(t2/update! :model/DataApp id {:display_name "Renamed"})@@ -37,84 +29,66 @@(t2/update! :model/DataApp id {:bundle (.getBytes "OTHER" "UTF-8")})(is (not= v1 (hash)))(t2/update! :model/DataApp id {:bundle nil})- (is (nil? (hash))))))+ (is (nil? (hash)))+ (testing "the hash is never written directly"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"bundle hash is computed from its bundle"+ (t2/update! :model/DataApp id {:bundle_hash "stale"})))))))-(deftest insert-creates-the-resources-the-app-owns-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (let [{:keys [resource_collection_id]} (t2/select-one :model/DataApp (insert-app!))]- (is (=? {:name "Data App: m" :namespace :data-apps :location "/"}- (t2/select-one :model/Collection :id resource_collection_id))- "the collection is the app's own, in the data-apps namespace")- (is (= #{} (perms/collection-read-access-group-ids resource_collection_id))- "no group but Administrators can read the new collection"))))--(deftest insert-publishes-the-creation-of-the-collection-test- (testing "the collection is created the way any collection is, so what listens to collection events (remote sync,- the activity log) sees it"- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (let [created (atom [])]- (mt/with-dynamic-fn-redefs [events/publish-event! (fn [topic {:keys [object]}]- (when (= :event/collection-create topic)- (swap! created conj (:id object))))]- (let [id (insert-app!)]- (is (= [(t2/select-one-fn :resource_collection_id :model/DataApp id)] @created))))))))--(deftest insert-keeps-the-collection-it-is-given-test- (testing "an import names the collection its manifest references, so the insert links that one rather than creating"- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (mt/with-temp [:model/Collection {collection-id :id} {:name "Data App: m" :namespace :data-apps}]- (let [before (t2/count :model/Collection)- app (t2/select-one :model/DataApp (insert-app! :resource_collection_id collection-id))]- (is (= collection-id (:resource_collection_id app)))- (is (= before (t2/count :model/Collection)) "no second collection is created"))))))+(deftest insert-requires-resource-collection-test+ (testing "the API creates an app's collection, so an insert without one is refused"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"needs a collection of its own"+ (mt/with-temp [:model/DataApp _ {:name "m" :resource_collection_id nil}])))+ (is (not (t2/exists? :model/DataApp :name "m")))))(deftest the-resource-collection-cannot-change-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (mt/with-temp [:model/Collection {other-id :id} {:name "Other" :namespace :data-apps}]- (let [id (insert-app!)- collection-id (t2/select-one-fn :resource_collection_id :model/DataApp id)]- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"resource collection cannot be changed"- (t2/update! :model/DataApp id {:resource_collection_id other-id})))- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"resource collection cannot be changed"- (t2/update! :model/DataApp id {:resource_collection_id nil})))- (is (= collection-id (t2/select-one-fn :resource_collection_id :model/DataApp id)))- (testing "an update that names the same collection is not a change"- (t2/update! :model/DataApp id {:resource_collection_id collection-id :display_name "Renamed"})- (is (= "Renamed" (t2/select-one-fn :display_name :model/DataApp id))))- (testing "an app whose collection was deleted out from under it may be given one again"- (t2/delete! :model/Collection :id collection-id)- (is (nil? (t2/select-one-fn :resource_collection_id :model/DataApp id)) "the reference was cleared")- (t2/update! :model/DataApp id {:resource_collection_id other-id})- (is (= other-id (t2/select-one-fn :resource_collection_id :model/DataApp id))))))))+ (mt/with-temp [:model/Collection {other-id :id} {:name "Other" :namespace :data-apps}+ :model/DataApp {id :id collection-id :resource_collection_id} {}]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"resource collection cannot be changed"+ (t2/update! :model/DataApp id {:resource_collection_id other-id})))+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"resource collection cannot be changed"+ (t2/update! :model/DataApp id {:resource_collection_id nil})))+ (is (= collection-id (t2/select-one-fn :resource_collection_id :model/DataApp id)))+ (testing "an update that names the same collection is not a change"+ (t2/update! :model/DataApp id {:resource_collection_id collection-id :display_name "Renamed"})+ (is (= "Renamed" (t2/select-one-fn :display_name :model/DataApp id))))))++(deftest app-without-collection-can-be-given-one-test+ (mt/with-temp [:model/DataApp {id :id collection-id :resource_collection_id} {}+ :model/DataApp {taken-id :resource_collection_id} {}+ :model/Collection {other-id :id} {:namespace :data-apps}]+ (t2/delete! :model/Collection collection-id)+ (is (nil? (t2/select-one-fn :resource_collection_id :model/DataApp id)))+ (testing "another app's collection is refused"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"already belongs to another data app"+ (t2/update! :model/DataApp id {:resource_collection_id taken-id}))))+ (t2/update! :model/DataApp id {:resource_collection_id other-id})+ (is (= other-id (t2/select-one-fn :resource_collection_id :model/DataApp id)))))(deftest delete-removes-the-resource-collection-the-app-owns-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (let [id (insert-app!)- {:keys [resource_collection_id]} (t2/select-one :model/DataApp id)]- (t2/delete! :model/DataApp id)- (is (not (t2/exists? :model/Collection :id resource_collection_id))))))+ (mt/with-temp [:model/DataApp {id :id collection-id :resource_collection_id} {}]+ (t2/delete! :model/DataApp id)+ (is (not (t2/exists? :model/Collection :id collection-id)))))(deftest writes-are-normalized-and-validated-against-the-schema-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (let [id (insert-app! :display_name " M " :bundle_path "./dist/index.js"- :allowed_hosts ["https://API.example.com/"])]- (is (=? {:display_name "M" :bundle_path "dist/index.js" :version 1 :allowed_hosts ["https://api.example.com"]}- (t2/select-one :model/DataApp id)))- (testing "an update with an invalid column is refused"- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"does not match schema"- (t2/update! :model/DataApp id {:bundle_path "../escape.js"})))- (is (= "dist/index.js" (t2/select-one-fn :bundle_path :model/DataApp id)))))- (testing "an insert without a required manifest column is refused"- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"does not match schema"- (t2/insert! :model/DataApp {:name "x" :bundle_path "i.js"}))))- (testing "an insert with an invalid slug is refused"+ (mt/with-temp [:model/DataApp {id :id} {:display_name " M "+ :bundle_path "./dist/index.js"+ :allowed_hosts ["https://API.example.com/"]}]+ (is (=? {:display_name "M" :bundle_path "dist/index.js" :version 1 :allowed_hosts ["https://api.example.com"]}+ (t2/select-one :model/DataApp id)))+ (testing "an update with an invalid column is refused"(is (thrown-with-msg? clojure.lang.ExceptionInfo #"does not match schema"- (insert-app! :name "Not A Slug"))))))+ (t2/update! :model/DataApp id {:bundle_path "../escape.js"})))+ (is (= "dist/index.js" (t2/select-one-fn :bundle_path :model/DataApp id)))))+ (testing "an insert without a required manifest column is refused"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"does not match schema"+ (t2/insert! :model/DataApp {:name "x" :bundle_path "i.js"}))))+ (testing "an insert with an invalid slug is refused"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"does not match schema"+ (mt/with-temp [:model/DataApp _ {:name "Not A Slug"}])))))(deftest to-json-never-includes-the-bundle-bytes-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (insert-app!)- (let [app (t2/select-one [:model/DataApp :bundle :display_name] :name "m")+ (mt/with-temp [:model/DataApp {id :id} {:display_name "M" :bundle bundle-bytes}]+ (let [app (t2/select-one [:model/DataApp :bundle :display_name] id)decoded (json/decode (json/encode app))](is (contains? app :bundle) "the instance selected with its bundle carries the raw bytes")(is (not (contains? decoded "bundle"))@@ -122,42 +96,40 @@(is (= "M" (get decoded "display_name"))))))(deftest default-selects-exclude-the-bundle-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (insert-app! :allowed_hosts ["https://api.example.com"])- (is (not (contains? (t2/select-one :model/DataApp :name "m") :bundle)))- (is (= "BUNDLEBYTES" (String. ^bytes (data-apps.db/data-app-bundle (t2/select-one-pk :model/DataApp :name "m"))- "UTF-8")))+ (mt/with-temp [:model/DataApp {id :id app-name :name} {:display_name "M"+ :bundle bundle-bytes+ :allowed_hosts ["https://api.example.com"]}]+ (is (not (contains? (t2/select-one :model/DataApp id) :bundle)))+ (is (= "BUNDLEBYTES" (String. ^bytes (data-apps.db/data-app-bundle id) "UTF-8")))(testing "data-app-by-slug returns metadata without the bundle blob"- (let [app (data-apps.db/data-app-by-slug "m")]+ (let [app (data-apps.db/data-app-by-slug app-name)](is (not (contains? app :bundle)))(is (= "M" (:display_name app)))(is (= ["https://api.example.com"] (:allowed_hosts app)))))))(deftest allowed-hosts-reads-as-a-vector-never-nil-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/with-temp [:model/DataApp {without-id :id} {}+ :model/DataApp {with-id :id} {:allowed_hosts ["https://a.com" "https://b.com"]}](testing "a row stored with NULL allowed_hosts reads back as []"- (insert-app!)- (is (= [] (:allowed_hosts (t2/select-one :model/DataApp :name "m")))))+ (is (= [] (:allowed_hosts (t2/select-one :model/DataApp without-id)))))(testing "a stored list round-trips through the JSON transform"- (insert-app! :name "n" :allowed_hosts ["https://a.com" "https://b.com"])(is (= ["https://a.com" "https://b.com"]- (:allowed_hosts (t2/select-one :model/DataApp :name "n")))))))+ (:allowed_hosts (t2/select-one :model/DataApp with-id)))))))(deftest permissions-test- (mt/with-model-cleanup [:model/DataApp :model/Collection]- (let [id (insert-app!)]- (testing "a superuser can read, write, and create"- (mt/with-premium-features #{:data-apps}- (binding [api/*is-superuser?* true]- (is (mi/can-read? :model/DataApp id))- (is (mi/can-write? :model/DataApp id))- (is (mi/can-create? :model/DataApp {})))))- (testing "without the data apps feature, a superuser cannot read"- (mt/with-premium-features #{}- (binding [api/*is-superuser?* true]- (is (not (mi/can-read? :model/DataApp id))))))- (testing "a user without access to the app's collection cannot read, write, or create"- (binding [api/*is-superuser?* false]- (is (not (mi/can-read? :model/DataApp id)))- (is (not (mi/can-write? :model/DataApp id)))- (is (not (mi/can-create? :model/DataApp {}))))))))+ (mt/with-temp [:model/DataApp {id :id} {}]+ (testing "a superuser can read, write, and create"+ (mt/with-premium-features #{:data-apps}+ (binding [api/*is-superuser?* true]+ (is (mi/can-read? :model/DataApp id))+ (is (mi/can-write? :model/DataApp id))+ (is (mi/can-create? :model/DataApp {})))))+ (testing "without the data apps feature, a superuser cannot read"+ (mt/with-premium-features #{}+ (binding [api/*is-superuser?* true]+ (is (not (mi/can-read? :model/DataApp id))))))+ (testing "a user without access to the app's collection cannot read, write, or create"+ (binding [api/*is-superuser?* false]+ (is (not (mi/can-read? :model/DataApp id)))+ (is (not (mi/can-write? :model/DataApp id)))+ (is (not (mi/can-create? :model/DataApp {})))))))diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/resources_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/resources_test.cljdeleted file mode 100644index 5ce2d89758f..00000000000--- a/enterprise/backend/test/metabase_enterprise/data_apps/resources_test.clj+++ /dev/null@@ -1,89 +0,0 @@-(ns metabase-enterprise.data-apps.resources-test- (:require- [clojure.test :refer :all]- [metabase-enterprise.data-apps.group-access :as group-access]- [metabase-enterprise.data-apps.resources :as data-app.resources]- [metabase-enterprise.data-apps.test-util :as data-apps.tu]- [metabase.collections.models.collection :as collection]- [metabase.permissions.core :as perms]- [metabase.test :as mt]- [toucan2.core :as t2]))--(set! *warn-on-reflection* true)--(defn- create-data-app!- [name]- (t2/insert-returning-instance! :model/DataApp- {:name name- :display_name name- :bundle_path (format "data_apps/%s/index.js" name)}))--(deftest the-resource-collection-stays-at-the-root-of-its-namespace-test- (testing "an app collection lives in the data-apps namespace, so it can't be filed under a regular collection,- where trashing the ancestor would take the app's copies with it"- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (mt/with-test-user :crowberto- (mt/with-temp [:model/Collection {ancestor-id :id} {:name "Filed under" :location "/"}]- (let [app (create-data-app! "wrens")- {:keys [resource_collection_id]} (data-app.resources/ensure-resources! app)]- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"cannot move a data app's collection"- (collection/move-collection!- (t2/select-one :model/Collection :id resource_collection_id)- (collection/children-location (t2/select-one :model/Collection :id ancestor-id)))))- (is (= "/" (t2/select-one-fn :location :model/Collection :id resource_collection_id)))))))))--(deftest ensure-resources-restores-a-trashed-collection-test- (testing "trashing the resource collection archives the copies the app is served from,- so ensure-resources! has to bring both back or a successful sync leaves the app blank"- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (let [app (create-data-app! "sparrows")- {:keys [resource_collection_id]} (data-app.resources/ensure-resources! app)]- ;; The app collection is readable by its own group alone, so only an admin- ;; can put the copy there.- (mt/with-test-user :crowberto- (data-apps.tu/with-published-tables! [(mt/id :venues)]- (mt/with-temp [:model/Card {card-id :id} {:collection_id resource_collection_id :dataset_query (data-apps.tu/venues-query)}]- ;; a data app's collection can no longer be trashed, so trash it as an instance from before that rule did- (mt/with-dynamic-fn-redefs [perms/data-app-collection? (constantly false)]- (collection/archive-or-unarchive-collection!- (t2/select-one :model/Collection :id resource_collection_id)- {:archived true}))- (is (true? (t2/select-one-fn :archived :model/Collection :id resource_collection_id))- "precondition: the collection is in the trash")- (is (true? (t2/select-one-fn :archived :model/Card :id card-id))- "precondition: trashing the collection archived the copy")- (data-app.resources/ensure-resources! (t2/select-one :model/DataApp :id (:id app)))- (is (false? (t2/select-one-fn :archived :model/Collection :id resource_collection_id))- "the app collection is out of the trash")- (is (false? (t2/select-one-fn :archived :model/Card :id card-id))- "the copy the app serves is readable again"))))))))--(deftest ensure-resources-recreates-a-deleted-collection-test- (testing "the reference is nullable so the collection can be deleted on its own; the next import gives the app- a collection again rather than leaving it broken"- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (let [app (create-data-app! "finches")]- (t2/delete! :model/Collection :id (:resource_collection_id app))- (let [{:keys [resource_collection_id]} (data-app.resources/ensure-resources! app)]- (is (pos-int? resource_collection_id))- (is (not= (:resource_collection_id app) resource_collection_id))- (is (=? {:name "Data App: finches" :namespace :data-apps :location "/"}- (t2/select-one :model/Collection :id resource_collection_id)))- (is (= resource_collection_id (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app)))))))))--(deftest ensure-resources-leaves-collection-grants-alone-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (mt/with-temp [:model/PermissionsGroup {other-group-id :id} {}]- (let [app (create-data-app! "birds")- {collection-id :resource_collection_id} (data-app.resources/ensure-resources! app)- group-id (:id (t2/insert-returning-instance! :model/PermissionsGroup {:name "Finches"}))- _ (group-access/add-groups! app [group-id])- grants (fn [] (t2/select-fn-set (juxt :group_id :object) :model/Permissions- :object [:in [(perms/collection-read-path collection-id)- (perms/collection-readwrite-path collection-id)]]))]- (perms/revoke-collection-permissions! group-id collection-id)- (perms/grant-collection-read-permissions! other-group-id collection-id)- (let [before (grants)]- (data-app.resources/ensure-resources! app)- (is (= #{[other-group-id (perms/collection-read-path collection-id)]} before))- (is (= before (grants))))))))diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/search_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/search_test.cljindex c1eb0932787..86777b05de4 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/search_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/search_test.clj@@ -65,10 +65,8 @@(deftest search-finds-the-data-apps-the-user-can-read-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp {slug-match-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"- :bundle_path "sightings.js"}- :model/DataApp {granted-id :id} {:name "plovers" :display_name "Sandpiper counts"- :bundle_path "counts.js"}+ (mt/with-temp [:model/DataApp {slug-match-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"}+ :model/DataApp {granted-id :id} {:name "plovers" :display_name "Sandpiper counts"}:model/PermissionsGroup {group-id :id} {}:model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}](grant-app-access! group-id granted-id)@@ -88,12 +86,9 @@(deftest search-finds-disabled-and-outdated-data-apps-the-user-can-read-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp {current-id :id} {:name "sandpiper-dunlin" :display_name "Dunlin"- :bundle_path "dunlin.js" :version 2}- :model/DataApp {outdated-id :id} {:name "sandpiper-knot" :display_name "Knot"- :bundle_path "knot.js" :version 1}- :model/DataApp {disabled-id :id} {:name "sandpiper-sanderling" :display_name "Sanderling"- :bundle_path "sanderling.js" :version 2 :enabled false}+ (mt/with-temp [:model/DataApp {current-id :id} {:name "sandpiper-dunlin" :display_name "Dunlin" :version 2}+ :model/DataApp {outdated-id :id} {:name "sandpiper-knot" :display_name "Knot" :version 1}+ :model/DataApp {disabled-id :id} {:name "sandpiper-sanderling" :display_name "Sanderling" :version 2 :enabled false}:model/PermissionsGroup {group-id :id} {}:model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}](grant-app-access! group-id current-id)@@ -115,12 +110,10 @@(mt/with-temporary-setting-values [use-tenants true](mt/with-temp [:model/Tenant tenant {:name "Tenant"}:model/User tenant-user {:tenant_id (:id tenant)}- :model/DataApp {enabled-id :id} {:name "plovers" :display_name "Sandpiper counts"- :bundle_path "counts.js"}+ :model/DataApp {enabled-id :id} {:name "plovers" :display_name "Sandpiper counts"}:model/DataApp {disabled-id :id} {:name "sandpiper-drafts" :display_name "Drafts"- :enabled false :bundle_path "drafts.js"}- :model/DataApp {ungranted-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"- :bundle_path "sightings.js"}+ :enabled false}+ :model/DataApp {ungranted-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"}:model/PermissionsGroup {group-id :id} {}:model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}](grant-app-access! group-id enabled-id)@@ -138,10 +131,9 @@(deftest type-filter-offers-data-apps-only-to-users-who-can-read-one-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp {enabled-id :id} {:name "plovers" :display_name "Sandpiper counts"- :bundle_path "counts.js"}+ (mt/with-temp [:model/DataApp {enabled-id :id} {:name "plovers" :display_name "Sandpiper counts"}:model/DataApp {disabled-id :id} {:name "sandpiper-drafts" :display_name "Drafts"- :enabled false :bundle_path "drafts.js"}+ :enabled false}:model/PermissionsGroup {other-group-id :id} {}:model/PermissionsGroup {group-id :id} {}:model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}]@@ -158,8 +150,7 @@(deftest type-filter-offers-no-data-apps-without-the-feature-test(mt/with-premium-features #{}- (mt/with-temp [:model/DataApp {app-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"- :bundle_path "s.js"}+ (mt/with-temp [:model/DataApp {app-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"}:model/PermissionsGroup {group-id :id} {}:model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}](grant-app-access! group-id app-id)@@ -178,8 +169,7 @@(deftest semantic-type-filter-offers-data-apps-only-to-users-who-can-read-one-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp {app-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"- :bundle_path "s.js"}+ (mt/with-temp [:model/DataApp {app-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"}:model/PermissionsGroup {group-id :id} {}:model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}](grant-app-access! group-id app-id)@@ -197,10 +187,8 @@(deftest search-by-ids-finds-the-requested-data-app-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp {app-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"- :bundle_path "sightings.js"}- :model/DataApp {other-id :id} {:name "plovers" :display_name "Sandpiper counts"- :bundle_path "counts.js"}+ (mt/with-temp [:model/DataApp {app-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"}+ :model/DataApp {other-id :id} {:name "plovers" :display_name "Sandpiper counts"}:model/PermissionsGroup {group-id :id} {}:model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}](grant-app-access! group-id app-id)@@ -214,14 +202,13 @@(deftest search-finds-no-data-apps-without-the-feature-test(mt/with-premium-features #{}- (mt/with-temp [:model/DataApp _ {:name "sandpiper-sightings" :display_name "Shorebirds" :bundle_path "s.js"}]+ (mt/with-temp [:model/DataApp _ {:name "sandpiper-sightings" :display_name "Shorebirds"}](with-each-engine#(is (= {} (found-data-apps :crowberto :models "data-app")))))))(deftest deleting-a-data-app-removes-it-from-the-index-test(mt/with-premium-features #{:data-apps}- (mt/with-temp [:model/DataApp {app-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"- :bundle_path "s.js"}]+ (mt/with-temp [:model/DataApp {app-id :id} {:name "sandpiper-sightings" :display_name "Shorebirds"}](is (contains? (found-data-apps :crowberto :models "data-app") app-id))(t2/delete! :model/DataApp app-id)(is (= {} (found-data-apps :crowberto :models "data-app"))))))diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/serialization_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/serialization_test.cljindex 911985cec52..738058af3e9 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/serialization_test.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/serialization_test.clj@@ -7,7 +7,6 @@[metabase-enterprise.serialization.core :as serialization][metabase-enterprise.serialization.test-util :as ts][metabase-enterprise.serialization.v2.extract :as extract]- [metabase.actions.core :as actions][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata][metabase.models.serialization :as serdes]@@ -17,14 +16,9 @@(set! *warn-on-reflection* true)-(defn- insert-app! [& {:as extra}]- (t2/insert-returning-instance! :model/DataApp- (merge {:name "sales-ops"- :display_name "Sales Ops"- :bundle_path "dist/index.js"- :bundle (.getBytes "console.log(1)" "UTF-8")- :allowed_hosts ["https://api.example.com"]}- extra)))+(def ^:private app-bundle+ "The bundle of the apps these tests export and import."+ (.getBytes "console.log(1)" "UTF-8"))(defn- export!"Export the data apps, with the collection each owns (its serdes descendant) when `with-collections?`."@@ -63,70 +57,70 @@extra))(deftest export-writes-the-manifest-and-the-bundle-file-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (ts/with-random-dump-dir [dump-dir "data-app-export-"]- (let [app (insert-app! :description "Pipeline health")]- (export! dump-dir)- (testing "the manifest is a serdes YAML in the app's directory, keyed like a hand-written data_app.yaml"- (is (= {:serdes/meta [{:model "DataApp"}]- :entity_id (:entity_id app)- :slug "sales-ops"- :name "Sales Ops"- :description "Pipeline health"- :version 1- :path "dist/index.js"- :allowed_hosts ["https://api.example.com"]- :collection (t2/select-one-fn :entity_id :model/Collection :id (:resource_collection_id app))}- (dissoc (yaml/from-file (io/file dump-dir "data_apps" "sales-ops" "data_app.yaml"))- :created_at))))- (testing "the bundle is a plain file at its path next to the manifest"- (is (= "console.log(1)"- (slurp (io/file dump-dir "data_apps" "sales-ops" "dist" "index.js")))))))))+ (ts/with-random-dump-dir [dump-dir "data-app-export-"]+ (mt/with-temp [:model/DataApp app {:name "sales-ops" :display_name "Sales Ops" :description "Pipeline health" :bundle app-bundle+ :allowed_hosts ["https://api.example.com"]}]+ (export! dump-dir)+ (testing "the manifest is a serdes YAML in the app's directory, keyed like a hand-written data_app.yaml"+ (is (= {:serdes/meta [{:model "DataApp"}]+ :entity_id (:entity_id app)+ :slug "sales-ops"+ :name "Sales Ops"+ :description "Pipeline health"+ :version 1+ :path "dist/index.js"+ :allowed_hosts ["https://api.example.com"]+ :collection (t2/select-one-fn :entity_id :model/Collection :id (:resource_collection_id app))}+ (dissoc (yaml/from-file (io/file dump-dir "data_apps" "sales-ops" "data_app.yaml"))+ :created_at))))+ (testing "the bundle is a plain file at its path next to the manifest"+ (is (= "console.log(1)"+ (slurp (io/file dump-dir "data_apps" "sales-ops" "dist" "index.js"))))))))(deftest round-trip-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/with-model-cleanup [:model/DataApp :model/Collection](ts/with-random-dump-dir [dump-dir "data-app-round-trip-"]- (let [app (insert-app! :enabled false)- collection-entity-id (t2/select-one-fn :entity_id :model/Collection :id (:resource_collection_id app))]- (export! dump-dir :with-collections? true)- (testing "the app's collection is exported with it, like any collection"- (is (= [collection-entity-id]- (->> (file-seq (io/file dump-dir "collections"))- (filter #(.isFile ^java.io.File %))- (map (comp :entity_id yaml/from-file))))))- (t2/delete! :model/DataApp (:id app))- (import! dump-dir)- (let [imported (t2/select-one :model/DataApp :entity_id (:entity_id app))]- (is (=? {:name "sales-ops"- :display_name "Sales Ops"- :bundle_path "dist/index.js"- :bundle_hash (:bundle_hash app)- :allowed_hosts ["https://api.example.com"]}- imported))- (is (= "console.log(1)" (bundle-text imported)))- (testing "new app starts enabled, like one created through the API"- (is (true? (:enabled imported))))- (testing "the import links the collection the manifest names without importing group access"- (is (=? {:entity_id collection-entity-id :namespace :data-apps}- (t2/select-one :model/Collection :id (:resource_collection_id imported))))- (is (empty? (group-access/assigned-groups imported))))))))))+ (mt/with-temp [:model/DataApp app {:name "sales-ops" :display_name "Sales Ops" :enabled false :bundle app-bundle+ :allowed_hosts ["https://api.example.com"]}]+ (let [collection-entity-id (t2/select-one-fn :entity_id :model/Collection :id (:resource_collection_id app))]+ (export! dump-dir :with-collections? true)+ (testing "the app's collection is exported with it, like any collection"+ (is (= [collection-entity-id]+ (->> (file-seq (io/file dump-dir "collections"))+ (filter #(.isFile ^java.io.File %))+ (map (comp :entity_id yaml/from-file))))))+ (t2/delete! :model/DataApp (:id app))+ (import! dump-dir)+ (let [imported (t2/select-one :model/DataApp :entity_id (:entity_id app))]+ (is (=? {:name "sales-ops"+ :display_name "Sales Ops"+ :bundle_path "dist/index.js"+ :bundle_hash (:bundle_hash app)+ :allowed_hosts ["https://api.example.com"]}+ imported))+ (is (= "console.log(1)" (bundle-text imported)))+ (testing "new app starts enabled, like one created through the API"+ (is (true? (:enabled imported))))+ (testing "the import links the collection the manifest names without importing group access"+ (is (=? {:entity_id collection-entity-id :namespace :data-apps}+ (t2/select-one :model/Collection :id (:resource_collection_id imported))))+ (is (empty? (group-access/assigned-groups imported)))))))))))(deftest import-refuses-a-manifest-that-names-another-collection-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (ts/with-random-dump-dir [dump-dir "data-app-switch-"]- (mt/with-temp [:model/Collection {other-entity-id :entity_id} {:name "Other" :namespace :data-apps}]- (let [app (insert-app!)]- (write-app-files! dump-dir "sales-ops" (app-yaml (:entity_id app) "sales-ops" :collection other-entity-id)- {"dist/index.js" "B"})- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Failed to load" (import! dump-dir)))- (is (= (:resource_collection_id app)- (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))))))))))+ (ts/with-random-dump-dir [dump-dir "data-app-switch-"]+ (mt/with-temp [:model/Collection {other-entity-id :entity_id} {:namespace :data-apps}+ :model/DataApp app {:name "sales-ops"}]+ (write-app-files! dump-dir "sales-ops" (app-yaml (:entity_id app) "sales-ops" :collection other-entity-id)+ {"dist/index.js" "B"})+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Failed to load" (import! dump-dir)))+ (is (= (:resource_collection_id app)+ (t2/select-one-fn :resource_collection_id :model/DataApp :id (:id app))))))))(deftest import-refuses-a-manifest-naming-a-collection-the-repository-lacks-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/with-model-cleanup [:model/DataApp :model/Collection](ts/with-random-dump-dir [dump-dir "data-app-no-collection-"](write-app-files! dump-dir "x" (app-yaml "pZrj7PDuz3vSWYYi0QFhd" "x" :collection "nosuchcollection00001"){"dist/index.js" "B"})@@ -136,27 +130,25 @@(deftest import-updates-in-place-and-keeps-local-state-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (ts/with-random-dump-dir [dump-dir "data-app-update-"]- (let [app (insert-app! :enabled false)- group (t2/insert-returning-instance! :model/PermissionsGroup {:name "Assigned group"})]- (group-access/add-groups! app [(:id group)])- (write-app-files! dump-dir "sales-ops" (app-yaml (:entity_id app) "sales-ops" :name "Renamed"- :path "./dist/index.js")- {"dist/index.js" "console.log(2)"})- (import! dump-dir)- (let [updated (t2/select-one :model/DataApp :id (:id app))]- (is (=? {:display_name "Renamed"- :enabled false- :resource_collection_id (:resource_collection_id app)}- updated))- (is (= [(:id group)] (mapv :id (group-access/assigned-groups updated))))- (is (= "console.log(2)" (bundle-text updated)))- (is (not= (:bundle_hash app) (:bundle_hash updated)))))))))+ (ts/with-random-dump-dir [dump-dir "data-app-update-"]+ (mt/with-temp [:model/DataApp app {:name "sales-ops" :enabled false} :model/PermissionsGroup group {}]+ (group-access/add-groups! app [(:id group)])+ (write-app-files! dump-dir "sales-ops" (app-yaml (:entity_id app) "sales-ops" :name "Renamed"+ :path "./dist/index.js")+ {"dist/index.js" "console.log(2)"})+ (import! dump-dir)+ (let [updated (t2/select-one :model/DataApp :id (:id app))]+ (is (=? {:display_name "Renamed"+ :enabled false+ :resource_collection_id (:resource_collection_id app)}+ updated))+ (is (= [(:id group)] (mapv :id (group-access/assigned-groups updated))))+ (is (= "console.log(2)" (bundle-text updated)))+ (is (not= (:bundle_hash app) (:bundle_hash updated))))))))(deftest import-rejects-invalid-apps-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]+ (mt/with-model-cleanup [:model/DataApp :model/Collection](testing "a missing bundle file"(ts/with-random-dump-dir [dump-dir "data-app-missing-"](write-app-files! dump-dir "x" (app-yaml "pZrj7PDuz3vSWYYi0QFhd" "x") {})@@ -182,60 +174,58 @@(deftest import-clears-an-omitted-description-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (ts/with-random-dump-dir [dump-dir "data-app-description-"]- (let [app (insert-app! :description "Pipeline health")]- (write-app-files! dump-dir "sales-ops" (app-yaml (:entity_id app) "sales-ops") {"dist/index.js" "B"})- (import! dump-dir)- (is (nil? (t2/select-one-fn :description :model/DataApp :id (:id app)))))))))+ (ts/with-random-dump-dir [dump-dir "data-app-description-"]+ (mt/with-temp [:model/DataApp app {:name "sales-ops" :description "Pipeline health"}]+ (write-app-files! dump-dir "sales-ops" (app-yaml (:entity_id app) "sales-ops") {"dist/index.js" "B"})+ (import! dump-dir)+ (is (nil? (t2/select-one-fn :description :model/DataApp :id (:id app))))))))(deftest import-does-not-take-over-an-app-made-on-the-instance-test(mt/with-premium-features #{:data-apps}- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (ts/with-random-dump-dir [dump-dir "data-app-takeover-"]- (let [app (insert-app!)]- (write-app-files! dump-dir "sales-ops" (app-yaml "Ld3cXiYs9n8HP3q3FvC7R" "sales-ops") {"dist/index.js" "B"})- (let [e (try (import! dump-dir) nil (catch clojure.lang.ExceptionInfo e e))]- (is (re-find #"Failed to load" (ex-message e)))- (testing "the pull says what to do, rather than failing on the slug's unique index"- (is (re-find #"named \"sales-ops\" already exists on this instance"- (ex-message (ex-cause e))))))- (is (=? {:entity_id (:entity_id app)} (t2/select-one :model/DataApp :id (:id app)))))))))+ (ts/with-random-dump-dir [dump-dir "data-app-takeover-"]+ (mt/with-temp [:model/DataApp app {:name "sales-ops"}]+ (write-app-files! dump-dir "sales-ops" (app-yaml "Ld3cXiYs9n8HP3q3FvC7R" "sales-ops") {"dist/index.js" "B"})+ (let [e (try (import! dump-dir) nil (catch clojure.lang.ExceptionInfo e e))]+ (is (re-find #"Failed to load" (ex-message e)))+ (testing "the pull says what to do, rather than failing on the slug's unique index"+ (is (re-find #"named \"sales-ops\" already exists on this instance"+ (ex-message (ex-cause e))))))+ (is (=? {:entity_id (:entity_id app)} (t2/select-one :model/DataApp :id (:id app))))))))(deftest export-includes-data-apps-test- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup]- (let [app (insert-app!)]- (is (some #(= [{:model "DataApp" :id (:entity_id app)}] (map (fn [m] (dissoc m :label)) (:serdes/meta %)))- (into [] (extract/extract {:no-collections true :no-data-model true :no-settings true})))))))+ (mt/with-temp [:model/DataApp {app-entity-id :entity_id} {}]+ (is (some #(= [{:model "DataApp" :id app-entity-id}] (map (fn [m] (dissoc m :label)) (:serdes/meta %)))+ (into [] (extract/extract {:no-collections true :no-data-model true :no-settings true}))))))(deftest round-trip-with-resources-test(testing "an app travels with its collection and what that holds, written under collections/data_apps/"(mt/with-premium-features #{:data-apps}(data-apps.tu/with-published-tables! [(mt/id :venues)]- (mt/with-model-cleanup [:model/DataApp :model/Collection :model/PermissionsGroup :model/Card :model/Action]+ (mt/with-model-cleanup [:model/DataApp :model/Collection :model/Card :model/Action](ts/with-random-dump-dir [dump-dir "data-app-resources-"]- (let [app (insert-app!)- collection-id (:resource_collection_id app)- mp (mt/metadata-provider)- query (lib/query mp (lib.metadata/table mp (mt/id :venues)))]- (mt/with-temp [:model/Card {question-eid :entity_id} {:name "Venues list" :type :question- :collection_id collection-id :dataset_query query}]- (let [action-id (actions/insert! {:name "Rename venue"- :type :query- :collection_id collection-id- :database_id (mt/id)- :dataset_query (lib/native-query mp "UPDATE venues SET name = 'x'")})- action-eid (t2/select-one-fn :entity_id :model/Action :id action-id)]- (export! dump-dir :with-collections? true)- (testing "the files sit under the collection's directory of the data-apps namespace"- (doseq [path ["data_app__sales_ops.yaml" "data_app__sales_ops/venues_list.yaml"- "data_app__sales_ops/rename_venue.yaml"]]- (is (.exists (io/file dump-dir "collections" "data_apps" path)) path)))- (t2/delete! :model/DataApp (:id app))- (is (not (t2/exists? :model/Card :entity_id question-eid)) "deleting the app deletes its collection's cards")- (import! dump-dir)- (let [imported (t2/select-one :model/DataApp :entity_id (:entity_id app))- collection-id (:resource_collection_id imported)]- (is (pos-int? collection-id))- (is (= collection-id (t2/select-one-fn :collection_id :model/Card :entity_id question-eid)))- (is (= collection-id (t2/select-one-fn :collection_id :model/Action :entity_id action-eid)))))))))))))+ (mt/with-temp [:model/DataApp {app-id :id app-entity-id :entity_id collection-id :resource_collection_id}+ {:name "sales-ops"}+ :model/Card {question-eid :entity_id}+ {:name "Venues list"+ :collection_id collection-id+ :dataset_query (lib/query (mt/metadata-provider)+ (lib.metadata/table (mt/metadata-provider) (mt/id :venues)))}+ :model/Action {action-id :id action-eid :entity_id}+ {:name "Rename venue" :type :query :collection_id collection-id}+ :model/QueryAction _+ {:action_id action-id+ :database_id (mt/id)+ :dataset_query (lib/native-query (mt/metadata-provider) "UPDATE venues SET name = 'x'")}]+ (export! dump-dir :with-collections? true)+ (testing "the files sit under the collection's directory of the data-apps namespace"+ (doseq [path ["data_app__sales_ops.yaml" "data_app__sales_ops/venues_list.yaml"+ "data_app__sales_ops/rename_venue.yaml"]]+ (is (.exists (io/file dump-dir "collections" "data_apps" path)) path)))+ (t2/delete! :model/DataApp app-id)+ (is (not (t2/exists? :model/Card :entity_id question-eid)) "deleting the app deletes its collection's cards")+ (import! dump-dir)+ (let [imported (t2/select-one :model/DataApp :entity_id app-entity-id)+ collection-id (:resource_collection_id imported)]+ (is (pos-int? collection-id))+ (is (= collection-id (t2/select-one-fn :collection_id :model/Card :entity_id question-eid)))+ (is (= collection-id (t2/select-one-fn :collection_id :model/Action :entity_id action-eid)))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/test_util.clj b/enterprise/backend/test/metabase_enterprise/data_apps/test_util.cljindex dc77dad4306..e459d184731 100644--- a/enterprise/backend/test/metabase_enterprise/data_apps/test_util.clj+++ b/enterprise/backend/test/metabase_enterprise/data_apps/test_util.clj@@ -2,6 +2,7 @@(:require[clojure.set :as set][clojure.string :as str]+ [metabase-enterprise.data-apps.apps :as data-apps.apps][metabase-enterprise.data-studio.db :as data-studio.db][metabase-enterprise.serialization.core :as serialization][metabase-enterprise.serialization.dump :as serialization.dump]@@ -20,6 +21,11 @@(set! *warn-on-reflection* true)+(defn app-url+ "The API URL of the data `app`, followed by `parts`."+ [{app-name :name} & parts]+ (str/join "/" (list* "ee/apps" app-name parts)))+(defn collection-entity-id"A stable resource collection entity ID for the app in `data_apps/<slug>`."[slug]@@ -59,7 +65,7 @@:as options}](let [entity-id (or entity_id (app-entity-id dir))collection (if (contains? options :collection) collection (collection-entity-id dir))- collection-name (str "Data App: " slug)+ collection-name (data-apps.apps/collection-name slug)resources (if (contains? options :resources)resources(when collection {(collection-path collection-name) (collection-file collection collection-name)}))]diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/task/dependency_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/task/dependency_test.cljindex ea16a26efc5..166126b7efa 100644--- a/enterprise/backend/test/metabase_enterprise/dependencies/task/dependency_test.clj+++ b/enterprise/backend/test/metabase_enterprise/dependencies/task/dependency_test.clj@@ -1248,18 +1248,17 @@(data-apps.tu/with-published-tables! [(mt/id :venues)](deps.test/run-with-dependencies-setup!(fn [mp]- (mt/with-temp [:model/DataApp {app-id :id collection-id :resource_collection_id}- {:name "birds" :display_name "Birds" :bundle_path "birds.js"}- :model/Collection {given-collection-id :id} {:namespace "data-apps"}- :model/Card {card-id :id} {:dataset_query (lib/query mp (lib.metadata/table mp (mt/id :venues)))- :collection_id given-collection-id}]- (t2/delete! :model/Collection collection-id)+ (mt/with-temp [:model/DataApp app {}+ :model/Collection collection {:namespace "data-apps"}+ :model/Card card {:dataset_query (lib/query mp (lib.metadata/table mp (mt/id :venues)))+ :collection_id (:id collection)}]+ (t2/delete! :model/Collection (:resource_collection_id app))(deps.test/synchronously-run-dependency-job!)- (is (empty? (data-app-deps app-id)))- (t2/update! :model/DataApp app-id {:resource_collection_id given-collection-id})- (assert-pending :data-app app-id)+ (is (empty? (data-app-deps (:id app))))+ (t2/update! :model/DataApp (:id app) {:resource_collection_id (:id collection)})+ (assert-pending :data-app (:id app))(deps.test/synchronously-run-dependency-job!)- (is (= #{[:card card-id]} (data-app-deps app-id)))))))))+ (is (= #{[:card (:id card)]} (data-app-deps (:id app))))))))))(deftest ^:synchronized data-app-dependency-job-skips-the-bundle-test(testing "the dependency job computes an app's dependencies without reading its bundle"diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/init_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/init_test.cljindex df69df205aa..aeafc62bd86 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/init_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/init_test.clj@@ -307,26 +307,21 @@;;; ------------------------------------------- Data app ledger backfill -------------------------------------------(defn- do-with-untracked-data-apps!- "Runs `f` with `{:published :tracked}` data app ids under `remote-sync-type` `sync-type`, where only- `:tracked` has a DataApp ledger row."+ "Runs `f` with the ids of a `:published` and a `:tracked` data app, and the `:published-name` of the first, under+ `remote-sync-type` `sync-type`, where only `:tracked` has a DataApp ledger row."[sync-type f](mt/with-temporary-setting-values [:remote-sync-url "file://my/repo.git":remote-sync-type sync-type:remote-sync-branch "main"]- (mt/with-model-cleanup [:model/RemoteSyncObject :model/DataApp :model/Collection :model/PermissionsGroup]- (let [insert-app! (fn [slug]- (t2/insert-returning-pk! :model/DataApp {:name slug- :display_name slug- :bundle_path "index.js"- :bundle (.getBytes "BUNDLE" "UTF-8")}))- published (insert-app! "published")- tracked (insert-app! "tracked")]+ (mt/with-model-cleanup [:model/RemoteSyncObject]+ (mt/with-temp [:model/DataApp {published :id published-name :name} {}+ :model/DataApp {tracked :id tracked-name :name} {}](t2/delete! :model/RemoteSyncObject :model_type "DataApp")- (t2/insert! :model/RemoteSyncObject {:model_type "DataApp" :model_id tracked :model_name "tracked"+ (t2/insert! :model/RemoteSyncObject {:model_type "DataApp" :model_id tracked :model_name tracked-name:status "synced" :status_changed_at (t/offset-date-time)})(mt/with-dynamic-fn-redefs [impl/async-import! (constantly nil)remote-sync.object/dirty? (constantly false)]- (f {:published published :tracked tracked}))))))+ (f {:published published :published-name published-name :tracked tracked}))))))(defn- data-app-rso-statuses [](t2/select-fn->fn :model_id :status :model/RemoteSyncObject :model_type "DataApp"))@@ -335,10 +330,10 @@(testing "read-write init tracks every untracked published data app as 'create', once, so the next push writes it"(do-with-untracked-data-apps!:read-write- (fn [{:keys [published tracked]}]+ (fn [{:keys [published published-name tracked]}](#'init/remote-sync-init)(is (= {published "create" tracked "synced"} (data-app-rso-statuses)))- (is (=? {:model_name "published"}+ (is (=? {:model_name published-name}(t2/select-one :model/RemoteSyncObject :model_type "DataApp" :model_id published)))(testing "a second run inserts nothing"(is (= 0 (rs-events/backfill-data-app-tracking!))))))))diff --git a/frontend/src/metabase-types/api/data-app.ts b/frontend/src/metabase-types/api/data-app.tsindex 02c5a615e94..87a8d8f4cdb 100644--- a/frontend/src/metabase-types/api/data-app.ts+++ b/frontend/src/metabase-types/api/data-app.ts@@ -38,6 +38,16 @@ export interface DataApp {updated_at: string;}+export interface CreateDataAppRequest {+ name: string;+ display_name?: string;+ description?: string | null;+ version?: number;+ bundle_path?: string;+ allowed_hosts?: string[];+ enabled?: boolean;+}+/*** Status of the connected repository as it relates to data apps. The connection* itself (URL / branch / token) is configured on the remote-sync settings page.diff --git a/test/metabase/test/util.clj b/test/metabase/test/util.cljindex 6af3a2686ea..4c81e341195 100644--- a/test/metabase/test/util.clj+++ b/test/metabase/test/util.clj@@ -136,7 +136,8 @@:model/Action(fn [_] {:creator_id (rasta-id)- :entity_id (u/generate-nano-id)})+ :entity_id (u/generate-nano-id)+ :name (u.random/random-name)}):model/Channel(fn [_] (default-timestamped@@ -183,7 +184,8 @@(fn [_] {:entity_id (u/generate-nano-id):name (u/lower-case-en (u.random/random-name)):display_name (u.random/random-name)- :bundle_path "dist/index.js"})+ :bundle_path "dist/index.js"+ :bundle (.getBytes "" "UTF-8")}):model/Database(fn [_] (default-timestamped@@ -492,6 +494,17 @@(locking dimension-lock(next-method model explicit-attributes f)))+(methodical/defmethod t2.with-temp/do-with-temp* :around :model/DataApp+ "Give a temp data app a temp collection named like the one the API creates, unless the attributes set+ `resource_collection_id`, even to nil."+ [model explicit-attributes f]+ (let [attributes (merge (t2.with-temp/with-temp-defaults model) explicit-attributes)]+ (if (contains? attributes :resource_collection_id)+ (next-method model attributes f)+ (t2.with-temp/with-temp [:model/Collection {collection-id :id} {:name (str "Data App: " (:name attributes))+ :namespace "data-apps"}]+ (next-method model (assoc attributes :resource_collection_id collection-id) f)))))+(defn- set-with-temp-defaults! [](doseq [[model defaults-fn] with-temp-defaults-fns](methodical/defmethod t2.with-temp/with-temp-defaults model