clojure notehub removed; let's start from scratch

This commit is contained in:
Christian Müller
2015-08-15 20:40:21 +02:00
parent 4a96805381
commit 6df0d7990d
14 changed files with 0 additions and 1299 deletions
-214
View File
@@ -1,214 +0,0 @@
(ns notehub.test.api
(:require
[cheshire.core :refer :all]
[notehub.storage :as storage])
(:use [notehub.api]
[notehub.handler]
[clojure.test]))
(def note "hello world!\nThis is a _test_ note!")
(def pid "somePlugin")
(def pid2 "somePlugin2")
(def note-title (let [[y m d] (get-date)]
(apply str (interpose "/" [y m d "hello-world"]))))
(def note-url (str (apply str "/" (interpose "/" (get-date))) "/hello-world"))
(defn substring? [a b] (not (= nil (re-matches (re-pattern (str "(?s).*" a ".*")) b))))
(defmacro isnt [arg] `(is (not ~arg)))
(defn send-request
([resource] (send-request resource {}))
([resource params]
(let [[method url] (if (vector? resource) resource [:get resource])]
(app-routes {:request-method method :uri url :params params}))))
(defn has-status [input status]
(= status (:status input)))
(defn register-publisher-fixture [f]
(def psk (storage/register-publisher pid))
(f)
(storage/revoke-publisher pid)
(storage/revoke-publisher pid2)
(storage/delete-note note-title))
(use-fixtures :each register-publisher-fixture)
(deftest api
(testing "API"
(testing "publisher registration"
(is (storage/valid-publisher? pid))
(isnt (storage/valid-publisher? pid2))
(let [psk2 (storage/register-publisher pid2)]
(is (= psk2 (storage/get-psk pid2)))
(is (storage/valid-publisher? pid2))
(is (storage/revoke-publisher pid2))
(isnt (storage/valid-publisher? "any_PID"))
(isnt (storage/valid-publisher? pid2))))
(testing "note publishing & retrieval"
(isnt (:success (:status (get-note {:noteID "some note id"}))))
(is (= "note is empty" (:message (:status (post-note {:note "" :pid pid :signature (storage/sign pid psk "")})))))
(let [post-response (post-note {:note note :pid pid :signature (storage/sign pid psk note)})
get-response (get-note post-response)]
(is (:success (:status post-response)))
(is (:success (:status get-response)))
(is (= note (:note get-response)))
(is (= (:longURL post-response) (:longURL get-response) note-url))
(is (= (:shortURL post-response) (:shortURL get-response)))
(is (storage/note-exists? (:noteID post-response)))
(let [su (last (clojure.string/split (:shortURL get-response) #"/"))]
(is (= su (storage/create-short-url (:noteID post-response) (storage/resolve-url su)))))
(let [resp (send-request (:shortURL get-response))
resp (send-request ((:headers resp) "Location"))]
(is (substring? "hello world"(:body resp))))
(is (= (:publisher get-response) pid))
(is (= (:title get-response) (derive-title note)))
(is (= "1" (get-in get-response [:statistics :views])))
(isnt (get-in get-response [:statistics :edited]))
(is (= "noteID 'randomString' unknown"
(get-in
(parse-string
(:body (send-request "/api/note" {:version "1.4" :noteID "randomString"})))
["status" "message"])))
(is (= "3" (get-in (get-note post-response) [:statistics :views])))))
(testing "creation with wrong signature"
(let [response (post-note {:note note :pid pid :signature (storage/sign pid2 psk note)})]
(isnt (:success (:status response)))
(is (= "signature invalid" (:message (:status response)))))
(let [response (post-note {:note note :pid pid :signature (storage/sign pid2 psk "any note")})]
(isnt (:success (:status response)))
(is (= "signature invalid" (:message (:status response)))))
(isnt (:success (:status (post-note {:note note :pid pid :signature (storage/sign pid "random_psk" note)}))))
(is (:success (:status (post-note {:note note :pid pid :signature (storage/sign pid psk note)}))))
(let [randomPID "randomPID"
psk2 (storage/register-publisher randomPID)
_ (storage/revoke-publisher randomPID)
response (post-note {:note note :pid randomPID :signature (storage/sign randomPID psk2 note)})]
(isnt (:success (:status response)))
(is (= (:message (:status response)) "pid invalid"))))
(testing "note update"
(let [post-response (post-note {:note note :pid pid :signature (storage/sign pid psk note) :password "passwd"})
note-id (:noteID post-response)
new-note "a new note!"]
(is (:success (:status post-response)))
(is (:success (:status (get-note {:noteID note-id}))))
(is (= note (:note (get-note {:noteID note-id}))))
(let [update-response (update-note {:noteID note-id :note new-note :pid pid
:signature (storage/sign pid psk new-note) :password "passwd"})]
(isnt (:success (:status update-response)))
(is (= "signature invalid" (:message (:status update-response)))))
(is (= note (:note (get-note {:noteID note-id}))))
(let [update-response (update-note {:noteID note-id :note new-note :pid pid
:signature (storage/sign pid psk note-id new-note "passwd")
:password "passwd"})]
(is (= { :success true } (:status update-response)))
(isnt (= nil (get-in (get-note {:noteID note-id}) [:statistics :edited])))
(is (= new-note (:note (get-note {:noteID note-id})))))
(let [update-response (update-note {:noteID note-id :note "aaa" :pid pid
:signature (storage/sign pid psk note-id "aaa" "pass")
:password "pass"})]
(isnt (:success (:status update-response)))
(is (= "password invalid" (:message (:status update-response)))))
(is (= new-note (:note (get-note {:noteID note-id}))))
(is (= new-note (:note (get-note {:noteID note-id}))))))))
(deftest api-note-creation
(testing "Note creation"
(let [response (send-request [:post "/api/note"]
{:note note
:pid pid
:signature (storage/sign pid psk note)
:version "1.4"})
body (parse-string (:body response))
noteID (body "noteID")]
(is (has-status response 200))
(is (get-in body ["status" "success"]))
(is (= note ((parse-string
(:body (send-request [:get "/api/note"] {:version "1.4" :noteID noteID}))) "note")))
(is (= "Deprecated API version" (get-in (parse-string
(:body (send-request [:get "/api/note"] {:version "1.3" :noteID noteID}))) ["status" "message"])))
(is (= "API version expected" (get-in (parse-string
(:body (send-request [:get "/api/note"] {:noteID noteID}))) ["status" "message"])))
(is (= note ((parse-string
(:body (send-request [:get "/api/note"] {:version "1.4" :noteID noteID}))) "note")))
(isnt (= note ((parse-string
(:body (send-request [:get "/api/note"] {:version "1.3" :noteID noteID}))) "note")))
(is (do
(storage/delete-note noteID)
(not (storage/note-exists? noteID)))))))
(deftest api-note-creation-with-params
(testing "Note creation with params"
(let [response (send-request [:post "/api/note"]
{:note note
:pid pid
:signature (storage/sign pid psk note)
:version "1.4"
:theme "dark"
:text-size 1.1
:text-font "Helvetica"})
body (parse-string (:body response))
noteID (body "noteID")]
(let [url ((:headers
(send-request
(str "/"
(last (clojure.string/split
((parse-string (:body response)) "shortURL") #"/"))))) "Location")]
(= url ((parse-string (:body response)) "longURL"))
(is (substring? "theme=dark" url))
(is (substring? "text-size=1.1" url))
(is (substring? "text-font=Helvetica" url)))
(is (do
(storage/delete-note noteID)
(not (storage/note-exists? noteID)))))))
(deftest api-note-update
(let [response (send-request [:post "/api/note"]
{:note note
:pid pid
:signature (storage/sign pid psk note)
:version "1.4"
:password "qwerty"})
body (parse-string (:body response))
noteID (body "noteID")]
(testing "Note update"
(is (has-status response 200))
(is (get-in body ["status" "success"]))
(is (storage/note-exists? noteID))
(is (substring? "_test_ note"
((parse-string
(:body (send-request [:get "/api/note"] {:version "1.4" :noteID noteID}))) "note")))
(let [response (send-request [:put "/api/note"]
{:noteID noteID
:note "WRONG pass"
:pid pid
:signature (storage/sign pid psk noteID "WRONG pass" "qwerty1")
:password "qwerty1"
:version "1.4"})
body (parse-string (:body response))]
(is (has-status response 200))
(isnt (get-in body ["status" "success"]))
(is (= "password invalid" (get-in body ["status" "message"])))
(isnt (get-in body ["statistics" "edited"]))
(is (substring? "_test_ note"
((parse-string
(:body (send-request [:get "/api/note"] {:version "1.4" :noteID noteID}))) "note"))))
(is (get-in (parse-string
(:body (send-request [:put "/api/note"]
{:noteID noteID
:note "UPDATED CONTENT"
:pid pid
:signature (storage/sign pid psk noteID "UPDATED CONTENT" "qwerty")
:password "qwerty"
:version "1.4"}))) ["status" "success"]))
(isnt (= nil (((parse-string
(:body (send-request [:get "/api/note"] {:version "1.4" :noteID noteID})))
"statistics") "edited")))
(let [resp (send-request [:get "/api/note"] {:version "1.4" :noteID noteID})]
(is (substring? "UPDATED CONTENT"
((parse-string
(:body resp)) "note"))))
(is (do
(storage/delete-note noteID)
(not (storage/note-exists? noteID)))))))
-129
View File
@@ -1,129 +0,0 @@
(ns notehub.test.handler
(:use clojure.test
[notehub.api :only [get-date url]]
notehub.storage
ring.mock.request
notehub.test.api
notehub.handler))
(defn build-key [[y m d] t] (notehub.api/build-key y m d t))
(def date [2012 6 3])
(def test-title "some-title")
(def test-note "# This is a test note.\nHello _world_. Motörhead, тест.")
(def session-key (str (quot (System/currentTimeMillis) 100000000) "somemd5hash"))
(defn create-testnote-fixture [f]
(add-note (build-key date test-title) test-note "testPID")
(f)
(delete-note (build-key date test-title)))
(use-fixtures :each create-testnote-fixture)
(is (= (url 2010 05 06 "test-title" "export") "/2010/5/6/test-title/export"))
(deftest testing-fixture
(testing "Was a not created?"
(is (= (get-note (build-key date test-title)) test-note))
(is (note-exists? (build-key date test-title)))))
(deftest export-test
(testing "Markdown export"
(is (= (:body (send-request (url 2012 6 3 "some-title" "export"))) test-note))))
(deftest note-creation
(let [date (get-date)
title "this-is-a-test-note"
[year month day] date]
(testing "Note creation"
(let [resp (send-request
[:post "/post-note"]
{:session session-key
:note test-note
:signature (sign session-key test-note)})]
(is (has-status resp 302))
(is (note-exists? (build-key date title)))
(is (substring? "Hello <em>world</em>"
((send-request (url year month day title)) :body)))
(is (do
(delete-note (build-key date title))
(not (note-exists? (build-key date title)))))))))
(deftest note-creation-utf
(let [date (get-date)
title "радуга"
note "# Радуга\nкаждый охотник желает знать, где сидят фазаны."
[year month day] date]
(testing "Note creation with UTF8 symbols"
(is (has-status
(send-request
[:post "/post-note"]
{:session session-key
:note note
:signature (sign session-key note)}) 302))
(is (note-exists? (build-key date title)))
(is (substring? "знать" ((send-request (url year month day title)) :body)))
(is (do
(delete-note (build-key date title))
(not (note-exists? (build-key date title))))))))
(deftest note-update
(let [date (get-date)
title "this-is-a-test-note"
[year month day] date
hash (sign session-key test-note)]
(testing "Note update"
(is (has-status
(send-request
[:post "/post-note"]
{:session session-key
:note test-note
:password "qwerty"
:signature hash}) 302))
(is (note-exists? (build-key date title)))
(is (substring? "test note" ((send-request (url year month day title)) :body)))
(is (has-status
(send-request
[:post "/update-note"]
{:noteID (build-key [year month day] title)
:note "WRONG pass"
:password "qwerty1" }) 403))
(is (substring? "test note" ((send-request (url year month day title)) :body)))
(is (has-status
(send-request
[:post "/update-note"]
{:noteID (build-key [year month day] title)
:note "UPDATED CONTENT 123"
:password "qwerty" }) 302))
(is (substring? "UPDATED CONTENT" ((send-request (url year month day title)) :body)))
(is (do
(delete-note (build-key date title))
(not (note-exists? (build-key date title))))))))
(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 "/api") 200) "accessing API")
(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"))))
(deftest test-app
(testing "main route"
(let [response (app (request :get "/"))]
(is (= (:status response) 200))
(is (substring? "Free and Hassle-free" (:body response)))))
(testing "not-found route"
(let [response (app (request :get "/invalid"))]
(is (= (:status response) 404)))))
-70
View File
@@ -1,70 +0,0 @@
(ns notehub.test.storage
(:use [notehub.storage]
[clojure.test])
(:require [taoensso.carmine :as car :refer (wcar)]))
(defn build-key [[y m d] t] (notehub.api/build-key y m d t))
(def date [2012 06 03])
(def test-title "Some title.")
(def test-note "This is a test note.")
(def metadata {:year 2012,
:month 6,
:day 23,
:title test-title,
:theme "dark",
:header-font "Anton"})
(def test-short-url "")
(deftest signature
(is (= "07e1c0d9533b5168e18a99f4540448af" (sign "wwq123456"))))
(deftest storage
(testing "Storage"
(testing "of short-url mechanism"
(let [fakeID (build-key date test-title)
url (create-short-url fakeID metadata)
url2 (create-short-url fakeID metadata)]
(is (= 1 (redis :scard (str fakeID :urls))))
(def test-short-url (create-short-url fakeID (assoc metadata :a :b)))
(is (= 2 (redis :scard (str fakeID :urls))))
(is (short-url-exists? url))
(is (= url url2))
(is (= metadata (resolve-url url)))
(delete-short-url url)
(is (not (short-url-exists? url))))))
(testing "of correct note creation"
(is (= (let [note-id (build-key date test-title)]
(add-note note-id test-note "testPID")
(is (= 2 (redis :scard (str note-id :urls))))
(create-short-url note-id metadata)
(is (= 3 (redis :scard (str note-id :urls))))
(increment-note-view note-id)
(get-note note-id))
test-note))
(is (= "1" (get-note-views (build-key date test-title))))
(is (= (do
(increment-note-view (build-key date test-title))
(get-note-views (build-key date test-title)))
"2")))
(testing "of note update"
(is (= (do
(add-note (build-key date test-title) test-note "testPID" "12345qwert")
(get-note (build-key date test-title)))
test-note))
(is (valid-password? (build-key date test-title) "12345qwert"))
(is (= (do
(edit-note (build-key date test-title) "update")
(get-note (build-key date test-title)))
"update")))
(testing "of the note access"
(is (not= (get-note (build-key date test-title)) "any text")))
(testing "of note existence"
(is (note-exists? (build-key date test-title)))
(is (short-url-exists? test-short-url))
(is (= 3 (redis :scard (str (build-key date test-title) :urls))))
(delete-note (build-key date test-title))
(is (not (short-url-exists? test-short-url)))
(is (not (note-exists? (build-key date test-title))))
(is (= 0 (redis :scard (str (build-key date test-title) :urls))))
(is (not (note-exists? (build-key [2013 06 03] test-title))))
(is (not (note-exists? (build-key date "some title"))))))