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.sso.settings-test
(:require
[clojure.test :refer :all]
[metabase-enterprise.sso.settings :as sso-settings]
[metabase.session.core :as session]
[metabase.sso.settings :as sso.settings]
[metabase.sso.test-helpers :as sso.test-helpers]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
[metabase.test.util :as tu]))
(use-fixtures :once (fixtures/initialize :db))
(def ^:private default-idp-uri "http://test.idp.metabase.com")
(def ^:private default-idp-cert (slurp "test_resources/sso/auth0-public-idp.cert"))
(deftest get-saml-settings-token-features-test
(testing "Getting SAML settings should return their default values without :sso-saml feature flag enabled"
(mt/with-premium-features #{:sso-saml}
(tu/with-temporary-setting-values [saml-identity-provider-uri default-idp-uri
saml-identity-provider-certificate default-idp-cert
saml-identity-provider-issuer "54321"
saml-application-name "Not Metabase"
saml-keystore-path "test_resources/keystore.jks"
saml-keystore-password "123456"
saml-keystore-alias "sp"
saml-attribute-email "not default email"
saml-attribute-firstname "not default first_name"
saml-attribute-lastname "not default last_name"
saml-group-sync true
saml-attribute-group "group"
saml-group-mappings {:group_1 [1]}
saml-enabled true]
(doseq [feature? [true false]]
(mt/with-premium-features (if feature? #{:sso-saml} #{})
(is (= (if feature? default-idp-uri nil)
(sso-settings/saml-identity-provider-uri)))
(is (= (if feature? default-idp-cert nil)
(sso-settings/saml-identity-provider-certificate)))
(is (= (if feature? "54321" nil)
(sso-settings/saml-identity-provider-issuer)))
(is (= (if feature? "Not Metabase" "Metabase")
(sso-settings/saml-application-name)))
(is (= (if feature? "test_resources/keystore.jks" nil)
(sso-settings/saml-keystore-path)))
(is (= (if feature? "123456" "changeit")
(sso-settings/saml-keystore-password)))
(is (= (if feature? "sp" nil)
(sso-settings/saml-keystore-alias)))
(is (= (if feature? "not default email" "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")
(sso-settings/saml-attribute-email)))
(is (= (if feature? "not default first_name" "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname")
(sso-settings/saml-attribute-firstname)))
(is (= (if feature? "not default last_name" "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname")
(sso-settings/saml-attribute-lastname)))
(is (= (if feature? true false)
(sso-settings/saml-group-sync)))
(is (= (if feature? "group" nil)
(sso-settings/saml-attribute-group)))
(is (= (if feature? {:group_1 [1]} {})
(sso-settings/saml-group-mappings)))
(is (= (if feature? true false)
(sso-settings/saml-configured)))
(is (= (if feature? true false)
(sso-settings/saml-enabled)))))))))
(deftest set-saml-settings-token-features-test
(testing "Setting SAML settings should error without the :sso-saml feature flag enabled"
(mt/with-premium-features #{}
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-identity-provider-uri is not enabled because feature :sso-saml is not available"
(sso-settings/saml-identity-provider-uri! default-idp-uri)))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-identity-provider-certificate is not enabled because feature :sso-saml is not available"
(sso-settings/saml-identity-provider-certificate! default-idp-cert)))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-identity-provider-issuer is not enabled because feature :sso-saml is not available"
(sso-settings/saml-identity-provider-issuer! "54321")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-application-name is not enabled because feature :sso-saml is not available"
(sso-settings/saml-application-name! "Not Metabase")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-keystore-path is not enabled because feature :sso-saml is not available"
(sso-settings/saml-keystore-path! "test_resources/keystore.jks")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-keystore-password is not enabled because feature :sso-saml is not available"
(sso-settings/saml-keystore-password! "123456")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-keystore-alias is not enabled because feature :sso-saml is not available"
(sso-settings/saml-keystore-alias! "sp")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-attribute-email is not enabled because feature :sso-saml is not available"
(sso-settings/saml-attribute-email! "email")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-attribute-firstname is not enabled because feature :sso-saml is not available"
(sso-settings/saml-attribute-firstname! "firstname")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-attribute-lastname is not enabled because feature :sso-saml is not available"
(sso-settings/saml-attribute-lastname! "lastname")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-group-sync is not enabled because feature :sso-saml is not available"
(sso-settings/saml-group-sync! true)))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-attribute-group is not enabled because feature :sso-saml is not available"
(sso-settings/saml-attribute-group! "group")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-group-mappings is not enabled because feature :sso-saml is not available"
(sso-settings/saml-group-mappings! {:group_1 [1]})))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-enabled is not enabled because feature :sso-saml is not available"
(sso-settings/saml-enabled! true)))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting saml-slo-enabled is not enabled because feature :sso-saml is not available"
(sso-settings/saml-slo-enabled! true))))))
(deftest saml-scim-user-provisioning
(testing "SAML user provisioning is disabled when SCIM is enabled"
(mt/with-premium-features #{:sso-saml :scim}
(mt/with-temporary-setting-values [saml-user-provisioning-enabled? true
scim-enabled true]
(is (false? (sso-settings/saml-user-provisioning-enabled?)))))))
(deftest jwt-settings-token-features-test
(testing "Getting JWT settings should return their default values without :sso-jwt feature flag enabled"
(doseq [feature? [true false]]
(mt/with-premium-features #{:sso-jwt}
(tu/with-temporary-setting-values [jwt-identity-provider-uri default-idp-uri
jwt-shared-secret "01234"
jwt-attribute-email "not default email"
jwt-attribute-firstname "not default first_name"
jwt-attribute-lastname "not default last_name"
jwt-attribute-groups "not default groups"
jwt-group-sync true
jwt-group-mappings {:group_id [1]}
jwt-enabled true]
(mt/with-premium-features (if feature? #{:sso-jwt} #{})
(is (= (if feature? default-idp-uri nil)
(sso-settings/jwt-identity-provider-uri)))
(is (= (if feature? "01234" nil)
(sso-settings/jwt-shared-secret)))
(is (= (if feature? "not default email" "email")
(sso-settings/jwt-attribute-email)))
(is (= (if feature? "not default first_name" "first_name")
(sso-settings/jwt-attribute-firstname)))
(is (= (if feature? "not default last_name" "last_name")
(sso-settings/jwt-attribute-lastname)))
(is (= (if feature? true false)
(sso-settings/jwt-group-sync)))
(is (= (if feature? "not default groups" "groups")
(sso-settings/jwt-attribute-groups)))
(is (= (if feature? {:group_id [1]} {})
(sso-settings/jwt-group-mappings)))
(is (= (if feature? true false)
(sso-settings/jwt-enabled)))))))))
(deftest set-jwt-settings-token-features-test
(testing "Setting JWT settings should error without the :sso-jwt feature flag enabled"
(mt/with-premium-features #{}
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting jwt-identity-provider-uri is not enabled because feature :sso-jwt is not available"
(sso-settings/jwt-identity-provider-uri! default-idp-uri)))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting jwt-shared-secret is not enabled because feature :sso-jwt is not available"
(sso-settings/jwt-shared-secret! "01234")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting jwt-attribute-email is not enabled because feature :sso-jwt is not available"
(sso-settings/jwt-attribute-email! "email")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting jwt-attribute-firstname is not enabled because feature :sso-jwt is not available"
(sso-settings/jwt-attribute-firstname! "first_name")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting jwt-attribute-lastname is not enabled because feature :sso-jwt is not available"
(sso-settings/jwt-attribute-lastname! "last_name")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting jwt-group-sync is not enabled because feature :sso-jwt is not available"
(sso-settings/jwt-group-sync! true)))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting jwt-attribute-groups is not enabled because feature :sso-jwt is not available"
(sso-settings/jwt-attribute-groups! "groups")))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting jwt-group-mappings is not enabled because feature :sso-jwt is not available"
(sso-settings/jwt-group-mappings! {:group_id [1]})))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting jwt-enabled is not enabled because feature :sso-jwt is not available"
(sso-settings/jwt-enabled! true))))))
(deftest jwt-enabled-without-configuration-test
(testing "jwt-enabled returns true even when JWT is not configured"
(mt/with-premium-features #{:sso-jwt}
(tu/with-temporary-setting-values [jwt-enabled true
jwt-identity-provider-uri nil
jwt-shared-secret nil]
(is (true? (sso-settings/jwt-enabled)))
(is (false? (sso-settings/jwt-configured)))
(is (false? (sso-settings/jwt-enabled-and-configured))))))
(testing "jwt-enabled-and-configured returns true only when both enabled and configured"
(mt/with-premium-features #{:sso-jwt}
(tu/with-temporary-setting-values [jwt-enabled true
jwt-identity-provider-uri "example.com"
jwt-shared-secret "0123456789012345678901234567890123456789012345678901234567890123"]
(is (true? (sso-settings/jwt-enabled)))
(is (true? (sso-settings/jwt-configured)))
(is (true? (sso-settings/jwt-enabled-and-configured)))))))
(deftest can-turn-off-password-login-with-jwt-enabled
(mt/with-premium-features #{:sso-jwt}
(tu/with-temporary-setting-values [jwt-enabled true
jwt-identity-provider-uri "example.com"
jwt-shared-secret "0123456789012345678901234567890123456789012345678901234567890123"
enable-password-login true]
(testing "can't change enable-password-login setting if disabled-password-login feature is disabled"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Setting enable-password-login is not enabled because feature :disable-password-login is not available"
(session/enable-password-login! false))))
(testing "can change enable-password-login setting if jwt enabled and have disabled-password-login feature"
(mt/with-additional-premium-features #{:disable-password-login}
(session/enable-password-login! false)
(is (= false
(session/enable-password-login))))))))
(deftest sso-source-enabled?-saml-test
(testing "sso-source-enabled? for SAML"
(mt/with-premium-features #{:sso-saml}
(tu/with-temporary-setting-values [saml-identity-provider-uri default-idp-uri
saml-identity-provider-certificate default-idp-cert
saml-enabled true]
(testing "returns true when SAML is configured, enabled, and licensed"
(is (true? (sso.settings/sso-source-enabled? :saml))))
(testing "returns false when SAML feature is unlicensed (e.g., license downgrade)"
(mt/with-premium-features #{}
(is (false? (sso.settings/sso-source-enabled? :saml)))))))))
(deftest sso-source-enabled?-jwt-test
(testing "sso-source-enabled? for JWT"
(mt/with-premium-features #{:sso-jwt}
(tu/with-temporary-setting-values [jwt-identity-provider-uri default-idp-uri
jwt-shared-secret "01234"
jwt-enabled true]
(testing "returns true when JWT is configured, enabled, and licensed"
(is (true? (sso.settings/sso-source-enabled? :jwt))))
(testing "returns false when JWT feature is unlicensed (e.g., license downgrade)"
(mt/with-premium-features #{}
(is (false? (sso.settings/sso-source-enabled? :jwt)))))))))
(deftest sso-source-enabled?-google-test
(testing "sso-source-enabled? for Google"
(mt/with-temporary-setting-values [google-auth-client-id "pretend-client-id.apps.googleusercontent.com"
google-auth-enabled true]
(is (true? (sso.settings/sso-source-enabled? :google))))
(mt/with-temporary-setting-values [google-auth-enabled false]
(is (false? (sso.settings/sso-source-enabled? :google))))))
(deftest sso-source-enabled?-unknown-test
(testing "sso-source-enabled? returns false for unknown sources"
(is (false? (sso.settings/sso-source-enabled? :unknown-provider)))))
(deftest other-sso-enabled?-test
(testing "other-sso-enabled? gates the `/auth/sso` login button (SAML/JWT only)"
(testing "false when nothing is enabled"
(mt/with-premium-features #{:sso-saml :sso-jwt}
(is (false? (sso-settings/other-sso-enabled?)))))
(testing "true when SAML is enabled and configured"
(mt/with-premium-features #{:sso-saml}
(tu/with-temporary-setting-values [saml-identity-provider-uri default-idp-uri
saml-identity-provider-certificate default-idp-cert
saml-enabled true]
(is (true? (sso-settings/other-sso-enabled?))))))
(testing "true when JWT is enabled and configured"
(mt/with-premium-features #{:sso-jwt}
(tu/with-temporary-setting-values [jwt-identity-provider-uri default-idp-uri
jwt-shared-secret "01234"
jwt-enabled true]
(is (true? (sso-settings/other-sso-enabled?))))))
(testing "false when only Slack Connect is enabled (UXW-3940 regression)"
;; Slack Connect uses /auth/sso/slack-connect, not /auth/sso, so it must
;; not flip on the SAML/JWT login button. See UXW-3940.
(sso.test-helpers/with-slack-default-setup!
(is (true? (sso.settings/slack-connect-enabled)))
(is (false? (sso-settings/other-sso-enabled?)))))
(testing "false when only OIDC is enabled (OIDC uses /auth/sso/oidc, not /auth/sso)"
(mt/with-premium-features #{:sso-oidc}
(tu/with-temporary-setting-values [oidc-providers [{:key "test-idp"
:login-prompt "Test IdP"
:issuer-uri "https://test.idp.example.com"
:client-id "test-client-id"
:client-secret "test-client-secret"
:scopes ["openid" "email" "profile"]
:enabled true}]]
(is (false? (sso-settings/other-sso-enabled?))))))))
(deftest enable-password-login-honors-oidc-as-sso-test
(testing "enable-password-login is honored when OIDC is the only SSO provider"
(mt/with-premium-features #{:sso-oidc :disable-password-login}
(tu/with-temporary-setting-values [oidc-providers [{:key "test-idp"
:login-prompt "Test IdP"
:issuer-uri "https://test.idp.example.com"
:client-id "test-client-id"
:client-secret "test-client-secret"
:scopes ["openid" "email" "profile"]
:enabled true}]
enable-password-login false]
(is (true? (sso.settings/sso-enabled?))
"sso-enabled? should be true when OIDC is the only provider")
(is (false? (session/enable-password-login))
"enable-password-login should be honored when OIDC is enabled")))))
(deftest saml-settings-clear-flips-configured-flag-test
(testing "clearing the SAML idp settings bundle flips saml-configured and saml-enabled back to false"
(mt/with-premium-features #{:sso-saml}
(tu/with-temporary-setting-values [saml-identity-provider-uri default-idp-uri
saml-identity-provider-certificate default-idp-cert
saml-enabled true]
(is (true? (sso-settings/saml-configured)))
(is (true? (sso-settings/saml-enabled))))
(tu/with-temporary-setting-values [saml-identity-provider-uri nil
saml-identity-provider-certificate nil
saml-enabled true]
(is (false? (sso-settings/saml-configured)))
(is (false? (sso-settings/saml-enabled)))))))