Skip to content

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

UXW-5198: Improved data analysts grace period (#81969)

John Swansond60c8f2e3bcf
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn
index 92f7f9a29b5..e56de7889ed 100644
--- a/.clj-kondo/config.edn
+++ b/.clj-kondo/config.edn
@@ -495,6 +495,7 @@
metabase-enterprise.workspaces.test-util/with-workspaces! clojure.core/let
metabase.actions.test-util/with-actions clojure.core/let
metabase.transforms.test-util/with-transform-cleanup! clojure.core/let
+ metabase.transforms.test-util/with-transforms-api-users! clojure.core/fn
metabase.api.common/let-404 clojure.core/let
metabase.app-db.connection/with-unshared-connection clojure.core/fn
metabase.app-db.core/with-unshared-connection clojure.core/fn
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 701e93639cc..58ff04a2f08 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -267,6 +267,7 @@
events
lib.schema
models
+ premium-features
request
settings
util
diff --git a/docs/people-and-groups/managing.md b/docs/people-and-groups/managing.md
index b6f291135db..f167f1854d2 100644
--- a/docs/people-and-groups/managing.md
+++ b/docs/people-and-groups/managing.md
@@ -202,6 +202,8 @@ Other than the fixed access listed above, you can assign data, collection, and a
By default, a person in a Data Analysts group will have only read-only access to [Transforms](../data-modeling/transforms/transforms-overview.md) that use tables which the person otherwise can view. To give a Data Analyst the ability to _create_ and _run_ transforms, you'll need to give them [transform permissions](../permissions/data.md#transform-permissions) on appropriate tables.
+If you change to a plan that doesn't include the Data Analyst group feature, your Data Analyst group will still exist, along with the permissions you've granted to it. But Metabase pauses the group's access to Data Studio. You can remove people from the group, but you can't add anyone new, and the group disappears once you've removed the last person. If you return to a plan that includes the Data Analyst group feature, Metabase will restore your Data Analyst group, the permissions you've given it, and its access to Data Studio.
+
## Creating a group
Go to **Admin** > **People** > **Groups**, and click the **Add a group** button.
diff --git a/e2e/test/scenarios/admin/people.cy.spec.js b/e2e/test/scenarios/admin/people.cy.spec.js
index 17799a1d535..d8c258f1603 100644
--- a/e2e/test/scenarios/admin/people.cy.spec.js
+++ b/e2e/test/scenarios/admin/people.cy.spec.js
@@ -865,12 +865,14 @@ describe("scenarios > admin > people > group managers", () => {
});
// Demote myself from being manager
- H.popover().findByLabelText("collection").click();
+ H.popover().findByRole("button", { name: "collection" }).click();
confirmLosingAbilityToManageGroup();
- H.popover().findByLabelText("collection").should("not.exist");
+ H.popover()
+ .findByRole("button", { name: "collection" })
+ .should("not.exist");
// Remove myself from another group
- H.popover().findByLabelText("data").click();
+ H.popover().findByRole("button", { name: "data" }).click();
confirmLosingAbilityToManageGroup();
// Redirected to the home page
diff --git a/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj b/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj
index 047f5ce329d..74e3db5e3a3 100644
--- a/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj
+++ b/enterprise/backend/src/metabase_enterprise/advanced_permissions/common.clj
@@ -75,7 +75,7 @@
:can_access_monitoring (perms/set-has-application-permission-of-type? permissions-set :monitoring)
:can_access_data_model can-access-data-model
:can_access_db_details (perms/user-has-any-perms-of-type? user-id :perms/manage-database)
- :can_access_transforms (or api/*is-superuser?* (and api/*is-data-analyst?*
+ :can_access_transforms (or api/*is-superuser?* (and (api/entitled-data-analyst?)
(perms/user-has-any-perms-of-type? api/*current-user-id* :perms/transforms
:exclude-db-ids [audit/audit-db-id])))
:is_data_analyst api/*is-data-analyst?*
diff --git a/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj b/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj
index a37008a02e0..8d7fcd0f77a 100644
--- a/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_studio/api/table.clj
@@ -146,7 +146,7 @@
_query-params
_body
_request]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(let [table (api/read-check :model/Table id)]
(when (:is_published table)
(publishing-info id))))
@@ -169,7 +169,7 @@
[_route-params
_query-params
body :- ::publish-table-selectors]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(let [target-collection (api/check-404 (data-studio.db/collection (:collection_id body)))
_ (api/check-400 (= (:type target-collection) collection/library-data-collection-type)
(tru "Tables can only be published to Library/Data collections."))
@@ -193,7 +193,7 @@
[_route-params
_query-params
body :- ::table-selectors]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(let [selectors (body->table-selectors body)
downstream-ids (all-downstream-table-ids selectors)
;; Get table IDs before update for event publishing
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/api.clj b/enterprise/backend/src/metabase_enterprise/dependencies/api.clj
index 6ff73ed6fa4..b7d53c23371 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/api.clj
@@ -222,9 +222,14 @@
:measure [:id :name :description :created_at :creator_id :table_id]})
(defn- current-user-visibility
- "The current user, as the `:visible` filter-spec opts consumed by `metabase-enterprise.dependencies.db`."
+ "The current user, as the `:visible` filter-spec opts consumed by `metabase-enterprise.dependencies.db`.
+
+ Analyst-wide visibility comes with the role rather than a permissions graph, so it pauses while the
+ `advanced-permissions` feature is unavailable (see [[api/entitled-data-analyst?]])."
[{:keys [include-archived-items]}]
- (cond-> {:user-id api/*current-user-id* :is-superuser? api/*is-superuser?* :is-data-analyst? api/*is-data-analyst?*}
+ (cond-> {:user-id api/*current-user-id*
+ :is-superuser? api/*is-superuser?*
+ :is-data-analyst? (api/entitled-data-analyst?)}
include-archived-items (assoc :include-archived-items include-archived-items)))
(defn- readable-graph-dependencies
diff --git a/enterprise/backend/src/metabase_enterprise/library/api.clj b/enterprise/backend/src/metabase_enterprise/library/api.clj
index c80c464dd05..a0869621aeb 100644
--- a/enterprise/backend/src/metabase_enterprise/library/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/library/api.clj
@@ -22,7 +22,7 @@
[_route
_query
_body]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(api/check-400 (not (collections/library-collection)) "Library already exists")
(collections/create-library-collection!))
diff --git a/enterprise/backend/src/metabase_enterprise/scim/db.clj b/enterprise/backend/src/metabase_enterprise/scim/db.clj
index cda85effe7f..d777e3b383b 100644
--- a/enterprise/backend/src/metabase_enterprise/scim/db.clj
+++ b/enterprise/backend/src/metabase_enterprise/scim/db.clj
@@ -152,6 +152,11 @@
[group-id :- ms/PositiveInt]
(t2/delete! :model/PermissionsGroup group-id))
+(mu/defn group-member-user-ids
+ "The user IDs of the members of the PermissionsGroup with `group-id`."
+ [group-id :- ms/PositiveInt]
+ (t2/select-fn-set :user_id :model/PermissionsGroupMembership :group_id group-id))
+
(mu/defn group-members
"Rows of group ID, member email, and member entity ID for the memberships of the PermissionsGroups with
`group-ids`."
diff --git a/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj b/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj
index fba7ff0b575..ec99dc91fad 100644
--- a/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj
@@ -4,6 +4,7 @@
`v2` in the API path represents the fact that we implement SCIM 2.0."
(:require
+ [clojure.set :as set]
[metabase-enterprise.scim.db :as scim.db]
[metabase-enterprise.scim.settings :as scim.settings]
[metabase.analytics-interface.core :as analytics]
@@ -15,6 +16,7 @@
[metabase.users.schema :as users.schema]
[metabase.util :as u]
[metabase.util.i18n :as i18n]
+ [metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
@@ -450,15 +452,22 @@
mb-group->scim)))
(defn- update-group-membership
- "Updates the membership of `group-id` to be the set of users in the collection `user-entity-ids`. Clears
- any existing members."
+ "Updates the membership of `group-id` to be the set of users in the collection `user-entity-ids`.
+
+ Member values that match no User are skipped. When none of them match, the push is a no-op rather than an
+ emptying of the group: the IdP is referring to users this instance never provisioned, and wiping the group
+ would hide that drift behind a successful response."
[group-id user-entity-ids]
- (let [user-ids (scim.db/user-ids-by-entity-ids user-entity-ids)]
- (when-let [memberships (not-empty (map
- (fn [user-id] {:group group-id :user user-id})
- user-ids))]
- (perms/remove-all-users-from-group! group-id)
- (perms/add-users-to-groups! memberships))))
+ (let [desired-ids (set (scim.db/user-ids-by-entity-ids user-entity-ids))
+ current-ids (set (scim.db/group-member-user-ids group-id))]
+ (if (empty? desired-ids)
+ (log/warnf "SCIM group %d membership push named %d member(s), none of them known users; leaving membership alone"
+ group-id (count user-entity-ids))
+ (do
+ (doseq [user-id (set/difference current-ids desired-ids)]
+ (perms/remove-user-from-group! user-id group-id))
+ (perms/add-users-to-groups! (for [user-id (set/difference desired-ids current-ids)]
+ {:group group-id :user user-id}))))))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
diff --git a/enterprise/backend/src/metabase_enterprise/transform_testing/api.clj b/enterprise/backend/src/metabase_enterprise/transform_testing/api.clj
index 59e89f1a727..93b806a621a 100644
--- a/enterprise/backend/src/metabase_enterprise/transform_testing/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/transform_testing/api.clj
@@ -34,7 +34,7 @@
[_route-params
{:keys [transform-id]} :- [:map {:closed true}
[:transform-id {:optional true} [:maybe ms/PositiveInt]]]]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(filter mi/can-read? (transform-testing.db/transform-tests {:transform-id transform-id})))
(api.macros/defendpoint :get "/:id" :- ::transform-testing.schema/transform-test
diff --git a/enterprise/backend/test/metabase_enterprise/advanced_permissions/common_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_permissions/common_test.clj
index 6713c6d5985..7749f3da654 100644
--- a/enterprise/backend/test/metabase_enterprise/advanced_permissions/common_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/advanced_permissions/common_test.clj
@@ -76,6 +76,25 @@
(is (partial= {:can_access_transforms true}
(user-permissions :crowberto))))))))
+(deftest can-access-transforms-follows-the-advanced-permissions-feature-test
+ (testing "can_access_transforms pauses for a data analyst once advanced-permissions is gone"
+ (perms.test-util/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-restored-data-perms!
+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/view-data :unrestricted)
+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/transforms :yes)
+ (mt/with-current-user (mt/user->id :rasta)
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (true? (get-in (advanced-permissions.common/with-advanced-permissions {})
+ [:permissions :can_access_transforms]))))
+ (mt/with-premium-features #{}
+ (is (false? (get-in (advanced-permissions.common/with-advanced-permissions {})
+ [:permissions :can_access_transforms])))))))
+ (testing "a superuser is unaffected"
+ (mt/with-current-user (mt/user->id :crowberto)
+ (mt/with-premium-features #{}
+ (is (true? (get-in (advanced-permissions.common/with-advanced-permissions {})
+ [:permissions :can_access_transforms]))))))))
+
(deftest current-user-query-permissions-published-table-test
(testing "GET /api/user/current can_create_queries respects published tables"
(mt/with-premium-features #{:library}
diff --git a/enterprise/backend/test/metabase_enterprise/data_studio/api/permissions_groups_test.clj b/enterprise/backend/test/metabase_enterprise/data_studio/api/permissions_groups_test.clj
index e3fad0f93f2..58c0de7b827 100644
--- a/enterprise/backend/test/metabase_enterprise/data_studio/api/permissions_groups_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_studio/api/permissions_groups_test.clj
@@ -1,14 +1,18 @@
(ns metabase-enterprise.data-studio.api.permissions-groups-test
(:require
- [clojure.string :as str]
[clojure.test :refer [deftest testing is]]
[metabase.permissions.core :as perms]
[metabase.permissions.models.permissions-group :as perms-group]
- [metabase.permissions.models.permissions-group-membership :as perms-group-membership]
- [metabase.premium-features.token-check :as token-check]
[metabase.test :as mt]
+ [metabase.warehouse-schema-overlay.core :as warehouse-schema-overlay]
[toucan2.core :as t2]))
+(defn- user-table-fn
+ "The value of `column` on the Table with `table-id` as users see it. A bare `t2/select :model/Table` shows sync's."
+ [column table-id]
+ (t2/select-one-fn column :model/Table :id table-id
+ {:from [(warehouse-schema-overlay/table-query)]}))
+
(deftest fetch-groups-test
(testing "GET /api/permissions/group - Data Analysts group is visible with the feature"
(mt/with-premium-features #{:advanced-permissions}
@@ -19,113 +23,81 @@
(is (not (contains? (set (map :id (mt/user-http-request :crowberto :get 200 "permissions/group")))
(:id (perms-group/data-analyst))))))))
-;;; ---------------------------------------- sync-data-analyst-group-for-oss! tests ----------------------------------------
+(defn- add-data-analyst!
+ "Put the user with `user-id` in the Data Analysts group."
+ [user-id]
+ ;; additions are gated on the feature, so grant it for the setup
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/add-user-to-group! user-id (:id (perms-group/data-analyst)))))
-(defmacro with-reset-data-analyst-group! [& body]
- `(let [original-group# (t2/select-one :model/PermissionsGroup :magic_group_type perms-group/data-analyst-magic-group-type)]
- (try
- (mt/with-model-cleanup [:model/PermissionsGroup]
- (do ~@body))
- (finally
- (with-bindings {(var perms-group/*allow-modifying-magic-groups*) true}
- (t2/update! :model/PermissionsGroup :id (:id original-group#)
- {:magic_group_type perms-group/data-analyst-magic-group-type
- :name (:name original-group#)}))))))
+(def ^:private no-permissions-message "You don't have permissions to do that.")
-(deftest sync-data-analyst-group-for-oss!-ee-noop-test
- (testing "When we canonically have the feature, sync-data-analyst-group-for-oss! does nothing"
- (mt/with-dynamic-fn-redefs [token-check/canonically-has-feature? (constantly true)]
- (let [data-analyst-group-id (:id (perms-group/data-analyst))]
- (mt/with-temp [:model/User {user-id :id} {}]
- (perms/add-user-to-group! user-id data-analyst-group-id)
- (let [member-count-before (t2/count :model/PermissionsGroupMembership :group_id data-analyst-group-id)]
- (perms-group/sync-data-analyst-group-for-oss!)
- ;; Group should still have same members
- (is (= member-count-before
- (t2/count :model/PermissionsGroupMembership :group_id data-analyst-group-id)))
- ;; Group should still be the magic group
- (is (= perms-group/data-analyst-magic-group-type
- (t2/select-one-fn :magic_group_type :model/PermissionsGroup :id data-analyst-group-id)))))))))
+(deftest grandfathered-member-loses-data-studio-access-without-the-feature-test
+ (testing "a Data Analysts member added under the feature is refused Data Studio once the feature is gone"
+ (mt/with-temp [:model/User {analyst-id :id} {}
+ :model/Database {db-id :id} {}
+ :model/Table {table-id :id} {:db_id db-id}]
+ (add-data-analyst! analyst-id)
+ (mt/with-premium-features #{}
+ (is (= no-permissions-message
+ (mt/user-http-request analyst-id :post 403 "data-studio/table/edit"
+ {:table_ids [table-id]
+ :data_layer "final"})))
+ (is (= no-permissions-message
+ (mt/user-http-request analyst-id :post 403 "data-studio/table/selection"
+ {:table_ids [table-id]})))
+ (is (not= :final (user-table-fn :data_layer table-id)))))))
-(deftest sync-data-analyst-group-for-oss!-converts-with-members-test
- (testing "When canonically lacking the feature, sync-data-analyst-group-for-oss! converts the Data Analysts group when it has members"
- (mt/with-dynamic-fn-redefs [token-check/canonically-has-feature? (constantly false)]
- (with-reset-data-analyst-group!
- (let [original-group-id (t2/select-one-pk :model/PermissionsGroup :magic_group_type perms-group/data-analyst-magic-group-type)]
- (mt/with-temp [:model/User {user-id :id} {}]
- (perms/add-user-to-group! user-id original-group-id)
- (perms-group/sync-data-analyst-group-for-oss!)
- (testing "Original group should be renamed and demoted"
- (let [original-group (t2/select-one :model/PermissionsGroup :id original-group-id)]
- (is (str/starts-with? (:name original-group) "Data Analysts (converted)"))
- (is (nil? (:magic_group_type original-group)))))
- (testing "User should still be in the converted group"
- (is (t2/exists? :model/PermissionsGroupMembership
- :user_id user-id
- :group_id original-group-id)))
- (testing "New magic group should exist and be empty"
- (let [new-magic-group (t2/select-one :model/PermissionsGroup
- :magic_group_type perms-group/data-analyst-magic-group-type)]
- (is (some? new-magic-group))
- (is (= "Data Analysts" (:name new-magic-group)))
- (is (zero? (t2/count :model/PermissionsGroupMembership :group_id (:id new-magic-group))))))))))))
+(deftest superuser-keeps-data-studio-access-without-the-feature-test
+ (testing "a superuser reaches Data Studio on a feature-less instance"
+ (mt/with-temp [:model/Database {db-id :id} {}
+ :model/Table {table-id :id} {:db_id db-id}]
+ (mt/with-premium-features #{}
+ (is (= {} (mt/user-http-request :crowberto :post 200 "data-studio/table/edit"
+ {:table_ids [table-id]
+ :data_layer "final"})))
+ (is (= :final (user-table-fn :data_layer table-id)))
+ (is (map? (mt/user-http-request :crowberto :post 200 "data-studio/table/selection"
+ {:table_ids [table-id]})))))))
-(deftest sync-data-analyst-group-for-oss!-preserves-name-test
- (testing "When the magic group has a non-default name (e.g. from a migration conflict), the new magic group keeps that name"
- (mt/with-dynamic-fn-redefs [token-check/canonically-has-feature? (constantly false)]
- (with-reset-data-analyst-group!
- (let [original-group-id (t2/select-one-pk :model/PermissionsGroup :magic_group_type perms-group/data-analyst-magic-group-type)]
- ;; Simulate the migration conflict scenario: rename the magic group to the fallback name
- (with-bindings {(var perms-group/*allow-modifying-magic-groups*) true}
- (t2/update! :model/PermissionsGroup original-group-id {:name "Metabase Data Analysts"}))
- (mt/with-temp [:model/User {user-id :id} {}]
- (perms/add-user-to-group! user-id original-group-id)
- (perms-group/sync-data-analyst-group-for-oss!)
- (testing "Converted group should use the magic group's name as its base"
- (let [converted-group (t2/select-one :model/PermissionsGroup :id original-group-id)]
- (is (= "Metabase Data Analysts (converted)" (:name converted-group)))
- (is (nil? (:magic_group_type converted-group)))))
- (testing "New magic group should reuse the old magic group's name"
- (let [new-magic-group (t2/select-one :model/PermissionsGroup
- :magic_group_type perms-group/data-analyst-magic-group-type)]
- (is (= "Metabase Data Analysts" (:name new-magic-group)))))))))))
+(deftest member-keeps-data-studio-access-with-the-feature-test
+ (testing "a Data Analysts member reaches Data Studio while the feature is present"
+ (mt/with-temp [:model/User {analyst-id :id} {}
+ :model/Database {db-id :id} {}
+ :model/Table {table-id :id} {:db_id db-id}]
+ (add-data-analyst! analyst-id)
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (= {} (mt/user-http-request analyst-id :post 200 "data-studio/table/edit"
+ {:table_ids [table-id]
+ :data_layer "final"})))
+ (is (= :final (user-table-fn :data_layer table-id)))
+ (is (map? (mt/user-http-request analyst-id :post 200 "data-studio/table/selection"
+ {:table_ids [table-id]})))))))
-(deftest sync-data-analyst-group-for-oss!-idempotent-empty-test
- (testing "Sync is idempotent when magic group is empty (no members to convert)"
- (mt/with-dynamic-fn-redefs [token-check/canonically-has-feature? (constantly false)]
- (with-reset-data-analyst-group!
- (let [group-count-before (t2/count :model/PermissionsGroup)
- data-analyst-group-id (t2/select-one-pk :model/PermissionsGroup :magic_group_type perms-group/data-analyst-magic-group-type)]
- ;; Ensure no members in the group
- (perms-group-membership/with-allow-direct-deletion
- (t2/delete! :model/PermissionsGroupMembership :group_id data-analyst-group-id))
- (perms-group/sync-data-analyst-group-for-oss!)
- (testing "No new groups should be created"
- (is (= group-count-before (t2/count :model/PermissionsGroup))))
- (testing "Magic group should still exist unchanged"
- (is (= perms-group/data-analyst-magic-group-type
- (t2/select-one-fn :magic_group_type :model/PermissionsGroup :id data-analyst-group-id)))))))))
+(deftest non-member-has-no-data-studio-access-without-the-feature-test
+ (testing "a non-member gets no Data Studio access on a feature-less instance"
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/Database {db-id :id} {}
+ :model/Table {table-id :id} {:db_id db-id}]
+ (mt/with-premium-features #{}
+ (is (= no-permissions-message
+ (mt/user-http-request user-id :post 403 "data-studio/table/edit"
+ {:table_ids [table-id]
+ :data_layer "final"})))
+ (is (= no-permissions-message
+ (mt/user-http-request user-id :post 403 "data-studio/table/selection"
+ {:table_ids [table-id]})))))))
-(deftest sync-data-analyst-group-for-oss!-indeterminate-retries-test
- (testing "When token check is indeterminate, sync retries in background until canonical response"
- (let [call-count (atom 0)
- check-results (atom [nil nil false])] ;; indeterminate, indeterminate, then definitively no feature
- (with-redefs [token-check/canonically-has-feature? (fn [_feature]
- (let [idx (swap! call-count inc)]
- (nth @check-results (min (dec idx) (dec (count @check-results))))))
- perms-group/seconds-to-sleep-per-attempt 0]
- (with-reset-data-analyst-group!
- (let [original-group-id (t2/select-one-pk :model/PermissionsGroup :magic_group_type perms-group/data-analyst-magic-group-type)]
- (mt/with-temp [:model/User {user-id :id} {}]
- (perms/add-user-to-group! user-id original-group-id)
- ;; sync-data-analyst-group-for-oss! will spawn a future since first call returns nil
- (let [result (perms-group/sync-data-analyst-group-for-oss!)]
- ;; Wait for the future to complete
- (when (future? result)
- (deref result 500 :timeout))
- (testing "Should have retried until canonical response"
- (is (>= @call-count 3)))
- (testing "Original group should be converted after retries"
- (let [original-group (t2/select-one :model/PermissionsGroup :id original-group-id)]
- (is (str/starts-with? (:name original-group) "Data Analysts (converted)"))
- (is (nil? (:magic_group_type original-group)))))))))))))
+(deftest removing-a-member-on-a-feature-less-instance-is-allowed-test
+ (testing "a member can be removed without the feature, and stays without Data Studio access afterwards"
+ (mt/with-temp [:model/User {analyst-id :id} {}
+ :model/Database {db-id :id} {}
+ :model/Table {table-id :id} {:db_id db-id}]
+ (add-data-analyst! analyst-id)
+ (mt/with-premium-features #{}
+ (perms/remove-user-from-group! analyst-id (:id (perms-group/data-analyst)))
+ (is (not (t2/exists? :model/PermissionsGroupMembership
+ :user_id analyst-id :group_id (:id (perms-group/data-analyst)))))
+ (is (= no-permissions-message
+ (mt/user-http-request analyst-id :post 403 "data-studio/table/selection"
+ {:table_ids [table-id]})))))))
diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
index 03382108044..a2bc5df2708 100644
--- a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
@@ -2776,7 +2776,7 @@
response)))))))))
(deftest data-analyst-can-access-dependency-graph-test
- (mt/with-premium-features #{:data-studio :dependencies :transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :data-studio :dependencies :transforms-basic :hosting}
(testing "Data analysts can access dependency diagnostics endpoints"
(let [data-analyst-group-id (:id (perms-group/data-analyst))]
(mt/with-temp [:model/User {analyst-id :id} {:first_name "Data"
@@ -2785,16 +2785,23 @@
:is_data_analyst true}
:model/PermissionsGroupMembership _ {:user_id analyst-id
:group_id data-analyst-group-id}
- :model/Database {db-id :id} {}
- :model/Table {_table-id :id} {:db_id db-id}
- :model/Transform {transform-id :id} {:source_database_id db-id
- :name "Test Transform"}]
- (testing "graph/unreferenced"
- (is (map? (mt/user-http-request analyst-id :get 200
- "ee/dependencies/graph/unreferenced"))))
- (testing "graph/breaking"
- (is (map? (mt/user-http-request analyst-id :get 200
- "ee/dependencies/graph/breaking"))))
- (testing "graph with transform"
- (is (map? (mt/user-http-request analyst-id :get 200
- (str "ee/dependencies/graph?type=transform&id=" transform-id))))))))))
+ ;; the sample database: All Users may query it, so analyst-wide transform visibility applies
+ :model/Transform {transform-id :id} {:source_database_id (mt/id)
+ :name "Analyst Grace Transform - gracetest"}]
+ (letfn [(unreferenced-transform-ids []
+ (->> (mt/user-http-request analyst-id :get 200
+ "ee/dependencies/graph/unreferenced?types=transform&query=gracetest")
+ :data
+ (map :id)
+ set))]
+ (testing "graph/unreferenced"
+ (is (contains? (unreferenced-transform-ids) transform-id)))
+ (testing "graph/breaking"
+ (is (map? (mt/user-http-request analyst-id :get 200
+ "ee/dependencies/graph/breaking"))))
+ (testing "graph with transform"
+ (is (map? (mt/user-http-request analyst-id :get 200
+ (str "ee/dependencies/graph?type=transform&id=" transform-id)))))
+ (testing "analyst-wide visibility pauses while advanced-permissions is unavailable"
+ (mt/with-premium-features #{:data-studio :dependencies :transforms-basic :hosting}
+ (is (not (contains? (unreferenced-transform-ids) transform-id)))))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/scim/v2/api_test.clj b/enterprise/backend/test/metabase_enterprise/scim/v2/api_test.clj
index 5ba8f3ebfae..8e3b5abffb5 100644
--- a/enterprise/backend/test/metabase_enterprise/scim/v2/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/scim/v2/api_test.clj
@@ -3,6 +3,7 @@
[clojure.test :refer :all]
[metabase-enterprise.scim.api :as scim]
[metabase-enterprise.scim.v2.api :as scim-api]
+ [metabase.permissions.core :as perms]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
@@ -445,6 +446,17 @@
(is (= new-group-name (:name group)))
(is (= 1 (count (:members group))))
(is (= (mt/user->id :crowberto) (-> group :members first :user_id)))))))
+ (testing "A members list naming only unknown users leaves the membership alone rather than emptying the group"
+ (mt/with-temp [:model/PermissionsGroup group {:name (format "Test SCIM group %s" (random-uuid))}
+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id (:id group)}]
+ (let [entity-id (t2/select-one-fn :entity_id :model/PermissionsGroup :id (:id group))
+ group-update {:schemas ["urn:ietf:params:scim:schemas:core:2.0:Group"]
+ :id entity-id
+ :displayName (:name group)
+ :members [{:value (str (random-uuid))}]}]
+ (scim-client :put 200 (format "ee/scim/v2/Groups/%s" entity-id) group-update)
+ (is (= #{(mt/user->id :rasta)}
+ (t2/select-fn-set :user_id :model/PermissionsGroupMembership :group_id (:id group)))))))
(testing "404 is returned when trying to update a data-app group, and its membership is left alone"
(mt/with-temp [:model/PermissionsGroup app-group {:name "App Group" :is_data_app_group true}
:model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id (:id app-group)}]
@@ -479,3 +491,101 @@
(let [entity-id (t2/select-one-fn :entity_id :model/PermissionsGroup :id (:id app-group))]
(scim-client :delete 404 (format "ee/scim/v2/Groups/%s" entity-id))
(is (t2/exists? :model/PermissionsGroup :id (:id app-group))))))))
+
+;;; ------------------------- Data Analysts group: additions gated, removals never -------------------------
+
+(defn- data-analyst-scim-group-update
+ "A SCIM Group PUT body setting the Data Analysts group's members to `user-ids`."
+ [user-ids]
+ (let [group (perms-group/data-analyst)]
+ {:schemas ["urn:ietf:params:scim:schemas:core:2.0:Group"]
+ :id (t2/select-one-fn :entity_id :model/PermissionsGroup :id (:id group))
+ :displayName (:name group)
+ :members (for [user-id user-ids]
+ {:value (t2/select-one-fn :entity_id :model/User :id user-id)})}))
+
+(defn- data-analyst-group-member-ids []
+ (set (t2/select-fn-set :user_id :model/PermissionsGroupMembership
+ :group_id (:id (perms-group/data-analyst)))))
+
+(defn- put-data-analyst-group! [expected-status user-ids]
+ (let [entity-id (t2/select-one-fn :entity_id :model/PermissionsGroup :id (:id (perms-group/data-analyst)))]
+ (scim-client :put expected-status (format "ee/scim/v2/Groups/%s" entity-id)
+ (data-analyst-scim-group-update user-ids))))
+
+(deftest scim-data-analyst-group-add-requires-advanced-permissions-test
+ (testing "PUT /Groups/:id cannot add a new member to the Data Analysts group without :advanced-permissions"
+ (mt/with-temp [:model/User {existing-id :id} {:email (format "scim-analyst-%[email protected]" (random-uuid))}
+ :model/User {new-id :id} {:email (format "scim-analyst-%[email protected]" (random-uuid))}]
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/add-user-to-group! existing-id (:id (perms-group/data-analyst))))
+ (mt/with-premium-features #{}
+ (with-scim-setup!
+ (let [before (data-analyst-group-member-ids)
+ response (put-data-analyst-group! 402 [existing-id new-id])]
+ (testing "the error names the required feature, so provisioning drift is visible at the IdP"
+ (is (= (str perms/fail-to-add-data-analyst-msg) response)))
+ (testing "and the push rolls back, leaving existing memberships untouched"
+ (is (= before (data-analyst-group-member-ids)))
+ (is (contains? (data-analyst-group-member-ids) existing-id))
+ (is (not (contains? (data-analyst-group-member-ids) new-id))))))))))
+
+(deftest scim-data-analyst-group-removal-only-put-is-not-gated-test
+ (testing "PUT /Groups/:id can remove a member of the Data Analysts group without :advanced-permissions"
+ (mt/with-temp [:model/User {keep-id :id} {:email (format "scim-analyst-%[email protected]" (random-uuid))}
+ :model/User {drop-id :id} {:email (format "scim-analyst-%[email protected]" (random-uuid))}]
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/add-user-to-group! keep-id (:id (perms-group/data-analyst)))
+ (perms/add-user-to-group! drop-id (:id (perms-group/data-analyst))))
+ (mt/with-premium-features #{}
+ (with-scim-setup!
+ (put-data-analyst-group! 200 [keep-id])
+ (let [members (data-analyst-group-member-ids)]
+ (is (contains? members keep-id))
+ (is (not (contains? members drop-id))))
+ (testing "and the removed member loses the is_data_analyst flag"
+ (is (not (t2/select-one-fn :is_data_analyst :model/User :id drop-id)))))))))
+
+(deftest scim-data-analyst-group-idempotent-resync-is-not-gated-test
+ (testing "PUT /Groups/:id with an unchanged member list succeeds without :advanced-permissions"
+ (mt/with-temp [:model/User {user-id :id} {:email (format "scim-analyst-%[email protected]" (random-uuid))}]
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/add-user-to-group! user-id (:id (perms-group/data-analyst))))
+ (mt/with-premium-features #{}
+ (with-scim-setup!
+ (let [before (data-analyst-group-member-ids)]
+ (put-data-analyst-group! 200 [user-id])
+ (is (= before (data-analyst-group-member-ids)))
+ (is (contains? (data-analyst-group-member-ids) user-id))
+ (is (true? (boolean (t2/select-one-fn :is_data_analyst :model/User :id user-id))))))))))
+
+(deftest scim-data-analyst-group-add-with-advanced-permissions-test
+ (testing "PUT /Groups/:id can add members to the Data Analysts group with :advanced-permissions"
+ (mt/with-temp [:model/User {user-id :id} {:email (format "scim-analyst-%[email protected]" (random-uuid))}]
+ (mt/with-premium-features #{:advanced-permissions}
+ (with-scim-setup!
+ (put-data-analyst-group! 200 [user-id])
+ (is (contains? (data-analyst-group-member-ids) user-id))
+ (is (true? (boolean (t2/select-one-fn :is_data_analyst :model/User :id user-id)))))))))
+
+(deftest scim-group-put-is-diff-based-test
+ (testing "PUT /Groups/:id leaves untouched members' rows alone rather than deleting and recreating them"
+ (with-scim-setup!
+ (mt/with-temp [:model/PermissionsGroup group {:name (format "Test SCIM group %s" (random-uuid))}
+ :model/User {keep-id :id} {}
+ :model/User {drop-id :id} {}]
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/add-user-to-group! keep-id (:id group))
+ (perms/add-user-to-group! drop-id (:id group)))
+ (let [membership-id #(t2/select-one-pk :model/PermissionsGroupMembership
+ :group_id (:id group) :user_id keep-id)
+ original-id (membership-id)
+ entity-id (t2/select-one-fn :entity_id :model/PermissionsGroup :id (:id group))]
+ (scim-client :put 200 (format "ee/scim/v2/Groups/%s" entity-id)
+ {:schemas ["urn:ietf:params:scim:schemas:core:2.0:Group"]
+ :id entity-id
+ :displayName (:name group)
+ :members [{:value (t2/select-one-fn :entity_id :model/User :id keep-id)}]})
+ (is (= original-id (membership-id)))
+ (is (not (t2/exists? :model/PermissionsGroupMembership
+ :group_id (:id group) :user_id drop-id))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/transform_testing/api_test.clj b/enterprise/backend/test/metabase_enterprise/transform_testing/api_test.clj
index 07071c11aed..b9931cfe81d 100644
--- a/enterprise/backend/test/metabase_enterprise/transform_testing/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/transform_testing/api_test.clj
@@ -23,7 +23,8 @@
:target {:type "table" :schema "PUBLIC" :name "PEOPLE_SUMMARY" :database (mt/id)}})
(defmacro ^:private with-transforms-enabled [& body]
- `(mt/with-premium-features #{:transforms-basic :transforms-testing}
+ ;; advanced-permissions: the Data Analyst role only opens transform tests while the plan includes it
+ `(mt/with-premium-features #{:advanced-permissions :transforms-basic :transforms-testing}
(mt/with-temporary-raw-setting-values [~'transforms-enabled "true"]
~@body)))
diff --git a/enterprise/backend/test/metabase_enterprise/transforms_inspector/api_test.clj b/enterprise/backend/test/metabase_enterprise/transforms_inspector/api_test.clj
index 5246f368c60..d2460f66d3e 100644
--- a/enterprise/backend/test/metabase_enterprise/transforms_inspector/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/transforms_inspector/api_test.clj
@@ -12,7 +12,7 @@
(set! *warn-on-reflection* true)
(deftest inspect-lens-not-found-test
- (mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :transforms-python :hosting}
(testing "GET /api/ee/transforms/:id/inspect/:lens-id returns 404 for a nonexistent lens"
(mt/with-temp [:model/Transform {transform-id :id} {}]
(mt/with-data-analyst-role! (mt/user->id :lucky)
@@ -24,7 +24,7 @@
;;; -------------------------------------------------- Inspector Query API --------------------------------------------------
(deftest inspect-query-execute-test
- (mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :transforms-python :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(testing "POST /api/ee/transforms/:id/inspect/:lens-id/query executes a query with inspector context"
(mt/with-temp [:model/Transform {transform-id :id} {}]
@@ -43,7 +43,7 @@
(is (= "transform-inspector" (:context result))))))))))))
(deftest inspect-query-with-lens-params-test
- (mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :transforms-python :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(testing "POST /api/ee/transforms/:id/inspect/:lens-id/query passes lens_params through to query execution"
(mt/with-temp [:model/Transform {transform-id :id} {}]
@@ -59,7 +59,7 @@
(is (= "transform-inspector" (:context result)))))))))))
(deftest inspect-query-permissions-test
- (mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :transforms-python :hosting}
(testing "POST /api/ee/transforms/:id/inspect/:lens-id/query requires transforms permission"
(mt/with-temp [:model/Transform {transform-id :id} {}]
(let [mp (mt/metadata-provider)]
@@ -100,7 +100,7 @@
;; Each `testing` block uses a distinct (metric, labels) combo, so no `prometheus/clear!` is
;; needed to flush the metrics.
(deftest inspect-metrics-test
- (mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :transforms-python :hosting}
(mt/with-prometheus-system! [_ system]
(mt/with-temp [:model/Transform {transform-id :id} {}]
(testing "Permission-denied failures do NOT bump inspector counters"
diff --git a/enterprise/backend/test/metabase_enterprise/transforms_python/transforms_api_test.clj b/enterprise/backend/test/metabase_enterprise/transforms_python/transforms_api_test.clj
index 61cb2e82346..a203d751602 100644
--- a/enterprise/backend/test/metabase_enterprise/transforms_python/transforms_api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/transforms_python/transforms_api_test.clj
@@ -69,7 +69,7 @@
:name "gadget_products"
:database (mt/id)}}))))))
(testing "with transforms-python feature flag"
- (mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :transforms-python :hosting}
(with-transform-cleanup! [table-name "gadget_products"]
(let [transform (create-transform!)]
(is (= "print('hello chris')"
diff --git a/frontend/src/metabase/admin/people/components/GroupDetail.tsx b/frontend/src/metabase/admin/people/components/GroupDetail.tsx
index 7d8e0f60ad2..36e3ff16cf8 100644
--- a/frontend/src/metabase/admin/people/components/GroupDetail.tsx
+++ b/frontend/src/metabase/admin/people/components/GroupDetail.tsx
@@ -8,10 +8,12 @@ import {
useUpdateMembershipMutation,
} from "metabase/api";
import { getErrorMessage } from "metabase/api/utils";
+import { useHasTokenFeature } from "metabase/common/hooks";
import { useConfirmation } from "metabase/common/hooks/use-confirmation";
import { useToast } from "metabase/common/hooks/use-toast";
import {
canEditMembership,
+ getAddMembersDisabledReason,
getGroupNameLocalized,
isAdminGroup,
isDefaultGroup,
@@ -19,7 +21,7 @@ import {
import { PLUGIN_GROUP_MANAGERS, PLUGIN_TENANTS } from "metabase/plugins";
import { useDispatch } from "metabase/redux";
import { SettingsSection } from "metabase/settings-components";
-import { Box, Button, Text } from "metabase/ui";
+import { Box, Button, Text, Tooltip } from "metabase/ui";
import type { Group, Member, Membership, User } from "metabase-types/api";
import { Alert } from "./Alert";
@@ -38,6 +40,11 @@ export const GroupDetail = ({
}: GroupDetailProps) => {
const dispatch = useDispatch();
const [sendToast] = useToast();
+ const hasAdvancedPermissions = useHasTokenFeature("advanced_permissions");
+ const addMembersDisabledReason = getAddMembersDisabledReason(
+ group,
+ hasAdvancedPermissions,
+ );
const [createMembership] = useCreateMembershipMutation();
const [updateMembership] = useUpdateMembershipMutation();
@@ -124,11 +131,11 @@ export const GroupDetail = ({
)}
titleActions={
canEditMembership(group) && (
- <Button
- variant="filled"
+ <AddMembersButton
+ disabledReason={addMembersDisabledReason}
+ isAddingUsers={addUserVisible}
onClick={onAddUsersClicked}
- disabled={addUserVisible}
- >{t`Add members`}</Button>
+ />
)
}
>
@@ -148,6 +155,40 @@ export const GroupDetail = ({
);
};
+interface AddMembersButtonProps {
+ disabledReason: string | null;
+ isAddingUsers: boolean;
+ onClick: () => void;
+}
+
+const AddMembersButton = ({
+ disabledReason,
+ isAddingUsers,
+ onClick,
+}: AddMembersButtonProps) => {
+ const button = (
+ <Button
+ variant="filled"
+ onClick={onClick}
+ disabled={isAddingUsers || disabledReason != null}
+ >{t`Add members`}</Button>
+ );
+
+ if (disabledReason == null) {
+ return button;
+ }
+
+ return (
+ <Tooltip label={disabledReason}>
+ {/* Wrapper so the tooltip still shows over the disabled button,
+ which itself does not emit pointer events. */}
+ <Box component="span" display="inline-flex">
+ {button}
+ </Box>
+ </Tooltip>
+ );
+};
+
const GroupDescription = ({ group }: { group: Group }) => {
// Let plugin handle tenant-specific descriptions first
const tenantDescription = PLUGIN_TENANTS.GroupDescription({ group });
diff --git a/frontend/src/metabase/admin/people/components/GroupDetail.unit.spec.tsx b/frontend/src/metabase/admin/people/components/GroupDetail.unit.spec.tsx
new file mode 100644
index 00000000000..29d188bf27c
--- /dev/null
+++ b/frontend/src/metabase/admin/people/components/GroupDetail.unit.spec.tsx
@@ -0,0 +1,138 @@
+import userEvent from "@testing-library/user-event";
+import fetchMock from "fetch-mock";
+
+import { setupUsersEndpoints } from "__support__/server-mocks";
+import { mockSettings } from "__support__/settings";
+import { createMockState } from "__support__/state";
+import { getIcon, renderWithProviders, screen, waitFor } from "__support__/ui";
+import type { Group, Member, TokenFeatures } from "metabase-types/api";
+import {
+ createMockGroup,
+ createMockTokenFeatures,
+ createMockUser,
+} from "metabase-types/api/mocks";
+
+import { GroupDetail } from "./GroupDetail";
+
+const currentUser = createMockUser({ id: 1, is_superuser: true });
+
+const MEMBERSHIP_ID = 10;
+const MEMBERSHIP_PATH = `path:/api/permissions/membership/${MEMBERSHIP_ID}`;
+
+const createMockMember = (opts?: Partial<Member>): Member => ({
+ user_id: 2,
+ group_id: 5,
+ membership_id: MEMBERSHIP_ID,
+ email: "[email protected]",
+ first_name: "Ana",
+ last_name: "Lyst",
+ is_group_manager: false,
+ is_superuser: false,
+ ...opts,
+});
+
+const createMockGroupWithMembers = ({
+ members = [createMockMember()],
+ ...opts
+}: Partial<Group> = {}): Group => ({
+ ...createMockGroup(opts),
+ members,
+});
+
+const DATA_ANALYSTS = createMockGroupWithMembers({
+ id: 5,
+ name: "Data Analysts",
+ magic_group_type: "data-analyst",
+});
+
+const MARKETING = createMockGroupWithMembers({
+ id: 6,
+ name: "Marketing",
+ magic_group_type: null,
+ members: [createMockMember({ group_id: 6, membership_id: 11 })],
+});
+
+const setup = ({
+ group,
+ tokenFeatures = {},
+}: {
+ group: Group;
+ tokenFeatures?: Partial<TokenFeatures>;
+}) => {
+ const settings = mockSettings({
+ "token-features": createMockTokenFeatures(tokenFeatures),
+ });
+ setupUsersEndpoints([]);
+ fetchMock.delete(MEMBERSHIP_PATH, 204);
+
+ renderWithProviders(
+ <GroupDetail
+ group={group}
+ membershipsByUser={{}}
+ currentUser={currentUser}
+ />,
+ { storeInitialState: createMockState({ settings, currentUser }) },
+ );
+};
+
+const getAddMembersButton = () =>
+ screen.getByRole("button", { name: "Add members" });
+
+describe("GroupDetail", () => {
+ describe("without the advanced-permissions feature", () => {
+ it("disables adding members to the Data Analysts group and explains why", async () => {
+ setup({ group: DATA_ANALYSTS });
+
+ expect(getAddMembersButton()).toBeDisabled();
+
+ await userEvent.hover(getAddMembersButton());
+ expect(await screen.findByRole("tooltip")).toHaveTextContent(
+ "Adding members to this group requires a plan that includes the Data Analysts group. Members can only be removed.",
+ );
+ });
+
+ it("keeps member removal working on the Data Analysts group", async () => {
+ setup({ group: DATA_ANALYSTS });
+
+ expect(screen.getByText("[email protected]")).toBeInTheDocument();
+ await userEvent.click(getIcon("close"));
+
+ await waitFor(() =>
+ expect(
+ fetchMock.callHistory.calls(MEMBERSHIP_PATH, { method: "DELETE" }),
+ ).toHaveLength(1),
+ );
+ });
+
+ it("leaves other groups' add-members button enabled", () => {
+ setup({ group: MARKETING });
+
+ expect(getAddMembersButton()).toBeEnabled();
+ });
+ });
+
+ describe("with the advanced-permissions feature", () => {
+ it("leaves adding members to the Data Analysts group enabled", async () => {
+ setup({
+ group: DATA_ANALYSTS,
+ tokenFeatures: { advanced_permissions: true },
+ });
+
+ expect(getAddMembersButton()).toBeEnabled();
+
+ await userEvent.click(getAddMembersButton());
+ expect(getAddMembersButton()).toBeDisabled();
+ });
+
+ it("puts no tooltip on the add-members button", async () => {
+ setup({
+ group: DATA_ANALYSTS,
+ tokenFeatures: { advanced_permissions: true },
+ });
+
+ await userEvent.hover(getAddMembersButton());
+
+ expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
+ });
+ });
+});
diff --git a/frontend/src/metabase/admin/people/components/MembershipSelect/MembershipSelect.module.css b/frontend/src/metabase/admin/people/components/MembershipSelect/MembershipSelect.module.css
index fc7ec32a929..c2a13a8b3b7 100644
--- a/frontend/src/metabase/admin/people/components/MembershipSelect/MembershipSelect.module.css
+++ b/frontend/src/metabase/admin/people/components/MembershipSelect/MembershipSelect.module.css
@@ -1,8 +1,7 @@
.membershipSelectItem {
display: flex;
align-items: center;
- justify-content: space-between;
- padding: 0.5rem 1.5rem;
+ padding-right: 1rem;
background-color: var(--mb-color-background_page-primary);
color: var(--mb-color-text-secondary);
font-weight: bold;
@@ -16,3 +15,22 @@
}
}
}
+
+/* The row-wide membership toggle; the group-manager toggle sits beside it. */
+.membershipToggle {
+ flex: 1;
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 1.5rem;
+ padding: 0.5rem 0.5rem 0.5rem 1.5rem;
+
+ &[aria-disabled="true"] {
+ cursor: not-allowed;
+ }
+
+ &:focus-visible {
+ outline: 2px solid var(--mb-color-focus);
+ outline-offset: -2px;
+ }
+}
diff --git a/frontend/src/metabase/admin/people/components/MembershipSelect/MembershipSelect.tsx b/frontend/src/metabase/admin/people/components/MembershipSelect/MembershipSelect.tsx
index fea54b1d8a3..85eb0d14b49 100644
--- a/frontend/src/metabase/admin/people/components/MembershipSelect/MembershipSelect.tsx
+++ b/frontend/src/metabase/admin/people/components/MembershipSelect/MembershipSelect.tsx
@@ -2,14 +2,24 @@ import { useDisclosure } from "@mantine/hooks";
import { useMemo } from "react";
import { t } from "ttag";
+import { useHasTokenFeature } from "metabase/common/hooks";
import {
+ getAddMembersDisabledReason,
getGroupNameLocalized,
isAdminGroup,
isDataAnalystGroup,
isDefaultGroup,
} from "metabase/common/utils/groups";
import { PLUGIN_GROUP_MANAGERS, PLUGIN_TENANTS } from "metabase/plugins";
-import { Box, Divider, Flex, Icon, Popover } from "metabase/ui";
+import {
+ Box,
+ Divider,
+ Flex,
+ Icon,
+ Popover,
+ Tooltip,
+ UnstyledButton,
+} from "metabase/ui";
import { isNotNull } from "metabase/utils/types";
import type { GroupInfo, Member } from "metabase-types/api";
@@ -64,6 +74,7 @@ export const MembershipSelect = ({
}: MembershipSelectProps) => {
const [popoverOpened, { open: openPopover, toggle: togglePopover }] =
useDisclosure();
+ const hasAdvancedPermissions = useHasTokenFeature("advanced_permissions");
const selectedGroupIds = Array.from(memberships.keys());
const { pinnedGroups, regularGroups } = useMemo(
() => getGroupSections(groups),
@@ -86,11 +97,15 @@ export const MembershipSelect = ({
};
const renderGroup = (group: GroupInfo) => {
+ const isMember = memberships.has(group.id);
+ const addDisabledReason = isMember
+ ? null
+ : getAddMembersDisabledReason(group, hasAdvancedPermissions);
const isDisabled =
(isAdminGroup(group) && isCurrentUser) ||
isDefaultGroup(group) ||
- PLUGIN_TENANTS.isExternalUsersGroup(group);
- const isMember = memberships.has(group.id);
+ PLUGIN_TENANTS.isExternalUsersGroup(group) ||
+ addDisabledReason != null;
const canEditMembershipType =
isMember &&
!isUserAdmin &&
@@ -98,37 +113,43 @@ export const MembershipSelect = ({
!PLUGIN_TENANTS.isTenantGroup(group) &&
!isAdminGroup(group);
+ // The group-manager toggle is a button of its own, so it sits beside the
+ // membership button rather than inside it. aria-disabled instead of
+ // disabled keeps the row focusable and hoverable, so the tooltip can
+ // explain why it can't be toggled.
return (
- <li
- className={S.membershipSelectItem}
- key={group.id}
- aria-label={group.name}
- onClick={() =>
- isDisabled ? undefined : handleToggleMembership(group.id)
- }
- style={{ cursor: isDisabled ? "not-allowed" : "pointer" }}
- >
- <span>{getGroupNameLocalized(group)}</span>
- <Flex pl="lg" align="center" justify="end">
- {canEditMembershipType && (
- <PLUGIN_GROUP_MANAGERS.UserTypeToggle
- tooltipPlacement="bottom"
- isManager={memberships.get(group.id)?.is_group_manager}
- onChange={(is_group_manager: boolean) =>
- handleChangeMembership(group.id, {
- is_group_manager,
- })
+ <li key={group.id} className={S.membershipSelectItem}>
+ <Tooltip label={addDisabledReason} disabled={addDisabledReason == null}>
+ <UnstyledButton
+ type="button"
+ className={S.membershipToggle}
+ aria-pressed={isMember}
+ aria-disabled={isDisabled || undefined}
+ onClick={() => {
+ if (!isDisabled) {
+ handleToggleMembership(group.id);
}
- />
- )}
- <span
- style={{
- visibility: isMember ? "visible" : "hidden",
}}
>
- <Icon name="check" />
- </span>
- </Flex>
+ <span>{getGroupNameLocalized(group)}</span>
+ <Icon
+ name="check"
+ aria-hidden
+ style={{ visibility: isMember ? "visible" : "hidden" }}
+ />
+ </UnstyledButton>
+ </Tooltip>
+ {canEditMembershipType && (
+ <PLUGIN_GROUP_MANAGERS.UserTypeToggle
+ tooltipPlacement="bottom"
+ isManager={memberships.get(group.id)?.is_group_manager}
+ onChange={(is_group_manager: boolean) =>
+ handleChangeMembership(group.id, {
+ is_group_manager,
+ })
+ }
+ />
+ )}
</li>
);
};
diff --git a/frontend/src/metabase/admin/people/components/MembershipSelect/MembershipSelect.unit.spec.tsx b/frontend/src/metabase/admin/people/components/MembershipSelect/MembershipSelect.unit.spec.tsx
new file mode 100644
index 00000000000..3470a5b8891
--- /dev/null
+++ b/frontend/src/metabase/admin/people/components/MembershipSelect/MembershipSelect.unit.spec.tsx
@@ -0,0 +1,165 @@
+import userEvent from "@testing-library/user-event";
+
+import { mockSettings } from "__support__/settings";
+import { createMockState } from "__support__/state";
+import { renderWithProviders, screen, within } from "__support__/ui";
+import type { GroupInfo, Member, TokenFeatures } from "metabase-types/api";
+import {
+ createMockGroup,
+ createMockTokenFeatures,
+} from "metabase-types/api/mocks";
+
+import { MembershipSelect } from "./MembershipSelect";
+
+const ALL_USERS = createMockGroup({
+ id: 1,
+ name: "All Users",
+ magic_group_type: "all-internal-users",
+});
+const DATA_ANALYSTS = createMockGroup({
+ id: 2,
+ name: "Data Analysts",
+ magic_group_type: "data-analyst",
+});
+const MARKETING = createMockGroup({
+ id: 3,
+ name: "Marketing",
+ magic_group_type: null,
+});
+const GROUPS: GroupInfo[] = [ALL_USERS, DATA_ANALYSTS, MARKETING];
+
+const ADD_DISABLED_MESSAGE =
+ "Adding members to this group requires a plan that includes the Data Analysts group. Members can only be removed.";
+
+const setup = ({
+ memberGroupIds = [ALL_USERS.id],
+ tokenFeatures = {},
+}: {
+ memberGroupIds?: number[];
+ tokenFeatures?: Partial<TokenFeatures>;
+} = {}) => {
+ const settings = mockSettings({
+ "token-features": createMockTokenFeatures(tokenFeatures),
+ });
+ const memberships = new Map<number, Partial<Member>>(
+ memberGroupIds.map((id) => [id, { group_id: id }]),
+ );
+
+ const onAdd = jest.fn();
+ const onRemove = jest.fn();
+
+ renderWithProviders(
+ <MembershipSelect
+ groups={GROUPS}
+ memberships={memberships}
+ isUserAdmin={false}
+ onAdd={onAdd}
+ onRemove={onRemove}
+ onChange={jest.fn()}
+ />,
+ { storeInitialState: createMockState({ settings }) },
+ );
+
+ return { onAdd, onRemove };
+};
+
+const openPicker = () =>
+ userEvent.click(screen.getByLabelText("group-summary"));
+
+const getGroupItem = (name: string) => screen.getByRole("button", { name });
+
+describe("MembershipSelect", () => {
+ describe("without the advanced-permissions feature", () => {
+ it("does not add the user to the Data Analysts group and explains why", async () => {
+ const { onAdd } = setup();
+ await openPicker();
+
+ await userEvent.click(getGroupItem("Data Analysts"));
+ expect(onAdd).not.toHaveBeenCalled();
+
+ await userEvent.hover(getGroupItem("Data Analysts"));
+ expect(await screen.findByRole("tooltip")).toHaveTextContent(
+ ADD_DISABLED_MESSAGE,
+ );
+ });
+
+ it("exposes each row as a toggle button with its membership and gating state", async () => {
+ setup({ memberGroupIds: [ALL_USERS.id, MARKETING.id] });
+ await openPicker();
+
+ expect(getGroupItem("Marketing")).toHaveAttribute("aria-pressed", "true");
+ expect(getGroupItem("Marketing")).not.toHaveAttribute("aria-disabled");
+ expect(getGroupItem("Data Analysts")).toHaveAttribute(
+ "aria-pressed",
+ "false",
+ );
+ expect(getGroupItem("Data Analysts")).toHaveAttribute(
+ "aria-disabled",
+ "true",
+ );
+ });
+
+ it("still removes an existing Data Analysts member", async () => {
+ const { onRemove } = setup({
+ memberGroupIds: [ALL_USERS.id, DATA_ANALYSTS.id],
+ });
+ await openPicker();
+
+ await userEvent.click(getGroupItem("Data Analysts"));
+
+ expect(onRemove).toHaveBeenCalledWith(DATA_ANALYSTS.id);
+ });
+
+ it("leaves other groups addable", async () => {
+ const { onAdd } = setup();
+ await openPicker();
+
+ await userEvent.click(getGroupItem("Marketing"));
+
+ expect(onAdd).toHaveBeenCalledWith(MARKETING.id, {
+ is_group_manager: false,
+ });
+ });
+
+ it("keeps the Data Analysts group pinned above the custom groups", async () => {
+ setup();
+ await openPicker();
+
+ const lists = screen.getAllByRole("list");
+ expect(
+ within(lists[0]).getByRole("button", { name: "Data Analysts" }),
+ ).toBeInTheDocument();
+ expect(
+ within(lists[1]).getByRole("button", { name: "Marketing" }),
+ ).toBeInTheDocument();
+ });
+ });
+
+ describe("with the advanced-permissions feature", () => {
+ it("adds the user to the Data Analysts group", async () => {
+ const { onAdd } = setup({
+ tokenFeatures: { advanced_permissions: true },
+ });
+ await openPicker();
+
+ await userEvent.click(getGroupItem("Data Analysts"));
+
+ expect(onAdd).toHaveBeenCalledWith(DATA_ANALYSTS.id, {
+ is_group_manager: false,
+ });
+ });
+
+ it("keeps the Data Analysts group pinned above the custom groups", async () => {
+ setup({ tokenFeatures: { advanced_permissions: true } });
+ await openPicker();
+
+ const lists = screen.getAllByRole("list");
+ expect(
+ within(lists[0]).getByRole("button", { name: "Data Analysts" }),
+ ).toBeInTheDocument();
+ expect(
+ within(lists[1]).getByRole("button", { name: "Marketing" }),
+ ).toBeInTheDocument();
+ });
+ });
+});
diff --git a/frontend/src/metabase/collections/components/CollectionEmptyState/CollectionEmptyState.unit.spec.tsx b/frontend/src/metabase/collections/components/CollectionEmptyState/CollectionEmptyState.unit.spec.tsx
index cdf56ac0a97..049786d8577 100644
--- a/frontend/src/metabase/collections/components/CollectionEmptyState/CollectionEmptyState.unit.spec.tsx
+++ b/frontend/src/metabase/collections/components/CollectionEmptyState/CollectionEmptyState.unit.spec.tsx
@@ -47,7 +47,10 @@ async function setup({
const state = createMockState({
settings: mockSettings({
- "token-features": createMockTokenFeatures({ library: true }),
+ "token-features": createMockTokenFeatures({
+ library: true,
+ advanced_permissions: isAnalyst,
+ }),
}),
currentUser: createMockUser({
is_superuser: isAdmin,
diff --git a/frontend/src/metabase/common/components/GroupsMultiSelect/GroupsMultiSelect.tsx b/frontend/src/metabase/common/components/GroupsMultiSelect/GroupsMultiSelect.tsx
index d7273d77d77..2fde3918a80 100644
--- a/frontend/src/metabase/common/components/GroupsMultiSelect/GroupsMultiSelect.tsx
+++ b/frontend/src/metabase/common/components/GroupsMultiSelect/GroupsMultiSelect.tsx
@@ -1,8 +1,10 @@
import { type KeyboardEvent, useState } from "react";
import { t } from "ttag";
+import { useHasTokenFeature } from "metabase/common/hooks";
import {
canEditMembership,
+ getAddMembersDisabledReason,
getGroupNameLocalized,
getGroupSortOrder,
isAdminGroup,
@@ -18,6 +20,7 @@ import {
Flex,
Pill,
PillsInput,
+ Tooltip,
useCombobox,
} from "metabase/ui";
import { isNotNull } from "metabase/utils/types";
@@ -57,6 +60,13 @@ export const GroupsMultiSelect = ({
}: GroupsMultiSelectProps) => {
const combobox = useCombobox();
const [search, setSearch] = useState("");
+ const hasAdvancedPermissions = useHasTokenFeature("advanced_permissions");
+
+ // Only additions are gated; an already-selected group stays deselectable.
+ const getAddDisabledReason = (group: GroupInfo) =>
+ value.includes(group.id)
+ ? null
+ : getAddMembersDisabledReason(group, hasAdvancedPermissions);
const groupsById = new Map(groups.map((group) => [group.id, group]));
@@ -116,7 +126,7 @@ export const GroupsMultiSelect = ({
const handleOptionSubmit = (groupIdString: string) => {
const group = groupsById.get(Number(groupIdString));
- if (group && !isGroupLocked(group)) {
+ if (group && !isGroupLocked(group) && getAddDisabledReason(group) == null) {
toggleGroup(group.id);
setSearch("");
}
@@ -145,6 +155,7 @@ export const GroupsMultiSelect = ({
group={group}
isSelected={value.includes(group.id)}
isLocked={isGroupLocked(group)}
+ addDisabledReason={getAddDisabledReason(group)}
showManagerToggle={canEditManager(group)}
isManager={isManager(group.id)}
onToggleManager={() => onToggleManager?.(group.id)}
@@ -290,6 +301,7 @@ interface GroupOptionProps {
group: GroupInfo;
isSelected: boolean;
isLocked: boolean;
+ addDisabledReason: string | null;
showManagerToggle: boolean;
isManager: boolean;
onToggleManager: () => void;
@@ -299,35 +311,42 @@ const GroupOption = ({
group,
isSelected,
isLocked,
+ addDisabledReason,
showManagerToggle,
isManager,
onToggleManager,
}: GroupOptionProps) => (
- <Combobox.Option
- value={String(group.id)}
- aria-label={getGroupNameLocalized(group)}
- active={isSelected}
- disabled={isLocked}
+ <Tooltip
+ label={addDisabledReason}
+ disabled={addDisabledReason == null}
+ position="right"
>
- <Flex align="center" justify="space-between" gap="sm">
- <Flex align="center" gap="sm">
- <Checkbox
- className={S.checkbox}
- checked={isSelected}
- readOnly
- aria-hidden
- tabIndex={-1}
- />
- <span>{getGroupNameLocalized(group)}</span>
- </Flex>
- {showManagerToggle && (
- <Box onMouseDown={(event) => event.preventDefault()}>
- <PLUGIN_GROUP_MANAGERS.UserTypeToggle
- isManager={isManager}
- onChange={onToggleManager}
+ <Combobox.Option
+ value={String(group.id)}
+ aria-label={getGroupNameLocalized(group)}
+ active={isSelected}
+ disabled={isLocked || addDisabledReason != null}
+ >
+ <Flex align="center" justify="space-between" gap="sm">
+ <Flex align="center" gap="sm">
+ <Checkbox
+ className={S.checkbox}
+ checked={isSelected}
+ readOnly
+ aria-hidden
+ tabIndex={-1}
/>
- </Box>
- )}
- </Flex>
- </Combobox.Option>
+ <span>{getGroupNameLocalized(group)}</span>
+ </Flex>
+ {showManagerToggle && (
+ <Box onMouseDown={(event) => event.preventDefault()}>
+ <PLUGIN_GROUP_MANAGERS.UserTypeToggle
+ isManager={isManager}
+ onChange={onToggleManager}
+ />
+ </Box>
+ )}
+ </Flex>
+ </Combobox.Option>
+ </Tooltip>
);
diff --git a/frontend/src/metabase/common/components/GroupsMultiSelect/GroupsMultiSelect.unit.spec.tsx b/frontend/src/metabase/common/components/GroupsMultiSelect/GroupsMultiSelect.unit.spec.tsx
index c65f8d3068e..aa29d215ddb 100644
--- a/frontend/src/metabase/common/components/GroupsMultiSelect/GroupsMultiSelect.unit.spec.tsx
+++ b/frontend/src/metabase/common/components/GroupsMultiSelect/GroupsMultiSelect.unit.spec.tsx
@@ -3,8 +3,9 @@ import { useState } from "react";
import { setupEnterprisePlugins } from "__support__/enterprise";
import { mockSettings } from "__support__/settings";
+import { createMockState } from "__support__/state";
import { renderWithProviders, screen, within } from "__support__/ui";
-import type { GroupId, GroupInfo } from "metabase-types/api";
+import type { GroupId, GroupInfo, TokenFeatures } from "metabase-types/api";
import {
createMockGroup,
createMockTokenFeatures,
@@ -32,6 +33,11 @@ const SALES = createMockGroup({
name: "Sales",
magic_group_type: null,
});
+const DATA_ANALYSTS = createMockGroup({
+ id: 5,
+ name: "Data Analysts",
+ magic_group_type: "data-analyst",
+});
const GROUPS = [ALL_USERS, ADMINISTRATORS, MARKETING, SALES];
const ALL_TENANT_USERS = createMockGroup({
@@ -51,10 +57,12 @@ function setup(
managerGroupIds = [],
groups = GROUPS,
sections,
+ tokenFeatures = {},
}: {
managerGroupIds?: GroupId[];
groups?: GroupInfo[];
sections?: GroupSection[];
+ tokenFeatures?: Partial<TokenFeatures>;
} = {},
) {
const onChange = jest.fn<void, [GroupId[]]>();
@@ -85,7 +93,13 @@ function setup(
);
}
- renderWithProviders(<Wrapper />);
+ renderWithProviders(<Wrapper />, {
+ storeInitialState: createMockState({
+ settings: mockSettings({
+ "token-features": createMockTokenFeatures(tokenFeatures),
+ }),
+ }),
+ });
return { onChange, onToggleManager };
}
@@ -203,6 +217,113 @@ describe("GroupsMultiSelect", () => {
).not.toBeInTheDocument();
});
+ describe("the Data Analysts group", () => {
+ const ADD_DISABLED_MESSAGE =
+ "Adding members to this group requires a plan that includes the Data Analysts group. Members can only be removed.";
+ const GROUPS_WITH_ANALYSTS = [...GROUPS, DATA_ANALYSTS];
+
+ const openDropdown = () =>
+ userEvent.click(screen.getByRole("combobox", { name: "Groups" }));
+
+ describe("without the advanced-permissions feature", () => {
+ it("cannot be selected, and explains why", async () => {
+ const { onChange } = setup([ALL_USERS.id], {
+ groups: GROUPS_WITH_ANALYSTS,
+ });
+
+ await openDropdown();
+ const option = await screen.findByRole("option", {
+ name: "Data Analysts",
+ });
+ await userEvent.click(option);
+ expect(onChange).not.toHaveBeenCalled();
+
+ await userEvent.hover(option);
+ expect(await screen.findByRole("tooltip")).toHaveTextContent(
+ ADD_DISABLED_MESSAGE,
+ );
+ });
+
+ it("can still be deselected when the person is already a member", async () => {
+ const { onChange } = setup([ALL_USERS.id, DATA_ANALYSTS.id], {
+ groups: GROUPS_WITH_ANALYSTS,
+ });
+
+ await openDropdown();
+ await userEvent.click(
+ await screen.findByRole("option", { name: "Data Analysts" }),
+ );
+
+ expect(onChange).toHaveBeenLastCalledWith([ALL_USERS.id]);
+ });
+
+ it("can still be removed via its pill", async () => {
+ const { onChange } = setup([ALL_USERS.id, DATA_ANALYSTS.id], {
+ groups: GROUPS_WITH_ANALYSTS,
+ });
+
+ await userEvent.click(
+ screen.getByRole("button", { name: "Remove Data Analysts" }),
+ );
+
+ expect(onChange).toHaveBeenLastCalledWith([ALL_USERS.id]);
+ });
+
+ it("keeps its pinned position above the custom groups", async () => {
+ setup([ALL_USERS.id], { groups: GROUPS_WITH_ANALYSTS });
+
+ await openDropdown();
+ await screen.findByRole("option", { name: "Marketing" });
+
+ expectSingleDividerBetween("Data Analysts", "Marketing");
+ });
+
+ it("leaves the other groups selectable", async () => {
+ const { onChange } = setup([ALL_USERS.id], {
+ groups: GROUPS_WITH_ANALYSTS,
+ });
+
+ await openDropdown();
+ await userEvent.click(
+ await screen.findByRole("option", { name: "Marketing" }),
+ );
+
+ expect(onChange).toHaveBeenLastCalledWith([ALL_USERS.id, MARKETING.id]);
+ });
+ });
+
+ describe("with the advanced-permissions feature", () => {
+ it("can be selected", async () => {
+ const { onChange } = setup([ALL_USERS.id], {
+ groups: GROUPS_WITH_ANALYSTS,
+ tokenFeatures: { advanced_permissions: true },
+ });
+
+ await openDropdown();
+ await userEvent.click(
+ await screen.findByRole("option", { name: "Data Analysts" }),
+ );
+
+ expect(onChange).toHaveBeenLastCalledWith([
+ ALL_USERS.id,
+ DATA_ANALYSTS.id,
+ ]);
+ });
+
+ it("keeps its pinned position above the custom groups", async () => {
+ setup([ALL_USERS.id], {
+ groups: GROUPS_WITH_ANALYSTS,
+ tokenFeatures: { advanced_permissions: true },
+ });
+
+ await openDropdown();
+ await screen.findByRole("option", { name: "Marketing" });
+
+ expectSingleDividerBetween("Data Analysts", "Marketing");
+ });
+ });
+ });
+
describe("with sections", () => {
const SECTIONS = [
{
@@ -315,6 +436,7 @@ describe("GroupsMultiSelect", () => {
describe("with the group-managers feature (EE)", () => {
beforeEach(() => {
+ // The plugin reads the token features as it initializes, before any render.
mockSettings({
"token-features": createMockTokenFeatures({
advanced_permissions: true,
@@ -324,7 +446,9 @@ describe("GroupsMultiSelect", () => {
});
it("calls onToggleManager when the dropdown toggle is clicked", async () => {
- const { onToggleManager } = setup([ALL_USERS.id, MARKETING.id]);
+ const { onToggleManager } = setup([ALL_USERS.id, MARKETING.id], {
+ tokenFeatures: { advanced_permissions: true },
+ });
await userEvent.click(screen.getByRole("combobox", { name: "Groups" }));
@@ -339,6 +463,7 @@ describe("GroupsMultiSelect", () => {
it("un-tags a manager's pill when demoted via the dropdown toggle", async () => {
const { onToggleManager } = setup([ALL_USERS.id, MARKETING.id], {
managerGroupIds: [MARKETING.id],
+ tokenFeatures: { advanced_permissions: true },
});
// The pill starts tagged as a manager.
@@ -370,6 +495,7 @@ describe("GroupsMultiSelect", () => {
// Input order puts the custom group first to prove the pinned default floats up.
setup([ALL_TENANT_USERS.id], {
groups: [CONTRACTORS, ALL_TENANT_USERS],
+ tokenFeatures: { tenants: true },
});
await userEvent.click(screen.getByRole("combobox", { name: "Groups" }));
diff --git a/frontend/src/metabase/common/data-studio/selectors.ts b/frontend/src/metabase/common/data-studio/selectors.ts
index e185e1f5df0..a41dae6d91d 100644
--- a/frontend/src/metabase/common/data-studio/selectors.ts
+++ b/frontend/src/metabase/common/data-studio/selectors.ts
@@ -1,14 +1,18 @@
-import { getUserIsAdmin, getUserIsAnalyst } from "metabase/current-user";
+import {
+ getUserIsAdmin,
+ getUserIsEntitledAnalyst,
+} from "metabase/current-user";
import { PLUGIN_REMOTE_SYNC } from "metabase/plugins";
import type { State } from "metabase/redux/store";
import { isWithinIframe } from "metabase/utils/iframe";
// Must be in sync with CanAccessDataStudio in frontend/src/metabase/data-studio/route-guards.tsx
+// Must be in sync with api/check-data-studio-access in src/metabase/api/common.clj
export function canAccessDataStudio(state: State) {
if (isWithinIframe()) {
return false;
}
- return getUserIsAdmin(state) || getUserIsAnalyst(state);
+ return getUserIsAdmin(state) || getUserIsEntitledAnalyst(state);
}
export const getUserCanWriteSegments = (
diff --git a/frontend/src/metabase/common/data-studio/selectors.unit.spec.ts b/frontend/src/metabase/common/data-studio/selectors.unit.spec.ts
index 8a9cfc62ac5..ab545d98c78 100644
--- a/frontend/src/metabase/common/data-studio/selectors.unit.spec.ts
+++ b/frontend/src/metabase/common/data-studio/selectors.unit.spec.ts
@@ -1,5 +1,10 @@
+import { mockSettings } from "__support__/settings";
import { createMockState } from "__support__/state";
-import { createMockUser } from "metabase-types/api/mocks";
+import type { TokenFeatures, User } from "metabase-types/api";
+import {
+ createMockTokenFeatures,
+ createMockUser,
+} from "metabase-types/api/mocks";
import { canAccessDataStudio } from "./selectors";
@@ -9,6 +14,20 @@ jest.mock("metabase/utils/iframe", () => ({
const { isWithinIframe } = jest.requireMock("metabase/utils/iframe");
+const setup = ({
+ user,
+ tokenFeatures = {},
+}: {
+ user: Partial<User>;
+ tokenFeatures?: Partial<TokenFeatures>;
+}) =>
+ createMockState({
+ currentUser: createMockUser(user),
+ settings: mockSettings({
+ "token-features": createMockTokenFeatures(tokenFeatures),
+ }),
+ });
+
describe("canAccessDataStudio", () => {
beforeEach(() => {
jest.clearAllMocks();
@@ -17,41 +36,54 @@ describe("canAccessDataStudio", () => {
it("returns false when in embedding iframe", () => {
isWithinIframe.mockReturnValue(true);
- const state = createMockState({
- currentUser: createMockUser({ is_superuser: true }),
+ const state = setup({
+ user: { is_superuser: true },
+ tokenFeatures: { advanced_permissions: true },
});
expect(canAccessDataStudio(state)).toBe(false);
});
- it("returns true when user is admin", () => {
- const state = createMockState({
- currentUser: createMockUser({
- is_superuser: true,
- is_data_analyst: false,
- }),
+ it("returns true when user is admin and the feature is present", () => {
+ const state = setup({
+ user: { is_superuser: true, is_data_analyst: false },
+ tokenFeatures: { advanced_permissions: true },
+ });
+
+ expect(canAccessDataStudio(state)).toBe(true);
+ });
+
+ it("returns true when user is admin and the feature is absent", () => {
+ const state = setup({
+ user: { is_superuser: true, is_data_analyst: false },
+ tokenFeatures: { advanced_permissions: false },
});
expect(canAccessDataStudio(state)).toBe(true);
});
- it("returns true when user is analyst", () => {
- const state = createMockState({
- currentUser: createMockUser({
- is_superuser: false,
- is_data_analyst: true,
- }),
+ it("returns true when user is analyst and the feature is present", () => {
+ const state = setup({
+ user: { is_superuser: false, is_data_analyst: true },
+ tokenFeatures: { advanced_permissions: true },
});
expect(canAccessDataStudio(state)).toBe(true);
});
+ it("returns false when user is analyst and the feature is absent", () => {
+ const state = setup({
+ user: { is_superuser: false, is_data_analyst: true },
+ tokenFeatures: { advanced_permissions: false },
+ });
+
+ expect(canAccessDataStudio(state)).toBe(false);
+ });
+
it("returns false when user is neither admin nor analyst", () => {
- const state = createMockState({
- currentUser: createMockUser({
- is_superuser: false,
- is_data_analyst: false,
- }),
+ const state = setup({
+ user: { is_superuser: false, is_data_analyst: false },
+ tokenFeatures: { advanced_permissions: true },
});
expect(canAccessDataStudio(state)).toBe(false);
diff --git a/frontend/src/metabase/common/monitor/selectors.ts b/frontend/src/metabase/common/monitor/selectors.ts
index 0d1915a36c4..176362dd820 100644
--- a/frontend/src/metabase/common/monitor/selectors.ts
+++ b/frontend/src/metabase/common/monitor/selectors.ts
@@ -1,16 +1,18 @@
import {
getUser,
getUserIsAdmin,
- getUserIsAnalyst,
+ getUserIsEntitledAnalyst,
} from "metabase/current-user";
import type { State } from "metabase/redux/store";
import { isWithinIframe } from "metabase/utils/iframe";
+// Like Data Studio, diagnostics come with the Data Analyst role rather than a
+// permissions graph, so they pause while the plan lacks the feature.
export function canAccessMonitorDiagnostics(state: State) {
if (isWithinIframe()) {
return false;
}
- return getUserIsAdmin(state) || getUserIsAnalyst(state);
+ return getUserIsAdmin(state) || getUserIsEntitledAnalyst(state);
}
export function canAccessMonitoringTools(state: State) {
diff --git a/frontend/src/metabase/common/monitor/selectors.unit.spec.ts b/frontend/src/metabase/common/monitor/selectors.unit.spec.ts
index 3466cd8271c..d3e3268b71a 100644
--- a/frontend/src/metabase/common/monitor/selectors.unit.spec.ts
+++ b/frontend/src/metabase/common/monitor/selectors.unit.spec.ts
@@ -1,5 +1,9 @@
+import { mockSettings } from "__support__/settings";
import { createMockState } from "__support__/state";
-import { createMockUser } from "metabase-types/api/mocks";
+import {
+ createMockTokenFeatures,
+ createMockUser,
+} from "metabase-types/api/mocks";
import {
canAccessAiAuditing,
@@ -15,6 +19,21 @@ jest.mock("metabase/utils/iframe", () => ({
const { isWithinIframe } = jest.requireMock("metabase/utils/iframe");
+const createAnalystState = ({
+ hasAdvancedPermissions = true,
+}: { hasAdvancedPermissions?: boolean } = {}) =>
+ createMockState({
+ currentUser: createMockUser({
+ is_superuser: false,
+ is_data_analyst: true,
+ }),
+ settings: mockSettings({
+ "token-features": createMockTokenFeatures({
+ advanced_permissions: hasAdvancedPermissions,
+ }),
+ }),
+ });
+
describe("canAccessMonitor", () => {
beforeEach(() => {
jest.clearAllMocks();
@@ -42,14 +61,13 @@ describe("canAccessMonitor", () => {
});
it("returns true when user is analyst", () => {
- const state = createMockState({
- currentUser: createMockUser({
- is_superuser: false,
- is_data_analyst: true,
- }),
- });
+ expect(canAccessMonitor(createAnalystState())).toBe(true);
+ });
- expect(canAccessMonitor(state)).toBe(true);
+ it("returns false for an analyst whose plan lost the feature", () => {
+ const state = createAnalystState({ hasAdvancedPermissions: false });
+
+ expect(canAccessMonitor(state)).toBe(false);
});
it("returns true for a monitoring-only user (tools access)", () => {
@@ -101,14 +119,13 @@ describe("canAccessMonitorDiagnostics", () => {
});
it("returns true when user is analyst", () => {
- const state = createMockState({
- currentUser: createMockUser({
- is_superuser: false,
- is_data_analyst: true,
- }),
- });
+ expect(canAccessMonitorDiagnostics(createAnalystState())).toBe(true);
+ });
- expect(canAccessMonitorDiagnostics(state)).toBe(true);
+ it("returns false for an analyst whose plan lost the feature", () => {
+ const state = createAnalystState({ hasAdvancedPermissions: false });
+
+ expect(canAccessMonitorDiagnostics(state)).toBe(false);
});
it("returns false for a monitoring-only user (no diagnostics access)", () => {
diff --git a/frontend/src/metabase/common/utils/groups.ts b/frontend/src/metabase/common/utils/groups.ts
index c18ef0f8cd9..000392baf02 100644
--- a/frontend/src/metabase/common/utils/groups.ts
+++ b/frontend/src/metabase/common/utils/groups.ts
@@ -51,6 +51,19 @@ export function canEditMembership(group: Pick<GroupInfo, "magic_group_type">) {
return !isDefaultGroup(group) && !PLUGIN_TENANTS.isExternalUsersGroup(group);
}
+/**
+ * Mirrors the server-side gate: additions to the Data Analysts group are refused
+ * without advanced-permissions, removals are always allowed.
+ */
+export function getAddMembersDisabledReason(
+ group: Pick<GroupInfo, "magic_group_type">,
+ hasAdvancedPermissions: boolean,
+): string | null {
+ return isDataAnalystGroup(group) && !hasAdvancedPermissions
+ ? t`Adding members to this group requires a plan that includes the Data Analysts group. Members can only be removed.`
+ : null;
+}
+
export function getGroupNameLocalized(group: Pick<GroupInfo, "name">) {
const specialName = SPECIAL_GROUP_NAMES[group.name];
return specialName ? specialName() : group.name;
diff --git a/frontend/src/metabase/current-user/index.ts b/frontend/src/metabase/current-user/index.ts
index 106d0eeaca8..dde09776d0b 100644
--- a/frontend/src/metabase/current-user/index.ts
+++ b/frontend/src/metabase/current-user/index.ts
@@ -19,6 +19,7 @@ export {
getUserId,
getUserIsAdmin,
getUserIsAnalyst,
+ getUserIsEntitledAnalyst,
getUserPersonalCollectionId,
} from "./selectors";
export { useUserAcknowledgement } from "./use-user-acknowledgement";
diff --git a/frontend/src/metabase/current-user/selectors.ts b/frontend/src/metabase/current-user/selectors.ts
index 4a529ca9c13..9e2dbcf4ad3 100644
--- a/frontend/src/metabase/current-user/selectors.ts
+++ b/frontend/src/metabase/current-user/selectors.ts
@@ -1,6 +1,7 @@
import { createSelector } from "@reduxjs/toolkit";
import type { State } from "metabase/redux/store";
+import { getTokenFeature } from "metabase/settings";
import type { User } from "metabase-types/api";
import { currentUserApi } from "./api/current-user";
@@ -24,6 +25,18 @@ export const getUserIsAnalyst = createSelector(
(user) => !!user?.is_data_analyst,
);
+/**
+ * A Data Analyst whose plan still includes the feature. Membership survives a
+ * downgrade (the permissions it grants keep working), but the app areas the
+ * role unlocks on its own pause until the feature returns.
+ *
+ * Mirrors entitled-data-analyst? in src/metabase/api/common.clj, minus the
+ * superuser shortcut.
+ */
+export const getUserIsEntitledAnalyst = (state: State): boolean =>
+ getUserIsAnalyst(state) &&
+ Boolean(getTokenFeature(state, "advanced_permissions"));
+
export const canManageSubscriptions = createSelector(
[
getUserIsAdmin,
diff --git a/frontend/src/metabase/current-user/selectors.unit.spec.ts b/frontend/src/metabase/current-user/selectors.unit.spec.ts
index 7778fd7a356..1bca7506bca 100644
--- a/frontend/src/metabase/current-user/selectors.unit.spec.ts
+++ b/frontend/src/metabase/current-user/selectors.unit.spec.ts
@@ -1,12 +1,17 @@
+import { mockSettings } from "__support__/settings";
import { createMockState } from "__support__/state";
import { reinitialize } from "metabase/plugins";
-import { createMockUser } from "metabase-types/api/mocks";
+import {
+ createMockTokenFeatures,
+ createMockUser,
+} from "metabase-types/api/mocks";
import { PLUGIN_APPLICATION_PERMISSIONS_SELECTORS } from "./plugins";
import {
canAccessDataModel,
getUserAttributes,
getUserIsAdmin,
+ getUserIsEntitledAnalyst,
} from "./selectors";
describe("metabase/current-user", () => {
@@ -26,6 +31,45 @@ describe("metabase/current-user", () => {
expect(getUserIsAdmin(state)).toBe(false);
});
+ describe("getUserIsEntitledAnalyst", () => {
+ const setup = ({
+ isAnalyst,
+ hasAdvancedPermissions,
+ }: {
+ isAnalyst: boolean;
+ hasAdvancedPermissions: boolean;
+ }) =>
+ createMockState({
+ currentUser: createMockUser({
+ is_superuser: false,
+ is_data_analyst: isAnalyst,
+ }),
+ settings: mockSettings({
+ "token-features": createMockTokenFeatures({
+ advanced_permissions: hasAdvancedPermissions,
+ }),
+ }),
+ });
+
+ it("is true for an analyst while the plan includes the feature", () => {
+ const state = setup({ isAnalyst: true, hasAdvancedPermissions: true });
+
+ expect(getUserIsEntitledAnalyst(state)).toBe(true);
+ });
+
+ it("is false for an analyst once the plan lost the feature", () => {
+ const state = setup({ isAnalyst: true, hasAdvancedPermissions: false });
+
+ expect(getUserIsEntitledAnalyst(state)).toBe(false);
+ });
+
+ it("is false for a non-analyst even with the feature", () => {
+ const state = setup({ isAnalyst: false, hasAdvancedPermissions: true });
+
+ expect(getUserIsEntitledAnalyst(state)).toBe(false);
+ });
+ });
+
describe("canAccessDataModel", () => {
afterEach(() => {
reinitialize();
diff --git a/frontend/src/metabase/data-studio/route-guards.unit.spec.tsx b/frontend/src/metabase/data-studio/route-guards.unit.spec.tsx
new file mode 100644
index 00000000000..9abba73b5e0
--- /dev/null
+++ b/frontend/src/metabase/data-studio/route-guards.unit.spec.tsx
@@ -0,0 +1,79 @@
+import { mockSettings } from "__support__/settings";
+import { createMockState } from "__support__/state";
+import { renderWithProviders, screen, waitFor } from "__support__/ui";
+import { Route } from "metabase/router";
+import type { TokenFeatures, User } from "metabase-types/api";
+import {
+ createMockTokenFeatures,
+ createMockUser,
+} from "metabase-types/api/mocks";
+
+import { CanAccessDataStudio } from "./route-guards";
+
+interface SetupOpts {
+ user: Partial<User>;
+ tokenFeatures?: Partial<TokenFeatures>;
+}
+
+const setup = ({ user, tokenFeatures = {} }: SetupOpts) =>
+ renderWithProviders(
+ <>
+ <Route element={<CanAccessDataStudio />}>
+ <Route path="/data-studio" element={<div>data studio page</div>} />
+ </Route>
+ <Route path="/unauthorized" element={<div>unauthorized</div>} />
+ </>,
+ {
+ storeInitialState: createMockState({
+ currentUser: createMockUser(user),
+ settings: mockSettings({
+ "has-user-setup": true,
+ "token-features": createMockTokenFeatures(tokenFeatures),
+ }),
+ }),
+ withRouter: true,
+ initialRoute: "/data-studio",
+ },
+ );
+
+describe("CanAccessDataStudio", () => {
+ it("renders for analysts when advanced-permissions is available", () => {
+ setup({
+ user: { is_data_analyst: true, is_superuser: false },
+ tokenFeatures: { advanced_permissions: true },
+ });
+
+ expect(screen.getByText("data studio page")).toBeInTheDocument();
+ });
+
+ it("redirects analysts to unauthorized when advanced-permissions is absent", async () => {
+ const { router } = setup({
+ user: { is_data_analyst: true, is_superuser: false },
+ tokenFeatures: { advanced_permissions: false },
+ });
+
+ await waitFor(() => {
+ expect(router?.location.pathname).toBe("/unauthorized");
+ });
+ });
+
+ it("renders for admins when advanced-permissions is absent", () => {
+ setup({
+ user: { is_data_analyst: false, is_superuser: true },
+ tokenFeatures: { advanced_permissions: false },
+ });
+
+ expect(screen.getByText("data studio page")).toBeInTheDocument();
+ });
+
+ it("redirects users who are neither admins nor analysts", async () => {
+ const { router } = setup({
+ user: { is_data_analyst: false, is_superuser: false },
+ tokenFeatures: { advanced_permissions: true },
+ });
+
+ await waitFor(() => {
+ expect(router?.location.pathname).toBe("/unauthorized");
+ });
+ });
+});
diff --git a/frontend/src/metabase/forms/components/FormGroupWidget/FormGroupWidget.tsx b/frontend/src/metabase/forms/components/FormGroupWidget/FormGroupWidget.tsx
index ce845ef4ffb..630624bcf2f 100644
--- a/frontend/src/metabase/forms/components/FormGroupWidget/FormGroupWidget.tsx
+++ b/frontend/src/metabase/forms/components/FormGroupWidget/FormGroupWidget.tsx
@@ -4,8 +4,10 @@ import { forwardRef, useCallback } from "react";
import { t } from "ttag";
import { useListPermissionsGroupsQuery } from "metabase/api";
+import { useHasTokenFeature } from "metabase/common/hooks";
+import { getAddMembersDisabledReason } from "metabase/common/utils/groups";
import type { SelectProps } from "metabase/ui";
-import { Loader, Select } from "metabase/ui";
+import { DefaultSelectItem, Loader, Select, Tooltip } from "metabase/ui";
import type { GroupId } from "metabase-types/api";
interface FormGroupWidgetProps extends Omit<
@@ -24,6 +26,7 @@ export const FormGroupWidget = forwardRef(function FormGroupWidget(
const [{ value }, { error, touched }, { setValue, setTouched }] = useField<
GroupId | null | undefined
>(name);
+ const hasAdvancedPermissions = useHasTokenFeature("advanced_permissions");
const handleChange = useCallback(
(newValue: string) => {
@@ -46,11 +49,35 @@ export const FormGroupWidget = forwardRef(function FormGroupWidget(
return <Loader size={16} />;
}
+ // Only a change of group is an addition; keeping the current one is not.
+ const disabledReasonByValue = new Map(
+ groups.map((group) => [
+ String(group.id),
+ group.id === value
+ ? null
+ : getAddMembersDisabledReason(group, hasAdvancedPermissions),
+ ]),
+ );
+
const groupOptions = groups.map(({ id, name }) => ({
value: String(id),
label: name,
+ disabled: disabledReasonByValue.get(String(id)) != null,
}));
+ const renderOption: SelectProps["renderOption"] = ({ option, checked }) => {
+ const disabledReason = disabledReasonByValue.get(option.value) ?? null;
+ return (
+ <Tooltip
+ label={disabledReason}
+ disabled={disabledReason == null}
+ position="right"
+ >
+ <DefaultSelectItem {...option} selected={checked} />
+ </Tooltip>
+ );
+ };
+
return (
<Select
placeholder={t`Select a group`}
@@ -60,6 +87,7 @@ export const FormGroupWidget = forwardRef(function FormGroupWidget(
value={value == null ? undefined : String(value)}
error={touched && error ? <div role="alert">{error}</div> : null}
data={groupOptions}
+ renderOption={renderOption}
onChange={handleChange}
onBlur={handleBlur}
/>
diff --git a/frontend/src/metabase/forms/components/FormGroupWidget/FormGroupWidget.unit.spec.tsx b/frontend/src/metabase/forms/components/FormGroupWidget/FormGroupWidget.unit.spec.tsx
new file mode 100644
index 00000000000..45facdedc5a
--- /dev/null
+++ b/frontend/src/metabase/forms/components/FormGroupWidget/FormGroupWidget.unit.spec.tsx
@@ -0,0 +1,98 @@
+import userEvent from "@testing-library/user-event";
+
+import { setupGroupsEndpoint } from "__support__/server-mocks";
+import { mockSettings } from "__support__/settings";
+import { createMockState } from "__support__/state";
+import { renderWithProviders, screen, within } from "__support__/ui";
+import { Form, FormProvider } from "metabase/forms";
+import type { GroupId, TokenFeatures } from "metabase-types/api";
+import {
+ createMockGroup,
+ createMockTokenFeatures,
+} from "metabase-types/api/mocks";
+
+import { FormGroupWidget } from "./FormGroupWidget";
+
+const ALL_USERS = createMockGroup({
+ id: 1,
+ name: "All Users",
+ magic_group_type: "all-internal-users",
+});
+const DATA_ANALYSTS = createMockGroup({
+ id: 2,
+ name: "Data Analysts",
+ magic_group_type: "data-analyst",
+});
+
+const LABEL = "Group";
+const ADD_DISABLED_MESSAGE =
+ "Adding members to this group requires a plan that includes the Data Analysts group. Members can only be removed.";
+
+const setup = async ({
+ tokenFeatures = {},
+ currentGroupId = null,
+}: {
+ tokenFeatures?: Partial<TokenFeatures>;
+ currentGroupId?: GroupId | null;
+} = {}) => {
+ setupGroupsEndpoint([ALL_USERS, DATA_ANALYSTS]);
+ const settings = mockSettings({
+ "token-features": createMockTokenFeatures(tokenFeatures),
+ });
+
+ renderWithProviders(
+ <FormProvider
+ initialValues={{ group_id: currentGroupId }}
+ onSubmit={jest.fn()}
+ >
+ <Form>
+ <FormGroupWidget name="group_id" label={LABEL} />
+ </Form>
+ </FormProvider>,
+ { storeInitialState: createMockState({ settings }) },
+ );
+
+ await userEvent.click(await screen.findByLabelText(LABEL));
+};
+
+const getOption = (name: string) => screen.getByRole("option", { name });
+
+describe("FormGroupWidget", () => {
+ it("disables the Data Analysts option without the advanced-permissions feature", async () => {
+ await setup();
+
+ expect(getOption("Data Analysts")).toHaveAttribute(
+ "data-combobox-disabled",
+ "true",
+ );
+ expect(getOption("All Users")).not.toHaveAttribute(
+ "data-combobox-disabled",
+ );
+
+ await userEvent.hover(
+ within(getOption("Data Analysts")).getByText("Data Analysts"),
+ );
+ expect(await screen.findByRole("tooltip")).toHaveTextContent(
+ ADD_DISABLED_MESSAGE,
+ );
+ });
+
+ it("keeps the gated group selectable when it is already the current one", async () => {
+ await setup({ currentGroupId: DATA_ANALYSTS.id });
+
+ expect(getOption("Data Analysts")).not.toHaveAttribute(
+ "data-combobox-disabled",
+ );
+
+ await userEvent.click(getOption("Data Analysts"));
+ expect(await screen.findByLabelText(LABEL)).toHaveValue("Data Analysts");
+ });
+
+ it("leaves the Data Analysts option selectable with the advanced-permissions feature", async () => {
+ await setup({ tokenFeatures: { advanced_permissions: true } });
+
+ expect(getOption("Data Analysts")).not.toHaveAttribute(
+ "data-combobox-disabled",
+ );
+ });
+});
diff --git a/frontend/src/metabase/home/components/Onboarding/tests/common.unit.spec.tsx b/frontend/src/metabase/home/components/Onboarding/tests/common.unit.spec.tsx
index 14f5e75d7c0..d5fefb5a585 100644
--- a/frontend/src/metabase/home/components/Onboarding/tests/common.unit.spec.tsx
+++ b/frontend/src/metabase/home/components/Onboarding/tests/common.unit.spec.tsx
@@ -358,7 +358,12 @@ describe("Onboarding", () => {
});
it("'data-studio' CTA should be visible to non-admin analysts", () => {
- setup({ isAdmin: false, isAnalyst: true, openItem: "data-studio" });
+ setup({
+ isAdmin: false,
+ isAnalyst: true,
+ openItem: "data-studio",
+ tokenFeatures: { advanced_permissions: true },
+ });
const cta = within(screen.getByTestId("data-studio-cta")).getByRole(
"link",
diff --git a/frontend/src/metabase/metrics/components/MetricHeader/MetricTabs/MetricTabs.tsx b/frontend/src/metabase/metrics/components/MetricHeader/MetricTabs/MetricTabs.tsx
index 30e9597ffec..386fa8b0a37 100644
--- a/frontend/src/metabase/metrics/components/MetricHeader/MetricTabs/MetricTabs.tsx
+++ b/frontend/src/metabase/metrics/components/MetricHeader/MetricTabs/MetricTabs.tsx
@@ -7,7 +7,10 @@ import {
PillTabNavigation,
} from "metabase/common/components/PillTabNavigation";
import type { MetricUrls } from "metabase/common/metrics/types";
-import { getUserIsAdmin, getUserIsAnalyst } from "metabase/current-user";
+import {
+ getUserIsAdmin,
+ getUserIsEntitledAnalyst,
+} from "metabase/current-user";
import { useMetadataProvider } from "metabase/metadata-store";
import { isNumericMetric } from "metabase/metrics/utils/validation";
import { PLUGIN_DEPENDENCIES } from "metabase/plugins";
@@ -26,7 +29,7 @@ export function MetricTabs({ card, urls }: MetricTabsProps) {
const hasDimensions =
metric?.dimensions != null && metric.dimensions.length > 0;
const canSeeDependencies = useSelector(
- (state) => getUserIsAdmin(state) || getUserIsAnalyst(state),
+ (state) => getUserIsAdmin(state) || getUserIsEntitledAnalyst(state),
);
const query = useMemo(
() => Lib.fromJsQuery(metadataProvider, card.dataset_query),
diff --git a/frontend/src/metabase/metrics/components/MetricHeader/MetricTabs/MetricTabs.unit.spec.tsx b/frontend/src/metabase/metrics/components/MetricHeader/MetricTabs/MetricTabs.unit.spec.tsx
index e6a00420d64..edc1406ab7d 100644
--- a/frontend/src/metabase/metrics/components/MetricHeader/MetricTabs/MetricTabs.unit.spec.tsx
+++ b/frontend/src/metabase/metrics/components/MetricHeader/MetricTabs/MetricTabs.unit.spec.tsx
@@ -31,13 +31,15 @@ const urls: MetricUrls = {
};
describe("MetricTabs", () => {
+ const settings = mockSettings({
+ "token-features": createMockTokenFeatures({
+ advanced_permissions: true,
+ cache_granular_controls: true,
+ dependencies: true,
+ }),
+ });
+
beforeAll(() => {
- mockSettings({
- "token-features": createMockTokenFeatures({
- cache_granular_controls: true,
- dependencies: true,
- }),
- });
setupEnterprisePlugins();
});
@@ -72,6 +74,7 @@ describe("MetricTabs", () => {
is_superuser: role === "admin",
is_data_analyst: role === "analyst",
}),
+ settings,
entities: createMockEntitiesState({
databases: hasDataPermissions ? [createSampleDatabase()] : [],
questions: [card],
diff --git a/frontend/src/metabase/metrics/pages/MetricAboutPage/MetricAbout/DescriptionSection/DescriptionSection.tsx b/frontend/src/metabase/metrics/pages/MetricAboutPage/MetricAbout/DescriptionSection/DescriptionSection.tsx
index 1c200a56d87..edf97bce5c6 100644
--- a/frontend/src/metabase/metrics/pages/MetricAboutPage/MetricAbout/DescriptionSection/DescriptionSection.tsx
+++ b/frontend/src/metabase/metrics/pages/MetricAboutPage/MetricAbout/DescriptionSection/DescriptionSection.tsx
@@ -10,7 +10,10 @@ import { EditableText } from "metabase/common/components/EditableText";
import { Markdown } from "metabase/common/components/Markdown";
import { useMetadataToasts } from "metabase/common/hooks";
import type { MetricUrls } from "metabase/common/metrics/types";
-import { getUserIsAdmin, getUserIsAnalyst } from "metabase/current-user";
+import {
+ getUserIsAdmin,
+ getUserIsEntitledAnalyst,
+} from "metabase/current-user";
import { PLUGIN_DEPENDENCIES } from "metabase/plugins";
import { useSelector } from "metabase/redux";
import { Box, Stack, Text, Tooltip } from "metabase/ui";
@@ -30,8 +33,9 @@ export function DescriptionSection({ card, urls }: DescriptionSectionProps) {
const { sendSuccessToast, sendErrorToast } = useMetadataToasts();
const canSeeRelationships =
- useSelector((state) => getUserIsAdmin(state) || getUserIsAnalyst(state)) &&
- PLUGIN_DEPENDENCIES.isEnabled;
+ useSelector(
+ (state) => getUserIsAdmin(state) || getUserIsEntitledAnalyst(state),
+ ) && PLUGIN_DEPENDENCIES.isEnabled;
const { data: database } = useGetDatabaseQuery(
{ id: card.database_id! },
diff --git a/frontend/src/metabase/metrics/pages/MetricAboutPage/MetricAbout/DescriptionSection/DescriptionSection.unit.spec.tsx b/frontend/src/metabase/metrics/pages/MetricAboutPage/MetricAbout/DescriptionSection/DescriptionSection.unit.spec.tsx
index 25f69583c01..4f70a7bcedb 100644
--- a/frontend/src/metabase/metrics/pages/MetricAboutPage/MetricAbout/DescriptionSection/DescriptionSection.unit.spec.tsx
+++ b/frontend/src/metabase/metrics/pages/MetricAboutPage/MetricAbout/DescriptionSection/DescriptionSection.unit.spec.tsx
@@ -97,7 +97,10 @@ function setup({
is_data_analyst: role === "analyst",
}),
settings: mockSettings({
- "token-features": createMockTokenFeatures({ dependencies: true }),
+ "token-features": createMockTokenFeatures({
+ advanced_permissions: true,
+ dependencies: true,
+ }),
}),
});
// Enable the dependencies plugin via real EE init (reads the mocked settings).
diff --git a/frontend/src/metabase/monitor/components/MonitorLayout/MonitorLayout.unit.spec.tsx b/frontend/src/metabase/monitor/components/MonitorLayout/MonitorLayout.unit.spec.tsx
index fedaa1f7cdb..d0dfb8ff7c9 100644
--- a/frontend/src/metabase/monitor/components/MonitorLayout/MonitorLayout.unit.spec.tsx
+++ b/frontend/src/metabase/monitor/components/MonitorLayout/MonitorLayout.unit.spec.tsx
@@ -289,6 +289,7 @@ describe("MonitorLayout", () => {
it("hides the migrated Tools tabs for an analyst without the monitoring permission", async () => {
setup({
+ tokenFeatures: { advanced_permissions: true },
user: createMockUser({
is_superuser: false,
is_data_analyst: true,
@@ -350,6 +351,7 @@ describe("MonitorLayout", () => {
it("hides Alerts management for an analyst even with the monitoring permission", async () => {
setup({
+ tokenFeatures: { advanced_permissions: true },
user: createMockUser({
is_superuser: false,
is_data_analyst: true,
diff --git a/frontend/src/metabase/monitor/route-guards.unit.spec.tsx b/frontend/src/metabase/monitor/route-guards.unit.spec.tsx
index e407f781f6d..a51f8fe4720 100644
--- a/frontend/src/metabase/monitor/route-guards.unit.spec.tsx
+++ b/frontend/src/metabase/monitor/route-guards.unit.spec.tsx
@@ -2,7 +2,11 @@ import { createMockSettingsState, createMockState } from "__support__/state";
import { renderWithProviders, screen, waitFor } from "__support__/ui";
import { Route } from "metabase/router";
import { parseSearchQuery } from "metabase/utils/browser";
-import { createMockUser } from "metabase-types/api/mocks";
+import type { TokenFeatures } from "metabase-types/api";
+import {
+ createMockTokenFeatures,
+ createMockUser,
+} from "metabase-types/api/mocks";
import {
CanAccessAiAuditing,
@@ -14,9 +18,10 @@ describe("monitor route-guards", () => {
describe("CanAccessMonitor", () => {
interface SetupOpts {
currentUser?: ReturnType<typeof createMockUser>;
+ tokenFeatures?: Partial<TokenFeatures>;
}
- const setup = ({ currentUser }: SetupOpts = {}) => {
+ const setup = ({ currentUser, tokenFeatures }: SetupOpts = {}) => {
return renderWithProviders(
<>
<Route element={<CanAccessMonitor />}>
@@ -28,7 +33,10 @@ describe("monitor route-guards", () => {
{
storeInitialState: createMockState({
currentUser,
- settings: createMockSettingsState({ "has-user-setup": true }),
+ settings: createMockSettingsState({
+ "has-user-setup": true,
+ "token-features": createMockTokenFeatures(tokenFeatures),
+ }),
}),
withRouter: true,
initialRoute: "/monitor",
@@ -69,10 +77,25 @@ describe("monitor route-guards", () => {
is_data_analyst: true,
is_superuser: false,
}),
+ tokenFeatures: { advanced_permissions: true },
});
expect(screen.getByText("monitor page")).toBeInTheDocument();
});
+
+ it("redirects an analyst whose plan lost the feature to unauthorized", async () => {
+ const { router } = setup({
+ currentUser: createMockUser({
+ is_data_analyst: true,
+ is_superuser: false,
+ }),
+ tokenFeatures: { advanced_permissions: false },
+ });
+
+ await waitFor(() => {
+ expect(router?.location.pathname).toBe("/unauthorized");
+ });
+ });
});
describe("CanAccessAlertsManagement", () => {
diff --git a/frontend/src/metabase/nav/components/AppSwitcher/AppSwitcher.unit.spec.tsx b/frontend/src/metabase/nav/components/AppSwitcher/AppSwitcher.unit.spec.tsx
index 7d2a2be95c7..64def4c3040 100644
--- a/frontend/src/metabase/nav/components/AppSwitcher/AppSwitcher.unit.spec.tsx
+++ b/frontend/src/metabase/nav/components/AppSwitcher/AppSwitcher.unit.spec.tsx
@@ -9,7 +9,7 @@ import {
import { renderWithProviders, screen, waitFor, within } from "__support__/ui";
import { dayjs } from "metabase/dayjs";
import { Route } from "metabase/router";
-import type { HelpLinkSetting } from "metabase-types/api";
+import type { HelpLinkSetting, TokenFeatures } from "metabase-types/api";
import {
createMockMetabaseInfo,
createMockTokenFeatures,
@@ -50,18 +50,22 @@ const adminNavItem = {
async function setup({
isAdmin = false,
+ isAnalyst = false,
isHosted = false,
isPaidPlan = true,
helpLinkSetting = "metabase",
helpLinkCustomDestinationSetting = "https://custom-destination.com/help",
instanceCreationDate = dayjs().toISOString(),
+ tokenFeatures = {},
}: {
isAdmin?: boolean;
+ isAnalyst?: boolean;
isHosted?: boolean;
isPaidPlan?: boolean;
helpLinkSetting?: HelpLinkSetting;
helpLinkCustomDestinationSetting?: string;
instanceCreationDate?: string;
+ tokenFeatures?: Partial<TokenFeatures>;
} = {}) {
setupBugReportingDetailsEndpoint();
@@ -71,7 +75,7 @@ async function setup({
"help-link": helpLinkSetting,
"help-link-custom-destination": helpLinkCustomDestinationSetting,
"instance-creation": instanceCreationDate,
- "token-features": createMockTokenFeatures(),
+ "token-features": createMockTokenFeatures(tokenFeatures),
});
const admin = createMockAdminState({
@@ -92,7 +96,11 @@ async function setup({
storeInitialState: {
admin,
settings,
- currentUser: { ...USER, is_superuser: isAdmin },
+ currentUser: {
+ ...USER,
+ is_superuser: isAdmin,
+ is_data_analyst: isAnalyst,
+ },
},
},
);
@@ -209,6 +217,37 @@ describe("ProfileLink", () => {
});
});
+ it("should show data studio and monitor for analysts when advanced-permissions is available", async () => {
+ await setup({
+ isAnalyst: true,
+ tokenFeatures: { advanced_permissions: true },
+ });
+
+ expect(screen.getByText("Data studio")).toBeInTheDocument();
+ expect(screen.getByText("Monitor")).toBeInTheDocument();
+ });
+
+ // Data Studio and Monitor diagnostics both come with the Data Analyst role rather than a
+ // permissions graph, so both pause while the plan lacks the feature.
+ it("should hide data studio and monitor from analysts when advanced-permissions is absent", async () => {
+ await setup({
+ isAnalyst: true,
+ tokenFeatures: { advanced_permissions: false },
+ });
+
+ expect(screen.queryByText("Data studio")).not.toBeInTheDocument();
+ expect(screen.queryByText("Monitor")).not.toBeInTheDocument();
+ });
+
+ it("should show data studio for admins when advanced-permissions is absent", async () => {
+ await setup({
+ isAdmin: true,
+ tokenFeatures: { advanced_permissions: false },
+ });
+
+ expect(screen.getByText("Data studio")).toBeInTheDocument();
+ });
+
it("tracks opening Monitor from the app switcher", async () => {
trackMonitorOpened.mockClear();
await setup({ isAdmin: true });
diff --git a/frontend/src/metabase/reference/glossary/GlossaryContainer.unit.spec.tsx b/frontend/src/metabase/reference/glossary/GlossaryContainer.unit.spec.tsx
new file mode 100644
index 00000000000..70d976d859b
--- /dev/null
+++ b/frontend/src/metabase/reference/glossary/GlossaryContainer.unit.spec.tsx
@@ -0,0 +1,43 @@
+import fetchMock from "fetch-mock";
+
+import { createMockState } from "__support__/state";
+import { renderWithProviders, screen } from "__support__/ui";
+import type { GlossaryItem } from "metabase/api";
+import { createMockUser } from "metabase-types/api/mocks";
+
+import { GlossaryContainer } from "./GlossaryContainer";
+
+const GLOSSARY: GlossaryItem[] = [
+ { id: 1, term: "ARR", definition: "Annual recurring revenue" },
+];
+
+// Writability comes from the API: `can_write` mirrors api/entitled-data-analyst?, which gates the
+// glossary write endpoints, so the container never has to reason about roles or token features itself.
+const setup = ({ canWrite }: { canWrite: boolean }) => {
+ fetchMock.get("path:/api/glossary", { data: GLOSSARY, can_write: canWrite });
+
+ renderWithProviders(<GlossaryContainer />, {
+ storeInitialState: createMockState({
+ currentUser: createMockUser(),
+ }),
+ withRouter: true,
+ });
+};
+
+const getNewTermButton = () => screen.queryByRole("button", { name: /term/i });
+
+describe("GlossaryContainer", () => {
+ it("lets the user manage the glossary when the API reports it as writable", async () => {
+ setup({ canWrite: true });
+
+ expect(await screen.findByText("ARR")).toBeInTheDocument();
+ expect(getNewTermButton()).toBeInTheDocument();
+ });
+
+ it("makes the glossary read-only when the API reports it as not writable", async () => {
+ setup({ canWrite: false });
+
+ expect(await screen.findByText("ARR")).toBeInTheDocument();
+ expect(getNewTermButton()).not.toBeInTheDocument();
+ });
+});
diff --git a/src/metabase/api/common.clj b/src/metabase/api/common.clj
index a99b5af57d9..93848b4b0d7 100644
--- a/src/metabase/api/common.clj
+++ b/src/metabase/api/common.clj
@@ -200,15 +200,31 @@
(check-403 *is-superuser?*))
(defn is-data-analyst?
- "Returns a boolean representing whether the current user is a data analyst (or superuser)."
+ "Whether the current user is a superuser or a member of the Data Analysts group, regardless of whether the
+ `advanced-permissions` feature is available. See [[entitled-data-analyst?]]."
[]
+ ;; role membership only: the capabilities the permissions graph renders survive the loss of the feature
(or *is-superuser?* *is-data-analyst?*))
-(defn check-data-analyst
- "Check that `*current-user*` is a data analyst (or superuser) or throw a 403.
- Superusers are automatically considered data analysts."
+(defn- advanced-permissions-enabled?
[]
- (check-403 (is-data-analyst?)))
+ ;; resolved late: metabase.premium-features loads on top of this namespace
+ ((requiring-resolve 'metabase.premium-features.core/enable-advanced-permissions?)))
+
+(defn entitled-data-analyst?
+ "Whether the current user is a superuser, or a member of the Data Analysts group while the `advanced-permissions`
+ feature is available."
+ []
+ ;; capabilities that appear in no permissions graph pause on downgrade, so they key off this rather than
+ ;; [[is-data-analyst?]]
+ (or *is-superuser?*
+ (and *is-data-analyst?*
+ (advanced-permissions-enabled?))))
+
+(defn check-data-studio-access
+ "Check that `*current-user*` may enter Data Studio, or throw a 403."
+ []
+ (check-403 (entitled-data-analyst?)))
;; checkp- functions: as in "check param". These functions expect that you pass a symbol so they can throw exceptions
;; w/ relevant error messages.
diff --git a/src/metabase/core/core.clj b/src/metabase/core/core.clj
index b2b29b06080..ca3b82fee37 100644
--- a/src/metabase/core/core.clj
+++ b/src/metabase/core/core.clj
@@ -25,7 +25,6 @@
[metabase.metrics.core :as metrics]
[metabase.notification.core :as notification]
[metabase.oauth-server.api :as oauth-server.api]
- [metabase.permissions.core :as perms]
[metabase.plugins.core :as plugins]
[metabase.premium-features.core :refer [defenterprise]]
[metabase.sample-data.core :as sample-data]
@@ -206,8 +205,6 @@
;; negatives, but for now there's not much we can do
(mdb/setup-db! :create-sample-content? (not config/is-test?))
(mdb/encrypt-plaintext-columns!)
- ;; In OSS, convert any Data Analysts group with members to a normal visible group
- (perms/sync-data-analyst-group-for-oss!)
;; Disable read-only mode if its on during startup.
;; This can happen if a cloud migration process dies during h2 dump.
(when (cloud-migration/read-only-mode)
diff --git a/src/metabase/data_studio/api/table.clj b/src/metabase/data_studio/api/table.clj
index 71a08eabb4f..e14f0e7c2c0 100644
--- a/src/metabase/data_studio/api/table.clj
+++ b/src/metabase/data_studio/api/table.clj
@@ -166,7 +166,7 @@
[:entity_type {:optional true} [:maybe :string]]
[:owner_email {:optional true} [:maybe :string]]
[:owner_user_id {:optional true} [:maybe :int]]]]]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(let [selectors (select-keys body [:database_ids :schema_ids :table_ids])
set-ks [:data_authority
:data_source
@@ -192,7 +192,7 @@
[_route-params
_query-params
body :- ::table-selectors]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(let [selectors (select-keys body [:database_ids :schema_ids :table_ids])
selected-tables (data-studio.db/selection-columns-for-selectors selectors 2)
selected-table (when-not (next selected-tables)
@@ -219,7 +219,7 @@
[_
_
body :- ::table-selectors]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(let [tables (data-studio.db/tables-matching-selectors-in-id-order body)
db-ids (sort (set (map :db_id tables)))]
(doseq [database (data-studio.db/databases db-ids)]
@@ -239,7 +239,7 @@
[_
_
body :- ::table-selectors]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(let [tables (data-studio.db/tables-matching-selectors-in-id-order body)]
;; same permission skip as the single-table api, see comment in /:id/rescan_values
(doseq [table tables]
@@ -252,7 +252,7 @@
[_
_
body :- ::table-selectors]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(let [tables (data-studio.db/tables-matching-selectors-in-id-order body)]
(data-studio.db/delete-field-values-for-tables! (map :id tables))
nil))
diff --git a/src/metabase/glossary/api.clj b/src/metabase/glossary/api.clj
index d8afac1f2db..aeedac13324 100644
--- a/src/metabase/glossary/api.clj
+++ b/src/metabase/glossary/api.clj
@@ -26,7 +26,7 @@
[_route-params
{:keys [search]} :- [:maybe [:map {:closed true} [:search {:optional true} [:maybe ms/NonBlankString]]]]]
{:data (t2/hydrate (glossary.db/glossary-entries search) :creator)
- :can_write (boolean (and (api/is-data-analyst?) (editable?)))})
+ :can_write (boolean (and (api/entitled-data-analyst?) (editable?)))})
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
@@ -39,7 +39,7 @@
body :- [:map {:closed true}
[:term ms/NonBlankString]
[:definition ms/NonBlankString]]]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(check-editable!)
(t2/hydrate (glossary.core/create-entry! api/*current-user-id* body) :creator))
@@ -54,7 +54,7 @@
body :- [:map {:closed true}
[:term ms/NonBlankString]
[:definition ms/NonBlankString]]]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(check-editable!)
(t2/hydrate (api/check-404 (glossary.core/update-entry! api/*current-user-id* id body)) :creator))
@@ -65,7 +65,7 @@
(api.macros/defendpoint :delete "/:id"
"Delete a glossary entry."
[{:keys [id]} :- [:map {:closed true} [:id ms/PositiveInt]]]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(check-editable!)
(api/check-404 (glossary.core/delete-entry! api/*current-user-id* id))
api/generic-204-no-content)
diff --git a/src/metabase/measures/models/measure.clj b/src/metabase/measures/models/measure.clj
index 7cf8812953f..2b13c07d85c 100644
--- a/src/metabase/measures/models/measure.clj
+++ b/src/metabase/measures/models/measure.clj
@@ -71,7 +71,7 @@
(let [table (or (:table instance)
(measures.db/table (:table_id instance)))]
(and (or api/*is-superuser?*
- (and api/*is-data-analyst?*
+ (and (api/entitled-data-analyst?)
(perms/user-has-permission-for-table?
api/*current-user-id*
:perms/view-data
@@ -89,7 +89,7 @@
(let [table (or (:table instance)
(measures.db/table (:table_id instance)))]
(and (or api/*is-superuser?*
- (and api/*is-data-analyst?*
+ (and (api/entitled-data-analyst?)
(perms/user-has-permission-for-table?
api/*current-user-id*
:perms/view-data
diff --git a/src/metabase/permissions/core.clj b/src/metabase/permissions/core.clj
index bef8acde2c6..a5b46a0534e 100644
--- a/src/metabase/permissions/core.clj
+++ b/src/metabase/permissions/core.clj
@@ -107,8 +107,7 @@
set-has-full-permissions?]
[metabase.permissions.models.permissions-group
non-magic-groups
- all-users-magic-group-type
- sync-data-analyst-group-for-oss!]
+ all-users-magic-group-type]
[metabase.permissions.models.permissions-group-membership
add-users-to-groups!
add-user-to-groups!
@@ -116,6 +115,7 @@
allow-changing-all-users-group-members
allow-changing-all-external-users-group-members
fail-to-remove-last-admin-msg
+ fail-to-add-data-analyst-msg
remove-user-from-group!
remove-user-from-groups!
remove-all-users-from-group!
diff --git a/src/metabase/permissions/db.clj b/src/metabase/permissions/db.clj
index b492670afc4..56a32c73cd7 100644
--- a/src/metabase/permissions/db.clj
+++ b/src/metabase/permissions/db.clj
@@ -312,11 +312,6 @@
[magic-group-type :- :string]
(t2/select-one [:model/PermissionsGroup :id :name :magic_group_type] :magic_group_type magic-group-type))
-(mu/defn group-by-magic-type
- "The PermissionsGroup of `magic-group-type`, or nil."
- [magic-group-type :- :string]
- (t2/select-one :model/PermissionsGroup :magic_group_type magic-group-type))
-
(mu/defn group-id-by-magic-type
"The ID of the PermissionsGroup of `magic-group-type`, or nil."
[magic-group-type :- :string]
@@ -352,11 +347,6 @@
[group-ids :- [:set ms/PositiveInt]]
(t2/select-pk->fn :is_tenant_group [:model/PermissionsGroup :id :is_tenant_group] :id [:in group-ids]))
-(mu/defn group-names-like
- "The set of PermissionsGroup names matching the SQL `pattern`."
- [pattern :- :string]
- (t2/select-fn-set :name :model/PermissionsGroup :name [:like pattern]))
-
(mu/defn group-members
"The active Users in the PermissionsGroups with `group-ids`. When `include-group-manager?` is true each row also
carries the membership's `:is_group_manager` flag."
@@ -381,17 +371,6 @@
:order-by [[[:lower :u.first_name] :asc]
[[:lower :u.last_name] :asc]]}))
-(mu/defn insert-group!
- "Insert `group` and return the new instance."
- [group :- (mut/select-keys ::permissions.schema/permissions-group.update [:name :magic_group_type :is_tenant_group])]
- (t2/insert-returning-instance! :model/PermissionsGroup group))
-
-(mu/defn update-group!
- "Apply `changes` to the PermissionsGroup with `group-id`."
- [group-id :- ms/PositiveInt
- changes :- (mut/select-keys ::permissions.schema/permissions-group.update [:name :magic_group_type :is_tenant_group])]
- (t2/update! :model/PermissionsGroup group-id changes))
-
(mu/defn group-member-counts
"A map of PermissionsGroup ID to number of active members in the group. Groups with no active members have no
entry."
@@ -435,11 +414,6 @@
[user-id-group-id->is-group-manager? :- [:map-of [:tuple ms/PositiveInt ms/PositiveInt] :boolean]]
(t2/query-one (insert-group-memberships-from-mapping-query user-id-group-id->is-group-manager?)))
-(mu/defn group-membership-count
- "The number of memberships of the PermissionsGroup with `group-id`."
- [group-id :- ms/PositiveInt]
- (t2/count :model/PermissionsGroupMembership :group_id group-id))
-
(mu/defn other-active-member-count
"The number of active Users other than `user-id` in the PermissionsGroup with `group-id`."
[group-id :- ms/PositiveInt
@@ -467,6 +441,19 @@
[group-id :- ms/PositiveInt]
(t2/select :model/PermissionsGroupMembership :group_id group-id))
+(mu/defn existing-membership-pairs :- [:set [:tuple ms/PositiveInt ms/PositiveInt]]
+ "The `[user-id group-id]` pairs among `pairs` that already have a PermissionsGroupMembership."
+ [pairs :- [:sequential [:tuple ms/PositiveInt ms/PositiveInt]]]
+ (if (empty? pairs)
+ #{}
+ (let [wanted (set pairs)]
+ (into #{}
+ (comp (map (juxt :user_id :group_id))
+ (filter wanted))
+ (t2/select [:model/PermissionsGroupMembership :user_id :group_id]
+ :user_id [:in (map first pairs)]
+ :group_id [:in (map second pairs)])))))
+
(mu/defn delete-memberships-for-user!
"Delete the PermissionsGroupMemberships of the User with `user-id`."
[user-id :- ::lib.schema.id/user]
@@ -718,11 +705,6 @@
changes :- (mut/select-keys ::users.schema/user.update [:is_superuser :is_data_analyst])]
(t2/update! :model/User :id [:in user-ids] changes))
-(mu/defn clear-data-analyst-flags!
- "Unset `is_data_analyst` on every User that has it set."
- []
- (t2/update! :model/User {:is_data_analyst true} {:is_data_analyst false}))
-
(mu/defn deactivate-active-tenant-users!
"Deactivate every active tenant User, marking them as deactivated with their tenant."
[]
diff --git a/src/metabase/permissions/models/permissions_group.clj b/src/metabase/permissions/models/permissions_group.clj
index 31d43ea7d62..2db256a2250 100644
--- a/src/metabase/permissions/models/permissions_group.clj
+++ b/src/metabase/permissions/models/permissions_group.clj
@@ -17,7 +17,6 @@
[metabase.settings.core :as setting]
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]
- [metabase.util.log :as log]
[methodical.core :as methodical]
[toucan2.core :as t2]
[toucan2.tools.hydrate :as t2.hydrate]))
@@ -92,23 +91,17 @@
(when (exists-with-name? group-name)
(throw (ex-info (tru "A group with that name already exists.") {:status-code 400}))))
-#_{:clj-kondo/ignore [:metabase/discourage-dynamic-vars]}
-(def ^:dynamic ^:private *allow-modifying-magic-groups*
- "Dynamic var that, when bound to true, allows modifying magic groups. Used by [[sync-data-analyst-group-for-oss!]]."
- false)
-
(defn- check-not-magic-group
"Make sure we're not trying to edit/delete one of the magic groups, or throw an exception."
[{id :id}]
{:pre [(integer? id)]}
- (when-not *allow-modifying-magic-groups*
- (doseq [magic-group [(all-users)
- (all-external-users)
- (admin)
- (data-analyst)]]
- (when (= id (:id magic-group))
- (throw (ex-info (tru "You cannot edit or delete the ''{0}'' permissions group!" (:name magic-group))
- {:status-code 400}))))))
+ (doseq [magic-group [(all-users)
+ (all-external-users)
+ (admin)
+ (data-analyst)]]
+ (when (= id (:id magic-group))
+ (throw (ex-info (tru "You cannot edit or delete the ''{0}'' permissions group!" (:name magic-group))
+ {:status-code 400})))))
;;; --------------------------------------------------- Lifecycle ----------------------------------------------------
@@ -207,77 +200,3 @@
(let [group-id->num-members (group-id->num-members)]
(for [group groups]
(assoc group :member_count (get group-id->num-members (u/the-id group) 0)))))
-
-;;; ------------------------------------------ OSS Data Analyst Group Handling ------------------------------------------
-
-(defn- unique-converted-group-name
- "Generate a unique name for the converted Data Analysts group.
- Returns \"Data Analysts (converted)\", or \"Data Analysts (converted) (2)\", etc."
- [group-name]
- (let [base-name (format "%s (converted)" group-name)
- like-pattern (str base-name "%")
- existing-names (permissions.db/group-names-like like-pattern)]
- (if-not (contains? existing-names base-name)
- base-name
- (loop [n 2]
- (let [candidate (str base-name " (" n ")")]
- (if-not (contains? existing-names candidate)
- candidate
- (recur (inc n))))))))
-
-(defn- grant-library-permissions!
- "Grant write permissions on all library collections to a group."
- [group-id]
- (when-let [collection-ids (seq (permissions.db/library-collection-ids))]
- (permissions.db/insert-permissions! (for [coll-id collection-ids]
- {:group_id group-id
- :object (str "/collection/" coll-id "/")}))))
-
-(defn- do-sync-conversion!
- "Convert the Data Analysts magic group (if it has members) to a normal visible group, and create a fresh empty
- magic group in its place. This ensures that we don't have an invisible group that affects permissions on OSS."
- []
- (when-let [existing-group (permissions.db/group-by-magic-type data-analyst-magic-group-type)]
- (when (pos? (permissions.db/group-membership-count (:id existing-group)))
- (log/info "Converting Data Analysts group to normal group for OSS")
- (binding [*allow-modifying-magic-groups* true]
- (t2/with-transaction [_conn]
- ;; Rename and demote the existing group to a normal visible group
- (permissions.db/update-group! (:id existing-group)
- {:name (unique-converted-group-name (:name existing-group))
- :magic_group_type nil})
- ;; Create new empty magic group with default library permissions, reusing the old name
- (let [{new-group-id :id} (permissions.db/insert-group! {:name (:name existing-group)
- :magic_group_type data-analyst-magic-group-type})]
- (grant-library-permissions! new-group-id))
- (permissions.db/clear-data-analyst-flags!))))))
-
-(def ^:private seconds-to-sleep-per-attempt 1)
-
-(defn sync-data-analyst-group-for-oss!
- "On startup, convert the Data Analysts group to a normal visible group if this instance definitively lacks
- the `:advanced-permissions` premium feature.
-
- In OSS, we don't want the Data Analysts group to be invisible while still granting permissions — that's a hidden
- backdoor. Instead, we convert any existing Data Analysts group (with members) to a normal group with a unique name
- like 'Data Analysts (converted)' that admins can see and manage. We then create a fresh empty Data Analysts magic
- group.
-
- Uses [[premium-features/canonically-has-feature?]] to distinguish between 'definitively no feature' and 'token
- check failed (indeterminate)'. If the token check is indeterminate, retries in a background thread until a
- canonical response is received.
-
- This is idempotent: if the magic group has no members, nothing happens."
- []
- (let [result (premium-features/canonically-has-feature? :advanced-permissions)]
- (case result
- true nil
- false (do-sync-conversion!)
- nil (future
- (loop [attempt 1]
- (Thread/sleep (long (* 1000 (min (* attempt seconds-to-sleep-per-attempt) 60))))
- (let [result (premium-features/canonically-has-feature? :advanced-permissions)]
- (case result
- true nil
- false (do-sync-conversion!)
- nil (recur (inc attempt)))))))))
diff --git a/src/metabase/permissions/models/permissions_group_membership.clj b/src/metabase/permissions/models/permissions_group_membership.clj
index c3b1646eecf..a1885e592a3 100644
--- a/src/metabase/permissions/models/permissions_group_membership.clj
+++ b/src/metabase/permissions/models/permissions_group_membership.clj
@@ -6,6 +6,7 @@
[metabase.permissions.db :as permissions.db]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.permissions.schema :as permissions.schema]
+ [metabase.premium-features.core :as premium-features]
[metabase.util :as u]
[metabase.util.i18n :refer [deferred-tru tru]]
[metabase.util.malli :as mu]
@@ -20,6 +21,12 @@
"Exception message when try to remove the last admin."
(deferred-tru "You cannot remove the last member of the ''Admin'' group!"))
+(def fail-to-add-data-analyst-msg
+ "Exception message when trying to add a member to the Data Analysts group without the `:advanced-permissions`
+ premium feature."
+ (deferred-tru (str "Adding people to the ''Data Analysts'' group requires the Advanced Permissions feature, "
+ "which is not enabled on this instance.")))
+
#_{:clj-kondo/ignore [:metabase/discourage-dynamic-vars]}
(def ^:dynamic *allow-changing-all-users-group-members*
"Should we allow people to be added to or removed from the All Users permissions group? By default, this is `false`,
@@ -62,6 +69,15 @@
(throw (ex-info (tru "You cannot add or remove users to/from the ''All tenant users'' group.")
{:status-code 400})))))
+(defn- check-can-add-to-data-analyst-group
+ "Throw a 402 if we're trying to *add* a user to the Data Analysts group without the `:advanced-permissions` premium
+ feature. Removals are never gated."
+ [group-id]
+ (when (and (= group-id (:id (perms-group/data-analyst)))
+ (not (premium-features/enable-advanced-permissions?)))
+ (throw (ex-info (str fail-to-add-data-analyst-msg)
+ {:status-code 402}))))
+
(defn throw-if-last-admin!
"Throw an Exception if there are no admins left besides this one. The assumption is that the one admin is about to be
archived or have their admin status removed."
@@ -144,6 +160,11 @@
(throw (ex-info "Conflicting permissions group memberships"
{:conflicts (distinct pgms)})))
(boolean (:is-group-manager? (first pgms))))))
+ ;; re-adding an existing member is a no-op, so everything below only ever sees genuine additions:
+ ;; the tenant checks, the Data Analysts gate, the insert, the flag updates and the events
+ user-id-group-id->is-group-manager? (apply dissoc user-id-group-id->is-group-manager?
+ (permissions.db/existing-membership-pairs
+ (keys user-id-group-id->is-group-manager?)))
[user-ids group-ids] (->> user-id-group-id->is-group-manager?
keys
(reduce (fn [[uids gids] [user-id group-id]]
@@ -163,7 +184,8 @@
:group-is-tenant? (group-id->tenant? group-id)}))))
_ (doseq [group-id group-ids]
(check-not-all-users-group group-id)
- (check-not-all-external-users-group group-id))
+ (check-not-all-external-users-group group-id)
+ (check-can-add-to-data-analyst-group group-id))
_ (doseq [[[user-id group-id] is-group-manager?] user-id-group-id->is-group-manager?]
(when (and is-group-manager? (user-id->tenant? user-id))
(throw (ex-info (tru "Tenant users cannot be made group managers")
@@ -185,25 +207,26 @@
(keep (fn [[user-id group-id]]
(when (= group-id (:id (perms-group/data-analyst)))
user-id))))]
- (t2/with-transaction [_conn]
- (when (< (permissions.db/insert-group-memberships-from-mapping! user-id-group-id->is-group-manager?)
- (count user-id-group-id->is-group-manager?))
- ;; Theoretically, there could be a race condition in the above check: a user or group may be changed to a tenant
- ;; user/group or vice versa AFTER we check (above) but BEFORE the insert (below). So just make sure that the
- ;; number of inserted rows is correct - if not, throw an exception and we'll roll back.
- (throw (ex-info (tru "Error inserting Permissions Group Membership") {})))
- (when (seq new-admin-ids)
- (permissions.db/update-users! new-admin-ids {:is_superuser true}))
- (when (seq new-data-analyst-ids)
- (permissions.db/update-users! new-data-analyst-ids {:is_data_analyst true}))
- ;; Publish events for each new membership
- (doseq [[[user-id group-id] is-group-manager?] user-id-group-id->is-group-manager?]
- (events/publish-event! :event/group-membership-create
- {:user-id api/*current-user-id*
- :object (t2/instance :model/PermissionsGroupMembership
- {:user_id user-id
- :group_id group-id
- :is_group_manager is-group-manager?})}))))))
+ (when (seq user-id-group-id->is-group-manager?)
+ (t2/with-transaction [_conn]
+ (when (< (permissions.db/insert-group-memberships-from-mapping! user-id-group-id->is-group-manager?)
+ (count user-id-group-id->is-group-manager?))
+ ;; Theoretically, there could be a race condition in the above check: a user or group may be changed to a tenant
+ ;; user/group or vice versa AFTER we check (above) but BEFORE the insert (below). So just make sure that the
+ ;; number of inserted rows is correct - if not, throw an exception and we'll roll back.
+ (throw (ex-info (tru "Error inserting Permissions Group Membership") {})))
+ (when (seq new-admin-ids)
+ (permissions.db/update-users! new-admin-ids {:is_superuser true}))
+ (when (seq new-data-analyst-ids)
+ (permissions.db/update-users! new-data-analyst-ids {:is_data_analyst true}))
+ ;; Publish events for each new membership
+ (doseq [[[user-id group-id] is-group-manager?] user-id-group-id->is-group-manager?]
+ (events/publish-event! :event/group-membership-create
+ {:user-id api/*current-user-id*
+ :object (t2/instance :model/PermissionsGroupMembership
+ {:user_id user-id
+ :group_id group-id
+ :is_group_manager is-group-manager?})})))))))
(defn add-user-to-groups!
"Add a user to multiple groups"
diff --git a/src/metabase/permissions_rest/api.clj b/src/metabase/permissions_rest/api.clj
index fb924c0bf5c..0be7199c8ed 100644
--- a/src/metabase/permissions_rest/api.clj
+++ b/src/metabase/permissions_rest/api.clj
@@ -195,8 +195,8 @@
"IDs of the permission groups holding a stored read (or read-write) grant on the collection of a shareable item (a
`dashboard` or a `question`). The \"invite someone to view\" group picker lists all groups and uses these ids to mark
the ones whose members can already see the item; the Administrators group has implicit access to everything and is
- never included. The ids are otherwise unfiltered; system-managed groups like Data Analysts appear when they hold a
- grant, and clients intersect the ids with the groups they display. Superuser-only, like the invite action itself."
+ never included. The Data Analysts group is omitted unless `advanced-permissions` is enabled. The ids are otherwise
+ unfiltered. Superuser-only, like the invite action itself."
[_route-params
{:keys [id] item-type :type} :- [:map {:closed true}
[:type [:enum "dashboard" "question"]]
@@ -206,7 +206,9 @@
"dashboard" :model/Dashboard
"question" :model/Card)
item (api/read-check model id)]
- (vec (sort (perms/collection-read-access-group-ids (:collection_id item))))))
+ (vec (sort (cond-> (perms/collection-read-access-group-ids (:collection_id item))
+ (not (premium-features/enable-advanced-permissions?))
+ (disj (u/the-id (perms/data-analyst-group))))))))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
@@ -288,8 +290,6 @@
(permissions-rest.db/group-memberships
{:manager-user-id (when (and (not api/*is-superuser?*) api/*is-group-manager?*)
api/*current-user-id*)
- :excluded-group-id (when-not (premium-features/enable-advanced-permissions?)
- (u/the-id (perms/data-analyst-group)))
:exclude-tenant-groups? (not (setting/get :use-tenants))})))
(defn- check-data-app-group-feature!
diff --git a/src/metabase/permissions_rest/db.clj b/src/metabase/permissions_rest/db.clj
index ea10fa89ed5..2e79177dc6b 100644
--- a/src/metabase/permissions_rest/db.clj
+++ b/src/metabase/permissions_rest/db.clj
@@ -18,14 +18,27 @@
[:= :user_id manager-user-id]
[:= :is_group_manager true]]}]))
+(defn- data-analyst-visibility-clause
+ "Clause over a `permissions_group` query keeping every group except the data-analyst magic group, unless it has at least one active member."
+ []
+ [:or
+ [:= nil :magic_group_type]
+ [:not= "data-analyst" :magic_group_type]
+ [:exists ^:allow-subquery {:select [1]
+ :from [[:permissions_group_membership :pgm]]
+ :join [[:core_user :member] [:= :member.id :pgm.user_id]]
+ :where [:and
+ [:= :pgm.group_id :permissions_group.id]
+ [:= :member.is_active true]]}]])
+
(mu/defn permissions-groups
"Up to `limit` PermissionsGroups starting at `offset` (both optional), ordered by lower-cased name.
`tenancy` (\"external\"/\"internal\"/nil) narrows to tenant/non-tenant groups (nil returns both); when
`tenancy` is \"external\" and `tenants-enabled?` is false, no groups are returned. `manager-user-id`, when given,
restricts to the groups that User manages. `advanced-permissions-enabled?` false excludes the data-analyst magic
- group. `tenants-enabled?` false also excludes tenant groups outright, independent of `tenancy`.
- `exclude-data-app-groups?` excludes groups that a data app manages."
+ group unless it has at least one active member. `tenants-enabled?` false also excludes tenant groups outright,
+ independent of `tenancy`. `exclude-data-app-groups?` excludes groups that a data app manages."
[limit :- [:maybe ms/PositiveInt]
offset :- [:maybe ms/IntGreaterThanOrEqualToZero]
{:keys [tenancy manager-user-id tenants-enabled? advanced-permissions-enabled?
@@ -40,8 +53,7 @@
(managed-groups-clause :id manager-user-id)
(when exclude-data-app-groups? [:not :is_data_app_group])
(when-not tenants-enabled? [:not :is_tenant_group])
- (when-not advanced-permissions-enabled?
- [:or [:= nil :magic_group_type] [:not= "data-analyst" :magic_group_type]])]
+ (when-not advanced-permissions-enabled? (data-analyst-visibility-clause))]
where (case tenancy
"external" (if tenants-enabled?
[:and base-where [:= :is_tenant_group true]]
@@ -87,18 +99,15 @@
(mu/defn group-memberships
"The membership id, group id, user id, and group manager flag of every PermissionsGroupMembership, optionally
- restricted to the groups `manager-user-id` manages, excluding `excluded-group-id`, and excluding tenant groups
- when `exclude-tenant-groups?`."
- [{:keys [manager-user-id excluded-group-id exclude-tenant-groups?]}
+ restricted to the groups `manager-user-id` manages, and excluding tenant groups when `exclude-tenant-groups?`."
+ [{:keys [manager-user-id exclude-tenant-groups?]}
:- [:map {:closed true}
[:manager-user-id {:optional true} [:maybe ::lib.schema.id/user]]
- [:excluded-group-id {:optional true} [:maybe ms/PositiveInt]]
[:exclude-tenant-groups? {:optional true} :boolean]]]
(t2/select [:model/PermissionsGroupMembership [:id :membership_id] :group_id :user_id :is_group_manager]
{:where (into [:and]
(keep identity)
[(managed-groups-clause :group_id manager-user-id)
- (when excluded-group-id [:not= :group_id excluded-group-id])
(when exclude-tenant-groups?
[:not-in :group_id ^:allow-subquery {:select [:id]
:from [:permissions_group]
diff --git a/src/metabase/premium_features/token_check.clj b/src/metabase/premium_features/token_check.clj
index 75b366aa576..55f6df4dad3 100644
--- a/src/metabase/premium_features/token_check.clj
+++ b/src/metabase/premium_features/token_check.clj
@@ -192,6 +192,16 @@
[:meters {:optional true} Meters]
[:quotas {:optional true} [:sequential Quota]]])
+(def ^:private MinimalTokenStatus
+ "The least a decoded token-status body must carry before we treat it as the store's answer and cache it.
+
+ This is deliberately not [[TokenStatus]]: we want to be relatively loose with this schema so that we don't
+ risk marking responses as invalid even if they're usable."
+ [:map {:closed false}
+ [:valid :boolean]
+ [:status [:string {:min 1}]]
+ [:features {:optional true} [:maybe [:sequential :string]]]])
+
(defn- http-fetch
[base-url token site-uuid]
(some-> (token-status-url token base-url)
@@ -202,22 +212,39 @@
:socket-timeout 5000 ;; in milliseconds
:connection-timeout 2000}))) ;; in milliseconds
+(defn- canonical
+ "Mark a decoded token status as canonical: an authoritative answer from the store, one we may cache and hold
+ on to, including a 4xx that definitively rejects the token (expired, does not exist). Anything transient
+ (network error, 5xx, missing or unparseable body) must throw instead so cached state is left alone."
+ [decoded]
+ (assoc decoded :canonical? true))
+
+(defn- parse-status-body
+ "Decode a token-status response body, returning the status map, or nil when the body is missing,
+ unparseable (e.g. a proxy's HTML error page), or does not satisfy [[MinimalTokenStatus]]."
+ [body]
+ (let [decoded (try
+ (some-> body json/decode+kw)
+ (catch Exception _e nil))]
+ (when (mr/validate MinimalTokenStatus decoded)
+ decoded)))
+
(defn- fetch-token-and-parse-body
[token base-url site-uuid]
(log/infof "Checking with the MetaStore to see whether token '%s' is valid..." (u.str/mask token))
(let [{:keys [body status] :as resp} (http-fetch base-url token site-uuid)]
- (cond
- (http/success? resp) (do (analytics/inc! :metabase-token-check/attempt {:status :success})
- (some-> body json/decode+kw (assoc :canonical? true)))
- (<= 400 status 499) (or (some-> body json/decode+kw (assoc :canonical? true))
- {:valid false
- :canonical? false
- :status "Unable to validate token"
- :error-details "Token validation provided no response"})
+ (if (or (http/success? resp) (<= 400 status 499))
+ (do (when (http/success? resp)
+ (analytics/inc! :metabase-token-check/attempt {:status :success}))
+ (or (some-> (parse-status-body body) canonical)
+ ;; a 2xx/4xx whose body is missing or undecodable (a proxy's or WAF's error page, a stripped
+ ;; body) carries no verdict from the store, so it is transient like a network error: not cached,
+ ;; retried under the breaker, and the previous canonical status stands meanwhile
+ (throw (ex-info "Token validation provided no response." {:status status}))))
;; exceptions are not cached.
- :else (do (analytics/inc! :metabase-token-check/attempt {:status :failure})
- (throw (ex-info "An unknown error occurred when validating token." {:status status
- :body body}))))))
+ (do (analytics/inc! :metabase-token-check/attempt {:status :failure})
+ (throw (ex-info "An unknown error occurred when validating token." {:status status
+ :body body}))))))
(defn- metering-url
[token base-url]
@@ -290,15 +317,15 @@
(mr/validate [:re AirgapToken] token)
(do
(log/infof "Checking airgapped token '%s'..." (u.str/mask token))
- (assoc (decode-airgap-token token) :canonical? true))
+ (canonical (decode-airgap-token token)))
:else
(do
(log/error (u/format-color 'red "Invalid token format!"))
- {:valid false
- :canonical? true
- :status "invalid"
- :error-details (trs "Token should be a valid 64 hexadecimal character token or an airgap token.")})))
+ (canonical
+ {:valid false
+ :status "invalid"
+ :error-details (trs "Token should be a valid 64 hexadecimal character token or an airgap token.")}))))
#_{:clj-kondo/ignore [:metabase/discourage-dynamic-vars]}
(def ^:dynamic *token-check-happening* "Var to prevent recursive calls to `fetch-token-status`" false)
@@ -343,10 +370,13 @@
(catch dev.failsafe.CircuitBreakerOpenException _e
(throw (ex-info (tru "Token validation is currently unavailable.")
{:cause :token-check/circuit-breaker})))
+ ;; a timed-out execution has no cause to unwrap — translate it ourselves
+ (catch dev.failsafe.TimeoutExceededException _e
+ (throw (ex-info (tru "Token validation timed out.") {})))
;; other exceptions are wrapped by Diehard in a FailsafeException. Unwrap them before
;; rethrowing.
(catch dev.failsafe.FailsafeException e
- (throw (.getCause e)))))))
+ (throw (or (.getCause e) e)))))))
(-clear-cache! [_]
(-clear-cache! token-checker)))))
@@ -434,15 +464,20 @@
(let [local-cache (or local-cache (atom {}))
refresh-in-progress? (atom false)]
(letfn [(do-refresh! [token token-hash]
- (let [result (-check-token token-checker token)
- result-hash (hash-token-status result)
- now (t/instant)]
- (write-cache-to-db! token-hash result-hash)
- (update-locked-meters! result)
- (swap! local-cache assoc token-hash {:result result
- :result-hash result-hash
- :updated-at now})
- result))]
+ (let [result (-check-token token-checker token)]
+ ;; a non-authoritative return here is an internal bug: every legitimate inner path
+ ;; returns a canonical map or throws
+ (when-not (:canonical? result)
+ (throw (ex-info "Refusing to cache a non-authoritative token status"
+ {:status (:status result)})))
+ (let [result-hash (hash-token-status result)
+ now (t/instant)]
+ (write-cache-to-db! token-hash result-hash)
+ (update-locked-meters! result)
+ (swap! local-cache assoc token-hash {:result result
+ :result-hash result-hash
+ :updated-at now})
+ result)))]
(reify TokenChecker
(-check-token [_ token]
(if-not (app-db/db-is-set-up?)
@@ -542,16 +577,21 @@
:hard-ttl hard-ttl
:local-cache db-hash-local-cache})
- local-ttl
- (local-cached-token-checker {:local-ttl local-ttl})
-
+ ;; inside the memoize so failure maps are memoized for local-ttl (negative cache; core.memoize
+ ;; single-flights values but not thrown exceptions), outside db-hash so they never reach the
+ ;; durable cache
:always
- (error-catching-token-checker)))
+ (error-catching-token-checker)
+
+ local-ttl
+ (local-cached-token-checker {:local-ttl local-ttl})))
(def token-checker
"The token checker. Combines http/airgapping validation, circuit breaking, DB-hash-aware caching, and error handling."
(make-checker {:base store-and-airgap-token-checker
- :circuit-breaker {:failure-threshold-ratio-in-period [10 10 (u/seconds->ms 60)]
+ ;; the window must fit 10 worst-case failures (10s timeout + 5s negative cache
+ ;; each ≈ 135s) or slow failures can never open the breaker
+ :circuit-breaker {:failure-threshold-ratio-in-period [10 10 (u/seconds->ms 180)]
:delay-ms (u/seconds->ms 30)
:success-threshold 1
:on-open (fn [_] (log/info "Engaging circuit breaker in token check"))
@@ -587,6 +627,9 @@
(mr/validate [:re AirgapToken] new-value))
(throw (ex-info (tru "Token format is invalid.")
{:status-code 400, :error-details "Token should be 64 hexadecimal characters."})))
+ ;; validate against the store, not a cached verdict — the token may be the same string with a
+ ;; renewed subscription behind it
+ (clear-cache!)
(let [decoded (check-token new-value)]
(when-not (:valid decoded)
(throw (ex-info (:status decoded)
@@ -622,9 +665,12 @@
"Get the features associated with the system's premium features token."
[]
(try
- (or (some-> (premium-features.settings/premium-embedding-token)
- (check-token)
- :features set)
+ ;; an invalid token (e.g. expired) confers no features, even if the token-status response still
+ ;; lists them (EMB-2341)
+ (or (let [{:keys [valid features]} (some-> (premium-features.settings/premium-embedding-token)
+ (check-token))]
+ (when valid
+ (set features)))
#{})
(catch Throwable e
(when (:pass-thru (ex-data e))
@@ -681,7 +727,8 @@
(if-let [token (premium-features.settings/premium-embedding-token)]
(let [result (check-token token)]
(when (:canonical? result)
- (boolean (contains? (set (:features result)) (name feature)))))
+ (boolean (and (:valid result)
+ (contains? (set (:features result)) (name feature))))))
false))
(defn ee-feature-error
diff --git a/src/metabase/segments/models/segment.clj b/src/metabase/segments/models/segment.clj
index ee9d1aa5261..e79c3acb1bb 100644
--- a/src/metabase/segments/models/segment.clj
+++ b/src/metabase/segments/models/segment.clj
@@ -94,7 +94,7 @@
(let [table (or (:table instance)
(segments.db/table (:table_id instance)))]
(and (or (mi/superuser?)
- (and api/*is-data-analyst?*
+ (and (api/entitled-data-analyst?)
(perms/user-has-permission-for-table?
api/*current-user-id*
:perms/view-data
@@ -114,7 +114,7 @@
(let [table (or (:table instance)
(segments.db/table (:table_id instance)))]
(and (or (mi/superuser?)
- (and api/*is-data-analyst?*
+ (and (api/entitled-data-analyst?)
(perms/user-has-permission-for-table?
api/*current-user-id*
:perms/view-data
diff --git a/src/metabase/transforms/models/transform.clj b/src/metabase/transforms/models/transform.clj
index 8bbc2946cfa..5ad5fc3b624 100644
--- a/src/metabase/transforms/models/transform.clj
+++ b/src/metabase/transforms/models/transform.clj
@@ -40,7 +40,7 @@
[instance & args]
(and (transforms.u/check-feature-enabled instance)
(or api/*is-superuser?*
- (and (api/is-data-analyst?)
+ (and (api/entitled-data-analyst?)
(apply transforms.u/source-tables-readable? instance args)))))
(defn- native-transform-write-allowed?
@@ -93,12 +93,12 @@
[_model instance]
;; Inline can-write? logic since instance is a plain map without model metadata.
;; can-write? requires: can-read?, has-db-transforms-permission?, and transforms-editable?
- ;; can-read? requires: is-superuser? OR (is-data-analyst? AND source-tables-readable?)
+ ;; can-read? requires: is-superuser? OR (entitled-data-analyst? AND source-tables-readable?)
(and (remote-sync/transforms-editable?)
(transforms.u/check-feature-enabled instance)
(or api/*is-superuser?*
(let [source-db-id (or (:source_database_id instance) (transforms-base.i/source-db-id instance))]
- (and api/*is-data-analyst?*
+ (and (api/entitled-data-analyst?)
(transforms.u/source-tables-readable? instance)
(transform-database-permissions? instance)
(native-transform-write-allowed? instance source-db-id))))))
diff --git a/src/metabase/transforms/models/transform_job.clj b/src/metabase/transforms/models/transform_job.clj
index e8a83a7c8ec..d559de26d15 100644
--- a/src/metabase/transforms/models/transform_job.clj
+++ b/src/metabase/transforms/models/transform_job.clj
@@ -28,14 +28,14 @@
(defmethod mi/can-read? :model/TransformJob
([_instance]
- (api/is-data-analyst?))
+ (api/entitled-data-analyst?))
([_model _pk]
- (api/is-data-analyst?)))
+ (api/entitled-data-analyst?)))
(defmethod mi/can-write? :model/TransformJob
([instance]
(or api/*is-superuser?*
- (and api/*is-data-analyst?*
+ (and (api/entitled-data-analyst?)
(let [transforms (or (:transforms instance)
(when-let [tag-ids (seq (:tag_ids instance))]
(transform/transforms-with-tags tag-ids)))]
@@ -49,7 +49,7 @@
(defmethod mi/can-create? :model/TransformJob
[_model instance]
(or api/*is-superuser?*
- (and api/*is-data-analyst?*
+ (and (api/entitled-data-analyst?)
;; Support batch hydration: check pre-hydrated :transforms first,
;; then fall back to looking up transforms from :tag_ids
(let [transforms (or (:transforms instance)
diff --git a/src/metabase/transforms/models/transform_tag.clj b/src/metabase/transforms/models/transform_tag.clj
index 4764e11a05c..e60ea7f0134 100644
--- a/src/metabase/transforms/models/transform_tag.clj
+++ b/src/metabase/transforms/models/transform_tag.clj
@@ -20,14 +20,14 @@
(defmethod mi/can-read? :model/TransformTag
([_instance]
- (api/is-data-analyst?))
+ (api/entitled-data-analyst?))
([_model _pk]
- (api/is-data-analyst?)))
+ (api/entitled-data-analyst?)))
(defmethod mi/can-write? :model/TransformTag
([instance]
(or api/*is-superuser?*
- (and api/*is-data-analyst?*
+ (and (api/entitled-data-analyst?)
(let [transforms (transform/transforms-with-tags [(:id instance)])]
(every? mi/can-write? transforms)))))
([_model pk]
@@ -36,7 +36,7 @@
(defmethod mi/can-create? :model/TransformTag
[_model _instance]
- (api/is-data-analyst?))
+ (api/entitled-data-analyst?))
(defn schedules-for-transforms
"Map each id in `transform-ids` to the cron schedules of the active jobs that run it via shared tags.
diff --git a/src/metabase/transforms/util.clj b/src/metabase/transforms/util.clj
index 0925f6589b4..043e56331c4 100644
--- a/src/metabase/transforms/util.clj
+++ b/src/metabase/transforms/util.clj
@@ -48,7 +48,7 @@
(defn enabled-source-types-for-user
"Returns set of enabled source types for WHERE clause filtering."
[]
- (when (api/is-data-analyst?)
+ (when (api/entitled-data-analyst?)
(transforms.gating/enabled-source-types)))
(defn- source-query-permissions-ok?
diff --git a/src/metabase/transforms_rest/api/transform.clj b/src/metabase/transforms_rest/api/transform.clj
index c70de0c7c9a..45ebaf4932c 100644
--- a/src/metabase/transforms_rest/api/transform.clj
+++ b/src/metabase/transforms_rest/api/transform.clj
@@ -225,7 +225,7 @@
[:end-time {:optional true} [:maybe ms/NonBlankString]]
[:run-methods {:optional true} [:maybe (ms/QueryVectorOf [:enum "manual" "cron"])]]
[:user-id {:optional true} [:maybe ms/PositiveInt]]]]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(-> (transforms.core/paged-runs (assoc query-params
:offset (request/offset)
:limit (request/limit)))
@@ -274,7 +274,7 @@
[:transform-ids {:optional true} [:maybe (ms/QueryVectorOf ms/PositiveInt)]]
[:sort-column {:optional true} [:maybe [:enum "start_time" "end_time"]]]
[:sort-direction {:optional true} [:maybe [:enum "asc" "desc"]]]]]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(-> (transforms.core/paged-run-summaries (assoc query-params
:types (map keyword (:types query-params))
:offset (request/offset)
@@ -285,7 +285,7 @@
"Get a transform run by ID."
[{:keys [run-id]} :- [:map {:closed true}
[:run-id ms/PositiveInt]]]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(-> (api/read-check :model/TransformRun run-id)
(t2/hydrate [:transform :collection :transform_tag_ids])
transforms-base.u/present-run))
diff --git a/src/metabase/transforms_rest/api/transform_dag_run.clj b/src/metabase/transforms_rest/api/transform_dag_run.clj
index 42051207f2f..70c03bc8b4f 100644
--- a/src/metabase/transforms_rest/api/transform_dag_run.clj
+++ b/src/metabase/transforms_rest/api/transform_dag_run.clj
@@ -26,7 +26,7 @@
[check! {:keys [source_transform_id] :as _dag-run}]
(if source_transform_id
(check! :model/Transform source_transform_id)
- (api/check-data-analyst)))
+ (api/check-data-studio-access)))
(api.macros/defendpoint :get "/:run-id/transform-runs" :- [:sequential transforms-rest.api.u/MemberTransformRunResponse]
"Get the transform runs that made up a specific DAG run."
diff --git a/src/metabase/transforms_rest/api/transform_job.clj b/src/metabase/transforms_rest/api/transform_job.clj
index b2e997718fc..a547d96d3e5 100644
--- a/src/metabase/transforms_rest/api/transform_job.clj
+++ b/src/metabase/transforms_rest/api/transform_job.clj
@@ -277,7 +277,7 @@
[:last-run-statuses {:optional true} [:maybe (ms/QueryVectorOf [:enum "started" "succeeded" "failed" "timeout"])]]
[:tag-ids {:optional true} [:maybe (ms/QueryVectorOf ms/IntGreaterThanOrEqualToZero)]]]]
(log/info "Getting all transform jobs")
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(let [jobs (transforms-rest.db/jobs)]
(into []
(comp (map add-next-run)
diff --git a/src/metabase/transforms_rest/api/transform_tag.clj b/src/metabase/transforms_rest/api/transform_tag.clj
index a8bc992d851..defeaf68f9d 100644
--- a/src/metabase/transforms_rest/api/transform_tag.clj
+++ b/src/metabase/transforms_rest/api/transform_tag.clj
@@ -63,7 +63,7 @@
[_route-params
_query-params]
(log/info "Getting all transform tags")
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(t2/hydrate (transforms-rest.db/tags) :can_run))
(def ^{:arglists '([request respond raise])} routes
diff --git a/src/metabase/users/db.clj b/src/metabase/users/db.clj
index 33599b9b80e..a3afa960b0c 100644
--- a/src/metabase/users/db.clj
+++ b/src/metabase/users/db.clj
@@ -7,6 +7,7 @@
[metabase.api.common :as api]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.permissions.core :as perms]
+ [metabase.premium-features.core :as premium-features]
[metabase.users.schema :as users.schema]
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
@@ -49,6 +50,14 @@
[:= :p.perm_type "perms/manage-table-metadata"]
[:= :p.perm_value "yes"]]}])
+(defn- data-studio-access-clauses
+ "The disjuncts, any one of which grants a user access to Data Studio."
+ []
+ (cond-> [:core_user.is_superuser
+ (table-metadata-perms-exist-clause)]
+ ;; Data Studio entry pauses on downgrade, so membership only counts while the feature is available
+ (premium-features/enable-advanced-permissions?) (conj :core_user.is_data_analyst)))
+
(defn- tenant-clause
"Honeysql clause restricting `:tenant_id`: `tenant-filter` is a tenant id to restrict to, `:all` for no
restriction, `:external` for any non-nil tenant, or nil for no tenant (internal users)."
@@ -82,15 +91,10 @@
(some? is-data-analyst?) (sql.helpers/where (if is-data-analyst?
:core_user.is_data_analyst
[:not :core_user.is_data_analyst]))
- (some? can-access-data-studio?) (sql.helpers/where (if can-access-data-studio?
- [:or
- :core_user.is_data_analyst
- :core_user.is_superuser
- (table-metadata-perms-exist-clause)]
- [:and
- [:not :core_user.is_data_analyst]
- [:not :core_user.is_superuser]
- [:not (table-metadata-perms-exist-clause)]]))
+ (some? can-access-data-studio?) (sql.helpers/where (let [clauses (data-studio-access-clauses)]
+ (if can-access-data-studio?
+ (into [:or] clauses)
+ (into [:and] (map #(vector :not %)) clauses))))
(some? group-ids) (sql.helpers/right-join
:permissions_group_membership
[:= :core_user.id :permissions_group_membership.user_id])
diff --git a/src/metabase/users_rest/api.clj b/src/metabase/users_rest/api.clj
index 2c7cf081c87..da2c3ce639f 100644
--- a/src/metabase/users_rest/api.clj
+++ b/src/metabase/users_rest/api.clj
@@ -190,7 +190,7 @@
[:enum :all :internal :external]]]
[:tenant_id {:optional true} [:maybe ms/PositiveInt]]]]
(or api/*is-superuser?*
- api/*is-data-analyst?*
+ (api/entitled-data-analyst?)
(if group_id
(perms/check-manager-of-group group_id)
(perms/check-group-manager)))
diff --git a/src/metabase/warehouse_schema_rest/api/table.clj b/src/metabase/warehouse_schema_rest/api/table.clj
index 0b9e1c23d79..a19df77e6b9 100644
--- a/src/metabase/warehouse_schema_rest/api/table.clj
+++ b/src/metabase/warehouse_schema_rest/api/table.clj
@@ -258,7 +258,7 @@
(defn- check-can-publish-tables-to-collection!
[tables collection-id]
- (api/check-data-analyst)
+ (api/check-data-studio-access)
(let [collection (api/check-404 (warehouse-schema-rest.db/collection collection-id))]
(api/check-400 (= (:type collection) collections/library-data-collection-type)
(tru "Tables can only be published to Library/Data collections."))
diff --git a/test/metabase/data_studio/api/table_test.clj b/test/metabase/data_studio/api/table_test.clj
index d5863fda8cc..ccf554224e6 100644
--- a/test/metabase/data_studio/api/table_test.clj
+++ b/test/metabase/data_studio/api/table_test.clj
@@ -63,7 +63,7 @@
(is (= :metabase-transform (user-table-fn :data_source table-id))))))
(deftest data-analyst-can-access-endpoints-test
- (testing "Data analysts (members of Data Analysts group) can access data studio endpoints"
+ (testing "Data analysts (members of Data Analysts group) can access data studio endpoints with advanced-permissions"
(let [data-analyst-group-id (:id (perms-group/data-analyst))]
(mt/with-temp [:model/User {analyst-id :id} {:first_name "Data"
:last_name "Analyst"
@@ -72,13 +72,20 @@
:model/PermissionsGroupMembership _ {:user_id analyst-id :group_id data-analyst-group-id}
:model/Database {db-id :id} {}
:model/Table {table-id :id} {:db_id db-id}]
- (testing "data analyst can edit tables"
- (is (= {} (mt/user-http-request analyst-id :post 200 "data-studio/table/edit"
- {:table_ids [table-id]
- :data_layer "final"}))))
- (testing "data analyst can get selection info"
- (is (map? (mt/user-http-request analyst-id :post 200 "data-studio/table/selection"
- {:table_ids [table-id]}))))))))
+ (mt/when-ee-evailable
+ (mt/with-premium-features #{:advanced-permissions}
+ (testing "data analyst can edit tables"
+ (is (= {} (mt/user-http-request analyst-id :post 200 "data-studio/table/edit"
+ {:table_ids [table-id]
+ :data_layer "final"}))))
+ (testing "data analyst can get selection info"
+ (is (map? (mt/user-http-request analyst-id :post 200 "data-studio/table/selection"
+ {:table_ids [table-id]}))))))
+ (mt/with-premium-features #{}
+ (testing "without advanced-permissions the data analyst is refused"
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request analyst-id :post 403 "data-studio/table/selection"
+ {:table_ids [table-id]})))))))))
(deftest regular-user-cannot-access-data-studio-test
(testing "Regular users (not in Data Analysts group) cannot access data studio endpoints"
diff --git a/test/metabase/glossary/api_test.clj b/test/metabase/glossary/api_test.clj
index 6f7cd21efaa..764fd4197c9 100644
--- a/test/metabase/glossary/api_test.clj
+++ b/test/metabase/glossary/api_test.clj
@@ -119,19 +119,27 @@
(is (false? (:can_write (mt/user-http-request :rasta :get 200 "glossary")))))
(testing "superusers see the glossary as writable"
(is (true? (:can_write (mt/user-http-request :crowberto :get 200 "glossary")))))))
- (testing "data analysts can mutate the glossary"
+ (testing "data analysts can mutate the glossary with advanced-permissions"
(mt/with-model-cleanup [:model/Glossary]
(mt/with-temp [:model/User {analyst-id :id} {:is_data_analyst true}
:model/PermissionsGroupMembership _ {:user_id analyst-id
:group_id (:id (perms-group/data-analyst))}]
- (is (true? (:can_write (mt/user-http-request analyst-id :get 200 "glossary"))))
- (let [{gid :id} (mt/user-http-request analyst-id :post 200 "glossary"
- {:term "Analyst" :definition "Created by analyst"})]
- (is (pos-int? gid))
- (is (=? {:definition "Edited by analyst"}
- (mt/user-http-request analyst-id :put 200 (str "glossary/" gid)
- {:term "Analyst" :definition "Edited by analyst"})))
- (is (nil? (mt/user-http-request analyst-id :delete 204 (str "glossary/" gid))))))))))
+ (mt/when-ee-evailable
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (true? (:can_write (mt/user-http-request analyst-id :get 200 "glossary"))))
+ (let [{gid :id} (mt/user-http-request analyst-id :post 200 "glossary"
+ {:term "Analyst" :definition "Created by analyst"})]
+ (is (pos-int? gid))
+ (is (=? {:definition "Edited by analyst"}
+ (mt/user-http-request analyst-id :put 200 (str "glossary/" gid)
+ {:term "Analyst" :definition "Edited by analyst"})))
+ (is (nil? (mt/user-http-request analyst-id :delete 204 (str "glossary/" gid)))))))
+ (mt/with-premium-features #{}
+ (testing "without advanced-permissions the data analyst is refused"
+ (is (false? (:can_write (mt/user-http-request analyst-id :get 200 "glossary"))))
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request analyst-id :post 403 "glossary"
+ {:term "Gated" :definition "Refused"}))))))))))
(deftest glossary-search-escapes-like-wildcards-test
(testing "GET /api/glossary?search= matches % and _ literally rather than as LIKE wildcards"
diff --git a/test/metabase/mcp/v2/tools/definitions_test.clj b/test/metabase/mcp/v2/tools/definitions_test.clj
index b049d78ce32..ab9b9ccc931 100644
--- a/test/metabase/mcp/v2/tools/definitions_test.clj
+++ b/test/metabase/mcp/v2/tools/definitions_test.clj
@@ -688,71 +688,86 @@
measure-args {:method "create" :table_id (mt/id :venues)
:name "definitions-test perms measure"
:definition (count-definition (mt/id :venues))}]
- (testing "GHY-4137: not admin-only — a data analyst with unrestricted view-data creates both"
- (is (=? {:id pos-int?} (tool-result (call-tool! analyst-id nil "segment_write" segment-args))))
- (is (=? {:id pos-int?} (tool-result (call-tool! analyst-id nil "measure_write" measure-args)))))
- (testing "GHY-4137: the same table grants without the data-analyst role are a permission denial, not a
- not-found"
- (is (= "\"You don't have permissions to do that.\""
- (tool-error (call-tool! plain-id nil "segment_write"
- (assoc segment-args :name "definitions-test denied segment")))))
- (is (= "\"You don't have permissions to do that.\""
- (tool-error (call-tool! plain-id nil "measure_write"
- (assoc measure-args :name "definitions-test denied measure"))))))
- (testing "GHY-4137: a data analyst without unrestricted view-data is denied — data analysts always read
- table metadata, so the table resolves and the domain permission check refuses the write"
- (is (= "\"You don't have permissions to do that.\""
- (tool-error (call-tool! blind-analyst-id nil "segment_write"
- (assoc segment-args :name "definitions-test blind segment")))))))))))
-
-;; not ^:parallel: with-no-data-perms-for-all-users! rewrites global data perms, and rows are
-;; created through the tool under with-model-cleanup
-(deftest table-move-permission-test
- (testing "GHY-4153/GHY-4154: `definition` carries the row to its own source table, so an update
- retargeting a table the caller cannot author on is denied — the write check ran against
- the old table, and only the domain create-check covers the new one"
- (mt/with-no-data-perms-for-all-users!
- (mt/with-model-cleanup [:model/Segment :model/Measure :model/Revision]
- (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
- :model/User {analyst-id :id} {:is_data_analyst true}]
- (perms/add-user-to-group! analyst-id group-id)
- ;; Block the database for this group first, then grant venues back. `with-no-data-perms-for-all-users!`
- ;; only resets the All Users group, so a freshly created group's default on `checkins` is ambient
- ;; state — and this test is only meaningful while the analyst cannot author there.
- (perms/set-database-permission! group-id (mt/id) :perms/view-data :blocked)
- (perms/set-table-permission! group-id (mt/id :venues) :perms/view-data :unrestricted)
- (perms/set-table-permission! group-id (mt/id :venues) :perms/create-queries :query-builder)
- (testing "precondition: venues is authorable and checkins is not"
- (is (true? (perms/user-has-permission-for-table?
- analyst-id :perms/view-data :unrestricted (mt/id) (mt/id :venues))))
- (is (false? (perms/user-has-permission-for-table?
- analyst-id :perms/view-data :unrestricted (mt/id) (mt/id :checkins)))))
- (let [segment (tool-result (call-tool! analyst-id nil "segment_write"
- {:method "create" :table_id (mt/id :venues)
- :name "definitions-test move segment"
- :definition mbql4-fragment}))
- measure (tool-result (call-tool! analyst-id nil "measure_write"
- {:method "create" :table_id (mt/id :venues)
- :name "definitions-test move measure"
- :definition (count-definition (mt/id :venues))}))]
- (testing "segment"
+ ;; a data analyst's write capabilities key off `advanced-permissions`, which no OSS build can have
+ (mt/when-ee-evailable
+ (mt/with-premium-features #{:advanced-permissions}
+ (testing "GHY-4137: not admin-only — a data analyst with unrestricted view-data creates both"
+ (is (=? {:id pos-int?} (tool-result (call-tool! analyst-id nil "segment_write" segment-args))))
+ (is (=? {:id pos-int?} (tool-result (call-tool! analyst-id nil "measure_write" measure-args)))))))
+ (mt/with-premium-features #{}
+ (testing "without advanced-permissions the same data analyst is refused"
(is (= "\"You don't have permissions to do that.\""
(tool-error (call-tool! analyst-id nil "segment_write"
- {:method "update" :id (:id segment)
- :definition (filter-definition :checkins :venue_id)
- :revision_message "move to checkins"}))))
- (testing "and the denial actually prevented the move"
- (is (= (mt/id :venues)
- (t2/select-one-fn :table_id :model/Segment :id (:id segment))))))
- (testing "measure"
+ (assoc segment-args :name "definitions-test gated segment")))))
(is (= "\"You don't have permissions to do that.\""
(tool-error (call-tool! analyst-id nil "measure_write"
- {:method "update" :id (:id measure)
- :definition (count-definition (mt/id :checkins))
- :revision_message "move to checkins"}))))
- (testing "and the denial actually prevented the move"
- (is (= (mt/id :venues)
- (t2/select-one-fn :table_id :model/Measure :id (:id measure))))))))))))
+ (assoc measure-args :name "definitions-test gated measure")))))))
+ (mt/with-premium-features #{:advanced-permissions}
+ (testing "GHY-4137: the same table grants without the data-analyst role are a permission denial, not a
+ not-found"
+ (is (= "\"You don't have permissions to do that.\""
+ (tool-error (call-tool! plain-id nil "segment_write"
+ (assoc segment-args :name "definitions-test denied segment")))))
+ (is (= "\"You don't have permissions to do that.\""
+ (tool-error (call-tool! plain-id nil "measure_write"
+ (assoc measure-args :name "definitions-test denied measure"))))))
+ (testing "GHY-4137: a data analyst without unrestricted view-data is denied — data analysts always read
+ table metadata, so the table resolves and the domain permission check refuses the write"
+ (is (= "\"You don't have permissions to do that.\""
+ (tool-error (call-tool! blind-analyst-id nil "segment_write"
+ (assoc segment-args :name "definitions-test blind segment"))))))))))))
+
+;; not ^:parallel: with-no-data-perms-for-all-users! rewrites global data perms, and rows are
+;; created through the tool under with-model-cleanup. EE-only: the analyst who authors the rows
+;; here only can with `advanced-permissions`, which no OSS build can have
+(mt/when-ee-evailable
+ (deftest table-move-permission-test
+ (testing "GHY-4153/GHY-4154: `definition` carries the row to its own source table, so an update
+ retargeting a table the caller cannot author on is denied — the write check ran against
+ the old table, and only the domain create-check covers the new one"
+ (mt/with-premium-features #{:advanced-permissions}
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-model-cleanup [:model/Segment :model/Measure :model/Revision]
+ (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
+ :model/User {analyst-id :id} {:is_data_analyst true}]
+ (perms/add-user-to-group! analyst-id group-id)
+ ;; Block the database for this group first, then grant venues back. `with-no-data-perms-for-all-users!`
+ ;; only resets the All Users group, so a freshly created group's default on `checkins` is ambient
+ ;; state — and this test is only meaningful while the analyst cannot author there.
+ (perms/set-database-permission! group-id (mt/id) :perms/view-data :blocked)
+ (perms/set-table-permission! group-id (mt/id :venues) :perms/view-data :unrestricted)
+ (perms/set-table-permission! group-id (mt/id :venues) :perms/create-queries :query-builder)
+ (testing "precondition: venues is authorable and checkins is not"
+ (is (true? (perms/user-has-permission-for-table?
+ analyst-id :perms/view-data :unrestricted (mt/id) (mt/id :venues))))
+ (is (false? (perms/user-has-permission-for-table?
+ analyst-id :perms/view-data :unrestricted (mt/id) (mt/id :checkins)))))
+ (let [segment (tool-result (call-tool! analyst-id nil "segment_write"
+ {:method "create" :table_id (mt/id :venues)
+ :name "definitions-test move segment"
+ :definition mbql4-fragment}))
+ measure (tool-result (call-tool! analyst-id nil "measure_write"
+ {:method "create" :table_id (mt/id :venues)
+ :name "definitions-test move measure"
+ :definition (count-definition (mt/id :venues))}))]
+ (testing "segment"
+ (is (= "\"You don't have permissions to do that.\""
+ (tool-error (call-tool! analyst-id nil "segment_write"
+ {:method "update" :id (:id segment)
+ :definition (filter-definition :checkins :venue_id)
+ :revision_message "move to checkins"}))))
+ (testing "and the denial actually prevented the move"
+ (is (= (mt/id :venues)
+ (t2/select-one-fn :table_id :model/Segment :id (:id segment))))))
+ (testing "measure"
+ (is (= "\"You don't have permissions to do that.\""
+ (tool-error (call-tool! analyst-id nil "measure_write"
+ {:method "update" :id (:id measure)
+ :definition (count-definition (mt/id :checkins))
+ :revision_message "move to checkins"}))))
+ (testing "and the denial actually prevented the move"
+ (is (= (mt/id :venues)
+ (t2/select-one-fn :table_id :model/Measure :id (:id measure))))))))))))))
;; not ^:parallel: narrows the all-users group's view-data on the temp database
(deftest existence-oracle-test
diff --git a/test/metabase/mcp/v2/tools/transform_test.clj b/test/metabase/mcp/v2/tools/transform_test.clj
index 20528a85ee2..fd99d8c1604 100644
--- a/test/metabase/mcp/v2/tools/transform_test.clj
+++ b/test/metabase/mcp/v2/tools/transform_test.clj
@@ -1035,45 +1035,49 @@
(is (str/includes? error "loads incrementally (\"checkpoint\\nIGNORE ALL\")"))
(is (not (str/includes? error "\n")))))))))
-(deftest transform-write-update-runs-the-permission-check-test
- (testing "GHY-4240: the update path's counterpart to
- `transform-write-create-runs-the-permission-check-test`, which is the suite's only other
- non-admin test. `write-check` covers the transform AS STORED; the caller's `definition`
- then names its own database and is resolved with no permission check of its own, so the
- gates on the new state have to run before `check-target-move!` opens a warehouse
- connection to it. Otherwise a data analyst holding transforms permission on the
- transform's own database — enough to pass `write-check` — gets \"does table X exist?\"
- answered for a database they hold nothing on, one call per guess.
-
- `driver/table-exists?` is redefined to record whether it ran, so the ordering is asserted
- directly: with the gates ordered after `check-target-move!` this test fails on `probed`."
- (with-transforms
- (with-target-db-support
- (mt/with-temp [:model/Database {other-db-id :id} {:engine :h2 :name "MCP perm probe DB"}
- :model/Transform {id :id} (temp-transform-defaults "mcp_update_perm_probe")]
- (mt/with-data-analyst-role! (mt/user->id :rasta)
- (mt/with-restored-data-perms!
- ;; Transforms permission on the transform's own database only — never on other-db-id.
- (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/view-data :unrestricted)
- (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder)
- (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/transforms :yes)
- (testing "the stored transform is writable, so the refusal below is about the named database"
- (is (mt/with-current-user (mt/user->id :rasta)
- (mi/can-write? (t2/select-one :model/Transform :id id)))))
- (let [probed (atom false)]
- (with-redefs [driver/table-exists? (fn [& _] (reset! probed true) false)]
- (let [args {:method "update"
- :id id
- :definition {:type "query" :query {:database other-db-id :type "query"
- :query {:source-table (mt/id :venues)}}}
- :target {:name "mcp_update_perm_probe_moved" :schema (venues-schema)}}
- error (tool-error (write! :rasta write-scopes args))]
- (is (false? @probed)
- "no warehouse connection is opened to a database the caller was never authorized against")
- (is (string? error))
- (is (= "mcp_update_perm_probe"
- (t2/select-one-fn #(get-in % [:target :name]) :model/Transform :id id))
- "and nothing is written when the check refuses")))))))))))
+(mt/when-ee-evailable
+ (deftest transform-write-update-runs-the-permission-check-test
+ (testing "GHY-4240: the update path's counterpart to
+ `transform-write-create-runs-the-permission-check-test`, which is the suite's only other
+ non-admin test. `write-check` covers the transform AS STORED; the caller's `definition`
+ then names its own database and is resolved with no permission check of its own, so the
+ gates on the new state have to run before `check-target-move!` opens a warehouse
+ connection to it. Otherwise a data analyst holding transforms permission on the
+ transform's own database — enough to pass `write-check` — gets \"does table X exist?\"
+ answered for a database they hold nothing on, one call per guess.
+
+ `driver/table-exists?` is redefined to record whether it ran, so the ordering is asserted
+ directly: with the gates ordered after `check-target-move!` this test fails on `probed`."
+ ;; `with-transforms` alone will not do: a data analyst's transforms access keys off `advanced-permissions`,
+ ;; which no OSS build can have
+ (mt/with-premium-features #{:transforms-basic :advanced-permissions}
+ (mt/with-temp-env-var-value! [mb-transforms-enabled true]
+ (with-target-db-support
+ (mt/with-temp [:model/Database {other-db-id :id} {:engine :h2 :name "MCP perm probe DB"}
+ :model/Transform {id :id} (temp-transform-defaults "mcp_update_perm_probe")]
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-restored-data-perms!
+ ;; Transforms permission on the transform's own database only — never on other-db-id.
+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/view-data :unrestricted)
+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder)
+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/transforms :yes)
+ (testing "the stored transform is writable, so the refusal below is about the named database"
+ (is (mt/with-current-user (mt/user->id :rasta)
+ (mi/can-write? (t2/select-one :model/Transform :id id)))))
+ (let [probed (atom false)]
+ (with-redefs [driver/table-exists? (fn [& _] (reset! probed true) false)]
+ (let [args {:method "update"
+ :id id
+ :definition {:type "query" :query {:database other-db-id :type "query"
+ :query {:source-table (mt/id :venues)}}}
+ :target {:name "mcp_update_perm_probe_moved" :schema (venues-schema)}}
+ error (tool-error (write! :rasta write-scopes args))]
+ (is (false? @probed)
+ "no warehouse connection is opened to a database the caller was never authorized against")
+ (is (string? error))
+ (is (= "mcp_update_perm_probe"
+ (t2/select-one-fn #(get-in % [:target :name]) :model/Transform :id id))
+ "and nothing is written when the check refuses")))))))))))))
(deftest transform-write-not-found-test
(testing "GHY-4240: an unresolvable id collapses to the shared not-found error"
diff --git a/test/metabase/measures/models/measure_test.clj b/test/metabase/measures/models/measure_test.clj
index a26891be8a8..a51f74aac79 100644
--- a/test/metabase/measures/models/measure_test.clj
+++ b/test/metabase/measures/models/measure_test.clj
@@ -191,7 +191,11 @@
:creator_id (mt/user->id :rasta)
:definition (measure-definition (lib/count))}]
(mt/with-test-user :crowberto
- (is (true? (mi/can-write? measure)))))))
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (true? (mi/can-write? measure))))
+ (testing "including without the advanced-permissions feature"
+ (mt/with-premium-features #{}
+ (is (true? (mi/can-write? measure)))))))))
(deftest can-write?-analyst-unrestricted-test
(testing "Data analysts with unrestricted view-data can write measures"
@@ -205,7 +209,12 @@
(perms/add-user-to-group! analyst-id group-id)
(data-perms/set-table-permission! group-id (mt/id :venues) :perms/view-data :unrestricted)
(session/with-current-user analyst-id
- (is (mi/can-write? measure)))))))
+ (mt/when-ee-evailable
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (mi/can-write? measure))))
+ (testing "but not once the advanced-permissions feature is gone"
+ (mt/with-premium-features #{}
+ (is (not (mi/can-write? measure))))))))))
(deftest can-write?-analyst-restricted-test
(testing "Data analysts without unrestricted view-data cannot write measures"
@@ -305,9 +314,16 @@
(perms/add-user-to-group! analyst-id group-id)
(data-perms/set-table-permission! group-id (mt/id :venues) :perms/view-data :unrestricted)
(session/with-current-user analyst-id
- (is (true? (mi/can-create? :model/Measure {:name "Test Measure"
- :table_id (mt/id :venues)
- :definition (measure-definition (lib/count))}))))))))
+ (mt/when-ee-evailable
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (true? (mi/can-create? :model/Measure {:name "Test Measure"
+ :table_id (mt/id :venues)
+ :definition (measure-definition (lib/count))})))))
+ (testing "but not once the advanced-permissions feature is gone"
+ (mt/with-premium-features #{}
+ (is (false? (mi/can-create? :model/Measure {:name "Test Measure"
+ :table_id (mt/id :venues)
+ :definition (measure-definition (lib/count))}))))))))))
(deftest can-create?-analyst-restricted-test
(testing "Data analysts without unrestricted view-data cannot create measures"
diff --git a/test/metabase/metabot/tools/resources_test.clj b/test/metabase/metabot/tools/resources_test.clj
index 2e28e0b4a8b..54c2eef5243 100644
--- a/test/metabase/metabot/tools/resources_test.clj
+++ b/test/metabase/metabot/tools/resources_test.clj
@@ -507,28 +507,30 @@
(is (=? {:resources [{:error string?}]}
(read-resource/read-resource {:uris ["metabase://transform/99999"]}))))))))
-(deftest read-transform-resource-source-permission-test
- (testing "transforms/get-transform refuses a transform whose stored query the user cannot run, even
- with query access to another table in its database, so the resource never reaches the source"
- (mt/with-premium-features #{:transforms-basic :hosting}
- (mt/with-temp [:model/Transform {transform-id :id}
- {:name "Orders Rollup"
- :source {:type "query"
- :query (lib/query (mt/metadata-provider)
- (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}}]
- (mt/with-data-analyst-role! (mt/user->id :rasta)
- (mt/with-no-data-perms-for-all-users!
- (perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/view-data :unrestricted)
- (perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/create-queries :query-builder)
- (mt/with-current-user (mt/user->id :rasta)
- (is (=? {:resources [{:error "You don't have permissions to do that."}]}
- (read-resource/read-resource {:uris [(str "metabase://transform/" transform-id)]}))))
- (testing "and the query renders once the source table is granted"
- (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/view-data :unrestricted)
- (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/create-queries :query-builder)
- (mt/with-current-user (mt/user->id :rasta)
- (let [result (read-resource/read-resource {:uris [(str "metabase://transform/" transform-id)]})]
- (is (some? (get-in result [:resources 0 :content :structured-output :source :query]))))))))))))
+;; EE-only: the analyst reading here only can with `advanced-permissions`, which no OSS build can have
+(mt/when-ee-evailable
+ (deftest read-transform-resource-source-permission-test
+ (testing "transforms/get-transform refuses a transform whose stored query the user cannot run, even
+ with query access to another table in its database, so the resource never reaches the source"
+ (mt/with-premium-features #{:transforms-basic :hosting :advanced-permissions}
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:name "Orders Rollup"
+ :source {:type "query"
+ :query (lib/query (mt/metadata-provider)
+ (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}}]
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-no-data-perms-for-all-users!
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/view-data :unrestricted)
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/create-queries :query-builder)
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (=? {:resources [{:error "You don't have permissions to do that."}]}
+ (read-resource/read-resource {:uris [(str "metabase://transform/" transform-id)]}))))
+ (testing "and the query renders once the source table is granted"
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/view-data :unrestricted)
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/create-queries :query-builder)
+ (mt/with-current-user (mt/user->id :rasta)
+ (let [result (read-resource/read-resource {:uris [(str "metabase://transform/" transform-id)]})]
+ (is (some? (get-in result [:resources 0 :content :structured-output :source :query])))))))))))))
(defn- read-title
"The chain-of-thought title `read-resource` derives from what it read."
diff --git a/test/metabase/metabot/tools/transforms_test.clj b/test/metabase/metabot/tools/transforms_test.clj
index 1c3c55dc44a..f7c256c14af 100644
--- a/test/metabase/metabot/tools/transforms_test.clj
+++ b/test/metabase/metabot/tools/transforms_test.clj
@@ -51,34 +51,40 @@
(defn- query-transform-details!
"`get_transform_details` output for a transform whose source is `query`, read as rasta with query
access to the whole database - so `transforms/get-transform` passes and whatever happens to the
- source is the export gate's doing."
+ source is the export gate's doing. A data analyst's transforms access keys off
+ `advanced-permissions`, so that is enabled too - and since no OSS build can have it, every
+ caller is wrapped in [[mt/when-ee-evailable]]."
[query]
- (mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
+ (mt/with-premium-features #{:transforms-basic :transforms-python :hosting :advanced-permissions}
(mt/with-temp [:model/Transform {transform-id :id}
{:name "Orders Rollup" :source {:type "query" :query query}}]
(mt/with-data-analyst-role! (mt/user->id :rasta)
(mt/with-current-user (mt/user->id :rasta)
(:output (agent-transforms/get-transform-details-tool {:transform_id transform-id})))))))
-(deftest get-transform-details-sandboxed-field-test
- (testing "a source query naming a field the user's sandbox hides renders without its query, since
- exporting it would resolve that field's id to a name"
- (let [query (-> (lib/query (mt/metadata-provider)
- (lib.metadata/table (mt/metadata-provider) (mt/id :venues)))
- (lib/filter (lib/> (lib.metadata/field (mt/metadata-provider) (mt/id :venues :price)) 1)))]
- (is (str/includes? (query-transform-details! query) "<query>"))
- (mt/with-dynamic-fn-redefs [metabot.perms/sandbox-restricted-fields (fn [_table-ids] {(mt/id :venues) #{}})]
- (is (not (str/includes? (query-transform-details! query) "<query>")))))))
+;; EE-only: the analyst reading here only can with `advanced-permissions`, which no OSS build can have
+(mt/when-ee-evailable
+ (deftest get-transform-details-sandboxed-field-test
+ (testing "a source query naming a field the user's sandbox hides renders without its query, since
+ exporting it would resolve that field's id to a name"
+ (let [query (-> (lib/query (mt/metadata-provider)
+ (lib.metadata/table (mt/metadata-provider) (mt/id :venues)))
+ (lib/filter (lib/> (lib.metadata/field (mt/metadata-provider) (mt/id :venues :price)) 1)))]
+ (is (str/includes? (query-transform-details! query) "<query>"))
+ (mt/with-dynamic-fn-redefs [metabot.perms/sandbox-restricted-fields (fn [_table-ids] {(mt/id :venues) #{}})]
+ (is (not (str/includes? (query-transform-details! query) "<query>"))))))))
-(deftest get-transform-details-unpermissionable-source-test
- (testing "a source query whose permissions cannot be calculated at all still renders, since that is
- not a refusal - it just renders unresolved, naming nothing the user may not see"
- (let [output (query-transform-details! {:database (mt/id)
- :type :query
- :query {:source-table "card__13371337"}})]
- (is (str/includes? output "<query>"))
- (is (str/includes? output ":source-card 13371337")
- "the card id is still a number, so nothing was resolved to a name"))))
+;; EE-only: the analyst reading here only can with `advanced-permissions`, which no OSS build can have
+(mt/when-ee-evailable
+ (deftest get-transform-details-unpermissionable-source-test
+ (testing "a source query whose permissions cannot be calculated at all still renders, since that is
+ not a refusal - it just renders unresolved, naming nothing the user may not see"
+ (let [output (query-transform-details! {:database (mt/id)
+ :type :query
+ :query {:source-table "card__13371337"}})]
+ (is (str/includes? output "<query>"))
+ (is (str/includes? output ":source-card 13371337")
+ "the card id is still a number, so nothing was resolved to a name")))))
(defn- rendered-source
"The source query [[query-export/transform-with-exportable-source]] leaves on a transform whose
@@ -112,42 +118,46 @@
:native {:query "SELECT 1"}}))
"SELECT 1"))))))
-(deftest get-transform-details-unpermissionable-native-source-test
- (testing "native SQL stays out when the check could not run, since its table and column names are
- already plain text and having no provider hides nothing"
- (let [output (query-transform-details!
- {:database (mt/id)
- :type :native
- :native {:query "SELECT * FROM {{snip}}"
- :template-tags {"snip" {:type :snippet
- :name "snip"
- :display-name "snip"
- :snippet-id Integer/MAX_VALUE}}}})]
- (is (not (str/includes? output "<query>")))
- (is (not (str/includes? output "SELECT"))))))
+;; EE-only: the analyst reading here only can with `advanced-permissions`, which no OSS build can have
+(mt/when-ee-evailable
+ (deftest get-transform-details-unpermissionable-native-source-test
+ (testing "native SQL stays out when the check could not run, since its table and column names are
+ already plain text and having no provider hides nothing"
+ (let [output (query-transform-details!
+ {:database (mt/id)
+ :type :native
+ :native {:query "SELECT * FROM {{snip}}"
+ :template-tags {"snip" {:type :snippet
+ :name "snip"
+ :display-name "snip"
+ :snippet-id Integer/MAX_VALUE}}}})]
+ (is (not (str/includes? output "<query>")))
+ (is (not (str/includes? output "SELECT")))))))
-(deftest get-transform-details-source-permission-test
- (testing "transforms/get-transform refuses a transform whose stored query the user cannot run, even
- with query access to another table in its database, so the tool never reaches the source"
- (mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
- (mt/with-temp [:model/Transform {transform-id :id}
- {:name "Orders Rollup"
- :source {:type "query"
- :query (lib/query (mt/metadata-provider)
- (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}}]
- (mt/with-data-analyst-role! (mt/user->id :rasta)
- (mt/with-no-data-perms-for-all-users!
- (perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/view-data :unrestricted)
- (perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/create-queries :query-builder)
- (mt/with-current-user (mt/user->id :rasta)
- (is (=? {:status-code 403 :output "You don't have permissions to do that."}
- (agent-transforms/get-transform-details-tool {:transform_id transform-id}))))
- (testing "and the query renders once the source table is granted"
- (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/view-data :unrestricted)
- (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/create-queries :query-builder)
- (mt/with-current-user (mt/user->id :rasta)
- (is (str/includes? (:output (agent-transforms/get-transform-details-tool {:transform_id transform-id}))
- "<query>"))))))))))
+;; EE-only: the analyst reading here only can with `advanced-permissions`, which no OSS build can have
+(mt/when-ee-evailable
+ (deftest get-transform-details-source-permission-test
+ (testing "transforms/get-transform refuses a transform whose stored query the user cannot run, even
+ with query access to another table in its database, so the tool never reaches the source"
+ (mt/with-premium-features #{:transforms-basic :transforms-python :hosting :advanced-permissions}
+ (mt/with-temp [:model/Transform {transform-id :id}
+ {:name "Orders Rollup"
+ :source {:type "query"
+ :query (lib/query (mt/metadata-provider)
+ (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}}]
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-no-data-perms-for-all-users!
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/view-data :unrestricted)
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :venues) :perms/create-queries :query-builder)
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (=? {:status-code 403 :output "You don't have permissions to do that."}
+ (agent-transforms/get-transform-details-tool {:transform_id transform-id}))))
+ (testing "and the query renders once the source table is granted"
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/view-data :unrestricted)
+ (perms/set-table-permission! (perms-group/all-users) (mt/id :orders) :perms/create-queries :query-builder)
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (str/includes? (:output (agent-transforms/get-transform-details-tool {:transform_id transform-id}))
+ "<query>")))))))))))
(deftest get-transform-details-python-source-test
(mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
@@ -162,23 +172,27 @@
(is (str/includes? output "<body>import pandas as pd</body>"))
(is (str/includes? output (str "<database>" (mt/id) "</database>"))))))))
-(deftest get-transform-details-source-card-permission-test
- (mt/with-premium-features #{:transforms-basic :transforms-python :hosting}
- (mt/with-non-admin-groups-no-root-collection-perms
- (mt/with-full-data-perms-for-all-users!
- (mt/with-data-analyst-role! (mt/user->id :rasta)
- (mt/with-temp [:model/Collection {collection-id :id} {}
- :model/Card {card-id :id}
- {:collection_id collection-id
- :database_id (mt/id)
- :dataset_query (lib/query (mt/metadata-provider)
- (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}
- :model/Transform {transform-id :id}
- {:name "Private source Card"
- :source {:type "query"
- :query (lib/query (mt/metadata-provider)
- (lib.metadata/card (mt/metadata-provider) card-id))}}]
- (mt/with-current-user (mt/user->id :rasta)
- (let [{:keys [output status-code]} (agent-transforms/get-transform-details-tool {:transform_id transform-id})]
- (is (= 403 status-code))
- (is (= "You don't have permissions to do that." output))))))))))
+;; EE-only: the analyst reading here only can with `advanced-permissions`, which no OSS build can have
+(mt/when-ee-evailable
+ (deftest get-transform-details-source-card-permission-test
+ ;; `advanced-permissions` is on so the refusal below is about the private source card, not the
+ ;; data analyst's missing entitlement
+ (mt/with-premium-features #{:transforms-basic :transforms-python :hosting :advanced-permissions}
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-full-data-perms-for-all-users!
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-temp [:model/Collection {collection-id :id} {}
+ :model/Card {card-id :id}
+ {:collection_id collection-id
+ :database_id (mt/id)
+ :dataset_query (lib/query (mt/metadata-provider)
+ (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}
+ :model/Transform {transform-id :id}
+ {:name "Private source Card"
+ :source {:type "query"
+ :query (lib/query (mt/metadata-provider)
+ (lib.metadata/card (mt/metadata-provider) card-id))}}]
+ (mt/with-current-user (mt/user->id :rasta)
+ (let [{:keys [output status-code]} (agent-transforms/get-transform-details-tool {:transform_id transform-id})]
+ (is (= 403 status-code))
+ (is (= "You don't have permissions to do that." output)))))))))))
diff --git a/test/metabase/permissions/models/permissions_group_membership_test.clj b/test/metabase/permissions/models/permissions_group_membership_test.clj
index e93595c5cdb..f5f0f142607 100644
--- a/test/metabase/permissions/models/permissions_group_membership_test.clj
+++ b/test/metabase/permissions/models/permissions_group_membership_test.clj
@@ -98,3 +98,102 @@
(is (thrown-with-msg? Exception #"Do not use `t2/delete!`"
(t2/delete! :model/PermissionsGroupMembership
:user_id user-id :group_id group-id))))))
+
+(defn- data-analyst-group-id []
+ (u/the-id (perms-group/data-analyst)))
+
+(defn- in-data-analyst-group? [user-id]
+ (t2/exists? :model/PermissionsGroupMembership :user_id user-id :group_id (data-analyst-group-id)))
+
+(defn- is-data-analyst-flag [user-id]
+ (t2/select-one-fn :is_data_analyst :model/User :id user-id))
+
+(deftest add-to-data-analyst-group-without-advanced-permissions-test
+ (testing "adding a user to the Data Analysts group is refused without the :advanced-permissions feature"
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (mt/with-premium-features #{}
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo #"Advanced Permissions"
+ (perms/add-user-to-group! user-id (data-analyst-group-id))))
+ (testing "no membership is created"
+ (is (not (in-data-analyst-group? user-id))))
+ (testing "the is_data_analyst flag is not set"
+ (is (not (is-data-analyst-flag user-id))))))))
+
+(mt/when-ee-evailable
+ (deftest existing-data-analysts-are-grandfathered-test
+ (testing "an existing member keeps their membership when the feature goes away"
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/add-user-to-group! user-id (data-analyst-group-id)))
+ (mt/with-premium-features #{}
+ (testing "nothing revokes the membership or the flag"
+ (is (in-data-analyst-group? user-id))
+ (is (true? (boolean (is-data-analyst-flag user-id)))))
+ (testing "but the group can only shrink from here"
+ (mt/with-temp [:model/User {other-user-id :id} {}]
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo #"Advanced Permissions"
+ (perms/add-user-to-group! other-user-id (data-analyst-group-id)))))))))))
+
+(mt/when-ee-evailable
+ (deftest add-to-data-analyst-group-with-advanced-permissions-test
+ (testing "adding a user to the Data Analysts group works with the :advanced-permissions feature"
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/add-user-to-group! user-id (data-analyst-group-id))
+ (is (in-data-analyst-group? user-id))
+ (testing "and mirrors the membership onto the is_data_analyst flag"
+ (is (true? (boolean (is-data-analyst-flag user-id))))))))))
+
+(mt/when-ee-evailable
+ (deftest remove-from-data-analyst-group-is-never-gated-test
+ (testing "removing a user from the Data Analysts group works without the :advanced-permissions feature"
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/add-user-to-group! user-id (data-analyst-group-id)))
+ (mt/with-premium-features #{}
+ (perms/remove-user-from-group! user-id (data-analyst-group-id))
+ (is (not (in-data-analyst-group? user-id)))
+ (testing "and unsets the is_data_analyst flag"
+ (is (not (is-data-analyst-flag user-id)))))))))
+
+(deftest add-to-other-groups-is-not-gated-test
+ (testing "the Data Analysts gate does not affect other groups"
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/PermissionsGroup {group-id :id} {:name "Test Group"}]
+ (mt/with-premium-features #{}
+ (perms/add-user-to-group! user-id group-id)
+ (is (t2/exists? :model/PermissionsGroupMembership :user_id user-id :group_id group-id))))))
+
+(deftest re-adding-an-existing-member-is-a-no-op-test
+ (testing "adding a user to a group they are already in changes nothing"
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/PermissionsGroup {group-id :id} {:name "Test Group"}]
+ (perms/add-user-to-group! user-id group-id)
+ (let [before (t2/select-one :model/PermissionsGroupMembership :user_id user-id :group_id group-id)]
+ (perms/add-user-to-group! user-id group-id)
+ (is (= before (t2/select-one :model/PermissionsGroupMembership :user_id user-id :group_id group-id)))))))
+
+(mt/when-ee-evailable
+ (deftest re-adding-an-existing-data-analyst-is-not-gated-test
+ (testing "re-asserting an existing Data Analyst's membership without the feature adds nothing, so nothing is refused"
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/add-user-to-group! user-id (data-analyst-group-id)))
+ (mt/with-premium-features #{}
+ (perms/add-user-to-group! user-id (data-analyst-group-id))
+ (is (in-data-analyst-group? user-id))
+ (is (true? (boolean (is-data-analyst-flag user-id)))))))))
+
+(deftest add-to-data-analyst-group-in-a-batch-is-gated-test
+ (testing "a batched add that includes the Data Analysts group is refused entirely"
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/PermissionsGroup {group-id :id} {:name "Test Group"}]
+ (mt/with-premium-features #{}
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo #"Advanced Permissions"
+ (perms/add-user-to-groups! user-id [group-id (data-analyst-group-id)])))
+ (is (not (in-data-analyst-group? user-id)))
+ (testing "and the rest of the batch is not applied either"
+ (is (not (t2/exists? :model/PermissionsGroupMembership :user_id user-id :group_id group-id))))))))
diff --git a/test/metabase/permissions_rest/api_test.clj b/test/metabase/permissions_rest/api_test.clj
index 3692bad324a..4849241b0a5 100644
--- a/test/metabase/permissions_rest/api_test.clj
+++ b/test/metabase/permissions_rest/api_test.clj
@@ -68,13 +68,141 @@
(is (= "You don't have permissions to do that."
(mt/user-http-request :rasta :get 403 "permissions/group"))))))
-(deftest no-data-analyst-groups-test
- (testing "GET /api/permissions/group"
- (testing "in OSS, the data analyst group is hidden"
- ;; note that this uses `config/ee-available?` instead of a feature to avoid hiding a group that may stil provide permissions!
- (when-not config/ee-available?
- (is (not (contains? (set (map :name (mt/user-http-request :crowberto :get 200 "permissions/group")))
- "Data Analysts")))))))
+(defn- data-analyst-group-id []
+ (u/the-id (perms-group/data-analyst)))
+
+(defn- add-to-data-analyst-group-with-feature!
+ "Add `user-id` to the Data Analysts group, temporarily enabling `advanced-permissions` to get past the addition gate."
+ [user-id]
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/add-user-to-group! user-id (data-analyst-group-id))))
+
+(defn- data-analyst-group-listed? []
+ (contains? (set (map :id (fetch-groups))) (data-analyst-group-id)))
+
+(mt/when-ee-evailable
+ (deftest data-analyst-group-listed-with-feature-test
+ (testing "GET /api/permissions/group lists the Data Analysts group with advanced-permissions, even when empty"
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (data-analyst-group-listed?))))))
+
+(deftest data-analyst-group-hidden-without-feature-when-empty-test
+ (testing "GET /api/permissions/group omits an empty Data Analysts group without advanced-permissions"
+ (mt/with-premium-features #{}
+ (is (not (data-analyst-group-listed?))))))
+
+(mt/when-ee-evailable
+ (deftest data-analyst-group-listed-without-feature-when-populated-test
+ (testing "GET /api/permissions/group lists a populated Data Analysts group without advanced-permissions"
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (add-to-data-analyst-group-with-feature! user-id)
+ (mt/with-premium-features #{}
+ (let [group (m/find-first #(= (data-analyst-group-id) (:id %)) (fetch-groups))]
+ (is (some? group))
+ (testing "with its member count"
+ (is (= 1 (:member_count group))))))))))
+
+(mt/when-ee-evailable
+ (deftest data-analyst-group-hidden-without-feature-after-last-member-removed-test
+ (testing "GET /api/permissions/group omits the Data Analysts group once its last member is removed"
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (add-to-data-analyst-group-with-feature! user-id)
+ (mt/with-premium-features #{}
+ (is (data-analyst-group-listed?))
+ (perms/remove-user-from-group! user-id (data-analyst-group-id))
+ (is (not (data-analyst-group-listed?))))))))
+
+(mt/when-ee-evailable
+ (deftest data-analyst-group-visibility-counts-active-members-only-test
+ (testing "GET /api/permissions/group counts only active members when deciding to list the Data Analysts group"
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (add-to-data-analyst-group-with-feature! user-id)
+ (mt/with-premium-features #{}
+ (is (data-analyst-group-listed?))
+ (testing "deactivating the only member hides the group"
+ (t2/update! :model/User user-id {:is_active false})
+ (is (not (data-analyst-group-listed?))))
+ (testing "reactivating them brings it back, the membership having survived"
+ (t2/update! :model/User user-id {:is_active true})
+ (is (data-analyst-group-listed?))
+ (is (t2/exists? :model/PermissionsGroupMembership
+ :user_id user-id :group_id (data-analyst-group-id)))))))))
+
+(mt/when-ee-evailable
+ (deftest data-analyst-group-membership-listing-test
+ (testing "GET /api/permissions/membership"
+ (testing "without advanced-permissions, memberships of a populated Data Analysts group are listed"
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (add-to-data-analyst-group-with-feature! user-id)
+ (mt/with-premium-features #{}
+ (let [memberships (mt/user-http-request :crowberto :get 200 "permissions/membership")]
+ (is (contains? (set (keep (fn [{:keys [user_id group_id]}]
+ (when (= user-id user_id) group_id))
+ (mapcat val memberships)))
+ (data-analyst-group-id))))))))))
+
+(deftest fetch-data-analyst-group-by-id-test
+ (testing "GET /api/permissions/group/:id returns the Data Analysts group regardless of feature or membership"
+ (doseq [features [#{:advanced-permissions} #{}]]
+ (testing (str "premium features: " features)
+ (testing "empty group"
+ (mt/with-premium-features features
+ (is (= (data-analyst-group-id)
+ (:id (mt/user-http-request :crowberto :get 200
+ (format "permissions/group/%d" (data-analyst-group-id))))))))
+ (mt/when-ee-evailable
+ (testing "populated group"
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (add-to-data-analyst-group-with-feature! user-id)
+ (mt/with-premium-features features
+ (let [group (mt/user-http-request :crowberto :get 200
+ (format "permissions/group/%d" (data-analyst-group-id)))]
+ (is (= (data-analyst-group-id) (:id group)))
+ (is (contains? (set (map :user_id (:members group))) user-id)))))))))))
+
+(deftest data-analyst-group-data-model-permission-is-locked-test
+ (testing "the Data Analysts group's data-model permission is rendered as enforced and cannot be edited"
+ (doseq [features [#{:advanced-permissions} #{}]]
+ (testing (str "premium features: " features)
+ (mt/with-premium-features features
+ (testing "GET /api/permissions/graph renders the locked value"
+ (is (= {:schemas "all"}
+ (get-in (mt/user-http-request :crowberto :get 200 "permissions/graph")
+ [:groups (data-analyst-group-id) (mt/id) :data-model]))))
+ (testing "PUT /api/permissions/graph refuses to change it"
+ (is (= "You cannot modify the data model permission for the 'Data Analysts' group."
+ (mt/user-http-request
+ :crowberto :put 400 "permissions/graph"
+ (assoc-in (data-perms.graph/api-graph)
+ [:groups (data-analyst-group-id) (mt/id) :data-model]
+ {:schemas :none}))))))))))
+
+(deftest data-analyst-group-cannot-be-renamed-or-deleted-test
+ (testing "the Data Analysts group is neither renameable nor deletable"
+ (let [error (format "You cannot edit or delete the '%s' permissions group!"
+ (t2/select-one-fn :name :model/PermissionsGroup :id (data-analyst-group-id)))
+ locked? (fn []
+ (is (= error
+ (mt/user-http-request :crowberto :put 400
+ (format "permissions/group/%d" (data-analyst-group-id))
+ {:name "Renamed Analysts"})))
+ (is (= error
+ (mt/user-http-request :crowberto :delete 400
+ (format "permissions/group/%d" (data-analyst-group-id)))))
+ (is (t2/exists? :model/PermissionsGroup
+ :id (data-analyst-group-id)
+ :magic_group_type perms-group/data-analyst-magic-group-type)))]
+ (doseq [features [#{:advanced-permissions} #{}]]
+ (testing (str "premium features: " features)
+ (testing "empty group"
+ (mt/with-premium-features features
+ (locked?)))
+ (mt/when-ee-evailable
+ (testing "populated group"
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (add-to-data-analyst-group-with-feature! user-id)
+ (mt/with-premium-features features
+ (locked?))))))))))
(deftest groups-list-limit-test
(testing "GET /api/permissions/group?limit=1&offset=1"
@@ -179,18 +307,32 @@
(perms/grant-collection-readwrite-permissions! writers coll)
(perms/grant-collection-read-permissions! (perms-group/all-users) coll)
(perms/grant-collection-read-permissions! (perms-group/data-analyst) coll)
- (let [ids (set (mt/user-http-request :crowberto :get 200 "permissions/invite-group-ids"
- :type "dashboard" :id (u/the-id dash)))]
- (testing "includes ids of groups with read or read-write access to the item's collection"
- (is (contains? ids (u/the-id readers)))
- (is (contains? ids (u/the-id writers)))
- (is (contains? ids (u/the-id (perms-group/all-users)))))
- (testing "the ids are unfiltered: system-managed groups like Data Analysts are included when they hold a grant"
- (is (contains? ids (u/the-id (perms-group/data-analyst)))))
- (testing "excludes groups without access"
- (is (not (contains? ids (u/the-id no-access)))))
- (testing "excludes the Administrators group, whose access is implicit rather than granted"
- (is (not (contains? ids (u/the-id (perms-group/admin))))))))
+ (let [fetch-ids (fn [] (set (mt/user-http-request :crowberto :get 200 "permissions/invite-group-ids"
+ :type "dashboard" :id (u/the-id dash))))]
+ (mt/with-premium-features #{:advanced-permissions}
+ (let [ids (fetch-ids)]
+ (testing "includes ids of groups with read or read-write access to the item's collection"
+ (is (contains? ids (u/the-id readers)))
+ (is (contains? ids (u/the-id writers)))
+ (is (contains? ids (u/the-id (perms-group/all-users)))))
+ (mt/when-ee-evailable
+ (testing "includes the Data Analysts group when it holds a grant"
+ (is (contains? ids (u/the-id (perms-group/data-analyst))))))
+ (testing "excludes groups without access"
+ (is (not (contains? ids (u/the-id no-access)))))
+ (testing "excludes the Administrators group, whose access is implicit rather than granted"
+ (is (not (contains? ids (u/the-id (perms-group/admin))))))))
+ (testing "without advanced-permissions the Data Analysts group is dropped, even when it holds a grant"
+ (mt/with-premium-features #{}
+ (let [ids (fetch-ids)]
+ (is (not (contains? ids (u/the-id (perms-group/data-analyst)))))
+ (is (contains? ids (u/the-id readers))))))
+ (mt/when-ee-evailable
+ (testing "without advanced-permissions the Data Analysts group is dropped even when populated"
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (add-to-data-analyst-group-with-feature! user-id)
+ (mt/with-premium-features #{}
+ (is (not (contains? (fetch-ids) (u/the-id (perms-group/data-analyst)))))))))))
(testing "works for questions, resolving the card's collection"
(mt/with-temp [:model/Collection coll {}
:model/Card card {:collection_id (u/the-id coll)}
@@ -793,3 +935,43 @@
(format "Expected at most 100 database calls, got %d" num-calls)))
(finally
(t2/delete! :model/PermissionsGroup :id [:in group-ids]))))))))
+
+(deftest add-data-analyst-group-membership-requires-advanced-permissions-test
+ (testing "POST /api/permissions/membership for the Data Analysts group"
+ (let [group-id (u/the-id (perms-group/data-analyst))
+ member? (fn [user-id]
+ (t2/exists? :model/PermissionsGroupMembership :user_id user-id :group_id group-id))]
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (testing "is refused without the :advanced-permissions feature"
+ (mt/with-premium-features #{}
+ (is (= (str perms/fail-to-add-data-analyst-msg)
+ (mt/user-http-request :crowberto :post 402 "permissions/membership"
+ {:group_id group-id, :user_id user-id})))
+ (is (not (member? user-id)))
+ (is (not (t2/select-one-fn :is_data_analyst :model/User :id user-id)))))
+ (mt/when-ee-evailable
+ (testing "succeeds with the :advanced-permissions feature"
+ (mt/with-premium-features #{:advanced-permissions}
+ (mt/user-http-request :crowberto :post 200 "permissions/membership"
+ {:group_id group-id, :user_id user-id})
+ (is (member? user-id))
+ (is (true? (boolean (t2/select-one-fn :is_data_analyst :model/User :id user-id))))))
+ (testing "and the member can still be removed without the feature"
+ (mt/with-premium-features #{}
+ (let [membership-id (t2/select-one-pk :model/PermissionsGroupMembership
+ :user_id user-id :group_id group-id)]
+ (mt/user-http-request :crowberto :delete 204 (format "permissions/membership/%d" membership-id))
+ (is (not (member? user-id)))
+ (is (not (t2/select-one-fn :is_data_analyst :model/User :id user-id)))))))))))
+
+(mt/when-ee-evailable
+ (deftest clear-data-analyst-group-membership-without-advanced-permissions-test
+ (testing "PUT /api/permissions/membership/:group-id/clear works for the Data Analysts group without the feature"
+ (let [group-id (u/the-id (perms-group/data-analyst))]
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (mt/with-premium-features #{:advanced-permissions}
+ (perms/add-user-to-group! user-id group-id))
+ (mt/with-premium-features #{}
+ (mt/user-http-request :crowberto :put 204 (format "permissions/membership/%d/clear" group-id))
+ (is (not (t2/exists? :model/PermissionsGroupMembership :user_id user-id :group_id group-id)))
+ (is (not (t2/select-one-fn :is_data_analyst :model/User :id user-id)))))))))
diff --git a/test/metabase/premium_features/token_check_test.clj b/test/metabase/premium_features/token_check_test.clj
index 6db0e1062e1..a9b361c7fda 100644
--- a/test/metabase/premium_features/token_check_test.clj
+++ b/test/metabase/premium_features/token_check_test.clj
@@ -59,17 +59,19 @@
(let [call-count (atom 0)
token (tu/random-token)
response (atom :error)
- ;; i've removed the circuit breaker from the stack. that sometimes shuts down the tests in a way we don't want
- ;; to exercise here
+ ;; no circuit breaker (carries state between blocks) and no local-ttl (it negative-caches
+ ;; failures) — this pins that the DURABLE tiers never store them
checker (binding [token-check/*customize-checker* true]
- (token-check/make-checker {:local-ttl (t/seconds 5)
- :soft-ttl (t/minutes 1)
- :hard-ttl (t/minutes 2)}))]
+ (token-check/make-checker {:soft-ttl (t/minutes 1)
+ :hard-ttl (t/minutes 2)}))]
(mt/with-dynamic-fn-redefs [token-check/http-fetch (fn [& _]
(swap! call-count inc)
(case @response
- :error (throw (ex-info "kaboom" {:ka :boom}))
- :500 {:status 500}))]
+ :error (throw (ex-info "kaboom" {:ka :boom}))
+ :500 {:status 500}
+ :400-empty {:status 400}
+ :400-html {:status 400 :body "<html>Bad Gateway</html>"}
+ :200-empty {:status 200}))]
(testing "For timeouts, 5XX errors, etc. we don't cache the result"
(dotimes [_ 5] (token-check/check-token checker token))
(is (= 5 @call-count)))
@@ -77,13 +79,248 @@
(reset! call-count 0)
(reset! response :500)
(dotimes [_ 5] (token-check/check-token checker token))
- (is (= 5 @call-count))))))
+ (is (= 5 @call-count)))
+ (testing "A 4XX with no body is not a real answer from the store — treated as transient, not cached"
+ (reset! call-count 0)
+ (reset! response :400-empty)
+ (dotimes [_ 5] (token-check/check-token checker token))
+ (is (= 5 @call-count))
+ (is (= {:valid false
+ :canonical? false
+ :status "Unable to validate token"
+ :error-details "Token validation provided no response."}
+ (token-check/check-token checker token))))
+ (testing "An unparseable body (e.g. a proxy's error page) is transient, not cached, and surfaces a legible error"
+ (reset! call-count 0)
+ (reset! response :400-html)
+ (dotimes [_ 5] (token-check/check-token checker token))
+ (is (= 5 @call-count))
+ (is (=? {:canonical? false
+ :error-details "Token validation provided no response."}
+ (token-check/check-token checker token))))
+ (testing "A 2XX with no body is transient, not cached, and surfaces a legible error"
+ (reset! call-count 0)
+ (reset! response :200-empty)
+ (dotimes [_ 5] (token-check/check-token checker token))
+ (is (= 5 @call-count))
+ (is (=? {:canonical? false
+ :error-details "Token validation provided no response."}
+ (token-check/check-token checker token)))))))
(deftest not-found-test
(mt/with-log-level :fatal
(is (=? {:valid false, :status "Token does not exist."}
(token-check/check-token (fresh-checker) (tu/random-token))))))
+(deftest expired-token-status-is-authoritative-test
+ (testing "a 403 with a body is a real answer from the store: stored faithfully, marked canonical (EMB-2341)"
+ (let [token (tu/random-token)
+ status {:valid false
+ :status "Token is expired."
+ :features ["sso" "snippet-collections" "database-routing"]
+ :plan-alias "pro-self-hosted"
+ :trial false
+ :valid-thru "2026-08-25T12:00:00Z"}
+ checker (binding [token-check/*customize-checker* true]
+ (token-check/make-checker {:local-ttl (t/seconds 5)
+ :soft-ttl (t/hours 12)
+ :hard-ttl (t/hours 36)}))]
+ (try
+ (mt/with-dynamic-fn-redefs [token-check/http-fetch (fn [& _] {:status 403 :body (json/encode status)})]
+ (is (= (assoc status :canonical? true)
+ (token-check/check-token checker token))))
+ (finally
+ (token-check/-clear-cache! checker))))))
+
+(deftest invalid-token-in-2xx-is-authoritative-test
+ (testing "an invalid verdict in a 2xx is a real answer: cached, and features gated the same as a 4xx"
+ (let [token (tu/random-token)
+ call-count (atom 0)
+ body (json/encode {:valid false :status "Token is expired." :features ["sso"]})
+ checker (binding [token-check/*customize-checker* true]
+ (token-check/make-checker {:local-ttl (t/seconds 5)
+ :soft-ttl (t/hours 12)
+ :hard-ttl (t/hours 36)}))]
+ (try
+ (mt/with-dynamic-fn-redefs [token-check/http-fetch (fn [& _]
+ (swap! call-count inc)
+ {:status 200 :body body})]
+ (is (= {:valid false :status "Token is expired." :features ["sso"] :canonical? true}
+ (token-check/check-token checker token)))
+ (token-check/check-token checker token)
+ (is (= 1 @call-count) "authoritative answers are cached"))
+ (finally
+ (token-check/-clear-cache! checker))))))
+
+(deftest hung-request-times-out-test
+ (testing "a request that never returns times out with a legible error and is not cached"
+ (let [token (tu/random-token)
+ call-count (atom 0)
+ checker (binding [token-check/*customize-checker* true]
+ (token-check/make-checker
+ {:base (reify token-check/TokenChecker
+ (-check-token [_ _]
+ (swap! call-count inc)
+ (Thread/sleep 5000))
+ (-clear-cache! [_]))
+ :circuit-breaker {:failure-threshold-ratio-in-period [10 10 1000]
+ :delay-ms 50
+ :success-threshold 1}
+ :timeout-ms 100
+ :local-ttl (t/millis 1)
+ :soft-ttl (t/minutes 1)
+ :hard-ttl (t/minutes 2)}))]
+ (try
+ (is (= {:valid false
+ :canonical? false
+ :status "Unable to validate token"
+ :error-details "Token validation timed out."}
+ (token-check/check-token checker token)))
+ (Thread/sleep 5) ;; expire the local memoize layer
+ (token-check/check-token checker token)
+ (is (= 2 @call-count) "timeouts are not cached")
+ (finally
+ (token-check/-clear-cache! checker))))))
+
+(deftest transient-failures-are-negative-cached-test
+ ;; timing rule: asserting behavior WITHIN a TTL window needs a TTL generous enough that test
+ ;; overhead can't overrun it (10s); sleeping PAST a tight TTL is deterministic (sleep ≥ duration)
+ (testing "a transient failure is memoized for local-ttl — an outage costs one attempt per TTL, not one per feature check"
+ (let [token (tu/random-token)
+ call-count (atom 0)
+ checker (binding [token-check/*customize-checker* true]
+ (token-check/make-checker {:local-ttl (t/seconds 10)
+ :soft-ttl (t/minutes 1)
+ :hard-ttl (t/minutes 2)}))]
+ (try
+ (mt/with-dynamic-fn-redefs [token-check/http-fetch (fn [& _]
+ (swap! call-count inc)
+ (throw (ex-info "network failure!" {})))]
+ (dotimes [_ 5] (token-check/check-token checker token))
+ (is (= 1 @call-count) "failures within the TTL are served from the negative cache")
+ (is (=? {:valid false :canonical? false :error-details "network failure!"}
+ (token-check/check-token checker token))))
+ (finally
+ (token-check/-clear-cache! checker)))))
+ (testing "the negative cache expires with the TTL — failures are retried"
+ (let [token (tu/random-token)
+ call-count (atom 0)
+ checker (binding [token-check/*customize-checker* true]
+ (token-check/make-checker {:local-ttl (t/millis 50)
+ :soft-ttl (t/minutes 1)
+ :hard-ttl (t/minutes 2)}))]
+ (try
+ (mt/with-dynamic-fn-redefs [token-check/http-fetch (fn [& _]
+ (swap! call-count inc)
+ (throw (ex-info "network failure!" {})))]
+ (token-check/check-token checker token)
+ (is (= 1 @call-count))
+ (Thread/sleep 100)
+ (token-check/check-token checker token)
+ (is (= 2 @call-count)))
+ (finally
+ (token-check/-clear-cache! checker)))))
+ (testing "concurrent misses share a single attempt (core.memoize single-flights values, and failures are values here)"
+ (let [token (tu/random-token)
+ call-count (atom 0)
+ checker (binding [token-check/*customize-checker* true]
+ (token-check/make-checker {:local-ttl (t/seconds 10)
+ :soft-ttl (t/minutes 1)
+ :hard-ttl (t/minutes 2)}))]
+ (try
+ (mt/with-dynamic-fn-redefs [token-check/http-fetch (fn [& _]
+ (swap! call-count inc)
+ (Thread/sleep 100)
+ (throw (ex-info "network failure!" {})))]
+ (let [checks (mapv (fn [_] (future (token-check/check-token checker token))) (range 8))]
+ (is (every? #(false? (:valid %)) (mapv deref checks)))
+ (is (= 1 @call-count) "8 concurrent callers, one network attempt")))
+ (finally
+ (token-check/-clear-cache! checker))))))
+
+(deftest bare-rejection-is-transient-test
+ (doseq [http-status [403 404]]
+ (testing (format "a bare %d (no body) carries no verdict from the store: a proxy may have stripped it, so it is transient, not cached"
+ http-status)
+ (let [token (tu/random-token)
+ call-count (atom 0)
+ ;; no local-ttl: it negative-caches failures, and this pins that the durable tiers never store them
+ checker (binding [token-check/*customize-checker* true]
+ (token-check/make-checker {:soft-ttl (t/minutes 1)
+ :hard-ttl (t/minutes 2)}))]
+ (try
+ (mt/with-dynamic-fn-redefs [token-check/http-fetch (fn [& _]
+ (swap! call-count inc)
+ {:status http-status})]
+ (dotimes [_ 5] (token-check/check-token checker token))
+ (is (= 5 @call-count) "not cached: every check goes back to the store")
+ (is (=? {:valid false
+ :canonical? false
+ :error-details "Token validation provided no response."}
+ (token-check/check-token checker token))))
+ (finally
+ (token-check/-clear-cache! checker)))))))
+
+(deftest malformed-status-body-is-transient-test
+ (doseq [[description body] {"an empty object" "{}"
+ "an unrelated object" "{\"foo\":1}"
+ "a non-boolean valid" "{\"valid\":\"yes\",\"status\":\"ok\"}"
+ "a blank status" "{\"valid\":false,\"status\":\"\"}"
+ "features that aren't a list" "{\"valid\":true,\"status\":\"ok\",\"features\":\"sso\"}"
+ "a JSON value, not an object" "[1,2,3]"}]
+ (testing (format "%s decodes as JSON but is not a token status: transient, never stamped canonical or cached" description)
+ (let [token (tu/random-token)
+ call-count (atom 0)
+ checker (binding [token-check/*customize-checker* true]
+ (token-check/make-checker {:soft-ttl (t/minutes 1)
+ :hard-ttl (t/minutes 2)}))]
+ (try
+ (mt/with-dynamic-fn-redefs [token-check/http-fetch (fn [& _]
+ (swap! call-count inc)
+ {:status 200 :body body})]
+ (dotimes [_ 3] (token-check/check-token checker token))
+ (is (= 3 @call-count) "not cached")
+ (is (=? {:valid false
+ :canonical? false
+ :error-details "Token validation provided no response."}
+ (token-check/check-token checker token))))
+ (finally
+ (token-check/-clear-cache! checker)))))))
+
+(deftest do-refresh-refuses-non-canonical-test
+ (testing "the caching layer refuses to hold on to a result not marked :canonical?"
+ (let [token (tu/random-token)
+ call-count (atom 0)
+ checker (binding [token-check/*customize-checker* true]
+ (token-check/make-checker
+ {:base (reify token-check/TokenChecker
+ (-check-token [_ _]
+ (swap! call-count inc)
+ {:valid true :status "fake" :features ["sso"]})
+ (-clear-cache! [_]))
+ :soft-ttl (t/hours 12)
+ :hard-ttl (t/hours 36)}))]
+ (try
+ (dotimes [_ 3] (token-check/check-token checker token))
+ (is (= 3 @call-count) "non-canonical results are not cached")
+ (is (=? {:valid false
+ :canonical? false
+ :error-details "Refusing to cache a non-authoritative token status"}
+ (token-check/check-token checker token)))
+ (finally
+ (token-check/-clear-cache! checker))))))
+
+(deftest token-features-honors-valid-test
+ (testing "*token-features* returns no features when the token status is not valid, even if features are present"
+ (mt/with-temporary-raw-setting-values [premium-embedding-token (tu/random-token)]
+ (mt/with-dynamic-fn-redefs [token-check/check-token (fn [_token] {:valid false
+ :canonical? true
+ :status "Token is expired."
+ :features ["sso"]})]
+ (is (= #{} (token-check/*token-features*)))
+ (is (false? (token-check/has-feature? :sso)))
+ (is (false? (token-check/canonically-has-feature? :sso)))))))
+
(deftest fetch-token-does-not-call-db-when-cached
(testing "No DB calls are made when checking token status if the status is in local cache"
(let [token (tu/random-token)
@@ -101,14 +338,14 @@
(let [token (tu/random-token)
behavior (atom :success)
call-count (atom 0)
- good-response {:valid true
- :status "fake"
- :features ["feature1" "feature2"]}
+ good-response {:valid true
+ :status "fake"
+ :features ["feature1" "feature2"]
+ :canonical? true}
token-response (fn [_token]
(swap! call-count inc)
(case @behavior
:success good-response
- :timeout (Thread/sleep 200)
:error (throw (ex-info "network issues!" {:ka :boom}))))
checker (token-check/make-checker
{:base (reify token-check/TokenChecker
@@ -135,13 +372,14 @@
(is (< @call-count 50)))
(testing "When connectivity is restored we get successful token checks"
(reset! behavior :success)
- (Thread/sleep 100) ;; wait for circuit breaker to open back up
+ (Thread/sleep 100) ;; wait for circuit breaker to open back up (also outlasts the 50ms negative cache)
(is (= good-response (token-check/-check-token checker token)))))
(testing "App db not setup yet does not invoke the circuit breaker (#65294)"
(let [token (tu/random-token)
- good-response {:valid true
- :status "fake"
- :features ["feature1" "feature2"]}
+ good-response {:valid true
+ :status "fake"
+ :features ["feature1" "feature2"]
+ :canonical? true}
token-response (fn [_token]
good-response)
checker (token-check/make-checker
@@ -169,6 +407,7 @@
:error-details "Metabase DB is not yet set up"}
(token-check/-check-token checker token))))
(testing "When the db-is-set-up? we are not blocked by the circuit breaker"
+ (Thread/sleep 60) ;; outlast the 50ms local-ttl — the not-set-up failure map is negative-cached
(mt/with-dynamic-fn-redefs [mdb/db-is-set-up? (constantly true)]
(is (= good-response (token-check/-check-token checker token))))))))
@@ -545,7 +784,7 @@
(deftest db-hash-aware-token-checker-fresh-hit-test
(testing "Fresh cache entry (< soft-ttl) is returned without hitting the underlying checker"
(let [call-count (atom 0)
- good-response {:valid true :status "OK" :features ["sandboxes"]}
+ good-response {:valid true :status "OK" :features ["sandboxes"] :canonical? true}
checker (make-db-hash-aware-checker
(fn [_token]
(swap! call-count inc)
@@ -565,8 +804,8 @@
(deftest db-hash-aware-token-checker-stale-async-refresh-test
(testing "Stale entry (soft..hard) returns cached value and triggers async refresh"
(let [call-count (atom 0)
- good-response {:valid true :status "OK" :features ["sandboxes"]}
- updated-response {:valid true :status "OK" :features ["sandboxes" "audit-app"]}
+ good-response {:valid true :status "OK" :features ["sandboxes"] :canonical? true}
+ updated-response {:valid true :status "OK" :features ["sandboxes" "audit-app"] :canonical? true}
response-atom (atom good-response)
refresh-done (promise)
checker (make-db-hash-aware-checker
@@ -587,9 +826,8 @@
;; Second call: stale → returns old cached value, kicks off async refresh
(binding [token-check/*testing-only-call-after-refresh* #(deliver refresh-done true)]
(is (= good-response (token-check/-check-token checker token)))
- (is (true? @refresh-done)))
- ;; Wait for the async refresh to complete
- (is (not= :timeout (deref refresh-done 5000 :timeout)))
+ ;; bounded deref — unbounded hangs the whole run if the refresh never fires
+ (is (true? (deref refresh-done 5000 :timeout))))
;; Expire local cache
(Thread/sleep 10)
;; Third call: should now see the refreshed value
@@ -600,7 +838,7 @@
(deftest db-hash-aware-token-checker-expired-sync-test
(testing "Expired entry (> hard-ttl) delegates synchronously"
(let [call-count (atom 0)
- good-response {:valid true :status "OK" :features ["sandboxes"]}
+ good-response {:valid true :status "OK" :features ["sandboxes"] :canonical? true}
checker (make-db-hash-aware-checker
(fn [_token]
(swap! call-count inc)
@@ -625,7 +863,7 @@
checker (make-db-hash-aware-checker
(fn [token]
(swap! call-count inc)
- {:valid true :status "OK" :features [(str "feat-" token)]})
+ {:valid true :status "OK" :features [(str "feat-" token)] :canonical? true})
{:soft-ttl (t/minutes 1) :hard-ttl (t/minutes 2)})
token-a (tu/random-token)
token-b (tu/random-token)]
@@ -646,7 +884,7 @@
(testing "clear-cache! removes both local and DB cache"
(let [checker (make-db-hash-aware-checker
(fn [_token]
- {:valid true :status "OK" :features ["sandboxes"]})
+ {:valid true :status "OK" :features ["sandboxes"] :canonical? true})
{:soft-ttl (t/minutes 1) :hard-ttl (t/minutes 2)})
token (tu/random-token)]
(token-check/-check-token checker token)
@@ -661,15 +899,15 @@
checker (make-db-hash-aware-checker
(fn [_token]
(swap! call-count inc)
- {:valid false :status "Token does not exist."})
+ {:valid false :status "Token does not exist." :canonical? true})
{:soft-ttl (t/minutes 1) :hard-ttl (t/minutes 2)})
token (tu/random-token)]
(try
- (is (= {:valid false :status "Token does not exist."}
+ (is (= {:valid false :status "Token does not exist." :canonical? true}
(token-check/-check-token checker token)))
(is (= 1 @call-count))
;; Second call should use cache
- (is (= {:valid false :status "Token does not exist."}
+ (is (= {:valid false :status "Token does not exist." :canonical? true}
(token-check/-check-token checker token)))
(is (= 1 @call-count))
(finally
@@ -679,8 +917,8 @@
(testing "When instance A refreshes and gets new features, instance B detects the hash mismatch and re-fetches"
(let [call-count-a (atom 0)
call-count-b (atom 0)
- old-response {:valid true :status "OK" :features ["sandboxes"]}
- new-response {:valid true :status "OK" :features ["sandboxes" "audit-app"]}
+ old-response {:valid true :status "OK" :features ["sandboxes"] :canonical? true}
+ new-response {:valid true :status "OK" :features ["sandboxes" "audit-app"] :canonical? true}
response-a (atom old-response)
response-b (atom old-response)
checker-a (make-db-hash-aware-checker
@@ -732,7 +970,7 @@
(deftest db-hash-aware-token-checker-tamper-resistance-test
(testing "Tampering with the DB hash triggers a re-fetch from MetaStore, not a cache hit"
(let [call-count (atom 0)
- good-response {:valid true :status "OK" :features ["sandboxes"]}
+ good-response {:valid true :status "OK" :features ["sandboxes"] :canonical? true}
checker (make-db-hash-aware-checker
(fn [_token]
(swap! call-count inc)
@@ -758,7 +996,7 @@
(deftest startup-clears-token-cache-test
(testing "The startup hook clears the token cache table so the first check after boot hits MetaStore"
(let [call-count (atom 0)
- good-response {:valid true :status "OK" :features ["sandboxes"]}
+ good-response {:valid true :status "OK" :features ["sandboxes"] :canonical? true}
checker (make-db-hash-aware-checker
(fn [_token]
(swap! call-count inc)
@@ -781,7 +1019,7 @@
(deftest db-hash-aware-token-checker-db-not-set-up-test
(testing "When DB is not set up, delegates directly to inner checker without touching the DB"
(let [call-count (atom 0)
- good-response {:valid true :status "OK" :features ["sandboxes"]}
+ good-response {:valid true :status "OK" :features ["sandboxes"] :canonical? true}
checker (make-db-hash-aware-checker
(fn [_token]
(swap! call-count inc)
@@ -838,3 +1076,23 @@
(mt/with-temporary-setting-values [premium-embedding-token nil]
(token-check/-set-premium-embedding-token! token)
(is (= token (premium-features/premium-embedding-token))))))))
+
+(deftest set-premium-embedding-token-busts-stale-cache-test
+ (testing "re-entering a token after renewal revalidates with the store instead of a cached expired verdict (EMB-2341)"
+ (let [token (tu/random-token)
+ expired (json/encode {:valid false :status "Token is expired."})
+ renewed (json/encode {:valid true :status "Token is valid." :features ["sso"]})
+ state (atom :expired)]
+ (mt/with-dynamic-fn-redefs [token-check/http-fetch (fn [& _]
+ (case @state
+ :expired {:status 403 :body expired}
+ :renewed {:status 200 :body renewed}))]
+ (try
+ ;; prime the cache with the expired verdict
+ (is (=? {:valid false :canonical? true} (token-check/check-token token)))
+ (reset! state :renewed)
+ (mt/with-temporary-setting-values [premium-embedding-token nil]
+ (token-check/-set-premium-embedding-token! token)
+ (is (= token (premium-features/premium-embedding-token))))
+ (finally
+ (token-check/clear-cache!)))))))
diff --git a/test/metabase/revisions/api_test.clj b/test/metabase/revisions/api_test.clj
index b4722427942..223fad2446e 100644
--- a/test/metabase/revisions/api_test.clj
+++ b/test/metabase/revisions/api_test.clj
@@ -150,7 +150,7 @@
(deftest transform-revisions-guard-prior-source-per-entitlement-test
(testing "a Transform's revision history is authorized per snapshot: a prior :source is served only to callers
entitled to the database it read from"
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/with-temp [:model/Database {x-db-id :id} {}
:model/Database {y-db-id :id} {}
:model/Transform {transform-id :id}
@@ -183,21 +183,22 @@
(let [revisions (list-fn :crowberto 200)]
(is (contains-x? revisions))
(is (some #(= "changed the source." (:description %)) revisions))))
- (mt/with-data-analyst-role! (mt/user->id :rasta)
- (mt/with-restored-data-perms!
- ;; rasta holds the transforms entitlement on the current source database but none on the prior one
- (data-perms/set-database-permission! (perms-group/all-users) y-db-id :perms/view-data :unrestricted)
- (data-perms/set-database-permission! (perms-group/all-users) y-db-id :perms/create-queries :query-builder-and-native)
- (data-perms/set-database-permission! (perms-group/all-users) y-db-id :perms/transforms :yes)
- (data-perms/set-database-permission! (perms-group/all-users) x-db-id :perms/create-queries :no)
- (testing "an analyst entitled only to the current source can still read the transform"
- (let [revisions (list-fn :rasta 200)]
- (is (seq revisions))
- (testing "but the prior source read from a database they cannot query is withheld"
- (is (not (contains-x? revisions))))))
- (testing "an analyst entitled to neither source cannot read the revision history at all"
- (data-perms/set-database-permission! (perms-group/all-users) y-db-id :perms/create-queries :no)
- (list-fn :rasta 403)))))))))))
+ (mt/when-ee-evailable
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-restored-data-perms!
+ ;; rasta holds the transforms entitlement on the current source database but none on the prior one
+ (data-perms/set-database-permission! (perms-group/all-users) y-db-id :perms/view-data :unrestricted)
+ (data-perms/set-database-permission! (perms-group/all-users) y-db-id :perms/create-queries :query-builder-and-native)
+ (data-perms/set-database-permission! (perms-group/all-users) y-db-id :perms/transforms :yes)
+ (data-perms/set-database-permission! (perms-group/all-users) x-db-id :perms/create-queries :no)
+ (testing "an analyst entitled only to the current source can still read the transform"
+ (let [revisions (list-fn :rasta 200)]
+ (is (seq revisions))
+ (testing "but the prior source read from a database they cannot query is withheld"
+ (is (not (contains-x? revisions))))))
+ (testing "an analyst entitled to neither source cannot read the revision history at all"
+ (data-perms/set-database-permission! (perms-group/all-users) y-db-id :perms/create-queries :no)
+ (list-fn :rasta 403))))))))))))
;;; # POST /revision/revert
diff --git a/test/metabase/segments/models/segment_test.clj b/test/metabase/segments/models/segment_test.clj
index 1387e282645..b32a13343e5 100644
--- a/test/metabase/segments/models/segment_test.clj
+++ b/test/metabase/segments/models/segment_test.clj
@@ -183,7 +183,11 @@
:creator_id (mt/user->id :rasta)
:definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}]
(mt/with-test-user :crowberto
- (is (true? (mi/can-write? segment)))))))
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (true? (mi/can-write? segment))))
+ (testing "including without the advanced-permissions feature"
+ (mt/with-premium-features #{}
+ (is (true? (mi/can-write? segment)))))))))
(deftest can-write?-analyst-unrestricted-test
(testing "Data analysts with unrestricted view-data can write segments"
@@ -197,7 +201,12 @@
(perms/add-user-to-group! analyst-id group-id)
(data-perms/set-table-permission! group-id (mt/id :venues) :perms/view-data :unrestricted)
(session/with-current-user analyst-id
- (is (mi/can-write? segment)))))))
+ (mt/when-ee-evailable
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (mi/can-write? segment))))
+ (testing "but not once the advanced-permissions feature is gone"
+ (mt/with-premium-features #{}
+ (is (not (mi/can-write? segment))))))))))
(deftest can-write?-analyst-restricted-test
(testing "Data analysts without unrestricted view-data cannot write segments"
@@ -234,9 +243,18 @@
(perms/add-user-to-group! analyst-id group-id)
(data-perms/set-table-permission! group-id (mt/id :venues) :perms/view-data :unrestricted)
(session/with-current-user analyst-id
- (is (true? (mi/can-create? :model/Segment {:name "Test Segment"
- :table_id (mt/id :venues)
- :definition {:filter [:> [:field (mt/id :venues :price) nil] 2]}}))))))))
+ (mt/when-ee-evailable
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (true? (mi/can-create? :model/Segment {:name "Test Segment"
+ :table_id (mt/id :venues)
+ :definition {:filter [:> [:field (mt/id :venues :price) nil]
+ 2]}})))))
+ (testing "but not once the advanced-permissions feature is gone"
+ (mt/with-premium-features #{}
+ (is (false? (mi/can-create? :model/Segment {:name "Test Segment"
+ :table_id (mt/id :venues)
+ :definition {:filter [:> [:field (mt/id :venues :price) nil]
+ 2]}}))))))))))
(deftest can-create?-analyst-restricted-test
(testing "Data analysts without unrestricted view-data cannot create segments"
diff --git a/test/metabase/sso/common_test.clj b/test/metabase/sso/common_test.clj
index 74f8710b482..200b82b5996 100644
--- a/test/metabase/sso/common_test.clj
+++ b/test/metabase/sso/common_test.clj
@@ -241,3 +241,32 @@
(is (= (str "Attempted to remove the last admin during group sync! "
"Check your SSO group mappings and make sure the Administrators group is mapped correctly.")
(:message (first (messages))))))))))
+
+(deftest sync-groups-data-analyst-without-advanced-permissions-test
+ (testing "a mapped add to the Data Analysts group is logged and skipped when :advanced-permissions is missing"
+ (mt/with-premium-features #{}
+ (mt/with-log-messages-for-level [messages :error]
+ (mt/with-user-in-groups [group {:name (str ::group)}
+ user []]
+ (integrations.common/sync-group-memberships! user #{group (perms-group/data-analyst)})
+ (testing "the rest of the sync still happens"
+ (is (= #{"All Users" ":metabase.sso.common-test/group"}
+ (group-memberships user))))
+ (testing "the user is not put in the Data Analysts group"
+ (is (not (t2/exists? :model/PermissionsGroupMembership
+ :user_id (u/the-id user)
+ :group_id (u/the-id (perms-group/data-analyst))))))
+ (testing "and the refusal is logged"
+ (is (some #(re-find #"Advanced Permissions" (str (:message %)))
+ (messages)))))))))
+
+(mt/when-ee-evailable
+ (deftest sync-groups-data-analyst-with-advanced-permissions-test
+ (testing "a mapped add to the Data Analysts group succeeds when :advanced-permissions is present"
+ (mt/with-premium-features #{:advanced-permissions}
+ (mt/with-user-in-groups [user []]
+ (integrations.common/sync-group-memberships! user #{(perms-group/data-analyst)})
+ (is (t2/exists? :model/PermissionsGroupMembership
+ :user_id (u/the-id user)
+ :group_id (u/the-id (perms-group/data-analyst))))
+ (is (true? (boolean (t2/select-one-fn :is_data_analyst :model/User :id (u/the-id user))))))))))
diff --git a/test/metabase/transforms/models/transform_job_test.clj b/test/metabase/transforms/models/transform_job_test.clj
index 5d778970839..996ed9f2523 100644
--- a/test/metabase/transforms/models/transform_job_test.clj
+++ b/test/metabase/transforms/models/transform_job_test.clj
@@ -2,6 +2,7 @@
"Tests for the transform job model."
(:require
[clojure.test :refer :all]
+ [metabase.models.interface :as mi]
[metabase.test :as mt]
[metabase.transforms.models.transform-job :as transform-job]
[metabase.util.i18n :as i18n]
@@ -77,3 +78,24 @@
(str (:description (t2/select-one :model/TransformJob (:id job))))))
(is (= (str (:name translations))
(str (:name (t2/select-one :model/TransformJob (:id job))))))))))
+
+(deftest data-analyst-job-access-follows-the-advanced-permissions-feature-test
+ (testing "a data analyst reads, writes and creates transform jobs only while advanced-permissions is available"
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-current-user (mt/user->id :rasta)
+ (mt/when-ee-evailable
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (mi/can-read? :model/TransformJob 1))
+ (is (mi/can-write? (t2/instance :model/TransformJob {})))
+ (is (mi/can-create? :model/TransformJob {}))))
+ (testing "and none once the feature is gone"
+ (mt/with-premium-features #{}
+ (is (not (mi/can-read? :model/TransformJob 1)))
+ (is (not (mi/can-write? (t2/instance :model/TransformJob {}))))
+ (is (not (mi/can-create? :model/TransformJob {})))))))
+ (testing "a superuser is unaffected"
+ (mt/with-current-user (mt/user->id :crowberto)
+ (mt/with-premium-features #{}
+ (is (mi/can-read? :model/TransformJob 1))
+ (is (mi/can-write? (t2/instance :model/TransformJob {})))
+ (is (mi/can-create? :model/TransformJob {})))))))
diff --git a/test/metabase/transforms/models/transform_tag_test.clj b/test/metabase/transforms/models/transform_tag_test.clj
index 1957b1a0390..5d275da38a1 100644
--- a/test/metabase/transforms/models/transform_tag_test.clj
+++ b/test/metabase/transforms/models/transform_tag_test.clj
@@ -2,6 +2,7 @@
"Tests for the transform tag model."
(:require
[clojure.test :refer :all]
+ [metabase.models.interface :as mi]
[metabase.test :as mt]
[metabase.transforms.models.transform-tag :as transform-tag]
[metabase.util.i18n :as i18n]
@@ -25,3 +26,25 @@
{:name "default2"})
(is (= "default2"
(:name (t2/select-one :model/TransformTag (:id tag))))))))
+
+(deftest data-analyst-tag-access-follows-the-advanced-permissions-feature-test
+ (testing "a data analyst reads, writes and creates transform tags only while advanced-permissions is available"
+ (mt/with-temp [:model/TransformTag tag {:name "gated"}]
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-current-user (mt/user->id :rasta)
+ (mt/when-ee-evailable
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (mi/can-read? :model/TransformTag 1))
+ (is (mi/can-write? tag))
+ (is (mi/can-create? :model/TransformTag {}))))
+ (testing "and none once the feature is gone"
+ (mt/with-premium-features #{}
+ (is (not (mi/can-read? :model/TransformTag 1)))
+ (is (not (mi/can-write? tag)))
+ (is (not (mi/can-create? :model/TransformTag {})))))))
+ (testing "a superuser is unaffected"
+ (mt/with-current-user (mt/user->id :crowberto)
+ (mt/with-premium-features #{}
+ (is (mi/can-read? :model/TransformTag 1))
+ (is (mi/can-write? tag))
+ (is (mi/can-create? :model/TransformTag {}))))))))
diff --git a/test/metabase/transforms/models/transform_test.clj b/test/metabase/transforms/models/transform_test.clj
index cb0d6d26eb5..ef8cfaf42ab 100644
--- a/test/metabase/transforms/models/transform_test.clj
+++ b/test/metabase/transforms/models/transform_test.clj
@@ -107,45 +107,46 @@
(is (not (mi/can-read? reloaded)))
(is (not (mi/can-write? reloaded)))))))))
-(deftest native-transform-requires-query-builder-and-native-test
- (testing "Writing a native-query transform requires \"Query builder and native\" on the source database, even if the
- group was misconfigured with transforms permission but only query-builder access (no native)."
- (mt/with-premium-features #{:transforms-basic :hosting}
- (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}
- :model/Transform native-transform
- {:name "Native Transform"
- :source {:type "query"
- :query {:database (mt/id)
- :type "native"
- :native {:query "SELECT 1"}}}}
- :model/Transform mbql-transform
- {:name "MBQL Transform"
- :source {:type "query"
- :query (lib/query (mt/metadata-provider)
- (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}}]
- (let [native (t2/select-one :model/Transform (:id native-transform))
- mbql (t2/select-one :model/Transform (:id mbql-transform))]
- (mt/with-data-analyst-role! (mt/user->id :rasta)
- (mt/with-restored-data-perms!
- ;; Remove native access granted to All Users by default so the user's only path to native would be
- ;; the (mis)configured transforms group.
- (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/view-data :unrestricted)
- (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder)
- (testing "transforms granted, but create-queries only query-builder (no native) — preexisting misconfiguration"
- (data-perms/set-database-permission! group-id (mt/id) :perms/view-data :unrestricted)
- (data-perms/set-database-permission! group-id (mt/id) :perms/create-queries :query-builder)
- (data-perms/set-database-permission! group-id (mt/id) :perms/transforms :yes)
- (mt/with-current-user (mt/user->id :rasta)
- (testing "native transform cannot be written"
- (is (not (mi/can-write? native))))
- (testing "non-native (MBQL) transform is unaffected"
- (is (mi/can-write? mbql)))))
- (testing "with query-builder-and-native, native transform can be written"
- (data-perms/set-database-permission! group-id (mt/id) :perms/create-queries :query-builder-and-native)
- (data-perms/set-database-permission! group-id (mt/id) :perms/transforms :yes)
- (mt/with-current-user (mt/user->id :rasta)
- (is (mi/can-write? native)))))))))))
+(mt/when-ee-evailable
+ (deftest native-transform-requires-query-builder-and-native-test
+ (testing "Writing a native-query transform requires \"Query builder and native\" on the source database, even if the
+ group was misconfigured with transforms permission but only query-builder access (no native)."
+ (mt/with-premium-features #{:transforms-basic :hosting :advanced-permissions}
+ (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}
+ :model/Transform native-transform
+ {:name "Native Transform"
+ :source {:type "query"
+ :query {:database (mt/id)
+ :type "native"
+ :native {:query "SELECT 1"}}}}
+ :model/Transform mbql-transform
+ {:name "MBQL Transform"
+ :source {:type "query"
+ :query (lib/query (mt/metadata-provider)
+ (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}}]
+ (let [native (t2/select-one :model/Transform (:id native-transform))
+ mbql (t2/select-one :model/Transform (:id mbql-transform))]
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-restored-data-perms!
+ ;; Remove native access granted to All Users by default so the user's only path to native would be
+ ;; the (mis)configured transforms group.
+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/view-data :unrestricted)
+ (data-perms/set-database-permission! (perms-group/all-users) (mt/id) :perms/create-queries :query-builder)
+ (testing "transforms granted, but create-queries only query-builder (no native) — preexisting misconfiguration"
+ (data-perms/set-database-permission! group-id (mt/id) :perms/view-data :unrestricted)
+ (data-perms/set-database-permission! group-id (mt/id) :perms/create-queries :query-builder)
+ (data-perms/set-database-permission! group-id (mt/id) :perms/transforms :yes)
+ (mt/with-current-user (mt/user->id :rasta)
+ (testing "native transform cannot be written"
+ (is (not (mi/can-write? native))))
+ (testing "non-native (MBQL) transform is unaffected"
+ (is (mi/can-write? mbql)))))
+ (testing "with query-builder-and-native, native transform can be written"
+ (data-perms/set-database-permission! group-id (mt/id) :perms/create-queries :query-builder-and-native)
+ (data-perms/set-database-permission! group-id (mt/id) :perms/transforms :yes)
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (mi/can-write? native))))))))))))
(deftest execute-fails-clearly-when-source-db-deleted-test
(testing "running a query transform whose source database has been deleted fails with a clear error"
@@ -233,45 +234,73 @@
(is (=? {:id run-id, :transform_id transform-id} last-run))
(is (not (contains? last-run :last_heartbeat)))))))
-(deftest source-references-gate-transform-permissions-test
- (testing "A Card the source query names is required to read, write or create the transform"
- (mt/with-premium-features #{:transforms-basic :hosting}
- (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
- :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}
- :model/Collection collection {}
- :model/Card {card-id :id}
- {:collection_id (:id collection)
- :database_id (mt/id)
- :dataset_query (lib/query (mt/metadata-provider)
- (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}
- :model/Transform transform
- {:name "Reads a Card"
- :source {:type "query"
- :query {:database (mt/id)
- :type "native"
- :native {:query (format "SELECT * FROM {{#%d}} AS c" card-id)
- :template-tags {(str "#" card-id)
- {:id (str "#" card-id)
- :name (str "#" card-id)
- :display-name (str "#" card-id)
- :type :card
- :card-id card-id}}}}}}]
- (mt/with-non-admin-groups-no-collection-perms collection
- (let [stored (t2/select-one :model/Transform (:id transform))
- body (into {} stored)]
- (mt/with-data-analyst-role! (mt/user->id :rasta)
- (mt/with-restored-data-perms!
- (data-perms/set-database-permission! group-id (mt/id) :perms/view-data :unrestricted)
- (data-perms/set-database-permission! group-id (mt/id) :perms/create-queries :query-builder-and-native)
- (data-perms/set-database-permission! group-id (mt/id) :perms/transforms :yes)
- (testing "while the Card is unreadable"
- (mt/with-current-user (mt/user->id :rasta)
- (is (not (mi/can-read? stored)))
- (is (not (mi/can-write? stored)))
- (is (not (mi/can-create? :model/Transform body)))))
- (perms/grant-collection-read-permissions! group-id collection)
- (testing "once the Card is readable"
- (mt/with-current-user (mt/user->id :rasta)
- (is (mi/can-read? stored))
- (is (mi/can-write? stored))
- (is (mi/can-create? :model/Transform body))))))))))))
+(mt/when-ee-evailable
+ (deftest source-references-gate-transform-permissions-test
+ (testing "A Card the source query names is required to read, write or create the transform"
+ (mt/with-premium-features #{:transforms-basic :hosting :advanced-permissions}
+ (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}
+ :model/Collection collection {}
+ :model/Card {card-id :id}
+ {:collection_id (:id collection)
+ :database_id (mt/id)
+ :dataset_query (lib/query (mt/metadata-provider)
+ (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}
+ :model/Transform transform
+ {:name "Reads a Card"
+ :source {:type "query"
+ :query {:database (mt/id)
+ :type "native"
+ :native {:query (format "SELECT * FROM {{#%d}} AS c" card-id)
+ :template-tags {(str "#" card-id)
+ {:id (str "#" card-id)
+ :name (str "#" card-id)
+ :display-name (str "#" card-id)
+ :type :card
+ :card-id card-id}}}}}}]
+ (mt/with-non-admin-groups-no-collection-perms collection
+ (let [stored (t2/select-one :model/Transform (:id transform))
+ body (into {} stored)]
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-restored-data-perms!
+ (data-perms/set-database-permission! group-id (mt/id) :perms/view-data :unrestricted)
+ (data-perms/set-database-permission! group-id (mt/id) :perms/create-queries :query-builder-and-native)
+ (data-perms/set-database-permission! group-id (mt/id) :perms/transforms :yes)
+ (testing "while the Card is unreadable"
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (not (mi/can-read? stored)))
+ (is (not (mi/can-write? stored)))
+ (is (not (mi/can-create? :model/Transform body)))))
+ (perms/grant-collection-read-permissions! group-id collection)
+ (testing "once the Card is readable"
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (mi/can-read? stored))
+ (is (mi/can-write? stored))
+ (is (mi/can-create? :model/Transform body)))))))))))))
+
+(deftest data-analyst-access-follows-the-advanced-permissions-feature-test
+ (testing "a data analyst reads and writes transforms only while advanced-permissions is available"
+ (mt/with-temp [:model/PermissionsGroup {group-id :id} {}
+ :model/PermissionsGroupMembership _ {:user_id (mt/user->id :rasta) :group_id group-id}
+ :model/Transform transform
+ {:name "MBQL Transform"
+ :source {:type "query"
+ :query (lib/query (mt/metadata-provider)
+ (lib.metadata/table (mt/metadata-provider) (mt/id :orders)))}}]
+ (let [stored (t2/select-one :model/Transform (:id transform))]
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-restored-data-perms!
+ (data-perms/set-database-permission! group-id (mt/id) :perms/view-data :unrestricted)
+ (data-perms/set-database-permission! group-id (mt/id) :perms/create-queries :query-builder-and-native)
+ (data-perms/set-database-permission! group-id (mt/id) :perms/transforms :yes)
+ (mt/with-current-user (mt/user->id :rasta)
+ (mt/when-ee-evailable
+ (mt/with-premium-features #{:transforms-basic :hosting :advanced-permissions}
+ (is (mi/can-read? stored))
+ (is (mi/can-write? stored))
+ (is (mi/can-create? :model/Transform (into {} stored)))))
+ (testing "and none once the feature is gone"
+ (mt/with-premium-features #{:transforms-basic :hosting}
+ (is (not (mi/can-read? stored)))
+ (is (not (mi/can-write? stored)))
+ (is (not (mi/can-create? :model/Transform (into {} stored)))))))))))))
diff --git a/test/metabase/transforms/test_util.clj b/test/metabase/transforms/test_util.clj
index a1839e25258..2a1eda8b134 100644
--- a/test/metabase/transforms/test_util.clj
+++ b/test/metabase/transforms/test_util.clj
@@ -3,6 +3,7 @@
[clojure.string :as str]
[clojure.test :refer :all]
[metabase.api.common :as api]
+ [metabase.config.core :as config]
[metabase.driver :as driver]
[metabase.driver.util :as driver.u]
[metabase.test :as mt]
@@ -20,6 +21,25 @@
;; BigQuery runs routinely take 50-70s in CI.
120)
+(defn do-with-transforms-api-users!
+ "Implementation of [[with-transforms-api-users!]]."
+ [f]
+ ;; a data analyst only reaches the transforms API with `advanced-permissions`, which no OSS build can
+ ;; have — so under OSS only the superuser drives these endpoints
+ (doseq [user (cond-> [:crowberto] config/ee-available? (conj :lucky))]
+ (testing (str "as " (name user))
+ (if (= user :crowberto)
+ (f user)
+ (mt/with-data-analyst-role! (mt/user->id user)
+ (f user))))))
+
+(defmacro with-transforms-api-users!
+ "Runs `body` once per user who may drive the transforms API, with `user` bound to the test-user keyword:
+ `:crowberto` in every edition, and `:lucky` as a data analyst when EE is available. Every assertion in
+ `body` must hold for both."
+ [[user] & body]
+ `(do-with-transforms-api-users! (^:once fn* [~user] ~@body)))
+
(defn seconds-from-now-ns
"Returns a deadline `seconds` from now in nanoseconds, for use with `System/nanoTime`.
We use nanoTime rather than currentTimeMillis because it is monotonic and not affected by
diff --git a/test/metabase/transforms/util_test.clj b/test/metabase/transforms/util_test.clj
index bef57ed0788..25f71b7977c 100644
--- a/test/metabase/transforms/util_test.clj
+++ b/test/metabase/transforms/util_test.clj
@@ -825,3 +825,18 @@
(let [table (t2/select-one :model/Table (:id @synced-table))]
(is (nil? (:schema table))
"Table schema should remain nil when physical table isn't in default schema")))))))
+
+(deftest enabled-source-types-for-user-follows-the-advanced-permissions-feature-test
+ (testing "a data analyst sees the enabled source types only while advanced-permissions is available"
+ (mt/with-data-analyst-role! (mt/user->id :rasta)
+ (mt/with-current-user (mt/user->id :rasta)
+ (mt/when-ee-evailable
+ (mt/with-premium-features #{:transforms-basic :hosting :advanced-permissions}
+ (is (= #{"native" "mbql"} (transforms.u/enabled-source-types-for-user)))))
+ (testing "and none once the feature is gone"
+ (mt/with-premium-features #{:transforms-basic :hosting}
+ (is (nil? (transforms.u/enabled-source-types-for-user))))))))
+ (testing "a superuser is unaffected"
+ (mt/with-current-user (mt/user->id :crowberto)
+ (mt/with-premium-features #{:transforms-basic :hosting}
+ (is (= #{"native" "mbql"} (transforms.u/enabled-source-types-for-user)))))))
diff --git a/test/metabase/transforms_rest/api/transform_dag_run_test.clj b/test/metabase/transforms_rest/api/transform_dag_run_test.clj
index 196f3edc0c9..9bbbb50e683 100644
--- a/test/metabase/transforms_rest/api/transform_dag_run_test.clj
+++ b/test/metabase/transforms_rest/api/transform_dag_run_test.clj
@@ -5,7 +5,9 @@
[clojure.test :refer :all]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
- [metabase.transforms.test-util :refer [parse-instant utc-timestamp]]
+ [metabase.transforms.test-util :refer [parse-instant
+ utc-timestamp
+ with-transforms-api-users!]]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
@@ -20,8 +22,8 @@
(deftest get-dag-run-transform-runs-test
(testing "GET /api/transform-dag-run/:run-id/transform-runs"
(mt/with-temporary-raw-setting-values [transforms-enabled "true"]
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/Transform {seed-id :id} {:name "Seed"}
:model/Transform {t1-id :id} {:name "TR1"}
:model/TransformDagRun {run-id :id} {:source_transform_id seed-id
@@ -49,12 +51,12 @@
:transform_name "TR1"
:start_time (parse-instant "2025-08-01T10:00:00")}]
(testing "returns the member runs, ordered by start_time ascending"
- (let [response (mt/user-http-request :lucky :get 200
+ (let [response (mt/user-http-request user :get 200
(str "transform-dag-run/" run-id "/transform-runs"))]
(is (= [tr1-id tr2-id] (map :id response)))
(is (every? #(= run-id (:dag_run_id %)) response))))
(testing "response shape"
- (let [response (mt/user-http-request :lucky :get 200
+ (let [response (mt/user-http-request user :get 200
(str "transform-dag-run/" run-id "/transform-runs"))
tr1 (first (filter #(= tr1-id (:id %)) response))]
(is (= t1-id (:transform_id tr1)))
@@ -64,13 +66,13 @@
(is (= (utc-timestamp "2025-09-01T10:00:00") (:start_time tr1)))
(is (= (utc-timestamp "2025-09-01T10:02:00") (:end_time tr1)))))
(testing "returns 404 for a non-existent DAG run"
- (mt/user-http-request :lucky :get 404 "transform-dag-run/999999/transform-runs"))))))))
+ (mt/user-http-request user :get 404 "transform-dag-run/999999/transform-runs"))))))))
(deftest get-dag-run-transform-runs-deleted-seed-test
(testing "GET /api/transform-dag-run/:run-id/transform-runs still lists member runs after the seed transform is deleted"
(mt/with-temporary-raw-setting-values [transforms-enabled "true"]
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
;; source_transform_id nil as the SET NULL FK leaves it after the seed transform is deleted
(mt/with-temp [:model/Transform {t1-id :id} {:name "TR1"}
:model/TransformDagRun {run-id :id} {:source_transform_id nil
@@ -84,7 +86,7 @@
:run_method "manual"
:transform_name "TR1"
:start_time (parse-instant "2025-09-01T10:00:00")}]
- (let [response (mt/user-http-request :lucky :get 200 (str "transform-dag-run/" run-id "/transform-runs"))]
+ (let [response (mt/user-http-request user :get 200 (str "transform-dag-run/" run-id "/transform-runs"))]
(is (= [tr1-id] (map :id response))))
(testing "non-data-analysts are still denied"
(mt/user-http-request :rasta :get 403 (str "transform-dag-run/" run-id "/transform-runs")))))))))
@@ -107,8 +109,8 @@
(deftest cancel-dag-run-test
(testing "POST /api/transform-dag-run/:run-id/cancel"
(mt/with-temporary-raw-setting-values [transforms-enabled "true"]
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
;; canceling write-checks the seed transform, which needs the transforms permission on its source DB
(mt/with-db-perm-for-group! (perms-group/all-users) (mt/id) :perms/transforms :yes
(testing "cancels an active DAG run and requests cancellation of its active members"
@@ -125,7 +127,7 @@
:is_active true
:run_method "manual"
:start_time (parse-instant "2025-09-01T10:00:00")}]
- (mt/user-http-request :lucky :post 204 (str "transform-dag-run/" run-id "/cancel"))
+ (mt/user-http-request user :post 204 (str "transform-dag-run/" run-id "/cancel"))
(let [run (t2/select-one :model/TransformDagRun :id run-id)]
(is (= :canceled (:status run)))
(is (nil? (:is_active run)))
@@ -139,24 +141,24 @@
:status "succeeded"
:start_time (parse-instant "2025-09-01T10:00:00")
:end_time (parse-instant "2025-09-01T10:05:00")}]
- (mt/user-http-request :lucky :post 400 (str "transform-dag-run/" run-id "/cancel"))
+ (mt/user-http-request user :post 400 (str "transform-dag-run/" run-id "/cancel"))
(is (= :succeeded (:status (t2/select-one :model/TransformDagRun :id run-id)))
"a finished run is never resurrected into a canceled state")))
(testing "returns 404 for a non-existent DAG run"
- (mt/user-http-request :lucky :post 404 "transform-dag-run/999999/cancel"))))))))
+ (mt/user-http-request user :post 404 "transform-dag-run/999999/cancel"))))))))
(deftest cancel-dag-run-deleted-seed-test
(testing "POST /api/transform-dag-run/:run-id/cancel still works after the seed transform is deleted"
(mt/with-temporary-raw-setting-values [transforms-enabled "true"]
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/TransformDagRun {run-id :id} {:source_transform_id nil
:source_transform_name "Deleted Seed"
:direction "upstream"
:status "started"
:is_active true
:start_time (parse-instant "2025-09-01T10:00:00")}]
- (mt/user-http-request :lucky :post 204 (str "transform-dag-run/" run-id "/cancel"))
+ (mt/user-http-request user :post 204 (str "transform-dag-run/" run-id "/cancel"))
(is (= :canceled (:status (t2/select-one :model/TransformDagRun :id run-id))))))))))
(deftest cancel-dag-run-requires-write-permission-test
@@ -178,8 +180,8 @@
(deftest dag-transforms-preview-test
(testing "GET /api/transform/:id/dag-transforms"
(mt/with-temporary-raw-setting-values [transforms-enabled "true"]
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
;; consumer reads producer's output table via the stored table_dependencies column, so the
;; dependency graph resolves without parsing any queries
(mt/with-temp [:model/Transform {producer-id :id} {:name "Producer"
@@ -190,23 +192,23 @@
(testing "upstream preview returns the dependency closure in execution order"
(is (= [{:id producer-id :name "Producer"}
{:id consumer-id :name "Consumer"}]
- (mt/user-http-request :lucky :get 200 (str "transform/" consumer-id "/dag-transforms")
+ (mt/user-http-request user :get 200 (str "transform/" consumer-id "/dag-transforms")
:direction "upstream"))))
(testing "upstream preview of a transform with no dependencies is just itself"
(is (= [{:id producer-id :name "Producer"}]
- (mt/user-http-request :lucky :get 200 (str "transform/" producer-id "/dag-transforms")
+ (mt/user-http-request user :get 200 (str "transform/" producer-id "/dag-transforms")
:direction "upstream"))))
(testing "downstream preview returns the dependents closure in execution order"
(is (= [{:id producer-id :name "Producer"}
{:id consumer-id :name "Consumer"}]
- (mt/user-http-request :lucky :get 200 (str "transform/" producer-id "/dag-transforms")
+ (mt/user-http-request user :get 200 (str "transform/" producer-id "/dag-transforms")
:direction "downstream"))))
(testing "direction is required and validated"
- (mt/user-http-request :lucky :get 400 (str "transform/" consumer-id "/dag-transforms"))
- (mt/user-http-request :lucky :get 400 (str "transform/" consumer-id "/dag-transforms")
+ (mt/user-http-request user :get 400 (str "transform/" consumer-id "/dag-transforms"))
+ (mt/user-http-request user :get 400 (str "transform/" consumer-id "/dag-transforms")
:direction "sideways"))
(testing "returns 404 for a non-existent transform"
- (mt/user-http-request :lucky :get 404 "transform/999999/dag-transforms"
+ (mt/user-http-request user :get 404 "transform/999999/dag-transforms"
:direction "upstream"))))))))
;;; +------------------------------------------------------------------------------------------------------------------+
diff --git a/test/metabase/transforms_rest/api/transform_job_test.clj b/test/metabase/transforms_rest/api/transform_job_test.clj
index 9c510118537..2ba83fed511 100644
--- a/test/metabase/transforms_rest/api/transform_job_test.clj
+++ b/test/metabase/transforms_rest/api/transform_job_test.clj
@@ -8,69 +8,71 @@
[metabase.transforms.models.transform-tag]
[metabase.transforms.schedule :as transforms.schedule]
[metabase.transforms.test-util :refer [parse-instant
- utc-timestamp]]
+ utc-timestamp
+ with-transforms-api-users!]]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
(deftest create-job-test
(testing "POST /api/transform-job"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
- (mt/with-temp [:model/TransformTag tag1 {:name "test-tag-1"}
- :model/TransformTag tag2 {:name "test-tag-2"}]
- (testing "Creates job with valid data"
- (let [response (mt/user-http-request :lucky :post 200 "transform-job"
- {:name "Test Job"
- :description "Test Description"
- :schedule "0 0 0 * * ?"
- :ui_display_type "cron/builder"
- :tag_ids [(:id tag1) (:id tag2)]})]
- (is (some? (:id response)))
- (is (= "Test Job" (:name response)))
- (is (= "Test Description" (:description response)))
- (is (= "0 0 0 * * ?" (:schedule response)))
- (is (= "cron/builder" (:ui_display_type response)))
- (is (= (set [(:id tag1) (:id tag2)]) (set (:tag_ids response))))))
- (testing "Validates cron expression"
- (let [response (mt/user-http-request :lucky :post 400 "transform-job"
- {:name "Bad Cron Job"
- :schedule "invalid cron"})]
- (is (string? response))
- (is (re-find #"Invalid cron expression" response))))
- (testing "Validates tag IDs exist"
- (let [response (mt/user-http-request :lucky :post 400 "transform-job"
- {:name "Job with bad tags"
- :schedule "0 0 0 * * ?"
- :tag_ids [999999]})]
- (is (string? response))
- (is (re-find #"tag IDs do not exist" response)))))))))
+ (mt/with-model-cleanup [:model/TransformJob]
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
+ (mt/with-temp [:model/TransformTag tag1 {:name "test-tag-1"}
+ :model/TransformTag tag2 {:name "test-tag-2"}]
+ (testing "Creates job with valid data"
+ (let [response (mt/user-http-request user :post 200 "transform-job"
+ {:name "Test Job"
+ :description "Test Description"
+ :schedule "0 0 0 * * ?"
+ :ui_display_type "cron/builder"
+ :tag_ids [(:id tag1) (:id tag2)]})]
+ (is (some? (:id response)))
+ (is (= "Test Job" (:name response)))
+ (is (= "Test Description" (:description response)))
+ (is (= "0 0 0 * * ?" (:schedule response)))
+ (is (= "cron/builder" (:ui_display_type response)))
+ (is (= (set [(:id tag1) (:id tag2)]) (set (:tag_ids response))))))
+ (testing "Validates cron expression"
+ (let [response (mt/user-http-request user :post 400 "transform-job"
+ {:name "Bad Cron Job"
+ :schedule "invalid cron"})]
+ (is (string? response))
+ (is (re-find #"Invalid cron expression" response))))
+ (testing "Validates tag IDs exist"
+ (let [response (mt/user-http-request user :post 400 "transform-job"
+ {:name "Job with bad tags"
+ :schedule "0 0 0 * * ?"
+ :tag_ids [999999]})]
+ (is (string? response))
+ (is (re-find #"tag IDs do not exist" response))))))))))
(deftest get-job-test
(testing "GET /api/transform-job/:id"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/TransformTag tag {:name "test-tag"}
:model/TransformJob job {:name "Test Job"
:schedule "0 0 0 * * ?"}
:model/TransformJobTransformTag _ {:job_id (:id job) :tag_id (:id tag) :position 0}]
(testing "Returns job with hydrated fields"
- (let [response (mt/user-http-request :lucky :get 200 (str "transform-job/" (:id job)))]
+ (let [response (mt/user-http-request user :get 200 (str "transform-job/" (:id job)))]
(is (= (:id job) (:id response)))
(is (= "Test Job" (:name response)))
(is (= [(:id tag)] (:tag_ids response)))
(is (true? (:active response)))
(is (nil? (:last_run response)))))
(testing "Returns 404 for non-existent job"
- (mt/user-http-request :lucky :get 404 "transform-job/999999")))))))
+ (mt/user-http-request user :get 404 "transform-job/999999")))))))
(deftest get-job-transforms-test
(testing "GET /api/transform-job/:id/transforms"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic :hosting}
- (let [lucky-id (mt/user->id :lucky)]
- (mt/with-temp [:model/Transform {transform1-id :id} {:name "tr1" :creator_id lucky-id}
- :model/Transform {transform2-id :id} {:name "tr2" :creator_id lucky-id}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
+ (with-transforms-api-users! [user]
+ (let [user-id (mt/user->id user)]
+ (mt/with-temp [:model/Transform {transform1-id :id} {:name "tr1" :creator_id user-id}
+ :model/Transform {transform2-id :id} {:name "tr2" :creator_id user-id}
:model/Transform _transform3 {}
:model/TransformTag {tag-id :id} {}
:model/TransformTransformTag _ {:transform_id transform1-id :tag_id tag-id :position 0}
@@ -78,19 +80,19 @@
:model/TransformJob {job-id :id} {}
:model/TransformJobTransformTag _ {:job_id job-id :tag_id tag-id :position 0}]
(testing "Returns the transforms of the job"
- (let [response (mt/user-http-request :lucky :get 200 (str "transform-job/" job-id "/transforms"))]
+ (let [response (mt/user-http-request user :get 200 (str "transform-job/" job-id "/transforms"))]
(is (=? [{:id transform1-id, :name "tr1"} {:id transform2-id, :name "tr2"}]
(sort-by :id response)))
(testing "Response hydrates creator"
(is (every? #(map? (:creator %)) response))
- (is (every? #(= lucky-id (get-in % [:creator :id])) response)))))
+ (is (every? #(= user-id (get-in % [:creator :id])) response)))))
(testing "Returns 404 for non-existent job"
- (mt/user-http-request :lucky :get 404 "transform-job/999999/transforms"))))))))
+ (mt/user-http-request user :get 404 "transform-job/999999/transforms"))))))))
(deftest list-jobs-test
(testing "GET /api/transform-job"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(let [at-5-second-schedule "5 * * * * ?"]
(mt/with-temp [:model/TransformTag {t1-id :id} {:name "test-tag"}
:model/TransformTag {t2-id :id} {:name "second test-tag"}
@@ -111,29 +113,29 @@
returned-jobs (fn [response] (into [] (filter (comp our-job-ids :id)) response))
returned-job-ids (fn [response] (into #{} (keep (comp our-job-ids :id)) response))]
(testing "listing without filtering"
- (let [response (mt/user-http-request :lucky :get 200 "transform-job")]
+ (let [response (mt/user-http-request user :get 200 "transform-job")]
(is (= our-job-ids (returned-job-ids response)))))
(testing "filtering by tag_ids"
- (let [response (mt/user-http-request :lucky :get 200 "transform-job" :tag-ids [t2-id])]
+ (let [response (mt/user-http-request user :get 200 "transform-job" :tag-ids [t2-id])]
(is (=? [{:schedule "0 0 0 * * ?"
:tag_ids [t2-id]
:name "Job 3"
:id j3-id}]
(returned-jobs response)))))
(testing "filtering by last_run_start_time"
- (let [response (mt/user-http-request :lucky :get 200 "transform-job" :last-run-start-time "2025-08-26~")]
+ (let [response (mt/user-http-request user :get 200 "transform-job" :last-run-start-time "2025-08-26~")]
(is (= #{j2-id} (returned-job-ids response)))))
(testing "filtering by last_run_statuses"
- (let [response (mt/user-http-request :lucky :get 200 "transform-job" :last-run-statuses ["started" "succeeded"])]
+ (let [response (mt/user-http-request user :get 200 "transform-job" :last-run-statuses ["started" "succeeded"])]
(is (= #{j2-id} (returned-job-ids response)))))
(testing "filtering by last_run_end_time without scheduled job"
- (let [response (mt/user-http-request :lucky :get 200 "transform-job" :next-run-start-time "2025-08-26~")]
+ (let [response (mt/user-http-request user :get 200 "transform-job" :next-run-start-time "2025-08-26~")]
(is (= #{} (returned-job-ids response)))))
(testing "filtering by last_run_end_time with scheduled job"
(mt/with-temp-scheduler!
(transforms.schedule/initialize-job! {:id j1-id, :schedule at-5-second-schedule})
(try
- (let [response (mt/user-http-request :lucky :get 200 "transform-job" :next-run-start-time "2025-08-27~")]
+ (let [response (mt/user-http-request user :get 200 "transform-job" :next-run-start-time "2025-08-27~")]
(is (=? [{:id j1-id
:last_run {:job_id j1-id
:run_method "cron"
@@ -149,46 +151,47 @@
(deftest update-job-test
(testing "PUT /api/transform-job/:id"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
- (mt/with-temp [:model/TransformTag tag1 {:name "tag-1"}
- :model/TransformTag tag2 {:name "tag-2"}]
- (let [job (mt/user-http-request :lucky :post 200 "transform-job"
- {:name "Original" :schedule "0 0 0 * * ?"})]
- (testing "Updates job fields"
- (let [response (mt/user-http-request :lucky :put 200 (str "transform-job/" (:id job))
- {:name "Updated"
- :description "New Description"
- :schedule "0 0 */2 * * ?"
- :tag_ids [(:id tag1) (:id tag2)]})]
- (is (= "Updated" (:name response)))
- (is (= "New Description" (:description response)))
- (is (= "0 0 */2 * * ?" (:schedule response)))
- (is (= (set [(:id tag1) (:id tag2)]) (set (:tag_ids response))))))
- (testing "Validates cron expression"
- (let [response (mt/user-http-request :lucky :put 400 (str "transform-job/" (:id job))
- {:schedule "invalid"})]
- (is (string? response))
- (is (re-find #"Invalid cron expression" response))))))))))
+ (mt/with-model-cleanup [:model/TransformJob]
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
+ (mt/with-temp [:model/TransformTag tag1 {:name "tag-1"}
+ :model/TransformTag tag2 {:name "tag-2"}]
+ (let [job (mt/user-http-request user :post 200 "transform-job"
+ {:name "Original" :schedule "0 0 0 * * ?"})]
+ (testing "Updates job fields"
+ (let [response (mt/user-http-request user :put 200 (str "transform-job/" (:id job))
+ {:name "Updated"
+ :description "New Description"
+ :schedule "0 0 */2 * * ?"
+ :tag_ids [(:id tag1) (:id tag2)]})]
+ (is (= "Updated" (:name response)))
+ (is (= "New Description" (:description response)))
+ (is (= "0 0 */2 * * ?" (:schedule response)))
+ (is (= (set [(:id tag1) (:id tag2)]) (set (:tag_ids response))))))
+ (testing "Validates cron expression"
+ (let [response (mt/user-http-request user :put 400 (str "transform-job/" (:id job))
+ {:schedule "invalid"})]
+ (is (string? response))
+ (is (re-find #"Invalid cron expression" response)))))))))))
(deftest update-job-active-test
(testing "PUT /api/transform-job/:id can toggle the active flag"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/TransformJob job {:name "Toggle Me" :schedule "0 0 0 * * ?"}]
(testing "Defaults to true"
- (is (true? (:active (mt/user-http-request :lucky :get 200 (str "transform-job/" (:id job)))))))
+ (is (true? (:active (mt/user-http-request user :get 200 (str "transform-job/" (:id job)))))))
(testing "Can be set to false"
- (let [response (mt/user-http-request :lucky :put 200 (str "transform-job/" (:id job))
+ (let [response (mt/user-http-request user :put 200 (str "transform-job/" (:id job))
{:active false})]
(is (false? (:active response))))
- (is (false? (:active (mt/user-http-request :lucky :get 200 (str "transform-job/" (:id job))))))
+ (is (false? (:active (mt/user-http-request user :get 200 (str "transform-job/" (:id job))))))
(is (false? (t2/select-one-fn :active :model/TransformJob :id (:id job)))))
(testing "Can be set back to true"
- (let [response (mt/user-http-request :lucky :put 200 (str "transform-job/" (:id job))
+ (let [response (mt/user-http-request user :put 200 (str "transform-job/" (:id job))
{:active true})]
(is (true? (:active response))))
- (is (true? (:active (mt/user-http-request :lucky :get 200 (str "transform-job/" (:id job))))))
+ (is (true? (:active (mt/user-http-request user :get 200 (str "transform-job/" (:id job))))))
(is (true? (t2/select-one-fn :active :model/TransformJob :id (:id job))))))))))
(deftest update-all-jobs-active-test
@@ -231,13 +234,13 @@
(mt/user-http-request :rasta :put 403 "transform-job/active" {:active false}))
(testing "data analysts are also rejected"
(mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
(mt/user-http-request :lucky :put 403 "transform-job/active" {:active false}))))))
(deftest active-trigger-lifecycle-test
(testing "PUT /api/transform-job/:id syncs the Quartz trigger with :active"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(mt/with-temp-scheduler!
(mt/with-temp [:model/TransformJob job {:name "Triggered" :schedule "0 0 0 * * ?"}]
;; with-temp doesn't run the API's create handler, so seed the trigger ourselves to
@@ -245,23 +248,23 @@
(transforms.schedule/initialize-job! job)
(try
(testing "Deactivating removes the trigger"
- (mt/user-http-request :lucky :put 200 (str "transform-job/" (:id job))
+ (mt/user-http-request user :put 200 (str "transform-job/" (:id job))
{:active false})
(is (nil? (transforms.schedule/existing-trigger (:id job)))))
(testing "Reactivating recreates the trigger from the stored schedule"
- (mt/user-http-request :lucky :put 200 (str "transform-job/" (:id job))
+ (mt/user-http-request user :put 200 (str "transform-job/" (:id job))
{:active true})
(is (some? (transforms.schedule/existing-trigger (:id job)))))
(testing "Schedule edits on an inactive job do not resurrect the trigger"
- (mt/user-http-request :lucky :put 200 (str "transform-job/" (:id job))
+ (mt/user-http-request user :put 200 (str "transform-job/" (:id job))
{:active false})
- (mt/user-http-request :lucky :put 200 (str "transform-job/" (:id job))
+ (mt/user-http-request user :put 200 (str "transform-job/" (:id job))
{:schedule "0 0 1 * * ?"})
(is (nil? (transforms.schedule/existing-trigger (:id job))))
(testing "but the new schedule is persisted in the DB"
(is (= "0 0 1 * * ?" (t2/select-one-fn :schedule :model/TransformJob :id (:id job)))))
(testing "and reactivating builds a trigger using the updated schedule"
- (mt/user-http-request :lucky :put 200 (str "transform-job/" (:id job))
+ (mt/user-http-request user :put 200 (str "transform-job/" (:id job))
{:active true})
(is (= "0 0 1 * * ?" (:schedule (transforms.schedule/existing-trigger (:id job)))))))
(finally
@@ -287,31 +290,31 @@
(deftest update-job-remove-tags-test
(testing "PUT /api/transform-job/:id"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(testing "should be able to remove all tags from a job"
(mt/with-temp [:model/TransformTag tag {}
:model/TransformJob job {}
:model/TransformJobTransformTag _ {:job_id (:id job) :tag_id (:id tag) :position 0}]
- (let [response (mt/user-http-request :lucky :put 200 (str "transform-job/" (:id job))
+ (let [response (mt/user-http-request user :put 200 (str "transform-job/" (:id job))
{:tag_ids []})]
(is (= [] (:tag_ids response))))))))))
(deftest delete-job-test
(testing "DELETE /api/transform-job/:id"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/TransformJob job {:name "To Delete" :schedule "0 0 0 * * ?"}]
(testing "Deletes job"
- (mt/user-http-request :lucky :delete 204 (str "transform-job/" (:id job)))
+ (mt/user-http-request user :delete 204 (str "transform-job/" (:id job)))
(is (nil? (t2/select-one :model/TransformJob :id (:id job)))))
(testing "Returns 404 for non-existent job"
- (mt/user-http-request :lucky :delete 404 "transform-job/999999")))))))
+ (mt/user-http-request user :delete 404 "transform-job/999999")))))))
(deftest execute-job-test
(testing "POST /api/transform-job/:id/run"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(testing "returns the created run id"
(mt/with-model-cleanup [:model/TransformJobRun]
(mt/with-temp [:model/TransformTag tag {:name "execute-test-tag"}
@@ -323,7 +326,7 @@
(mt/with-dynamic-fn-redefs [jobs/run-transforms! (fn [run-id & _]
(deliver called run-id)
{::jobs/status :succeeded})]
- (let [response (mt/user-http-request :lucky :post 202 (str "transform-job/" (:id job) "/run"))]
+ (let [response (mt/user-http-request user :post 202 (str "transform-job/" (:id job) "/run"))]
(is (= "Job run started" (:message response)))
(is (pos-int? (:job_run_id response)))
(is (= (:job_run_id response) (deref called 5000 ::timed-out)))
@@ -334,7 +337,7 @@
(is (= (:entity_id job) (:job_entity_id run)))))))))))
(testing "returns a nil run id when the job has no transforms"
(mt/with-temp [:model/TransformJob job {:name "Empty Job" :schedule "0 0 0 * * ?"}]
- (let [response (mt/user-http-request :lucky :post 202 (str "transform-job/" (:id job) "/run"))]
+ (let [response (mt/user-http-request user :post 202 (str "transform-job/" (:id job) "/run"))]
(is (nil? (:job_run_id response))))))
(testing "returns a nil run id when the job is already running"
(mt/with-temp [:model/TransformJob job {:name "Busy Job" :schedule "0 0 0 * * ?"}
@@ -343,7 +346,7 @@
:is_active true
:run_method "manual"
:start_time (parse-instant "2025-09-01T10:00:00")}]
- (let [response (mt/user-http-request :lucky :post 202 (str "transform-job/" (:id job) "/run"))]
+ (let [response (mt/user-http-request user :post 202 (str "transform-job/" (:id job) "/run"))]
(is (nil? (:job_run_id response))))))))))
(deftest permissions-test
@@ -365,8 +368,8 @@
(deftest get-job-runs-test
(testing "GET /api/transform-job/:job-id/runs"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/TransformJob {job-id :id} {:name "Run History Job" :schedule "0 0 0 * * ?"}
:model/TransformJobRun {r1-id :id} {:job_id job-id
:status "succeeded"
@@ -385,7 +388,7 @@
:start_time (parse-instant "2025-09-03T10:00:00")
:is_active true}]
(testing "returns paginated runs for a job"
- (let [response (mt/user-http-request :lucky :get 200 (str "transform-job/" job-id "/runs"))
+ (let [response (mt/user-http-request user :get 200 (str "transform-job/" job-id "/runs"))
ids (into #{} (map :id) (:data response))]
(is (contains? ids r1-id))
(is (contains? ids r2-id))
@@ -394,32 +397,32 @@
(is (pos? (:limit response)))
(is (integer? (:offset response)))))
(testing "filters by status"
- (let [response (mt/user-http-request :lucky :get 200
+ (let [response (mt/user-http-request user :get 200
(str "transform-job/" job-id "/runs")
:status "failed")]
(is (= [r2-id] (map :id (:data response))))
(is (= 1 (:total response)))))
(testing "filters by run-method"
- (let [response (mt/user-http-request :lucky :get 200
+ (let [response (mt/user-http-request user :get 200
(str "transform-job/" job-id "/runs")
:run-method "manual")]
(is (= [r2-id] (map :id (:data response))))
(is (= 1 (:total response)))))
(testing "filters by start-time"
- (let [response (mt/user-http-request :lucky :get 200
+ (let [response (mt/user-http-request user :get 200
(str "transform-job/" job-id "/runs")
:start-time "2025-09-01")]
(is (= [r1-id] (map :id (:data response))))
(is (= 1 (:total response)))))
(testing "sorts by start_time asc"
- (let [response (mt/user-http-request :lucky :get 200
+ (let [response (mt/user-http-request user :get 200
(str "transform-job/" job-id "/runs")
:sort-column "start_time"
:sort-direction "asc")]
(is (= r1-id (-> response :data first :id)))))
(testing "response shape"
(let [run (first (filter #(= r1-id (:id %))
- (:data (mt/user-http-request :lucky :get 200
+ (:data (mt/user-http-request user :get 200
(str "transform-job/" job-id "/runs")))))]
(is (= job-id (:job_id run)))
(is (= "cron" (:run_method run)))
@@ -427,14 +430,14 @@
(is (= (utc-timestamp "2025-09-01T10:00:00") (:start_time run)))
(is (= (utc-timestamp "2025-09-01T10:05:00") (:end_time run)))))
(testing "returns 404 for non-existent job"
- (mt/user-http-request :lucky :get 404 "transform-job/999999/runs"))
+ (mt/user-http-request user :get 404 "transform-job/999999/runs"))
(testing "does not return runs from other jobs"
(mt/with-temp [:model/TransformJob {other-job-id :id} {:name "Other Job" :schedule "0 0 0 * * ?"}
:model/TransformJobRun _ {:job_id other-job-id
:status "succeeded"
:run_method "cron"
:start_time (parse-instant "2025-09-04T10:00:00")}]
- (let [response (mt/user-http-request :lucky :get 200
+ (let [response (mt/user-http-request user :get 200
(str "transform-job/" job-id "/runs"))]
(is (every? #(= job-id (:job_id %)) (:data response)))))))))))
@@ -450,16 +453,16 @@
(deftest get-job-run-transform-runs-test
(testing "GET /api/transform-job/:job-id/runs/:run-id/transform-runs"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
- (let [lucky-id (mt/user->id :lucky)]
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
+ (let [user-id (mt/user->id user)]
(mt/with-temp [:model/TransformJob {job-id :id} {:name "Drill-down Job" :schedule "0 0 0 * * ?"}
:model/TransformJobRun {run-id :id} {:job_id job-id
:status "failed"
:run_method "cron"
:start_time (parse-instant "2025-09-01T10:00:00")}
- :model/Transform {t1-id :id} {:name "TR1" :creator_id lucky-id}
- :model/Transform {t2-id :id} {:name "TR2" :creator_id lucky-id}
+ :model/Transform {t1-id :id} {:name "TR1" :creator_id user-id}
+ :model/Transform {t2-id :id} {:name "TR2" :creator_id user-id}
:model/TransformRun {tr1-id :id} {:transform_id t1-id
:job_run_id run-id
:status "succeeded"
@@ -482,12 +485,12 @@
:transform_name "TR1"
:start_time (parse-instant "2025-08-01T10:00:00")}]
(testing "returns transform runs for the job run"
- (let [response (mt/user-http-request :lucky :get 200
+ (let [response (mt/user-http-request user :get 200
(str "transform-job/" job-id "/runs/" run-id "/transform-runs"))]
(is (= #{tr1-id tr2-id} (into #{} (map :id) response)))
(is (= 2 (count response)))))
(testing "response shape"
- (let [response (mt/user-http-request :lucky :get 200
+ (let [response (mt/user-http-request user :get 200
(str "transform-job/" job-id "/runs/" run-id "/transform-runs"))
tr1 (first (filter #(= tr1-id (:id %)) response))]
(is (= t1-id (:transform_id tr1)))
@@ -496,7 +499,7 @@
(is (= "succeeded" (:status tr1)))
(is (= "TR1" (:transform_name tr1)))))
(testing "orders by start_time ascending"
- (let [response (mt/user-http-request :lucky :get 200
+ (let [response (mt/user-http-request user :get 200
(str "transform-job/" job-id "/runs/" run-id "/transform-runs"))]
(is (= [tr1-id tr2-id] (map :id response)))))
(testing "does not include transform runs from other job runs"
@@ -510,18 +513,18 @@
:run_method "cron"
:transform_name "TR1"
:start_time (parse-instant "2025-09-02T10:00:00")}]
- (let [response (mt/user-http-request :lucky :get 200
+ (let [response (mt/user-http-request user :get 200
(str "transform-job/" job-id "/runs/" run-id "/transform-runs"))]
(is (every? #(= run-id (:job_run_id %)) response)))))
(testing "returns 404 for non-existent job"
- (mt/user-http-request :lucky :get 404
+ (mt/user-http-request user :get 404
(str "transform-job/999999/runs/" run-id "/transform-runs")))
(testing "returns 404 for non-existent run"
- (mt/user-http-request :lucky :get 404
+ (mt/user-http-request user :get 404
(str "transform-job/" job-id "/runs/999999/transform-runs")))
(testing "returns 404 when run does not belong to job"
(mt/with-temp [:model/TransformJob {other-job-id :id} {:name "Other" :schedule "0 0 0 * * ?"}]
- (mt/user-http-request :lucky :get 404
+ (mt/user-http-request user :get 404
(str "transform-job/" other-job-id "/runs/" run-id "/transform-runs"))))))))))
(deftest get-job-run-transform-runs-requires-auth-test
@@ -541,8 +544,8 @@
(deftest cancel-job-run-test
(testing "POST /api/transform-job/:job-id/runs/:run-id/cancel"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(testing "cancels an active job run and requests cancellation of its active members"
(mt/with-temp [:model/TransformJob {job-id :id} {:name "Cancel Job" :schedule "0 0 0 * * ?"}
:model/TransformJobRun {run-id :id} {:job_id job-id
@@ -558,7 +561,7 @@
:run_method "cron"
:start_time (parse-instant "2025-09-01T10:00:00")
:end_time nil}]
- (mt/user-http-request :lucky :post 204 (str "transform-job/" job-id "/runs/" run-id "/cancel"))
+ (mt/user-http-request user :post 204 (str "transform-job/" job-id "/runs/" run-id "/cancel"))
(let [run (t2/select-one :model/TransformJobRun :id run-id)]
(is (= :canceled (:status run)))
(is (nil? (:is_active run)))
@@ -572,7 +575,7 @@
:run_method "cron"
:start_time (parse-instant "2025-09-01T10:00:00")
:end_time (parse-instant "2025-09-01T10:05:00")}]
- (mt/user-http-request :lucky :post 400 (str "transform-job/" job-id "/runs/" run-id "/cancel"))
+ (mt/user-http-request user :post 400 (str "transform-job/" job-id "/runs/" run-id "/cancel"))
(is (= :succeeded (:status (t2/select-one :model/TransformJobRun :id run-id)))
"a finished run is never resurrected into a canceled state")))
(testing "returns 404 for a non-existent job or run, or a run of another job"
@@ -583,9 +586,9 @@
:is_active true
:run_method "manual"
:start_time (parse-instant "2025-09-01T10:00:00")}]
- (mt/user-http-request :lucky :post 404 (str "transform-job/999999/runs/" run-id "/cancel"))
- (mt/user-http-request :lucky :post 404 (str "transform-job/" job-id "/runs/999999/cancel"))
- (mt/user-http-request :lucky :post 404 (str "transform-job/" other-job-id "/runs/" run-id "/cancel"))))))))
+ (mt/user-http-request user :post 404 (str "transform-job/999999/runs/" run-id "/cancel"))
+ (mt/user-http-request user :post 404 (str "transform-job/" job-id "/runs/999999/cancel"))
+ (mt/user-http-request user :post 404 (str "transform-job/" other-job-id "/runs/" run-id "/cancel"))))))))
(deftest cancel-job-run-requires-write-permission-test
(testing "POST /api/transform-job/:job-id/runs/:run-id/cancel requires write permission on the job"
diff --git a/test/metabase/transforms_rest/api/transform_runs_test.clj b/test/metabase/transforms_rest/api/transform_runs_test.clj
index 6ce27707cf8..861313a538f 100644
--- a/test/metabase/transforms_rest/api/transform_runs_test.clj
+++ b/test/metabase/transforms_rest/api/transform_runs_test.clj
@@ -5,7 +5,9 @@
[clojure.test :refer :all]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.test :as mt]
- [metabase.transforms.test-util :refer [parse-instant utc-timestamp]]
+ [metabase.transforms.test-util :refer [parse-instant
+ utc-timestamp
+ with-transforms-api-users!]]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
@@ -60,16 +62,16 @@
[[binding] & body]
`(do-with-runs-fixture! (fn [~binding] ~@body)))
-(defn- get-runs [& params]
+(defn- get-runs [user & params]
;; a large limit so fixture rows can't be pushed off the page by rows other tests created
- (apply mt/user-http-request :lucky :get 200 "transform/runs" :limit 1000 params))
+ (apply mt/user-http-request user :get 200 "transform/runs" :limit 1000 params))
(deftest unified-runs-listing-test
(testing "GET /api/transform/runs"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(with-runs-fixture! [{:keys [ta-id tb-id job-id job-run-id dag-run-id member-a-id member-b-id standalone-id]}]
- (let [response (get-runs)
+ (let [response (get-runs user)
rows (rows-by-type response)]
(testing "returns all three kinds of root run"
(is (contains? rows ["job" job-run-id]))
@@ -112,70 +114,70 @@
(deftest unified-runs-filters-test
(testing "GET /api/transform/runs filters"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(with-runs-fixture! [{:keys [ta-id tb-id job-run-id dag-run-id standalone-id]}]
(testing "types= restricts the kinds returned"
- (let [response (get-runs :types ["job"])]
+ (let [response (get-runs user :types ["job"])]
(is (every? #(= "job" (:run_type %)) (:data response)))
(is (contains? (rows-by-type response) ["job" job-run-id])))
- (let [response (get-runs :types ["dag" "transform"])
+ (let [response (get-runs user :types ["dag" "transform"])
rows (rows-by-type response)]
(is (every? #(contains? #{"dag" "transform"} (:run_type %)) (:data response)))
(is (contains? rows ["dag" dag-run-id]))
(is (contains? rows ["transform" standalone-id]))))
(testing "statuses= filters across all kinds, matching any of the given statuses"
- (let [rows (rows-by-type (get-runs :statuses ["failed"]))]
+ (let [rows (rows-by-type (get-runs user :statuses ["failed"]))]
(is (contains? rows ["dag" dag-run-id]))
(is (not (contains? rows ["job" job-run-id])))
(is (not (contains? rows ["transform" standalone-id]))))
- (let [rows (rows-by-type (get-runs :statuses ["failed" "succeeded"]))]
+ (let [rows (rows-by-type (get-runs user :statuses ["failed" "succeeded"]))]
(is (contains? rows ["dag" dag-run-id]))
(is (contains? rows ["job" job-run-id]))
(is (contains? rows ["transform" standalone-id]))))
(testing "run-methods= filters by trigger"
- (let [rows (rows-by-type (get-runs :run-methods ["cron"]))]
+ (let [rows (rows-by-type (get-runs user :run-methods ["cron"]))]
(is (contains? rows ["job" job-run-id]))
(is (not (contains? rows ["dag" dag-run-id])))
(is (not (contains? rows ["transform" standalone-id]))))
- (let [rows (rows-by-type (get-runs :run-methods ["manual"]))]
+ (let [rows (rows-by-type (get-runs user :run-methods ["manual"]))]
(is (contains? rows ["dag" dag-run-id]))
(is (contains? rows ["transform" standalone-id]))
(is (not (contains? rows ["job" job-run-id])))))
(testing "transform-ids= returns runs that ran any of the transforms"
(testing "job runs with a member run of it, plus its standalone runs"
- (let [rows (rows-by-type (get-runs :transform-ids [ta-id]))]
+ (let [rows (rows-by-type (get-runs user :transform-ids [ta-id]))]
(is (contains? rows ["job" job-run-id]))
(is (contains? rows ["transform" standalone-id]))
(is (not (contains? rows ["dag" dag-run-id])))))
(testing "DAG runs with a member run of it"
- (let [rows (rows-by-type (get-runs :transform-ids [tb-id]))]
+ (let [rows (rows-by-type (get-runs user :transform-ids [tb-id]))]
(is (contains? rows ["dag" dag-run-id]))
(is (not (contains? rows ["job" job-run-id])))
(is (not (contains? rows ["transform" standalone-id])))))
(testing "multiple ids match as a logical OR"
- (let [rows (rows-by-type (get-runs :transform-ids [ta-id tb-id]))]
+ (let [rows (rows-by-type (get-runs user :transform-ids [ta-id tb-id]))]
(is (contains? rows ["job" job-run-id]))
(is (contains? rows ["dag" dag-run-id]))
(is (contains? rows ["transform" standalone-id])))))
(testing "start-time= filters by run start"
- (let [rows (rows-by-type (get-runs :start-time "2025-09-03"))]
+ (let [rows (rows-by-type (get-runs user :start-time "2025-09-03"))]
(is (contains? rows ["transform" standalone-id]))
(is (not (contains? rows ["job" job-run-id])))
(is (not (contains? rows ["dag" dag-run-id])))))
(testing "end-time= filters by run end"
- (let [rows (rows-by-type (get-runs :end-time "2025-09-01"))]
+ (let [rows (rows-by-type (get-runs user :end-time "2025-09-01"))]
(is (contains? rows ["job" job-run-id]))
(is (not (contains? rows ["dag" dag-run-id])))
(is (not (contains? rows ["transform" standalone-id]))))))))))
(deftest unified-runs-sorting-and-pagination-test
(testing "GET /api/transform/runs sorting and pagination"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(with-runs-fixture! [{:keys [job-run-id dag-run-id standalone-id]}]
(testing "sort-column=start_time asc keeps our rows in chronological order"
- (let [response (get-runs :sort-column "start_time" :sort-direction "asc")
+ (let [response (get-runs user :sort-column "start_time" :sort-direction "asc")
ours (filter (comp #{["job" job-run-id] ["dag" dag-run-id] ["transform" standalone-id]}
(juxt :run_type :id))
(:data response))]
@@ -183,7 +185,7 @@
(is (= [["job" job-run-id] ["dag" dag-run-id] ["transform" standalone-id]]
(map (juxt :run_type :id) ours)))))
(testing "limit/offset are honored"
- (let [response (mt/user-http-request :lucky :get 200 "transform/runs" :limit 1 :offset 0)]
+ (let [response (mt/user-http-request user :get 200 "transform/runs" :limit 1 :offset 0)]
(is (= 1 (count (:data response))))
(is (= 1 (:limit response)))
(is (= 0 (:offset response)))
@@ -191,8 +193,8 @@
(deftest unified-runs-deleted-entities-test
(testing "GET /api/transform/runs keeps rows for deleted jobs/transforms, naming them from the run-start snapshot"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/Transform {ta-id :id} {:name "Doomed Transform"}
:model/Transform {tb-id :id} {:name "Doomed Seed"}
:model/TransformJob {job-id :id} {:name "Doomed Job" :schedule "0 0 0 * * ?"}
@@ -213,7 +215,7 @@
:start_time (parse-instant "2025-09-03T10:00:00")}]
(t2/delete! :model/TransformJob :id job-id)
(t2/delete! :model/Transform :id [:in [ta-id tb-id]])
- (let [rows (rows-by-type (get-runs))]
+ (let [rows (rows-by-type (get-runs user))]
(testing "job run survives (job_id has no FK) with its snapshot name"
(let [row (rows ["job" job-run-id])]
(is (some? row))
@@ -237,19 +239,19 @@
(deftest get-run-by-id-test
(testing "GET /api/transform/run/:run-id returns the run when the caller can read its transform"
;; :hosting so the transforms-enabled setting defaults to true, as in create-transform-test
- (mt/with-premium-features #{:transforms-basic :hosting}
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (mt/with-premium-features #{:transforms-basic :hosting :advanced-permissions}
+ (with-transforms-api-users! [user]
;; native access on the test DB makes the default (native-source) temp transform readable
(mt/with-db-perms-for-group! (perms-group/all-users) (mt/id) {:perms/create-queries :query-builder-and-native}
(mt/with-temp [:model/Transform {transform-id :id} {:name "Readable Transform"}
:model/TransformRun {run-id :id} {:transform_id transform-id}]
- (let [response (mt/user-http-request :lucky :get 200 (str "transform/run/" run-id))]
+ (let [response (mt/user-http-request user :get 200 (str "transform/run/" run-id))]
(is (= run-id (:id response)))
(is (= transform-id (get-in response [:transform :id]))))))))))
(deftest get-run-by-id-requires-transform-read-test
(testing "GET /api/transform/run/:run-id read-checks the run's transform (SEC-1180)"
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:transforms-basic :hosting :advanced-permissions}
(mt/with-data-analyst-role! (mt/user->id :lucky)
(mt/with-temp [:model/Database {db-id :id} {}
:model/Transform {transform-id :id} {:name "Hidden Transform"
diff --git a/test/metabase/transforms_rest/api/transform_tag_test.clj b/test/metabase/transforms_rest/api/transform_tag_test.clj
index bab42d6a4a6..f3ceae89fe0 100644
--- a/test/metabase/transforms_rest/api/transform_tag_test.clj
+++ b/test/metabase/transforms_rest/api/transform_tag_test.clj
@@ -5,6 +5,7 @@
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
[metabase.transforms.models.transform-tag]
+ [metabase.transforms.test-util :refer [with-transforms-api-users!]]
[metabase.util :as u]
[toucan2.core :as t2]))
@@ -12,11 +13,11 @@
(deftest create-tag-test
(testing "POST /api/transform-tag"
- (mt/with-premium-features #{:transforms-basic}
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(testing "Creates a new tag with valid name"
(let [tag-name (str "test-tag-" (u/generate-nano-id))
- response (mt/user-http-request :lucky :post 200 "transform-tag"
+ response (mt/user-http-request user :post 200 "transform-tag"
{:name tag-name})]
(try
(is (some? (:id response)))
@@ -28,65 +29,65 @@
(t2/delete! :model/TransformTag :id (:id response))))))
(testing "Returns 400 for duplicate tag name"
(mt/with-temp [:model/TransformTag tag {}]
- (is (string? (mt/user-http-request :lucky :post 400 "transform-tag"
+ (is (string? (mt/user-http-request user :post 400 "transform-tag"
{:name (:name tag)}))
"Should return 400 with error message for duplicate name")))
(testing "Returns validation error for empty name"
- (let [response (mt/user-http-request :lucky :post "transform-tag"
+ (let [response (mt/user-http-request user :post "transform-tag"
{:name ""})]
(is (:errors response) "Should return validation errors for empty name"))
- (let [response (mt/user-http-request :lucky :post "transform-tag"
+ (let [response (mt/user-http-request user :post "transform-tag"
{:name " "})]
(is (:errors response) "Should return validation errors for blank name")))))))
(deftest update-tag-test
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
(testing "PUT /api/transform-tag/:tag-id"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(testing "Updates tag name successfully"
(mt/with-temp [:model/TransformTag tag {}]
(let [updated-name (str "updated-" (u/generate-nano-id))
- response (mt/user-http-request :lucky :put 200
+ response (mt/user-http-request user :put 200
(str "transform-tag/" (:id tag))
{:name updated-name})]
(is (= (:id tag) (:id response)))
(is (= updated-name (:name response))))))
(testing "Returns 404 for non-existent tag"
(is (= "Not found."
- (mt/user-http-request :lucky :put 404
+ (mt/user-http-request user :put 404
"transform-tag/999999"
{:name "new-name"}))))
(testing "Returns 400 when updating to duplicate name"
(mt/with-temp [:model/TransformTag existing-tag {}
:model/TransformTag tag-to-update {}]
- (is (string? (mt/user-http-request :lucky :put 400
+ (is (string? (mt/user-http-request user :put 400
(str "transform-tag/" (:id tag-to-update))
{:name (:name existing-tag)}))
"Should return 400 with error message for duplicate name")))))))
(deftest delete-tag-test
(testing "DELETE /api/transform-tag/:tag-id"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(testing "Deletes tag successfully"
(mt/with-temp [:model/TransformTag tag {}]
(is (t2/exists? :model/TransformTag :id (:id tag)))
- (mt/user-http-request :lucky :delete 204 (str "transform-tag/" (:id tag)))
+ (mt/user-http-request user :delete 204 (str "transform-tag/" (:id tag)))
(is (not (t2/exists? :model/TransformTag :id (:id tag))))))
(testing "Returns 404 for non-existent tag"
(is (= "Not found."
- (mt/user-http-request :lucky :delete 404
+ (mt/user-http-request user :delete 404
"transform-tag/999999"))))))))
(deftest list-tags-test
(testing "GET /api/transform-tag"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
+ (with-transforms-api-users! [user]
(testing "Returns all tags ordered by name"
(mt/with-temp [:model/TransformTag tag1 {:name "tag 1"}
:model/TransformTag tag2 {:name "tag 3"}
:model/TransformTag tag3 {:name "tag 2"}]
- (let [response (mt/user-http-request :lucky :get 200 "transform-tag")
+ (let [response (mt/user-http-request user :get 200 "transform-tag")
tag-names (map :name response)]
;; Should include our test tags
(is (some #(= (:name tag1) %) tag-names))
@@ -97,7 +98,7 @@
(deftest permissions-test
(testing "Transform tag endpoints require data-analyst permissions"
- (mt/with-premium-features #{:transforms-basic}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic}
(testing "POST /api/transform-tag"
(is (string? (mt/user-http-request :rasta :post 403 "transform-tag"
{:name "test"}))))
diff --git a/test/metabase/transforms_rest/api/transform_test.clj b/test/metabase/transforms_rest/api/transform_test.clj
index 9f3efa6e79d..31d90f79122 100644
--- a/test/metabase/transforms_rest/api/transform_test.clj
+++ b/test/metabase/transforms_rest/api/transform_test.clj
@@ -28,7 +28,8 @@
transform-run-timeout-seconds
utc-timestamp
wait-for-table
- with-transform-cleanup!]]
+ with-transform-cleanup!
+ with-transforms-api-users!]]
[metabase.util :as u]
[toucan2.core :as t2]))
@@ -82,15 +83,15 @@
~@body))
(deftest create-transform-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-db-perms!
(with-transform-cleanup! [table-name "gadget_products"]
(let [query (make-query "Gadget")
schema (get-test-schema)
- response (mt/user-http-request :lucky :post 200 "transform"
+ response (mt/user-http-request user :post 200 "transform"
{:name "Gadget Products"
:source {:type "query"
:query query}
@@ -98,24 +99,24 @@
:schema schema
:name table-name}})
transform-id (:id response)
- lucky-id (mt/user->id :lucky)
+ user-id (mt/user->id user)
creator-id (t2/select-one-fn :creator_id :model/Transform transform-id)]
(testing "Response includes creator_id"
- (is (= lucky-id (:creator_id response))))
+ (is (= user-id (:creator_id response))))
(testing "Database record has creator_id set correctly"
- (is (= lucky-id creator-id)))
+ (is (= user-id creator-id)))
(testing "Response hydrates creator"
(is (map? (:creator response)))
- (is (= lucky-id (get-in response [:creator :id]))))
+ (is (= user-id (get-in response [:creator :id]))))
(testing "Response includes owner_user_id defaulting to creator"
- (is (= lucky-id (:owner_user_id response))))
+ (is (= user-id (:owner_user_id response))))
(testing "Response hydrates owner"
(is (map? (:owner response)))
- (is (= lucky-id (get-in response [:owner :id]))))))))))))
+ (is (= user-id (get-in response [:owner :id]))))))))))))
(deftest update-transform-without-schema-test
(testing "Updating a transform to clear its schema is rejected on schemas-supporting databases"
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(when (driver.u/supports? driver/*driver* :schemas (mt/db))
@@ -144,10 +145,10 @@
(deftest create-transform-without-schema-test
(testing "Creating a transform without a schema is rejected on databases that support schemas, and allowed on those that don't"
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-db-perms!
(with-transform-cleanup! [table-name "no_schema_products"]
(let [query (make-query "Gadget")
@@ -160,23 +161,23 @@
:name table-name}})]
(if (driver.u/supports? driver/*driver* :schemas (mt/db))
(testing "nil schema is rejected with 400 on schemas-supporting driver"
- (let [response (mt/user-http-request :lucky :post 400 "transform" (request nil))]
+ (let [response (mt/user-http-request user :post 400 "transform" (request nil))]
(is (nil? (:id response)))))
(testing "nil schema is allowed on non-schemas drivers"
- (let [response (mt/user-http-request :lucky :post 200 "transform" (request nil))]
+ (let [response (mt/user-http-request user :post 200 "transform" (request nil))]
(is (some? (:id response)))))))))))))))
(deftest create-transform-with-param-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-db-perms!
(with-transform-cleanup! [table-name "gadget_products"]
(testing "Can create a transform with a param"
(let [query (lib/native-query (mt/metadata-provider) "select * from foo [[where {{id}} = id]]")
schema (get-test-schema)
- response (mt/user-http-request :lucky :post 200 "transform"
+ response (mt/user-http-request user :post 200 "transform"
{:name "Gadget Products"
:source {:type "query"
:query query}
@@ -187,10 +188,10 @@
(is (some? transform-id)))))))))))
(deftest create-transform-with-required-param-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-db-perms!
(with-transform-cleanup! [table-name "gadget_products"]
(testing "Cannot create a transform with a required param"
@@ -200,7 +201,7 @@
query (lib/with-template-tags base-query
{"id" (assoc tag :required true)})
schema (get-test-schema)
- response (mt/user-http-request :lucky :post 400 "transform"
+ response (mt/user-http-request user :post 400 "transform"
{:name "Gadget Products"
:source {:type "query"
:query query}
@@ -213,16 +214,16 @@
(:message response))))))))))))
(deftest create-transform-with-unofficial-required-param-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-db-perms!
(with-transform-cleanup! [table-name "gadget_products"]
(testing "Cannot create a transform with a param that is necessary but not marked as required"
(let [query (lib/native-query (mt/metadata-provider) "select * from foo where {{id}} = id")
schema (get-test-schema)
- response (mt/user-http-request :lucky :post 400 "transform"
+ response (mt/user-http-request user :post 400 "transform"
{:name "Gadget Products"
:source {:type "query"
:query query}
@@ -235,7 +236,7 @@
(:message response))))))))))))
(deftest create-transform-with-owner-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(testing "Creating a transform with explicit owner_user_id"
@@ -259,7 +260,7 @@
"Hydrated owner should match the specified user"))))))))
(deftest create-transform-with-external-email-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(testing "Creating a transform with external owner_email"
@@ -282,7 +283,7 @@
"Hydrated owner should be email-only map"))))))))
(deftest update-transform-owner-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-cleanup! [table-name "update_owner_test"]
@@ -325,7 +326,7 @@
(is (nil? (:owner updated)))))))))))
(deftest update-transform-query-with-param-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-cleanup! [table-name "update_owner_test"]
@@ -347,7 +348,7 @@
:query new-query}}))))))))
(deftest update-transform-query-with-required-param-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-cleanup! [table-name "update_owner_test"]
@@ -375,7 +376,7 @@
(:message response))))))))))
(deftest update-transform-query-with-unofficial-required-param-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-cleanup! [table-name "update_owner_test"]
@@ -399,17 +400,17 @@
(:message response))))))))))
(deftest transform-type-detection-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "Transform type is automatically detected and set based on source"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-db-perms!
(testing "MBQL query transforms are detected as :mbql"
(with-transform-cleanup! [table-name "mbql_transform"]
(let [mbql-query (mt/mbql-query transforms_products)
schema (get-test-schema)
- response (mt/user-http-request :lucky :post 200 "transform"
+ response (mt/user-http-request user :post 200 "transform"
{:name "MBQL Transform"
:source {:type "query"
:query mbql-query}
@@ -420,7 +421,7 @@
(testing "Native query transforms are detected as :native"
(with-transform-cleanup! [table-name "native_transform"]
(let [schema (get-test-schema)
- response (mt/user-http-request :lucky :post 200 "transform"
+ response (mt/user-http-request user :post 200 "transform"
{:name "Native Transform"
:source {:type "query"
:query (lib/native-query (mt/metadata-provider) "SELECT 1")}
@@ -430,17 +431,17 @@
(is (= "native" (:source_type response)))))))))))))
(deftest transform-type-updates-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "Transform type is automatically updated when source changes"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-db-perms!
(with-transform-cleanup! [table-name "type_update_transform"]
(let [native-query (lib/native-query (mt/metadata-provider) "SELECT 1")
mbql-query (mt/mbql-query transforms_products)
schema (get-test-schema)
- created (mt/user-http-request :lucky :post 200 "transform"
+ created (mt/user-http-request user :post 200 "transform"
{:name "Native Transform"
:source {:type "query"
:query native-query}
@@ -449,7 +450,7 @@
:name table-name}})]
(is (= "native" (:source_type created)))
(testing "Type automatically changes to mbql when updating to an MBQL query"
- (let [updated (mt/user-http-request :lucky :put 200
+ (let [updated (mt/user-http-request user :put 200
(format "transform/%s" (:id created))
{:source {:type "query"
:query mbql-query}})]
@@ -457,16 +458,16 @@
(deftest create-transform-feature-flag-test
(mt/with-temporary-raw-setting-values [transforms-enabled "true"]
- (mt/with-premium-features #{}
+ (mt/with-premium-features #{:advanced-permissions}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(testing "Creating a query transform succeeds when not hosted"
(mt/dataset transforms-dataset/transforms-test
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-db-perms!
(with-transform-cleanup! [table-name "test_transform"]
(let [query (make-query "Gadget")
schema (get-test-schema)
- response (mt/user-http-request :lucky :post 200 "transform"
+ response (mt/user-http-request user :post 200 "transform"
{:name "Test Transform"
:source {:type "query"
:query query}
@@ -477,11 +478,11 @@
(deftest update-transform-feature-flag-test
(mt/with-temporary-raw-setting-values [transforms-enabled "true"]
- (mt/with-premium-features #{}
+ (mt/with-premium-features #{:advanced-permissions}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(testing "Updating a query transform works when not hosted"
(with-transform-cleanup! [table-name "test_update"]
(let [query (make-query "Gadget")
@@ -492,19 +493,19 @@
:target {:type "table"
:schema schema
:name table-name}}
- created (mt/user-http-request :lucky :post 200 "transform" transform-payload)
- response (mt/user-http-request :lucky :put 200
+ created (mt/user-http-request user :post 200 "transform" transform-payload)
+ response (mt/user-http-request user :put 200
(format "transform/%d" (:id created))
(assoc transform-payload :name "Updated Transform"))]
(is (= "Updated Transform" (:name response)))))))))))))
(deftest run-transform-feature-flag-test
(mt/with-temporary-raw-setting-values [transforms-enabled "true"]
- (mt/with-premium-features #{}
+ (mt/with-premium-features #{:advanced-permissions}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(testing "Running a query transform works when not hosted"
(mt/dataset transforms-dataset/transforms-test
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-db-perms!
(with-transform-cleanup! [table-name "test_run"]
(let [query (make-query "Gadget")
@@ -515,8 +516,8 @@
:target {:type "table"
:schema schema
:name table-name}}
- created (mt/user-http-request :lucky :post 200 "transform" transform-payload)
- response (mt/user-http-request :lucky :post 202
+ created (mt/user-http-request user :post 200 "transform" transform-payload)
+ response (mt/user-http-request user :post 202
(format "transform/%d/run" (:id created)))]
(is (= "Transform run started" (:message response)))))))))))))
@@ -546,34 +547,35 @@
(mt/user-http-request :crowberto :post 403
(format "transform/%d/run" transform-id)))))))))))
-(deftest run-transform-permission-test
- (mt/with-premium-features #{:transforms-basic :hosting}
- (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
- (testing "POST /transform/:id/run requires only read permission, not write"
- (mt/dataset transforms-dataset/transforms-test
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (with-transform-cleanup! [table-name "test_read_run"]
- (mt/with-temp [:model/Transform transform {:name "Test Read Run Transform"
- :source {:type "query"
- :query (make-query "Gadget")}
- :target {:type "table"
- :schema (get-test-schema)
- :name table-name}}]
- (testing "modifying the transform is forbidden (no write permission)"
- (mt/user-http-request :lucky :put 403 (format "transform/%d" (:id transform))
- {:name "Renamed"}))
- (testing "running the transform succeeds (only read permission required)"
- (is (= "Transform run started"
- (:message (mt/user-http-request :lucky :post 202
- (format "transform/%d/run" (:id transform)))))))))))))))
+(mt/when-ee-evailable
+ (deftest run-transform-permission-test
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
+ (testing "POST /transform/:id/run requires only read permission, not write"
+ (mt/dataset transforms-dataset/transforms-test
+ (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transform-cleanup! [table-name "test_read_run"]
+ (mt/with-temp [:model/Transform transform {:name "Test Read Run Transform"
+ :source {:type "query"
+ :query (make-query "Gadget")}
+ :target {:type "table"
+ :schema (get-test-schema)
+ :name table-name}}]
+ (testing "modifying the transform is forbidden (no write permission)"
+ (mt/user-http-request :lucky :put 403 (format "transform/%d" (:id transform))
+ {:name "Renamed"}))
+ (testing "running the transform succeeds (only read permission required)"
+ (is (= "Transform run started"
+ (:message (mt/user-http-request :lucky :post 202
+ (format "transform/%d/run" (:id transform))))))))))))))))
(deftest list-transforms-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(testing "Can list without query parameters"
(with-transform-db-perms!
- (mt/user-http-request :lucky :get 200 "transform")))
+ (mt/user-http-request user :get 200 "transform")))
(testing "Can list with query parameters"
(mt/dataset transforms-dataset/transforms-test
(with-transform-db-perms!
@@ -585,23 +587,23 @@
:target {:type "table"
:schema (get-test-schema)
:name table-name}}
- _ (mt/user-http-request :lucky :post 200 "transform" body)
- list-resp (mt/user-http-request :lucky :get 200 "transform")
- lucky-id (mt/user->id :lucky)]
+ _ (mt/user-http-request user :post 200 "transform" body)
+ list-resp (mt/user-http-request user :get 200 "transform")
+ user-id (mt/user->id user)]
(is (seq list-resp))
(testing "List response hydrates creator"
(is (every? #(map? (:creator %)) list-resp))
- (is (some #(= lucky-id (get-in % [:creator :id])) list-resp)))
+ (is (some #(= user-id (get-in % [:creator :id])) list-resp)))
(testing "List response includes source_readable field"
(is (every? #(contains? % :source_readable) list-resp))
(is (some #(true? (:source_readable %)) list-resp)
"At least one transform should have readable sources")))))))))))
(deftest filter-transforms-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "should be able to filter transforms"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/Transform {t1-id :id} {}
:model/Transform {t2-id :id} {}
:model/TransformTag {tag1-id :id} {:name "tag1"}
@@ -614,24 +616,24 @@
:is_active true}]
(testing "no filters"
(is (=? [{:id t1-id} {:id t2-id}]
- (mt/user-http-request :lucky :get 200 "transform"))))
+ (mt/user-http-request user :get 200 "transform"))))
(testing "last_run_start_time filter"
(is (=? [{:id t1-id}]
- (mt/user-http-request :lucky :get 200 "transform" :last-run-start-time "2025-08-26T10:12:11"))))
+ (mt/user-http-request user :get 200 "transform" :last-run-start-time "2025-08-26T10:12:11"))))
(testing "last_run_statuses filter"
(is (=? [{:id t1-id}]
- (mt/user-http-request :lucky :get 200 "transform" :last-run-statuses ["started" "succeeded"]))))
+ (mt/user-http-request user :get 200 "transform" :last-run-statuses ["started" "succeeded"]))))
(testing "tag_ids filter"
(is (=? [{:id t1-id}]
- (mt/user-http-request :lucky :get 200 "transform" :tag-ids [tag1-id])))
+ (mt/user-http-request user :get 200 "transform" :tag-ids [tag1-id])))
(is (=? [{:id t2-id}]
- (mt/user-http-request :lucky :get 200 "transform" :tag-ids [tag2-id]))))))))))
+ (mt/user-http-request user :get 200 "transform" :tag-ids [tag2-id]))))))))))
(deftest get-transforms-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-db-perms!
(with-transform-cleanup! [table-name "gadget_products"]
(let [body {:name "Gadget Products"
@@ -641,30 +643,32 @@
:target {:type "table"
:schema (get-test-schema)
:name table-name}}
- resp (mt/user-http-request :lucky :post 200 "transform" body)
- get-resp (mt/user-http-request :lucky :get 200 (format "transform/%s" (:id resp)))
- lucky-id (mt/user->id :lucky)]
+ resp (mt/user-http-request user :post 200 "transform" body)
+ get-resp (mt/user-http-request user :get 200 (format "transform/%s" (:id resp)))
+ user-id (mt/user->id user)]
(is (=? (m/dissoc-in body [:source :query :lib/metadata])
(update-in get-resp [:source :query] lib/normalize)))
(testing "GET response hydrates creator"
(is (map? (:creator get-resp)))
- (is (= lucky-id (get-in get-resp [:creator :id]))))))))))))
+ (is (= user-id (get-in get-resp [:creator :id]))))))))))))
(deftest requestable-indexes-field-test
- ;; `#{}` keeps this on the OSS path: query transforms need no token off-cloud, just the `transforms-enabled` setting.
- (mt/with-premium-features #{}
+ ;; withholding `:transforms-basic`/`:hosting` keeps this on the off-cloud path: query transforms need no token
+ ;; there, just the `transforms-enabled` setting. `:advanced-permissions` is what entitles the data analyst; the
+ ;; superuser reaches the endpoint without it.
+ (mt/with-premium-features #{:advanced-permissions}
(mt/with-temporary-raw-setting-values [transforms-enabled "true"]
(testing "GET /transform/:id surfaces the target driver's requestable index methods"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-db-perms!
(with-transform-cleanup! [table-name "reqidx_products"]
(let [body {:name "Reqidx Products"
:source {:type "query" :query (make-query "Gadget")}
:target {:type "table" :schema (get-test-schema) :name table-name}}
- created (mt/user-http-request :lucky :post 200 "transform" body)
- get-resp (mt/user-http-request :lucky :get 200 (format "transform/%s" (:id created)))
+ created (mt/user-http-request user :post 200 "transform" body)
+ get-resp (mt/user-http-request user :get 200 (format "transform/%s" (:id created)))
kinds (set (keys (driver/supported-index-methods driver/*driver* (mt/db))))]
(testing "the key is always present"
(is (contains? get-resp :requestable_indexes)))
@@ -677,38 +681,39 @@
(testing "drivers with no index support report nil"
(is (nil? (:requestable_indexes get-resp)))))))))))))))
-(deftest source-readable-field-test
- (mt/with-premium-features #{:transforms-basic :hosting}
- (testing "Transforms API includes source_readable field"
- (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
- (mt/dataset transforms-dataset/transforms-test
- (with-transform-cleanup! [table-name "test_readable"]
- (let [body {:name "Test Transform"
- :description "Test"
- :source {:type "query"
- :query (make-query "Gadget")}
- :target {:type "table"
- :schema (get-test-schema)
- :name table-name}}]
- (testing "Users with transforms permission can see source_readable field"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (with-transform-db-perms!
- (let [created (mt/user-http-request :lucky :post 200 "transform" body)]
- (testing "in POST /transform response"
- (is (contains? created :source_readable))
- (is (boolean? (:source_readable created))))
- (testing "in GET /transform response"
- (let [list-resp (mt/user-http-request :lucky :get 200 "transform")]
- (is (every? #(contains? % :source_readable) list-resp))
- (is (every? #(boolean? (:source_readable %)) list-resp))))
- (testing "in GET /transform/:id response"
- (let [get-resp (mt/user-http-request :lucky :get 200 (format "transform/%s" (:id created)))]
- (is (contains? get-resp :source_readable))
- (is (boolean? (:source_readable get-resp)))))
- (testing "source_readable is true when user has database read permission"
- (let [get-resp (mt/user-http-request :lucky :get 200 (format "transform/%s" (:id created)))]
- (is (true? (:source_readable get-resp))
- "User with transforms permission should be able to read the source database"))))))))))))))
+(mt/when-ee-evailable
+ (deftest source-readable-field-test
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
+ (testing "Transforms API includes source_readable field"
+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
+ (mt/dataset transforms-dataset/transforms-test
+ (with-transform-cleanup! [table-name "test_readable"]
+ (let [body {:name "Test Transform"
+ :description "Test"
+ :source {:type "query"
+ :query (make-query "Gadget")}
+ :target {:type "table"
+ :schema (get-test-schema)
+ :name table-name}}]
+ (testing "Users with transforms permission can see source_readable field"
+ (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transform-db-perms!
+ (let [created (mt/user-http-request :lucky :post 200 "transform" body)]
+ (testing "in POST /transform response"
+ (is (contains? created :source_readable))
+ (is (boolean? (:source_readable created))))
+ (testing "in GET /transform response"
+ (let [list-resp (mt/user-http-request :lucky :get 200 "transform")]
+ (is (every? #(contains? % :source_readable) list-resp))
+ (is (every? #(boolean? (:source_readable %)) list-resp))))
+ (testing "in GET /transform/:id response"
+ (let [get-resp (mt/user-http-request :lucky :get 200 (format "transform/%s" (:id created)))]
+ (is (contains? get-resp :source_readable))
+ (is (boolean? (:source_readable get-resp)))))
+ (testing "source_readable is true when user has database read permission"
+ (let [get-resp (mt/user-http-request :lucky :get 200 (format "transform/%s" (:id created)))]
+ (is (true? (:source_readable get-resp))
+ "User with transforms permission should be able to read the source database")))))))))))))))
(defn- ->transform [transform-name query]
{:source {:type "query",
@@ -719,8 +724,8 @@
:type "table"}})
(deftest get-transform-dependencies-test
- (mt/with-premium-features #{:transforms-basic :hosting}
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
+ (with-transforms-api-users! [user]
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/with-temp [:model/Table {table :id} {:schema "public", :name "orders_2"}
:model/Field _ {:table_id table, :name "foo"}
@@ -728,7 +733,7 @@
(assoc :target_table_id table))
:model/Transform child (-> (->transform "transform2" (mt/mbql-query nil {:source-table table}))
(assoc-in [:target :name] "orders_3"))]
- (let [deps-resp (mt/user-http-request :lucky :get 200 (format "transform/%s/dependencies" (:id child)))]
+ (let [deps-resp (mt/user-http-request user :get 200 (format "transform/%s/dependencies" (:id child)))]
(is (=? [{:name "transform1"
:source {:type "query"
:query {:database (mt/id)
@@ -742,14 +747,14 @@
(is (every? #(map? (:creator %)) deps-resp)))))))))
(deftest put-transforms-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-cleanup! [table-name "gadget_products"]
(let [query2 (make-query "None")
- resp (mt/user-http-request :lucky :post 200 "transform"
+ resp (mt/user-http-request user :post 200 "transform"
{:name "Gadget Products"
:source {:type "query"
:query (make-query "Gadget")}
@@ -763,21 +768,21 @@
:target {:type "table"
:schema (get-test-schema)
:name table-name}}
- put-resp (mt/user-http-request :lucky :put 200 (format "transform/%s" (:id resp))
+ put-resp (mt/user-http-request user :put 200 (format "transform/%s" (:id resp))
transform)
- lucky-id (mt/user->id :lucky)]
+ user-id (mt/user->id user)]
(is (=? (m/dissoc-in transform [:source :query :lib/metadata])
(update-in put-resp [:source :query] lib/normalize)))
(testing "PUT response hydrates creator"
(is (map? (:creator put-resp)))
- (is (= lucky-id (get-in put-resp [:creator :id]))))))))))))
+ (is (= user-id (get-in put-resp [:creator :id]))))))))))))
(deftest change-target-table-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-cleanup! [table1-name "dookey_products"
table2-name "doohickey_products"]
(let [query2 (make-query "Doohickey")
@@ -787,7 +792,7 @@
:target {:type "table"
:schema (get-test-schema)
:name table1-name}}
- resp (mt/user-http-request :lucky :post 200 "transform"
+ resp (mt/user-http-request user :post 200 "transform"
original)
updated {:name "Doohickey Products"
:description "Desc"
@@ -798,62 +803,62 @@
:name table2-name}}]
(is (=? (-> updated
(m/dissoc-in [:source :query :lib/metadata]))
- (-> (mt/user-http-request :lucky :put 200 (format "transform/%s" (:id resp)) updated)
+ (-> (mt/user-http-request user :put 200 (format "transform/%s" (:id resp)) updated)
(update-in [:source :query] lib/normalize))))
(is (false? (transforms-base.u/target-table-exists? original)))))))))))
(deftest delete-transforms-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-cleanup! [table-name "gadget_products"]
- (let [resp (mt/user-http-request :lucky :post 200 "transform"
+ (let [resp (mt/user-http-request user :post 200 "transform"
{:name "Gadget Products"
:source {:type "query"
:query (make-query "Gadget")}
:target {:type "table"
:schema (get-test-schema)
:name table-name}})]
- (mt/user-http-request :lucky :delete 204 (format "transform/%s" (:id resp)))
- (mt/user-http-request :lucky :get 404 (format "transform/%s" (:id resp)))))))))))
+ (mt/user-http-request user :delete 204 (format "transform/%s" (:id resp)))
+ (mt/user-http-request user :get 404 (format "transform/%s" (:id resp)))))))))))
(deftest delete-table-transforms-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-cleanup! [table-name "gadget_products"]
- (let [resp (mt/user-http-request :lucky :post 200 "transform"
+ (let [resp (mt/user-http-request user :post 200 "transform"
{:name "Gadget Products"
:source {:type "query"
:query (make-query "Gadget")}
:target {:type "table"
:schema (get-test-schema)
:name table-name}})]
- (mt/user-http-request :lucky :delete 204 (format "transform/%s/table" (:id resp)))))))))))
+ (mt/user-http-request user :delete 204 (format "transform/%s/table" (:id resp)))))))))))
(defn- test-run!
- [transform-id]
+ "Run the transform as `user` and block until its target table exists."
+ [user transform-id]
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (let [resp (mt/user-http-request :lucky :post 202 (format "transform/%s/run" transform-id))
- timeout-s transform-run-timeout-seconds
- deadline (seconds-from-now-ns timeout-s)]
- (is (=? {:message "Transform run started"}
- resp))
- (loop []
- (when (> (System/nanoTime) deadline)
- (throw (ex-info (str "Transform run timed out after " timeout-s " seconds") {})))
- (let [resp (mt/user-http-request :lucky :get 200 (format "transform/%s" transform-id))
- status (some-> resp :last_run :status keyword)]
- (when-not (contains? #{:started :succeeded} status)
- (throw (ex-info (str "Transform run failed with status " status) {:resp resp})))
- (when-not (some? (:table resp))
- (Thread/sleep 100)
- (recur))))))))
+ (let [resp (mt/user-http-request user :post 202 (format "transform/%s/run" transform-id))
+ timeout-s transform-run-timeout-seconds
+ deadline (seconds-from-now-ns timeout-s)]
+ (is (=? {:message "Transform run started"}
+ resp))
+ (loop []
+ (when (> (System/nanoTime) deadline)
+ (throw (ex-info (str "Transform run timed out after " timeout-s " seconds") {})))
+ (let [resp (mt/user-http-request user :get 200 (format "transform/%s" transform-id))
+ status (some-> resp :last_run :status keyword)]
+ (when-not (contains? #{:started :succeeded} status)
+ (throw (ex-info (str "Transform run failed with status " status) {:resp resp})))
+ (when-not (some? (:table resp))
+ (Thread/sleep 100)
+ (recur)))))))
(defn- check-query-results
"Verifies that a transform successfully created a table with expected data.
@@ -893,12 +898,12 @@
" in table " table-name ", but got " actual-count)))))
(deftest execute-transform-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "transform execution with :transforms/table target"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(let [schema (t2/select-one-fn :schema :model/Table (mt/id :transforms_products))]
(with-transform-cleanup! [{table1-name :name :as target1} {:type "table"
:schema schema
@@ -911,9 +916,9 @@
:source {:type "query"
:query (make-query "Gadget")}
:target target1}
- {transform-id :id} (mt/user-http-request :lucky :post 200 "transform"
+ {transform-id :id} (mt/user-http-request user :post 200 "transform"
original)
- _ (do (test-run! transform-id)
+ _ (do (test-run! user transform-id)
(wait-for-table table1-name 30000))
_ (is (true? (transforms-base.u/target-table-exists? original)))
_ (check-query-results table1-name [5 11 16] "Gadget")
@@ -925,30 +930,30 @@
(is (=? (-> updated
(m/dissoc-in [:source :query :lib/metadata]))
(->
- (mt/user-http-request :lucky :put 200 (format "transform/%s" transform-id) updated)
+ (mt/user-http-request user :put 200 (format "transform/%s" transform-id) updated)
(update-in [:source :query] lib/normalize))))
- (test-run! transform-id)
+ (test-run! user transform-id)
(wait-for-table table2-name 30000)
(is (true? (transforms-base.u/target-table-exists? original)))
(is (true? (transforms-base.u/target-table-exists? updated)))
(check-query-results table2-name [2 3 4 13] "Doohickey")))))))))))
(deftest get-runs-filter-by-single-transform-id-test
- (mt/with-premium-features #{}
+ (mt/with-premium-features #{:advanced-permissions}
(testing "GET /api/transform/run - filter by single transform ID"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/Transform transform1 {}
:model/Transform transform2 {}
:model/TransformRun run1 {:transform_id (:id transform1)}
:model/TransformRun run2 {:transform_id (:id transform2)}]
(testing "Filter by transform1 ID only returns transform1 runs"
- (let [response (mt/user-http-request :lucky :get 200 "transform/run"
+ (let [response (mt/user-http-request user :get 200 "transform/run"
:transform-ids [(:id transform1)])]
(assert-run-count response 1)
(assert-transform-ids response #{(:id transform1)})
(is (= (:id run1) (-> response :data first :id)))))
(testing "Filter by transform2 ID only returns transform2 runs"
- (let [response (mt/user-http-request :lucky :get 200 "transform/run"
+ (let [response (mt/user-http-request user :get 200 "transform/run"
:transform-ids [(:id transform2)])]
(assert-run-count response 1)
(assert-transform-ids response #{(:id transform2)})
@@ -1268,7 +1273,7 @@
(assert-transform-ids response #{(:id transform1)})))))))
(deftest transform-revisions-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-cleanup! [table-name "transform_revisions_test"]
@@ -1307,7 +1312,7 @@
(test-transform-revisions :put (str "transform/" transform-id) widget-req 2)))))))
(deftest permissions-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "Transform endpoints require transforms permission"
(mt/with-temp [:model/Transform transform {}]
(testing "Regular users without transform permission get 403"
@@ -1324,17 +1329,18 @@
{:name "Updated"})
(mt/user-http-request :rasta :delete 403 (str "transform/" (:id transform)))
(mt/user-http-request :rasta :post 403 (format "transform/%d/run" (:id transform))))
- (testing "Data analysts can read transforms"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/user-http-request :lucky :get 200 "transform")
- (mt/user-http-request :lucky :get 200 (str "transform/" (:id transform)))))))))
+ (mt/when-ee-evailable
+ (testing "Data analysts can read transforms"
+ (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (mt/user-http-request :lucky :get 200 "transform")
+ (mt/user-http-request :lucky :get 200 (str "transform/" (:id transform))))))))))
;;; ------------------------------------------------------------
;;; User Attribution Tests
;;; ------------------------------------------------------------
(deftest manual-run-user-attribution-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "Manual runs are attributed to the triggering user, not the owner"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
@@ -1369,65 +1375,66 @@
;;; Collection Items Integration Tests
;;; ------------------------------------------------------------
-(deftest collection-items-include-transforms-test
- (mt/with-premium-features #{:transforms-basic :hosting}
- (testing "GET /api/collection/:id/items"
- (testing "Includes transforms in collection items"
- (mt/with-data-analyst-role! (mt/user->id :lucky)
- (mt/with-temp [:model/Collection {collection-id :id} {:name "Transforms Collection"
- :namespace :transforms}
- :model/Transform {transform-id :id}
- {:name "Test Transform"
- :description "A test transform"
- :collection_id collection-id}]
- ;; Test 1: Transform appears in unfiltered results
- (let [items (:data (mt/user-http-request :lucky :get 200
- (format "collection/%d/items" collection-id)))]
- (is (= 1 (count items)))
- (is (= "transform" (:model (first items))))
- (is (= "Test Transform" (:name (first items)))))
- ;; Test 2: Transform appears when filtered by models=transform
- (let [items (:data (mt/user-http-request :lucky :get 200
- (format "collection/%d/items" collection-id)
- :models "transform"))]
- (is (= 1 (count items)))
- (is (= transform-id (:id (first items)))))
- ;; Test 3: Transform NOT returned when filtering for other models only
- (let [items (:data (mt/user-http-request :lucky :get 200
- (format "collection/%d/items" collection-id)
- :models "card"))]
- (is (empty? items)))
- ;; Test 4: Non-analysts users don't see transforms
- (perms/grant-collection-read-permissions! (perms/all-users-group) collection-id)
- (let [items (:data (mt/user-http-request :rasta :get 200
- (format "collection/%d/items" collection-id)))]
- (is (empty? items)))
- ;; Test 5: Admins see transforms
- (let [items (:data (mt/user-http-request :crowberto :get 200
- (format "collection/%d/items" collection-id)))]
- (is (= 1 (count items)))
- (is (= "transform" (:model (first items))))
- (is (= "Test Transform" (:name (first items)))))))))))
+(mt/when-ee-evailable
+ (deftest collection-items-include-transforms-test
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
+ (testing "GET /api/collection/:id/items"
+ (testing "Includes transforms in collection items"
+ (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (mt/with-temp [:model/Collection {collection-id :id} {:name "Transforms Collection"
+ :namespace :transforms}
+ :model/Transform {transform-id :id}
+ {:name "Test Transform"
+ :description "A test transform"
+ :collection_id collection-id}]
+ ;; Test 1: Transform appears in unfiltered results
+ (let [items (:data (mt/user-http-request :lucky :get 200
+ (format "collection/%d/items" collection-id)))]
+ (is (= 1 (count items)))
+ (is (= "transform" (:model (first items))))
+ (is (= "Test Transform" (:name (first items)))))
+ ;; Test 2: Transform appears when filtered by models=transform
+ (let [items (:data (mt/user-http-request :lucky :get 200
+ (format "collection/%d/items" collection-id)
+ :models "transform"))]
+ (is (= 1 (count items)))
+ (is (= transform-id (:id (first items)))))
+ ;; Test 3: Transform NOT returned when filtering for other models only
+ (let [items (:data (mt/user-http-request :lucky :get 200
+ (format "collection/%d/items" collection-id)
+ :models "card"))]
+ (is (empty? items)))
+ ;; Test 4: Non-analysts users don't see transforms
+ (perms/grant-collection-read-permissions! (perms/all-users-group) collection-id)
+ (let [items (:data (mt/user-http-request :rasta :get 200
+ (format "collection/%d/items" collection-id)))]
+ (is (empty? items)))
+ ;; Test 5: Admins see transforms
+ (let [items (:data (mt/user-http-request :crowberto :get 200
+ (format "collection/%d/items" collection-id)))]
+ (is (= 1 (count items)))
+ (is (= "transform" (:model (first items))))
+ (is (= "Test Transform" (:name (first items))))))))))))
(deftest create-transform-with-tags-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "POST /api/transform with tag_ids"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(let [schema (get-test-schema)]
(testing "Can create transform with tags"
;; Create tags via API since we're testing transform creation with existing tags
- (let [tag1 (mt/user-http-request :lucky :post 200 "transform-tag"
+ (let [tag1 (mt/user-http-request user :post 200 "transform-tag"
{:name (str "test-tag-1-" (random-uuid))})
- tag2 (mt/user-http-request :lucky :post 200 "transform-tag"
+ tag2 (mt/user-http-request user :post 200 "transform-tag"
{:name (str "test-tag-2-" (random-uuid))})]
(try
(let [transform-request (-> (merge (mt/with-temp-defaults :model/Transform)
{:tag_ids [(:id tag1) (:id tag2)]})
(assoc-in [:target :schema] schema))
- transform-response (mt/user-http-request :lucky :post 200 "transform"
+ transform-response (mt/user-http-request user :post 200 "transform"
transform-request)]
(try
(is (= (:name transform-request) (:name transform-response)))
@@ -1439,7 +1446,7 @@
(testing "Can create transform without tags"
(let [transform-request (assoc-in (mt/with-temp-defaults :model/Transform)
[:target :schema] schema)
- transform-response (mt/user-http-request :lucky :post 200 "transform"
+ transform-response (mt/user-http-request user :post 200 "transform"
transform-request)]
(try
(is (= (:name transform-request) (:name transform-response)))
@@ -1448,11 +1455,11 @@
(t2/delete! :model/Transform :id (:id transform-response))))))))))))))
(deftest update-transform-tags-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "PUT /api/transform/:id with tag_ids"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/Transform transform {:name "Test Transform"
:source {:type "query"
:query {:database (mt/id)
@@ -1464,24 +1471,24 @@
:model/TransformTag tag2 {:name "update-tag-2"}
:model/TransformTag tag3 {:name "update-tag-3"}]
(testing "Can add tags to transform"
- (let [updated (mt/user-http-request :lucky :put 200 (str "transform/" (:id transform))
+ (let [updated (mt/user-http-request user :put 200 (str "transform/" (:id transform))
{:tag_ids [(:id tag1) (:id tag2)]})]
(is (= [(:id tag1) (:id tag2)] (sort (:tag_ids updated))))))
(testing "Can update tags on transform"
- (let [updated (mt/user-http-request :lucky :put 200 (str "transform/" (:id transform))
+ (let [updated (mt/user-http-request user :put 200 (str "transform/" (:id transform))
{:tag_ids [(:id tag2) (:id tag3)]})]
(is (= [(:id tag2) (:id tag3)] (sort (:tag_ids updated))))))
(testing "Can remove all tags from transform"
- (let [updated (mt/user-http-request :lucky :put 200 (str "transform/" (:id transform))
+ (let [updated (mt/user-http-request user :put 200 (str "transform/" (:id transform))
{:tag_ids []})]
(is (= [] (:tag_ids updated))))))))))))
(deftest get-transform-with-tags-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "GET /api/transform/:id returns tag_ids"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/Transform transform {:name "Transform With Tags"
:source {:type "query"
:query {:database (mt/id)
@@ -1494,7 +1501,7 @@
;; Add tags to transform
(transform.model/update-transform-tags! (:id transform) [(:id tag1) (:id tag2)])
(testing "Single transform returns tag_ids"
- (let [fetched (mt/user-http-request :lucky :get 200 (str "transform/" (:id transform)))]
+ (let [fetched (mt/user-http-request user :get 200 (str "transform/" (:id transform)))]
(is (= [(:id tag1) (:id tag2)] (sort (:tag_ids fetched))))))
(testing "Transform without tags returns empty array"
(mt/with-temp [:model/Transform transform2 {:name "Transform Without Tags"
@@ -1504,14 +1511,14 @@
:native {:query "SELECT 1"}}}
:target {:type "table"
:name "untagged_table"}}]
- (let [fetched (mt/user-http-request :lucky :get 200 (str "transform/" (:id transform2)))]
+ (let [fetched (mt/user-http-request user :get 200 (str "transform/" (:id transform2)))]
(is (= [] (:tag_ids fetched)))))))))))))
(deftest list-transforms-with-tags-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "GET /api/transform returns transforms with tag_ids"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/Transform transform1 {:name "Transform 1"
:source {:type "query"
:query {:database (mt/id)
@@ -1532,18 +1539,18 @@
(transform.model/update-transform-tags! (:id transform1) [(:id tag1)])
(transform.model/update-transform-tags! (:id transform2) [(:id tag1) (:id tag2)])
(testing "List endpoint returns all transforms with their tag_ids"
- (let [transforms (mt/user-http-request :lucky :get 200 "transform")
+ (let [transforms (mt/user-http-request user :get 200 "transform")
t1 (some #(when (= (:id %) (:id transform1)) %) transforms)
t2 (some #(when (= (:id %) (:id transform2)) %) transforms)]
(is (= [(:id tag1)] (:tag_ids t1)))
(is (= [(:id tag1) (:id tag2)] (sort (:tag_ids t2))))))))))))
(deftest delete-tag-removes-associations-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "Deleting a tag removes it from all transforms"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/Transform transform {:name "Transform for Delete Test"
:source {:type "query"
:query {:database (mt/id)
@@ -1553,29 +1560,29 @@
:name "delete_test_table"}}
:model/TransformTag tag2 {:name "tag-to-keep"}]
;; Create tag1 via API since we're testing its deletion
- (let [tag1 (mt/user-http-request :lucky :post 200 "transform-tag"
+ (let [tag1 (mt/user-http-request user :post 200 "transform-tag"
{:name "tag-to-delete"})]
(try
;; Add both tags to transform
(transform.model/update-transform-tags! (:id transform) [(:id tag1) (:id tag2)])
;; Verify tags are associated
- (let [fetched (mt/user-http-request :lucky :get 200 (str "transform/" (:id transform)))]
+ (let [fetched (mt/user-http-request user :get 200 (str "transform/" (:id transform)))]
(is (= (set [(:id tag1) (:id tag2)]) (set (:tag_ids fetched)))))
;; Delete tag1
- (mt/user-http-request :lucky :delete 204 (str "transform-tag/" (:id tag1)))
+ (mt/user-http-request user :delete 204 (str "transform-tag/" (:id tag1)))
;; Verify tag1 is removed but tag2 remains
- (let [fetched (mt/user-http-request :lucky :get 200 (str "transform/" (:id transform)))]
+ (let [fetched (mt/user-http-request user :get 200 (str "transform/" (:id transform)))]
(is (= [(:id tag2)] (vec (:tag_ids fetched)))))
(finally
(t2/delete! :model/TransformTag :id (:id tag1))))))))))))
(deftest preserve-tag-order-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "Tag order is preserved when adding/updating transform tags"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(mt/with-temp [:model/TransformTag tag1 {:name "order-tag-1"}
:model/TransformTag tag2 {:name "order-tag-2"}
:model/TransformTag tag3 {:name "order-tag-3"}]
@@ -1584,21 +1591,21 @@
(let [transform-request (-> (merge (mt/with-temp-defaults :model/Transform)
{:tag_ids [(:id tag3) (:id tag1) (:id tag2)]})
(assoc-in [:target :schema] schema))
- transform (mt/user-http-request :lucky :post 200 "transform"
+ transform (mt/user-http-request user :post 200 "transform"
transform-request)]
(try
;; Should preserve the exact order: tag3, tag1, tag2
(is (= [(:id tag3) (:id tag1) (:id tag2)] (:tag_ids transform)))
;; Verify order is preserved when fetching
- (let [fetched (mt/user-http-request :lucky :get 200 (str "transform/" (:id transform)))]
+ (let [fetched (mt/user-http-request user :get 200 (str "transform/" (:id transform)))]
(is (= [(:id tag3) (:id tag1) (:id tag2)] (:tag_ids fetched))))
;; Update with different order
- (let [updated (mt/user-http-request :lucky :put 200 (str "transform/" (:id transform))
+ (let [updated (mt/user-http-request user :put 200 (str "transform/" (:id transform))
{:tag_ids [(:id tag2) (:id tag3) (:id tag1)]})]
;; Should now have the new order: tag2, tag3, tag1
(is (= [(:id tag2) (:id tag3) (:id tag1)] (:tag_ids updated))))
;; Verify new order persists
- (let [fetched-again (mt/user-http-request :lucky :get 200 (str "transform/" (:id transform)))]
+ (let [fetched-again (mt/user-http-request user :get 200 (str "transform/" (:id transform)))]
(is (= [(:id tag2) (:id tag3) (:id tag1)] (:tag_ids fetched-again))))
(finally
(t2/delete! :model/Transform :id (:id transform))))))
@@ -1606,7 +1613,7 @@
(let [transform-request (-> (merge (mt/with-temp-defaults :model/Transform)
{:tag_ids [(:id tag1) (:id tag2) (:id tag1)]})
(assoc-in [:target :schema] schema))
- transform (mt/user-http-request :lucky :post 200 "transform"
+ transform (mt/user-http-request user :post 200 "transform"
transform-request)]
(try
;; Should only have each tag once, but preserve relative order
@@ -1615,7 +1622,7 @@
(t2/delete! :model/Transform :id (:id transform)))))))))))))))
(deftest root-collection-items-include-transforms-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "GET /api/collection/root/items"
(testing "Includes transforms in root collection items"
(mt/with-temp [:model/Transform {transform-id :id}
@@ -1650,7 +1657,7 @@
(is (empty? items)))))))))
(deftest transforms-appear-in-here-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "GET /api/collection/:id/items"
(testing "Transforms in a collection appear in its :here field"
(mt/with-temp [:model/Collection {parent-id :id} {:name "Parent"
@@ -1666,7 +1673,7 @@
(is (= ["transform"] (:here child-coll)))))))))
(deftest transforms-appear-in-below-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "GET /api/collection/:id/items"
(testing "Transforms in descendant collections appear in :below field"
(mt/with-temp [:model/Collection {parent-id :id} {:name "Parent"
@@ -1686,7 +1693,7 @@
(is (= ["transform"] (:below child-coll)))))))))
(deftest incremental-column-type-validated-on-create-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "POST /api/transform column type validation with checkpoint-filter-field-id"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
@@ -1708,7 +1715,7 @@
(is (re-find #"unsupported type" response))))))))))
(deftest incremental-column-type-validated-on-update-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "PUT /api/transform column type validation with checkpoint-filter-field-id"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
@@ -1741,7 +1748,7 @@
(deftest update-tag-ids-with-stale-checkpoint-field-test
(testing "Updating only tag_ids succeeds even when the checkpoint field has since been deleted (GDGT-2410)"
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(let [schema (get-test-schema)]
@@ -1784,7 +1791,7 @@
:required true}}}})
(deftest incremental-table-tag-validated-on-create-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "POST /api/transform rejects a table-incremental native query with no table template tag (GDGT-2524)"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
@@ -1818,7 +1825,7 @@
(is (re-find #"requires a table variable" response))))))))))
(deftest incremental-table-tag-validated-on-update-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "PUT /api/transform rejects removing the table tag from an incremental transform (GDGT-2524)"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
@@ -1870,7 +1877,7 @@
(is (re-find #"requires a table variable" response))))))))))))
(deftest lookback-validated-on-create-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "POST /api/transform lookback window validation (GDGT-2868)"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
@@ -1927,7 +1934,7 @@
(get-in created [:source :source-incremental-strategy :lookback]))))))))))))
(deftest lookback-validated-on-update-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(testing "PUT /api/transform lookback window validation (GDGT-2868)"
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
@@ -1985,7 +1992,7 @@
(get-in updated [:source :source-incremental-strategy :lookback])))))))))))))
(deftest search-filters-transform-source-types-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
;; the temp index table is created here, before `with-temp` opens its transaction: creating (and
@@ -2021,7 +2028,7 @@
(is (not (contains? transform-ids python-id))))))))))))
(deftest search-hides-transforms-for-non-superusers-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
;; see search-filters-transform-source-types-test for why the index scope sits outside `with-temp`
@@ -2042,7 +2049,7 @@
(is (not (contains? ids transform-id))))))))))))
(deftest search-includes-native-and-mbql-query-transforms-test
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
;; see search-filters-transform-source-types-test for why the index scope sits outside `with-temp`
@@ -2114,11 +2121,11 @@
(deftest execute-transform-with-field-filter-template-tag-test
(testing "a required field-filter (:dimension) template tag resolves via its default value at unattended execution"
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-cleanup! [table-name "field_filter_default_output"]
(let [mp (mt/metadata-provider)
category-col (lib.metadata/field mp (mt/id :transforms_products :category))
@@ -2136,51 +2143,51 @@
:type :dimension :widget-type :string/=
:dimension (lib/ref category-col)
:required true :default ["Gadget"]}})
- {id :id} (mt/user-http-request :lucky :post 200 "transform"
+ {id :id} (mt/user-http-request user :post 200 "transform"
{:name "Field Filter Transform"
:source {:type "query" :query query}
:target {:type "table"
:schema (get-test-schema)
:name table-name}})]
- (test-run! id)
+ (test-run! user id)
(check-query-results table-name [5 11 16] "Gadget"))))))))))
(deftest execute-transform-with-table-template-tag-test
(testing "a table (table-variable) template tag substitutes a schema-qualified identifier at unattended execution"
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(with-transform-cleanup! [table-name "table_tag_output"]
(let [source-table-id (mt/id :transforms_products)
query (lib/with-template-tags
(lib/native-query (mt/metadata-provider) "SELECT * FROM {{ tbl }}")
{"tbl" {:id "tbl" :name "tbl" :display-name "Tbl" :type :table
:table-id source-table-id}})
- {id :id} (mt/user-http-request :lucky :post 200 "transform"
+ {id :id} (mt/user-http-request user :post 200 "transform"
{:name "Table Tag Transform"
:source {:type "query" :query query}
:target {:type "table"
:schema (get-test-schema)
:name table-name}})]
- (test-run! id)
+ (test-run! user id)
(wait-for-table table-name 30000)
(is (= (count (table-rows "transforms_products"))
(count (table-rows table-name)))))))))))))
(deftest reject-target-name-collision-test
(testing "target-name collision with a pre-existing, non-transform table is rejected"
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
(with-transform-db-perms!
- (mt/with-data-analyst-role! (mt/user->id :lucky)
+ (with-transforms-api-users! [user]
(let [schema (get-test-schema)
existing-table-name (t2/select-one-fn :name :model/Table (mt/id :transforms_products))
other-existing-name (t2/select-one-fn :name :model/Table (mt/id :transforms_orders))]
(testing "POST /api/transform"
- (mt/user-http-request :lucky :post 403 "transform"
+ (mt/user-http-request user :post 403 "transform"
{:name "Colliding Transform"
:source {:type "query" :query (make-query "Gadget")}
:target {:type "table"
@@ -2188,38 +2195,40 @@
:name existing-table-name}}))
(testing "PUT /api/transform/:id (target-change collision)"
(with-transform-cleanup! [table-name "collision_source"]
- (let [created (mt/user-http-request :lucky :post 200 "transform"
+ (let [created (mt/user-http-request user :post 200 "transform"
{:name "Non-colliding Transform"
:source {:type "query" :query (make-query "Gadget")}
:target {:type "table"
:schema schema
:name table-name}})]
- (mt/user-http-request :lucky :put 403 (format "transform/%d" (:id created))
+ (mt/user-http-request user :put 403 (format "transform/%d" (:id created))
{:target {:type "table"
:schema schema
:name other-existing-name}}))))))))))))
(deftest delete-target-then-recreate-same-name-test
(testing "re-running after DELETE /api/transform/:id/table recreates the same target name"
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)
(mt/dataset transforms-dataset/transforms-test
- (with-transform-cleanup! [table-name "gadget_products_recreate"]
- (let [target {:type "table" :schema (get-test-schema) :name table-name}
- definition {:name "Recreate Target Transform"
- :source {:type "query" :query (make-query "Gadget")}
- :target target}
- {id :id} (mt/user-http-request :crowberto :post 200 "transform" definition)]
- (test-run! id)
- (is (true? (transforms-base.u/target-table-exists? definition)))
- (mt/user-http-request :crowberto :delete 204 (format "transform/%s/table" id))
- (is (false? (transforms-base.u/target-table-exists? definition)))
- (test-run! id)
- (is (true? (transforms-base.u/target-table-exists? definition))))))))))
+ (with-transforms-api-users! [user]
+ (with-transform-db-perms!
+ (with-transform-cleanup! [table-name "gadget_products_recreate"]
+ (let [target {:type "table" :schema (get-test-schema) :name table-name}
+ definition {:name "Recreate Target Transform"
+ :source {:type "query" :query (make-query "Gadget")}
+ :target target}
+ {id :id} (mt/user-http-request user :post 200 "transform" definition)]
+ (test-run! user id)
+ (is (true? (transforms-base.u/target-table-exists? definition)))
+ (mt/user-http-request user :delete 204 (format "transform/%s/table" id))
+ (is (false? (transforms-base.u/target-table-exists? definition)))
+ (test-run! user id)
+ (is (true? (transforms-base.u/target-table-exists? definition))))))))))))
(deftest get-transform-with-deleted-database-test
(testing "GET /api/transform/:id degrades gracefully when its source database has been deleted"
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/with-temp [:model/Database db {}]
(let [mp (lib.metadata.jvm/application-database-metadata-provider (:id db))
query (lib/native-query mp "select 1")]
@@ -2231,7 +2240,7 @@
(deftest cancel-transform-run-endpoint-test
(testing "POST /api/transform/:id/cancel"
- (mt/with-premium-features #{:transforms-basic :hosting}
+ (mt/with-premium-features #{:advanced-permissions :transforms-basic :hosting}
(mt/test-driver :postgres
(mt/dataset transforms-dataset/transforms-test
(with-transform-cleanup! [table-name "cancel_endpoint_output"]
diff --git a/test/metabase/users_rest/api_test.clj b/test/metabase/users_rest/api_test.clj
index f6d0cd250fa..186294f7343 100644
--- a/test/metabase/users_rest/api_test.clj
+++ b/test/metabase/users_rest/api_test.clj
@@ -93,29 +93,32 @@
(is (= "You don't have permissions to do that."
(mt/user-http-request :lucky :get 403 "user" :query "rasta")))))))
-(deftest user-list-for-data-analysts-test
- (testing "GET /api/user"
- (testing "A data analyst can get a list of all active users"
- (mt/with-temp [:model/User {analyst-id :id :as analyst} {:first_name "Analyst"
- :last_name "Testuser"
- :is_data_analyst true}]
- (let [result (->> (:data (mt/user-http-request analyst :get 200 "user"))
- (filter #(or (mt/test-user? %) (= (:id %) analyst-id))))]
- (is (= #{"[email protected]"
- (set (map :email result)))))))
- (testing "A sandboxed data analyst only sees themselves"
- (mt/with-temp [:model/User {_ :id :as analyst} {:first_name "Sandboxed"
- :last_name "Analyst"
- :is_data_analyst true}]
- (mt/with-dynamic-fn-redefs [perms-util/sandboxed-or-impersonated-user? (constantly true)]
- (let [result (:data (mt/user-http-request analyst :get 200 "user"))]
- (is (= ["[email protected]"]
- (map :email result)))))))))
+(mt/when-ee-evailable
+ (deftest user-list-for-data-analysts-test
+ (testing "GET /api/user"
+ ;; data analysts may list users only while advanced-permissions is available
+ (mt/with-premium-features #{:advanced-permissions}
+ (testing "A data analyst can get a list of all active users"
+ (mt/with-temp [:model/User {analyst-id :id :as analyst} {:first_name "Analyst"
+ :last_name "Testuser"
+ :is_data_analyst true}]
+ (let [result (->> (:data (mt/user-http-request analyst :get 200 "user"))
+ (filter #(or (mt/test-user? %) (= (:id %) analyst-id))))]
+ (is (= #{"[email protected]"
+ (set (map :email result)))))))
+ (testing "A sandboxed data analyst only sees themselves"
+ (mt/with-temp [:model/User {_ :id :as analyst} {:first_name "Sandboxed"
+ :last_name "Analyst"
+ :is_data_analyst true}]
+ (mt/with-dynamic-fn-redefs [perms-util/sandboxed-or-impersonated-user? (constantly true)]
+ (let [result (:data (mt/user-http-request analyst :get 200 "user"))]
+ (is (= ["[email protected]"]
+ (map :email result)))))))))))
(deftest user-list-for-group-managers-test
(testing "Group Managers"
@@ -1294,30 +1297,105 @@
(deftest update-data-analyst-status-test
(testing "PUT /api/user/:id"
- (testing "Test that a superuser can set the :is_data_analyst flag (adds to Data Analysts group)"
- (mt/with-temp [:model/User {user-id :id} {:first_name "Test" :last_name "User" :email "[email protected]"}]
- (is (not (user-is-data-analyst? user-id)))
- (mt/user-http-request :crowberto :put 200 (str "user/" user-id)
- {:is_data_analyst true})
- (is (user-is-data-analyst? user-id))))
- (testing "Test that a superuser can unset the :is_data_analyst flag (removes from Data Analysts group)"
- (mt/with-temp [:model/User {user-id :id} {:first_name "Test" :last_name "User" :email "[email protected]"}]
- (mt/user-http-request :crowberto :put 200 (str "user/" user-id)
+ (mt/with-premium-features #{:advanced-permissions}
+ (mt/when-ee-evailable
+ (testing "Test that a superuser can set the :is_data_analyst flag (adds to Data Analysts group)"
+ (mt/with-temp [:model/User {user-id :id} {:first_name "Test" :last_name "User" :email "[email protected]"}]
+ (is (not (user-is-data-analyst? user-id)))
+ (mt/user-http-request :crowberto :put 200 (str "user/" user-id)
+ {:is_data_analyst true})
+ (is (user-is-data-analyst? user-id))))
+ (testing "Test that a superuser can unset the :is_data_analyst flag (removes from Data Analysts group)"
+ (mt/with-temp [:model/User {user-id :id} {:first_name "Test" :last_name "User" :email "[email protected]"}]
+ (mt/user-http-request :crowberto :put 200 (str "user/" user-id)
+ {:is_data_analyst true})
+ (is (user-is-data-analyst? user-id))
+ (mt/user-http-request :crowberto :put 200 (str "user/" user-id)
+ {:is_data_analyst false})
+ (is (not (user-is-data-analyst? user-id))))))
+ (testing "Test that a normal user cannot change the :is_data_analyst flag for themselves"
+ (is (not (user-is-data-analyst? (mt/user->id :rasta))))
+ (mt/user-http-request :rasta :put 200 (str "user/" (mt/user->id :rasta))
{:is_data_analyst true})
- (is (user-is-data-analyst? user-id))
- (mt/user-http-request :crowberto :put 200 (str "user/" user-id)
- {:is_data_analyst false})
- (is (not (user-is-data-analyst? user-id)))))
- (testing "Test that a normal user cannot change the :is_data_analyst flag for themselves"
- (is (not (user-is-data-analyst? (mt/user->id :rasta))))
- (mt/user-http-request :rasta :put 200 (str "user/" (mt/user->id :rasta))
- {:is_data_analyst true})
- (is (not (user-is-data-analyst? (mt/user->id :rasta)))))
- (testing "Test that a normal user cannot change the :is_data_analyst flag for another user"
- (mt/with-temp [:model/User {user-id :id} {:first_name "Test" :last_name "User" :email "[email protected]"}]
- (is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :put 403 (str "user/" user-id)
- {:is_data_analyst true})))))))
+ (is (not (user-is-data-analyst? (mt/user->id :rasta)))))
+ (testing "Test that a normal user cannot change the :is_data_analyst flag for another user"
+ (mt/with-temp [:model/User {user-id :id} {:first_name "Test" :last_name "User" :email "[email protected]"}]
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request :rasta :put 403 (str "user/" user-id)
+ {:is_data_analyst true}))))))))
+
+(deftest update-data-analyst-status-requires-advanced-permissions-test
+ (testing "PUT /api/user/:id with is_data_analyst"
+ (testing "is refused without the :advanced-permissions feature"
+ (mt/with-temp [:model/User {user-id :id} {:email "[email protected]"}]
+ (mt/with-premium-features #{}
+ (is (= (str perms/fail-to-add-data-analyst-msg)
+ (mt/user-http-request :crowberto :put 402 (str "user/" user-id)
+ {:is_data_analyst true})))
+ (testing "and neither the membership nor the flag is left behind"
+ (is (not (user-is-data-analyst? user-id)))
+ (is (not (t2/select-one-fn :is_data_analyst :model/User :id user-id)))))))
+ (mt/when-ee-evailable
+ (testing "can still unset the flag without the :advanced-permissions feature"
+ (mt/with-temp [:model/User {user-id :id} {:email "[email protected]"}]
+ (mt/with-premium-features #{:advanced-permissions}
+ (mt/user-http-request :crowberto :put 200 (str "user/" user-id) {:is_data_analyst true})
+ (is (user-is-data-analyst? user-id)))
+ (mt/with-premium-features #{}
+ (mt/user-http-request :crowberto :put 200 (str "user/" user-id) {:is_data_analyst false})
+ (is (not (user-is-data-analyst? user-id)))
+ (is (not (t2/select-one-fn :is_data_analyst :model/User :id user-id)))))))))
+
+(deftest reasserting-data-analyst-status-is-idempotent-test
+ (testing "PUT /api/user/:id with is_data_analyst true on someone already in the group"
+ (mt/when-ee-evailable
+ (mt/with-temp [:model/User {user-id :id} {:email "[email protected]"}]
+ (mt/with-premium-features #{:advanced-permissions}
+ (mt/user-http-request :crowberto :put 200 (str "user/" user-id) {:is_data_analyst true})
+ (testing "is a no-op while the feature is available"
+ (mt/user-http-request :crowberto :put 200 (str "user/" user-id) {:is_data_analyst true})
+ (is (user-is-data-analyst? user-id))))
+ (mt/with-premium-features #{}
+ (testing "and still a no-op after a downgrade: nothing is being added"
+ (mt/user-http-request :crowberto :put 200 (str "user/" user-id) {:is_data_analyst true})
+ (is (user-is-data-analyst? user-id))
+ (is (true? (t2/select-one-fn :is_data_analyst :model/User :id user-id)))))))))
+
+(deftest invite-user-into-data-analyst-group-requires-advanced-permissions-test
+ (testing "POST /api/user cannot put a new user in the Data Analysts group without :advanced-permissions"
+ (let [email (format "invited-analyst-%[email protected]" (random-uuid))
+ group-id (:id (perms-group/data-analyst))]
+ (try
+ (mt/with-premium-features #{}
+ (is (= (str perms/fail-to-add-data-analyst-msg)
+ (mt/user-http-request :crowberto :post 402 "user"
+ {:first_name "Invited"
+ :last_name "Analyst"
+ :email email
+ :user_group_memberships [{:id (:id (perms-group/all-users))}
+ {:id group-id}]})))
+ (testing "and no user is created at all, since the invite is transactional"
+ (is (not (t2/exists? :model/User :email email)))))
+ (finally
+ (t2/delete! :model/User :email email))))))
+
+(mt/when-ee-evailable
+ (deftest invite-user-into-data-analyst-group-with-advanced-permissions-test
+ (testing "POST /api/user can put a new user in the Data Analysts group with :advanced-permissions"
+ (let [email (format "invited-analyst-%[email protected]" (random-uuid))
+ group-id (:id (perms-group/data-analyst))]
+ (try
+ (mt/with-premium-features #{:advanced-permissions}
+ (let [{user-id :id} (mt/user-http-request :crowberto :post 200 "user"
+ {:first_name "Invited"
+ :last_name "Analyst"
+ :email email
+ :user_group_memberships [{:id (:id (perms-group/all-users))}
+ {:id group-id}]})]
+ (is (user-is-data-analyst? user-id))
+ (is (true? (boolean (t2/select-one-fn :is_data_analyst :model/User :id user-id))))))
+ (finally
+ (t2/delete! :model/User :email email)))))))
(deftest filter-by-data-analyst-test
(testing "GET /api/user"
@@ -1347,6 +1425,37 @@
(testing "non-analyst is included"
(is (contains? result-ids non-analyst-id))))))))
+(deftest data-analyst-user-list-access-follows-the-advanced-permissions-feature-test
+ (testing "GET /api/user is open to a data analyst only while advanced-permissions is available"
+ (mt/with-temp [:model/User {analyst-id :id} {:email "[email protected]"
+ :is_data_analyst true}]
+ (mt/when-ee-evailable
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (map? (mt/user-http-request analyst-id :get 200 "user")))))
+ (mt/with-premium-features #{}
+ (is (= "You don't have permissions to do that."
+ (mt/user-http-request analyst-id :get 403 "user")))))))
+
+(deftest filter-by-can-access-data-studio-test
+ (testing "GET /api/user"
+ (mt/with-temp [:model/User {analyst-id :id} {:email "[email protected]"
+ :is_data_analyst true}
+ :model/User {plain-id :id} {:email "[email protected]"}]
+ (letfn [(matching-ids [can-access?]
+ (set (map :id (:data (mt/user-http-request :crowberto :get 200 "user"
+ :can_access_data_studio can-access?)))))]
+ (mt/when-ee-evailable
+ (testing "a data analyst can access Data Studio while advanced-permissions is available"
+ (mt/with-premium-features #{:advanced-permissions}
+ (is (contains? (matching-ids true) analyst-id))
+ (is (not (contains? (matching-ids true) plain-id))))))
+ (testing "but not once the feature is gone"
+ (mt/with-premium-features #{}
+ (is (not (contains? (matching-ids true) analyst-id)))
+ (is (contains? (matching-ids false) analyst-id))
+ (testing "while superusers still can"
+ (is (contains? (matching-ids true) (mt/user->id :crowberto))))))))))
+
(deftest update-permissions-test
(testing "PUT /api/user/:id"
(testing "Check that a non-superuser CANNOT update someone else's user details"