0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-23 23:18:48 -05:00

Add some srepl helpers for resend email verification

This commit is contained in:
Andrey Antukh 2022-09-20 14:13:19 +02:00
parent 3c2ba92f6c
commit c4aba025c4
4 changed files with 92 additions and 36 deletions

View file

@ -297,6 +297,27 @@
(assoc :default-team-id (:id team)) (assoc :default-team-id (:id team))
(assoc :default-project-id (:default-project-id team))))) (assoc :default-project-id (:default-project-id team)))))
(defn send-email-verification!
[conn sprops profile]
(let [vtoken (tokens/generate sprops
{:iss :verify-email
:exp (dt/in-future "72h")
:profile-id (:id profile)
:email (:email profile)})
;; NOTE: this token is mainly used for possible complains
;; identification on the sns webhook
ptoken (tokens/generate sprops
{:iss :profile-identity
:profile-id (:id profile)
:exp (dt/in-future {:days 30})})]
(eml/send! {::eml/conn conn
::eml/factory eml/register
:public-uri (cf/get :public-uri)
:to (:email profile)
:name (:fullname profile)
:token vtoken
:extra-data ptoken})))
(defn register-profile (defn register-profile
[{:keys [conn sprops session] :as cfg} {:keys [token] :as params}] [{:keys [conn sprops session] :as cfg} {:keys [token] :as params}]
(let [claims (tokens/verify sprops {:token token :iss :prepared-register}) (let [claims (tokens/verify sprops {:token token :iss :prepared-register})
@ -342,23 +363,8 @@
;; In all other cases, send a verification email. ;; In all other cases, send a verification email.
:else :else
(let [vtoken (tokens/generate sprops (do
{:iss :verify-email (send-email-verification! conn sprops profile)
:exp (dt/in-future "48h")
:profile-id (:id profile)
:email (:email profile)})
ptoken (tokens/generate sprops
{:iss :profile-identity
:profile-id (:id profile)
:exp (dt/in-future {:days 30})})]
(eml/send! {::eml/conn conn
::eml/factory eml/register
:public-uri (:public-uri cfg)
:to (:email profile)
:name (:fullname profile)
:token vtoken
:extra-data ptoken})
(with-meta profile (with-meta profile
{::audit/replace-props (audit/profile->props profile) {::audit/replace-props (audit/profile->props profile)
::audit/profile-id (:id profile)})))))) ::audit/profile-id (:id profile)}))))))

View file

@ -169,8 +169,7 @@
params (assoc params params (assoc params
:profile profile :profile profile
:email (str/lower email))] :email (str/lower email))]
(if (or (cf/get :smtp-enabled) (if (contains? cf/flags :smtp)
(contains? cf/flags :smtp))
(request-email-change cfg params) (request-email-change cfg params)
(change-email-immediately cfg params))))) (change-email-immediately cfg params)))))

View file

@ -10,9 +10,15 @@
(:require (:require
[app.common.logging :as l] [app.common.logging :as l]
[app.common.pprint :as p] [app.common.pprint :as p]
[app.common.spec :as us]
[app.db :as db]
[app.rpc.commands.auth :as cmd.auth]
[app.rpc.queries.profile :as profile]
[app.srepl.fixes :as f] [app.srepl.fixes :as f]
[app.srepl.helpers :as h] [app.srepl.helpers :as h]
[clojure.pprint :refer [pprint]])) [app.util.time :as dt]
[clojure.pprint :refer [pprint]]
[cuerdas.core :as str]))
(defn print-available-tasks (defn print-available-tasks
[system] [system]
@ -30,7 +36,53 @@
(defn send-test-email! (defn send-test-email!
[system destination] [system destination]
(us/verify!
:expr (some? system)
:hint "system should be provided")
(us/verify!
:expr (string? destination)
:hint "destination should be provided")
(let [handler (:app.emails/sendmail system)] (let [handler (:app.emails/sendmail system)]
(handler {:body "test email" (handler {:body "test email"
:subject "test email" :subject "test email"
:to [destination]}))) :to [destination]})))
(defn resend-email-verification-email!
[system email]
(us/verify!
:expr (some? system)
:hint "system should be provided")
(let [sprops (:app.setup/props system)
pool (:app.db/pool system)
profile (profile/retrieve-profile-data-by-email pool email)]
(cmd.auth/send-email-verification! pool sprops profile)
:email-sent))
(defn update-profile
"Update a limited set of profile attrs."
[system & {:keys [email id active? deleted?]}]
(us/verify!
:expr (some? system)
:hint "system should be provided")
(us/verify!
:expr (or (string? email) (uuid? id))
:hint "email or id should be provided")
(let [pool (:app.db/pool system)
params (cond-> {}
(true? active?) (assoc :is-active true)
(false? active?) (assoc :is-active false)
(true? deleted?) (assoc :deleted-at (dt/now)))
opts (cond-> {}
(some? email) (assoc :email (str/lower email))
(some? id) (assoc :id id))]
(some-> (db/update! pool :profile params opts)
(profile/decode-profile-row))))

View file

@ -97,7 +97,7 @@
:profile-id (:id profile)} :profile-id (:id profile)}
out (th/query! data)] out (th/query! data)]
;; (th/print-result! out) #_(th/print-result! out)
(t/is (nil? (:error out))) (t/is (nil? (:error out)))
(let [result (:result out)] (let [result (:result out)]
@ -338,22 +338,21 @@
(t/deftest test-email-change-request-without-smtp (t/deftest test-email-change-request-without-smtp
(with-mocks [email-send-mock {:target 'app.emails/send! :return nil} (with-mocks [email-send-mock {:target 'app.emails/send! :return nil}]
cfg-get-mock {:target 'app.config/get (with-redefs [app.config/flags #{}]
:return (th/mock-config-get-with (let [profile (th/create-profile* 1)
{:smtp-enabled false})}] pool (:app.db/pool th/*system*)
(let [profile (th/create-profile* 1) data {::th/type :request-email-change
pool (:app.db/pool th/*system*) :profile-id (:id profile)
data {::th/type :request-email-change :email "user1@example.com"}]
:profile-id (:id profile)
:email "user1@example.com"}]
;; without complaints (let [out (th/mutation! data)
(let [out (th/mutation! data) res (:result out)]
res (:result out)]
(t/is (= {:changed true} res)) ;; (th/print-result! out)
(let [mock (deref email-send-mock)] (t/is (= {:changed true} res))
(t/is (false? (:called? mock)))))))) (let [mock (deref email-send-mock)]
(t/is (false? (:called? mock)))))))))
(t/deftest test-request-profile-recovery (t/deftest test-request-profile-recovery