summaryrefslogtreecommitdiff
path: root/test/weepinbell/verifier_test.clj
blob: b30fd95ec8d79a91585022ea3ae2e25323dfdb53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(ns weepinbell.verifier-test
  (:require [clojure.test :refer [deftest testing is]]
            [weepinbell.processor.verifier :as v]
            [weepinbell.http.client]
            [weepinbell.test-utility :refer [with-webmention]]))

(defn- test-verify [source target]
  (-> {:wm {:s source :t target}
       :ok? true
       :id "123"}
      v/verify
      :verified?))

(deftest verification-happy-paths-test
  (testing "Test if processor can correctly verifies a Webmention."
    (with-webmention :new ["/source" {:status 200
                                      :headers {"Content-Type" "text/plain; charset=utf-8"}
                                      :body (str "Oh I love this post:"
                                                 " http://localhost:38888/target\n"
                                                 "       this is very clever"
                                                 " and I find it inspiring")}
                           "/target" {:status 200
                                      :headers {"Content-Type" "text/plain"}}]
      (is (= true (test-verify "http://localhost:38888/source"
                               "http://localhost:38888/target")))))
  (testing "Test if processor can correctly verifies a Webmention."
    (with-webmention :new ["/source" {:status 206
                                      :headers {"Content-Type" "text/html"}
                                      :body (str "Oh I love this post:"
                                                 " http://localhost:38888/target\n"
                                                 "       this is very clev")}
                           "/target" {:status 200
                                      :headers {"Content-Type" "text/plain"}}]
      (is (= true (test-verify "http://localhost:38888/source"
                               "http://localhost:38888/target"))))))

(deftest verification-exceptional-cases-test
  (with-redefs [weepinbell.http.client/default-options {:timeout 100}
                weepinbell.processor.verifier/retry-times 0]
    (testing "source is not found"
      (with-webmention :new ["/source" {:status 404
                                        :headers {"Content-Type" "text/plain"}
                                        :body "Not Found."}
                             "/target" {:status 200
                                        :headers {"Content-Type" "text/plain"}}]
        (is (not (test-verify "http://localhost:38888/source"
                              "http://localhost:38888/target")))))
    (testing "source returns error status 502"
      (with-webmention :new ["/source" {:status 502
                                        :headers {"Content-Type" "text/plain"}
                                        :body "Bad Gateway"}
                             "/target" {:status 200
                                        :headers {"Content-Type" "text/plain"}}]
        (is (not (test-verify "http://localhost:38888/source"
                              "http://localhost:38888/target")))))
    (testing "source body does not contain any URL"
      (with-webmention :new ["/source" {:status 200
                                        :headers {"Content-Type" "text/plain"}
                                        :body "I didn't mention anyone"}
                             "/target" {:status 200
                                        :headers {"Content-Type" "text/plain"}}]
        (is (not (test-verify "http://localhost:38888/source"
                              "http://localhost:38888/target")))))
    (testing "source body does not contain target URL"
      (with-webmention :new ["/source" {:status 200
                                        :headers {"Content-Type" "text/plain"}
                                        :body (str "I mentioned christ but not alice\n"
                                                   "https://chris.blog/archive/xxx")}
                             "/target" {:status 200
                                        :headers {"Content-Type" "text/plain"}}]
        (is (not (test-verify "http://localhost:38888/source"
                              "http://localhost:38888/target")))))
    (testing "source response has no Content-Type header"
      (with-webmention :new ["/source" {:status 200
                                        :body (str "I mentioned alice's other post\n"
                                                   "https://alice.blog/article/efg")}
                             "/target" {:status 200
                                        :headers {"Content-Type" "text/plain"}}]
        (is (not (test-verify "http://localhost:38888/source"
                              "http://localhost:38888/target")))))
    (testing "target response has no Content-Type header"
      (with-webmention :new ["/source" {:status 200
                                        :headers {"Content-Type" "text/plain"}
                                        :body (str "I mentioned alice's other post\n"
                                                   "https://alice.blog/article/efg")}
                             "/target" {:status 200}]
        (is (not (test-verify "http://localhost:38888/source"
                              "http://localhost:38888/target")))))
    (testing "unsupported content type"
      (with-webmention :new ["/source" {:status 200
                                        :headers {"Content-Type" "application/json"}
                                        :body "{\"message\": \"I mentioned alice's other post\n https://alice.blog/article/efg\"}"}
                             "/target" {:status 200
                                        :headers {"Content-Type" "text/plain"}}]
        (is (not (test-verify "http://localhost:38888/source"
                              "http://localhost:38888/target")))))
    (testing "responses have no status"
      (with-webmention :new ["/source" {:status nil
                                        :headers {"Content-Type" "text/plain"}
                                        :body (str "I mentioned alice's other post\n"
                                                   "https://alice.blog/article/efg")}
                             "/target" {:headers {"Content-Type" "text/plain"}}]
        (is (not (test-verify "http://localhost:38888/source"
                              "http://localhost:38888/target")))))
    (testing "HTTP not complete, 100 status"
      (with-webmention :new ["/source" {:status 100
                                        :headers {"Content-Type" "text/plain"}
                                        :body "oops"}
                             "/target" {:status 200
                                        :headers {"Content-Type" "text/plain"}}]
        (is (not (test-verify "http://localhost:38888/source"
                              "http://localhost:38888/target")))))))