From f1397f47003f3b0c431b0fbf2503cc0d7e15e9bc Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Mon, 6 Mar 2017 16:55:05 +0000 Subject: [PATCH] Use user factory for user_roles_controller tests. --- .../controllers/user_roles_controller_test.rb | 76 +++++++++++-------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/test/controllers/user_roles_controller_test.rb b/test/controllers/user_roles_controller_test.rb index 61cf78b88..ef1a57cbe 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 - ## # test all routes which lead to this controller def test_routes @@ -19,20 +17,29 @@ class UserRolesControllerTest < ActionController::TestCase ## # test the grant action def test_grant + target_user = create(:user) + normal_user = create(:user) + administrator_user = create(:administrator_user) + # Create a super user which has all known roles + super_user = create(:user) + UserRole::ALL_ROLES.each do |role| + create(:user_role, :user => super_user, :granter => administrator_user, :role => role) + end + # Granting should fail when not logged in - post :grant, :display_name => users(:normal_user).display_name, :role => "moderator" + post :grant, :display_name => target_user.display_name, :role => "moderator" assert_response :forbidden # Login as an unprivileged user - session[:user] = users(:public_user).id + session[:user] = normal_user.id # 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) + post :grant, :display_name => target_user.display_name, :role => "moderator" + assert_redirected_to user_path(target_user.display_name) assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error] # Login as an administrator - session[:user] = users(:administrator_user).id + session[:user] = administrator_user.id UserRole::ALL_ROLES.each do |role| # Granting a role to a non-existent user should fail @@ -43,52 +50,61 @@ class UserRolesControllerTest < ActionController::TestCase assert_template "user/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, :display_name => super_user.display_name, :role => role end - assert_redirected_to user_path(users(:super_user).display_name) + assert_redirected_to user_path(super_user.display_name) 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, :display_name => target_user.display_name, :role => role end - assert_redirected_to user_path(users(:normal_user).display_name) + assert_redirected_to user_path(target_user.display_name) # ...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, :display_name => target_user.display_name, :role => role end - assert_redirected_to user_path(users(:normal_user).display_name) + assert_redirected_to user_path(target_user.display_name) 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, :display_name => target_user.display_name, :role => "no_such_role" end - assert_redirected_to user_path(users(:normal_user).display_name) + assert_redirected_to user_path(target_user.display_name) 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) + # Create a super user which has all known roles + super_user = create(:user) + UserRole::ALL_ROLES.each do |role| + create(:user_role, :user => super_user, :granter => administrator_user, :role => role) + end + # Revoking should fail when not logged in - post :revoke, :display_name => users(:normal_user).display_name, :role => "moderator" + post :revoke, :display_name => target_user.display_name, :role => "moderator" assert_response :forbidden # Login as an unprivileged user - session[:user] = users(:public_user).id + session[:user] = normal_user.id # 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) + post :revoke, :display_name => target_user.display_name, :role => "moderator" + assert_redirected_to user_path(target_user.display_name) assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error] # Login as an administrator - session[:user] = users(:administrator_user).id + session[:user] = administrator_user.id UserRole::ALL_ROLES.each do |role| # Removing a role from a non-existent user should fail @@ -101,30 +117,30 @@ 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, :display_name => users(:normal_user).display_name, :role => role + post :revoke, :display_name => target_user.display_name, :role => role end - assert_redirected_to user_path(users(:normal_user).display_name) + assert_redirected_to user_path(target_user.display_name) 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, :display_name => super_user.display_name, :role => role end - assert_redirected_to user_path(users(:super_user).display_name) + assert_redirected_to user_path(super_user.display_name) # ...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, :display_name => super_user.display_name, :role => role end - assert_redirected_to user_path(users(:super_user).display_name) + assert_redirected_to user_path(super_user.display_name) 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, :display_name => target_user.display_name, :role => "no_such_role" end - assert_redirected_to user_path(users(:normal_user).display_name) + assert_redirected_to user_path(target_user.display_name) assert_equal "The string `no_such_role' is not a valid role.", flash[:error] end end -- 2.39.5