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 # Create a super user which has all known roles
24 super_user = create(:user)
25 UserRole::ALL_ROLES.each do |role|
26 create(:user_role, :user => super_user, :granter => administrator_user, :role => role)
29 # Granting should fail when not logged in
30 post :grant, :display_name => target_user.display_name, :role => "moderator"
31 assert_response :forbidden
33 # Login as an unprivileged user
34 session[:user] = normal_user.id
36 # Granting should still fail
37 post :grant, :display_name => target_user.display_name, :role => "moderator"
38 assert_redirected_to user_path(target_user.display_name)
39 assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error]
41 # Login as an administrator
42 session[:user] = administrator_user.id
44 UserRole::ALL_ROLES.each do |role|
45 # Granting a role to a non-existent user should fail
46 assert_difference "UserRole.count", 0 do
47 post :grant, :display_name => "non_existent_user", :role => role
49 assert_response :not_found
50 assert_template "user/no_such_user"
51 assert_select "h1", "The user non_existent_user does not exist"
53 # Granting a role to a user that already has it should fail
54 assert_no_difference "UserRole.count" do
55 post :grant, :display_name => super_user.display_name, :role => role
57 assert_redirected_to user_path(super_user.display_name)
58 assert_equal "The user already has role #{role}.", flash[:error]
60 # Granting a role to a user that doesn't have it should work...
61 assert_difference "UserRole.count", 1 do
62 post :grant, :display_name => target_user.display_name, :role => role
64 assert_redirected_to user_path(target_user.display_name)
66 # ...but trying a second time should fail
67 assert_no_difference "UserRole.count" do
68 post :grant, :display_name => target_user.display_name, :role => role
70 assert_redirected_to user_path(target_user.display_name)
71 assert_equal "The user already has role #{role}.", flash[:error]
74 # Granting a non-existent role should fail
75 assert_difference "UserRole.count", 0 do
76 post :grant, :display_name => target_user.display_name, :role => "no_such_role"
78 assert_redirected_to user_path(target_user.display_name)
79 assert_equal "The string `no_such_role' is not a valid role.", flash[:error]
83 # test the revoke action
85 target_user = create(:user)
86 normal_user = create(:user)
87 administrator_user = create(:administrator_user)
88 # Create a super user which has all known roles
89 super_user = create(:user)
90 UserRole::ALL_ROLES.each do |role|
91 create(:user_role, :user => super_user, :granter => administrator_user, :role => role)
94 # Revoking should fail when not logged in
95 post :revoke, :display_name => target_user.display_name, :role => "moderator"
96 assert_response :forbidden
98 # Login as an unprivileged user
99 session[:user] = normal_user.id
101 # Revoking should still fail
102 post :revoke, :display_name => target_user.display_name, :role => "moderator"
103 assert_redirected_to user_path(target_user.display_name)
104 assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error]
106 # Login as an administrator
107 session[:user] = administrator_user.id
109 UserRole::ALL_ROLES.each do |role|
110 # Removing a role from a non-existent user should fail
111 assert_difference "UserRole.count", 0 do
112 post :revoke, :display_name => "non_existent_user", :role => role
114 assert_response :not_found
115 assert_template "user/no_such_user"
116 assert_select "h1", "The user non_existent_user does not exist"
118 # Removing a role from a user that doesn't have it should fail
119 assert_no_difference "UserRole.count" do
120 post :revoke, :display_name => target_user.display_name, :role => role
122 assert_redirected_to user_path(target_user.display_name)
123 assert_equal "The user does not have role #{role}.", flash[:error]
125 # Removing a role from a user that has it should work...
126 assert_difference "UserRole.count", -1 do
127 post :revoke, :display_name => super_user.display_name, :role => role
129 assert_redirected_to user_path(super_user.display_name)
131 # ...but trying a second time should fail
132 assert_no_difference "UserRole.count" do
133 post :revoke, :display_name => super_user.display_name, :role => role
135 assert_redirected_to user_path(super_user.display_name)
136 assert_equal "The user does not have role #{role}.", flash[:error]
139 # Revoking a non-existent role should fail
140 assert_difference "UserRole.count", 0 do
141 post :revoke, :display_name => target_user.display_name, :role => "no_such_role"
143 assert_redirected_to user_path(target_user.display_name)
144 assert_equal "The string `no_such_role' is not a valid role.", flash[:error]