From 9266dc750c88139feec1b77ca6e7c09c2611040f Mon Sep 17 00:00:00 2001 From: Christian Mueller Date: Mon, 17 Feb 2014 20:33:21 +0100 Subject: [PATCH] note update broken, when updating with older API verison --- src/notehub/api.clj | 20 +++++++++++++++----- test/notehub/test/api.clj | 14 ++++++++++---- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/notehub/api.clj b/src/notehub/api.clj index 7753591..91e78b3 100644 --- a/src/notehub/api.clj +++ b/src/notehub/api.clj @@ -61,9 +61,18 @@ (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)) + (if (and (:noteID params) (< (Double/parseDouble version) 1.3)) + (let [resp (f (assoc 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")})) (defn get-note [{:keys [noteID]}] @@ -109,11 +118,12 @@ {: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}) (let [errors (filter identity [(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") (when (blank? note) "note is empty") (when-not (storage/valid-password? noteID password) "password invalid")])] diff --git a/test/notehub/test/api.clj b/test/notehub/test/api.clj index 0a651e9..1515d77 100644 --- a/test/notehub/test/api.clj +++ b/test/notehub/test/api.clj @@ -66,7 +66,11 @@ (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.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]))))) (testing "creation with wrong signature" (let [response (post-note {:note note :pid pid :signature (storage/sign pid2 psk note)})] @@ -166,11 +170,12 @@ :version "1.0" :password "qwerty"}) body (parse-string (:body response)) - noteID (body "noteID")] + origID (body "noteID") + noteID (clojure.string/replace origID #"/" " ")] (testing "Note update" (is (has-status response 200)) (is (get-in body ["status" "success"])) - (is (storage/note-exists? noteID)) + (is (storage/note-exists? origID)) (is (substring? "_test_ note" ((parse-string (:body (send-request [:get "/api/note"] {:version "1.0" :noteID noteID}))) "note"))) @@ -184,7 +189,8 @@ body (parse-string (:body response))] (is (has-status response 200)) (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"])) (is (substring? "_test_ note" ((parse-string