Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Assert only on password reset email count (#79721)

Ben Grabowe29527e5b3f0
diff --git a/enterprise/backend/test/metabase_enterprise/support_access_grants/session_integration_test.clj b/enterprise/backend/test/metabase_enterprise/support_access_grants/session_integration_test.clj
index 65a52a6dcba..f4920fb3a76 100644
--- a/enterprise/backend/test/metabase_enterprise/support_access_grants/session_integration_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/support_access_grants/session_integration_test.clj
@@ -13,13 +13,31 @@
[metabase.session.api :as api.session]
[metabase.session.core :as session]
[metabase.test :as mt]
+ [metabase.test.fixtures :as fixtures]
[metabase.util :as u]
[toucan2.core :as t2]))
+;; :test-users is load-bearing, not boilerplate -- deactivating the support user is only legal while another admin
+;; exists, and crowberto is that admin.
+(use-fixtures :once (fixtures/initialize :db :web-server :test-users))
+
(use-fixtures :each (fn [f] (mt/with-premium-features #{:support-access-grants}
(with-redefs [api.session/throttling-disabled? true]
(f)))))
+(defn- password-reset-emails
+ "Messages in the fake inbox for `email` sent by /api/session/forgot_password.
+
+ Other senders reach the support address too - notably the `:event/support-access-grant-created`
+ notification, which fires on a background thread and also carries a reset URL - so match on the
+ subject rather than counting everything in the inbox."
+ [email]
+ (filter #(re-find #"Password Reset Request" (:subject %)) (get @mt/inbox email)))
+
+(defn- email-body
+ [message]
+ (-> message :body first :content))
+
(deftest reset-password-with-support-access-grant-token-test
(testing "POST /api/session/reset_password works with support access grant token"
(mt/with-temp [:model/User {creator-id :id} {}]
@@ -325,7 +343,7 @@
(mt/user-http-request user :post 204 "session/forgot_password" {:email (:email user)})
(is (not (t2/exists? :model/AuthIdentity :user_id (:id user) :provider "emailed-secret-password-reset"))
"Should not create a normal password reset token")
- (is (empty? @mt/inbox)
+ (is (empty? (password-reset-emails (:email user)))
"No email should be sent when the grant has no active credentials")))))))
(deftest forgot-password-support-user-active-grant-test
@@ -345,10 +363,10 @@
(mt/with-fake-inbox
(mt/client :post 204 "session/forgot_password" {:email "[email protected]"})
(testing "Email is sent with a valid reset URL"
- (let [emails (get @mt/inbox "[email protected]")]
- (is (= 1 (count emails)) "Should send exactly one email")
- (is (mt/received-email-body? "[email protected]"
- #"http://test.example.com/auth/reset_password/\d+_")
+ (let [emails (password-reset-emails "[email protected]")]
+ (is (= 1 (count emails)) "Should send exactly one password reset email")
+ (is (re-find #"http://test.example.com/auth/reset_password/\d+_"
+ (email-body (first emails)))
"Email should contain a reset URL")))
(testing "No normal password reset token is created"
(let [support-user (t2/select-one :model/User :email "[email protected]")]
@@ -383,10 +401,10 @@
;; Step 1: Request a password reset for the support user.
(mt/client :post 204 "session/forgot_password" {:email "[email protected]"})
;; Step 2: Extract the refreshed token from the email.
- (let [emails (get @mt/inbox "[email protected]")
- _ (is (= 1 (count emails)) "Should send exactly one email")
- email-body (-> emails first :body first :content)
- [_ reset-token] (re-find #"/auth/reset_password/(\d+_[\w_-]+)" email-body)
+ (let [emails (password-reset-emails "[email protected]")
+ _ (is (= 1 (count emails)) "Should send exactly one password reset email")
+ [_ reset-token] (re-find #"/auth/reset_password/(\d+_[\w_-]+)"
+ (email-body (first emails)))
_ (is (some? reset-token) "Should find reset token in email")
new-password "ResetViaForgot123!"
support-user (t2/select-one :model/User :email "[email protected]")]