2011-05-15 23:33:32 +00:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class UsersControllerTest < ActionController::TestCase
|
|
|
|
include Devise::TestHelpers
|
|
|
|
setup do
|
2014-04-01 08:15:29 +00:00
|
|
|
request.env['devise.mapping'] = Devise.mappings[:user]
|
2011-05-15 23:55:37 +00:00
|
|
|
@user = users(:erik)
|
2011-05-15 23:33:32 +00:00
|
|
|
end
|
|
|
|
|
2011-12-25 07:23:19 +00:00
|
|
|
test 'should render edit form' do
|
2011-05-15 23:33:32 +00:00
|
|
|
sign_in @user
|
|
|
|
get :edit
|
|
|
|
assert_response :success
|
|
|
|
assert_select 'form#edit_form' do
|
|
|
|
assert_select '[action=?]', '/users'
|
|
|
|
assert_select '[method=?]', 'post'
|
|
|
|
end
|
2014-03-25 09:47:44 +00:00
|
|
|
assert_select 'input', count: 15
|
|
|
|
assert_select 'label', count: 12
|
2011-05-15 23:33:32 +00:00
|
|
|
assert_select 'input[name="commit"]' do
|
|
|
|
assert_select '[type=?]', 'submit'
|
|
|
|
assert_select '[value=?]', 'Update'
|
|
|
|
end
|
2012-01-14 22:31:01 +00:00
|
|
|
assert_select 'a.btn', 'Back'
|
2011-05-15 23:33:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should update user if password is correct' do
|
|
|
|
sign_in @user
|
2012-06-18 17:49:28 +00:00
|
|
|
assert_not_equal 'New Name', @user.name
|
2014-03-25 09:47:44 +00:00
|
|
|
put :update, user: {name: 'New Name', current_password: 'correct'}
|
2011-05-15 23:33:32 +00:00
|
|
|
@user.reload
|
2012-06-18 17:49:28 +00:00
|
|
|
assert_equal 'New Name', @user.name
|
2011-05-15 23:33:32 +00:00
|
|
|
assert_response :redirect
|
2014-03-25 09:47:44 +00:00
|
|
|
assert_redirected_to controller: 'sidebar', action: 'search'
|
2011-05-15 23:33:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should return error if password is incorrect' do
|
|
|
|
sign_in @user
|
2014-03-25 09:47:44 +00:00
|
|
|
put :update, user: {name: 'New Name', current_password: 'incorrect'}
|
2011-05-15 23:33:32 +00:00
|
|
|
assert_response :error
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should create user if information is valid' do
|
2014-03-25 09:47:44 +00:00
|
|
|
post :create, user: {email: 'user@example.com', name: 'User', password: 'correct', password_confirmation: 'correct'}
|
2011-05-15 23:33:32 +00:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should return error if information is invalid' do
|
2014-03-25 09:47:44 +00:00
|
|
|
post :create, user: {email: 'user@example.com', name: 'User', password: 'correct', password_confirmation: 'incorrect'}
|
2011-05-15 23:33:32 +00:00
|
|
|
assert_response :error
|
|
|
|
end
|
|
|
|
end
|