Add tests for users controller

This commit is contained in:
Erik Michaels-Ober 2011-05-15 16:33:32 -07:00
parent 7128651e17
commit b48e6919fa
3 changed files with 73 additions and 4 deletions

View File

@ -9,7 +9,7 @@ class UsersController < Devise::RegistrationsController
redirect_to(:controller => "hydrants", :action => "show", :hydrant_id => params[:hydrant_id])
else
clean_up_passwords(resource)
render(:json => {"errors" => resource.errors})
render(:json => {"errors" => resource.errors}, :status => 500)
end
end
@ -20,7 +20,7 @@ class UsersController < Devise::RegistrationsController
render(:json => resource)
else
clean_up_passwords(resource)
render(:json => {"errors" => resource.errors})
render(:json => {"errors" => resource.errors}, :status => 500)
end
end
end

View File

@ -4,6 +4,7 @@ class SessionsControllerTest < ActionController::TestCase
include Devise::TestHelpers
setup do
request.env["devise.mapping"] = Devise.mappings[:user]
@user = users(:user)
end
test 'should render combo form' do
@ -18,17 +19,24 @@ class SessionsControllerTest < ActionController::TestCase
assert_select 'label', :count => 10
end
test 'should redirect if user is already authenticated' do
sign_in @user
get :new
assert_response :redirect
end
test 'should authenticate user if password is correct' do
post :create, :user => {:email => 'user@example.com', :password => 'correct'}
post :create, :user => {:email => @user.email, :password => 'correct'}
assert_response :success
end
test 'should return error if password is incorrect' do
post :create, :user => {:email => 'user@example.com', :password => 'incorrect'}
post :create, :user => {:email => @user.email, :password => 'incorrect'}
assert_response 401
end
test 'should empty session on sign out' do
sign_in @user
get :destroy
assert_equal Hash.new, session
assert_response :success

View File

@ -0,0 +1,61 @@
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
include Devise::TestHelpers
setup do
request.env["devise.mapping"] = Devise.mappings[:user]
@user = users(:user)
end
test 'should render info window' do
sign_in @user
get :edit
assert_response :success
assert_select 'div#info_window', true
assert_select 'form#edit_form' do
assert_select '[action=?]', '/users'
assert_select '[method=?]', 'post'
end
assert_select 'h2', 'Edit your Profile'
assert_select 'input', :count => 14
assert_select 'label', :count => 7
assert_select 'input[name="commit"]' do
assert_select '[type=?]', 'submit'
assert_select '[value=?]', 'Update'
end
assert_select 'form#back_form' do
assert_select '[action=?]', '/hydrant'
assert_select '[method=?]', 'get'
end
assert_select 'input[name="commit"]' do
assert_select '[type=?]', 'submit'
assert_select '[value=?]', 'Back'
end
end
test 'should update user if password is correct' do
sign_in @user
assert_not_equal @user.name, 'Erik Michaels-Ober'
put :update, :user => {:name => 'Erik Michaels-Ober', :current_password => 'correct'}
@user.reload
assert_equal @user.name, 'Erik Michaels-Ober'
assert_response :redirect
assert_redirected_to :controller => 'hydrants', :action => 'show'
end
test 'should return error if password is incorrect' do
sign_in @user
put :update, :user => {:name => 'Erik Michaels-Ober', :current_password => 'incorrect'}
assert_response :error
end
test 'should create user if information is valid' do
post :create, :user => {:email => 'user1@example.com', :name => @user.name, :password => 'correct', :password_confirmation => 'correct'}
assert_response :success
end
test 'should return error if information is invalid' do
post :create, :user => {:email => 'user2@example.com', :name => @user.name, :password => 'correct', :password_confirmation => 'incorrect'}
assert_response :error
end
end