Browse Source

routing tests added

master
Christian Mueller 12 years ago
parent
commit
a5eaf38130
  1. 2
      src/NoteHub/storage.clj
  2. 104
      test/NoteHub/test/api.clj
  3. 3
      test/NoteHub/test/views/pages.clj

2
src/NoteHub/storage.clj

@ -106,7 +106,7 @@
(defn delete-note (defn delete-note
[noteID] [noteID]
(doseq [kw [password views note]] (doseq [kw [password views note published edited]]
; TODO: delete short url by looking for the title ; TODO: delete short url by looking for the title
(redis/hdel db kw noteID))) (redis/hdel db kw noteID)))

104
test/NoteHub/test/api.clj

@ -11,6 +11,7 @@
(def pid2 "somePlugin2") (def pid2 "somePlugin2")
(def note-title (str (apply print-str (get-date)) " hello-world-this-is-a-test-note")) (def note-title (str (apply print-str (get-date)) " hello-world-this-is-a-test-note"))
(def note-url (str domain (apply str (interpose "/" (get-date))) "/hello-world-this-is-a-test-note")) (def note-url (str domain (apply str (interpose "/" (get-date))) "/hello-world-this-is-a-test-note"))
(defn substring? [a b] (not (= nil (re-matches (re-pattern (str "(?s).*" a ".*")) b))))
(defmacro isnt [arg] `(is (not ~arg))) (defmacro isnt [arg] `(is (not ~arg)))
@ -92,66 +93,61 @@
:pid pid :pid pid
:signature (get-signature pid psk note) :signature (get-signature pid psk note)
:version "1.0"}) :version "1.0"})
body (parse-string (:body response))] body (parse-string (:body response))
noteID (body "noteID")]
(is (has-status response 200)) (is (has-status response 200))
(is (get-in body ["status" "success"])) (is (get-in body ["status" "success"]))
(is (= note ((parse-string (is (= note ((parse-string
(:body (send-request [:get "/api/note"] {:version "1.0" :noteID (body "noteID")}))) "note"))) (:body (send-request [:get "/api/note"] {:version "1.0" :noteID noteID}))) "note")))
(is (do (is (do
(storage/delete-note (body "noteID")) (storage/delete-note noteID)
(not (storage/note-exists? (body "noteID")))))))) (not (storage/note-exists? noteID)))))))
#_
(deftest note-update (deftest note-update
(let [session-key (create-session) (let [response (send-request [:post "/api/note"]
date (get-date) {:note note
title "test-note" :pid pid
[year month day] date] :signature (get-signature pid psk note)
:version "1.0"
:password "qwerty"})
body (parse-string (:body response))
noteID (body "noteID")]
(testing "Note update" (testing "Note update"
(is (has-status (is (has-status response 200))
(send-request (is (get-in body ["status" "success"]))
[:post "/post-note"] (is (storage/note-exists? noteID))
{:session-key session-key (is (substring? "_test_ note"
:draft "test note" ((parse-string
:password "qwerty" (:body (send-request [:get "/api/note"] {:version "1.0" :noteID noteID}))) "note")))
:session-value (str (get-hash (str "test note" session-key)))}) 302)) (let [response (send-request [:put "/api/note"]
(is (note-exists? (build-key date title))) {:noteID noteID
(is (substring? "test note" :note "WRONG pass"
((send-request (url year month day title)) :body))) :pid pid
(is (has-status :signature (get-signature pid psk noteID "WRONG pass" "qwerty1")
(send-request :password "qwerty1"
[:post "/update-note"] :version "1.0"})
{:key (build-key [year month day] title) body (parse-string (:body response))]
:draft "WRONG pass" (is (has-status response 200))
:password "qwerty1" }) 403)) (isnt (get-in body ["status" "success"]))
(is (substring? "test note" (is (= "password invalid" (get-in body ["status" "message"])))
((send-request (url year month day title)) :body))) (isnt (get-in body ["statistics" "edited"]))
(is (has-status (is (substring? "_test_ note"
(send-request ((parse-string
[:post "/update-note"] (:body (send-request [:get "/api/note"] {:version "1.0" :noteID noteID}))) "note"))))
{:key (build-key [year month day] title) (is (get-in (parse-string
:draft "UPDATED CONTENT" (:body (send-request [:put "/api/note"]
:password "qwerty" }) 302)) {:noteID noteID
:note "UPDATED CONTENT"
:pid pid
:signature (get-signature pid psk noteID "UPDATED CONTENT" "qwerty")
:password "qwerty"
:version "1.0"}))) ["status" "success"]))
(isnt (= nil (((parse-string
(:body (send-request [:get "/api/note"] {:version "1.0" :noteID noteID})))
"statistics") "edited")))
(is (substring? "UPDATED CONTENT" (is (substring? "UPDATED CONTENT"
((send-request (url year month day title)) :body))) ((parse-string
(:body (send-request [:get "/api/note"] {:version "1.0" :noteID noteID}))) "note")))
(is (do (is (do
(delete-note (build-key date title)) (storage/delete-note noteID)
(not (note-exists? (build-key date title)))))))) (not (storage/note-exists? noteID)))))))
#_
(deftest requests
(testing "HTTP Status"
(testing "of a wrong access"
(is (has-status (send-request "/wrong-page") 404))
(is (has-status (send-request (url 2012 6 3 "lol" "stat")) 404))
(is (has-status (send-request (url 2012 6 3 "lol" "export")) 404))
(is (has-status (send-request (url 2012 6 3 "lol")) 404))
(is (has-status (send-request (url 2012 6 4 "wrong-title")) 404)))
(testing "of corrupt note-post"
(is (has-status (send-request [:post "/post-note"]) 400)))
(testing "valid accesses"
;(is (has-status (send-request "/new") 200) "accessing /new")
(is (has-status (send-request (url 2012 6 3 "some-title")) 200) "accessing test note")
(is (has-status (send-request (url 2012 6 3 "some-title" "export")) 200) "accessing test note's export")
(is (has-status (send-request (url 2012 6 3 "some-title" "stats")) 200) "accessing test note's stats")
(is (has-status (send-request "/") 200) "accessing landing page"))))

3
test/NoteHub/test/views/pages.clj

@ -6,8 +6,7 @@
[NoteHub.storage] [NoteHub.storage]
[clojure.test])) [clojure.test]))
(defn substring? [a b] (defn substring? [a b] (not (= nil (re-matches (re-pattern (str "(?s).*" a ".*")) b))))
(not (= nil (re-matches (re-pattern (str "(?s).*" a ".*")) b))))
(def date [2012 6 3]) (def date [2012 6 3])
(def test-title "some-title") (def test-title "some-title")
(def test-note "# This is a test note.\nHello _world_. Motörhead, тест.") (def test-note "# This is a test note.\nHello _world_. Motörhead, тест.")

Loading…
Cancel
Save