Skip to content
Snippets Groups Projects
Unverified Commit b1298421 authored by Cam Saul's avatar Cam Saul Committed by GitHub
Browse files

Switch to Honey SQL 2 and Toucan 2, part 2 (#27743)

* Switch to Toucan 2 part 1

* Fix Kondo errors

* Switch to Toucan 2, part 2

* Fix Kondo errors
parent b8b0c1e3
No related branches found
No related tags found
No related merge requests found
Showing
with 369 additions and 348 deletions
...@@ -79,7 +79,6 @@ ...@@ -79,7 +79,6 @@
metabase.public-settings.premium-features/enable-enhancements? {:namespaces ["metabase\\.models.*"]} metabase.public-settings.premium-features/enable-enhancements? {:namespaces ["metabase\\.models.*"]}
metabase.query-processor.middleware.resolve-joins/maybe-resolve-source-table {:namespaces ["metabase.*"]} metabase.query-processor.middleware.resolve-joins/maybe-resolve-source-table {:namespaces ["metabase.*"]}
metabase.query-processor.util/normalize-token {:namespaces ["metabase.*"]} metabase.query-processor.util/normalize-token {:namespaces ["metabase.*"]}
metabase.util/key-by {:namespaces ["metabase(?:-enterprise)?.*"]}
schema.core/both {:namespaces ["metabase.*"]} schema.core/both {:namespaces ["metabase.*"]}
schema.core/either {:namespaces ["metabase.*"]}}} schema.core/either {:namespaces ["metabase.*"]}}}
...@@ -89,10 +88,7 @@ ...@@ -89,10 +88,7 @@
:unresolved-var :unresolved-var
{:exclude {:exclude
[colorize.core [colorize.core]}
metabase.test/with-gtaps
metabase.test/with-gtaps-for-user
metabase.test/with-user-attributes]}
:unsorted-required-namespaces {:level :warning} :unsorted-required-namespaces {:level :warning}
...@@ -138,6 +134,7 @@ ...@@ -138,6 +134,7 @@
medley.core m medley.core m
metabase-enterprise.audit-app.pages.common common metabase-enterprise.audit-app.pages.common common
metabase-enterprise.sandbox.api.table table metabase-enterprise.sandbox.api.table table
metabase-enterprise.test met
metabase.analytics.stats stats metabase.analytics.stats stats
metabase.api.activity api.activity metabase.api.activity api.activity
metabase.api.alert api.alert metabase.api.alert api.alert
...@@ -326,6 +323,7 @@ ...@@ -326,6 +323,7 @@
metabase.sync.util sync-util metabase.sync.util sync-util
metabase.sync.util-test sync.util-test metabase.sync.util-test sync.util-test
metabase.task.sync-databases task.sync-databases metabase.task.sync-databases task.sync-databases
metabase.test mt
metabase.test-runner.assert-exprs test-runner.assert-exprs metabase.test-runner.assert-exprs test-runner.assert-exprs
metabase.test-runner.init test-runner.init metabase.test-runner.init test-runner.init
metabase.test-runner.junit test-runner.junit metabase.test-runner.junit test-runner.junit
...@@ -440,6 +438,7 @@ ...@@ -440,6 +438,7 @@
clojure.core/type hooks.metabase.models.disallow-type-or-class/hook clojure.core/type hooks.metabase.models.disallow-type-or-class/hook
metabase-enterprise.advanced-permissions.models.permissions.application-permissions-test/with-new-group-and-current-graph hooks.common/with-two-top-level-bindings metabase-enterprise.advanced-permissions.models.permissions.application-permissions-test/with-new-group-and-current-graph hooks.common/with-two-top-level-bindings
metabase-enterprise.audit-app.pages-test/with-temp-objects hooks.common/with-one-binding metabase-enterprise.audit-app.pages-test/with-temp-objects hooks.common/with-one-binding
metabase-enterprise.sandbox.models.group-table-access-policy/GroupTableAccessPolicy hooks.metabase.models.disallow-invoking-model/hook
metabase-enterprise.serialization.test-util/with-temp-dpc hooks.toucan.util.test/with-temp* metabase-enterprise.serialization.test-util/with-temp-dpc hooks.toucan.util.test/with-temp*
metabase.analytics.prometheus-test/with-prometheus-system hooks.common/with-two-bindings metabase.analytics.prometheus-test/with-prometheus-system hooks.common/with-two-bindings
metabase.api.alert-test/with-alert-in-collection hooks.common/with-four-bindings metabase.api.alert-test/with-alert-in-collection hooks.common/with-four-bindings
...@@ -447,10 +446,10 @@ ...@@ -447,10 +446,10 @@
metabase.api.card-test/with-temp-native-card hooks.common/with-two-bindings metabase.api.card-test/with-temp-native-card hooks.common/with-two-bindings
metabase.api.card-test/with-temp-native-card-with-params hooks.common/with-two-bindings metabase.api.card-test/with-temp-native-card-with-params hooks.common/with-two-bindings
metabase.api.collection-test/with-french-user-and-personal-collection hooks.common/with-two-top-level-bindings metabase.api.collection-test/with-french-user-and-personal-collection hooks.common/with-two-top-level-bindings
metabase.api.common/defendpoint-schema hooks.metabase.api.common/defendpoint
metabase.api.common/defendpoint hooks.metabase.api.common/defendpoint metabase.api.common/defendpoint hooks.metabase.api.common/defendpoint
metabase.api.common/defendpoint-async hooks.metabase.api.common/defendpoint metabase.api.common/defendpoint-async hooks.metabase.api.common/defendpoint
metabase.api.common/defendpoint-async-schema hooks.metabase.api.common/defendpoint metabase.api.common/defendpoint-async-schema hooks.metabase.api.common/defendpoint
metabase.api.common/defendpoint-schema hooks.metabase.api.common/defendpoint
metabase.api.dashboard-test/with-chain-filter-fixtures hooks.common/let-one-with-optional-value metabase.api.dashboard-test/with-chain-filter-fixtures hooks.common/let-one-with-optional-value
metabase.api.embed-test/do-response-formats hooks.common/with-two-bindings metabase.api.embed-test/do-response-formats hooks.common/with-two-bindings
metabase.api.embed-test/with-chain-filter-fixtures hooks.common/let-one-with-optional-value metabase.api.embed-test/with-chain-filter-fixtures hooks.common/let-one-with-optional-value
...@@ -621,6 +620,7 @@ ...@@ -621,6 +620,7 @@
clojurewerkz.quartzite.triggers/build macros.quartz/build-trigger clojurewerkz.quartzite.triggers/build macros.quartz/build-trigger
metabase-enterprise.sandbox.test-util/with-gtaps macros.metabase-enterprise.sandbox.test-util/with-gtaps metabase-enterprise.sandbox.test-util/with-gtaps macros.metabase-enterprise.sandbox.test-util/with-gtaps
metabase-enterprise.serialization.test-util/with-world macros.metabase-enterprise.serialization.test-util/with-world metabase-enterprise.serialization.test-util/with-world macros.metabase-enterprise.serialization.test-util/with-world
metabase-enterprise.test/with-gtaps macros.metabase-enterprise.sandbox.test-util/with-gtaps
metabase.api.card-test/with-ordered-items macros.metabase.api.card-test/with-ordered-items metabase.api.card-test/with-ordered-items macros.metabase.api.card-test/with-ordered-items
metabase.api.collection-test/with-collection-hierarchy macros.metabase.api.collection-test/with-collection-hierarchy metabase.api.collection-test/with-collection-hierarchy macros.metabase.api.collection-test/with-collection-hierarchy
metabase.api.collection-test/with-some-children-of-collection macros.metabase.api.collection-test/with-some-children-of-collection metabase.api.collection-test/with-some-children-of-collection macros.metabase.api.collection-test/with-some-children-of-collection
...@@ -639,11 +639,9 @@ ...@@ -639,11 +639,9 @@
metabase.test.util/with-temp-env-var-value macros.metabase.test.util/with-temp-env-var-value metabase.test.util/with-temp-env-var-value macros.metabase.test.util/with-temp-env-var-value
metabase.test.util/with-temporary-raw-setting-values macros.metabase.test.util/with-temporary-raw-setting-values metabase.test.util/with-temporary-raw-setting-values macros.metabase.test.util/with-temporary-raw-setting-values
metabase.test/with-group-for-user macros.metabase.test.data.users/with-group-for-user metabase.test/with-group-for-user macros.metabase.test.data.users/with-group-for-user
metabase.test/with-gtaps macros.metabase-enterprise.sandbox.test-util/with-gtaps
metabase.test/with-persistence-enabled macros.metabase.test.persistence/with-persistence-enabled metabase.test/with-persistence-enabled macros.metabase.test.persistence/with-persistence-enabled
metabase.test/with-temp-env-var-value macros.metabase.test.util/with-temp-env-var-value metabase.test/with-temp-env-var-value macros.metabase.test.util/with-temp-env-var-value
metabase.test/with-temporary-raw-setting-values macros.metabase.test.util/with-temporary-raw-setting-values metabase.test/with-temporary-raw-setting-values macros.metabase.test.util/with-temporary-raw-setting-values}}
toucan.models/defmodel macros.toucan.models/defmodel}}
:config-in-call :config-in-call
{metabase.util.honey-sql-2-extensions/with-database-type-info {:linters {:type-mismatch {:level :off}}}} {metabase.util.honey-sql-2-extensions/with-database-type-info {:linters {:type-mismatch {:level :off}}}}
......
(ns macros.toucan.models)
(defmacro defmodel [model-name & _args]
`(do
(def ~model-name "Docstring." nil)))
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
(:require (:require
[clojure.core.async :as a] [clojure.core.async :as a]
[clojure.core.memoize :as memoize] [clojure.core.memoize :as memoize]
[clojure.java.jdbc :as jdbc]
[clojure.string :as str] [clojure.string :as str]
[clojure.walk :as walk] [clojure.walk :as walk]
[honeysql.core :as hsql] [honeysql.core :as hsql]
...@@ -13,6 +12,7 @@ ...@@ -13,6 +12,7 @@
[medley.core :as m] [medley.core :as m]
[metabase-enterprise.audit-app.query-processor.middleware.handle-audit-queries :as qp.middleware.audit] [metabase-enterprise.audit-app.query-processor.middleware.handle-audit-queries :as qp.middleware.audit]
[metabase.db :as mdb] [metabase.db :as mdb]
[metabase.db.connection :as mdb.connection]
[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute] [metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]
[metabase.driver.sql-jdbc.sync :as sql-jdbc.sync] [metabase.driver.sql-jdbc.sync :as sql-jdbc.sync]
[metabase.driver.sql.query-processor :as sql.qp] [metabase.driver.sql.query-processor :as sql.qp]
...@@ -93,7 +93,7 @@ ...@@ -93,7 +93,7 @@
;; another ;; another
(let [timezone (memoize/ttl sql-jdbc.sync/db-default-timezone :ttl/threshold (u/hours->ms 1))] (let [timezone (memoize/ttl sql-jdbc.sync/db-default-timezone :ttl/threshold (u/hours->ms 1))]
(fn [] (fn []
(timezone (mdb/db-type) (db/connection))))) (timezone (mdb/db-type) {:datasource mdb.connection/*application-db*}))))
(defn- compile-honeysql [driver honeysql-query] (defn- compile-honeysql [driver honeysql-query]
(try (try
...@@ -114,7 +114,7 @@ ...@@ -114,7 +114,7 @@
;; instead of mocking up a chunk of regular QP pipeline. ;; instead of mocking up a chunk of regular QP pipeline.
(binding [qp.timezone/*results-timezone-id-override* (application-db-default-timezone)] (binding [qp.timezone/*results-timezone-id-override* (application-db-default-timezone)]
(try (try
(with-open [conn (jdbc/get-connection (db/connection)) (with-open [conn (.getConnection mdb.connection/*application-db*)
stmt (sql-jdbc.execute/prepared-statement driver conn sql params) stmt (sql-jdbc.execute/prepared-statement driver conn sql params)
rs (sql-jdbc.execute/execute-prepared-statement! driver stmt)] rs (sql-jdbc.execute/execute-prepared-statement! driver stmt)]
(let [rsmeta (.getMetaData rs) (let [rsmeta (.getMetaData rs)
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
[metabase.integrations.common :as integrations.common] [metabase.integrations.common :as integrations.common]
[metabase.integrations.ldap.default-implementation :as default-impl] [metabase.integrations.ldap.default-implementation :as default-impl]
[metabase.integrations.ldap.interface :as i] [metabase.integrations.ldap.interface :as i]
[metabase.models.interface :as mi]
[metabase.models.setting :as setting :refer [defsetting]] [metabase.models.setting :as setting :refer [defsetting]]
[metabase.models.user :as user :refer [User]] [metabase.models.user :as user :refer [User]]
[metabase.public-settings.premium-features [metabase.public-settings.premium-features
...@@ -73,7 +74,7 @@ ...@@ -73,7 +74,7 @@
(ldap-group-membership-filter))] (ldap-group-membership-filter))]
(assoc user-info :attributes (syncable-user-attributes result))))) (assoc user-info :attributes (syncable-user-attributes result)))))
(defenterprise-schema fetch-or-create-user! :- (class User) (defenterprise-schema fetch-or-create-user! :- (mi/InstanceOf User)
"Using the `user-info` (from `find-user`) get the corresponding Metabase user, creating it if necessary." "Using the `user-info` (from `find-user`) get the corresponding Metabase user, creating it if necessary."
:feature :any :feature :any
[{:keys [first-name last-name email groups attributes], :as user-info} :- EEUserInfo [{:keys [first-name last-name email groups attributes], :as user-info} :- EEUserInfo
......
...@@ -20,11 +20,10 @@ ...@@ -20,11 +20,10 @@
(db/select-one GroupTableAccessPolicy :group_id group_id :table_id table_id) (db/select-one GroupTableAccessPolicy :group_id group_id :table_id table_id)
(db/select GroupTableAccessPolicy {:order-by [[:id :asc]]}))) (db/select GroupTableAccessPolicy {:order-by [[:id :asc]]})))
#_{:clj-kondo/ignore [:deprecated-var]} (api/defendpoint GET "/:id"
(api/defendpoint-schema GET "/:id"
"Fetch GTAP by `id`" "Fetch GTAP by `id`"
[id] [id]
(api/check-404 (GroupTableAccessPolicy id))) (api/check-404 (db/select-one GroupTableAccessPolicy :id id)))
;; TODO - not sure what other endpoints we might need, e.g. for fetching the list above but for a given group or Table ;; TODO - not sure what other endpoints we might need, e.g. for fetching the list above but for a given group or Table
...@@ -54,20 +53,19 @@ ...@@ -54,20 +53,19 @@
[id :as {{:keys [card_id #_attribute_remappings], :as body} :body}] [id :as {{:keys [card_id #_attribute_remappings], :as body} :body}]
{card_id (s/maybe su/IntGreaterThanZero) {card_id (s/maybe su/IntGreaterThanZero)
#_attribute_remappings #_AttributeRemappings} ; TODO - fix me #_attribute_remappings #_AttributeRemappings} ; TODO - fix me
(api/check-404 (GroupTableAccessPolicy id)) (api/check-404 (db/select-one GroupTableAccessPolicy :id id))
;; Only update `card_id` and/or `attribute_remappings` if the values are present in the body of the request. ;; Only update `card_id` and/or `attribute_remappings` if the values are present in the body of the request.
;; This allows existing values to be "cleared" by being set to nil ;; This allows existing values to be "cleared" by being set to nil
(when (some #(contains? body %) [:card_id :attribute_remappings]) (when (some #(contains? body %) [:card_id :attribute_remappings])
(db/update! GroupTableAccessPolicy id (db/update! GroupTableAccessPolicy id
(u/select-keys-when body (u/select-keys-when body
:present #{:card_id :attribute_remappings}))) :present #{:card_id :attribute_remappings})))
(GroupTableAccessPolicy id)) (db/select-one GroupTableAccessPolicy :id id))
#_{:clj-kondo/ignore [:deprecated-var]} (api/defendpoint DELETE "/:id"
(api/defendpoint-schema DELETE "/:id"
"Delete a GTAP entry." "Delete a GTAP entry."
[id] [id]
(api/check-404 (GroupTableAccessPolicy id)) (api/check-404 (db/select-one GroupTableAccessPolicy :id id))
(db/delete! GroupTableAccessPolicy :id id) (db/delete! GroupTableAccessPolicy :id id)
api/generic-204-no-content) api/generic-204-no-content)
......
...@@ -146,7 +146,7 @@ ...@@ -146,7 +146,7 @@
(defn- pre-update [{:keys [id], :as updates}] (defn- pre-update [{:keys [id], :as updates}]
(u/prog1 updates (u/prog1 updates
(let [original (GroupTableAccessPolicy id) (let [original (db/select-one GroupTableAccessPolicy :id id)
updated (merge original updates)] updated (merge original updates)]
(when-not (= (:table_id original) (:table_id updated)) (when-not (= (:table_id original) (:table_id updated))
(throw (ex-info (tru "You cannot change the Table ID of a GTAP once it has been created.") (throw (ex-info (tru "You cannot change the Table ID of a GTAP once it has been created.")
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
[metabase.models.database :refer [Database]] [metabase.models.database :refer [Database]]
[metabase.models.dimension :refer [Dimension]] [metabase.models.dimension :refer [Dimension]]
[metabase.models.field :refer [Field]] [metabase.models.field :refer [Field]]
[metabase.models.interface :as mi]
[metabase.models.metric :refer [Metric]] [metabase.models.metric :refer [Metric]]
[metabase.models.pulse :refer [Pulse]] [metabase.models.pulse :refer [Pulse]]
[metabase.models.segment :refer [Segment]] [metabase.models.segment :refer [Segment]]
...@@ -34,8 +35,11 @@ ...@@ -34,8 +35,11 @@
(io/make-parents filename) (io/make-parents filename)
(spit filename (yaml/generate-string obj :dumper-options {:flow-style :block}))) (spit filename (yaml/generate-string obj :dumper-options {:flow-style :block})))
(def ^:private as-file? (defn- as-file?
(comp (set (map type [Pulse Dashboard Metric Segment Field User])) type)) [instance]
(some (fn [model]
(mi/instance-of? model instance))
[Pulse Dashboard Metric Segment Field User]))
(defn- spit-entity (defn- spit-entity
[path entity] [path entity]
......
...@@ -195,11 +195,11 @@ ...@@ -195,11 +195,11 @@
(.getName (io/file path))) (.getName (io/file path)))
(defn- unresolved-names->string (defn- unresolved-names->string
([entity] ([model]
(unresolved-names->string entity nil)) (unresolved-names->string model nil))
([entity insert-id] ([model insert-id]
(str (str
(when-let [nm (:name entity)] (str "\"" nm "\"")) (when-let [nm (:name model)] (str "\"" nm "\""))
(when insert-id (format " (inserted as ID %d) " insert-id)) (when insert-id (format " (inserted as ID %d) " insert-id))
"missing:\n " "missing:\n "
(str/join (str/join
...@@ -207,7 +207,7 @@ ...@@ -207,7 +207,7 @@
(map (map
(fn [[k v]] (fn [[k v]]
(format "at %s -> %s" (str/join "/" v) k)) (format "at %s -> %s" (str/join "/" v) k))
(::unresolved-names entity)))))) (::unresolved-names model))))))
(defmulti load (defmulti load
"Load an entity of type `model` stored at `path` in the context `context`. "Load an entity of type `model` stored at `path` in the context `context`.
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
"Tests for special behavior of `/api/metabase/dashboard` endpoints in the Metabase Enterprise Edition." "Tests for special behavior of `/api/metabase/dashboard` endpoints in the Metabase Enterprise Edition."
(:require (:require
[clojure.test :refer :all] [clojure.test :refer :all]
[metabase-enterprise.test :as met]
[metabase.api.dashboard-test :as api.dashboard-test] [metabase.api.dashboard-test :as api.dashboard-test]
[metabase.models :refer [DashboardCard FieldValues]] [metabase.models :refer [DashboardCard FieldValues]]
[metabase.models.params.chain-filter-test :as chain-filter-test] [metabase.models.params.chain-filter-test :as chain-filter-test]
...@@ -14,7 +15,7 @@ ...@@ -14,7 +15,7 @@
(deftest chain-filter-sandboxed-field-values-test (deftest chain-filter-sandboxed-field-values-test
(testing "When chain filter endpoints would normally return cached FieldValues (#13832), make sure sandboxing is respected" (testing "When chain filter endpoints would normally return cached FieldValues (#13832), make sure sandboxing is respected"
(mt/with-gtaps {:gtaps {:categories {:query (mt/mbql-query categories {:filter [:< $id 3]})}}} (met/with-gtaps {:gtaps {:categories {:query (mt/mbql-query categories {:filter [:< $id 3]})}}}
(mt/with-temp-vals-in-db FieldValues (u/the-id (db/select-one-id FieldValues :field_id (mt/id :categories :name))) {:values ["Good" "Bad"]} (mt/with-temp-vals-in-db FieldValues (u/the-id (db/select-one-id FieldValues :field_id (mt/id :categories :name))) {:values ["Good" "Bad"]}
(api.dashboard-test/with-chain-filter-fixtures [{:keys [dashboard]}] (api.dashboard-test/with-chain-filter-fixtures [{:keys [dashboard]}]
(testing "GET /api/dashboard/:id/params/:param-key/values" (testing "GET /api/dashboard/:id/params/:param-key/values"
...@@ -31,7 +32,7 @@ ...@@ -31,7 +32,7 @@
(deftest add-card-parameter-mapping-permissions-test (deftest add-card-parameter-mapping-permissions-test
(testing "POST /api/dashboard/:id/cards" (testing "POST /api/dashboard/:id/cards"
(testing "Should check current user's data permissions for the `parameter_mapping`" (testing "Should check current user's data permissions for the `parameter_mapping`"
(mt/with-gtaps {:gtaps {:venues {}}} (met/with-gtaps {:gtaps {:venues {}}}
(api.dashboard-test/do-with-add-card-parameter-mapping-permissions-fixtures (api.dashboard-test/do-with-add-card-parameter-mapping-permissions-fixtures
(fn [{:keys [card-id mappings add-card! dashcards]}] (fn [{:keys [card-id mappings add-card! dashcards]}]
(testing "Should be able to add a card with `parameter_mapping` with only sandboxed perms" (testing "Should be able to add a card with `parameter_mapping` with only sandboxed perms"
...@@ -53,7 +54,7 @@ ...@@ -53,7 +54,7 @@
(deftest update-cards-parameter-mapping-permissions-test (deftest update-cards-parameter-mapping-permissions-test
(testing "PUT /api/dashboard/:id/cards" (testing "PUT /api/dashboard/:id/cards"
(testing "Should check current user's data permissions for the `parameter_mapping`" (testing "Should check current user's data permissions for the `parameter_mapping`"
(mt/with-gtaps {:gtaps {:venues {}}} (met/with-gtaps {:gtaps {:venues {}}}
(api.dashboard-test/do-with-update-cards-parameter-mapping-permissions-fixtures (api.dashboard-test/do-with-update-cards-parameter-mapping-permissions-fixtures
(fn [{:keys [dashboard-id card-id update-mappings! new-mappings]}] (fn [{:keys [dashboard-id card-id update-mappings! new-mappings]}]
(testing "Should be able to update `:parameter_mappings` *with* only sandboxed perms" (testing "Should be able to update `:parameter_mappings` *with* only sandboxed perms"
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
(:require (:require
[clojure.test :refer :all] [clojure.test :refer :all]
[metabase-enterprise.sandbox.test-util :as mt.tu] [metabase-enterprise.sandbox.test-util :as mt.tu]
[metabase-enterprise.test :as met]
[metabase.models :refer [Field FieldValues User]] [metabase.models :refer [Field FieldValues User]]
[metabase.models.field-values :as field-values] [metabase.models.field-values :as field-values]
[metabase.test :as mt] [metabase.test :as mt]
...@@ -10,7 +11,7 @@ ...@@ -10,7 +11,7 @@
(deftest fetch-field-test (deftest fetch-field-test
(testing "GET /api/field/:id" (testing "GET /api/field/:id"
(mt/with-gtaps {:gtaps {:venues {:query (mt.tu/restricted-column-query (mt/id)) (met/with-gtaps {:gtaps {:venues {:query (mt.tu/restricted-column-query (mt/id))
:remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]}}} :remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]}}}
:attributes {:cat 50}} :attributes {:cat 50}}
(testing "Can I fetch a Field that I don't have read access for if I have segmented table access for it?" (testing "Can I fetch a Field that I don't have read access for if I have segmented table access for it?"
...@@ -40,7 +41,7 @@ ...@@ -40,7 +41,7 @@
:remappings {:cat [:variable [:template-tag "cat"]]}}} :remappings {:cat [:variable [:template-tag "cat"]]}}}
:attributes {:cat 50}}]]] :attributes {:cat 50}}]]]
(testing (format "GTAP rule is a %s query" query-type) (testing (format "GTAP rule is a %s query" query-type)
(mt/with-gtaps gtap-rule (met/with-gtaps gtap-rule
(testing (str "When I call the FieldValues API endpoint for a Field that I have segmented table access only " (testing (str "When I call the FieldValues API endpoint for a Field that I have segmented table access only "
"for, will I get ad-hoc values?\n") "for, will I get ad-hoc values?\n")
(letfn [(fetch-values [user field] (letfn [(fetch-values [user field]
...@@ -85,7 +86,7 @@ ...@@ -85,7 +86,7 @@
(testing "A User with a *different* sandbox should see their own values" (testing "A User with a *different* sandbox should see their own values"
(let [password (mt/random-name)] (let [password (mt/random-name)]
(mt/with-temp User [another-user {:password password}] (mt/with-temp User [another-user {:password password}]
(mt/with-gtaps-for-user another-user {:gtaps {:venues (met/with-gtaps-for-user another-user {:gtaps {:venues
{:remappings {:remappings
{:cat {:cat
[:dimension (mt/id :venues :category_id)]}}} [:dimension (mt/id :venues :category_id)]}}}
...@@ -110,7 +111,7 @@ ...@@ -110,7 +111,7 @@
;; sanity test without gtap ;; sanity test without gtap
(is (= [[1 "$"] [2 "$$"] [3 "$$$"] [4 "$$$$"]] (is (= [[1 "$"] [2 "$$"] [3 "$$$"] [4 "$$$$"]]
(:values (mt/user-http-request :rasta :get 200 (format "field/%d/values" field-id))))) (:values (mt/user-http-request :rasta :get 200 (format "field/%d/values" field-id)))))
(mt/with-gtaps {:gtaps {:venues (met/with-gtaps {:gtaps {:venues
{:remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]}}} {:remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]}}}
:attributes {:cat 4}} :attributes {:cat 4}}
(is (= [[1 "$"] [3 "$$$"]] (is (= [[1 "$"] [3 "$$$"]]
...@@ -118,7 +119,7 @@ ...@@ -118,7 +119,7 @@
(deftest search-test (deftest search-test
(testing "GET /api/field/:id/search/:search-id" (testing "GET /api/field/:id/search/:search-id"
(mt/with-gtaps {:gtaps {:venues (met/with-gtaps {:gtaps {:venues
{:remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]} {:remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]}
:query (mt.tu/restricted-column-query (mt/id))}} :query (mt.tu/restricted-column-query (mt/id))}}
:attributes {:cat 50}} :attributes {:cat 50}}
...@@ -136,7 +137,7 @@ ...@@ -136,7 +137,7 @@
(mt/user-http-request :rasta :get 200 url :value "Ta")))))))) (mt/user-http-request :rasta :get 200 url :value "Ta"))))))))
(deftest caching-test (deftest caching-test
(mt/with-gtaps {:gtaps (met/with-gtaps {:gtaps
{:venues {:venues
{:remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]} {:remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]}
:query (mt.tu/restricted-column-query (mt/id))}} :query (mt.tu/restricted-column-query (mt/id))}}
...@@ -156,7 +157,7 @@ ...@@ -156,7 +157,7 @@
(testing "Do different users has different sandbox FieldValues" (testing "Do different users has different sandbox FieldValues"
(let [password (mt/random-name)] (let [password (mt/random-name)]
(mt/with-temp User [another-user {:password password}] (mt/with-temp User [another-user {:password password}]
(mt/with-gtaps-for-user another-user {:gtaps {:venues (met/with-gtaps-for-user another-user {:gtaps {:venues
{:remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]} {:remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]}
:query (mt.tu/restricted-column-query (mt/id))}} :query (mt.tu/restricted-column-query (mt/id))}}
:attributes {:cat 5}} :attributes {:cat 5}}
......
...@@ -147,9 +147,9 @@ ...@@ -147,9 +147,9 @@
(deftest update-gtap-test (deftest update-gtap-test
(testing "PUT /api/mt/gtap" (testing "PUT /api/mt/gtap"
(mt/with-temp* [Table [{table-id :id}] (mt/with-temp* [Table [{table-id :id}]
PermissionsGroup [{group-id :id}] PermissionsGroup [{group-id :id}]
Card [{card-id :id}]] Card [{card-id :id}]]
(premium-features-test/with-premium-features #{:sandboxes} (premium-features-test/with-premium-features #{:sandboxes}
(testing "Test that we can update only the attribute remappings for a GTAP" (testing "Test that we can update only the attribute remappings for a GTAP"
(mt/with-temp GroupTableAccessPolicy [{gtap-id :id} {:table_id table-id (mt/with-temp GroupTableAccessPolicy [{gtap-id :id} {:table_id table-id
...@@ -216,8 +216,9 @@ ...@@ -216,8 +216,9 @@
:card_id card-id-1)))) :card_id card-id-1))))
(testing "Test that we can update a sandbox using the permission graph API" (testing "Test that we can update a sandbox using the permission graph API"
(let [sandbox-id (db/select-one-field :id GroupTableAccessPolicy {:table_id table-id-1 (let [sandbox-id (db/select-one-field :id GroupTableAccessPolicy
:group_id group-id}) :table_id table-id-1
:group_id group-id)
graph (assoc (perms/data-perms-graph) graph (assoc (perms/data-perms-graph)
:sandboxes [{:id sandbox-id :sandboxes [{:id sandbox-id
:card_id card-id-2 :card_id card-id-2
...@@ -231,9 +232,10 @@ ...@@ -231,9 +232,10 @@
:table_id table-id-1 :table_id table-id-1
:group_id group-id))))) :group_id group-id)))))
(testing "Test that we can create and update multiple sandboxes at once using the permission graph API" (testing "Test that we can create and update multiple sandboxes at once using the permission graph API"
(let [sandbox-id (db/select-one-field :id GroupTableAccessPolicy {:table_id table-id-1 (let [sandbox-id (db/select-one-field :id GroupTableAccessPolicy
:group_id group-id}) :table_id table-id-1
:group_id group-id)
graph (assoc (perms/data-perms-graph) graph (assoc (perms/data-perms-graph)
:sandboxes [{:id sandbox-id :sandboxes [{:id sandbox-id
:card_id card-id-1 :card_id card-id-1
......
...@@ -3,9 +3,11 @@ ...@@ -3,9 +3,11 @@
[cheshire.core :as json] [cheshire.core :as json]
[clojure.string :as str] [clojure.string :as str]
[clojure.test :refer :all] [clojure.test :refer :all]
[metabase-enterprise.sandbox.models.group-table-access-policy :refer [GroupTableAccessPolicy]] [metabase-enterprise.sandbox.models.group-table-access-policy
[metabase.models :refer [Card Database PermissionsGroup :refer [GroupTableAccessPolicy]]
PersistedInfo Table]] [metabase-enterprise.test :as met]
[metabase.models
:refer [Card Database PermissionsGroup PersistedInfo Table]]
[metabase.models.permissions-group :as perms-group] [metabase.models.permissions-group :as perms-group]
[metabase.models.persisted-info :as persisted-info] [metabase.models.persisted-info :as persisted-info]
[metabase.query-processor :as qp] [metabase.query-processor :as qp]
...@@ -61,7 +63,7 @@ ...@@ -61,7 +63,7 @@
{:native :none, :schemas {:PUBLIC {(mt/id :venues) {:query :all}}}}) {:native :none, :schemas {:PUBLIC {(mt/id :venues) {:query :all}}}})
:expected-perms (fn [] :expected-perms (fn []
{:schemas {:PUBLIC {(mt/id :venues) {:query "all"}}}})}}] {:schemas {:PUBLIC {(mt/id :venues) {:query "all"}}}})}}]
(mt/with-gtaps {:gtaps {:venues {}}} (met/with-gtaps {:gtaps {:venues {}}}
(testing message (testing message
(testing "sanity check" (testing "sanity check"
(testing "perms graph endpoint should return segmented perms for Venues table" (testing "perms graph endpoint should return segmented perms for Venues table"
...@@ -98,8 +100,8 @@ ...@@ -98,8 +100,8 @@
(testing "GTAP should be deleted from application DB" (testing "GTAP should be deleted from application DB"
(is (= [] (is (= []
(db/select GroupTableAccessPolicy (db/select GroupTableAccessPolicy
:group_id (u/the-id &group) :group_id (u/the-id &group)
:table_id (mt/id :venues))))) :table_id (mt/id :venues)))))
(testing "GTAP for same group, other database should not be affected" (testing "GTAP for same group, other database should not be affected"
(is (schema= [(s/one {:id su/IntGreaterThanZero (is (schema= [(s/one {:id su/IntGreaterThanZero
:group_id (s/eq (u/the-id &group)) :group_id (s/eq (u/the-id &group))
...@@ -108,8 +110,8 @@ ...@@ -108,8 +110,8 @@
:attribute_remappings (s/eq nil)} :attribute_remappings (s/eq nil)}
"GTAP")] "GTAP")]
(db/select GroupTableAccessPolicy (db/select GroupTableAccessPolicy
:group_id (u/the-id &group) :group_id (u/the-id &group)
:table_id (u/the-id db-2-table))))) :table_id (u/the-id db-2-table)))))
(testing "GTAP for same table, other group should not be affected" (testing "GTAP for same table, other group should not be affected"
(is (schema= [(s/one {:id su/IntGreaterThanZero (is (schema= [(s/one {:id su/IntGreaterThanZero
:group_id (s/eq (u/the-id other-group)) :group_id (s/eq (u/the-id other-group))
...@@ -157,14 +159,14 @@ ...@@ -157,14 +159,14 @@
:database_id (mt/id)}]] :database_id (mt/id)}]]
(fake-persist-card! card) (fake-persist-card! card)
(is (str/includes? (is (str/includes?
(:query (qp/compile (:query (qp/compile
{:database (mt/id) {:database (mt/id)
:query {:source-table (str "card__" (u/the-id card))} :query {:source-table (str "card__" (u/the-id card))}
:type :query})) :type :query}))
"metabase_cache"))))) "metabase_cache")))))
(testing "Queries from source if sandboxed" (testing "Queries from source if sandboxed"
(mt/with-gtaps (met/with-gtaps
{:gtaps {:venues {:query (mt/mbql-query venues) {:gtaps {:venues {:query (mt/mbql-query venues)
:remappings {:cat ["variable" [:field (mt/id :venues :category_id) nil]]}}} :remappings {:cat ["variable" [:field (mt/id :venues :category_id) nil]]}}}
:attributes {"cat" 50}} :attributes {"cat" 50}}
...@@ -174,8 +176,8 @@ ...@@ -174,8 +176,8 @@
:database_id (mt/id)}]] :database_id (mt/id)}]]
(fake-persist-card! card) (fake-persist-card! card)
(is (not (str/includes? (is (not (str/includes?
(:query (qp/compile (:query (qp/compile
{:database (mt/id) {:database (mt/id)
:query {:source-table (str "card__" (u/the-id card))} :query {:source-table (str "card__" (u/the-id card))}
:type :query})) :type :query}))
"metabase_cache")))))))) "metabase_cache"))))))))
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
"Tests that would logically be included in `metabase.api.pulse-test` but are separate as they are enterprise only." "Tests that would logically be included in `metabase.api.pulse-test` but are separate as they are enterprise only."
(:require (:require
[clojure.test :refer :all] [clojure.test :refer :all]
[metabase-enterprise.test :as met]
[metabase.integrations.slack] [metabase.integrations.slack]
[metabase.test :as mt])) [metabase.test :as mt]))
...@@ -12,7 +13,7 @@ ...@@ -12,7 +13,7 @@
(testing (str "Non-segmented users are able to send pulses to any slack channel that the configured instance can " (testing (str "Non-segmented users are able to send pulses to any slack channel that the configured instance can "
"see. A segmented user should not be able to send messages to those channels. This tests that a " "see. A segmented user should not be able to send messages to those channels. This tests that a "
"segmented user doesn't see any slack channels.") "segmented user doesn't see any slack channels.")
(mt/with-gtaps {:gtaps {:venues {}}} (met/with-gtaps {:gtaps {:venues {}}}
(mt/with-temporary-setting-values [slack-token nil] (mt/with-temporary-setting-values [slack-token nil]
(is (= nil (is (= nil
(-> (mt/user-http-request :rasta :get 200 "pulse/form_input") (-> (mt/user-http-request :rasta :get 200 "pulse/form_input")
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
[clojure.string :as str] [clojure.string :as str]
[clojure.test :refer :all] [clojure.test :refer :all]
[metabase-enterprise.sandbox.test-util :as mt.tu] [metabase-enterprise.sandbox.test-util :as mt.tu]
[metabase-enterprise.test :as met]
[metabase.test :as mt])) [metabase.test :as mt]))
(def ^:private all-columns (def ^:private all-columns
...@@ -17,10 +18,10 @@ ...@@ -17,10 +18,10 @@
(if (seq fields) (if (seq fields)
(set (map (comp str/upper-case :name) fields)) (set (map (comp str/upper-case :name) fields))
response)))] response)))]
(mt/with-gtaps {:gtaps {:venues (met/with-gtaps {:gtaps {:venues
{:remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]} {:remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]}
:query (mt.tu/restricted-column-query (mt/id))}} :query (mt.tu/restricted-column-query (mt/id))}}
:attributes {:cat 50}} :attributes {:cat 50}}
(testing "Users with restricted access to the columns of a table should only see columns included in the GTAP question" (testing "Users with restricted access to the columns of a table should only see columns included in the GTAP question"
(is (= #{"CATEGORY_ID" "ID" "NAME"} (is (= #{"CATEGORY_ID" "ID" "NAME"}
(field-names :rasta)))) (field-names :rasta))))
...@@ -31,8 +32,8 @@ ...@@ -31,8 +32,8 @@
(testing (str "If a GTAP has a question, but that question doesn't include a clause to restrict the columns that " (testing (str "If a GTAP has a question, but that question doesn't include a clause to restrict the columns that "
"are returned, all fields should be returned") "are returned, all fields should be returned")
(mt/with-gtaps {:gtaps {:venues {:query (mt/mbql-query venues) (met/with-gtaps {:gtaps {:venues {:query (mt/mbql-query venues)
:remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]}}} :remappings {:cat [:variable [:field (mt/id :venues :category_id) nil]]}}}
:attributes {:cat 50}} :attributes {:cat 50}}
(is (= all-columns (is (= all-columns
(field-names :rasta)))))))) (field-names :rasta))))))))
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
"Tests that would logically be included in `metabase.api.user-test` but are separate as they are enterprise only." "Tests that would logically be included in `metabase.api.user-test` but are separate as they are enterprise only."
(:require (:require
[clojure.test :refer :all] [clojure.test :refer :all]
[metabase.models.permissions-group-membership :refer [PermissionsGroupMembership]] [metabase-enterprise.test :as met]
[metabase.models.permissions-group-membership
:refer [PermissionsGroupMembership]]
[metabase.test :as mt] [metabase.test :as mt]
[metabase.test.fixtures :as fixtures] [metabase.test.fixtures :as fixtures]
[metabase.test.util :as tu] [metabase.test.util :as tu]
...@@ -16,7 +18,7 @@ ...@@ -16,7 +18,7 @@
;; themselves. This test checks that GET /api/user for a segmented user only returns themselves ;; themselves. This test checks that GET /api/user for a segmented user only returns themselves
(deftest segmented-user-list-test (deftest segmented-user-list-test
(testing "GET /api/user for a segmented user should return themselves" (testing "GET /api/user for a segmented user should return themselves"
(mt/with-gtaps {:gtaps {:venues {}}} (met/with-gtaps {:gtaps {:venues {}}}
;; Now do the request ;; Now do the request
(is (= [{:common_name "Rasta Toucan" (is (= [{:common_name "Rasta Toucan"
:last_name "Toucan" :last_name "Toucan"
...@@ -30,7 +32,7 @@ ...@@ -30,7 +32,7 @@
:group_id (u/the-id group) :group_id (u/the-id group)
:user_id (mt/user->id :rasta))] :user_id (mt/user->id :rasta))]
(db/update! PermissionsGroupMembership (:id membership) (db/update! PermissionsGroupMembership (:id membership)
:is_group_manager true)) :is_group_manager true))
(is (= [{:common_name "Rasta Toucan" (is (= [{:common_name "Rasta Toucan"
:last_name "Toucan" :last_name "Toucan"
:first_name "Rasta" :first_name "Rasta"
......
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
(:require (:require
[clojure.test :refer :all] [clojure.test :refer :all]
[metabase-enterprise.sandbox.api.util :as mt.api.u] [metabase-enterprise.sandbox.api.util :as mt.api.u]
[metabase.test :as mt])) [metabase-enterprise.test :as met]))
(defn- has-segmented-perms-when-segmented-db-exists? [user-kw] (defn- has-segmented-perms-when-segmented-db-exists? [user-kw]
(mt/with-gtaps-for-user user-kw {:gtaps {:venues {}}} (met/with-gtaps-for-user user-kw {:gtaps {:venues {}}}
(mt.api.u/segmented-user?))) (mt.api.u/segmented-user?)))
(deftest never-segment-admins-test (deftest never-segment-admins-test
......
(ns metabase-enterprise.sandbox.models.params.chain-filter-test (ns metabase-enterprise.sandbox.models.params.chain-filter-test
(:require (:require
[clojure.test :refer :all] [clojure.test :refer :all]
[metabase-enterprise.test :as met]
[metabase.api.common :as api] [metabase.api.common :as api]
[metabase.models :refer [FieldValues]] [metabase.models :refer [FieldValues]]
[metabase.models.field-values :as field-values] [metabase.models.field-values :as field-values]
...@@ -11,7 +12,7 @@ ...@@ -11,7 +12,7 @@
(deftest chain-filter-sandboxed-field-values-test (deftest chain-filter-sandboxed-field-values-test
(testing "When chain-filter would normally return cached FieldValues (#13832), make sure sandboxing is respected" (testing "When chain-filter would normally return cached FieldValues (#13832), make sure sandboxing is respected"
(mt/with-model-cleanup [FieldValues] (mt/with-model-cleanup [FieldValues]
(mt/with-gtaps {:gtaps {:categories {:query (mt/mbql-query categories {:filter [:< $id 3]})}}} (met/with-gtaps {:gtaps {:categories {:query (mt/mbql-query categories {:filter [:< $id 3]})}}}
(field-values/clear-advanced-field-values-for-field! (mt/id :categories :name)) (field-values/clear-advanced-field-values-for-field! (mt/id :categories :name))
(testing "values" (testing "values"
(is (= {:values ["African" "American"] (is (= {:values ["African" "American"]
......
(ns metabase-enterprise.sandbox.models.params.field-values-test (ns metabase-enterprise.sandbox.models.params.field-values-test
(:require (:require
[clojure.test :refer :all] [clojure.test :refer :all]
[metabase-enterprise.sandbox.models.group-table-access-policy :refer [GroupTableAccessPolicy]] [metabase-enterprise.sandbox.models.group-table-access-policy
[metabase-enterprise.sandbox.models.params.field-values :as ee-params.field-values] :refer [GroupTableAccessPolicy]]
[metabase.models :refer [Card Field FieldValues PermissionsGroup PermissionsGroupMembership User]] [metabase-enterprise.sandbox.models.params.field-values
:as ee-params.field-values]
[metabase-enterprise.test :as met]
[metabase.models
:refer [Card
Field
FieldValues
PermissionsGroup
PermissionsGroupMembership
User]]
[metabase.models.field-values :as field-values] [metabase.models.field-values :as field-values]
[metabase.models.params.field-values :as params.field-values] [metabase.models.params.field-values :as params.field-values]
[metabase.public-settings.premium-features-test :as premium-features-test] [metabase.public-settings.premium-features-test
:as premium-features-test]
[metabase.server.middleware.session :as mw.session] [metabase.server.middleware.session :as mw.session]
[metabase.test :as mt] [metabase.test :as mt]
[toucan.db :as db])) [toucan.db :as db]))
...@@ -14,9 +24,9 @@ ...@@ -14,9 +24,9 @@
(deftest get-or-create-advanced-field-values! (deftest get-or-create-advanced-field-values!
(doseq [fv-type [:sandbox :linked-filter]] (doseq [fv-type [:sandbox :linked-filter]]
(testing "create a new field values and fix up the human readable values" (testing "create a new field values and fix up the human readable values"
(mt/with-gtaps {:gtaps {:categories {:query (mt/mbql-query categories {:filter [:and (met/with-gtaps {:gtaps {:categories {:query (mt/mbql-query categories {:filter [:and
[:> $id 3] [:> $id 3]
[:< $id 6]]})}}} [:< $id 6]]})}}}
;; the categories-id doesn't have a field values, we fake it with a full fieldvalues to make it easier to test ;; the categories-id doesn't have a field values, we fake it with a full fieldvalues to make it easier to test
(db/insert! FieldValues {:type :full (db/insert! FieldValues {:type :full
:field_id (mt/id :categories :id) :field_id (mt/id :categories :id)
...@@ -24,8 +34,8 @@ ...@@ -24,8 +34,8 @@
:human_readable_values (map #(str "id_" %) (range 10))}) :human_readable_values (map #(str "id_" %) (range 10))})
(let [categories-id (mt/id :categories :id) (let [categories-id (mt/id :categories :id)
fv (params.field-values/get-or-create-advanced-field-values! fv (params.field-values/get-or-create-advanced-field-values!
fv-type fv-type
(db/select-one Field :id (mt/id :categories :id)))] (db/select-one Field :id (mt/id :categories :id)))]
(is (= 1 (db/count FieldValues :field_id categories-id :type fv-type))) (is (= 1 (db/count FieldValues :field_id categories-id :type fv-type)))
(is (= [4 5] (:values fv))) (is (= [4 5] (:values fv)))
(is (= ["id_4" "id_5"] (:human_readable_values fv))) (is (= ["id_4" "id_5"] (:human_readable_values fv)))
...@@ -33,19 +43,19 @@ ...@@ -33,19 +43,19 @@
(testing "call second time shouldn't create a new FieldValues" (testing "call second time shouldn't create a new FieldValues"
(params.field-values/get-or-create-advanced-field-values! (params.field-values/get-or-create-advanced-field-values!
:sandbox :sandbox
(db/select-one Field :id (mt/id :categories :id))) (db/select-one Field :id (mt/id :categories :id)))
(is (= 1 (db/count FieldValues :field_id categories-id :type fv-type))))))) (is (= 1 (db/count FieldValues :field_id categories-id :type fv-type)))))))
(testing "make sure the Fieldvalues respect [field-values/*total-max-length*]" (testing "make sure the Fieldvalues respect [field-values/*total-max-length*]"
(mt/with-gtaps {:gtaps {:categories {:query (mt/mbql-query categories {:filter [:and (met/with-gtaps {:gtaps {:categories {:query (mt/mbql-query categories {:filter [:and
[:> $id 3] [:> $id 3]
[:< $id 6]]})}}} [:< $id 6]]})}}}
(binding [field-values/*total-max-length* 5] (binding [field-values/*total-max-length* 5]
(is (= ["Asian"] (is (= ["Asian"]
(:values (params.field-values/get-or-create-advanced-field-values! (:values (params.field-values/get-or-create-advanced-field-values!
fv-type fv-type
(db/select-one Field :id (mt/id :categories :name))))))))))) (db/select-one Field :id (mt/id :categories :name)))))))))))
(deftest advanced-field-values-hash-test (deftest advanced-field-values-hash-test
(premium-features-test/with-premium-features #{:sandboxes} (premium-features-test/with-premium-features #{:sandboxes}
...@@ -53,7 +63,7 @@ ...@@ -53,7 +63,7 @@
(mt/with-temp-copy-of-db (mt/with-temp-copy-of-db
(testing "gtap with remappings" (testing "gtap with remappings"
(letfn [(hash-for-user-id [user-id login-attributes field-id] (letfn [(hash-for-user-id [user-id login-attributes field-id]
(mt/with-gtaps-for-user user-id (met/with-gtaps-for-user user-id
{:gtaps {:categories {:remappings {"State" [:dimension [:field (mt/id :categories :name) nil]]}}} {:gtaps {:categories {:remappings {"State" [:dimension [:field (mt/id :categories :name) nil]]}}}
:attributes login-attributes} :attributes login-attributes}
(ee-params.field-values/hash-key-for-sandbox field-id)))] (ee-params.field-values/hash-key-for-sandbox field-id)))]
...@@ -76,15 +86,15 @@ ...@@ -76,15 +86,15 @@
(is (= (hash-for-user-id user-id-1 {"State" "CA"} (mt/id :categories :name)) (is (= (hash-for-user-id user-id-1 {"State" "CA"} (mt/id :categories :name))
(hash-for-user-id user-id-2 {"State" "CA"} (mt/id :categories :name))))) (hash-for-user-id user-id-2 {"State" "CA"} (mt/id :categories :name)))))
(testing "same users but the login_attributes change should have different hash" (testing "same users but the login_attributes change should have different hash"
(is (not= (hash-for-user-id user-id-1 {"State" "CA"} (mt/id :categories :name)) (is (not= (hash-for-user-id user-id-1 {"State" "CA"} (mt/id :categories :name))
(hash-for-user-id user-id-1 {"State" "NY"} (mt/id :categories :name))))) (hash-for-user-id user-id-1 {"State" "NY"} (mt/id :categories :name)))))
(testing "2 users with different login_attributes should have different hash" (testing "2 users with different login_attributes should have different hash"
(is (not= (hash-for-user-id user-id-1 {"State" "CA"} (mt/id :categories :name)) (is (not= (hash-for-user-id user-id-1 {"State" "CA"} (mt/id :categories :name))
(hash-for-user-id user-id-2 {"State" "NY"} (mt/id :categories :name)))) (hash-for-user-id user-id-2 {"State" "NY"} (mt/id :categories :name))))
(is (not= (hash-for-user-id user-id-1 {} (mt/id :categories :name)) (is (not= (hash-for-user-id user-id-1 {} (mt/id :categories :name))
(hash-for-user-id user-id-2 {"State" "NY"} (mt/id :categories :name))))))))) (hash-for-user-id user-id-2 {"State" "NY"} (mt/id :categories :name)))))))))
(testing "gtap with card and remappings" (testing "gtap with card and remappings"
;; hack so that we don't have to setup all the sandbox permissions the table ;; hack so that we don't have to setup all the sandbox permissions the table
...@@ -116,9 +126,9 @@ ...@@ -116,9 +126,9 @@
(is (not= (hash-for-user-id-with-attributes user-id-1 {"State" "CA"} (mt/id :categories :name)) (is (not= (hash-for-user-id-with-attributes user-id-1 {"State" "CA"} (mt/id :categories :name))
(hash-for-user-id-with-attributes user-id-2 {"State" "CA"} (mt/id :categories :id))))) (hash-for-user-id-with-attributes user-id-2 {"State" "CA"} (mt/id :categories :id)))))
(testing "with different attributes, the hash should be the different" (testing "with different attributes, the hash should be the different"
(is (not= (hash-for-user-id-with-attributes user-id-1 {"State" "CA"} (mt/id :categories :name)) (is (not= (hash-for-user-id-with-attributes user-id-1 {"State" "CA"} (mt/id :categories :name))
(hash-for-user-id-with-attributes user-id-2 {"State" "NY"} (mt/id :categories :name))))))) (hash-for-user-id-with-attributes user-id-2 {"State" "NY"} (mt/id :categories :name)))))))
(testing "gtap with native question" (testing "gtap with native question"
(mt/with-temp* (mt/with-temp*
...@@ -176,9 +186,9 @@ ...@@ -176,9 +186,9 @@
(is (not= (hash-for-user-id-with-attributes user-id-1 {"State" "CA"} (mt/id :categories :name)) (is (not= (hash-for-user-id-with-attributes user-id-1 {"State" "CA"} (mt/id :categories :name))
(hash-for-user-id-with-attributes user-id-2 {"State" "CA"} (mt/id :categories :id))))) (hash-for-user-id-with-attributes user-id-2 {"State" "CA"} (mt/id :categories :id)))))
(testing "with different attributes, the hash should be the different" (testing "with different attributes, the hash should be the different"
(is (not= (hash-for-user-id-with-attributes user-id-1 {"State" "CA"} (mt/id :categories :name)) (is (not= (hash-for-user-id-with-attributes user-id-1 {"State" "CA"} (mt/id :categories :name))
(hash-for-user-id-with-attributes user-id-2 {"State" "NY"} (mt/id :categories :name))))))) (hash-for-user-id-with-attributes user-id-2 {"State" "NY"} (mt/id :categories :name)))))))
(testing "2 users in different groups and gtaps use 2 different cards" (testing "2 users in different groups and gtaps use 2 different cards"
(mt/with-temp* (mt/with-temp*
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
[clojure.java.io :as io] [clojure.java.io :as io]
[clojure.test :refer :all] [clojure.test :refer :all]
[medley.core :as m] [medley.core :as m]
[metabase-enterprise.test :as met]
[metabase.email.messages :as messages] [metabase.email.messages :as messages]
[metabase.models [metabase.models
:refer [Card Pulse PulseCard PulseChannel PulseChannelRecipient]] :refer [Card Pulse PulseCard PulseChannel PulseChannelRecipient]]
...@@ -17,9 +18,9 @@ ...@@ -17,9 +18,9 @@
(deftest sandboxed-pulse-test (deftest sandboxed-pulse-test
(testing "Pulses should get sent with the row-level restrictions of the User that created them." (testing "Pulses should get sent with the row-level restrictions of the User that created them."
(letfn [(send-pulse-created-by-user! [user-kw] (letfn [(send-pulse-created-by-user! [user-kw]
(mt/with-gtaps {:gtaps {:venues {:query (mt/mbql-query venues) (met/with-gtaps {:gtaps {:venues {:query (mt/mbql-query venues)
:remappings {:cat ["variable" [:field (mt/id :venues :category_id) nil]]}}} :remappings {:cat ["variable" [:field (mt/id :venues :category_id) nil]]}}}
:attributes {"cat" 50}} :attributes {"cat" 50}}
(mt/with-temp Card [card {:dataset_query (mt/mbql-query venues {:aggregation [[:count]]})}] (mt/with-temp Card [card {:dataset_query (mt/mbql-query venues {:aggregation [[:count]]})}]
;; `with-gtaps` binds the current test user; we don't want that falsely affecting results ;; `with-gtaps` binds the current test user; we don't want that falsely affecting results
(mt/with-test-user nil (mt/with-test-user nil
...@@ -55,15 +56,15 @@ ...@@ -55,15 +56,15 @@
(deftest e2e-sandboxed-pulse-test (deftest e2e-sandboxed-pulse-test
(testing "Sending Pulses w/ sandboxing, end-to-end" (testing "Sending Pulses w/ sandboxing, end-to-end"
(mt/with-gtaps {:gtaps {:venues {:query (mt/mbql-query venues (met/with-gtaps {:gtaps {:venues {:query (mt/mbql-query venues
{:filter [:= $price 3]})}}} {:filter [:= $price 3]})}}}
(let [query (mt/mbql-query venues (let [query (mt/mbql-query venues
{:aggregation [[:count]] {:aggregation [[:count]]
:breakout [$price]})] :breakout [$price]})]
(is (= [[3 13]] (is (= [[3 13]]
(mt/formatted-rows [int int] (mt/formatted-rows [int int]
(mt/with-test-user :rasta (mt/with-test-user :rasta
(qp/process-query query)))) (qp/process-query query))))
"Basic sanity check: make sure the query is properly set up to apply GTAPs") "Basic sanity check: make sure the query is properly set up to apply GTAPs")
(testing "GTAPs should apply to Pulses — they should get the same results as if running that query normally" (testing "GTAPs should apply to Pulses — they should get the same results as if running that query normally"
(is (= [[3 13]] (is (= [[3 13]]
...@@ -81,8 +82,8 @@ ...@@ -81,8 +82,8 @@
(deftest user-attributes-test (deftest user-attributes-test
(testing "Pulses should be sandboxed correctly by User login_attributes" (testing "Pulses should be sandboxed correctly by User login_attributes"
(mt/with-gtaps {:gtaps {:venues {:remappings {:price [:dimension [:field (mt/id :venues :price) nil]]}}} (met/with-gtaps {:gtaps {:venues {:remappings {:price [:dimension [:field (mt/id :venues :price) nil]]}}}
:attributes {"price" "1"}} :attributes {"price" "1"}}
(let [query (mt/mbql-query venues)] (let [query (mt/mbql-query venues)]
(mt/with-test-user :rasta (mt/with-test-user :rasta
(mt/with-temp Card [card {:dataset_query query}] (mt/with-temp Card [card {:dataset_query query}]
...@@ -101,8 +102,8 @@ ...@@ -101,8 +102,8 @@
(deftest pulse-preview-test (deftest pulse-preview-test
(testing "Pulse preview endpoints should be sandboxed" (testing "Pulse preview endpoints should be sandboxed"
(mt/with-gtaps {:gtaps {:venues {:remappings {:price [:dimension [:field (mt/id :venues :price) nil]]}}} (met/with-gtaps {:gtaps {:venues {:remappings {:price [:dimension [:field (mt/id :venues :price) nil]]}}}
:attributes {"price" "1"}} :attributes {"price" "1"}}
(let [query (mt/mbql-query venues)] (let [query (mt/mbql-query venues)]
(mt/with-test-user :rasta (mt/with-test-user :rasta
(mt/with-temp Card [card {:dataset_query query}] (mt/with-temp Card [card {:dataset_query query}]
...@@ -130,8 +131,8 @@ ...@@ -130,8 +131,8 @@
(deftest csv-downloads-test (deftest csv-downloads-test
(testing "CSV/XLSX downloads should be sandboxed" (testing "CSV/XLSX downloads should be sandboxed"
(mt/with-gtaps {:gtaps {:venues {:remappings {:price [:dimension [:field (mt/id :venues :price) nil]]}}} (met/with-gtaps {:gtaps {:venues {:remappings {:price [:dimension [:field (mt/id :venues :price) nil]]}}}
:attributes {"price" "1"}} :attributes {"price" "1"}}
(let [query (mt/mbql-query venues)] (let [query (mt/mbql-query venues)]
(mt/with-test-user :rasta (mt/with-test-user :rasta
(mt/with-temp* [Card [{card-id :id} {:dataset_query query}] (mt/with-temp* [Card [{card-id :id} {:dataset_query query}]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment