X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/b095b59d5e0ee805e49980af80b21bd1acff1345..349f5e0655f5026db54e4795e48a688eb2b7a654:/test/controllers/user_roles_controller_test.rb diff --git a/test/controllers/user_roles_controller_test.rb b/test/controllers/user_roles_controller_test.rb index 1d005824d..66735ccf5 100644 --- a/test/controllers/user_roles_controller_test.rb +++ b/test/controllers/user_roles_controller_test.rb @@ -1,8 +1,6 @@ -require 'test_helper' - -class UserRolesControllerTest < ActionController::TestCase - fixtures :users, :user_roles +require "test_helper" +class UserRolesControllerTest < ActionDispatch::IntegrationTest ## # test all routes which lead to this controller def test_routes @@ -19,116 +17,125 @@ class UserRolesControllerTest < ActionController::TestCase ## # test the grant action def test_grant + target_user = create(:user) + normal_user = create(:user) + administrator_user = create(:administrator_user) + super_user = create(:super_user) + # Granting should fail when not logged in - post :grant, :display_name => users(:normal_user).display_name, :role => "moderator" + post grant_role_path(target_user, "moderator") assert_response :forbidden # Login as an unprivileged user - session[:user] = users(:public_user).id + session_for(normal_user) # Granting should still fail - post :grant, :display_name => users(:normal_user).display_name, :role => "moderator" - assert_redirected_to user_path(users(:normal_user).display_name) - assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error] + post grant_role_path(target_user, "moderator") + assert_redirected_to :controller => :errors, :action => :forbidden # Login as an administrator - session[:user] = users(:administrator_user).id + session_for(administrator_user) UserRole::ALL_ROLES.each do |role| - # Granting a role to a non-existent user should fail assert_difference "UserRole.count", 0 do - post :grant, :display_name => "non_existent_user", :role => role + post grant_role_path("non_existent_user", role) end assert_response :not_found - assert_template "user/no_such_user" + assert_template "users/no_such_user" assert_select "h1", "The user non_existent_user does not exist" - # Granting a role from a user that already has it should fail + # Granting a role to a user that already has it should fail assert_no_difference "UserRole.count" do - post :grant, :display_name => users(:super_user).display_name, :role => role + post grant_role_path(super_user, role) end - assert_redirected_to user_path(users(:super_user).display_name) + assert_redirected_to user_path(super_user) assert_equal "The user already has role #{role}.", flash[:error] # Granting a role to a user that doesn't have it should work... assert_difference "UserRole.count", 1 do - post :grant, :display_name => users(:normal_user).display_name, :role => role + post grant_role_path(target_user, role) end - assert_redirected_to user_path(users(:normal_user).display_name) + assert_redirected_to user_path(target_user) # ...but trying a second time should fail assert_no_difference "UserRole.count" do - post :grant, :display_name => users(:normal_user).display_name, :role => role + post grant_role_path(target_user, role) end - assert_redirected_to user_path(users(:normal_user).display_name) + assert_redirected_to user_path(target_user) assert_equal "The user already has role #{role}.", flash[:error] - end # Granting a non-existent role should fail assert_difference "UserRole.count", 0 do - post :grant, :display_name => users(:normal_user).display_name, :role => "no_such_role" + post grant_role_path(target_user, "no_such_role") end - assert_redirected_to user_path(users(:normal_user).display_name) - assert_equal "The string `no_such_role' is not a valid role.", flash[:error] + assert_redirected_to user_path(target_user) + assert_equal "The string 'no_such_role' is not a valid role.", flash[:error] end ## # test the revoke action def test_revoke + target_user = create(:user) + normal_user = create(:user) + administrator_user = create(:administrator_user) + super_user = create(:super_user) + # Revoking should fail when not logged in - post :revoke, :display_name => users(:normal_user).display_name, :role => "moderator" + post revoke_role_path(target_user, "moderator") assert_response :forbidden # Login as an unprivileged user - session[:user] = users(:public_user).id + session_for(normal_user) # Revoking should still fail - post :revoke, :display_name => users(:normal_user).display_name, :role => "moderator" - assert_redirected_to user_path(users(:normal_user).display_name) - assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error] + post revoke_role_path(target_user, "moderator") + assert_redirected_to :controller => :errors, :action => :forbidden # Login as an administrator - session[:user] = users(:administrator_user).id + session_for(administrator_user) UserRole::ALL_ROLES.each do |role| - # Removing a role from a non-existent user should fail assert_difference "UserRole.count", 0 do - post :revoke, :display_name => "non_existent_user", :role => role + post revoke_role_path("non_existent_user", role) end assert_response :not_found - assert_template "user/no_such_user" + assert_template "users/no_such_user" assert_select "h1", "The user non_existent_user does not exist" # Removing a role from a user that doesn't have it should fail assert_no_difference "UserRole.count" do - post :revoke, :display_name => users(:normal_user).display_name, :role => role + post revoke_role_path(target_user, role) end - assert_redirected_to user_path(users(:normal_user).display_name) + assert_redirected_to user_path(target_user) assert_equal "The user does not have role #{role}.", flash[:error] - # Removing a role' from a user that has it should work... + # Removing a role from a user that has it should work... assert_difference "UserRole.count", -1 do - post :revoke, :display_name => users(:super_user).display_name, :role => role + post revoke_role_path(super_user, role) end - assert_redirected_to user_path(users(:super_user).display_name) + assert_redirected_to user_path(super_user) # ...but trying a second time should fail assert_no_difference "UserRole.count" do - post :revoke, :display_name => users(:super_user).display_name, :role => role + post revoke_role_path(super_user, role) end - assert_redirected_to user_path(users(:super_user).display_name) + assert_redirected_to user_path(super_user) assert_equal "The user does not have role #{role}.", flash[:error] - end # Revoking a non-existent role should fail assert_difference "UserRole.count", 0 do - post :revoke, :display_name => users(:normal_user).display_name, :role => "no_such_role" + post revoke_role_path(target_user, "no_such_role") end - assert_redirected_to user_path(users(:normal_user).display_name) - assert_equal "The string `no_such_role' is not a valid role.", flash[:error] + assert_redirected_to user_path(target_user) + assert_equal "The string 'no_such_role' is not a valid role.", flash[:error] + + # Revoking administrator role from current user should fail + post revoke_role_path(administrator_user, "administrator") + assert_redirected_to user_path(administrator_user) + assert_equal "Cannot revoke administrator role from current user.", flash[:error] end end