1 require File.dirname(__FILE__) + '/../test_helper'
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
28 cookies["_osm_username"] = users(:public_user).display_name
30 # Granting should still fail
31 post :grant, :display_name => users(:normal_user).display_name, :role => "moderator"
32 assert_redirected_to user_path(users(:normal_user).display_name)
33 assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error]
35 # Login as an administrator
36 session[:user] = users(:administrator_user).id
37 cookies["_osm_username"] = users(:administrator_user).display_name
39 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 "h2", "The user non_existent_user does not exist"
49 # Granting a role from a user that already has it should fail
50 assert_no_difference "UserRole.count" do
51 post :grant, :display_name => users(:super_user).display_name, :role => role
53 assert_redirected_to user_path(users(: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 => users(:normal_user).display_name, :role => role
60 assert_redirected_to user_path(users(:normal_user).display_name)
62 # ...but trying a second time should fail
63 assert_no_difference "UserRole.count" do
64 post :grant, :display_name => users(:normal_user).display_name, :role => role
66 assert_redirected_to user_path(users(:normal_user).display_name)
67 assert_equal "The user already has role #{role}.", flash[:error]
71 # Granting a non-existent role should fail
72 assert_difference "UserRole.count", 0 do
73 post :grant, :display_name => users(:normal_user).display_name, :role => "no_such_role"
75 assert_redirected_to user_path(users(:normal_user).display_name)
76 assert_equal "The string `no_such_role' is not a valid role.", flash[:error]
80 # test the revoke action
82 # Revoking should fail when not logged in
83 post :revoke, :display_name => users(:normal_user).display_name, :role => "moderator"
84 assert_response :forbidden
86 # Login as an unprivileged user
87 session[:user] = users(:public_user).id
88 cookies["_osm_username"] = users(:public_user).display_name
90 # Revoking should still fail
91 post :revoke, :display_name => users(:normal_user).display_name, :role => "moderator"
92 assert_redirected_to user_path(users(:normal_user).display_name)
93 assert_equal "Only administrators can perform user role management, and you are not an administrator.", flash[:error]
95 # Login as an administrator
96 session[:user] = users(:administrator_user).id
97 cookies["_osm_username"] = users(:administrator_user).display_name
99 UserRole::ALL_ROLES.each do |role|
101 # Removing a role from a non-existent user should fail
102 assert_difference "UserRole.count", 0 do
103 post :revoke, :display_name => "non_existent_user", :role => role
105 assert_response :not_found
106 assert_template "user/no_such_user"
107 assert_select "h2", "The user non_existent_user does not exist"
109 # Removing a role from a user that doesn't have it should fail
110 assert_no_difference "UserRole.count" do
111 post :revoke, :display_name => users(:normal_user).display_name, :role => role
113 assert_redirected_to user_path(users(:normal_user).display_name)
114 assert_equal "The user does not have role #{role}.", flash[:error]
116 # Removing a role' from a user that has it should work...
117 assert_difference "UserRole.count", -1 do
118 post :revoke, :display_name => users(:super_user).display_name, :role => role
120 assert_redirected_to user_path(users(:super_user).display_name)
122 # ...but trying a second time should fail
123 assert_no_difference "UserRole.count" do
124 post :revoke, :display_name => users(:super_user).display_name, :role => role
126 assert_redirected_to user_path(users(:super_user).display_name)
127 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 => users(:normal_user).display_name, :role => "no_such_role"
135 assert_redirected_to user_path(users(:normal_user).display_name)
136 assert_equal "The string `no_such_role' is not a valid role.", flash[:error]