4 class ListsControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/users", :method => :get },
10 { :controller => "users/lists", :action => "show" }
13 { :path => "/users", :method => :put },
14 { :controller => "users/lists", :action => "update" }
17 { :path => "/users/status", :method => :get },
18 { :controller => "users/lists", :action => "show", :status => "status" }
21 { :path => "/users/status", :method => :put },
22 { :controller => "users/lists", :action => "update", :status => "status" }
28 moderator_user = create(:moderator_user)
29 administrator_user = create(:administrator_user)
30 _suspended_user = create(:user, :suspended)
31 _ip_user = create(:user, :creation_address => "1.2.3.4")
33 # There are now 7 users - the five above, plus two extra "granters" for the
34 # moderator_user and administrator_user
35 assert_equal 7, User.count
37 # Shouldn't work when not logged in
39 assert_redirected_to login_path(:referer => users_list_path)
43 # Shouldn't work when logged in as a normal user
45 assert_redirected_to :controller => "/errors", :action => :forbidden
47 session_for(moderator_user)
49 # Shouldn't work when logged in as a moderator
51 assert_redirected_to :controller => "/errors", :action => :forbidden
53 session_for(administrator_user)
55 # Note there is a header row, so all row counts are users + 1
56 # Should work when logged in as an administrator
58 assert_response :success
60 assert_select "table#user_list tbody tr", :count => 7
62 # Should be able to limit by status
63 get users_list_path, :params => { :status => "suspended" }
64 assert_response :success
66 assert_select "table#user_list tbody tr", :count => 1
68 # Should be able to limit by IP address
69 get users_list_path, :params => { :ip => "1.2.3.4" }
70 assert_response :success
72 assert_select "table#user_list tbody tr", :count => 1
75 def test_show_paginated
76 1.upto(100).each do |n|
77 User.create(:display_name => "extra_#{n}",
78 :email => "extra#{n}@example.com",
79 :pass_crypt => "extraextra")
82 session_for(create(:administrator_user))
84 # 100 examples, an administrator, and a granter for the admin.
85 assert_equal 102, User.count
86 next_path = users_list_path
89 assert_response :success
91 assert_select "table#user_list tbody tr", :count => 50
92 check_no_page_link "Newer Users"
93 next_path = check_page_link "Older Users"
96 assert_response :success
98 assert_select "table#user_list tbody tr", :count => 50
99 check_page_link "Newer Users"
100 next_path = check_page_link "Older Users"
103 assert_response :success
104 assert_template :show
105 assert_select "table#user_list tbody tr", :count => 2
106 check_page_link "Newer Users"
107 check_no_page_link "Older Users"
110 def test_show_invalid_paginated
111 session_for(create(:administrator_user))
113 %w[-1 0 fred].each do |id|
114 get users_list_path(:before => id)
115 assert_redirected_to :controller => "/errors", :action => :bad_request
117 get users_list_path(:after => id)
118 assert_redirected_to :controller => "/errors", :action => :bad_request
122 def test_update_confirm
123 inactive_user = create(:user, :pending)
124 suspended_user = create(:user, :suspended)
126 # Shouldn't work when not logged in
127 assert_no_difference "User.active.count" do
128 put users_list_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
130 assert_response :forbidden
132 assert_equal "pending", inactive_user.reload.status
133 assert_equal "suspended", suspended_user.reload.status
135 session_for(create(:user))
137 # Shouldn't work when logged in as a normal user
138 assert_no_difference "User.active.count" do
139 put users_list_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
141 assert_redirected_to :controller => "/errors", :action => :forbidden
142 assert_equal "pending", inactive_user.reload.status
143 assert_equal "suspended", suspended_user.reload.status
145 session_for(create(:moderator_user))
147 # Shouldn't work when logged in as a moderator
148 assert_no_difference "User.active.count" do
149 put users_list_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
151 assert_redirected_to :controller => "/errors", :action => :forbidden
152 assert_equal "pending", inactive_user.reload.status
153 assert_equal "suspended", suspended_user.reload.status
155 session_for(create(:administrator_user))
157 # Should work when logged in as an administrator
158 assert_difference "User.active.count", 2 do
159 put users_list_path, :params => { :confirm => 1, :user => { inactive_user.id => 1, suspended_user.id => 1 } }
161 assert_redirected_to :action => :show
162 assert_equal "confirmed", inactive_user.reload.status
163 assert_equal "confirmed", suspended_user.reload.status
167 normal_user = create(:user)
168 confirmed_user = create(:user, :confirmed)
170 # Shouldn't work when not logged in
171 assert_no_difference "User.active.count" do
172 put users_list_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
174 assert_response :forbidden
176 assert_equal "active", normal_user.reload.status
177 assert_equal "confirmed", confirmed_user.reload.status
179 session_for(create(:user))
181 # Shouldn't work when logged in as a normal user
182 assert_no_difference "User.active.count" do
183 put users_list_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
185 assert_redirected_to :controller => "/errors", :action => :forbidden
186 assert_equal "active", normal_user.reload.status
187 assert_equal "confirmed", confirmed_user.reload.status
189 session_for(create(:moderator_user))
191 # Shouldn't work when logged in as a moderator
192 assert_no_difference "User.active.count" do
193 put users_list_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
195 assert_redirected_to :controller => "/errors", :action => :forbidden
196 assert_equal "active", normal_user.reload.status
197 assert_equal "confirmed", confirmed_user.reload.status
199 session_for(create(:administrator_user))
201 # Should work when logged in as an administrator
202 assert_difference "User.active.count", -2 do
203 put users_list_path, :params => { :hide => 1, :user => { normal_user.id => 1, confirmed_user.id => 1 } }
205 assert_redirected_to :action => :show
206 assert_equal "deleted", normal_user.reload.status
207 assert_equal "deleted", confirmed_user.reload.status
212 def check_no_page_link(name)
213 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
216 def check_page_link(name)
217 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
218 return buttons.first.attributes["href"].value