mirror of
https://github.com/penpot/penpot.git
synced 2025-05-06 19:15:55 +02:00
🐛 Fix many bugs in email sending namespaces.
This commit is contained in:
parent
e7db88a647
commit
d546d1fdca
6 changed files with 66 additions and 18 deletions
|
@ -51,6 +51,7 @@
|
||||||
org.clojure/tools.namespace {:mvn/version "0.3.1"}
|
org.clojure/tools.namespace {:mvn/version "0.3.1"}
|
||||||
fipp/fipp {:mvn/version "0.6.21"}
|
fipp/fipp {:mvn/version "0.6.21"}
|
||||||
criterium/criterium {:mvn/version "0.4.5"}
|
criterium/criterium {:mvn/version "0.4.5"}
|
||||||
|
mockery/mockery {:mvn/version "0.1.4"}
|
||||||
}
|
}
|
||||||
:extra-paths ["test"]}
|
:extra-paths ["test"]}
|
||||||
:repl {:main-opts ["-m" "rebel-readline.main"]}
|
:repl {:main-opts ["-m" "rebel-readline.main"]}
|
||||||
|
|
|
@ -29,8 +29,7 @@
|
||||||
[email context]
|
[email context]
|
||||||
(let [defaults {:from (:email-from cfg/config)
|
(let [defaults {:from (:email-from cfg/config)
|
||||||
:reply-to (:email-reply-to cfg/config)}]
|
:reply-to (:email-reply-to cfg/config)}]
|
||||||
(->> (email context)
|
(email (merge defaults context))))
|
||||||
(merge defaults))))
|
|
||||||
|
|
||||||
(defn send!
|
(defn send!
|
||||||
"Schedule the email for sending."
|
"Schedule the email for sending."
|
||||||
|
@ -39,8 +38,8 @@
|
||||||
(s/assert map? context)
|
(s/assert map? context)
|
||||||
(let [defaults {:from (:email-from cfg/config)
|
(let [defaults {:from (:email-from cfg/config)
|
||||||
:reply-to (:email-reply-to cfg/config)}
|
:reply-to (:email-reply-to cfg/config)}
|
||||||
data (->> (email context)
|
data (->> (merge defaults context)
|
||||||
(merge defaults)
|
(email)
|
||||||
(blob/encode))
|
(blob/encode))
|
||||||
priority (case (:priority context :high) :low 1 :high 10)
|
priority (case (:priority context :high) :low 1 :high 10)
|
||||||
sql "insert into email_queue (data, priority)
|
sql "insert into email_queue (data, priority)
|
||||||
|
|
|
@ -53,8 +53,7 @@
|
||||||
[conn id]
|
[conn id]
|
||||||
(let [sql "update email_queue
|
(let [sql "update email_queue
|
||||||
set status = 'ok'
|
set status = 'ok'
|
||||||
where id = $1
|
where id = $1"]
|
||||||
and deleted_at is null;"]
|
|
||||||
(-> (db/query-one conn [sql id])
|
(-> (db/query-one conn [sql id])
|
||||||
(p/then (constantly nil)))))
|
(p/then (constantly nil)))))
|
||||||
|
|
||||||
|
@ -111,11 +110,13 @@
|
||||||
(defn send-email
|
(defn send-email
|
||||||
[conn {:keys [id data] :as entry}]
|
[conn {:keys [id data] :as entry}]
|
||||||
(-> (impl-sendmail data)
|
(-> (impl-sendmail data)
|
||||||
(p/then (fn [_]
|
(p/handle (fn [v e]
|
||||||
(mark-email-as-sent conn id)))
|
(if e
|
||||||
(p/catch (fn [e]
|
(do
|
||||||
(log/error e "Error on sending email" id)
|
(log/error e "Error on sending email" id)
|
||||||
(mark-email-as-failed conn id)))))
|
(mark-email-as-failed conn id))
|
||||||
|
(mark-email-as-sent conn id))))))
|
||||||
|
|
||||||
|
|
||||||
;; --- Main Task Functions
|
;; --- Main Task Functions
|
||||||
|
|
||||||
|
@ -123,7 +124,8 @@
|
||||||
[opts]
|
[opts]
|
||||||
(db/with-atomic [conn db/pool]
|
(db/with-atomic [conn db/pool]
|
||||||
(p/let [items (fetch-emails conn)]
|
(p/let [items (fetch-emails conn)]
|
||||||
(p/run! (partial send-email conn) items))))
|
(-> (p/run! (partial send-email conn) items)
|
||||||
|
(p/then' (constantly (count items)))))))
|
||||||
|
|
||||||
(defn send-failed-emails
|
(defn send-failed-emails
|
||||||
[opts]
|
[opts]
|
||||||
|
|
|
@ -115,11 +115,13 @@
|
||||||
(defn- impl-query
|
(defn- impl-query
|
||||||
[conn sql params {:keys [xfm] :as opts}]
|
[conn sql params {:keys [xfm] :as opts}]
|
||||||
(let [conn (if (instance? IDeref conn) @conn conn)]
|
(let [conn (if (instance? IDeref conn) @conn conn)]
|
||||||
(->> (impl-execute conn sql params)
|
(-> (impl-execute conn sql params)
|
||||||
(p/map (fn [rows]
|
(p/catch' (fn [err]
|
||||||
(if xfm
|
(p/rejected err)))
|
||||||
(into [] xfm rows)
|
(p/then' (fn [rows]
|
||||||
(into [] (map vec) rows)))))))
|
(if xfm
|
||||||
|
(into [] xfm rows)
|
||||||
|
(into [] (map vec) rows)))))))
|
||||||
|
|
||||||
(defn impl-transact
|
(defn impl-transact
|
||||||
[pool f]
|
[pool f]
|
||||||
|
|
|
@ -19,7 +19,6 @@
|
||||||
(let [config (cfg/read-test-config)]
|
(let [config (cfg/read-test-config)]
|
||||||
(-> (mount/only #{#'uxbox.config/config
|
(-> (mount/only #{#'uxbox.config/config
|
||||||
#'uxbox.config/secret
|
#'uxbox.config/secret
|
||||||
;; #'uxbox.db/datasource
|
|
||||||
#'uxbox.core/system
|
#'uxbox.core/system
|
||||||
#'uxbox.db/pool
|
#'uxbox.db/pool
|
||||||
#'uxbox.migrations/migrations
|
#'uxbox.migrations/migrations
|
||||||
|
|
45
backend/test/uxbox/tests/test_emails.clj
Normal file
45
backend/test/uxbox/tests/test_emails.clj
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
;; 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) 2019 Andrey Antukh <niwi@niwi.nz>
|
||||||
|
|
||||||
|
(ns uxbox.tests.test-emails
|
||||||
|
(:require
|
||||||
|
[clojure.test :as t]
|
||||||
|
[promesa.core :as p]
|
||||||
|
[mockery.core :refer [with-mock]]
|
||||||
|
[uxbox.db :as db]
|
||||||
|
[uxbox.emails :as emails]
|
||||||
|
[uxbox.services.core :as sv]
|
||||||
|
[uxbox.tests.helpers :as th]))
|
||||||
|
|
||||||
|
(t/use-fixtures :once th/state-init)
|
||||||
|
(t/use-fixtures :each th/database-reset)
|
||||||
|
|
||||||
|
(t/deftest register-email-rendering
|
||||||
|
(let [result (emails/render emails/register {:to "example@uxbox.io"})]
|
||||||
|
(t/is (map? result))
|
||||||
|
(t/is (contains? result :subject))
|
||||||
|
(t/is (contains? result :body))
|
||||||
|
(t/is (contains? result :to))
|
||||||
|
(t/is (contains? result :reply-to))
|
||||||
|
(t/is (vector? (:body result)))))
|
||||||
|
|
||||||
|
(t/deftest email-sending-and-sendmail-job
|
||||||
|
(let [res @(emails/send! emails/register {:to "example@uxbox.io"})]
|
||||||
|
(t/is (nil? res)))
|
||||||
|
(with-mock mock
|
||||||
|
{:target 'uxbox.jobs.sendmail/impl-sendmail
|
||||||
|
:return (p/resolved nil)}
|
||||||
|
|
||||||
|
(let [res @(uxbox.jobs.sendmail/send-emails {})]
|
||||||
|
(t/is (= 1 res))
|
||||||
|
(t/is (:called? @mock))
|
||||||
|
(t/is (= 1 (:call-count @mock))))
|
||||||
|
|
||||||
|
(let [res @(uxbox.jobs.sendmail/send-emails {})]
|
||||||
|
(t/is (= 0 res))
|
||||||
|
(t/is (:called? @mock))
|
||||||
|
(t/is (= 1 (:call-count @mock))))))
|
||||||
|
|
Loading…
Add table
Reference in a new issue