Optimize demo user setup for performance tests

Use UUID-based demo emails to prevent concurrent profile collisions.\nUse fast PBKDF2 hashing for demo profiles while keeping regular user hashing unchanged.\nAdd focused coverage for hashing, email uniqueness, and the feature flag.\n\nAI-assisted-by: gpt-5.6-luna
This commit is contained in:
Andrey Antukh 2026-08-17 12:01:49 +00:00
parent f96d850049
commit 188a9cb29e
3 changed files with 54 additions and 5 deletions

View File

@ -14,10 +14,19 @@
:iterations 3 :iterations 3
:parallelism 2}) :parallelism 2})
(def ^:private weak-options
{:alg :pbkdf2+sha256
:iterations 100})
(defn derive-password (defn derive-password
[password] [password]
(hashers/derive password default-options)) (hashers/derive password default-options))
(defn derive-password-weak
"Derives a password with a fast algorithm for demo users."
[password]
(hashers/derive password weak-options))
(defn verify-password (defn verify-password
[attempt password] [attempt password]
(try (try

View File

@ -7,9 +7,10 @@
(ns app.rpc.commands.demo (ns app.rpc.commands.demo
"A demo specific mutations." "A demo specific mutations."
(:require (:require
[app.auth :refer [derive-password]] [app.auth :refer [derive-password-weak]]
[app.common.exceptions :as ex] [app.common.exceptions :as ex]
[app.common.time :as ct] [app.common.time :as ct]
[app.common.uuid :as uuid]
[app.config :as cf] [app.config :as cf]
[app.db :as db] [app.db :as db]
[app.loggers.audit :as audit] [app.loggers.audit :as audit]
@ -34,8 +35,8 @@
:code :demo-users-not-allowed :code :demo-users-not-allowed
:hint "Demo users are disabled by config.")) :hint "Demo users are disabled by config."))
(let [sem (System/currentTimeMillis) (let [sem (uuid/next)
email (str "demo-" sem ".demo@example.com") email (str "demo-" sem "@demo.example.com")
fullname (str "Demo User " sem) fullname (str "Demo User " sem)
password (-> (bn/random-bytes 16) password (-> (bn/random-bytes 16)
@ -47,7 +48,7 @@
:is-active true :is-active true
:is-demo true :is-demo true
:deleted-at (ct/in-future (cf/get-deletion-delay)) :deleted-at (ct/in-future (cf/get-deletion-delay))
:password (derive-password password) :password (derive-password-weak password)
:props {}} :props {}}
profile (db/tx-run! cfg (fn [cfg] profile (db/tx-run! cfg (fn [cfg]
(->> (auth/create-profile cfg params) (->> (auth/create-profile cfg params)
@ -55,4 +56,3 @@
(with-meta {:email email (with-meta {:email email
:password password} :password password}
{::audit/profile-id (:id profile)}))) {::audit/profile-id (:id profile)})))

View File

@ -0,0 +1,40 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC Sucursal en España SL
(ns backend-tests.rpc-demo-test
(:require
[app.auth :as auth]
[app.config :as cf]
[backend-tests.helpers :as th]
[clojure.test :as t]))
(t/use-fixtures :once th/state-init)
(t/use-fixtures :each th/database-reset)
;; Capture the real verifier before the shared test fixture replaces it.
(def verify-password* auth/verify-password)
(t/deftest weak-password-hash-verifies
(let [password "DemoPassword123!"
hashed (auth/derive-password-weak password)]
(t/is (:valid (verify-password* password hashed)))))
(t/deftest create-demo-profile-uses-unique-uuid-email
(with-redefs [cf/flags (conj cf/flags :demo-users)]
(let [first-result (th/command! {::th/type :create-demo-profile})
second-result (th/command! {::th/type :create-demo-profile})
first-profile (:result first-result)
second-profile (:result second-result)]
(t/is (nil? (:error first-result)))
(t/is (nil? (:error second-result)))
(t/is (re-matches #"demo-[0-9a-fA-F-]+@demo\.example\.com"
(:email first-profile)))
(t/is (not= (:email first-profile) (:email second-profile))))))
(t/deftest create-demo-profile-requires-feature-flag
(with-redefs [cf/flags (disj cf/flags :demo-users)]
(let [{:keys [error]} (th/command! {::th/type :create-demo-profile})]
(t/is (th/ex-of-code? error :demo-users-not-allowed)))))