3 class UserRolesControllerTest < ActionController::TestCase
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, :display_name => target_user.display_name, :role => "moderator"
27 assert_response :forbidden
29 # Login as an unprivileged user
30 session[:user] = normal_user.id
32 # Granting should still fail
33 post :grant, :display_name => target_user.display_name, :role => "moderator"
34 assert_redirected_to user_path(target_user.display_name)
35 assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error]
37 # Login as an administrator
38 session[:user] = administrator_user.id
40 UserRole::ALL_ROLES.each do |role|
41 # Granting a role to a non-existent user should fail
42 assert_difference "UserRole.count", 0 do
43 post :grant, :display_name => "non_existent_user", :role => role
45 assert_response :not_found
46 assert_template "user/no_such_user"
47 assert_select "h1", "The user non_existent_user does not exist"
49 # Granting a role to a user that already has it should fail
50 assert_no_difference "UserRole.count" do
51 post :grant, :display_name => super_user.display_name, :role => role
53 assert_redirected_to user_path(super_user.display_name)
54 assert_equal "The user already has role #{role}.", flash[:error]
56 # Granting a role to a user that doesn't have it should work...
57 assert_difference "UserRole.count", 1 do
58 post :grant, :display_name => target_user.display_name, :role => role
60 assert_redirected_to user_path(target_user.display_name)
62 # ...but trying a second time should fail
63 assert_no_difference "UserRole.count" do
64 post :grant, :display_name => target_user.display_name, :role => role
66 assert_redirected_to user_path(target_user.display_name)
67 assert_equal "The user already has role #{role}.", flash[:error]
70 # Granting a non-existent role should fail
71 assert_difference "UserRole.count", 0 do
72 post :grant, :display_name => target_user.display_name, :role => "no_such_role"
74 assert_redirected_to user_path(target_user.display_name)
75 assert_equal "The string `no_such_role' is not a valid role.", flash[:error]
79 # test the revoke action
81 target_user = create(:user)
82 normal_user = create(:user)
83 administrator_user = create(:administrator_user)
84 super_user = create(:super_user)
86 # Revoking should fail when not logged in
87 post :revoke, :display_name => target_user.display_name, :role => "moderator"
88 assert_response :forbidden
90 # Login as an unprivileged user
91 session[:user] = normal_user.id
93 # Revoking should still fail
94 post :revoke, :display_name => target_user.display_name, :role => "moderator"
95 assert_redirected_to user_path(target_user.display_name)
96 assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error]
98 # Login as an administrator
99 session[:user] = administrator_user.id
101 UserRole::ALL_ROLES.each do |role|
102 # Removing a role from a non-existent user should fail
103 assert_difference "UserRole.count", 0 do
104 post :revoke, :display_name => "non_existent_user", :role => role
106 assert_response :not_found
107 assert_template "user/no_such_user"
108 assert_select "h1", "The user non_existent_user does not exist"
110 # Removing a role from a user that doesn't have it should fail
111 assert_no_difference "UserRole.count" do
112 post :revoke, :display_name => target_user.display_name, :role => role
114 assert_redirected_to user_path(target_user.display_name)
115 assert_equal "The user does not have role #{role}.", flash[:error]
117 # Removing a role from a user that has it should work...
118 assert_difference "UserRole.count", -1 do
119 post :revoke, :display_name => super_user.display_name, :role => role
121 assert_redirected_to user_path(super_user.display_name)
123 # ...but trying a second time should fail
124 assert_no_difference "UserRole.count" do
125 post :revoke, :display_name => super_user.display_name, :role => role
127 assert_redirected_to user_path(super_user.display_name)
128 assert_equal "The user does not have role #{role}.", flash[:error]
131 # Revoking a non-existent role should fail
132 assert_difference "UserRole.count", 0 do
133 post :revoke, :display_name => target_user.display_name, :role => "no_such_role"
135 assert_redirected_to user_path(target_user.display_name)
136 assert_equal "The string `no_such_role' is not a valid role.", flash[:error]