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
Don't email admin users who haven't accepted invite (#74883)
Timothy S. Deandc017f71a72f
diff --git a/src/metabase/channel/email/messages.clj b/src/metabase/channel/email/messages.cljindex bb7a8151314..6c69f7ebb37 100644--- a/src/metabase/channel/email/messages.clj+++ b/src/metabase/channel/email/messages.clj@@ -108,14 +108,21 @@;;; ### Public Interface(defn all-admin-recipients- "Return a sequence of email addresses for all Admin users.+ "Return a sequence of email addresses for all Admin users who have accepted their invitation (i.e. have logged in at+ least once). Admins who have been invited but not yet accepted are excluded — they shouldn't receive notifications+ about activity in an instance they haven't joined.- The first recipient will be the site admin (or oldest admin if unset), which is the address that should be used in- `mailto` links (e.g., for the new user to email with any questions)."+ The first recipient will be the site admin (or oldest accepted admin if unset), which is the address that should be+ used in `mailto` links (e.g., for the new user to email with any questions)."[](concat (when-let [admin-email (system/admin-email)][admin-email])- (t2/select-fn-set :email 'User, :is_superuser true, :is_active true, :type "personal" {:order-by [[:id :asc]]})))+ (t2/select-fn-set :email 'User+ :is_superuser true+ :is_active true+ :last_login [:not= nil]+ :type "personal"+ {:order-by [[:id :asc]]})))(defn send-user-joined-admin-notification-email!"Send an email to the `invitor` (the Admin who invited `new-user`) letting them know `new-user` has joined."diff --git a/test/metabase/channel/email/messages_test.clj b/test/metabase/channel/email/messages_test.cljindex 1cf6a471d32..5fe61c049dd 100644--- a/test/metabase/channel/email/messages_test.clj+++ b/test/metabase/channel/email/messages_test.clj@@ -9,7 +9,8 @@[metabase.test :as mt][metabase.test.util :as tu][metabase.util.retry :as retry]- [metabase.util.retry-test :as rt]))+ [metabase.util.retry-test :as rt]+ [toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -98,12 +99,22 @@(is (= 1 (count @mt/inbox)))))))))(deftest all-admin-recipients- (mt/with-temp [:model/ApiKey _ {::api-key/unhashed-key (api-key/generate-key)- :name "Test API key"- :user_id (mt/user->id :crowberto)- :creator_id (mt/user->id :crowberto)- :updated_by_id (mt/user->id :crowberto)}]- (testing "all-admin-recipients returns all admin emails"- (let [emails (#'messages/all-admin-recipients)]- (is (some #(= % "[email protected]") emails))+ (mt/with-temp [:model/User accepted-admin {:is_superuser true+ :email "[email protected]"}+ :model/User _pending-admin {:is_superuser true+ :email "[email protected]"}+ :model/ApiKey _ {::api-key/unhashed-key (api-key/generate-key)+ :name "Test API key"+ :user_id (mt/user->id :crowberto)+ :creator_id (mt/user->id :crowberto)+ :updated_by_id (mt/user->id :crowberto)}]+ ;; `mt/with-temp` creates Users with `last_login = nil`, which the recipient filter treats as+ ;; "invited but not accepted." Mark one as having logged in to represent an accepted admin.+ (t2/update! :model/User (:id accepted-admin) {:last_login :%now})+ (let [emails (set (#'messages/all-admin-recipients))]+ (testing "includes admins who have accepted their invitation"+ (is (contains? emails "[email protected]")))+ (testing "excludes admins who have not yet accepted their invitation"+ (is (not (contains? emails "[email protected]"))))+ (testing "excludes API key users"(is (not (some #(str/starts-with? % "api-key-user") emails)))))))diff --git a/test/metabase/users/models/user_test.clj b/test/metabase/users/models/user_test.cljindex ff3cddf04b7..a5a5fc104e3 100644--- a/test/metabase/users/models/user_test.clj+++ b/test/metabase/users/models/user_test.clj@@ -78,7 +78,18 @@(def ^:private default-invitor{:email "[email protected]", :is_active true, :first_name "Crowberto"})-;; admin shouldn't get email saying user joined until they accept the invite (i.e., reset their password)+;; Admins are notified of new joins via `all-admin-recipients`, which excludes admins who haven't yet accepted their+;; own invitation (i.e. `last_login` is still nil). These tests therefore construct fresh admin users with explicit+;; `last_login` state — relying on seeded users like `:crowberto` is fragile because their `last_login` is set as a+;; side effect of earlier tests authenticating as them.++(defn- with-fresh-accepted-admin!+ "Create a temp admin User with `last_login` set (representing an admin who has accepted their invitation), pass them+ to `f`, then let `mt/with-temp` clean them up."+ [f]+ (mt/with-temp [:model/User admin {:is_superuser true, :email "[email protected]"}]+ (t2/update! :model/User (:id admin) {:last_login :%now})+ (f admin)))(deftest new-user-invite-email-test(notification.tu/with-send-notification-sync@@ -89,71 +100,99 @@(deftest admin-email-on-user-acceptance-test(notification.tu/with-send-notification-sync(testing "admin should get an email when a new user joins"- (is (= {"<New User>" ["You're invited to join Metabase's Metabase"]- "[email protected]" ["<New User> accepted their Metabase invite"]}- (-> (invite-user-accept-and-check-inboxes! :invitor default-invitor)- (select-keys ["<New User>" "[email protected]"])))))))+ (with-fresh-accepted-admin!+ (fn [admin]+ (is (= {"<New User>" ["You're invited to join Metabase's Metabase"]+ (:email admin) ["<New User> accepted their Metabase invite"]}+ (-> (invite-user-accept-and-check-inboxes! :invitor default-invitor)+ (select-keys ["<New User>" (:email admin)])))))))))(deftest admin-email-with-site-admin-test(notification.tu/with-send-notification-sync(testing "site admin should also get email when user joins"- (mt/with-temporary-setting-values [admin-email "[email protected]"]- (is (= {"<New User>" ["You're invited to join Metabase's Metabase"]- "[email protected]" ["<New User> accepted their Metabase invite"]- "[email protected]" ["<New User> accepted their Metabase invite"]}- (-> (invite-user-accept-and-check-inboxes! :invitor default-invitor)- (select-keys ["<New User>" "[email protected]" "[email protected]"]))))))))+ (with-fresh-accepted-admin!+ (fn [admin]+ (mt/with-temporary-setting-values [admin-email "[email protected]"]+ (is (= {"<New User>" ["You're invited to join Metabase's Metabase"]+ (:email admin) ["<New User> accepted their Metabase invite"]+ "[email protected]" ["<New User> accepted their Metabase invite"]}+ (-> (invite-user-accept-and-check-inboxes! :invitor default-invitor)+ (select-keys ["<New User>" (:email admin) "[email protected]"]))))))))))(deftest inactive-admin-no-email-test(notification.tu/with-send-notification-sync(testing "inactive admin should not get email when user joins"- (mt/with-temp [:model/User inactive-admin {:is_superuser true, :is_active false}]- (is (= {"<New User>" ["You're invited to join Metabase's Metabase"]- "[email protected]" ["<New User> accepted their Metabase invite"]}- (-> (invite-user-accept-and-check-inboxes! :invitor (assoc inactive-admin :is_active false))- (select-keys ["<New User>" "[email protected]" (:email inactive-admin)]))))))))+ (with-fresh-accepted-admin!+ (fn [admin]+ (mt/with-temp [:model/User inactive-admin {:is_superuser true, :is_active false}]+ (is (= {"<New User>" ["You're invited to join Metabase's Metabase"]+ (:email admin) ["<New User> accepted their Metabase invite"]}+ (-> (invite-user-accept-and-check-inboxes! :invitor (assoc inactive-admin :is_active false))+ (select-keys ["<New User>" (:email admin) (:email inactive-admin)]))))))))))++(deftest pending-admin-no-email-test+ (notification.tu/with-send-notification-sync+ (testing "an admin who hasn't yet accepted their own invitation should not receive user-joined emails"+ (with-fresh-accepted-admin!+ (fn [accepted-admin]+ (mt/with-temp [:model/User pending-admin {:is_superuser true+ :email "[email protected]"}]+ ;; pending-admin keeps last_login = nil — they've been invited but haven't accepted.+ (let [inboxes (invite-user-accept-and-check-inboxes! :invitor default-invitor)]+ (is (nil? (get inboxes (:email pending-admin)))+ "pending admin should not be notified")+ (is (= ["<New User> accepted their Metabase invite"]+ (get inboxes (:email accepted-admin)))+ "accepted admin should still be notified"))))))))(deftest google-auth-admin-emails-test(notification.tu/with-send-notification-sync- (testing "for google auth, all admins should get an email"+ (testing "for google auth, all accepted admins should get an email"(mt/with-temporary-raw-setting-values [send-new-sso-user-admin-email? "true"]- (mt/with-temp [:model/User _ {:is_superuser true, :email "[email protected]"}]- (is (= {"[email protected]" ["<New User> created a Metabase account"]- "[email protected]" ["<New User> created a Metabase account"]}+ (mt/with-temp [:model/User admin-1 {:is_superuser true, :email "[email protected]"}+ :model/User admin-2 {:is_superuser true, :email "[email protected]"}]+ (t2/update! :model/User (:id admin-1) {:last_login :%now})+ (t2/update! :model/User (:id admin-2) {:last_login :%now})+ (is (= {(:email admin-1) ["<New User> created a Metabase account"]+ (:email admin-2) ["<New User> created a Metabase account"]}(-> (invite-user-accept-and-check-inboxes! :google-auth? true)- (select-keys ["[email protected]" "[email protected]"])))))))))+ (select-keys [(:email admin-1) (:email admin-2)])))))))))(deftest google-auth-site-admin-email-test(notification.tu/with-send-notification-sync(testing "for google auth, site admin should also get email"(mt/with-temporary-raw-setting-values [send-new-sso-user-admin-email? "true"](mt/with-temporary-raw-setting-values [admin-email "[email protected]"]- (mt/with-temp [:model/User _ {:is_superuser true, :email "[email protected]"}]- (is (= {"[email protected]" ["<New User> created a Metabase account"]- "[email protected]" ["<New User> created a Metabase account"]- "[email protected]" ["<New User> created a Metabase account"]}- (-> (invite-user-accept-and-check-inboxes! :google-auth? true)+ (with-fresh-accepted-admin!+ (fn [admin]+ (is (= {(:email admin) ["<New User> created a Metabase account"]+ "[email protected]" ["<New User> created a Metabase account"]}+ (-> (invite-user-accept-and-check-inboxes! :google-auth? true)+ (select-keys [(:email admin) "[email protected]"])))))))))))(deftest google-auth-inactive-admin-no-email-test(notification.tu/with-send-notification-sync(testing "for google auth, inactive admin should not get email"(mt/with-temporary-raw-setting-values [send-new-sso-user-admin-email? "true"]- (mt/with-temp [:model/User user {:is_superuser true, :is_active false}]- (is (= {"[email protected]" ["<New User> created a Metabase account"]}- (-> (invite-user-accept-and-check-inboxes! :google-auth? true)- (select-keys ["[email protected]" (:email user)])))))))))+ (with-fresh-accepted-admin!+ (fn [admin]+ (mt/with-temp [:model/User inactive {:is_superuser true, :is_active false}]+ (is (= {(:email admin) ["<New User> created a Metabase account"]}+ (-> (invite-user-accept-and-check-inboxes! :google-auth? true)+ (select-keys [(:email admin) (:email inactive)])))))))))))(deftest google-auth-setting-disabled-test(notification.tu/with-send-notification-sync(testing "for google auth, no emails sent if setting is disabled"(mt/with-premium-features #{:sso-ldap}(mt/with-temporary-raw-setting-values [send-new-sso-user-admin-email? "false"]- (mt/with-temp [:model/User _ {:is_superuser true, :email "[email protected]"}]- (is (= (if config/ee-available? {} {"[email protected]" ["<New User> created a Metabase account"],- "[email protected]" ["<New User> created a Metabase account"]})- (-> (invite-user-accept-and-check-inboxes! :google-auth? true)- (select-keys ["[email protected]" "[email protected]"]))))))))))+ (with-fresh-accepted-admin!+ (fn [admin]+ (is (= (if config/ee-available?+ {}+ {(:email admin) ["<New User> created a Metabase account"]})+ (-> (invite-user-accept-and-check-inboxes! :google-auth? true)+ (select-keys [(:email admin)])))))))))))(deftest sso-login-link-email-test(notification.tu/with-send-notification-sync