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
(ns metabase-enterprise.data-apps.checks-test
"The checks that hold a data app's ad-hoc queries and card runs to its own cards and the Library, and its action runs
to its own actions, through the endpoints that run them."
(:require
[clojure.string :as str]
[clojure.test :refer :all]
[metabase-enterprise.data-apps.test-util :as data-apps.tu :refer [do-with-app-collection]]
[metabase.collections.core :as collections]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.search.test-util :as search.tu]
[metabase.test :as mt]
[metabase.util.json :as json]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
(defn- as-app
"Request options that mark a request as the data app's named `app-name`, or as an unnamed data app's when nil."
[app-name]
{:request-options {:headers (cond-> {"x-metabase-client" "data-app"}
app-name (assoc "x-metabase-client-identifier" app-name))}})
(def ^:private as-data-app
"Request options that mark a request as from the data app of [[do-with-app-collection]]."
(as-app "birds"))
(def ^:private as-preview
"Request options that mark a request as from a data app's dev preview."
(assoc-in as-data-app [:request-options :headers "x-metabase-data-app-dev"] "true"))
(def ^:private error-formats
"The format of the error message for each kind of entity a data app may not use."
{:cards "A data app can use only the cards in its own collection, not the cards %s."
:metrics "A data app can use only metrics from the Library, not the metrics %s."})
(defn- error-message
"The error message for a data app's use of the entities of `kind` with `ids`."
[kind & ids]
(format (error-formats kind) (str/join ", " (sort ids))))
(defn- card-error
"The error message for a data app's run of the card with `card-id`."
[card-id]
(format "A data app can use only the cards in its own collection and Library metrics, not the card %d." card-id))
(defn- action-error
"The error message for a data app's run of the action with `action-id`."
[action-id]
(format "A data app can run only the actions in its own collection, not the action %d." action-id))
(defn- run-query
"The response to a data app's request to run `query` through `POST /api/dataset`, expecting `status`."
([status query]
(run-query status query as-data-app))
([status query options]
(mt/user-http-request :crowberto :post status "dataset" options query)))
(defn- table-query
"A query of the table named `table-key`."
[table-key]
(let [mp (mt/metadata-provider)]
(lib/query mp (lib.metadata/table mp (mt/id table-key)))))
(defn- card-query
"A query whose source is the card with `card-id`."
[card-id]
(let [mp (mt/metadata-provider)]
(lib/query mp (lib.metadata/card mp card-id))))
(defn- category-name
"The CATEGORIES.NAME column `query` on VENUES reaches through its foreign key."
[query]
(some #(when (= (mt/id :categories :name) (:id %)) %) (lib/breakoutable-columns query)))
(defn- do-with-app-card!
"Call `f` with the ID of a card on the VENUES table in the collection of the data app of [[do-with-app-collection]],
and with that collection's ID."
[f]
(do-with-app-collection
(fn [collection-id]
(mt/with-temp [:model/Card {card-id :id} {:name "Venues"
:collection_id collection-id
:dataset_query (data-apps.tu/venues-query)}]
(f card-id collection-id)))))
(deftest app-card-and-table-run-test
(do-with-app-card!
(fn [card-id _collection-id]
(doseq [[label query] [["the app's card" (lib/limit (card-query card-id) 1)]
["a table" (lib/limit (table-query :venues) 1)]]]
(testing (str "a query on " label " runs, pivots, compiles and has metadata")
(is (= "completed" (:status (run-query 202 query))))
(is (= "completed" (:status (mt/user-http-request :crowberto :post 202 "dataset/pivot" as-data-app
(lib/aggregate query (lib/count))))))
(is (string? (:query (mt/user-http-request :crowberto :post 200 "dataset/native" as-data-app query))))
(is (seq (:tables (mt/user-http-request :crowberto :post 200 "dataset/query_metadata" as-data-app query)))))
(testing (str "an export of a query on " label ", which a form submission sends as a JSON string")
(is (mt/user-http-request :crowberto :post 200 "dataset/csv"
(assoc-in as-data-app [:request-options :content-type]
"application/x-www-form-urlencoded")
{:query (json/encode (lib/->legacy-MBQL query))})))))))
(deftest request-must-name-readable-app-test
(do-with-app-card!
(fn [card-id _collection-id]
(testing "a data app's request that names no app, or an app that doesn't exist, is not found"
(doseq [options [(as-app nil) (as-app "no-such-app")]]
(is (= "Not found." (run-query 404 (card-query card-id) options)))))
(testing "a dev preview's request reads the Library alone, whether its app exists or not"
(doseq [app-name ["no-such-app" "birds"]
:let [options (assoc-in (as-app app-name) [:request-options :headers "x-metabase-data-app-dev"] "true")]]
(is (= "completed" (:status (run-query 202 (-> (table-query :venues) (lib/limit 1)) options))))
(is (= (format "A data app's dev preview cannot build on cards, not the cards %d." card-id)
(run-query 400 (card-query card-id) options)))))
(testing "an app the user can't read is refused"
(is (= "You don't have permissions to do that."
(mt/user-http-request :rasta :post 403 "dataset" as-data-app (card-query card-id))))))))
(deftest unpublished-tables-are-read-test
(do-with-app-card!
(fn [card-id _collection-id]
(testing "an app and its dev preview read a table not published to the Library"
(doseq [options [as-data-app as-preview]]
(is (= "completed" (:status (run-query 202 (lib/limit (table-query :categories) 1) options))))))
(testing "and reach it through a foreign key, from a table or the app's card"
(doseq [[options query] [[as-preview (table-query :venues)] [as-data-app (card-query card-id)]]]
(is (= "completed" (:status (run-query 202 (-> query (lib/breakout (category-name query)) (lib/limit 1))
options)))))))))
(deftest explicit-joins-and-custom-columns-are-refused-test
(do-with-app-card!
(fn [card-id _collection-id]
(let [mp (mt/metadata-provider)]
(doseq [query [(table-query :venues) (card-query card-id)]]
(testing "an explicit join"
(is (= "A data app cannot run queries that join tables explicitly."
(run-query 400 (lib/join query (lib/join-clause (lib.metadata/table mp (mt/id :categories))))))))
(testing "a custom column"
(is (= "A data app cannot run queries with custom columns."
(run-query 400 (lib/expression query "Double price"
(lib/* (lib.metadata/field mp (mt/id :venues :price)) 2)))))))))))
(deftest other-cards-are-refused-test
(do-with-app-card!
(fn [_card-id _collection-id]
(mt/with-temp [:model/DataApp {other-collection-id :resource_collection_id} {:name "bees"
:display_name "Bees"
:bundle_path "bees.js"}
:model/Card {elsewhere-id :id} {:name "Venues elsewhere" :dataset_query (data-apps.tu/venues-query)}
:model/Card {other-app-id :id} {:name "Venues of bees"
:collection_id other-collection-id
:dataset_query (data-apps.tu/venues-query)}]
(testing "a card outside the app's collection, or of another app, is refused"
(doseq [other-id [elsewhere-id other-app-id]]
(is (= (error-message :cards other-id)
(run-query 400 (card-query other-id))))))
(testing "the other app runs its own card"
(is (= "completed" (:status (run-query 202 (lib/limit (card-query other-app-id) 1) (as-app "bees"))))))))))
(deftest app-without-collection-uses-no-card-test
(do-with-app-card!
(fn [_card-id _collection-id]
(mt/with-temp [:model/DataApp app {:name "wasps"}
:model/Card {elsewhere-id :id} {:dataset_query (data-apps.tu/venues-query)}]
(t2/delete! :model/Collection :id (:resource_collection_id app))
(testing "an app without a collection can use no card, not even one in the root collection"
(is (= (error-message :cards elsewhere-id)
(run-query 400 (card-query elsewhere-id) (as-app "wasps")))))
(testing "it still reads tables"
(is (= "completed" (:status (run-query 202 (lib/limit (table-query :venues) 1) (as-app "wasps"))))))))))
(deftest missing-entities-are-refused-test
(do-with-app-card!
(fn [_card-id _collection-id]
(testing "an ID of no entity gets the refusal an entity the app may not use gets, so it tells nothing of what exists"
(doseq [[kind query] [[:cards {:database (mt/id) :type :query :query {:source-table "card__999999999"}}]
[:metrics {:database (mt/id) :type :query :query {:source-table (mt/id :venues)
:aggregation [[:metric 999999999]]}}]]]
(is (= (error-message kind 999999999) (run-query 400 query)) (name kind)))))))
(deftest native-query-is-refused-test
(do-with-app-card!
(fn [_card-id _collection-id]
(testing "in its MBQL 5, legacy and nested legacy forms"
(doseq [query [(lib/native-query (mt/metadata-provider) "SELECT * FROM VENUES")
{:database (mt/id) :type :native :native {:query "SELECT * FROM VENUES"}}
{:database (mt/id)
:type :query
:query {:source-query {:native "SELECT * FROM VENUES"}}}]]
(is (re-find #"cannot run native queries" (run-query 400 query)))))
(testing "a request that is not a data app's still runs native SQL"
(is (= "completed" (:status (mt/user-http-request :crowberto :post 202 "dataset"
(lib/native-query (mt/metadata-provider) "SELECT 1")))))))))
(deftest internal-query-is-refused-test
(do-with-app-collection
(fn [_collection-id]
(is (re-find #"cannot run internal queries"
(run-query 400 {:type :internal
:fn "metabase-enterprise.audit-app.pages.dashboards/most-popular-with-avg-speed"}))))))
(deftest metrics-test
(do-with-app-card!
(fn [card-id _collection-id]
(data-apps.tu/do-with-sources!
(fn [{:keys [metric-id]}]
(testing "a Library metric runs"
(is (= "completed" (:status (run-query 202 (lib/aggregate (card-query card-id)
(lib.metadata/metric (mt/metadata-provider)
metric-id)))))))
(mt/with-temp [:model/Card {outside-id :id} {:name "Venue count elsewhere"
:type :metric
:database_id (mt/id)
:dataset_query (lib/aggregate (table-query :venues) (lib/count))}]
(testing "a metric outside the Library is refused"
(is (= (error-message :metrics outside-id)
(run-query 400 (lib/aggregate (card-query card-id)
(lib.metadata/metric (mt/metadata-provider) outside-id))))))))))))
(deftest segments-and-measures-test
(do-with-app-card!
(fn [_card-id _collection-id]
(let [categories (table-query :categories)
name-col (some #(when (= (mt/id :categories :name) (:id %)) %) (lib/filterable-columns categories))]
(mt/with-temp [:model/Segment {segment-id :id} {:name "Bars"
:table_id (mt/id :categories)
:definition (lib/filter categories (lib/= name-col "Bar"))}
:model/Measure {measure-id :id} {:name "Names"
:table_id (mt/id :categories)
:definition (lib/aggregate categories (lib/distinct name-col))}]
(let [mp (mt/metadata-provider)
with-segment #(lib/filter % (lib.metadata/segment mp segment-id))
with-measure #(lib/aggregate % (lib.metadata/measure mp measure-id))]
(testing "a segment or measure of a table not published to the Library runs"
(doseq [query [(with-segment categories) (with-measure categories)]]
(is (= "completed" (:status (run-query 202 query))))))))))))
(deftest card-endpoints-run-app-card-test
(do-with-app-card!
(fn [card-id _collection-id]
(testing "the app's card runs, pivots and exports"
(is (= "completed" (:status (mt/user-http-request :crowberto :post 202 (format "card/%d/query" card-id)
as-data-app))))
(is (= "completed" (:status (mt/user-http-request :crowberto :post 202 (format "card/pivot/%d/query" card-id)
as-data-app))))
(is (some? (mt/user-http-request :crowberto :post 200 (format "card/%d/query/csv" card-id) as-data-app))))
(testing "by its entity ID too"
(is (= "completed" (:status (mt/user-http-request :crowberto :post 202
(format "card/%s/query"
(t2/select-one-fn :entity_id :model/Card :id card-id))
as-data-app))))))))
(deftest card-endpoints-refuse-other-cards-test
(do-with-app-card!
(fn [_card-id _collection-id]
(mt/with-temp [:model/Card {elsewhere-id :id} {:name "Venues elsewhere" :dataset_query (data-apps.tu/venues-query)}]
(testing "a card outside the app's collection is refused"
(is (= (card-error elsewhere-id)
(mt/user-http-request :crowberto :post 400 (format "card/%d/query" elsewhere-id) as-data-app)))
(is (= (card-error elsewhere-id)
(mt/user-http-request :crowberto :post 400 (format "card/pivot/%d/query" elsewhere-id) as-data-app)))
(is (= (card-error elsewhere-id)
(mt/user-http-request :crowberto :post 400 (format "card/%d/query/csv" elsewhere-id) as-data-app))))
(testing "an app without a collection runs no card, not even one in the root collection"
(mt/with-temp [:model/DataApp app {:name "wasps"}]
(t2/delete! :model/Collection :id (:resource_collection_id app))
(is (= (card-error elsewhere-id)
(mt/user-http-request :crowberto :post 400 (format "card/%d/query" elsewhere-id) (as-app "wasps"))))))
(testing "a dev preview runs no saved card"
(is (= (format "A data app's dev preview cannot use saved cards but Library metrics, not the card %d." elsewhere-id)
(mt/user-http-request :crowberto :post 400 (format "card/%d/query" elsewhere-id) as-preview))))
(testing "an ID of no card is not found, as for any request"
(is (= "Not found." (mt/user-http-request :crowberto :post 404 "card/999999999/query" as-data-app))))
(testing "a request that is not a data app's still runs the card"
(is (= "completed" (:status (mt/user-http-request :crowberto :post 202
(format "card/%d/query" elsewhere-id))))))))))
(deftest card-read-endpoints-check-card-test
(do-with-app-card!
(fn [card-id _collection-id]
(mt/with-temp [:model/Card {elsewhere-id :id} {:name "Venues elsewhere" :dataset_query (data-apps.tu/venues-query)}]
(testing "the app's card and its metadata are readable"
(is (=? {:id card-id} (mt/user-http-request :crowberto :get 200 (format "card/%d" card-id) as-data-app)))
(is (map? (mt/user-http-request :crowberto :get 200 (format "card/%d/query_metadata" card-id) as-data-app)))
(is (=? {:id (str "card__" card-id)}
(mt/user-http-request :crowberto :get 200 (format "table/card__%d/query_metadata" card-id) as-data-app))))
(testing "a card outside the app's collection is refused"
(doseq [url ["card/%d" "card/%d/query_metadata" "table/card__%d/query_metadata"]]
(is (= (card-error elsewhere-id)
(mt/user-http-request :crowberto :get 400 (format url elsewhere-id) as-data-app))
url)))
(testing "a request that is not a data app's still reads it"
(is (=? {:id elsewhere-id} (mt/user-http-request :crowberto :get 200 (format "card/%d" elsewhere-id)))))))))
(deftest card-endpoints-allow-library-metrics-test
(do-with-app-collection
(fn [_collection-id]
(data-apps.tu/do-with-sources!
(fn [{:keys [metric-id]}]
(testing "a Library metric and its metadata are open to an app and its dev preview, as the SDK reads them"
(doseq [options [as-data-app as-preview]
url ["card/%d" "card/%d/query_metadata" "table/card__%d/query_metadata"]]
(is (map? (mt/user-http-request :crowberto :get 200 (format url metric-id) options)) url)))
(mt/with-temp [:model/Card {outside-id :id} {:name "Venue count elsewhere"
:type :metric
:database_id (mt/id)
:dataset_query (lib/aggregate (data-apps.tu/venues-query) (lib/count))}]
(testing "a metric outside the Library is refused"
(is (= (card-error outside-id)
(mt/user-http-request :crowberto :get 400 (format "card/%d" outside-id) as-data-app))))))))))
(deftest out-of-scope-endpoints-test
(testing "parameter values, dashboards, the native editor's snippets and autocompletion, and timelines are no part of a
data app's surface"
(do-with-app-card!
(fn [card-id _collection-id]
(mt/with-temp [:model/Dashboard {dashboard-id :id} {}]
(doseq [[method url body] [[:get (format "card/%d/params/kind/values" card-id)]
[:get (format "card/%d/params/kind/search/caf" card-id)]
[:get (format "card/%d/params/kind/remapping?value=bar" card-id)]
[:post "dataset/parameter/values" {:parameter {:id "kind" :type :string/=}}]
[:post "dataset/parameter/search/caf" {:parameter {:id "kind" :type :string/=}}]
[:post "dataset/parameter/remapping" {:parameter {:id "kind" :type :string/=} :value "bar"}]
[:get (format "dashboard/%d" dashboard-id)]
[:get (format "dashboard/%d/query_metadata" dashboard-id)]
[:get "native-query-snippet"]
[:get (format "database/%d/autocomplete_suggestions?prefix=ven" (mt/id))]
[:get (format "database/%d/card_autocomplete_suggestions?query=ven" (mt/id))]
[:get "timeline"]]]
(is (= "scope_not_permitted"
(:error (if body
(mt/user-http-request :crowberto method 403 url as-data-app body)
(mt/user-http-request :crowberto method 403 url as-data-app))))
url)))))))
(deftest refusal-names-large-id-without-grouping-test
(do-with-app-card!
(fn [_card-id _collection-id]
(mt/with-temp [:model/Card {elsewhere-id :id} {:id 1234567 :dataset_query (data-apps.tu/venues-query)}]
(testing "an ID of a thousand or more appears as it is, not as 1,234,567"
(is (= "A data app can use only the cards in its own collection and Library metrics, not the card 1234567."
(mt/user-http-request :crowberto :post 400 (format "card/%d/query" elsewhere-id) as-data-app))))))))
(defn- do-with-rename-action!
"Call `f` with the ID of a temp implicit action, on no model, that updates a category, in the collection with
`collection-id`."
[collection-id f]
(mt/with-temp [:model/Action {action-id :id} {:name "Update category"
:type :implicit
:collection_id collection-id}
:model/ImplicitAction _ {:action_id action-id
:kind "row/update"
:table_id (mt/id :categories)}]
(f action-id)))
(def ^:private category-parameters
{:id 1 :name "African"})
(deftest action-endpoints-run-app-action-test
(mt/with-actions-test-data-and-actions-enabled
(do-with-app-collection
(fn [collection-id]
(do-with-rename-action!
collection-id
(fn [action-id]
(testing "the app's action runs, and its values endpoint answers"
(is (= {:rows-updated 1}
(mt/user-http-request :crowberto :post 200 (format "action/%d/execute" action-id) as-data-app
{:parameters category-parameters})))
(is (= category-parameters
(mt/user-http-request :crowberto :post 200 (format "action/%d/execute/values" action-id)
as-data-app {:parameters category-parameters}))))
(testing "by its entity ID too"
(is (= {:rows-updated 1}
(mt/user-http-request :crowberto :post 200
(format "action/%s/execute"
(t2/select-one-fn :entity_id :model/Action :id action-id))
as-data-app
{:parameters category-parameters}))))))))))
(deftest action-endpoints-refuse-other-actions-test
(mt/with-actions-test-data-and-actions-enabled
(do-with-app-collection
(fn [_collection-id]
(mt/with-temp [:model/Collection {elsewhere-collection-id :id} {:namespace :data-actions}]
(do-with-rename-action!
elsewhere-collection-id
(fn [elsewhere-id]
(testing "an action outside the app's collection is refused"
(is (= (action-error elsewhere-id)
(mt/user-http-request :crowberto :post 400 (format "action/%d/execute" elsewhere-id) as-data-app
{:parameters category-parameters})))
(is (= (action-error elsewhere-id)
(mt/user-http-request :crowberto :post 400 (format "action/%d/execute/values" elsewhere-id)
as-data-app {:parameters category-parameters}))))
(testing "an ID of no action is not found, as for any request"
(is (= "Not found." (mt/user-http-request :crowberto :post 404 "action/999999999/execute" as-data-app
{:parameters category-parameters}))))
(testing "a request that is not a data app's still runs the action"
(is (= {:rows-updated 1}
(mt/user-http-request :crowberto :post 200 (format "action/%d/execute" elsewhere-id)
{:parameters category-parameters})))))))))))
(deftest preview-runs-only-adhoc-implicit-actions-test
(mt/with-actions-test-data-and-actions-enabled
(do-with-app-collection
(fn [collection-id]
(let [adhoc {:name "Rename" :type "implicit" :kind "row/update" :table_id (mt/id :categories)}]
(do-with-rename-action!
collection-id
(fn [action-id]
(testing "a dev preview runs an ad-hoc implicit action on a table not published to the Library"
(is (=? {:rows-updated some?}
(mt/user-http-request :crowberto :post 200 "action/execute" as-preview
{:action adhoc :parameters category-parameters}))))
(testing "nor a saved one, even the app's"
(is (= (format "A data app's dev preview can run only ad-hoc actions, not the action %d." action-id)
(mt/user-http-request :crowberto :post 400 (format "action/%d/execute" action-id) as-preview
{:parameters category-parameters}))))
(testing "an app runs no ad-hoc action"
(is (= "A data app can run only the actions in its own collection, not ad-hoc ones."
(mt/user-http-request :crowberto :post 400 "action/execute" as-data-app
{:action adhoc :parameters category-parameters})))))))))))
(deftest action-read-endpoints-hold-to-app-actions-test
(mt/with-actions-test-data-and-actions-enabled
(do-with-app-collection
(fn [collection-id]
(do-with-rename-action!
collection-id
(fn [action-id]
(mt/with-temp [:model/Collection {elsewhere-collection-id :id} {:namespace :data-actions}]
(do-with-rename-action!
elsewhere-collection-id
(fn [elsewhere-id]
(testing "the action list holds only the app's actions"
(is (= [action-id] (map :id (mt/user-http-request :crowberto :get 200 "action" as-data-app)))))
(testing "the app's action is readable"
(is (=? {:id action-id} (mt/user-http-request :crowberto :get 200 (format "action/%d" action-id) as-data-app))))
(testing "an action outside the app's collection is refused"
(is (= (action-error elsewhere-id)
(mt/user-http-request :crowberto :get 400 (format "action/%d" elsewhere-id) as-data-app))))
(testing "a request that is not a data app's lists and reads both"
(is (= #{action-id elsewhere-id}
(set (filter #{action-id elsewhere-id} (map :id (mt/user-http-request :crowberto :get 200 "action"))))))
(is (=? {:id elsewhere-id} (mt/user-http-request :crowberto :get 200 (format "action/%d" elsewhere-id))))))))))))))
(deftest app-without-collection-uses-no-action-test
(mt/with-actions-test-data-and-actions-enabled
(do-with-app-collection
(fn [_collection-id]
(mt/with-temp [:model/DataApp app {:name "wasps"}
:model/Collection {elsewhere-collection-id :id} {:namespace :data-actions}]
(do-with-rename-action!
elsewhere-collection-id
(fn [elsewhere-id]
(t2/delete! :model/Collection :id (:resource_collection_id app))
(testing "an app without a collection lists no action"
(is (= [] (mt/user-http-request :crowberto :get 200 "action" (as-app "wasps")))))
(testing "and reads or runs none"
(is (= (action-error elsewhere-id)
(mt/user-http-request :crowberto :get 400 (format "action/%d" elsewhere-id) (as-app "wasps"))))
(is (= (action-error elsewhere-id)
(mt/user-http-request :crowberto :post 400 (format "action/%d/execute" elsewhere-id) (as-app "wasps")
{:parameters category-parameters})))))))))))
(deftest table-and-field-endpoints-read-unpublished-tables-test
(do-with-app-collection
(fn [_collection-id]
(let [users (mt/id :users)
id (mt/id :users :id)
uname (mt/id :users :name)]
(testing "a data app reads a table not published to the Library and its fields"
(doseq [url [(format "table/%d" users)
(format "table/%d/query_metadata" users)
(format "table/%d/fks" users)
(format "field/%d" uname)
(format "field/%d/summary" uname)
(format "field/%d/values" uname)
(format "field/%d/search/%d?value=Re" uname uname)
(format "field/%d/remapping/%d?value=1" id uname)
(format "field/%d/related" uname)]]
(mt/user-http-request :crowberto :get 200 url as-data-app)))))))
(deftest database-endpoints-list-unpublished-tables-test
(do-with-app-collection
(fn [_collection-id]
(mt/with-temp [:model/Database db {:engine :h2 :details {}}
:model/Table table {:db_id (:id db) :name "UNPUBLISHED" :schema "PUBLIC"}]
(let [listed (fn [url] (some #(when (= (:id db) (:id %)) %)
(:data (mt/user-http-request :crowberto :get 200 url as-data-app))))]
(testing "the database list holds a database whose tables are not published to the Library, with them"
(is (= [(:id table)] (map :id (:tables (listed "database?include=tables")))))
(is (= ["PUBLIC"] (:schemas (listed "database?include=schemas"))))))
(testing "the database endpoints read it and list its table and schema"
(is (= [(:id table)]
(map :id (:tables (mt/user-http-request :crowberto :get 200 (format "database/%d?include=tables" (:id db))
as-data-app)))))
(is (= ["PUBLIC"] (mt/user-http-request :crowberto :get 200 (format "database/%d/schemas" (:id db)) as-data-app)))
(is (= [(:id table)]
(map :id (mt/user-http-request :crowberto :get 200 (format "database/%d/schema/PUBLIC" (:id db))
as-data-app)))))))))
(deftest search-is-not-narrowed-test
(search.tu/with-appdb-search-and-legacy-search
(search.tu/with-sync-search-indexing
(do-with-app-collection
(fn [_collection-id]
(mt/with-premium-features #{:data-apps :library}
(data-apps.tu/do-with-library!
(fn [_library]
(mt/with-temp [:model/Database db {:engine :h2 :details {}}
:model/Table table {:db_id (:id db) :name "ZEBRA_UNPUBLISHED" :display_name "Zebra unpublished"}
:model/Card metric {:name "Zebra outside metric"
:type :metric
:dataset_query (lib/aggregate (data-apps.tu/venues-query) (lib/count))}]
(testing "a data app finds a table not published to the Library and a metric outside it"
(let [found (set (map (juxt :model :id)
(:data (mt/user-http-request :crowberto :get 200 "search" as-data-app :q "zebra"))))]
(is (contains? found ["table" (:id table)]))
(is (contains? found ["metric" (:id metric)])))))))))))))
(deftest user-key-value-endpoints-allow-only-sdk-keys-test
(do-with-app-collection
(fn [_collection-id]
(let [sdk-key "user-key-value/namespace/last_download_format/key/download_format_preference"
other "user-key-value/namespace/user_acknowledgement/key/seen"]
(testing "a data app reads, writes and clears the key the SDK uses"
(mt/user-http-request :crowberto :put 200 sdk-key as-data-app {:value {:last_download_format "csv"
:last_table_download_format "csv"}})
(is (= {:last_download_format "csv" :last_table_download_format "csv"}
(mt/user-http-request :crowberto :get 200 sdk-key as-data-app)))
(mt/user-http-request :crowberto :delete 200 sdk-key as-data-app))
(testing "a data app is refused any other key"
(doseq [[method body] [[:put {:value true}] [:get nil] [:delete nil]]]
(is (= "A data app can use only the user key-values of the SDK, not the key seen in the namespace user_acknowledgement."
(apply mt/user-http-request :crowberto method 400 other as-data-app (when body [body])))
(name method))))))))
(defn- item-ids
"The `[model id]` pairs of the items that the request to `endpoint` with `options` finds."
[options endpoint]
(set (map (juxt :model :id) (:data (mt/user-http-request :crowberto :get 200 endpoint options)))))
(deftest collection-endpoints-show-only-library-test
(do-with-app-collection
(fn [_collection-id]
(data-apps.tu/do-with-library!
(fn [{:keys [data-id metrics-id]}]
(mt/with-model-cleanup [:model/Collection]
(mt/with-temp [:model/Collection outside {:name "Birds outside"}
:model/Card root-card {:name "Birds root card"}]
(let [library-id (:id (collections/library-collection))
dashboards-id (t2/select-one-pk :model/Collection
:location (str "/" library-id "/")
:type collections/library-dashboards-collection-type)
data-child (mt/user-http-request :crowberto :post 200 "collection" {:name "Birds data" :parent_id data-id})
data-grandchild (mt/user-http-request :crowberto :post 200 "collection" {:name "Birds data nested"
:parent_id (:id data-child)})
metrics-child (mt/user-http-request :crowberto :post 200 "collection" {:name "Birds metrics" :parent_id metrics-id})
metrics-grandchild (mt/user-http-request :crowberto :post 200 "collection" {:name "Birds metrics nested"
:parent_id (:id metrics-child)})]
(testing "the root items of a data app are the Library alone"
(let [found (item-ids as-data-app "collection/root/items")]
(is (contains? found ["collection" library-id]))
(is (not-any? found [["collection" (:id outside)] ["card" (:id root-card)]]))))
(testing "the Library holds its Data, Metrics and Dashboards collections"
(let [found (item-ids as-data-app (str "collection/" library-id "/items"))]
(is (contains? found ["collection" data-id]))
(is (contains? found ["collection" metrics-id]))
(is (contains? found ["collection" dashboards-id]))))
(testing "Data and Metrics hold their nested collections at every level"
(doseq [[parent child] [[data-id data-child]
[(:id data-child) data-grandchild]
[metrics-id metrics-child]
[(:id metrics-child) metrics-grandchild]]]
(is (contains? (item-ids as-data-app (str "collection/" parent "/items")) ["collection" (:id child)])
(:name child))))
(testing "a data app reads the Library, its collections and their nested collections"
(doseq [id (into [library-id data-id metrics-id dashboards-id]
(map :id)
[data-child data-grandchild metrics-child metrics-grandchild])]
(is (= id (:id (mt/user-http-request :crowberto :get 200 (str "collection/" id) as-data-app))))))
(testing "the collection list and the tree of a data app hold the Library, its collections and their nested collections"
(let [visible [library-id data-id metrics-id dashboards-id (:id data-grandchild) (:id metrics-grandchild)]
hidden [(:id outside)]]
(doseq [[label found] [["list" (set (map :id (mt/user-http-request :crowberto :get 200 "collection" as-data-app)))]
["tree" (set (map :id (mapcat #(tree-seq (comp seq :children) :children %)
(mt/user-http-request :crowberto :get 200 "collection/tree" as-data-app))))]]]
(is (every? found visible) label)
(is (not-any? found hidden) label))))
(testing "the shallow tree of a data app holds the nested collections of Data"
(let [found (set (map :id (mt/user-http-request :crowberto :get 200 "collection/tree" as-data-app
:shallow true :collection-id data-id)))]
(is (contains? found (:id data-child)))))
(testing "a data app is refused any other collection and its items"
(doseq [endpoint [(str "collection/" (:id outside)) (str "collection/" (:id outside) "/items")]]
(is (= (format "A data app can see only the Library and its collections, not the collection %s." (:id outside))
(mt/user-http-request :crowberto :get 400 endpoint as-data-app))
endpoint)))
(testing "a request that is not a data app's finds the other collection and the root card"
(let [found (item-ids {} "collection/root/items")]
(is (contains? found ["collection" (:id outside)]))
(is (contains? found ["card" (:id root-card)])))
(is (contains? (set (map :id (mt/user-http-request :crowberto :get 200 "collection"))) (:id outside))))))))))))
(deftest without-data-apps-feature-test
(testing "without the data-apps feature a data app's queries are not checked"
(mt/with-premium-features #{}
(is (= "completed" (:status (run-query 202 (lib/limit (data-apps.tu/venues-query) 1))))))))