Skip to content

Commit

Permalink
Consider all domain suffixes in Clojure
Browse files Browse the repository at this point in the history
  • Loading branch information
owst committed Mar 18, 2016
1 parent db84501 commit ee7c304
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
13 changes: 6 additions & 7 deletions platform/clojure/mailchecker.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@
[string]
(str/split string #"\."))

(defn top-domain-part
"Returns the top domain for email"
(defn all-domain-suffixes
"Returns all suffixes of the email domain, longest first"
[email]
(dot-join
(take-last 2
(dot-split (domain-part email)))))
(let [domain-parts (dot-split (domain-part email))]
(map #(dot-join (drop % domain-parts)) (range 0 (count domain-parts)))))

(defn in-blacklist?
"Returns true if email domain is not in the blacklist"
"Returns true if any suffix of the email domain is in the blacklist"
[email]
(contains? blacklist (top-domain-part email)))
(some (partial contains? blacklist) (all-domain-suffixes email)))

(defn valid?
"Returns true if the email is valid"
Expand Down
13 changes: 6 additions & 7 deletions platform/clojure/mailchecker.tmpl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@
[string]
(str/split string #"\."))

(defn top-domain-part
"Returns the top domain for email"
(defn all-domain-suffixes
"Returns all suffixes of the email domain, longest first"
[email]
(dot-join
(take-last 2
(dot-split (domain-part email)))))
(let [domain-parts (dot-split (domain-part email))]
(map #(dot-join (drop % domain-parts)) (range 0 (count domain-parts)))))

(defn in-blacklist?
"Returns true if email domain is not in the blacklist"
"Returns true if any suffix of the email domain is in the blacklist"
[email]
(contains? blacklist (top-domain-part email)))
(some (partial contains? blacklist) (all-domain-suffixes email)))

(defn valid?
"Returns true if the email is valid"
Expand Down
41 changes: 28 additions & 13 deletions test/platform.clojure.test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,50 @@
(ns clojure.test.example
(:use clojure.test))

(defn expect-valid-result [expected-valid email]
(is (= expected-valid (mailchecker/valid? email))))

(def expect-invalid (partial expect-valid-result false))
(def expect-valid (partial expect-valid-result true))

; Valid
(deftest true-for-valid-1
(is (= true (mailchecker/valid? "[email protected]"))))
(expect-valid "[email protected]"))
(deftest true-for-valid-2
(is (= true (mailchecker/valid? "[email protected]"))))
(expect-valid "[email protected]"))
(deftest true-for-valid-3
(is (= true (mailchecker/valid? "[email protected]"))))
(expect-valid "[email protected]"))
(deftest true-for-valid-4
(is (= true (mailchecker/valid? "[email protected]"))))
(expect-valid "[email protected]"))
(deftest true-for-valid-5
(is (= true (mailchecker/valid? "[email protected]"))))
(expect-valid "[email protected]"))
(deftest true-for-valid-6
(is (= true (mailchecker/valid? "[email protected]"))))
(expect-valid "[email protected]"))

; Invalid
(deftest false-for-invalid-1
(is(= false (mailchecker/valid? "plopplop.com"))))
(expect-invalid "plopplop.com"))
(deftest false-for-invalid-2
(is(= false (mailchecker/valid? "my+ok@ok=plop.com"))))
(expect-invalid "my+ok@ok=plop.com"))
(deftest false-for-invalid-3
(is(= false (mailchecker/valid? "my,[email protected]"))))
(expect-invalid "my,[email protected]"))

(deftest false-for-spam-1
(is(= false (mailchecker/valid? "[email protected]"))))
(expect-invalid "[email protected]"))
(deftest false-for-spam-2
(is(= false (mailchecker/valid? "[email protected]"))))
(expect-invalid "[email protected]"))
(deftest false-for-spam-3
(is(= false (mailchecker/valid? "[email protected]"))))
(expect-invalid "[email protected]"))
(deftest false-for-spam-4
(is(= false (mailchecker/valid? "[email protected]"))))
(expect-invalid "[email protected]"))

(deftest false-for-blacklist-entries
(every? (fn [domain]
(do (expect-invalid (str "test@" domain))
(expect-invalid (str "test@subdomain." domain))
;; Blacklisted domains should be valid as subdomains of a
;; valid domain.
(expect-valid (str "test@" domain ".gmail.com"))))
mailchecker/blacklist))

(run-all-tests)

0 comments on commit ee7c304

Please sign in to comment.