3 class UserRolesControllerTest < ActionController::TestCase
4 fixtures :users, :user_roles
7 # test all routes which lead to this controller
10 { :path => "/user/username/role/rolename/grant", :method => :post },
11 { :controller => "user_roles", :action => "grant", :display_name => "username", :role => "rolename" }
14 { :path => "/user/username/role/rolename/revoke", :method => :post },
15 { :controller => "user_roles", :action => "revoke", :display_name => "username", :role => "rolename" }
20 # test the grant action
22 # Granting should fail when not logged in
23 post :grant, :display_name => users(:normal_user).display_name, :role => "moderator"
24 assert_response :forbidden
26 # Login as an unprivileged user
27 session[:user] = users(:public_user).id
29 # Granting should still fail
30 post :grant, :display_name => users(:normal_user).display_name, :role => "moderator"
31 assert_redirected_to user_path(users(:normal_user).display_name)
32 assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error]
34 # Login as an administrator
35 session[:user] = users(:administrator_user).id
37 UserRole::ALL_ROLES.each do |role|
38 # Granting a role to a non-existent user should fail
39 assert_difference "UserRole.count", 0 do
40 post :grant, :display_name => "non_existent_user", :role => role
42 assert_response :not_found
43 assert_template "user/no_such_user"
44 assert_select "h1", "The user non_existent_user does not exist"
46 # Granting a role from a user that already has it should fail
47 assert_no_difference "UserRole.count" do
48 post :grant, :display_name => users(:super_user).display_name, :role => role
50 assert_redirected_to user_path(users(:super_user).display_name)
51 assert_equal "The user already has role #{role}.", flash[:error]
53 # Granting a role to a user that doesn't have it should work...
54 assert_difference "UserRole.count", 1 do
55 post :grant, :display_name => users(:normal_user).display_name, :role => role
57 assert_redirected_to user_path(users(:normal_user).display_name)
59 # ...but trying a second time should fail
60 assert_no_difference "UserRole.count" do
61 post :grant, :display_name => users(:normal_user).display_name, :role => role
63 assert_redirected_to user_path(users(:normal_user).display_name)
64 assert_equal "The user already has role #{role}.", flash[:error]
67 # Granting a non-existent role should fail
68 assert_difference "UserRole.count", 0 do
69 post :grant, :display_name => users(:normal_user).display_name, :role => "no_such_role"
71 assert_redirected_to user_path(users(:normal_user).display_name)
72 assert_equal "The string `no_such_role' is not a valid role.", flash[:error]
76 # test the revoke action
78 # Revoking should fail when not logged in
79 post :revoke, :display_name => users(:normal_user).display_name, :role => "moderator"
80 assert_response :forbidden
82 # Login as an unprivileged user
83 session[:user] = users(:public_user).id
85 # Revoking should still fail
86 post :revoke, :display_name => users(:normal_user).display_name, :role => "moderator"
87 assert_redirected_to user_path(users(:normal_user).display_name)
88 assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error]
90 # Login as an administrator
91 session[:user] = users(:administrator_user).id
93 UserRole::ALL_ROLES.each do |role|
94 # Removing a role from a non-existent user should fail
95 assert_difference "UserRole.count", 0 do
96 post :revoke, :display_name => "non_existent_user", :role => role
98 assert_response :not_found
99 assert_template "user/no_such_user"
100 assert_select "h1", "The user non_existent_user does not exist"
102 # Removing a role from a user that doesn't have it should fail
103 assert_no_difference "UserRole.count" do
104 post :revoke, :display_name => users(:normal_user).display_name, :role => role
106 assert_redirected_to user_path(users(:normal_user).display_name)
107 assert_equal "The user does not have role #{role}.", flash[:error]
109 # Removing a role' from a user that has it should work...
110 assert_difference "UserRole.count", -1 do
111 post :revoke, :display_name => users(:super_user).display_name, :role => role
113 assert_redirected_to user_path(users(:super_user).display_name)
115 # ...but trying a second time should fail
116 assert_no_difference "UserRole.count" do
117 post :revoke, :display_name => users(:super_user).display_name, :role => role
119 assert_redirected_to user_path(users(:super_user).display_name)
120 assert_equal "The user does not have role #{role}.", flash[:error]
123 # Revoking a non-existent role should fail
124 assert_difference "UserRole.count", 0 do
125 post :revoke, :display_name => users(:normal_user).display_name, :role => "no_such_role"
127 assert_redirected_to user_path(users(:normal_user).display_name)
128 assert_equal "The string `no_such_role' is not a valid role.", flash[:error]