diff --git a/src/NoteHub/storage.clj b/src/NoteHub/storage.clj index 7748742..b76c02b 100644 --- a/src/NoteHub/storage.clj +++ b/src/NoteHub/storage.clj @@ -18,21 +18,13 @@ (def sessions "sessions") (def short-url "short-url") -; Concatenates all fields to a string -(defn build-key - "Returns a storage-key for the given note coordinates" - [[year month day] title] - (print-str year month day title)) - (defn create-session - "Creates a random session token" [] (let [token (encrypt (str (rand-int Integer/MAX_VALUE)))] (do (redis/sadd db sessions token) token))) (defn invalidate-session - "Invalidates given session" [token] ; Jedis is buggy & returns an NPE for token == nil (when token @@ -41,49 +33,43 @@ was-valid)))) (defn update-note - "Updates a note with the given store key if the specified password is correct" - [key text passwd] - (let [stored-password (redis/hget db password key)] + [noteID text passwd] + (let [stored-password (redis/hget db password noteID)] (when (and stored-password (= passwd stored-password)) - (redis/hset db note key text)))) + (redis/hset db note noteID text)))) -(defn set-note - "Creates a note with the given title and text in the given date namespace" - ([date title text] (set-note date title text nil)) - ([date title text passwd] - (let [key (build-key date title)] - (do - (redis/hset db note key text) - (when (not (blank? passwd)) - (redis/hset db password key passwd)))))) +(defn add-note + ([noteID text] (add-note noteID text nil)) + ([noteID text passwd] + (do + (redis/hset db note noteID text) + (when (not (blank? passwd)) + (redis/hset db password noteID passwd))))) (defn get-note - "Gets the note from the given date namespaces for the specified title" - [date title] - (let [key (build-key date title) - text (redis/hget db note key)] + [noteID] + (let [text (redis/hget db note noteID)] (when text (do - (redis/hincrby db views key 1) + (redis/hincrby db views noteID 1) text)))) (defn get-note-views - "Returns the number of views for the specified date and note title" - [date title] - (redis/hget db views (build-key date title))) + "Returns the number of views for the specified noteID" + [noteID] + (redis/hget db views noteID)) (defn note-exists? - "Returns true if the note with the specified title and date exists" - [date title] - (redis/hexists db note (build-key date title))) + "Returns true if the note with the specified noteID" + [noteID] + (redis/hexists db note noteID)) (defn delete-note "Deletes the note with the specified coordinates" - [date title] - (let [key (build-key date title)] - (doseq [kw [password views note]] - ; TODO: delete short url by looking for the title - (redis/hdel db kw key)))) + [noteID] + (doseq [kw [password views note]] + ; TODO: delete short url by looking for the title + (redis/hdel db kw noteID))) (defn short-url-exists? "Checks whether the provided short url is taken (for testing only)" @@ -99,10 +85,10 @@ (defn delete-short-url "Deletes a short url (for testing only)" - [key] - (let [value (redis/hget db short-url key)] + [noteID] + (let [value (redis/hget db short-url noteID)] (do - (redis/hdel db short-url key) + (redis/hdel db short-url noteID) (redis/hdel db short-url value)))) (defn create-short-url diff --git a/src/NoteHub/views/pages.clj b/src/NoteHub/views/pages.clj index 653a815..148c1f9 100644 --- a/src/NoteHub/views/pages.clj +++ b/src/NoteHub/views/pages.clj @@ -16,6 +16,12 @@ (:import [java.util Calendar])) +; Concatenates all fields to a string +(defn build-key + "Returns a storage-key for the given note coordinates" + [[year month day] title] + (print-str year month day title)) + (defn get-hash "A simple hash-function, which computes a hash from the text field content and given session number. It is intended to be used as a spam @@ -103,8 +109,9 @@ ; Update Note Page (defpage "/:year/:month/:day/:title/edit" {:keys [year month day title]} (input-form "/update-note" :update - (html (hidden-field :key (build-key [year month day] title))) - (get-note [year month day] title) :enter-passwd)) + (let [noteID (build-key [year month day] title)] + (html (hidden-field :key noteID)) + (get-note noteID) :enter-passwd))) ; New Note Page (defpage "/new" {} @@ -118,16 +125,16 @@ (wrap (create-short-url params) (select-keys params [:title :theme :header-font :text-font]) - (get-note [year month day] title))) + (get-note (build-key [year month day] title)))) ; Provides Markdown of the specified note (defpage "/:year/:month/:day/:title/export" {:keys [year month day title]} - (when-let [md-text (get-note [year month day] title)] + (when-let [md-text (get-note (build-key [year month day] title))] (content-type "text/plain; charset=utf-8" md-text))) ; Provides the number of views of the specified note (defpage "/:year/:month/:day/:title/stats" {:keys [year month day title]} - (when-let [views (get-note-views [year month day] title)] + (when-let [views (get-note-views (build-key [year month day] title))] (layout (get-message :statistics) [:table#stats.helvetica.central-element [:tr @@ -167,11 +174,11 @@ ; TODO: replace to ccs/take when it gets fixed proposed-title (apply str (take max-length title-uncut)) date [year month day] - title (first (drop-while #(note-exists? date %) + title (first (drop-while #(note-exists? (build-key date %)) (cons proposed-title (map #(str proposed-title "-" (+ 2 %)) (range)))))] (do - (set-note date title draft password) + (add-note (build-key date title) draft password) (redirect (url year month day title)))) (response 400)))) diff --git a/test/NoteHub/test/storage.clj b/test/NoteHub/test/storage.clj index d8544ce..adc4eb1 100644 --- a/test/NoteHub/test/storage.clj +++ b/test/NoteHub/test/storage.clj @@ -1,5 +1,7 @@ (ns NoteHub.test.storage - (:use [NoteHub.storage] [clojure.test])) + (:use [NoteHub.storage] + [NoteHub.views.pages] + [clojure.test])) (def date [2012 06 03]) (def test-title "Some title.") @@ -25,29 +27,29 @@ (short-url-exists? url)))))) (testing "of correct note creation" (is (= (do - (set-note date test-title test-note) - (get-note date test-title)) + (add-note (build-key date test-title) test-note) + (get-note (build-key date test-title))) test-note)) - (is (= "1" (get-note-views date test-title))) + (is (= "1" (get-note-views (build-key date test-title)))) (is (= (do - (get-note date test-title) - (get-note-views date test-title)) + (get-note (build-key date test-title)) + (get-note-views (build-key date test-title))) "2"))) (testing "of note update" (is (= (do - (set-note date test-title test-note "12345qwert") - (get-note date test-title)) + (add-note (build-key date test-title) test-note "12345qwert") + (get-note (build-key date test-title))) test-note)) (is (= (do (update-note (build-key date test-title) "update" "12345qwert") - (get-note date test-title)) + (get-note (build-key date test-title))) "update")) (is (= (do (update-note (build-key date test-title) "not authorized" "44444") - (get-note date test-title)) + (get-note (build-key date test-title))) "update"))) (testing "of the note access" - (is (not= (get-note date test-title) "any text"))) + (is (not= (get-note (build-key date test-title)) "any text"))) (testing "session management" (let [s1 (create-session) s2 (create-session) @@ -58,9 +60,9 @@ (is (not (invalidate-session "wrongtoken"))) (is (invalidate-session s3)))) (testing "of note existence" - (is (note-exists? date test-title)) + (is (note-exists? (build-key date test-title))) (is (not (do - (delete-note date test-title) - (note-exists? date test-title)))) - (is (not (note-exists? [2013 06 03] test-title))) - (is (not (note-exists? date "some title")))))) + (delete-note (build-key date test-title)) + (note-exists? (build-key date test-title))))) + (is (not (note-exists? (build-key [2013 06 03] test-title)))) + (is (not (note-exists? (build-key date "some title"))))))) diff --git a/test/NoteHub/test/views/pages.clj b/test/NoteHub/test/views/pages.clj index be36b2b..a4416b2 100644 --- a/test/NoteHub/test/views/pages.clj +++ b/test/NoteHub/test/views/pages.clj @@ -6,16 +6,15 @@ [clojure.test])) (defn substring? [a b] - (not (= nil - (re-matches (re-pattern (str "(?s).*" a ".*")) b)))) + (not (= nil (re-matches (re-pattern (str "(?s).*" a ".*")) b)))) (def date [2012 6 3]) (def test-title "some-title") (def test-note "# This is a test note.\nHello _world_. Motörhead, тест.") (defn create-testnote-fixture [f] - (set-note date test-title test-note) + (add-note (build-key date test-title) test-note) (f) - (delete-note date test-title)) + (delete-note (build-key date test-title))) (use-fixtures :each create-testnote-fixture) @@ -23,8 +22,8 @@ (deftest testing-fixture (testing "Was a not created?" - (is (= (get-note date test-title) test-note)) - (is (note-exists? date test-title)))) + (is (= (get-note (build-key date test-title)) test-note)) + (is (note-exists? (build-key date test-title))))) (deftest export-test (testing "Markdown export" @@ -42,12 +41,12 @@ {:session-key session-key :draft test-note :session-value (str (get-hash (str test-note session-key)))}) 302)) - (is (note-exists? date title)) + (is (note-exists? (build-key date title))) (is (substring? "Hello _world_" ((send-request (url year month day title)) :body))) (is (do - (delete-note date title) - (not (note-exists? date title))))))) + (delete-note (build-key date title)) + (not (note-exists? (build-key date title)))))))) (deftest note-update (let [session-key (create-session) @@ -62,7 +61,7 @@ :draft "test note" :password "qwerty" :session-value (str (get-hash (str "test note" session-key)))}) 302)) - (is (note-exists? date title)) + (is (note-exists? (build-key date title))) (is (substring? "test note" ((send-request (url year month day title)) :body))) (is (has-status @@ -82,8 +81,8 @@ (is (substring? "UPDATED CONTENT" ((send-request (url year month day title)) :body))) (is (do - (delete-note date title) - (not (note-exists? date title))))))) + (delete-note (build-key date title)) + (not (note-exists? (build-key date title)))))))) (deftest requests (testing "HTTP Status"