This project is used to demonstrate how to build a simple web service which, when given a list of email addresses, will return the total number of unique instances of each address, after stripping out content that Gmail would normally ignore.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.8 KiB

(ns email-counter.test.handler
[clojure.string :as str]
[clojure.test :refer :all]
[email-counter.middleware.exception :as exception]
[email-counter.handler :refer :all]
[muuntaja.core :as m]
[mount.core :as mount]
[ring.mock.request :refer :all]
[schema.core :as s]))
(fn [f]
(mount/start #'email-counter.config/env
(s/with-fn-validation (f))
(finally (mount/stop)))))
(defn decode-response-body [{{:strs [Content-Type]} :headers :as response}]
(case (first (str/split Content-Type #";"))
(m/decode-response-body response)
(:body response)
(pr-str response)))
(def ^:private email-route "/api/v1/emailcount")
(defn clean-response [response]
(-> response
(assoc :body (decode-response-body response))
(dissoc :headers)))
(defn post-body [url body]
(let [response ((app) (json-body (request :post url) body))]
(clean-response response)))
(defn request-emails [emails]
(post-body email-route {:emails emails}))
(defn print-exceptions [exceptions-encountered]
(doseq [e @exceptions-encountered]
(.printStackTrace e)))
(deftest email-count
(let [exceptions-encountered (atom [])
print-exceptions (partial print-exceptions exceptions-encountered)]
(with-redefs [exception/log-error
(fn [e] (swap! exceptions-encountered conj e))]
(testing "ping"
(is (= {:status 200, :body {:message "pong"}}
(clean-response ((app) (request :get "/api/ping"))))))
(testing "emailcount"
(is (= {:status 200, :body {:total 2}}
(request-emails [""
"Test Email <>"
(testing "invalid email address"
(is (= {:status 400
:body "{:emails [(not (EmailAddress \"busted\"))]}"}
(request-emails ["busted"])))
(is (= {:status 400
(str "{:emails [(not (instance? java.lang.String 42)) "
"(not (EmailAddress \"\")) "
"(not (instance? java.lang.String nil)) "
"(not (instance? java.lang.String false)) "
(request-emails [42 "" nil false ""]))))
(testing "invalid request"
(is (= {:status 400
:body "{:emails missing-required-key, :test disallowed-key}"}
(post-body email-route {:test "stuff"}))))
(testing "no exceptions were actually encountered"
(is (= 0 (count @exceptions-encountered))