Browse files

Updated tests

  • Loading branch information...
1 parent 3301586 commit ed562f0116635f0f20bae9c83cddbef9c99bd2b1 @zk committed May 7, 2012
Showing with 96 additions and 74 deletions.
  1. +2 −0 .gitignore
  2. +2 −2 src/nsfw/csrf.clj
  3. +10 −1 src/nsfw/env.clj
  4. +16 −28 src/nsfw/html.clj
  5. +2 −1 src/nsfw/middleware.clj
  6. +41 −1 src/nsfw/mongo.clj
  7. +0 −6 test/nsfw/test/core.clj
  8. +0 −24 test/nsfw/test/gen.clj
  9. +12 −0 test/nsfw/test/mongo.clj
  10. +11 −11 test/nsfw/test/util.clj
View
2 .gitignore
@@ -3,3 +3,5 @@ pom.xml
lib
classes
.DS_Store
+/.lein-deps-sum
+/.lein-failures
View
4 src/nsfw/csrf.clj
@@ -12,10 +12,10 @@
;; Used in the middleware to bind the current csrf token for
;; use through (current) by actions and templates.
-(def *csrf-token* nil)
+(def ^:dynamic *csrf-token* nil)
(defn gen-token []
- (sha1-str (str (java.util.UUID/randomUUID))))
+ (sha1 (str (java.util.UUID/randomUUID))))
(defn insert-token [resp token]
(let [ses (get resp :session {})
View
11 src/nsfw/env.clj
@@ -1,5 +1,6 @@
(ns nsfw.env "Shell environment helpers."
- (:require [clojure.string :as str]))
+ (:require [clojure.string :as str])
+ (:refer-clojure :exclude (int str)))
(defn clj->env [sym-or-str]
(-> sym-or-str
@@ -14,3 +15,11 @@
(or (System/getenv (clj->env sym))
default))
+(defn int [sym & [default]]
+ (if-let [env-var (env sym)]
+ (Integer/parseInt env-var)
+ default))
+
+(defn str [sym & [default]]
+ (env sym default))
+
View
44 src/nsfw/html.clj
@@ -1,44 +1,32 @@
(ns nsfw.html
- (:use [hiccup core]))
-
-(defmacro defopts
- "Allows passing of key-value pairs at the end of a function call,
- which will be collected into a map named `opts`.
-
- Example:
-
- (defopts sum [a b]
- (merge opts {:total (+ a b)}))
-
- (sum 1 2 :difficulty \"simple\" :calc-time (System/currentTimeMillis))
- ;;=> {:total 3 :difficulty \"simple\" :calc-time 123456789012}"
- [name arg-vec & body]
- (let [args arg-vec
- opts (symbol "opts")]
- `(defn ~name [~@args & ~opts]
- (let [~opts (apply hash-map ~opts)]
- ~@body))))
+ (:require [hiccup.core :as hiccup]
+ [hiccup.page :as hiccup-page])
+ (:refer-clojure :exclude [meta]))
(defn href [href content & opts]
(let [opts (apply hash-map opts)]
- (html [:a (merge opts {:href href})
- content])))
+ (hiccup/html [:a (merge opts {:href href})
+ content])))
(defn image-url [name]
(str "/images/" name))
(defn image [name & opts]
(let [opts (apply hash-map opts)
opts (merge {:src (image-url name)} opts)]
- (html [:img opts])))
-
-(defn meta-tag [he content]
- (html [:meta {:http-equiv he :content content}]))
+ (hiccup/html [:img opts])))
-(defopts label [content]
- (html [:label opts
- content]))
+(defn meta [he content]
+ (hiccup/html [:meta {:http-equiv he :content content}]))
+(defn script [path]
+ [:script {:type "text/javascript" :src path}])
+(defn stylesheet [path]
+ [:link {:rel "stylesheet" :href path}])
+(defn html5 [& body]
+ (hiccup-page/html5 body))
+(defn html [body]
+ (hiccup/html body))
View
3 src/nsfw/middleware.clj
@@ -36,7 +36,8 @@
session, file handling, and params."
[handler & opts]
(let [opts (apply hash-map opts)
- session-store (get opts :session-store (memory-store))
+ session-store (get opts :session-store (memory-store
+ (get opts :session-atom (atom {}))))
public-path (get opts :public-path "resources/public")]
(-> handler
wrap-keyword-params
View
42 src/nsfw/mongo.clj
@@ -1,6 +1,46 @@
(ns nsfw.mongo
(:use [somnium.congomongo])
- (:require [nsfw.util :as util]))
+ (:require [nsfw.util :as util])
+ (:import [java.net URI]))
(defn bson-id [id-or-str]
(org.bson.types.ObjectId/massageToObjectId id-or-str))
+
+(defn parse-username
+ "ex. uri http://foo:bar@zaarly.com returns `\"foo\"`."
+ [^URI uri]
+ (when-let [un (->> uri
+ .getUserInfo
+ (take-while #(not= \: %))
+ (reduce str))]
+ (if-not (empty? un) un nil)))
+
+(defn parse-password
+ "ex. uri http://foo:bar@zaarly.com returns `\"bar\"`."
+ [^URI uri]
+ (when-let [pw (->> uri
+ .getUserInfo
+ (drop-while #(not= \: %))
+ (drop 1)
+ (reduce str))]
+ (if-not (empty? pw) pw nil)))
+
+(defn parse-mongo-conn-info
+ "Takes a string representing a mongod connection and returns a map
+ representing the connection information.
+
+ ex. (parse-mongo-conn-info \"mongodb://foo:bar@localhost:123/zaarly\")
+ ;; => {:host \"localhost\"
+ :db \"zaarly\"
+ :port 27107
+ :username \"foo\"
+ :password \"bar\"}"
+ [mongo-url-str]
+ (let [uri (java.net.URI. mongo-url-str)]
+ {:host (.getHost uri)
+ :db (->> uri .getPath (drop 1) (apply str))
+ :port (.getPort uri)
+ :username (parse-username uri)
+ :password (parse-password uri)}))
+
+
View
6 test/nsfw/test/core.clj
@@ -1,6 +0,0 @@
-(ns nsfw.test.core
- (:use [nsfw.core] :reload)
- (:use [clojure.test]))
-
-(deftest replace-me ;; FIXME: write
- (is false "No tests have been written."))
View
24 test/nsfw/test/gen.clj
@@ -1,24 +0,0 @@
-(ns nsfw.test.gen
- (:use [nsfw.gen] :reload)
- (:use [clojure.test]))
-
-(deftest test-resolve-file
- (is (= "./hello/world" (resolve-file "hello" "world")))
- (is (= "./hello/world" (resolve-file :hello :world))))
-
-(deftest test-resolve-js
- (is (= "./resources/public/js/hello.js" (resolve-js "hello")))
- (is (= "./resources/public/js/hello.js" (resolve-js :hello)))
- (is (= "./resources/public/js/hello/world.min.js" (resolve-js :hello :world.min)))
- (is (= "./resources/public/js/hello.js" (resolve-js "hello.js")))
- (is (= "./resources/public/js/hello.js" (resolve-js :hello.js))))
-
-(deftest test-read-project-name
- (is (= "nsfw" (read-project-name))))
-
-(deftest test-underscore-project-name
- (is (= "my_proj" (underscore-project-name "my-proj"))))
-
-(deftest test-resolve-project-src-file
- (is (= "./src/nsfw/hello.clj" (resolve-project-src-file "hello.clj")))
- (is (= "./src/nsfw/hello.clj" (resolve-project-src-file :hello.clj))))
View
12 test/nsfw/test/mongo.clj
@@ -0,0 +1,12 @@
+(ns nsfw.test.mongo
+ (:use [nsfw.mongo :as mon] :reload)
+ (:use [clojure.test]))
+
+(deftest test-parse-username
+ (is (= "foo" (parse-username (java.net.URI. "http://foo:bar@zaarly.com"))))
+ (is (= nil (parse-username (java.net.URI. "http://zaarly.com")))))
+
+(deftest test-parse-password
+ (is (= "bar" (parse-password (java.net.URI. "http://foo:bar@zaarly.com"))))
+ (is (= nil (parse-password (java.net.URI. "http://zaarly.com")))))
+
View
22 test/nsfw/test/util.clj
@@ -2,15 +2,15 @@
(:use nsfw.util :reload)
(:use clojure.test))
-(deftest test-json-encode
- (is (= "\"foo\"" (json-encode "foo")))
- (is (= "10" (json-encode 10)))
- (is (= "true" (json-encode true)))
- (is (= "null" (json-encode nil)))
- (is (= {:foo "bar"} (json-encode (json-decode {:foo "bar"})))))
+(deftest test-to-json
+ (is (= "\"foo\"" (to-json "foo")))
+ (is (= "10" (to-json 10)))
+ (is (= "true" (to-json true)))
+ (is (= "null" (to-json nil)))
+ (is (= {:foo "bar"} (from-json (to-json {:foo "bar"})))))
-(deftest test-json-decode
- (is (= "foo" (json-decode "\"foo\"")))
- (is (= 10 (json-decode "10")))
- (is (= true (json-decode "true")))
- (is (= nil (json-decode "null"))))
+(deftest test-from-json
+ (is (= "foo" (from-json "\"foo\"")))
+ (is (= 10 (from-json "10")))
+ (is (= true (from-json "true")))
+ (is (= nil (from-json "null"))))

0 comments on commit ed562f0

Please sign in to comment.