diff --git a/src/NoteHub/views/pages.clj b/src/NoteHub/views/pages.clj index 55d0976..da88a81 100644 --- a/src/NoteHub/views/pages.clj +++ b/src/NoteHub/views/pages.clj @@ -7,7 +7,7 @@ [clojure.core.incubator :only [-?>]] [hiccup.form] [noir.session :only [flash-put! flash-get]] - [noir.response :only [redirect]] + [noir.response :only [redirect status]] [noir.core :only [defpage render]] [noir.util.crypt :only [encrypt]] [noir.statuses] @@ -79,34 +79,31 @@ (common/layout title [:article (md-to-html post)]) - (get-page 404)))) + (status 404 "")))) ; New Note Posting (defpage [:post "/post-note"] {:keys [draft session-key session-value]} - (let [[year month day] (map #(+ (second %) (.get (Calendar/getInstance) (first %))) - {Calendar/YEAR 0, Calendar/MONTH 1, Calendar/DAY_OF_MONTH 0}) - untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %)) - (-> draft (split #"\n") first (sreplace " " "-") lower-case)) - trim (fn [s] (apply str (drop-while #(= \- %) s))) - title-uncut (-> untrimmed-line trim reverse trim reverse) - proposed-title (apply str (take max-title-length title-uncut)) - date [year month day] - title (first (drop-while #(note-exists? date %) - (cons proposed-title - (map #(str proposed-title "-" (+ 2 %)) (range)))))] + (let [valid-session (flash-get session-key) ; it was posted from a newly generated form + valid-draft (not (empty? draft)) ; the note is non-empty + valid-hash (try + (= (Short/parseShort session-value) ; the hash code is correct + (lib/hash #(.codePointAt % 0) (str draft session-key))) + (catch Exception e nil))] ; check whether the new note can be added - (let [valid-session (flash-get session-key) ; it was posted from a newly generated form - valid-draft (not (empty? draft)) ; the note is non-empty - valid-hash (= (Short/parseShort session-value) ; the hash code is correct - (lib/hash #(.codePointAt % 0) (str draft session-key)))] - (do - ; TODO: delete this if tests are written - (println "session:" valid-session "draft:" valid-draft "hash:" - (Short/parseShort session-value) - (lib/hash #(.codePointAt % 0) (str draft session-key))) - (if (and valid-session valid-draft valid-hash) - (do - (set-note date title draft) - ; TODO: the redirect is broken if title contains UTF chars - (redirect (apply str (interpose "/" ["" year month day title])))) - (get-page 400)))))) + (if (and valid-session valid-draft valid-hash) + (let [[year month day] (map #(+ (second %) (.get (Calendar/getInstance) (first %))) + {Calendar/YEAR 0, Calendar/MONTH 1, Calendar/DAY_OF_MONTH 0}) + untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %)) + (-> draft (split #"\n") first (sreplace " " "-") lower-case)) + trim (fn [s] (apply str (drop-while #(= \- %) s))) + title-uncut (-> untrimmed-line trim reverse trim reverse) + proposed-title (apply str (take max-title-length title-uncut)) + date [year month day] + title (first (drop-while #(note-exists? date %) + (cons proposed-title + (map #(str proposed-title "-" (+ 2 %)) (range)))))] + (do + (set-note date title draft) + ; TODO: the redirect is broken if title contains UTF chars + (redirect (apply str (interpose "/" ["" year month day title]))))) + (status 400 "")))) diff --git a/test/NoteHub/test/all.clj b/test/NoteHub/test/all.clj deleted file mode 100644 index 8ac755d..0000000 --- a/test/NoteHub/test/all.clj +++ /dev/null @@ -1,31 +0,0 @@ -(ns NoteHub.test.all - (:use [NoteHub.storage] [clojure.test])) - -(def date [2012 06 03]) -(def test-title "Some title.") -(def test-note "This is a test note.") - -(testing "Storage" - (testing "of correct note creation" - (is (= (do - (set-note date test-title test-note) - (get-note date test-title)) - test-note))) - (testing "of the note access" - (is (not= (get-note date test-title) "any text"))) - (testing "of note existence" - (is (note-exists? 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"))))) - - - -; to test -; -; crossover.lib -; storage -; pages -; noir.util.test diff --git a/test/NoteHub/test/storage.clj b/test/NoteHub/test/storage.clj index bc00145..046e141 100644 --- a/test/NoteHub/test/storage.clj +++ b/test/NoteHub/test/storage.clj @@ -1,4 +1,4 @@ -(ns NoteHub.test.all +(ns NoteHub.test.storage (:use [NoteHub.storage] [clojure.test])) (def date [2012 06 03]) diff --git a/test/NoteHub/test/views/pages.clj b/test/NoteHub/test/views/pages.clj new file mode 100644 index 0000000..fcf6059 --- /dev/null +++ b/test/NoteHub/test/views/pages.clj @@ -0,0 +1,19 @@ +(ns NoteHub.test.views.pages + (:use [NoteHub.views.pages] + [noir.util.test] + [clojure.test])) + +(deftest helper-functions + (testing "Markdown generation" + (is (= "

hello world

test code

" + (md-to-html "#_hello_ __world__\ntest `code`"))))) + +(deftest requests + (testing "HTTP Statuses" + (testing "of a wrong access" + (has-status (send-request "/wrong-page") 404)) + (testing "of corrupt note-post" + (has-status (send-request [:post "/2012/06/04/wrong-title"]) 404) + (has-status (send-request [:post "/post-note"]) 400)) + (testing "valid accesses" + (has-status (send-request "/") 200))))