3 class UserRolesControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/user/username/roles/rolename", :method => :post },
9 { :controller => "user_roles", :action => "create", :user_display_name => "username", :role => "rolename" }
12 { :path => "/user/username/roles/rolename", :method => :delete },
13 { :controller => "user_roles", :action => "destroy", :user_display_name => "username", :role => "rolename" }
18 # test the grant role 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 user_role_path(target_user, "moderator")
27 assert_response :forbidden
29 # Login as an unprivileged user
30 session_for(normal_user)
32 # Granting should still fail
33 post user_role_path(target_user, "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 user_role_path("non_existent_user", 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 user_role_path(super_user, 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 user_role_path(target_user, 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 user_role_path(target_user, 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 user_role_path(target_user, "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 role 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 delete user_role_path(target_user, "moderator")
87 assert_response :forbidden
89 # Login as an unprivileged user
90 session_for(normal_user)
92 # Revoking should still fail
93 delete user_role_path(target_user, "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 delete user_role_path("non_existent_user", 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 delete user_role_path(target_user, 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 delete user_role_path(super_user, 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 delete user_role_path(super_user, 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 delete user_role_path(target_user, "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 delete user_role_path(administrator_user, "administrator")
138 assert_redirected_to user_path(administrator_user)
139 assert_equal "Cannot revoke administrator role from current user.", flash[:error]