Browse Source

tests implementation finished

master
Christian Mueller 14 years ago
parent
commit
a05ac74185
  1. 27
      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

27
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,10 +79,18 @@
(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 [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
(if (and valid-session valid-draft valid-hash)
(let [[year month day] (map #(+ (second %) (.get (Calendar/getInstance) (first %))) (let [[year month day] (map #(+ (second %) (.get (Calendar/getInstance) (first %)))
{Calendar/YEAR 0, Calendar/MONTH 1, Calendar/DAY_OF_MONTH 0}) {Calendar/YEAR 0, Calendar/MONTH 1, Calendar/DAY_OF_MONTH 0})
untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %)) untrimmed-line (filter #(or (= \- %) (Character/isLetterOrDigit %))
@ -94,19 +102,8 @@
title (first (drop-while #(note-exists? date %) title (first (drop-while #(note-exists? date %)
(cons proposed-title (cons proposed-title
(map #(str proposed-title "-" (+ 2 %)) (range)))))] (map #(str proposed-title "-" (+ 2 %)) (range)))))]
; 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 (do
(set-note date title draft) (set-note date title draft)
; TODO: the redirect is broken if title contains UTF chars ; TODO: the redirect is broken if title contains UTF chars
(redirect (apply str (interpose "/" ["" year month day title])))) (redirect (apply str (interpose "/" ["" year month day title])))))
(get-page 400)))))) (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