3 class UserRolesControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/user/username/role/rolename/grant", :method => :post },
9 { :controller => "user_roles", :action => "grant", :display_name => "username", :role => "rolename" }
12 { :path => "/user/username/role/rolename/revoke", :method => :post },
13 { :controller => "user_roles", :action => "revoke", :display_name => "username", :role => "rolename" }
18 # test the grant action
20 target_user = create(:user)
21 normal_user = create(:user)
22 administrator_user = create(:administrator_user)
23 super_user = create(:super_user)
25 # Granting should fail when not logged in
26 post grant_role_path(:display_name => target_user.display_name, :role => "moderator")
27 assert_response :forbidden
29 # Login as an unprivileged user
30 session_for(normal_user)
32 # Granting should still fail
33 post grant_role_path(:display_name => target_user.display_name, :role => "moderator")
34 assert_redirected_to :controller => :errors, :action => :forbidden
36 # Login as an administrator
37 session_for(administrator_user)
39 UserRole::ALL_ROLES.each do |role|
40 # Granting a role to a non-existent user should fail
41 assert_difference "UserRole.count", 0 do
42 post grant_role_path(:display_name => "non_existent_user", :role => role)
44 assert_response :not_found
45 assert_template "users/no_such_user"
46 assert_select "h1", "The user non_existent_user does not exist"
48 # Granting a role to a user that already has it should fail
49 assert_no_difference "UserRole.count" do
50 post grant_role_path(:display_name => super_user.display_name, :role => role)
52 assert_redirected_to user_path(super_user)
53 assert_equal "The user already has role #{role}.", flash[:error]
55 # Granting a role to a user that doesn't have it should work...
56 assert_difference "UserRole.count", 1 do
57 post grant_role_path(:display_name => target_user.display_name, :role => role)
59 assert_redirected_to user_path(target_user)
61 # ...but trying a second time should fail
62 assert_no_difference "UserRole.count" do
63 post grant_role_path(:display_name => target_user.display_name, :role => role)
65 assert_redirected_to user_path(target_user)
66 assert_equal "The user already has role #{role}.", flash[:error]
69 # Granting a non-existent role should fail
70 assert_difference "UserRole.count", 0 do
71 post grant_role_path(:display_name => target_user.display_name, :role => "no_such_role")
73 assert_redirected_to user_path(target_user)
74 assert_equal "The string `no_such_role' is not a valid role.", flash[:error]
78 # test the revoke action
80 target_user = create(:user)
81 normal_user = create(:user)
82 administrator_user = create(:administrator_user)
83 super_user = create(:super_user)
85 # Revoking should fail when not logged in
86 post revoke_role_path(:display_name => target_user.display_name, :role => "moderator")
87 assert_response :forbidden
89 # Login as an unprivileged user
90 session_for(normal_user)
92 # Revoking should still fail
93 post revoke_role_path(:display_name => target_user.display_name, :role => "moderator")
94 assert_redirected_to :controller => :errors, :action => :forbidden
96 # Login as an administrator
97 session_for(administrator_user)
99 UserRole::ALL_ROLES.each do |role|
100 # Removing a role from a non-existent user should fail
101 assert_difference "UserRole.count", 0 do
102 post revoke_role_path(:display_name => "non_existent_user", :role => role)
104 assert_response :not_found
105 assert_template "users/no_such_user"
106 assert_select "h1", "The user non_existent_user does not exist"
108 # Removing a role from a user that doesn't have it should fail
109 assert_no_difference "UserRole.count" do
110 post revoke_role_path(:display_name => target_user.display_name, :role => role)
112 assert_redirected_to user_path(target_user)
113 assert_equal "The user does not have role #{role}.", flash[:error]
115 # Removing a role from a user that has it should work...
116 assert_difference "UserRole.count", -1 do
117 post revoke_role_path(:display_name => super_user.display_name, :role => role)
119 assert_redirected_to user_path(super_user)
121 # ...but trying a second time should fail
122 assert_no_difference "UserRole.count" do
123 post revoke_role_path(:display_name => super_user.display_name, :role => role)
125 assert_redirected_to user_path(super_user)
126 assert_equal "The user does not have role #{role}.", flash[:error]
129 # Revoking a non-existent role should fail
130 assert_difference "UserRole.count", 0 do
131 post revoke_role_path(:display_name => target_user.display_name, :role => "no_such_role")
133 assert_redirected_to user_path(target_user)
134 assert_equal "The string `no_such_role' is not a valid role.", flash[:error]
136 # Revoking administrator role from current user should fail
137 post revoke_role_path(:display_name => administrator_user.display_name, :role => "administrator")
138 assert_redirected_to user_path(administrator_user)
139 assert_equal "Cannot revoke administrator role from current user.", flash[:error]