Add tests for sessions controller

This commit is contained in:
Erik Michaels-Ober 2011-05-08 20:15:19 -07:00
parent e71c3ee8ad
commit b4c6028a88
3 changed files with 44 additions and 1 deletions

View File

@ -9,7 +9,7 @@ class SessionsController < Devise::SessionsController
sign_in(resource_name, resource)
render(:json => resource)
else
render(:json => {"errors" => {:password => ["You need to sign in or sign up before continuing."]}})
render(:json => {"errors" => {:password => ["You need to sign in or sign up before continuing."]}}, :status => 401)
end
end

7
test/fixtures/users.yml vendored Normal file
View File

@ -0,0 +1,7 @@
user:
name: User
organization: Code for America
email: user@example.com
voice_number: 1234567890
sms_number: 1234567890
encrypted_password: "$2a$10$KF/JMZ494ZLhWLgHZeBTf.cSL4l0Wjij4gIZP7BzkueAC1p4nW0ma"

View File

@ -0,0 +1,36 @@
require 'test_helper'
class SessionsControllerTest < ActionController::TestCase
include Devise::TestHelpers
setup do
request.env["devise.mapping"] = Devise.mappings[:user]
end
test 'should render combo form' do
get :new
assert_response :success
assert_select 'form' do
assert_select '[action=?]', '/users/sign_in'
assert_select '[method=?]', 'post'
end
assert_select 'h2', 'Adopt this Hydrant'
assert_select 'input', :count => 15
assert_select 'label', :count => 10
end
test 'should authenticate user if password is correct' do
post :create, :user => {:email => 'user@example.com', :password => 'correct'}
assert_response :success
end
test 'should return error if password is incorrect' do
post :create, :user => {:email => 'user@example.com', :password => 'incorrect'}
assert_response 401
end
test 'should empty session on sign out' do
get :destroy
assert_equal Hash.new, session
assert_response :success
end
end