blob: d834f56d6fabf02209dc8296c9a9f687097a6e29 (
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
|
;; The receiver only verifies URLs, and does not make HTTP requests
;; Verifying Content-Type and if target url is present in source
;; is the processor's concern. Thus this test does not contain any
;; test cases that covers those checks.
;; Receiver does this things:
;; 1. Receive a webmention request from handler
;; 2. Validate request
;; 2.1 Source and target should be valid URLs;
;; 2.2 Source and target should not be identical;
;; 2.3 Domain of target is in allow list.
;; 3. Put validated request into queue.
(ns weepinbell.receiver-test
(:require [clojure.test :refer [deftest testing is]]
[clojure.core.async :refer [chan poll!]]
[weepinbell.webserver.receiver :refer [receive]]))
;; utilities
(defn- wrap-400-msg [typ]
(let [msg (typ {:invalid-url "Invalid URLs. Source and target have to be valid http(s) urls."
:identical "Source and target are identical."
:domain-not-allowed "Domain of target is not allowed. This receiver does not receive webmention on its behalf."})]
{:status 400
:headers {}
:body {:status 400
:message msg}}))
;; end utilities
;; define test cases
(def happy-paths [["https://bob.blog" "https://alice.blog"]
["http://bob.blog" "http://alice.blog"]
["https://bob.blog/post/123" "https://alice.blog/article/abc"]
["http://bob.blog/post/123" "http://alice.blog/article/abc"]])
(def error-cases [;; invalid URLs
["htt://bob.blog" "https://alice.blog/article/abc" (wrap-400-msg :invalid-url)]
["ftp://bob.blog/file/x" "https://alice.blog/article/abc" (wrap-400-msg :invalid-url)]
["bob.blog/post/123" "https://alice.blog/article/abc" (wrap-400-msg :invalid-url)]
["https://bob.blog/post/123" "alice.blog/article/abc" (wrap-400-msg :invalid-url)]
["https://bob.blog/post/123" "ftp://alice.blog/file/x" (wrap-400-msg :invalid-url)]
;; identical URLs
["https://alice.blog/article/abc" "https://alice.blog/article/abc" (wrap-400-msg :identical)]
["https://alice.blog/article/abc/" "https://alice.blog/article/abc" (wrap-400-msg :identical)]
["https://alice.blog/article/abc#anchor" "https://alice.blog/article/abc" (wrap-400-msg :identical)]
["https://alice.blog/article/abc?a=1&b=2" "https://alice.blog/article/abc?b=2&a=1" (wrap-400-msg :identical)]
;; domain not allowed
["https://bob.blog/post/123" "https://chris.blog/archive/xxx" (wrap-400-msg :domain-not-allowed)]
;; nil input
[nil nil (wrap-400-msg :invalid-url)]])
;; end define test cases
;; webmention queue for testing
(def test-wmc (chan))
;; end webmention queue for testing
;; validation tests
(deftest happy-path-test
(testing "Receiver happy paths testing."
(->> happy-paths
(map (fn [testcase]
(let [[s t] testcase]
(is (= 201
(:status (receive {:s s :t t}
test-wmc
{:allowed-target-domains ["alice.blog"]})))))))
doall)))
(deftest error-response-test
(testing "Receiver error handling."
(->> error-cases
(map (fn [testcase]
(let [[s t expects] testcase]
(is (= expects
(receive {:s s :t t}
test-wmc
{:allowed-target-domains ["alice.blog"]}))))))
doall)))
;; end validation tests
;; in-queue test
;; see if receiver is able to put webmention request
;; into queue for processor to pick up
;; and if it falsely put unvalidated request
(deftest in-queue-test
(testing "Receiver putting Webmention in queue."
;; valid webmention
(->> happy-paths
(map (fn [testcase]
(let [[s t] testcase
wm {:s s :t t}
test-queue (chan)]
(receive wm test-queue {:allowed-target-domains ["alice.blog"]})
(is (= wm (poll! test-queue))))))
doall)
;; invalid webmention
(->> error-cases
(map (fn [testcase]
(let [[s t] testcase
wm {:s s :t t}
test-queue (chan)]
(receive wm test-queue {:allowed-target-domains ["alice.blog"]})
(is (not= wm (poll! test-queue))))))
doall)))
;; end in-queue test
|