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.advanced-config.file.users-test
(:require
[clojure.test :refer :all]
[metabase-enterprise.advanced-config.file :as advanced-config.file]
[metabase.setup.core :as setup]
[metabase.test :as mt]
[metabase.util.password :as u.password]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
(use-fixtures :each (fn [thunk]
(binding [advanced-config.file/*supported-versions* {:min 1, :max 1}]
(mt/with-premium-features #{:config-text-file}
(thunk)))))
(deftest init-from-config-file-test
(try
(testing "Create a User if it does not already exist"
(is (= :ok
(advanced-config.file/initialize!
{:version 1
:config {:users [{:first_name "Cam"
:last_name "Era"
:email "[email protected]"
:password "2cans"
:login_attributes {"a" 1}}]}})))
(is (partial= {:first_name "Cam"
:last_name "Era"
:email "[email protected]"
:login_attributes {"a" "1"}}
(t2/select-one [:model/User :first_name :last_name :email :login_attributes]
:email "[email protected]")))
(is (= 1
(t2/count :model/User :email "[email protected]"))))
(testing "upsert if User already exists, with merged login attributes"
(let [pw-credentials (fn [] (t2/select-one-fn :credentials :model/AuthIdentity
:user_id (t2/select-one-pk :model/User :email "[email protected]")
:provider "password"))
hashed-password (fn [] (:password_hash (pw-credentials)))
salt (fn [] (:password_salt (pw-credentials)))
original-hashed-password (hashed-password)]
(is (= :ok
(advanced-config.file/initialize!
{:version 1
:config {:users [{:first_name "Cam"
:last_name "Saul"
:email "[email protected]"
:password "2cans"
:login_attributes {"b" 2}}]}})))
(is (= 1
(t2/count :model/User :email "[email protected]")))
(is (partial= {:first_name "Cam"
:last_name "Saul"
:email "[email protected]"
:login_attributes {"a" "1" "b" "2"}}
(t2/select-one [:model/User :first_name :last_name :email :login_attributes]
:email "[email protected]")))
(testing "Password should be hashed, but it should be a NEW HASH"
(let [new-hashed-password (hashed-password)]
(is (not= original-hashed-password
new-hashed-password))
(testing "Password should not be saved as plaintext"
(is (not= "2cans"
new-hashed-password)))
(testing "Password should work correctly"
(is (u.password/verify-password "2cans" (salt) new-hashed-password)))))))
(finally
(t2/delete! :model/User :email "[email protected]"))))
(deftest init-from-config-file-force-admin-for-first-user-test
(testing "If this is the first user being created, always make the user a superuser regardless of what is specified"
(try
(testing "Create the first User"
(mt/with-dynamic-fn-redefs [setup/has-user-setup (constantly false)]
(is (= :ok
(advanced-config.file/initialize!
{:version 1
:config {:users [{:first_name "Cam"
:last_name "Era"
:email "[email protected]"
:password "2cans"
:is_superuser false}]}})))
(is (partial= {:first_name "Cam"
:last_name "Era"
:email "[email protected]"
:is_superuser true}
(t2/select-one :model/User :email "[email protected]")))
(is (= 1
(t2/count :model/User :email "[email protected]")))))
(testing "Create the another User, DO NOT force them to be an admin"
(is (= :ok
(advanced-config.file/initialize!
{:version 1
:config {:users [{:first_name "Cam"
:last_name "Saul"
:email "[email protected]"
:password "2cans"
:is_superuser false}]}})))
(is (partial= {:first_name "Cam"
:last_name "Saul"
:email "[email protected]"
:is_superuser false}
(t2/select-one :model/User :email "[email protected]")))
(is (= 1
(t2/count :model/User :email "[email protected]"))))
(finally (t2/delete! :model/User :email [:in #{"[email protected]"
"[email protected]"}])))))
(deftest init-from-config-file-env-var-for-password-test
(testing "Ensure that we can set User password using {{env ...}} templates"
(try
(binding [advanced-config.file/*env* (assoc @#'advanced-config.file/*env* :user-password "1234cans")]
(testing "Create a User if it does not already exist"
(is (= :ok
(advanced-config.file/initialize!
{:version 1
:config {:users [{:first_name "Cam"
:last_name "Era"
:email "[email protected]"
:password "{{env USER_PASSWORD}}"}]}}
{:expand-templates? true})))
(let [user (t2/select-one [:model/User :id :first_name :last_name :email]
:email "[email protected]")
{:keys [password_hash password_salt]} (t2/select-one-fn :credentials :model/AuthIdentity
:user_id (:id user) :provider "password")]
(is (partial= {:first_name "Cam"
:last_name "Era"
:email "[email protected]"}
user))
(is (u.password/verify-password "1234cans" password_salt password_hash)))))
(finally
(t2/delete! :model/User :email "[email protected]")))))
(deftest init-from-config-file-validation-test
(are [user error-pattern] (thrown-with-msg?
clojure.lang.ExceptionInfo
error-pattern
(#'advanced-config.file/config {:version 1
:config {:users [user]}}))
;; missing email
{:first_name "Cam"
:last_name "Era"
:password "2cans"}
(re-pattern (java.util.regex.Pattern/quote "failed: (contains? % :email)"))
;; missing first name
{:last_name "Era"
:email "[email protected]"
:password "2cans"}
(re-pattern (java.util.regex.Pattern/quote "failed: (contains? % :first_name)"))
;; missing last name
{:first_name "Cam"
:email "[email protected]"
:password "2cans"}
(re-pattern (java.util.regex.Pattern/quote "failed: (contains? % :last_name)"))
;; missing password
{:first_name "Cam"
:last_name "Era"
:email "[email protected]"}
(re-pattern (java.util.regex.Pattern/quote "failed: (contains? % :password)"))))
(deftest email-case-irrelevant-test
(testing "Using any case in email still works #62894"
(try
(let [config {:version 1
:config {:users [{:first_name "Cam"
:last_name "Era"
:email "[email protected]"
:password "2cans"
:login_attributes {"a" 1}}]}}]
(is (= :ok (advanced-config.file/initialize! config)))
(testing "It should not fail with 'duplicate'"
(is (= :ok (advanced-config.file/initialize! config)))))
(finally
(t2/delete! :model/User :email "[email protected]")
(t2/delete! :model/User :email "[email protected]")))))