X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/b8a8a88004d25837a10436fdc13128146b32c32b..6094a97ce6297d390e5bbf733f5c2b4cc6b2076c:/test/controllers/user_roles_controller_test.rb?ds=inline diff --git a/test/controllers/user_roles_controller_test.rb b/test/controllers/user_roles_controller_test.rb index e9aadcc3a..301a89f56 100644 --- a/test/controllers/user_roles_controller_test.rb +++ b/test/controllers/user_roles_controller_test.rb @@ -1,46 +1,45 @@ require "test_helper" -class UserRolesControllerTest < ActionController::TestCase +class UserRolesControllerTest < ActionDispatch::IntegrationTest ## # test all routes which lead to this controller def test_routes assert_routing( - { :path => "/user/username/role/rolename/grant", :method => :post }, - { :controller => "user_roles", :action => "grant", :display_name => "username", :role => "rolename" } + { :path => "/user/username/roles/rolename", :method => :post }, + { :controller => "user_roles", :action => "create", :user_display_name => "username", :role => "rolename" } ) assert_routing( - { :path => "/user/username/role/rolename/revoke", :method => :post }, - { :controller => "user_roles", :action => "revoke", :display_name => "username", :role => "rolename" } + { :path => "/user/username/roles/rolename", :method => :delete }, + { :controller => "user_roles", :action => "destroy", :user_display_name => "username", :role => "rolename" } ) end ## - # test the grant action - def test_grant + # test the grant role action + def test_update 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, :params => { :display_name => target_user.display_name, :role => "moderator" } + post user_role_path(target_user, "moderator") assert_response :forbidden # Login as an unprivileged user - session[:user] = normal_user.id + session_for(normal_user) # Granting should still fail - post :grant, :params => { :display_name => target_user.display_name, :role => "moderator" } - assert_redirected_to user_path(target_user) - assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error] + post user_role_path(target_user, "moderator") + assert_redirected_to :controller => :errors, :action => :forbidden # Login as an administrator - session[:user] = 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, :params => { :display_name => "non_existent_user", :role => role } + post user_role_path("non_existent_user", role) end assert_response :not_found assert_template "users/no_such_user" @@ -48,20 +47,20 @@ class UserRolesControllerTest < ActionController::TestCase # Granting a role to a user that already has it should fail assert_no_difference "UserRole.count" do - post :grant, :params => { :display_name => super_user.display_name, :role => role } + post user_role_path(super_user, role) end 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, :params => { :display_name => target_user.display_name, :role => role } + post user_role_path(target_user, role) end assert_redirected_to user_path(target_user) # ...but trying a second time should fail assert_no_difference "UserRole.count" do - post :grant, :params => { :display_name => target_user.display_name, :role => role } + post user_role_path(target_user, role) end assert_redirected_to user_path(target_user) assert_equal "The user already has role #{role}.", flash[:error] @@ -69,39 +68,38 @@ class UserRolesControllerTest < ActionController::TestCase # Granting a non-existent role should fail assert_difference "UserRole.count", 0 do - post :grant, :params => { :display_name => target_user.display_name, :role => "no_such_role" } + post user_role_path(target_user, "no_such_role") end assert_redirected_to user_path(target_user) - assert_equal "The string `no_such_role' is not a valid role.", flash[:error] + assert_equal "The string 'no_such_role' is not a valid role.", flash[:error] end ## - # test the revoke action - def test_revoke + # test the revoke role action + def test_destroy 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, :params => { :display_name => target_user.display_name, :role => "moderator" } + delete user_role_path(target_user, "moderator") assert_response :forbidden # Login as an unprivileged user - session[:user] = normal_user.id + session_for(normal_user) # Revoking should still fail - post :revoke, :params => { :display_name => target_user.display_name, :role => "moderator" } - assert_redirected_to user_path(target_user) - assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error] + delete user_role_path(target_user, "moderator") + assert_redirected_to :controller => :errors, :action => :forbidden # Login as an administrator - session[:user] = 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, :params => { :display_name => "non_existent_user", :role => role } + delete user_role_path("non_existent_user", role) end assert_response :not_found assert_template "users/no_such_user" @@ -109,20 +107,20 @@ class UserRolesControllerTest < ActionController::TestCase # Removing a role from a user that doesn't have it should fail assert_no_difference "UserRole.count" do - post :revoke, :params => { :display_name => target_user.display_name, :role => role } + delete user_role_path(target_user, role) end 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... assert_difference "UserRole.count", -1 do - post :revoke, :params => { :display_name => super_user.display_name, :role => role } + delete user_role_path(super_user, role) end assert_redirected_to user_path(super_user) # ...but trying a second time should fail assert_no_difference "UserRole.count" do - post :revoke, :params => { :display_name => super_user.display_name, :role => role } + delete user_role_path(super_user, role) end assert_redirected_to user_path(super_user) assert_equal "The user does not have role #{role}.", flash[:error] @@ -130,13 +128,13 @@ class UserRolesControllerTest < ActionController::TestCase # Revoking a non-existent role should fail assert_difference "UserRole.count", 0 do - post :revoke, :params => { :display_name => target_user.display_name, :role => "no_such_role" } + delete user_role_path(target_user, "no_such_role") end assert_redirected_to user_path(target_user) - assert_equal "The string `no_such_role' is not a valid role.", flash[:error] + assert_equal "The string 'no_such_role' is not a valid role.", flash[:error] # Revoking administrator role from current user should fail - post :revoke, :params => { :display_name => administrator_user.display_name, :role => "administrator" } + delete user_role_path(administrator_user, "administrator") assert_redirected_to user_path(administrator_user) assert_equal "Cannot revoke administrator role from current user.", flash[:error] end