Permalink
Browse files

- test admin requests with no user logged in, a non-admin, and admin

  • Loading branch information...
1 parent a0af3d7 commit 25863ae630b0e900af9965afc88b6bbf3c5be16c @jberkman committed Apr 30, 2012
Showing with 59 additions and 19 deletions.
  1. +26 −19 web/test/functional/admin/players_controller_test.rb
  2. +33 −0 web/test/test_helper.rb
@@ -5,45 +5,52 @@ class Admin::PlayersControllerTest < ActionController::TestCase
@player = players(:one)
end
- test "should get index" do
+ test_permissions "get index" do |admin|
get :index
- assert_response :success
- assert_not_nil assigns(:players)
+ if admin
+ assert_response :success
+ assert_not_nil assigns(:players)
+ end
end
- test "should get new" do
+ test_permissions "get new" do |admin|
get :new
- assert_response :success
+ assert_response :success if admin
end
- test "should create player" do
- assert_difference('Player.count') do
+ test_permissions "create player" do |admin|
+ if admin
+ assert_difference('Player.count') do
+ post :create, :player => @player.attributes
+ end
+ else
post :create, :player => @player.attributes
end
-
- assert_redirected_to admin_player_path(assigns(:player))
end
- test "should show player" do
+ test_permissions "show player" do |admin|
get :show, :id => @player.to_param
- assert_response :success
+ assert_response :success if admin
end
- test "should get edit" do
+ test_permissions "get edit" do |admin|
get :edit, :id => @player.to_param
- assert_response :success
+ assert_response :success if admin
end
- test "should update player" do
+ test_permissions "should update player" do |admin|
put :update, :id => @player.to_param, :player => @player.attributes
- assert_redirected_to admin_player_path(assigns(:player))
+ assert_redirected_to admin_player_path(assigns(:player)) if admin
end
- test "should destroy player" do
- assert_difference('Player.count', -1) do
+ test_permissions "should destroy player" do |admin|
+ if admin
+ assert_difference('Player.count', -1) do
+ delete :destroy, :id => @player.to_param
+ end
+ assert_redirected_to admin_players_path
+ else
delete :destroy, :id => @player.to_param
end
-
- assert_redirected_to admin_players_path
end
end
View
@@ -26,4 +26,37 @@ def assert_redirected_to_login_url message = nil
# http://test.host/admin/players?player[id]=1&player[user]=player1%40example.com
assert_redirected_to(AppEngine::Users.create_login_url(request.url), message)
end
+
+ class << self
+ def test_not_logged_in name
+ test "should get redirected to login not #{name}" do
+ method("_test_#{name}_block".to_sym).call false
+ assert_redirected_to_login_url
+ end
+ end
+
+ def test_not_admin name
+ test "should be forbidden not #{name}" do
+ setup_login nonadmin_user
+ assert_raise ApplicationController::Forbidden do
+ method("_test_#{name}_block".to_sym).call false
+ end
+ end
+ end
+
+ def test_admin name
+ test "should #{name}" do
+ setup_login admin_user, true
+ method("_test_#{name}_block".to_sym).call true
+ end
+ end
+
+ def test_permissions name, &block
+ test_name = "#{name.gsub(/\s+/,'_')}"
+ define_method("_test_#{test_name}_block".to_sym, &block)
+ test_not_logged_in test_name
+ test_not_admin test_name
+ test_admin test_name
+ end
+ end
end

0 comments on commit 25863ae

Please sign in to comment.