summaryrefslogtreecommitdiff
path: root/test/weepinbell/feed_test.clj
blob: 341f363ab8b6d0ee16ba670d3a487cb7a93f9525 (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
(ns weepinbell.feed-test
  (:require [clojure.test :refer [testing is deftest]]
            [clojure.xml :as xml]
            [clojure.java.io :as io]
            [weepinbell.webserver.feed :as feed]
            [weepinbell.utils.digest :refer [digest]]))

(def domain "test.example.com")

(def storage-data
  {:normal
   [{:webmention {:source "https://test.example.net/a"
                  :target "https://test.example.com/a"
                  :modified (System/currentTimeMillis)}}
    {:webmention {:source "https://test.example.net/b"
                  :target "https://test.example.com/b"
                  :id "c6b87cf154f6d60c7ae47e2364e2d912"}}]
   :empty []})
(defn make-storage [typ]
  {:get-by-domain (fn [_] (get storage-data typ))})

(def config {:base-url "https://weep.example.com"
             :allowed-target-domains [domain]
             :feed-length 3})

(defn- reduce-to-map [xml-vector]
  (->> xml-vector
       (map (fn [data]
              {(:tag data)
               (first (:content data))}))
       (reduce conj)))

(defn- get-items [response]
  (let [feed (-> response :body .getBytes io/input-stream xml/parse)]
    (->> (:content feed)
         (filter #(= :channel (:tag %)))
         first
         :content
         (filter #(= :item (:tag %)))
         (map :content)
         (map reduce-to-map))))

(deftest rss-generation-test
  (testing "404 JSON response if domain is not allowed"
    (let [response (feed/rss "test.example.org"
                             (make-storage :normal)
                             config)]
      (is (= (:status response) 404))))
  (testing "Empty webmention feed."
    (let [response (feed/rss domain (make-storage :empty) config)]
      (is (= (count (get-items response)) 0))))
  (testing "Normal webmention feed."
    (let [response (feed/rss domain (make-storage :normal) config)
          items (get-items response)]
      (is (= (count items)
             (count (:normal storage-data))))
      (is (= (get-in storage-data [:normal 0 :webmention :source])
             (:link (first items))))
      (is (= (str (:base-url config) "/api/mention/"
                  (digest (str (get-in storage-data [:normal 0 :webmention :source])
                               (get-in storage-data [:normal 0 :webmention :target]))))
             (:guid (first items)))))))