(ns riemann.mailgun-test
  (:require [riemann.mailgun :as mailgun]
            [clj-http.client :as client]
            [clojure.test :refer [deftest testing is are]]))

(defn- mock-post [result-atom email event]
  (with-redefs [client/post (fn [url opts]
                              (reset! result-atom
                                      (merge (:form-params opts)
                                             {:url url
                                              :basic-auth (:basic-auth opts)})))]
    (email event)))

(deftest mailgun-test
  (let [result-atom (atom {})
        sandbox "mail.relay"
        service-key "testkey"
        recipient "somedude@somewhere.com"
        default-body-result "At Wed Dec 31 16:02:03 PST 1969\ntest host testservice  (17)\nTags: []\nCustom Attributes: {}\n\n"
        default-subject-result "test host testservice"
        event {:service "testservice"
               :host "test host"
               :time 123
               :metric 17}
        mailer (mailgun/mailgun {:sandbox sandbox
                                 :service-key service-key})
        email (mailer recipient)]

    (testing "ensure the data posted to mailgun matches expectations"
      (mock-post result-atom email event)
      (are [key result] (= result (key @result-atom))
           :url (str "https://api.mailgun.net/v2/" sandbox "/messages")
           :basic-auth ["api" service-key]
           :from (str "Riemann <riemann@" sandbox ">")
           :to (list recipient)
           :subject default-subject-result
           :text default-body-result))

    (testing "ensure message overrides are used"
      (let [body-formatter-result "this is the body"
            body-formatter (fn [_] body-formatter-result)
            subject-formatter-result "this is the subject"
            subject-formatter (fn [_] subject-formatter-result)
            from-override "my-override@xanadu"
            mailer (mailgun/mailgun {:sandbox sandbox
                                     :service-key service-key}
                                    {:subject subject-formatter
                                     :body body-formatter
                                     :from from-override})
            email (mailer recipient)]
        (mock-post result-atom email event)
        (are [rkey result] (= result (rkey @result-atom))
           :subject subject-formatter-result
           :text body-formatter-result
           :from from-override)))

    (testing "ensure mailgun options are split out when given only one map"
      (let [from-override "my-override@xanadu"
            mailer (mailgun/mailgun {:sandbox sandbox
                                     :service-key service-key
                                     :from from-override})
            email (mailer recipient)]
        (mock-post result-atom email event)
        (is (= (:from @result-atom)
               from-override))))))

Add unit tests for mailgun output
Add unit tests for mailgun output

moonranger/riemann,shokunin/riemann,fullcontact/riemann,joerayme/riemann,rekhajoshm/riemann,alq666/riemann,Anvil/riemann,counsyl/riemann,abailly/riemann,aphyr/riemann,cswaroop/riemann,topecz/riemann,AkihiroSuda/riemann,rhysr/riemann,pharaujo/riemann,bg451/riemann,riemann/riemann,rekhajoshm/riemann,eric/riemann,Anvil/riemann,bfritz/riemann,alq666/riemann,nberger/riemann,fullcontact/riemann,aphyr/riemann,robashton/riemann,forter/riemann,pradeepchhetri/riemann,bwilber/riemann,timbuchwaldt/riemann,algernon/riemann,fullcontact/riemann,jeanpralo/riemann,rhysr/riemann,rhysr/riemann,jeanpralo/riemann,eric/riemann,cswaroop/riemann,micrub/riemann,VideoAmp/riemann-1,forter/riemann,shokunin/riemann,algernon/riemann,mbuczko/riemann,patrox/riemann,riemann/riemann,LubyRuffy/riemann,bmhatfield/riemann,bwilber/riemann,irudyak/riemann,vixns/riemann,lispmeister/riemann,zamaterian/riemann,patrox/riemann,LubyRuffy/riemann,cswaroop/riemann,micrub/riemann,DasAllFolks/riemann,zamaterian/riemann,VideoAmp/riemann-1,pyr/riemann,pyr/riemann,yeller/riemann,moonranger/riemann,jamtur01/riemann,patrox/riemann,lispmeister/riemann,bfritz/riemann,micrub/riemann,vincentbernat/riemann,bowlofstew/riemann,abailly/riemann,mbuczko/riemann,joerayme/riemann,DasAllFolks/riemann,forter/riemann,vixns/riemann,topecz/riemann,mirwan/riemann,jamtur01/riemann,algernon/riemann,mirwan/riemann,pharaujo/riemann,timbuchwaldt/riemann,bg451/riemann,pradeepchhetri/riemann,mirwan/riemann,irudyak/riemann,shokunin/riemann,vincentbernat/riemann,yeller/riemann,bmhatfield/riemann,rekhajoshm/riemann,robashton/riemann,bowlofstew/riemann,topecz/riemann,lispmeister/riemann,yeller/riemann,AkihiroSuda/riemann,nberger/riemann,counsyl/riemann