-- putting auto-commit back into 'add' command
[bkell-clj.git] / test / login_test.clj
blobbdc292574a4091f6d1d788797570a6d089023da2
1 (ns login-test
3         (:use [helpers] :reload-all)
4         (:use [depth_adapter])
5         (:require [bkell])
7     (:use [clojure.test])
8         (:import java.io.ByteArrayInputStream)
9         (:require clojure.contrib.str-utils)
10     (:require commands.add)
11     (:require commands.remove)
12     (:require commands.authenticate)
16 (def configs (load-file "etc/config/config.test.clj"))
19 ;; FIXTUREs
20 (defn test-fixture-shell
21     "Initialize the shell"
22     [test]
24     (println "test-fixture-shell CALLED")
25     (bkell/init-shell)
27     (test)
30 (defn test-fixture-db
31     "test to clear out shell memory before a test is run"
32     [test]
34     (println "test-fixture-db CALLED")
36     ;; make the shell active
37     ;; create a basic user in the DB
38         (dosync (alter bkell/shell conj { :active true }))
39     (add-user (:url-test configs) (:system-dir configs) { :tag "user" :attrs { :id "test.user" } } )
41     ;; ** execute the TEST function
42     (test)
44     ;; make the shell inactive
45         (dosync (alter bkell/shell conj { :active false }))
46     (remove-user (:url-test configs) (:system-dir configs) { :tag "user" :attrs { :id "test.user" } :content { :tag "stub" } } )
50 ;;(use-fixtures :once login-test/test-fixture-shell )
51 (use-fixtures :each login-test/test-fixture-shell login-test/test-fixture-db )
53 ;; test basic login
54 (deftest test-login []
56     
57     (let [
58           user_seq 
59           (login-user 
60             (helpers/get-user   (:url-test configs) (:system-dir configs) 
61                                 { :tag "user" :attrs { :id "test.user"} :content {:tag "stub"} } 
62             )) ]
63       
64       (is (not (nil? user_seq))
65           (str 
66               "User should NOT be nil > inputs > " 
67                 (:url-test configs) " " (:system-dir configs) " " { :tag "user" :attrs { :id "test.user"}} )
68       )
69       (is (not (nil? (@bkell/shell :logged-in-user)))
70           "User should be in a 'logged-in-user' state")
71     )
75 ;; test result when already logged in
76 (deftest test-existing-login []
78     (let [
79           user_seq 
80           (login-user (helpers/get-user (:url-test configs) (:system-dir configs) { :tag "user" :attrs { :id "test.user"} :content {:tag "stub"} } )) ]
81       
82       (try 
83         (def 
84           nd_user 
85           (login-user (helpers/get-user (:url-test configs) (:system-dir configs) { :tag "user" :attrs { :id "test.user"} :content {:tag "stub"} } )) )
86         ;;(catch ...)
87         (finally 
88           (is (not (nil? nd_user)) "2nd_user SHOULD NOT be nil") 
89         )
90       )
92     )
96 ;; test a login with a bad password
97 (deftest test-bad-password []
99     (comment let [  logged-in-user  
100             (login-user 
101               (helpers/get-user 
102                 (:url-test configs) (:system-dir configs) { :tag "user" :attrs { :id "test.user" :password "fubar" } :content {:tag "stub"} } ))]
103         (is (not (nil? nd_user)) "2nd_user SHOULD NOT be nil") 
104     )
108 ;; test logging out
109 (deftest test-logout []
111     (let [
112           user_seq 
113           (login-user (helpers/get-user (:url-test configs) (:system-dir configs) { :tag "user" :attrs { :id "test.user"} :content {:tag "stub"} } )) ]
114          
115         (let [logged-out-user 
116             (logout-user 
117               (helpers/get-user 
118                 (:url-test configs) (:system-dir configs) { :tag "user" :attrs { :id "test.user"} :content {:tag "stub"} } ))] 
119           
120           (is (not (nil? logged-out-user)) "SHOULD return the logged-out-user") 
121           (is (nil? (@bkell/shell :logged-in-user)) "there SHOULD NOT be a logged-in-user" )
122         )
124     )