Skip to content

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

Backend: search and type-filter metadata for collection items (#79332)

Anderson3ec36759e7fb
diff --git a/src/metabase/collections_rest/api.clj b/src/metabase/collections_rest/api.clj
index c1d0fe01999..1b5b14b25ab 100644
--- a/src/metabase/collections_rest/api.clj
+++ b/src/metabase/collections_rest/api.clj
@@ -344,6 +344,7 @@
[:pinned-state {:optional true} [:maybe (into [:enum] (map keyword) valid-pinned-state-values)]]
;; when specified, only return results of this type.
[:models {:optional true} [:maybe [:set (into [:enum] (map keyword) valid-model-param-values)]]]
+ [:search-text {:optional true} [:maybe :string]]
[:sort-info {:optional true} [:maybe [:map
[:sort-column (into [:enum {:error/message "sort-columns"}]
(map normalize-sort-choice)
@@ -382,6 +383,26 @@
1 = 2"
[:= [:inline 1] [:inline 2]])
+(defn- escape-like-pattern
+ "Escape characters that have special meaning in a SQL LIKE pattern so they match literally."
+ ^String [^String s]
+ (str/replace s #"([\\%_])" "\\\\$1"))
+
+(defn- search-text-clause
+ "Match every token in `search-text` against an item's name or last editor's first or last name."
+ [search-text]
+ (when-not (str/blank? search-text)
+ (when-let [tokens (->> (str/split (u/lower-case-en (str/trim search-text)) #"\s+")
+ (filter seq)
+ not-empty)]
+ (into [:and]
+ (for [token tokens
+ :let [pattern (str "%" (escape-like-pattern token) "%")]]
+ [:or
+ [:like [:lower :name] pattern]
+ [:like [:lower :last_edit_first_name] pattern]
+ [:like [:lower :last_edit_last_name] pattern]])))))
+
(defn- pinned-state->clause
([pinned-state]
(pinned-state->clause pinned-state :collection_position))
@@ -1145,71 +1166,89 @@
;; whatever
[[:id :asc]]]))
+(defn- total-count
+ "The size of the whole result set `rows` is a page of, read off the `total_count` window column.
+
+ A page past the end of the result set comes back empty and so carries no window column; in that case read the count
+ off the first row of the same query without its pagination."
+ [rows rows-query offset]
+ (or (some-> rows first :total_count)
+ (when (pos? (or offset 0))
+ (some-> (mdb/query (assoc rows-query :limit 1)) first :total_count))
+ 0))
+
(defn- collection-children*
- [collection models {:keys [sort-info archived?] :as options}]
- (let [sql-order (children-sort-clause sort-info (mdb/db-type))
- models (sort (map keyword models))
- queries (for [model models
- :let [query (collection-children-query model collection options)
- select-clause-type (some
- (fn [k]
- (when (get query k)
- k))
- [:select :select-distinct])]]
- (-> query
- (update select-clause-type add-missing-columns all-select-columns)
- (update select-clause-type add-model-ranking model)))
- viz-config {:include-archived-items :all
- :archive-operation-id nil
- :permission-level (if archived? :write :read)
- :include-trash-collection? archived?}
- rows-query {:with [[:visible_collection_ids (collection/visible-collection-query viz-config)]]
- :select [:* [[:over [[:count :*] {} :total_count]]]]
- :from [[{:union-all queries} :dummy_alias]]
- :order-by sql-order}
- limit (request/limit)
- offset (request/offset)
+ [collection models {:keys [sort-info archived? search-text] :as options}]
+ (let [sql-order (children-sort-clause sort-info (mdb/db-type))
+ models (sort (map keyword models))
+ queries (for [model models
+ :let [query (collection-children-query model collection options)
+ select-clause-type (some
+ (fn [k]
+ (when (get query k)
+ k))
+ [:select :select-distinct])]]
+ (-> query
+ (update select-clause-type add-missing-columns all-select-columns)
+ (update select-clause-type add-model-ranking model)))
+ viz-config {:include-archived-items :all
+ :archive-operation-id nil
+ :permission-level (if archived? :write :read)
+ :include-trash-collection? archived?}
+ search-clause (search-text-clause search-text)
+ rows-query (cond-> {:with [[:visible_collection_ids (collection/visible-collection-query viz-config)]]
+ :select [:* [[:over [[:count :*] {} :total_count]]]]
+ :from [[{:union-all queries} :dummy_alias]]
+ :order-by sql-order}
+ search-clause
+ (sql.helpers/where search-clause))
+ limit (request/limit)
+ offset (request/offset)
;; We didn't implement collection pagination for snippets namespace for root/items
;; Rip out the limit for now and put it back in when we want it
- limit-query (if (or
- (nil? limit)
- (nil? offset)
- (= (:collection-namespace options) "snippets"))
- rows-query
- (assoc rows-query
- ;; If limit is 0, we still execute the query with a limit of 1 so that we fetch a
- ;; :total_count
- :limit (if (zero? limit) 1 limit)
- :offset offset))
- rows (tracing/with-span :db-app "db-app.collection-items-query" {:collection/id (:id collection)}
- (mdb/query limit-query))
- res {:total (->> rows first :total_count)
- :data (if (= limit 0)
- []
- (tracing/with-span :db-app "db-app.collection-items-post-process" {:collection/id (:id collection)}
- (post-process-rows options collection rows)))
- :models models}
- limit-res (assoc res
- :limit (request/limit)
- :offset (request/offset))]
+ limit-query (if (or
+ (nil? limit)
+ (nil? offset)
+ (= (:collection-namespace options) "snippets"))
+ rows-query
+ (assoc rows-query
+ ;; If limit is 0, we still execute the query with a limit of 1 so that we fetch a
+ ;; :total_count
+ :limit (if (zero? limit) 1 limit)
+ :offset offset))
+ rows (tracing/with-span :db-app "db-app.collection-items-query" {:collection/id (:id collection)}
+ (mdb/query limit-query))
+ res {:total (total-count rows rows-query offset)
+ :data (if (= limit 0)
+ []
+ (tracing/with-span :db-app "db-app.collection-items-post-process" {:collection/id (:id collection)}
+ (post-process-rows options collection rows)))
+ :models models}
+ limit-res (assoc res
+ :limit (request/limit)
+ :offset (request/offset))]
(if (= (:collection-namespace options) "snippets")
res
limit-res)))
+(defn- valid-collection-models
+ "Return every item model that can appear in `collection-namespace`."
+ [collection-namespace]
+ (for [model-kw (cond-> [:collection :dataset :metric :card :dashboard :pulse :snippet :timeline :document :exploration :transform]
+ ;; Tables in collections are an EE feature (library)
+ (premium-features/has-feature? :library) (conj :table))
+ :let [toucan-model (model-name->toucan-model model-kw)
+ allowed-namespaces (collection/allowed-namespaces toucan-model)]
+ :when (or (= model-kw :collection)
+ (contains? allowed-namespaces (keyword collection-namespace)))]
+ model-kw))
+
(mu/defn collection-children
"Fetch a sequence of 'child' objects belonging to a Collection, filtered using `options`."
[{collection-namespace :namespace, :as collection} :- collection/CollectionWithLocationAndIDOrRoot
{:keys [models], :as options} :- CollectionChildrenOptions]
- (let [valid-models (for [model-kw (cond-> [:collection :dataset :metric :card :dashboard :pulse :snippet :timeline :document :exploration :transform]
- ;; Tables in collections are an EE feature (library)
- (premium-features/has-feature? :library) (conj :table))
- ;; only fetch models that are specified by the `model` param; or everything if it's empty
- :when (or (empty? models) (contains? models model-kw))
- :let [toucan-model (model-name->toucan-model model-kw)
- allowed-namespaces (collection/allowed-namespaces toucan-model)]
- :when (or (= model-kw :collection)
- (contains? allowed-namespaces (keyword collection-namespace)))]
- model-kw)]
+ (let [valid-models (cond->> (valid-collection-models collection-namespace)
+ (seq models) (filter models))]
(if (seq valid-models)
(collection-children* collection valid-models (assoc options :collection-namespace collection-namespace))
{:total 0
@@ -1218,6 +1257,41 @@
:offset (request/offset)
:models valid-models})))
+(mu/defn- collection-filter-metadata :- [:map
+ [:available_models [:sequential :string]]]
+ "Return the models that have at least one visible item in `collection`. Respect the requested scope and visibility,
+ but ignore model and search filters. When present, `restrict-models` limits the candidate models. Snippets are never
+ reported: they are not a filterable type."
+ [collection :- collection/CollectionWithLocationAndIDOrRoot
+ restrict-models :- [:maybe [:set :keyword]]
+ {:keys [archived?] :as options} :- CollectionChildrenOptions]
+ (let [candidates (cond->> (remove #{:snippet} (valid-collection-models (:namespace collection)))
+ (seq restrict-models) (filter restrict-models))
+ options (-> options
+ (dissoc :models :search-text)
+ (assoc :collection-namespace (:namespace collection)))]
+ ;; This result is independent of search and model filters. Requesting it with every filter update
+ ;; repeats the same EXISTS probes; a separately cached request could avoid that work.
+ (if (empty? candidates)
+ {:available_models []}
+ (let [viz-config {:include-archived-items :all
+ :archive-operation-id nil
+ :permission-level (if archived? :write :read)
+ :include-trash-collection? archived?}
+ row (first
+ (mdb/query
+ {:with [[:visible_collection_ids (collection/visible-collection-query viz-config)]]
+ :select (vec
+ (for [model candidates]
+ [[:exists (collection-children-query model collection options)] model]))}))]
+ {:available_models
+ (->> candidates
+ (keep (fn [model]
+ (when (api/bit->boolean (get row model))
+ (name model))))
+ sort
+ vec)}))))
+
(mu/defn- collection-detail
"Add a standard set of details to `collection`, including things like `effective_location`.
Works for either a normal Collection or the Root Collection."
@@ -1412,44 +1486,54 @@
By default, library collections are excluded from the results; to include them, pass `?include_library=true`.
+ Pass `?q=` to filter items by name or last editor. Pass `?include_available_models=true` to include the models that
+ have at least one visible item in the requested scope.
+
Note that this endpoint should return results in a similar shape to `/api/dashboard/:id/items`, so if this is
changed, that should too."
[_route-params
{:keys [models archived namespace pinned_state sort_column sort_direction official_collections_first
include_can_run_adhoc_query include_library collection_type
- show_dashboard_questions]} :- [:map
- [:models {:optional true} [:maybe Models]]
- [:collection_type {:optional true} CollectionType]
- [:include_can_run_adhoc_query {:default false} [:maybe ms/BooleanValue]]
- [:archived {:default false} [:maybe ms/BooleanValue]]
- [:namespace {:optional true} [:maybe ms/NonBlankString]]
- [:include_library {:default false} [:maybe ms/BooleanValue]]
- [:pinned_state {:optional true} [:maybe (into [:enum] valid-pinned-state-values)]]
- [:sort_column {:optional true} [:maybe (into [:enum] valid-sort-columns)]]
- [:sort_direction {:optional true} [:maybe (into [:enum] valid-sort-directions)]]
- [:official_collections_first {:optional true} [:maybe ms/MaybeBooleanValue]]
- [:show_dashboard_questions {:optional true} [:maybe ms/MaybeBooleanValue]]]]
+ show_dashboard_questions q include_available_models]} :- [:map
+ [:models {:optional true} [:maybe Models]]
+ [:collection_type {:optional true} CollectionType]
+ [:include_can_run_adhoc_query {:default false} [:maybe ms/BooleanValue]]
+ [:archived {:default false} [:maybe ms/BooleanValue]]
+ [:namespace {:optional true} [:maybe ms/NonBlankString]]
+ [:include_library {:default false} [:maybe ms/BooleanValue]]
+ [:pinned_state {:optional true} [:maybe (into [:enum] valid-pinned-state-values)]]
+ [:sort_column {:optional true} [:maybe (into [:enum] valid-sort-columns)]]
+ [:sort_direction {:optional true} [:maybe (into [:enum] valid-sort-directions)]]
+ [:official_collections_first {:optional true} [:maybe ms/MaybeBooleanValue]]
+ [:show_dashboard_questions {:optional true} [:maybe ms/MaybeBooleanValue]]
+ [:q {:optional true} [:maybe :string]]
+ [:include_available_models {:default false} [:maybe ms/BooleanValue]]]]
;; Return collection contents, including Collections that have an effective location of being in the Root
;; Collection for the Current User.
(let [root-collection (assoc collection/root-collection :namespace namespace)
model-set (set (map keyword (u/one-or-many models)))
- model-kwds (visible-model-kwds root-collection model-set)]
- (collection-children
- root-collection
- {:archived? (boolean archived)
- :include-can-run-adhoc-query include_can_run_adhoc_query
- :show-dashboard-questions? (boolean show_dashboard_questions)
- :collection-type collection_type
- :include-library? include_library
- :models (if-not (contains? namespaces-holding-non-collection-types namespace)
- #{:collection}
- model-kwds)
- :pinned-state (keyword pinned_state)
- :sort-info {:sort-column (or (some-> sort_column normalize-sort-choice) :name)
- :sort-direction (or (some-> sort_direction normalize-sort-choice) :asc)
- ;; default to sorting official collections first, but provide the option not to
- :official-collections-first? (or (nil? official_collections_first)
- (boolean official_collections_first))}})))
+ model-kwds (visible-model-kwds root-collection model-set)
+ restrict-models (when (or (not (contains? namespaces-holding-non-collection-types namespace))
+ (not (mi/can-read? root-collection)))
+ #{:collection})
+ options {:archived? (boolean archived)
+ :include-can-run-adhoc-query include_can_run_adhoc_query
+ :show-dashboard-questions? (boolean show_dashboard_questions)
+ :collection-type collection_type
+ :include-library? include_library
+ :models (if-not (contains? namespaces-holding-non-collection-types namespace)
+ #{:collection}
+ model-kwds)
+ :pinned-state (keyword pinned_state)
+ :search-text q
+ :sort-info {:sort-column (or (some-> sort_column normalize-sort-choice) :name)
+ :sort-direction (or (some-> sort_direction normalize-sort-choice) :asc)
+ ;; default to sorting official collections first, but provide the option not to
+ :official-collections-first? (or (nil? official_collections_first)
+ (boolean official_collections_first))}}]
+ (cond-> (collection-children root-collection options)
+ include_available_models
+ (merge (collection-filter-metadata root-collection restrict-models options)))))
;;; ----------------------------------------- Creating/Editing a Collection ------------------------------------------
@@ -1715,6 +1799,8 @@
when `is_not_pinned`, return non pinned objects only.
when `all`, return everything. By default returns everything.
* `include_can_run_adhoc_query` - when this is true hydrates the `can_run_adhoc_query` flag on card models
+ * `q` - filter items by name or last editor. Blank or whitespace-only values are ignored.
+ * `include_available_models` - include the models that have at least one visible item in the requested scope.
Note that this endpoint should return results in a similar shape to `/api/dashboard/:id/items`, so if this is
changed, that should too."
@@ -1722,30 +1808,36 @@
[:id [:or ms/PositiveInt ms/NanoIdString]]]
{:keys [models archived pinned_state sort_column sort_direction official_collections_first
include_can_run_adhoc_query
- show_dashboard_questions]} :- [:map
- [:models {:optional true} [:maybe Models]]
- [:archived {:default false} [:maybe ms/BooleanValue]]
- [:include_can_run_adhoc_query {:default false} [:maybe ms/BooleanValue]]
- [:pinned_state {:optional true} [:maybe (into [:enum] valid-pinned-state-values)]]
- [:sort_column {:optional true} [:maybe (into [:enum] valid-sort-columns)]]
- [:sort_direction {:optional true} [:maybe (into [:enum] valid-sort-directions)]]
- [:official_collections_first {:optional true} [:maybe ms/MaybeBooleanValue]]
- [:show_dashboard_questions {:default false} [:maybe ms/BooleanValue]]]]
+ show_dashboard_questions q include_available_models]} :- [:map
+ [:models {:optional true} [:maybe Models]]
+ [:archived {:default false} [:maybe ms/BooleanValue]]
+ [:include_can_run_adhoc_query {:default false} [:maybe ms/BooleanValue]]
+ [:pinned_state {:optional true} [:maybe (into [:enum] valid-pinned-state-values)]]
+ [:sort_column {:optional true} [:maybe (into [:enum] valid-sort-columns)]]
+ [:sort_direction {:optional true} [:maybe (into [:enum] valid-sort-directions)]]
+ [:official_collections_first {:optional true} [:maybe ms/MaybeBooleanValue]]
+ [:show_dashboard_questions {:default false} [:maybe ms/BooleanValue]]
+ [:q {:optional true} [:maybe :string]]
+ [:include_available_models {:default false} [:maybe ms/BooleanValue]]]]
(let [resolved-id (eid-translation/->id-or-404 :collection id)
- model-kwds (set (map keyword (u/one-or-many models)))
- collection (api/read-check :model/Collection resolved-id)]
- (u/prog1 (collection-children collection
- {:show-dashboard-questions? show_dashboard_questions
- :models model-kwds
- :include-library? true
- :archived? (or archived (:archived collection) (collection/is-trash? collection))
- :pinned-state (keyword pinned_state)
- :include-can-run-adhoc-query include_can_run_adhoc_query
- :sort-info {:sort-column (or (some-> sort_column normalize-sort-choice) :name)
- :sort-direction (or (some-> sort_direction normalize-sort-choice) :asc)
- ;; default to sorting official collections first, except for the trash.
- :official-collections-first? (if (and (nil? official_collections_first)
- (not (collection/is-trash? collection)))
- true
- (boolean official_collections_first))}})
- (events/publish-event! :event/collection-read {:object collection :user-id api/*current-user-id*}))))
+ model-kwds (set (map keyword (u/one-or-many models)))
+ collection (api/read-check :model/Collection resolved-id)
+ options {:show-dashboard-questions? show_dashboard_questions
+ :models model-kwds
+ :include-library? true
+ :archived? (or archived (:archived collection) (collection/is-trash? collection))
+ :pinned-state (keyword pinned_state)
+ :include-can-run-adhoc-query include_can_run_adhoc_query
+ :search-text q
+ :sort-info {:sort-column (or (some-> sort_column normalize-sort-choice) :name)
+ :sort-direction (or (some-> sort_direction normalize-sort-choice) :asc)
+ ;; default to sorting official collections first, except for the trash.
+ :official-collections-first? (if (and (nil? official_collections_first)
+ (not (collection/is-trash? collection)))
+ true
+ (boolean official_collections_first))}}
+ children (cond-> (collection-children collection options)
+ include_available_models
+ (merge (collection-filter-metadata collection nil options)))]
+ (events/publish-event! :event/collection-read {:object collection :user-id api/*current-user-id*})
+ children))
diff --git a/test/metabase/collections_rest/api_test.clj b/test/metabase/collections_rest/api_test.clj
index e70ec80cf62..8b5a98b61a0 100644
--- a/test/metabase/collections_rest/api_test.clj
+++ b/test/metabase/collections_rest/api_test.clj
@@ -878,6 +878,304 @@
(map :name)
set))))))))
+(deftest collection-items-search-test
+ (testing "GET /api/collection/:id/items"
+ (mt/with-temp [:model/Collection collection {}
+ :model/Card _ {:name "Revenue by month"
+ :collection_id (u/the-id collection)
+ :collection_position 1}
+ :model/Dashboard _ {:name "Revenue overview" :collection_id (u/the-id collection)}
+ :model/Collection child-collection {:name "Old revenue stuff"
+ :location (collection/children-location collection)}
+ :model/Card _ {:name "Customer 360" :collection_id (u/the-id collection)}
+ :model/Timeline _ {:name "Events" :collection_id (u/the-id collection)}
+ :model/Card _ {:name "Nested revenue" :collection_id (u/the-id child-collection)}]
+ (letfn [(fetch [& params]
+ (apply mt/user-http-request
+ :crowberto :get 200 (str "collection/" (u/the-id collection) "/items") params))
+ (item-identities [response]
+ (set (map (juxt :model :name) (:data response))))]
+ (testing "matches item names case-insensitively and only within the requested collection"
+ (let [response (fetch :q "revenue")]
+ (is (= 3 (:total response)))
+ (is (= #{["card" "Revenue by month"]
+ ["collection" "Old revenue stuff"]
+ ["dashboard" "Revenue overview"]}
+ (item-identities response)))))
+ (testing "requires every search token to match"
+ (is (= #{["dashboard" "Revenue overview"]}
+ (item-identities (fetch :q "REVENUE overview")))))
+ (testing "returns an empty page and filtered total when nothing matches"
+ (is (= {:data [] :total 0}
+ (select-keys (fetch :q "zzz") [:data :total]))))
+ (testing "combines search with the model filter"
+ (is (= #{["card" "Revenue by month"]}
+ (item-identities (fetch :q "revenue" :models "card")))))
+ (testing "combines search with the pinned-state filter"
+ (is (= #{["collection" "Old revenue stuff"]
+ ["dashboard" "Revenue overview"]}
+ (item-identities (fetch :q "revenue" :pinned_state "is_not_pinned")))))
+ (testing "trims search text and treats blank search text as absent"
+ (let [all-items (item-identities (fetch))]
+ (is (= (item-identities (fetch :q "revenue"))
+ (item-identities (fetch :q " revenue "))))
+ (is (= all-items (item-identities (fetch :q ""))))
+ (is (= all-items (item-identities (fetch :q " "))))
+ (is (= 5 (count all-items)))))
+ (testing "paginates the filtered set"
+ (let [first-page (fetch :q "revenue" :limit "1" :offset "0")
+ second-page (fetch :q "revenue" :limit "1" :offset "1")
+ out-of-range-page (fetch :q "revenue" :limit "1" :offset "3")]
+ (is (= 3 (:total first-page)))
+ (is (= 3 (:total second-page)))
+ (is (= 3 (:total out-of-range-page)))
+ (is (= 1 (count (:data first-page))))
+ (is (= 1 (count (:data second-page))))
+ (is (not= (item-identities first-page) (item-identities second-page)))
+ (is (= [] (:data out-of-range-page)))))))))
+
+(deftest collection-items-search-by-last-editor-test
+ (testing "GET /api/collection/:id/items"
+ (mt/with-temp [:model/Collection collection {}
+ :model/Card {rasta-card-id :id :as rasta-card} {:name "Quarterly sales"
+ :collection_id (u/the-id collection)}
+ :model/Card {lucky-card-id :id :as lucky-card} {:name "Annual sales"
+ :collection_id (u/the-id collection)}
+ :model/Collection _ {:name "Bird sightings"
+ :location (collection/children-location collection)}
+ :model/Revision _ {:model "Card"
+ :model_id rasta-card-id
+ :user_id (mt/user->id :rasta)
+ :object (revision/serialize-instance
+ rasta-card rasta-card-id rasta-card)}
+ :model/Revision _ {:model "Card"
+ :model_id rasta-card-id
+ :user_id (mt/user->id :crowberto)
+ :object (revision/serialize-instance
+ rasta-card rasta-card-id rasta-card)}
+ :model/Revision _ {:model "Card"
+ :model_id lucky-card-id
+ :user_id (mt/user->id :lucky)
+ :object (revision/serialize-instance
+ lucky-card lucky-card-id lucky-card)}]
+ (letfn [(item-names [q]
+ (->> (mt/user-http-request :crowberto :get 200
+ (str "collection/" (u/the-id collection) "/items")
+ :q q)
+ :data
+ (map :name)
+ set))]
+ (testing "matches only the most recent editor's first and last names"
+ (is (= #{"Quarterly sales"} (item-names "crowberto")))
+ (is (= #{"Quarterly sales"} (item-names "corv")))
+ (is (= #{"Quarterly sales"} (item-names "crowberto corv")))
+ (is (= #{} (item-names "rasta")))
+ (is (= #{} (item-names "toucan"))))
+ (testing "does not match items edited by someone else"
+ (is (= #{"Annual sales"} (item-names "lucky"))))
+ (testing "still matches a collection without last-edit information by name"
+ (is (= #{"Bird sightings"} (item-names "bird sightings"))))))))
+
+(deftest collection-items-search-escapes-like-wildcards-test
+ (testing "GET /api/collection/:id/items"
+ (mt/with-temp [:model/Collection collection {}
+ :model/Card _ {:name "100% done" :collection_id (u/the-id collection)}
+ :model/Card _ {:name "100x done" :collection_id (u/the-id collection)}
+ :model/Card _ {:name "a_b" :collection_id (u/the-id collection)}
+ :model/Card _ {:name "axb" :collection_id (u/the-id collection)}
+ :model/Card _ {:name "path\\name" :collection_id (u/the-id collection)}
+ :model/Card _ {:name "pathname" :collection_id (u/the-id collection)}]
+ (letfn [(item-names [q]
+ (->> (mt/user-http-request :crowberto :get 200
+ (str "collection/" (u/the-id collection) "/items")
+ :q q)
+ :data
+ (map :name)
+ set))]
+ (is (= #{"100% done"} (item-names "100%")))
+ (is (= #{"a_b"} (item-names "a_b")))
+ (is (= #{"path\\name"} (item-names "path\\name")))))))
+
+(deftest collection-items-available-models-test
+ (testing "GET /api/collection/:id/items"
+ (mt/with-temp [:model/Collection collection {}
+ :model/Card _ {:name "Question" :collection_id (u/the-id collection)}
+ :model/Dashboard _ {:name "Dashboard" :collection_id (u/the-id collection)}
+ :model/Card _ {:name "Metric"
+ :type :metric
+ :collection_id (u/the-id collection)
+ :collection_position 1}
+ :model/Collection _ {:name "Child collection"
+ :location (collection/children-location collection)}
+ :model/Timeline _ {:name "Timeline" :collection_id (u/the-id collection)}]
+ (letfn [(fetch [& params]
+ (apply mt/user-http-request
+ :crowberto :get 200 (str "collection/" (u/the-id collection) "/items") params))]
+ (let [all-models ["card" "collection" "dashboard" "metric" "timeline"]
+ unpinned-models ["card" "collection" "dashboard" "timeline"]
+ all-response (fetch :include_available_models true)
+ unpinned-response (fetch :include_available_models true :pinned_state "is_not_pinned")
+ pinned-response (fetch :include_available_models true :pinned_state "is_pinned")
+ search-response (fetch :include_available_models true :q "zzz")
+ models-response (fetch :include_available_models true :models "card")]
+ (testing "is absent unless explicitly requested"
+ (let [response (fetch)]
+ (is (not (contains? response :available_models)))))
+ (testing "is sorted and respects the pinned-state scope"
+ (is (= all-models (:available_models all-response)))
+ (is (= unpinned-models (:available_models unpinned-response)))
+ (is (= ["metric"] (:available_models pinned-response))))
+ (testing "ignores search text"
+ (is (= all-models (:available_models search-response)))
+ (is (= [] (:data search-response)))
+ (is (= 0 (:total search-response))))
+ (testing "ignores the requested models"
+ (is (= all-models (:available_models models-response)))
+ (is (= #{["card" "Question"]}
+ (set (map (juxt :model :name) (:data models-response)))))))))))
+
+(deftest collection-items-available-models-exploration-test
+ (testing "GET /api/collection/:id/items"
+ (mt/with-temp [:model/User owner {}
+ :model/Collection collection {}
+ :model/Card _ {:name "Question" :collection_id (u/the-id collection)}
+ :model/Exploration _ {:name "Exploration"
+ :creator_id (u/the-id owner)
+ :collection_id (u/the-id collection)}]
+ (testing "reports explorations so they can be filtered on"
+ (let [response (mt/user-http-request :crowberto :get 200
+ (str "collection/" (u/the-id collection) "/items")
+ :include_available_models true)]
+ (is (= ["card" "exploration"] (:available_models response))))))))
+
+(deftest collection-items-available-models-library-test
+ (testing "GET /api/collection/:id/items"
+ (mt/with-temp [:model/Collection collection {:type "library-data"}
+ :model/Table _ {:collection_id (u/the-id collection)
+ :is_published true}]
+ (letfn [(fetch []
+ (mt/user-http-request :crowberto :get 200
+ (str "collection/" (u/the-id collection) "/items")
+ :include_available_models true))]
+ (testing "excludes tables when the library feature is disabled"
+ (mt/with-premium-features #{}
+ (let [response (fetch)]
+ (is (= [] (:available_models response)))
+ (is (= [] (:data response))))))
+ (testing "includes tables when the library feature is enabled"
+ (mt/with-premium-features #{:library}
+ (let [response (fetch)]
+ (is (= ["table"] (:available_models response)))
+ (is (= ["table"] (mapv :model (:data response)))))))))))
+
+(deftest collection-items-available-models-permissions-test
+ (testing "GET /api/collection/:id/items"
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-temp [:model/Collection parent-collection {}
+ :model/Collection child-collection {:location (collection/children-location parent-collection)}]
+ (let [group (perms/all-users-group)]
+ (perms/revoke-collection-permissions! group parent-collection)
+ (perms/revoke-collection-permissions! group child-collection)
+ (perms/grant-collection-read-permissions! group parent-collection)
+ (letfn [(fetch []
+ (mt/user-http-request :rasta :get 200
+ (str "collection/" (u/the-id parent-collection) "/items")
+ :include_available_models true))]
+ (testing "does not include a child collection the user cannot read"
+ (let [response (fetch)]
+ (is (not (contains? (set (:available_models response)) "collection")))
+ (is (not-any? #(= (:id child-collection) (:id %)) (:data response)))))
+ (testing "includes the child collection after read permission is granted"
+ (perms/grant-collection-read-permissions! group child-collection)
+ (let [response (fetch)]
+ (is (contains? (set (:available_models response)) "collection"))
+ (is (some #(= (:id child-collection) (:id %)) (:data response)))))))))))
+
+(deftest collection-items-search-and-available-models-archived-test
+ (testing "GET /api/collection/:id/items"
+ (mt/with-temp [:model/Collection collection {}
+ :model/Card _ {:name "Old revenue"
+ :collection_id (u/the-id collection)
+ :archived true
+ :archived_directly false}
+ :model/Collection _ {:name "Archived collection"
+ :location (collection/children-location collection)
+ :archived true}
+ :model/Dashboard _ {:name "Current revenue" :collection_id (u/the-id collection)}]
+ (let [response (mt/user-http-request :crowberto :get 200
+ (str "collection/" (u/the-id collection) "/items")
+ :archived true
+ :q "revenue"
+ :include_available_models true)]
+ (is (= 1 (:total response)))
+ (is (= #{["card" "Old revenue"]}
+ (set (map (juxt :model :name) (:data response)))))
+ (is (= ["card" "collection"] (:available_models response)))))))
+
+(deftest trash-collection-items-search-test
+ (testing "GET /api/collection/:trash-id/items"
+ (mt/with-temp [:model/Collection collection {}
+ :model/Card matching-card {:name "Trashed quarterly revenue"
+ :collection_id (u/the-id collection)
+ :archived true
+ :archived_directly true}
+ :model/Card _ {:name "Trashed customer count"
+ :collection_id (u/the-id collection)
+ :archived true
+ :archived_directly true}
+ :model/Card _ {:name "Indirect quarterly revenue"
+ :collection_id (u/the-id collection)
+ :archived true
+ :archived_directly false}]
+ (let [response (mt/user-http-request :crowberto :get 200
+ (str "collection/" (collection/trash-collection-id) "/items")
+ :q "quarterly revenue"
+ :include_available_models true)]
+ (is (= 1 (:total response)))
+ (is (= #{[(:id matching-card) "Trashed quarterly revenue"]}
+ (set (map (juxt :id :name) (:data response)))))
+ (is (= ["card"] (:available_models response)))))))
+
+(deftest root-collection-items-search-and-available-models-test
+ (testing "GET /api/collection/root/items"
+ (mt/with-temp [:model/Card _ {:name "UXW4950 root revenue" :collection_id nil}
+ :model/Dashboard _ {:name "UXW4950 root dashboard" :collection_id nil}
+ :model/Collection visible-collection {:name "UXW4950 visible child" :location "/"}
+ :model/Collection currency-collection {:name "UXW4950 currency child"
+ :namespace "currency"
+ :location "/"}
+ :model/NativeQuerySnippet _ {:name "UXW4950 root snippet"}]
+ (testing "searches root items and reports models before search filtering"
+ (let [response (mt/user-http-request :crowberto :get 200 "collection/root/items"
+ :q "UXW4950 ROOT revenue"
+ :include_available_models true)]
+ (is (= 1 (:total response)))
+ (is (= #{["card" "UXW4950 root revenue"]}
+ (set (map (juxt :model :name) (:data response)))))
+ (is (set/subset? #{"card" "collection" "dashboard"}
+ (set (:available_models response))))))
+ (testing "restricts metadata to namespace-valid models"
+ (let [response (mt/user-http-request :crowberto :get 200 "collection/root/items"
+ :namespace "currency"
+ :include_available_models true)]
+ (is (= ["collection"] (:available_models response)))
+ (is (some #(= (:id currency-collection) (:id %)) (:data response)))))
+ (testing "never reports snippets in available models"
+ (let [response (mt/user-http-request :crowberto :get 200 "collection/root/items"
+ :namespace "snippets"
+ :include_available_models true)]
+ (is (some #(= (:model %) "snippet") (:data response)))
+ (is (not (contains? (set (:available_models response)) "snippet")))))
+ (testing "restricts metadata to collections for a user without root read permission"
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (perms/grant-collection-read-permissions! (perms/all-users-group) visible-collection)
+ (let [response (mt/user-http-request :rasta :get 200 "collection/root/items"
+ :q "UXW4950 ROOT revenue"
+ :include_available_models true)
+ available-models (set (:available_models response))]
+ (is (= [] (:data response)))
+ (is (= #{"collection"} available-models))))))))
+
(deftest collection-items-children-test
(testing "GET /api/collection/:id/items"
(testing "check that you get to see the children as appropriate"