Commits

Luke Plant  committed 0564114

Added tests for loginView

  • Participants
  • Parent commits 11b8100

Comments (0)

Files changed (2)

File testsuite/Main.hs

 import qualified Tests.Blog.DBUtils as DBUtils
 import qualified Tests.Blog.Formats as Formats
 import qualified Tests.Blog.Model as Model
+import qualified Tests.Blog.Views as View
 import Test.HUnit
 
 main = runTestTT (test [ "DBUtils tests" ~: DBUtils.tests
                        , "Format tests" ~: Formats.tests
                        , "Model tests" ~: Model.tests
+                       , "View tests" ~: View.tests
                        ])

File testsuite/Tests/Blog/Views.hs

 module Tests.Blog.Views where
 
+import Blog.Model
 import Blog.Settings
+import Blog.Views
+import Ella.Response
 import Ella.TestUtils
 import Test.HUnit
+import Tests.Blog.TestDB (connect)
+import Tests.Blog.Model (withEmptyUsersTable)
 
-login_req1 = mkPostReq "/login/" [("username", "luke")
-                                 ,("password", "testpassword")
-                                 ]
+login_req_bad = mkPostReq "/login/" [("username", "testusername")
+                                    ,("password", "testpassword2")
+                                    ]
+login_req_good = mkPostReq "/login/" [("username", "testusername")
+                                     ,("password", "testpassword")
+                                     ]
 
-tests = test [ ]
+
+withOneUser action = withEmptyUsersTable (\cn ->
+                                              do
+                                                createUser cn "testusername" False
+                                                setPassword cn "testusername" "testpassword"
+                                                action cn)
+
+testLoginFails = withOneUser
+                 (\cn -> do
+                    Just resp1 <- loginView' cn login_req_bad
+                    assertEqual "No cookies should be set" 0 (length $ cookies resp1)
+                 )
+
+
+testLoginSucceeds = withOneUser
+                    (\cn -> do
+                       Just resp1 <- loginView' cn login_req_good
+                       assertEqual "No cookies should be set" 2 (length $ cookies resp1)
+                    )
+
+tests = test [ "login fails" ~: testLoginFails
+             , "login succeeds" ~: testLoginSucceeds
+             ]