Skip to content
Snippets Groups Projects
Commit 07a7bd86 authored by Allen Gilliland's avatar Allen Gilliland
Browse files

unit tests for new email functions.

parent 2fe15607
No related branches found
No related tags found
No related merge requests found
(ns metabase.api.email-test
(:require [expectations :refer :all]
[metabase.email-test :refer [with-fake-inbox inbox]]
[metabase.models.setting :as setting]
[metabase.test.data.users :refer :all]))
(defn- email-settings
[]
{:email-smtp-host (setting/get :email-smtp-host)
:email-smtp-port (setting/get :email-smtp-port)
:email-smtp-security (setting/get :email-smtp-security)
:email-smtp-username (setting/get :email-smtp-username)
:email-smtp-password (setting/get :email-smtp-password)
:email-from-address (setting/get :email-from-address)})
;; /api/email/test - sends a test email to the given user
(expect
{:email-smtp-host "foobar"
:email-smtp-port "789"
:email-smtp-security "tls"
:email-smtp-username "munchkin"
:email-smtp-password "gobble gobble"
:email-from-address "eating@hungry.com"}
(let [orig-settings (email-settings)
_ ((user->client :crowberto) :put 200 "email" {:email-smtp-host "foobar"
:email-smtp-port "789"
:email-smtp-security "tls"
:email-smtp-username "munchkin"
:email-smtp-password "gobble gobble"
:email-from-address "eating@hungry.com"})
new-settings (email-settings)
_ (setting/set-all orig-settings)]
new-settings))
;; with-fake-inbox doesn't work in this case because of the api call
;; /api/email/test - sends a test email to the given user
;(expect
; [{:from "notifications@metabase.com",
; :to [(-> (fetch-user :crowberto) :email)],
; :subject "Metabase Test Email",
; :body [{:type "text/plain; charset=utf-8"
; :content "Your Metabase emails are working — hooray!"}]}]
; (with-fake-inbox
; ((user->client :crowberto) :post 200 "email/test")
; (@inbox (-> (fetch-user :crowberto) :email))))
(ns metabase.email.messages-test
(:require [expectations :refer :all]
[metabase.email :as email]
[metabase.email-test :refer [with-fake-inbox inbox]]
[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-hostname# (email/email-smtp-host)
orig-port# (email/email-smtp-port)]
(email/email-smtp-host "fake_smtp_host")
(email/email-smtp-port "587")
(try ~@body
(finally (email/email-smtp-host orig-hostname#)
(email/email-smtp-port orig-port#))))))
;; new user email
;; NOTE: we are not validating the content of the email body namely because it's got randomized elements and thus
;; it would be extremely hard to have a predictable test that we can rely on
......
(ns metabase.email-test
(:require [expectations :refer :all]
[metabase.email :as email]))
(def 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-hostname# (email/email-smtp-host)
orig-port# (email/email-smtp-port)]
(email/email-smtp-host "fake_smtp_host")
(email/email-smtp-port "587")
(try ~@body
(finally (email/email-smtp-host orig-hostname#)
(email/email-smtp-port orig-port#))))))
;; simple test of email sending capabilities
(expect
[{:from "notifications@metabase.com",
:to ["test@test.com"],
:subject "101 Reasons to use Metabase",
:body [{:type "text/html; charset=utf-8"
:content "101. Metabase will make you a better person"}]}]
(with-fake-inbox
(email/send-message
:subject "101 Reasons to use Metabase"
:recipients ["test@test.com"]
:message-type :html
:message "101. Metabase will make you a better person")
(@inbox "test@test.com")))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment