Browse Source

note update broken, when updating with older API verison

master
Christian Mueller 12 years ago
parent
commit
9266dc750c
  1. 20
      src/notehub/api.clj
  2. 14
      test/notehub/test/api.clj

20
src/notehub/api.clj

@ -61,9 +61,18 @@
(defn version-manager [f params] (defn version-manager [f params]
(if-let [version (:version params)] (if-let [version (:version params)]
(f (if (and (:noteID params) (< (Double/parseDouble version) 1.3)) (if (and (:noteID params) (< (Double/parseDouble version) 1.3))
(assoc params :noteID (sreplace (params :noteID) #" " "/")) (let [resp (f (assoc params
params)) :noteID (sreplace (params :noteID) #" " "/")
:noteID* (params :noteID)))
server-message (get-in resp [:status :message])]
(assoc-in resp [:status :message]
(str
server-message
(when server-message "; ")
"this API version is deprecated and "
"will be disabled by the end of June 2014!")))
(f params))
{:status (create-response false "API version expected")})) {:status (create-response false "API version expected")}))
(defn get-note [{:keys [noteID]}] (defn get-note [{:keys [noteID]}]
@ -109,11 +118,12 @@
{:status (create-response false (first errors))}))) {:status (create-response false (first errors))})))
(defn update-note [{:keys [noteID note pid signature password]}] (defn update-note [{:keys [noteID note pid signature password noteID*]}]
;(log "update-note: %s" {:pid pid :noteID noteID :signature signature :password password :note note}) ;(log "update-note: %s" {:pid pid :noteID noteID :signature signature :password password :note note})
(let [errors (filter identity (let [errors (filter identity
[(when-not (storage/valid-publisher? pid) "pid invalid") [(when-not (storage/valid-publisher? pid) "pid invalid")
(when-not (= signature (storage/sign pid (storage/get-psk pid) noteID note password)) ; TODO: noteID* is a hack introduced by backwards-comp. to older APIs
(when-not (= signature (storage/sign pid (storage/get-psk pid) (or noteID* noteID) note password))
"signature invalid") "signature invalid")
(when (blank? note) "note is empty") (when (blank? note) "note is empty")
(when-not (storage/valid-password? noteID password) "password invalid")])] (when-not (storage/valid-password? noteID password) "password invalid")])]

14
test/notehub/test/api.clj

@ -66,7 +66,11 @@
(is (= (:title get-response) (derive-title note))) (is (= (:title get-response) (derive-title note)))
(is (= "1" (get-in get-response [:statistics :views]))) (is (= "1" (get-in get-response [:statistics :views])))
(isnt (get-in get-response [:statistics :edited])) (isnt (get-in get-response [:statistics :edited]))
(is (= "noteID 'randomString' unknown"(get-in (parse-string (:body (send-request "/api/note" {:version "1.3" :noteID "randomString"}))) ["status" "message"]))) (is (= "noteID 'randomString' unknown"
(get-in
(parse-string
(:body (send-request "/api/note" {:version "1.3" :noteID "randomString"})))
["status" "message"])))
(is (= "3" (get-in (get-note post-response) [:statistics :views]))))) (is (= "3" (get-in (get-note post-response) [:statistics :views])))))
(testing "creation with wrong signature" (testing "creation with wrong signature"
(let [response (post-note {:note note :pid pid :signature (storage/sign pid2 psk note)})] (let [response (post-note {:note note :pid pid :signature (storage/sign pid2 psk note)})]
@ -166,11 +170,12 @@
:version "1.0" :version "1.0"
:password "qwerty"}) :password "qwerty"})
body (parse-string (:body response)) body (parse-string (:body response))
noteID (body "noteID")] origID (body "noteID")
noteID (clojure.string/replace origID #"/" " ")]
(testing "Note update" (testing "Note update"
(is (has-status response 200)) (is (has-status response 200))
(is (get-in body ["status" "success"])) (is (get-in body ["status" "success"]))
(is (storage/note-exists? noteID)) (is (storage/note-exists? origID))
(is (substring? "_test_ note" (is (substring? "_test_ note"
((parse-string ((parse-string
(:body (send-request [:get "/api/note"] {:version "1.0" :noteID noteID}))) "note"))) (:body (send-request [:get "/api/note"] {:version "1.0" :noteID noteID}))) "note")))
@ -184,7 +189,8 @@
body (parse-string (:body response))] body (parse-string (:body response))]
(is (has-status response 200)) (is (has-status response 200))
(isnt (get-in body ["status" "success"])) (isnt (get-in body ["status" "success"]))
(is (= "password invalid" (get-in body ["status" "message"]))) (is (= "password invalid; this API version is deprecated and will be disabled by the end of June 2014!"
(get-in body ["status" "message"])))
(isnt (get-in body ["statistics" "edited"])) (isnt (get-in body ["statistics" "edited"]))
(is (substring? "_test_ note" (is (substring? "_test_ note"
((parse-string ((parse-string

Loading…
Cancel
Save