3 class UserBlocksControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/blocks/new/username", :method => :get },
9 { :controller => "user_blocks", :action => "new", :display_name => "username" }
13 { :path => "/user_blocks", :method => :get },
14 { :controller => "user_blocks", :action => "index" }
17 { :path => "/user_blocks/new", :method => :get },
18 { :controller => "user_blocks", :action => "new" }
21 { :path => "/user_blocks", :method => :post },
22 { :controller => "user_blocks", :action => "create" }
25 { :path => "/user_blocks/1", :method => :get },
26 { :controller => "user_blocks", :action => "show", :id => "1" }
29 { :path => "/user_blocks/1/edit", :method => :get },
30 { :controller => "user_blocks", :action => "edit", :id => "1" }
33 { :path => "/user_blocks/1", :method => :put },
34 { :controller => "user_blocks", :action => "update", :id => "1" }
37 { :path => "/user_blocks/1", :method => :delete },
38 { :controller => "user_blocks", :action => "destroy", :id => "1" }
41 { :path => "/blocks/1/revoke", :method => :get },
42 { :controller => "user_blocks", :action => "revoke", :id => "1" }
45 { :path => "/blocks/1/revoke", :method => :post },
46 { :controller => "user_blocks", :action => "revoke", :id => "1" }
50 { :path => "/user/username/blocks", :method => :get },
51 { :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
54 { :path => "/user/username/blocks_by", :method => :get },
55 { :controller => "user_blocks", :action => "blocks_by", :display_name => "username" }
60 # test the index action
62 active_block = create(:user_block)
63 expired_block = create(:user_block, :expired)
64 revoked_block = create(:user_block, :revoked)
67 assert_response :success
68 assert_select "table#block_list", :count => 1 do
70 assert_select "a[href='#{user_block_path(active_block)}']", 1
71 assert_select "a[href='#{user_block_path(expired_block)}']", 1
72 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
77 # test the index action with multiple pages
79 create_list(:user_block, 50)
82 assert_response :success
83 assert_select "table#block_list", :count => 1 do
84 assert_select "tr", :count => 21
87 get user_blocks_path(:page => 2)
88 assert_response :success
89 assert_select "table#block_list", :count => 1 do
90 assert_select "tr", :count => 21
95 # test the show action
97 active_block = create(:user_block, :needs_view)
98 expired_block = create(:user_block, :expired)
99 revoked_block = create(:user_block, :revoked)
101 # Viewing a block should fail when a bogus ID is given
102 get user_block_path(:id => 99999)
103 assert_response :not_found
104 assert_template "not_found"
105 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
107 # Viewing an expired block should work
108 get user_block_path(:id => expired_block)
109 assert_response :success
111 # Viewing a revoked block should work
112 get user_block_path(:id => revoked_block)
113 assert_response :success
115 # Viewing an active block should work, but shouldn't mark it as seen
116 get user_block_path(:id => active_block)
117 assert_response :success
118 assert UserBlock.find(active_block.id).needs_view
120 # Login as the blocked user
121 session_for(active_block.user)
123 # Now viewing it should mark it as seen
124 get user_block_path(:id => active_block)
125 assert_response :success
126 assert_not UserBlock.find(active_block.id).needs_view
130 # test the new action
132 target_user = create(:user)
134 # Check that the block creation page requires us to login
135 get new_user_block_path(:display_name => target_user.display_name)
136 assert_redirected_to login_path(:referer => new_user_block_path(:display_name => target_user.display_name))
138 # Login as a normal user
139 session_for(create(:user))
141 # Check that normal users can't load the block creation page
142 get new_user_block_path(:display_name => target_user.display_name)
143 assert_response :redirect
144 assert_redirected_to :controller => "errors", :action => "forbidden"
146 # Login as a moderator
147 session_for(create(:moderator_user))
149 # Check that the block creation page loads for moderators
150 get new_user_block_path(:display_name => target_user.display_name)
151 assert_response :success
152 assert_select "form#new_user_block", :count => 1 do
153 assert_select "textarea#user_block_reason", :count => 1
154 assert_select "select#user_block_period", :count => 1
155 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
156 assert_select "input#display_name[type='hidden']", :count => 1
157 assert_select "input[type='submit'][value='Create block']", :count => 1
160 # We should get an error if the user doesn't exist
161 get new_user_block_path(:display_name => "non_existent_user")
162 assert_response :not_found
163 assert_template "users/no_such_user"
164 assert_select "h1", "The user non_existent_user does not exist"
168 # test the edit action
170 active_block = create(:user_block)
172 # Check that the block edit page requires us to login
173 get edit_user_block_path(:id => active_block)
174 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
176 # Login as a normal user
177 session_for(create(:user))
179 # Check that normal users can't load the block edit page
180 get edit_user_block_path(:id => active_block)
181 assert_response :redirect
182 assert_redirected_to :controller => "errors", :action => "forbidden"
184 # Login as a moderator
185 session_for(create(:moderator_user))
187 # Check that the block edit page loads for moderators
188 get edit_user_block_path(:id => active_block)
189 assert_response :success
190 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
191 assert_select "textarea#user_block_reason", :count => 1
192 assert_select "select#user_block_period", :count => 1
193 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
194 assert_select "input[type='submit'][value='Update block']", :count => 1
197 # We should get an error if the user doesn't exist
198 get edit_user_block_path(:id => 99999)
199 assert_response :not_found
200 assert_template "not_found"
201 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
205 # test the create action
207 target_user = create(:user)
208 moderator_user = create(:moderator_user)
210 # Not logged in yet, so creating a block should fail
211 post user_blocks_path
212 assert_response :forbidden
214 # Login as a normal user
215 session_for(create(:user))
217 # Check that normal users can't create blocks
218 post user_blocks_path
219 assert_response :redirect
220 assert_redirected_to :controller => "errors", :action => "forbidden"
222 # Login as a moderator
223 session_for(moderator_user)
225 # A bogus block period should result in an error
226 assert_no_difference "UserBlock.count" do
227 post user_blocks_path(:display_name => target_user.display_name,
228 :user_block_period => "99")
230 assert_redirected_to new_user_block_path(:display_name => target_user.display_name)
231 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
233 # Check that creating a block works
234 assert_difference "UserBlock.count", 1 do
235 post user_blocks_path(:display_name => target_user.display_name,
236 :user_block_period => "12",
237 :user_block => { :needs_view => false, :reason => "Vandalism" })
239 id = UserBlock.order(:id).ids.last
240 assert_redirected_to user_block_path(:id => id)
241 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
242 b = UserBlock.find(id)
243 assert_in_delta Time.now.utc, b.created_at, 1
244 assert_in_delta Time.now.utc, b.updated_at, 1
245 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
246 assert_not b.needs_view
247 assert_equal "Vandalism", b.reason
248 assert_equal "markdown", b.reason_format
249 assert_equal moderator_user.id, b.creator_id
251 # We should get an error if no user is specified
252 post user_blocks_path
253 assert_response :not_found
254 assert_template "users/no_such_user"
255 assert_select "h1", "The user does not exist"
257 # We should get an error if the user doesn't exist
258 post user_blocks_path(:display_name => "non_existent_user")
259 assert_response :not_found
260 assert_template "users/no_such_user"
261 assert_select "h1", "The user non_existent_user does not exist"
265 # test the update action
267 moderator_user = create(:moderator_user)
268 second_moderator_user = create(:moderator_user)
269 active_block = create(:user_block, :creator => moderator_user)
271 # Not logged in yet, so updating a block should fail
272 put user_block_path(:id => active_block)
273 assert_response :forbidden
275 # Login as a normal user
276 session_for(create(:user))
278 # Check that normal users can't update blocks
279 put user_block_path(:id => active_block)
280 assert_response :redirect
281 assert_redirected_to :controller => "errors", :action => "forbidden"
283 # Login as the wrong moderator
284 session_for(second_moderator_user)
286 # Check that only the person who created a block can update it
287 assert_no_difference "UserBlock.count" do
288 put user_block_path(:id => active_block,
289 :user_block_period => "12",
290 :user_block => { :needs_view => true, :reason => "Vandalism" })
292 assert_redirected_to edit_user_block_path(active_block)
293 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
295 # Login as the correct moderator
296 session_for(moderator_user)
298 # A bogus block period should result in an error
299 assert_no_difference "UserBlock.count" do
300 put user_block_path(:id => active_block, :user_block_period => "99")
302 assert_redirected_to edit_user_block_path(active_block)
303 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
305 # Check that updating a block works
306 assert_no_difference "UserBlock.count" do
307 put user_block_path(:id => active_block,
308 :user_block_period => "12",
309 :user_block => { :needs_view => true, :reason => "Vandalism" })
311 assert_redirected_to user_block_path(active_block)
312 assert_equal "Block updated.", flash[:notice]
313 b = UserBlock.find(active_block.id)
314 assert_in_delta Time.now.utc, b.updated_at, 1
316 assert_equal "Vandalism", b.reason
318 # We should get an error if the block doesn't exist
319 put user_block_path(:id => 99999)
320 assert_response :not_found
321 assert_template "not_found"
322 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
326 # test the revoke action
328 active_block = create(:user_block)
330 # Check that the block revoke page requires us to login
331 get revoke_user_block_path(:id => active_block)
332 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
334 # Login as a normal user
335 session_for(create(:user))
337 # Check that normal users can't load the block revoke page
338 get revoke_user_block_path(:id => active_block)
339 assert_response :redirect
340 assert_redirected_to :controller => "errors", :action => "forbidden"
342 # Login as a moderator
343 session_for(create(:moderator_user))
345 # Check that the block revoke page loads for moderators
346 get revoke_user_block_path(:id => active_block)
347 assert_response :success
348 assert_template "revoke"
349 assert_select "form", :count => 1 do
350 assert_select "input#confirm[type='checkbox']", :count => 1
351 assert_select "input[type='submit'][value='Revoke!']", :count => 1
354 # Check that revoking a block using GET should fail
355 get revoke_user_block_path(:id => active_block, :confirm => true)
356 assert_response :success
357 assert_template "revoke"
358 b = UserBlock.find(active_block.id)
359 assert b.ends_at - Time.now.utc > 100
361 # Check that revoking a block works using POST
362 post revoke_user_block_path(:id => active_block, :confirm => true)
363 assert_redirected_to user_block_path(active_block)
364 b = UserBlock.find(active_block.id)
365 assert_in_delta Time.now.utc, b.ends_at, 1
367 # We should get an error if the block doesn't exist
368 get revoke_user_block_path(:id => 99999)
369 assert_response :not_found
370 assert_template "not_found"
371 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
375 # test the blocks_on action
377 blocked_user = create(:user)
378 unblocked_user = create(:user)
379 normal_user = create(:user)
380 active_block = create(:user_block, :user => blocked_user)
381 revoked_block = create(:user_block, :revoked, :user => blocked_user)
382 expired_block = create(:user_block, :expired, :user => unblocked_user)
384 # Asking for a list of blocks with a bogus user name should fail
385 get user_blocks_on_path(:display_name => "non_existent_user")
386 assert_response :not_found
387 assert_template "users/no_such_user"
388 assert_select "h1", "The user non_existent_user does not exist"
390 # Check the list of blocks for a user that has never been blocked
391 get user_blocks_on_path(:display_name => normal_user.display_name)
392 assert_response :success
393 assert_select "table#block_list", false
394 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
396 # Check the list of blocks for a user that is currently blocked
397 get user_blocks_on_path(:display_name => blocked_user.display_name)
398 assert_response :success
399 assert_select "table#block_list", :count => 1 do
400 assert_select "tr", 3
401 assert_select "a[href='#{user_block_path(active_block)}']", 1
402 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
405 # Check the list of blocks for a user that has previously been blocked
406 get user_blocks_on_path(:display_name => unblocked_user.display_name)
407 assert_response :success
408 assert_select "table#block_list", :count => 1 do
409 assert_select "tr", 2
410 assert_select "a[href='#{user_block_path(expired_block)}']", 1
415 # test the blocks_on action with multiple pages
416 def test_blocks_on_paged
418 create_list(:user_block, 50, :user => user)
420 get user_blocks_on_path(:display_name => user.display_name)
421 assert_response :success
422 assert_select "table#block_list", :count => 1 do
423 assert_select "tr", :count => 21
426 get user_blocks_on_path(:display_name => user.display_name, :page => 2)
427 assert_response :success
428 assert_select "table#block_list", :count => 1 do
429 assert_select "tr", :count => 21
434 # test the blocks_by action
436 moderator_user = create(:moderator_user)
437 second_moderator_user = create(:moderator_user)
438 normal_user = create(:user)
439 active_block = create(:user_block, :creator => moderator_user)
440 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
441 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
443 # Asking for a list of blocks with a bogus user name should fail
444 get user_blocks_by_path(:display_name => "non_existent_user")
445 assert_response :not_found
446 assert_template "users/no_such_user"
447 assert_select "h1", "The user non_existent_user does not exist"
449 # Check the list of blocks given by one moderator
450 get user_blocks_by_path(:display_name => moderator_user.display_name)
451 assert_response :success
452 assert_select "table#block_list", :count => 1 do
453 assert_select "tr", 2
454 assert_select "a[href='#{user_block_path(active_block)}']", 1
457 # Check the list of blocks given by a different moderator
458 get user_blocks_by_path(:display_name => second_moderator_user.display_name)
459 assert_response :success
460 assert_select "table#block_list", :count => 1 do
461 assert_select "tr", 3
462 assert_select "a[href='#{user_block_path(expired_block)}']", 1
463 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
466 # Check the list of blocks (not) given by a normal user
467 get user_blocks_by_path(:display_name => normal_user.display_name)
468 assert_response :success
469 assert_select "table#block_list", false
470 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
474 # test the blocks_by action with multiple pages
475 def test_blocks_by_paged
476 user = create(:moderator_user)
477 create_list(:user_block, 50, :creator => user)
479 get user_blocks_by_path(:display_name => user.display_name)
480 assert_response :success
481 assert_select "table#block_list", :count => 1 do
482 assert_select "tr", :count => 21
485 get user_blocks_by_path(:display_name => user.display_name, :page => 2)
486 assert_response :success
487 assert_select "table#block_list", :count => 1 do
488 assert_select "tr", :count => 21