From 6e81c48abce9e54c23a84cb5c0592ea81be2c924 Mon Sep 17 00:00:00 2001
From: Alexander Polyankin <alexander.polyankin@metabase.com>
Date: Thu, 21 Mar 2024 17:46:32 +0200
Subject: [PATCH] Cleanup can-save (#40448)

---
 .../components/view/ViewHeader/ViewHeader.jsx          |  2 +-
 src/metabase/lib/query.cljc                            |  3 ++-
 test/metabase/lib/query_test.cljc                      | 10 ++++++++--
 3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/frontend/src/metabase/query_builder/components/view/ViewHeader/ViewHeader.jsx b/frontend/src/metabase/query_builder/components/view/ViewHeader/ViewHeader.jsx
index b4826d1b31c..45aa4fe57cf 100644
--- a/frontend/src/metabase/query_builder/components/view/ViewHeader/ViewHeader.jsx
+++ b/frontend/src/metabase/query_builder/components/view/ViewHeader/ViewHeader.jsx
@@ -461,7 +461,7 @@ function ViewTitleHeaderRightSide(props) {
   );
 
   const canSave = Lib.canSave(question.query());
-  const isSaveDisabled = !canSave || !isEditable;
+  const isSaveDisabled = !canSave;
   const disabledSaveTooltip = getDisabledSaveTooltip(
     isEditable,
     requiredTemplateTags,
diff --git a/src/metabase/lib/query.cljc b/src/metabase/lib/query.cljc
index 202f6f2b1c5..e846d81347c 100644
--- a/src/metabase/lib/query.cljc
+++ b/src/metabase/lib/query.cljc
@@ -76,7 +76,8 @@
 (mu/defn can-save :- :boolean
   "Returns whether the query can be saved."
   [query :- ::lib.schema/query]
-  (and (can-run query)
+  (and (lib.metadata/editable? query)
+       (can-run query)
        (boolean (can-save-method query))))
 
 (mu/defn query-with-stages :- ::lib.schema/query
diff --git a/test/metabase/lib/query_test.cljc b/test/metabase/lib/query_test.cljc
index 5ee4949e27f..62156fece1f 100644
--- a/test/metabase/lib/query_test.cljc
+++ b/test/metabase/lib/query_test.cljc
@@ -188,5 +188,11 @@
            (lib.query/query meta/metadata-provider
              {:database 74001, :type :query, :query {:source-table 74040}})))))
 
-(deftest ^:parallel can-save-mbql-test
-  (is (lib.query/can-save lib.tu/venues-query)))
+(deftest ^:parallel can-save-test
+  (mu/disable-enforcement
+    (are [can-save? query]
+      (= can-save?  (lib.query/can-save query))
+      true lib.tu/venues-query
+      false (assoc lib.tu/venues-query :database nil)           ; database unknown - no permissions
+      true (lib/native-query meta/metadata-provider "SELECT")
+      false (lib/native-query meta/metadata-provider ""))))
-- 
GitLab