Skip to content
Snippets Groups Projects
Unverified Commit b51d1788 authored by dpsutton's avatar dpsutton Committed by GitHub
Browse files

Fix flaky email tests (#19023)

Docstring for `regex-email-bodies` states:

> "Return messages in the fake inbox whose body matches the
regex(es). The body will be replaced by a map with the stringified regex
as it's key and a boolean indicated that the regex returned results."

But it does ont return messages in the fake inbox whose body matches any
of the regex. It returns them all regardless, with information if they
match or not. Simply correct this behavior to match the docstring

```clojure
{"rasta@metabase.com"
 ({:from "notifications@metabase.com",
   :to #{"rasta@metabase.com"},
   :subject "We've Noticed a New Metabase Login, Rasta",
   :body {"https://metabase.com/testmb" false, "has any results" false, "My question" false}}
  {:from "notifications@metabase.com",
   :to #{"rasta@metabase.com"},
   :subject "We've Noticed a New Metabase Login, Rasta",
   :body {"https://metabase.com/testmb" false, "has any results" false, "My question" false}}
  {:from "notifications@metabase.com",
   :to #{"rasta@metabase.com"},
   :subject "You set up an alert",
   :body {"https://metabase.com/testmb" true, "has any results" true, "My question" true}})}
```
parent 0480c2cc
No related branches found
No related tags found
No related merge requests found
......@@ -93,17 +93,54 @@
(zipmap (map str regex-seq)
(map #(boolean (re-find % content)) regex-seq)))))
(defn- regex-email-bodies*
[regexes emails]
(let [email-body->regex-boolean (create-email-body->regex-fn regexes)]
(->> emails
(m/map-vals (fn [emails-for-recipient]
(for [{:keys [body] :as email} emails-for-recipient
:let [matches (-> body first email-body->regex-boolean)]
:when (some true? (vals matches))]
(-> email
(update :to set)
(assoc :body matches)))))
(m/filter-vals seq))))
(defn regex-email-bodies
"Return messages in the fake inbox whose body matches the regex(es). The body will be replaced by a map with the
stringified regex as it's key and a boolean indicated that the regex returned results."
[& regexes]
(let [email-body->regex-boolean (create-email-body->regex-fn regexes)]
(m/map-vals (fn [emails-for-recipient]
(for [email emails-for-recipient]
(-> email
(update :to set)
(update :body (comp email-body->regex-boolean first)))))
@inbox)))
(regex-email-bodies* regexes @inbox))
(deftest regex-email-bodies-test
(letfn [(email [body] {:to #{"mail"}
:body [{:content body}]})
(clean [emails] (m/map-vals #(map :body %) emails))]
(testing "marks emails with regex match"
(let [emails {"bob@metabase.com" [(email "foo bar baz")
(email "other keyword")]
"sue@metabase.com" [(email "foo bar baz")]}]
(is (= {"bob@metabase.com" [{"foo" true "keyword" false} {"foo" false "keyword" true}]
"sue@metabase.com" [{"foo" true "keyword" false}]}
(clean (regex-email-bodies* [#"foo" #"keyword"] emails))))))
(testing "Returns only emails with at least one match"
;; drops the email that isn't matched by any regex
(testing "Drops the email that doesn't match"
(is (= {"bob@metabase.com" [{"foo" true "keyword" false}]}
(clean (regex-email-bodies* [#"foo" #"keyword"]
{"bob@metabase.com" [(email "foo")
(email "no-match")]})))))
(testing "Drops the entry for the other person with no matching emails"
(is (= {"bob@metabase.com" [{"foo" true "keyword" false}]}
(clean (regex-email-bodies* [#"foo" #"keyword"]
{"bob@metabase.com" [(email "foo")
(email "no-match")]
"sue@metabase.com" [(email "no-match")]}))))
(is (= {}
(clean (regex-email-bodies* [#"foo" #"keyword"]
{"bob@metabase.com" [(email "no-match")
(email "no-match")]
"sue@metabase.com" [(email "no-match")]}))))))))
(defn- mime-type [mime-type-str]
(-> mime-type-str
......
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