The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
(ns metabase-enterprise.impersonation.query-execution-test
"Integration tests that verify `is_impersonated` is recorded on `:model/QueryExecution` rows when a query runs
under an active connection-impersonation policy. Distinct from [[metabase-enterprise.impersonation.driver-test]]
which tests role resolution; here we drive a full userland query through the QP and inspect the persisted row."
{:clj-kondo/config '{:linters {:deprecated-var {:exclude {metabase.test.data/mbql-query {:namespaces [metabase-enterprise.impersonation.query-execution-test]}}}}}}
(:require
[clojure.test :refer :all]
[metabase-enterprise.impersonation.util-test :as impersonation.util-test]
[metabase.actions.execution :as actions.execution]
[metabase.actions.models :as action]
[metabase.query-processor :as qp]
[metabase.query-processor.error-type :as qp.error-type]
[metabase.query-processor.pipeline :as qp.pipeline]
[metabase.query-processor.util :as qp.util]
[metabase.test :as mt]
[toucan2.core :as t2]))
(defn- latest-query-execution []
(t2/select-one :model/QueryExecution {:order-by [[:id :desc]]}))
(deftest is-impersonated-true-when-impersonation-policy-active-test
(mt/with-premium-features #{:advanced-permissions}
(mt/with-model-cleanup [:model/QueryExecution]
(binding [qp.util/*execute-async?* false]
(impersonation.util-test/with-impersonations!
{:impersonations [{:db-id (mt/id) :attribute "impersonation_attr"}]
:attributes {"impersonation_attr" "impersonation_role"}}
(qp/process-query (qp/userland-query (mt/mbql-query venues {:limit 1}) {:context :question}))
(is (true? (:is_impersonated (latest-query-execution)))
"QueryExecution row should record is_impersonated=true when the query ran under an impersonation policy"))))))
(deftest is-impersonated-false-when-no-policy-test
(mt/with-model-cleanup [:model/QueryExecution]
(binding [qp.util/*execute-async?* false]
(mt/with-test-user :rasta
(qp/process-query (qp/userland-query (mt/mbql-query venues {:limit 1}) {:context :question}))
(is (false? (:is_impersonated (latest-query-execution))))))))
(deftest is-impersonated-true-when-impersonated-query-fails-test
(testing "When an impersonated query throws during execution, the persisted QueryExecution row should still
record is_impersonated=true. Reproduces a Clojure-binding gotcha: by the time the catch block in
process-userland-query-middleware fires, the impersonation `binding` established by the EE postprocessing
middleware has already been popped during stack unwind."
(mt/with-premium-features #{:advanced-permissions}
(mt/with-model-cleanup [:model/QueryExecution]
(binding [qp.util/*execute-async?* false
qp.pipeline/*run* (fn [_query _rff]
(throw (ex-info "Boom" {:type qp.error-type/qp})))]
(impersonation.util-test/with-impersonations!
{:impersonations [{:db-id (mt/id) :attribute "impersonation_attr"}]
:attributes {"impersonation_attr" "impersonation_role"}}
(try
(qp/process-query (qp/userland-query (mt/mbql-query venues {:limit 1}) {:context :question}))
(catch Throwable _))
(is (true? (:is_impersonated (latest-query-execution)))
"QueryExecution row for a FAILED impersonated query should still record is_impersonated=true")
(is (some? (:error (latest-query-execution)))
"Sanity check: the QueryExecution row should have an error message")))))))
(deftest action-row-records-impersonation-test
(testing "a native action run under an impersonation policy records is_impersonated, on success and on failure"
(mt/with-premium-features #{:advanced-permissions}
(mt/with-actions-test-data-and-actions-enabled
(mt/with-actions [{ok-action-id :action-id} {:type :query}
{bad-action-id :action-id} {:type :query
:parameters []
:dataset_query (mt/native-query
{:query "UPDATE categories SET name = 1/0 WHERE id = 1"})}]
(impersonation.util-test/with-impersonations!
{:impersonations [{:db-id (mt/id) :attribute "impersonation_attr"}]
:attributes {"impersonation_attr" "impersonation_role"}}
(testing "success"
(let [since (mt/latest-query-execution-id)]
(actions.execution/execute-action! (action/select-action :id ok-action-id) {"id" 1 "name" "Bird"})
(is (=? {:is_impersonated true, :error nil}
(first (mt/action-executions since))))))
(testing "failure"
(let [since (mt/latest-query-execution-id)]
(is (thrown? clojure.lang.ExceptionInfo
(actions.execution/execute-action! (action/select-action :id bad-action-id) {})))
(is (=? {:is_impersonated true, :error some?}
(first (mt/action-executions since))))))))))))
(deftest implicit-action-row-records-impersonation-test
(testing "an implicit action records is_impersonated from the user's policy on the database, as reads do"
(mt/with-premium-features #{:advanced-permissions}
(mt/with-actions-test-data-and-actions-enabled
(mt/with-actions [{:keys [action-id]} {:type :implicit :kind "row/update"}]
(impersonation.util-test/with-impersonations!
{:impersonations [{:db-id (mt/id) :attribute "impersonation_attr"}]
:attributes {"impersonation_attr" "impersonation_role"}}
;; H2 can't switch roles, so a true here comes from the policy, as it does for reads on H2
(testing "impersonated user"
(let [since (mt/latest-query-execution-id)]
(actions.execution/execute-action! (action/select-action :id action-id) {"id" 1 "name" "Bird"})
(is (=? {:is_impersonated true, :native false, :error nil}
(first (mt/action-executions since))))))
(testing "admins are never impersonated"
(mt/with-test-user :crowberto
(let [since (mt/latest-query-execution-id)]
(actions.execution/execute-action! (action/select-action :id action-id) {"id" 1 "name" "Bird"})
(is (=? {:is_impersonated false, :error nil}
(first (mt/action-executions since)))))))))))))