Skip to content
Snippets Groups Projects
Commit 1b430d6d authored by Cam Saul's avatar Cam Saul
Browse files

update tests

parent 4b38e84c
Branches
Tags
No related merge requests found
......@@ -14,6 +14,11 @@
;; ## PUBLIC INTERFACE
(def ^:dynamic *send-email-fn*
"Internal function used to send messages. Should take 2 args - a map of SMTP credentials, and a map of email details.
Provided so you can swap this out with an \"inbox\" for test purposes."
postal/send-message)
;; TODO - wouldn't this be *nicer* if all args were kwargs?
(defn send-message
"Send an email to one or more RECIPIENTS.
......@@ -39,17 +44,17 @@
(when-not (email-smtp-password)
(throw (Exception. "SMTP password is not set.")))
;; Now send the email
(let [{error :error error-message :message} (postal/send-message {:host (email-smtp-host)
:user (email-smtp-username)
:pass (email-smtp-password)
:port (Integer/parseInt (email-smtp-port))}
{:from (email-from-address)
:to recipients
:subject subject
:body (condp = message-type
:text message
:html [{:type "text/html; charset=utf-8"
:content message}])})]
(let [{error :error error-message :message} (*send-email-fn* {:host (email-smtp-host)
:user (email-smtp-username)
:pass (email-smtp-password)
:port (Integer/parseInt (email-smtp-port))}
{:from (email-from-address)
:to recipients
:subject subject
:body (condp = message-type
:text message
:html [{:type "text/html; charset=utf-8"
:content message}])})]
(when-not (= error :SUCCESS)
(throw (Exception. (format "Emails failed to send: error: %s; message: %s" error error-message))))
message)
......
(ns metabase.email.messages-test
(:require [expectations :refer :all]
[metabase.email :as email]
[metabase.email.messages :refer :all]))
(def ^:private inbox
"Map of email addresses -> sequence of messages they've recieved."
(atom {}))
(defn- reset-inbox!
"Clear all messages from `inbox`."
[]
(reset! inbox {}))
(defn- fake-inbox-email-fn
"A function that can be used in place of `*send-email-fn*`.
Put all messages into `inbox` instead of actually sending them."
[_ email]
(doseq [recipient (:to email)]
(swap! inbox assoc recipient (-> (get @inbox recipient [])
(conj email)))))
(defmacro with-fake-inbox
"Clear `inbox`, bind `*send-email-fn*` to `fake-inbox-email-fn`, set temporary settings for `email-smtp-username`
and `email-smtp-password`, and execute BODY."
[& body]
`(binding [email/*send-email-fn* fake-inbox-email-fn]
(reset-inbox!)
;; Push some fake settings for SMTP username + password, and restore originals when done
(let [orig-username# (email/email-smtp-username)
orig-password# (email/email-smtp-password)]
(email/email-smtp-username "fake_smtp_username")
(email/email-smtp-password "ABCD1234!!")
(try ~@body
(finally (email/email-smtp-username orig-username#)
(email/email-smtp-password orig-password#))))))
;; new user email
(expect
(str "<html><body><p>Welcome to Metabase test!</p>"
"<p>Your account is setup and ready to go, you just need to set a password so you can login. "
"Follow the link below to reset your account password.</p>"
"<p><a href=\"http://localhost/some/url\">http://localhost/some/url</a></p></body></html>")
(send-new-user-email "test" "test@test.com" "http://localhost/some/url"))
[{:from "notifications@metabase.com",
:to ["test@test.com"],
:subject "Your new Metabase account is all set up",
:body [{:type "text/html; charset=utf-8",
:content (str "<html><body><p>Welcome to Metabase test!</p>"
"<p>Your account is setup and ready to go, you just need to set a password so you can login. "
"Follow the link below to reset your account password.</p>"
"<p><a href=\"http://localhost/some/url\">http://localhost/some/url</a></p></body></html>")}]}]
(with-fake-inbox
(send-new-user-email "test" "test@test.com" "http://localhost/some/url")
(@inbox "test@test.com")))
;; password reset email
(expect
(str "<html><body><p>You're receiving this e-mail because you or someone else has requested a password for your user account at test.domain.com. "
"It can be safely ignored if you did not request a password reset. Click the link below to reset your password.</p>"
"<p><a href=\"http://localhost/some/url\">http://localhost/some/url</a></p></body></html>")
(send-password-reset-email "test@test.com" "test.domain.com" "http://localhost/some/url"))
[{:from "notifications@metabase.com",
:to ["test@test.com"],
:subject "[Metabase] Password Reset Request",
:body [{:type "text/html; charset=utf-8",
:content (str "<html><body><p>You're receiving this e-mail because you or someone else has requested a password for your user account at test.domain.com. "
"It can be safely ignored if you did not request a password reset. Click the link below to reset your password.</p>"
"<p><a href=\"http://localhost/some/url\">http://localhost/some/url</a></p></body></html>")}]}]
(with-fake-inbox
(send-password-reset-email "test@test.com" "test.domain.com" "http://localhost/some/url")
(@inbox "test@test.com")))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment