Skip to content
Snippets Groups Projects
Unverified Commit 2627bdf6 authored by metamben's avatar metamben Committed by GitHub
Browse files

Hydrate app ID on app collection bookmarks (#25024)

parent e9d84d5d
No related branches found
No related tags found
No related merge requests found
(ns metabase.models.bookmark
(:require [clojure.string :as str]
[metabase.db.connection :as mdb.connection]
[metabase.models.app :refer [App]]
[metabase.models.card :refer [Card]]
[metabase.models.collection :refer [Collection]]
[metabase.models.dashboard :refer [Dashboard]]
......@@ -30,7 +31,8 @@
(s/optional-key :dataset) (s/maybe s/Bool)
(s/optional-key :display) (s/maybe s/Str)
(s/optional-key :authority_level) (s/maybe s/Str)
(s/optional-key :description) (s/maybe s/Str)})
(s/optional-key :description) (s/maybe s/Str)
(s/optional-key :app_id) (s/maybe su/IntGreaterThanOrEqualToZero)})
(s/defn ^:private normalize-bookmark-result :- BookmarkResult
"Normalizes bookmark results. Bookmarks are left joined against the card, collection, and dashboard tables, but only
......@@ -41,7 +43,7 @@
normalized-result (zipmap (map unqualify-key (keys result)) (vals result))
id-str (str (:type normalized-result) "-" (:item_id normalized-result))]
(-> normalized-result
(select-keys [:item_id :type :name :dataset :description :display :authority_level])
(select-keys [:item_id :type :name :dataset :description :display :authority_level :app_id])
(assoc :id id-str))))
(defn- bookmarks-union-query
......@@ -91,11 +93,13 @@
[:collection.name (db/qualify 'Collection :name)]
[:collection.authority_level (db/qualify 'Collection :authority_level)]
[:collection.description (db/qualify 'Collection :description)]
[:collection.archived (db/qualify 'Collection :archived)]]
[:collection.archived (db/qualify 'Collection :archived)]
[:app.id (db/qualify 'Collection :app_id)]]
:from [[(bookmarks-union-query user-id) :bookmark]]
:left-join [[Card :card] [:= :bookmark.card_id :card.id]
[Dashboard :dashboard] [:= :bookmark.dashboard_id :dashboard.id]
[Collection :collection] [:= :bookmark.collection_id :collection.id]
[App :app] [:= :app.collection_id :collection.id]
[BookmarkOrdering :bookmark_ordering] [:and
[:= :bookmark_ordering.user_id user-id]
[:= :bookmark_ordering.type :bookmark.type]
......
(ns metabase.api.bookmark-test
"Tests for /api/bookmark endpoints."
(:require [clojure.test :refer :all]
[metabase.models.app :refer [App]]
[metabase.models.bookmark :refer [BookmarkOrdering CardBookmark
CollectionBookmark
DashboardBookmark]]
......@@ -16,7 +17,8 @@
(testing "POST /api/bookmark/:model/:model-id"
(mt/with-temp* [Collection [collection {:name "Test Collection"}]
Card [card {:name "Test Card" :display "area"}]
Dashboard [dashboard {:name "Test Dashboard"}]]
Dashboard [dashboard {:name "Test Dashboard"}]
App [app {:collection_id (:id collection), :dashboard_id (:id dashboard)}]]
(testing "check that we can bookmark a Collection"
(is (= (u/the-id collection)
(->> (mt/user-http-request :rasta :post 200 (str "bookmark/collection/" (u/the-id collection)))
......@@ -26,7 +28,7 @@
(->> (mt/user-http-request :rasta :post 200 (str "bookmark/card/" (u/the-id card)))
:card_id))))
(let [card-result (->> (mt/user-http-request :rasta :get 200 "bookmark")
(filter #(= (:type % ) "card"))
(filter #(= (:type %) "card"))
first)]
(testing "check a card bookmark has `:display` key"
(is (contains? card-result :display)))
......@@ -37,10 +39,12 @@
(->> (mt/user-http-request :rasta :post 200 (str "bookmark/dashboard/" (u/the-id dashboard)))
:dashboard_id))))
(testing "check that we can retreive the user's bookmarks"
(is (= #{"card" "collection" "dashboard"}
(->> (mt/user-http-request :rasta :get 200 "bookmark")
(map :type)
set))))
(let [result (mt/user-http-request :rasta :get 200 "bookmark")]
(is (= #{"card" "collection" "dashboard"}
(into #{} (map :type) result)))
(testing "that app_id is hydrated on app collections"
(is (partial= [{:app_id (:id app)}]
(filter #(= (:type %) "collection") result))))))
(testing "check that we can delete bookmarks"
(mt/user-http-request :rasta :delete 204 (str "bookmark/card/" (u/the-id card)))
(is (= #{"collection" "dashboard"}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment