Skip to content

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

Enforce remote-sync dependency guard on card bulk-move (GHY-3791) (#76278)

Eric Normand6bfb3cc0aecd
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/card_api_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/card_api_test.clj
new file mode 100644
index 00000000000..b5d99b41f58
--- /dev/null
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/card_api_test.clj
@@ -0,0 +1,61 @@
+(ns ^:synchronous metabase-enterprise.remote-sync.card-api-test
+ {:clj-kondo/config '{:linters {:deprecated-var {:exclude {metabase.test.data/mbql-query {:namespaces [metabase-enterprise.remote-sync.card-api-test]}}}}}}
+ (:require
+ [clojure.test :refer :all]
+ [metabase.test :as mt]
+ [toucan2.core :as t2]))
+
+(use-fixtures :each (fn [f]
+ (mt/with-temporary-setting-values [remote-sync-type :read-write]
+ (f))))
+
+(deftest bulk-move-into-remote-synced-with-non-remote-synced-deps-test
+ (testing "POST /api/card/collections rejects bulk-moving a card with non-remote-synced dependencies into a remote-synced collection (GHY-3791)"
+ (mt/with-temp [:model/Collection {regular-id :id} {:name "Regular" :location "/" :type nil}
+ :model/Collection {synced-id :id} {:name "Remote-Synced" :location "/" :is_remote_synced true}
+ :model/Card {source-card-id :id} {:name "Non-remote-synced source card"
+ :collection_id regular-id
+ :dataset_query (mt/native-query {:query "SELECT 1"})}
+ ;; Card lives at the root (collection_id nil): its pre-update collection_id is the
+ ;; short-circuit case in non-remote-synced-dependencies, exposing the stale-object bug.
+ :model/Card {root-card-id :id} {:name "Card depending on non-synced source"
+ :collection_id nil
+ :dataset_query (mt/mbql-query nil {:source-table (str "card__" source-card-id)})}]
+ (let [response (mt/user-http-request :crowberto :post 400 "card/collections"
+ {:collection_id synced-id
+ :card_ids [root-card-id]})]
+ (is (= "Uses content that is not remote synced." (:message response))))
+ (testing "card is not moved - transaction rolled back"
+ (is (nil? (t2/select-one-fn :collection_id :model/Card root-card-id)))))))
+
+(deftest bulk-move-depended-on-card-out-of-remote-synced-test
+ (testing "POST /api/card/collections rejects bulk-moving a remote-synced card that is depended on by other remote-synced content out of the synced set (GHY-3791)"
+ (mt/with-temp [:model/Collection {synced-id :id} {:name "Remote-Synced" :location "/" :is_remote_synced true}
+ :model/Collection {regular-id :id} {:name "Regular" :location "/" :type nil}
+ :model/Card {source-card-id :id} {:name "Remote-synced source card"
+ :collection_id synced-id
+ :dataset_query (mt/native-query {:query "SELECT 1"})}
+ :model/Card {_dependent-id :id} {:name "Remote-synced dependent card"
+ :collection_id synced-id
+ :dataset_query (mt/mbql-query nil {:source-table (str "card__" source-card-id)})}]
+ (let [response (mt/user-http-request :crowberto :post 400 "card/collections"
+ {:collection_id regular-id
+ :card_ids [source-card-id]})]
+ (is (= "Used by remote synced content." (:message response))))
+ (testing "card is not moved - transaction rolled back"
+ (is (= synced-id (t2/select-one-fn :collection_id :model/Card source-card-id)))))))
+
+(deftest bulk-move-into-remote-synced-with-remote-synced-deps-succeeds-test
+ (testing "POST /api/card/collections allows bulk-moving a card whose dependencies are all remote-synced into a remote-synced collection (GHY-3791)"
+ (mt/with-temp [:model/Collection {synced-id :id} {:name "Remote-Synced" :location "/" :is_remote_synced true}
+ :model/Card {source-card-id :id} {:name "Remote-synced source card"
+ :collection_id synced-id
+ :dataset_query (mt/native-query {:query "SELECT 1"})}
+ :model/Card {mover-id :id} {:name "Card depending on synced source"
+ :collection_id nil
+ :dataset_query (mt/mbql-query nil {:source-table (str "card__" source-card-id)})}]
+ (is (= {:status "ok"}
+ (mt/user-http-request :crowberto :post 200 "card/collections"
+ {:collection_id synced-id
+ :card_ids [mover-id]})))
+ (is (= synced-id (t2/select-one-fn :collection_id :model/Card mover-id))))))
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/dashboard_api_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/dashboard_api_test.clj
index abbe2c9a6e1..0cdd4b57ab1 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/dashboard_api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/dashboard_api_test.clj
@@ -178,3 +178,24 @@
"Copied dashboard should be in target collection")
(is (not= dash-id (:id response))
"Copied dashboard should have different ID")))))
+
+(deftest api-update-dashboard-add-non-remote-synced-dashcard-rejected-test
+ (testing "PUT /api/dashboard/:id rejects adding a dashcard that points to a non-remote-synced card (GHY-3791)"
+ (mt/with-temp [:model/Collection {non-remote-synced-id :id} {:name "Non-Remote-Synced" :location "/" :type nil}
+ :model/Collection {remote-synced-id :id} {:name "Remote-Synced" :location "/" :is_remote_synced true}
+ :model/Dashboard {dash-id :id} {:name "Remote-Synced Dashboard"
+ :collection_id remote-synced-id}
+ :model/Card {non-remote-synced-card-id :id} {:name "Non-Remote-Synced Card"
+ :collection_id non-remote-synced-id
+ :dataset_query (mt/native-query {:query "SELECT 1"})}]
+ (let [response (mt/user-http-request :crowberto :put 400 (str "dashboard/" dash-id)
+ {:dashcards [{:id -1
+ :card_id non-remote-synced-card-id
+ :row 0
+ :col 0
+ :size_x 4
+ :size_y 4}]
+ :tabs []})]
+ (is (= "Uses content that is not remote synced." (:message response))))
+ (testing "dashcard was not added - transaction rolled back"
+ (is (zero? (t2/count :model/DashboardCard :dashboard_id dash-id)))))))
diff --git a/src/metabase/queries_rest/api/card.clj b/src/metabase/queries_rest/api/card.clj
index 127a4a1cea9..e06159a9d5a 100644
--- a/src/metabase/queries_rest/api/card.clj
+++ b/src/metabase/queries_rest/api/card.clj
@@ -861,7 +861,7 @@
{:id [:in (set cards-without-position)]}
{:collection_id new-collection-id-or-nil}))
(doseq [card cards]
- (collection/check-non-remote-synced-dependencies card)))))
+ (collection/check-for-remote-sync-update card)))))
(when new-collection-id-or-nil
(events/publish-event! :event/collection-touch {:collection-id new-collection-id-or-nil :user-id api/*current-user-id*})))