refactoring + new tests
This commit is contained in:
+8
-1
@@ -46,7 +46,7 @@
|
||||
(map #(+ (second %) (.get (Calendar/getInstance) (first %)))
|
||||
{Calendar/YEAR 0, Calendar/MONTH 1, Calendar/DAY_OF_MONTH 0}))
|
||||
|
||||
(defn create-response
|
||||
(defn- create-response
|
||||
([success] {:success success})
|
||||
([success message & params]
|
||||
(assoc (create-response success) :message (apply format message params))))
|
||||
@@ -59,6 +59,13 @@
|
||||
(str domain "/" (storage/create-short-url token {:year year :month month :day day :title title}))
|
||||
(str domain (url year month day title))))))
|
||||
|
||||
(defn version-manager [f params]
|
||||
(if-let [version (:version params)]
|
||||
(f (if (and (:noteID params) (< (Double/parseDouble version) 1.3))
|
||||
(assoc params :noteID (sreplace (params :noteID) #" " "/"))
|
||||
params))
|
||||
(create-response false "API version expected")))
|
||||
|
||||
(defn get-note [{:keys [noteID]}]
|
||||
(if (storage/note-exists? noteID)
|
||||
(let [note (storage/get-note noteID)]
|
||||
|
||||
+3
-11
@@ -90,27 +90,19 @@
|
||||
{:headers {"Content-Type" ctype}
|
||||
:body content})
|
||||
|
||||
(defn version-manager [f params]
|
||||
(generate-string
|
||||
(if-let [version (:version params)]
|
||||
(f (if (and (:noteID params) (< (Float/parseFloat version) 1.3))
|
||||
(assoc params :noteID (sreplace (params :noteID) #" " "/"))
|
||||
params))
|
||||
(api/create-response false "API version expected"))))
|
||||
|
||||
(defroutes api-routes
|
||||
|
||||
(GET "/" [] (layout (get-message :api-title)
|
||||
(md-node :article (slurp "API.md"))))
|
||||
|
||||
(GET "/note" {params :params}
|
||||
(version-manager api/get-note params))
|
||||
(generate-string (api/version-manager api/get-note params)))
|
||||
|
||||
(POST "/note" {params :params}
|
||||
(version-manager api/post-note params))
|
||||
(generate-string (api/version-manager api/post-note params)))
|
||||
|
||||
(PUT "/note" {params :params}
|
||||
(version-manager api/update-note params)))
|
||||
(generate-string (api/version-manager api/update-note params))))
|
||||
|
||||
(defroutes app-routes
|
||||
(context "/api" [] api-routes)
|
||||
|
||||
@@ -19,8 +19,8 @@
|
||||
(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}))))
|
||||
(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)))
|
||||
@@ -59,9 +59,9 @@
|
||||
(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
|
||||
(clojure.string/replace (:shortURL get-response) domain ""))
|
||||
(clojure.string/replace (:shortURL get-response) domain ""))
|
||||
resp (send-request ((:headers resp) "Location"))]
|
||||
(is (substring? "hello world"(:body resp))))
|
||||
(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])))
|
||||
@@ -95,13 +95,13 @@
|
||||
(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"})]
|
||||
: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")
|
||||
:signature (storage/sign pid psk note-id "aaa" "pass")
|
||||
:password "pass"})]
|
||||
(isnt (:success (:status update-response)))
|
||||
(is (= "password invalid" (:message (:status update-response)))))
|
||||
@@ -121,6 +121,14 @@
|
||||
(is (get-in body ["status" "success"]))
|
||||
(is (= note ((parse-string
|
||||
(:body (send-request [:get "/api/note"] {:version "1.0" :noteID noteID}))) "note")))
|
||||
(is (= "API version expected" ((parse-string
|
||||
(:body (send-request [:get "/api/note"] {:noteID noteID}))) "message")))
|
||||
(is (= note ((parse-string
|
||||
(:body (send-request [:get "/api/note"] {:version "1.1"
|
||||
:noteID (clojure.string/replace noteID #"/" " ")}))) "note")))
|
||||
(isnt (= note ((parse-string
|
||||
(:body (send-request [:get "/api/note"] {:version "1.3"
|
||||
:noteID (clojure.string/replace noteID #"/" " ")}))) "note")))
|
||||
(is (do
|
||||
(storage/delete-note noteID)
|
||||
(not (storage/note-exists? noteID)))))))
|
||||
|
||||
Reference in New Issue
Block a user