OK, I made the destroy test happy by changing the post line a little:
# from
post :destroy, :id => @first_id, :user_id => users(:admin_user).id#to
post :destroy, { :id => @first_id }, { :user_id => users(:admin_user).id }
But the problem still crops up on the list method, which I only want to allow for admins. So I'll try to include the relevant code.
# from application.rb
def must_be_admin
temp_user = User.find(session[:user_id], :select => :level) rescue nil
if temp_user
if temp_user.level != "admin"
logger.debug('must_be_admin - user exists but not admin')
flash[:notice] = "Access not permitted"
redirect_to(:controller => :statics, :action => :index)
end
else
session[:original_uri] = request.request_uri
flash[:notice] = "Please log in"
redirect_to(:controller => :users, :action => :login)
end
end# from users_controller.rb
before_filter :must_be_admin, :only => [ :list, :destroy ]
def list
@user_pages, @users = paginate :users, :per_page => 20
end
# from users_controller_test.rb
def test_list_for_admin_user
get :list, {}, { :user_id => users(:admin_user).id }
assert_response :success
assert_template 'list'
assert_not_nil assigns(:users) # from scaffold code: not sure what this does
end
def test_list_for_non_admin_user
get :list, {}, { :user_id => users(:normal_user).id }
assert_response :redirect
assert_redirected_to :controller => 'statics', :action => 'index'
assert_template 'statics/show'
assert_equal 'Access not permitted', flash[:notice]
end
def test_list_for_not_logged_in
get :list, {}, { :user_id => nil }
assert_response :redirect
assert_redirected_to :action => 'login'
assert_template 'login'
assert_equal 'Please log in', flash[:notice]
end
test_list_for_admin_user runs OK. The other two tests cause these errors:
4) Error:
test_list_for_non_admin_user(UsersControllerTest):
NoMethodError: undefined method `first' for :statics:Symbol
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:72:in `assert_redirected_to'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:43:in `each'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:43:in `assert_redirected_to'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions.rb:60:in `clean_backtrace'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:35:in `assert_redirected_to'
test/functional/users_controller_test.rb:40:in `test_list_for_non_admin_user'
5) Error:
test_list_for_not_logged_in(UsersControllerTest):
NoMethodError: undefined method `first' for :users:Symbol
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:72:in `assert_redirected_to'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:43:in `each'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:43:in `assert_redirected_to'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions.rb:60:in `clean_backtrace'
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/assertions/response_assertions.rb:35:in `assert_redirected_to'
test/functional/users_controller_test.rb:49:in `test_list_for_not_logged_in'
I hope I posted enough code to figure this out. Thanks for having a look.