Browse Source

tests implementation finished

master
Christian Mueller 14 years ago
parent
commit
a05ac74185
  1. 53
      src/NoteHub/views/pages.clj
  2. 31
      test/NoteHub/test/all.clj
  3. 2
      test/NoteHub/test/storage.clj
  4. 19
      test/NoteHub/test/views/pages.clj

53
src/NoteHub/views/pages.clj

@ -7,7 +7,7 @@
[clojure.core.incubator :only [-?>]] [clojure.core.incubator :only [-?>]]
[hiccup.form] [hiccup.form]
[noir.session :only [flash-put! flash-get]] [noir.session :only [flash-put! flash-get]]
[noir.response :only [redirect]] [noir.response :only [redirect status]]
[noir.core :only [defpage render]] [noir.core :only [defpage render]]
[noir.util.crypt :only [encrypt]] [noir.util.crypt :only [encrypt]]
[noir.statuses] [noir.statuses]
@ -79,34 +79,31 @@
(common/layout title (common/layout title
[:article [:article
(md-to-html post)]) (md-to-html post)])
(get-page 404)))) (status 404 ""))))
; New Note Posting ; New Note Posting
(defpage [:post "/post-note"] {:keys [draft session-key session-value]} (defpage [:post "/post-note"] {:keys [draft session-key session-value]}
(let [[year month day] (map #(+ (second %) (.get (Calendar/getInstance) (first %))) (let [valid-session (flash-get session-key) ; it was posted from a newly generated form
{Calendar/YEAR 0, Calendar/MONTH 1, Calendar/DAY_OF_MONTH 0}) valid-draft (not (empty? draft)) ; the note is non-empty
untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %)) valid-hash (try
(-> draft (split #"\n") first (sreplace " " "-") lower-case)) (= (Short/parseShort session-value) ; the hash code is correct
trim (fn [s] (apply str (drop-while #(= \- %) s))) (lib/hash #(.codePointAt % 0) (str draft session-key)))
title-uncut (-> untrimmed-line trim reverse trim reverse) (catch Exception e nil))]
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)))))]
; check whether the new note can be added ; check whether the new note can be added
(let [valid-session (flash-get session-key) ; it was posted from a newly generated form (if (and valid-session valid-draft valid-hash)
valid-draft (not (empty? draft)) ; the note is non-empty (let [[year month day] (map #(+ (second %) (.get (Calendar/getInstance) (first %)))
valid-hash (= (Short/parseShort session-value) ; the hash code is correct {Calendar/YEAR 0, Calendar/MONTH 1, Calendar/DAY_OF_MONTH 0})
(lib/hash #(.codePointAt % 0) (str draft session-key)))] untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %))
(do (-> draft (split #"\n") first (sreplace " " "-") lower-case))
; TODO: delete this if tests are written trim (fn [s] (apply str (drop-while #(= \- %) s)))
(println "session:" valid-session "draft:" valid-draft "hash:" title-uncut (-> untrimmed-line trim reverse trim reverse)
(Short/parseShort session-value) proposed-title (apply str (take max-title-length title-uncut))
(lib/hash #(.codePointAt % 0) (str draft session-key))) date [year month day]
(if (and valid-session valid-draft valid-hash) title (first (drop-while #(note-exists? date %)
(do (cons proposed-title
(set-note date title draft) (map #(str proposed-title "-" (+ 2 %)) (range)))))]
; TODO: the redirect is broken if title contains UTF chars (do
(redirect (apply str (interpose "/" ["" year month day title])))) (set-note date title draft)
(get-page 400)))))) ; TODO: the redirect is broken if title contains UTF chars
(redirect (apply str (interpose "/" ["" year month day title])))))
(status 400 ""))))

31
test/NoteHub/test/all.clj

@ -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

2
test/NoteHub/test/storage.clj

@ -1,4 +1,4 @@
(ns NoteHub.test.all (ns NoteHub.test.storage
(:use [NoteHub.storage] [clojure.test])) (:use [NoteHub.storage] [clojure.test]))
(def date [2012 06 03]) (def date [2012 06 03])

19
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 (= "<h1><em>hello</em> <strong>world</strong></h1><p>test <code>code</code></p>"
(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))))
Loading…
Cancel
Save