diff --git a/src/NoteHub/api.clj b/src/NoteHub/api.clj index f77c0bd..747eb67 100644 --- a/src/NoteHub/api.clj +++ b/src/NoteHub/api.clj @@ -55,12 +55,11 @@ ([note pid signature] (post-note note pid signature nil)) ([note pid signature password] (let [errors (filter identity - (lazy-seq [(when-not (storage/valid-publisher? pid) "pid invalid") (when-not (= signature (get-signature pid (storage/get-psk pid) note)) "signature invalid") - (when (blank? note) "note is empty")]))] + (when (blank? note) "note is empty")])] (if (empty? errors) (let [[year month day] (get-date) untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %)) @@ -88,17 +87,19 @@ (defn update-note [noteID note pid signature password] (let [errors (filter identity - (lazy-seq + (seq [(when-not (storage/valid-publisher? pid) "pid invalid") (when-not (= signature (get-signature pid (storage/get-psk pid) noteID note password)) "signature invalid") (when (blank? note) "note is empty") - (when-not (storage/update-note noteID note password) "password invalid")]))] + (when-not (storage/valid-password? noteID password) "password invalid")]))] (if (empty? errors) - { - :longURL (getURL noteID) - :shortURL (getURL noteID :short) - :status (create-response true) - } + (do + (storage/edit-note noteID note) + { + :longURL (getURL noteID) + :shortURL (getURL noteID :short) + :status (create-response true) + }) {:status (create-response false (first errors))}))) diff --git a/src/NoteHub/storage.clj b/src/NoteHub/storage.clj index d1c5dab..59e7a50 100644 --- a/src/NoteHub/storage.clj +++ b/src/NoteHub/storage.clj @@ -54,6 +54,7 @@ (do (redis/srem db sessions token) was-valid)))) +; TODO: deprecated (defn update-note [noteID text passwd] (let [stored-password (redis/hget db password noteID)] @@ -61,6 +62,12 @@ (redis/hset db edited noteID (get-current-date)) (redis/hset db note noteID text)))) +(defn edit-note + [noteID text] + (do + (redis/hset db edited noteID (get-current-date)) + (redis/hset db note noteID text))) + (defn add-note ([noteID text] (add-note noteID text nil)) ([noteID text passwd] @@ -70,6 +77,10 @@ (when (not (blank? passwd)) (redis/hset db password noteID passwd))))) +(defn valid-password? [noteID passwd] + (let [stored (redis/hget db password noteID)] + (and stored (= stored passwd)))) + (defn get-note [noteID] (let [text (redis/hget db note noteID)] diff --git a/test/NoteHub/test/api.clj b/test/NoteHub/test/api.clj index cbdcf0b..1b2d4f3 100644 --- a/test/NoteHub/test/api.clj +++ b/test/NoteHub/test/api.clj @@ -4,8 +4,11 @@ (:use [NoteHub.api] [clojure.test])) +(def note "hello world! This is a _test_ note!") (def pid "somePlugin") (def pid2 "somePlugin2") +(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")) (defmacro isnt [arg] `(is (not ~arg))) @@ -13,7 +16,8 @@ (def psk (storage/register-publisher pid)) (f) (storage/revoke-publisher pid) - (storage/revoke-publisher pid2)) + (storage/revoke-publisher pid2) + (storage/delete-note note-title)) (use-fixtures :each register-publisher-fixture) @@ -28,36 +32,49 @@ (is (storage/revoke-publisher pid2)) (isnt (storage/valid-publisher? "any_PID")) (isnt (storage/valid-publisher? pid2)))) - #_ (testing "note publishing & retrieval" + (testing "note publishing & retrieval" + (isnt (:success (:status (get-note "some note id")))) (let [post-response (post-note note pid (get-signature pid psk note)) get-response (get-note (:noteID post-response))] + (is (= "note is empty" (:message (:status (post-note "" pid (get-signature pid psk "")))))) (is (:success (:status post-response))) (is (:success (:status get-response))) (is (= note (:note get-response))) - ; TODO: test all response fields!!!! - (is (= (:longURL post-response) (:longURL get-response))) - (is (= (:shortURL post-response) (:shortURL get-response)))) - (isnt (:success (:status (post-note note pid (get-signature pid psk note))))) + (is (= (:longURL post-response) (:longURL get-response) note-url)) + (is (= (:shortURL post-response) (:shortURL get-response))))) + (testing "creation with wrong signature" + (let [response (post-note note pid (get-signature pid2 psk note))] + (isnt (:success (:status response))) + (is (= "signature invalid" (:message (:status response))))) + (let [response (post-note note pid (get-signature pid2 psk "any note"))] + (isnt (:success (:status response))) + (is (= "signature invalid" (:message (:status response))))) (isnt (:success (:status (post-note note pid (get-signature pid "random_psk" note))))) (is (:success (:status (post-note note pid (get-signature pid psk note))))) - (let [psk2 (storage/register-publisher "randomPID")] - (is (:success (:status (post-note note "randomPID" (get-signature pid psk2 note))))) - (is (storage/revoke-publisher pid2)) - (isnt (:success (:status (post-note note "randomPID" (get-signature pid psk2 note))))))) - #_ (testing "note update" - (let [post-response (post-note note pid (get-signature pid psk note) "passwd") - note-id (:noteID post-response) - get-response (get-note note-id) - new-note "a new note!" - update-response (update-note note-id new-note pid (get-signature pid psk new-note) "passwd") - get-response-new (get-note note-id) - update-response-false (update-note note-id new-note pid (get-signature pid psk new-note) "pass") - ] - (is (:success (:status post-response))) - (is (:success (:status get-response))) - (is (:success (:status get-response-new))) - (is (:success (:status update-response))) - (isnt (:success (:status update-response-false))) - (is (= note (:note get-response))) - (is (= new-note (:note get-response-new))) - (is (= new-note (:note (get-note note-id)))))))) + (let [randomPID "randomPID" + psk2 (storage/register-publisher randomPID) + _ (storage/revoke-publisher randomPID) + response (post-note note randomPID (get-signature randomPID psk2 note))] + (isnt (:success (:status response))) + (is (= (:message (:status response)) "pid invalid")))) + (testing "note update" + (let [post-response (post-note note pid (get-signature pid psk note) "passwd") + note-id (:noteID post-response) + new-note "a new note!"] + (is (:success (:status post-response))) + (is (:success (:status (get-note note-id)))) + (is (= note (:note (get-note note-id)))) + (let [update-response (update-note note-id new-note pid (get-signature pid psk new-note) "passwd")] + (isnt (:success (:status update-response))) + (is (= "signature invalid" (:message (:status update-response))))) + (is (= note (:note (get-note note-id)))) + (let [update-response (update-note note-id new-note pid + (get-signature pid psk note-id new-note "passwd") "passwd")] + (is (= { :success true } (:status update-response))) + (is (= new-note (:note (get-note note-id))))) + (let [update-response (update-note note-id "aaa" pid + (get-signature pid psk note-id "aaa" "pass") "pass")] + (isnt (:success (:status update-response))) + (is (= "password invalid" (:message (:status update-response))))) + (is (= new-note (:note (get-note note-id)))) + (is (= new-note (:note (get-note note-id)))))))) diff --git a/test/NoteHub/test/storage.clj b/test/NoteHub/test/storage.clj index 8c92178..d1e6957 100644 --- a/test/NoteHub/test/storage.clj +++ b/test/NoteHub/test/storage.clj @@ -41,6 +41,7 @@ (add-note (build-key date test-title) test-note "12345qwert") (get-note (build-key date test-title))) test-note)) + #_(is (valid-password? (build-key date test-title) "12345qwert")) (is (= (do (update-note (build-key date test-title) "update" "12345qwert") (get-note (build-key date test-title)))