Browse Source

API tests implemented

master
Christian Mueller 12 years ago
parent
commit
eae64cbbe1
  1. 8
      API.md
  2. 11
      src/NoteHub/api.clj
  3. 62
      test/NoteHub/test/api.clj
  4. 102
      test/NoteHub/test/storage.clj

8
API.md

@ -14,7 +14,7 @@ All API requests must be issued with one special parameter `version` denoting th
A simple `GET` request to the following URL: A simple `GET` request to the following URL:
http://notehub.org/api/get-note?version=1.0&title=<NOTE-ID> http://notehub.org/api/note?version=1.0&title=<NOTE-ID>
will return a JSON object containing following self explaining fields: `note`, `longURL`, `shortURL`, `statistics`, `status`. will return a JSON object containing following self explaining fields: `note`, `longURL`, `shortURL`, `statistics`, `status`.
@ -42,7 +42,7 @@ The note ID is a string, containing the date of publishing and a few first words
A note must be created by a `POST` request to the following URL: A note must be created by a `POST` request to the following URL:
http://notehub.org/api/post-note http://notehub.org/api/note
with the following parameters: with the following parameters:
@ -74,9 +74,9 @@ The status object serves the same purpose as in the case of note retrieval.
## Note Update ## Note Update
To update a note, an `UPDATE` request must be issued to the following URL: To update a note, an `PUT` request must be issued to the following URL:
http://notehub.org/api/update-note http://notehub.org/api/note
with the following parameters: with the following parameters:

11
src/NoteHub/api.clj

@ -0,0 +1,11 @@
(ns NoteHub.api)
(def api-version "1.0")
(defn get-signature [& args])
(defn post-note [& args])
(defn get-note [& args])
(defn update-note [& args])
(defn register-publisher [& args])
(defn revoke-publisher [& args])
(defn valid-publisher? [& args])

62
test/NoteHub/test/api.clj

@ -0,0 +1,62 @@
(ns NoteHub.test.api
(:use [NoteHub.api] [clojure.test]))
(def note "Hello world, this is a test note!")
(def note2 "Another test note")
(def pid "somePlugin")
(def pid2 "somePlugin2")
(def ver api-version)
(defmacro isnt [arg] `(is (not ~arg)))
(defn register-publisher-fixture [f]
(def psk (register-publisher pid))
(f)
(revoke-publisher pid))
(deftest api
(testing "API"
(testing "signature implementation"
(is (= 3577853521 (get-signature "Lorem ipsum dolor sit amet" "abcdef")))
(is (= -180217198 (get-signature "Notehub is a free pastebin for markdown" "12345678")))
(is (= 6887137804 (get-signature "abcd !§$%& параграф" "A VERY LONG KEY"))))
(testing "publisher registration"
(let [psk2 (register-publisher pid2)]
(is (valid-publisher? pid))
(is (valid-publisher? pid2))
(is (revoke-publisher pid2))
(isnt (revoke-publisher "anyPID"))
(isnt (valid-publisher? "any_PID"))
(isnt (valid-publisher? pid2))))
(testing "note publishing & retrieval"
(let [post-response (post-note note pid (get-signature note psk) ver)
get-response (get-note ver (:noteID post-response))]
(is (:success (:status post-response)))
(is (:success (:status get-response)))
(is (= note (:note get-response)))
(is (= (:longURL post-response) (:longURL get-response)))
(is (= (:shortURL post-response) (:shortURL get-response))))
(isnt (:success (:status (post-note note pid (get-signature note2 psk) ver))))
(isnt (:success (:status (post-note note pid (get-signature note "random_psk") ver))))
(is (:success (:status (post-note note pid (get-signature note psk) ver))))
(let [psk2 (register-publisher "randomPID")]
(is (:success (:status (post-note note "randomPID" (get-signature note psk2) ver))))
(is (revoke-publisher pid2))
(isnt (:success (:status (post-note note "randomPID" (get-signature note psk2) ver))))))
(testing "note update"
(let [post-response (post-note note pid (get-signature note psk) ver "passwd")
note-id (:noteID post-response)
get-response (get-note ver note-id)
new-note "a new note!"
update-response (update-note note-id new-note pid (get-signature new-note psk) ver "passwd")
get-response-new (get-note ver note-id)
update-response-false (update-note note-id new-note pid (get-signature new-note psk) ver "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))))))))

102
test/NoteHub/test/storage.clj

@ -13,54 +13,54 @@
(deftest storage (deftest storage
(testing "Storage" (testing "Storage"
(testing "of short-url mechanism" (testing "of short-url mechanism"
(let [url (create-short-url metadata) (let [url (create-short-url metadata)
url2 (create-short-url metadata)] url2 (create-short-url metadata)]
(is (short-url-exists? url)) (is (short-url-exists? url))
(is (= url url2)) (is (= url url2))
(is (= metadata (resolve-url url))) (is (= metadata (resolve-url url)))
(is (not (do (is (not (do
(delete-short-url url) (delete-short-url url)
(short-url-exists? url)))))) (short-url-exists? url))))))
(testing "of correct note creation" (testing "of correct note creation"
(is (= (do (is (= (do
(set-note date test-title test-note) (set-note date test-title test-note)
(get-note date test-title)) (get-note date test-title))
test-note)) test-note))
(is (= "1" (get-note-views date test-title))) (is (= "1" (get-note-views date test-title)))
(is (= (do (is (= (do
(get-note date test-title) (get-note date test-title)
(get-note-views date test-title)) (get-note-views date test-title))
"2"))) "2")))
(testing "of note update" (testing "of note update"
(is (= (do (is (= (do
(set-note date test-title test-note "12345qwert") (set-note date test-title test-note "12345qwert")
(get-note date test-title)) (get-note date test-title))
test-note)) test-note))
(is (= (do (is (= (do
(update-note (build-key date test-title) "update" "12345qwert") (update-note (build-key date test-title) "update" "12345qwert")
(get-note date test-title)) (get-note date test-title))
"update")) "update"))
(is (= (do (is (= (do
(update-note (build-key date test-title) "not authorized" "44444") (update-note (build-key date test-title) "not authorized" "44444")
(get-note date test-title)) (get-note date test-title))
"update"))) "update")))
(testing "of the note access" (testing "of the note access"
(is (not= (get-note date test-title) "any text"))) (is (not= (get-note date test-title) "any text")))
(testing "session management" (testing "session management"
(let [s1 (create-session) (let [s1 (create-session)
s2 (create-session) s2 (create-session)
s3 (create-session)] s3 (create-session)]
(is (invalidate-session s1)) (is (invalidate-session s1))
(is (not (invalidate-session (str s1 s2)))) (is (not (invalidate-session (str s1 s2))))
(is (invalidate-session s2)) (is (invalidate-session s2))
(is (not (invalidate-session "wrongtoken"))) (is (not (invalidate-session "wrongtoken")))
(is (invalidate-session s3)))) (is (invalidate-session s3))))
(testing "of note existence" (testing "of note existence"
(is (note-exists? date test-title)) (is (note-exists? date test-title))
(is (not (do (is (not (do
(delete-note date test-title) (delete-note date test-title)
(note-exists? date test-title)))) (note-exists? date test-title))))
(is (not (note-exists? [2013 06 03] test-title))) (is (not (note-exists? [2013 06 03] test-title)))
(is (not (note-exists? date "some title")))))) (is (not (note-exists? date "some title"))))))

Loading…
Cancel
Save