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
Fix test-user caching across appdb swaps (#80436)
Chris Truter59b869629638
diff --git a/test/metabase/test/data/users.clj b/test/metabase/test/data/users.cljindex 119bee2b8a5..227601965e3 100644--- a/test/metabase/test/data/users.clj+++ b/test/metabase/test/data/users.clj@@ -90,14 +90,18 @@;;;;;; If we run INSIDE of a transaction before the test users have been created globally then do not memoize the IDs,;;; since the users will get discarded and we will need to recreate them next time around.+;;;+;;; "Globally" is per application database: [[metabase.test.data/with-empty-h2-app-db!]] swaps in an empty+;;; one, where the users another database created do not exist. Tracking it as a boolean meant the first+;;; branch handed back ids for absent users, and the session insert failed on FK_SESSION_REF_USER_ID.(let [f (fn [](zipmap usernames(map (comp u/the-id fetch-user) usernames)))memoized (mdb/memoize-for-application-db f)- created-globally? (atom false)+ created-globally (atom #{})f* (fn [](cond- @created-globally?+ (contains? @created-globally (mdb/unique-identifier))(memoized)(mdb/in-transaction?)@@ -105,7 +109,7 @@:else(u/prog1 (memoized)- (reset! created-globally? true))))]+ (swap! created-globally conj (mdb/unique-identifier)))))](mu/defn user->id"Creates user if needed. With zero args, returns map of user name to ID. With 1 arg, returns ID of that User. CreatesUser(s) if needed. Memoized if not ran inside of a transaction.@@ -140,6 +144,11 @@{:username email:password password}))+;; {[app-db username] session-key}+;;+;; The app db is in the key because `with-empty-h2-app-db!` swaps in a different one. A token from the old+;; database names a session the new one has never heard of, so the request 401s. Tokens minted while the empty+;; db is in place would stick around after the original comes back, too.(defonce ^:private tokens (atom {}));;; This is done by hitting the app DB directly instead of hitting [[metabase.test.http-client/authenticate]] to avoid@@ -166,13 +175,14 @@(mu/defn username->token :- ms/UUIDString"Return cached session token for a test User, logging in first if needed."[username :- TestUserName]- (or (@tokens username)- (locking tokens- (or (@tokens username)- (u/prog1 (authenticate! username)- (swap! tokens assoc username <>))))- (throw (Exception. (format "Authentication failed for %s with credentials %s"- username (user->credentials username))))))+ (let [cache-key [(mdb/unique-identifier) username]]+ (or (@tokens cache-key)+ (locking tokens+ (or (@tokens cache-key)+ (u/prog1 (authenticate! username)+ (swap! tokens assoc cache-key <>))))+ (throw (Exception. (format "Authentication failed for %s with credentials %s"+ username (user->credentials username)))))))(defn clear-cached-session-tokens!"Clear any cached session tokens, which may have expired or been removed. You should do this in the even you get a