Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 25863ae630b0e900af9965afc88b6bbf3c5be16c 1 parent a0af3d7
jacob berkman authored
45 web/test/functional/admin/players_controller_test.rb
@@ -5,45 +5,52 @@ class Admin::PlayersControllerTest < ActionController::TestCase
5 5 @player = players(:one)
6 6 end
7 7
8   - test "should get index" do
  8 + test_permissions "get index" do |admin|
9 9 get :index
10   - assert_response :success
11   - assert_not_nil assigns(:players)
  10 + if admin
  11 + assert_response :success
  12 + assert_not_nil assigns(:players)
  13 + end
12 14 end
13 15
14   - test "should get new" do
  16 + test_permissions "get new" do |admin|
15 17 get :new
16   - assert_response :success
  18 + assert_response :success if admin
17 19 end
18 20
19   - test "should create player" do
20   - assert_difference('Player.count') do
  21 + test_permissions "create player" do |admin|
  22 + if admin
  23 + assert_difference('Player.count') do
  24 + post :create, :player => @player.attributes
  25 + end
  26 + else
21 27 post :create, :player => @player.attributes
22 28 end
23   -
24   - assert_redirected_to admin_player_path(assigns(:player))
25 29 end
26 30
27   - test "should show player" do
  31 + test_permissions "show player" do |admin|
28 32 get :show, :id => @player.to_param
29   - assert_response :success
  33 + assert_response :success if admin
30 34 end
31 35
32   - test "should get edit" do
  36 + test_permissions "get edit" do |admin|
33 37 get :edit, :id => @player.to_param
34   - assert_response :success
  38 + assert_response :success if admin
35 39 end
36 40
37   - test "should update player" do
  41 + test_permissions "should update player" do |admin|
38 42 put :update, :id => @player.to_param, :player => @player.attributes
39   - assert_redirected_to admin_player_path(assigns(:player))
  43 + assert_redirected_to admin_player_path(assigns(:player)) if admin
40 44 end
41 45
42   - test "should destroy player" do
43   - assert_difference('Player.count', -1) do
  46 + test_permissions "should destroy player" do |admin|
  47 + if admin
  48 + assert_difference('Player.count', -1) do
  49 + delete :destroy, :id => @player.to_param
  50 + end
  51 + assert_redirected_to admin_players_path
  52 + else
44 53 delete :destroy, :id => @player.to_param
45 54 end
46   -
47   - assert_redirected_to admin_players_path
48 55 end
49 56 end
33 web/test/test_helper.rb
@@ -26,4 +26,37 @@ def assert_redirected_to_login_url message = nil
26 26 # http://test.host/admin/players?player[id]=1&player[user]=player1%40example.com
27 27 assert_redirected_to(AppEngine::Users.create_login_url(request.url), message)
28 28 end
  29 +
  30 + class << self
  31 + def test_not_logged_in name
  32 + test "should get redirected to login not #{name}" do
  33 + method("_test_#{name}_block".to_sym).call false
  34 + assert_redirected_to_login_url
  35 + end
  36 + end
  37 +
  38 + def test_not_admin name
  39 + test "should be forbidden not #{name}" do
  40 + setup_login nonadmin_user
  41 + assert_raise ApplicationController::Forbidden do
  42 + method("_test_#{name}_block".to_sym).call false
  43 + end
  44 + end
  45 + end
  46 +
  47 + def test_admin name
  48 + test "should #{name}" do
  49 + setup_login admin_user, true
  50 + method("_test_#{name}_block".to_sym).call true
  51 + end
  52 + end
  53 +
  54 + def test_permissions name, &block
  55 + test_name = "#{name.gsub(/\s+/,'_')}"
  56 + define_method("_test_#{test_name}_block".to_sym, &block)
  57 + test_not_logged_in test_name
  58 + test_not_admin test_name
  59 + test_admin test_name
  60 + end
  61 + end
29 62 end

0 comments on commit 25863ae

Please sign in to comment.
Something went wrong with that request. Please try again.