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" }
58 { :path => "/user/username/blocks/revoke_all", :method => :get },
59 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
62 { :path => "/user/username/blocks/revoke_all", :method => :post },
63 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
68 # test the index action
70 active_block = create(:user_block)
71 expired_block = create(:user_block, :expired)
72 revoked_block = create(:user_block, :revoked)
75 assert_response :success
76 assert_select "table#block_list", :count => 1 do
78 assert_select "a[href='#{user_block_path(active_block)}']", 1
79 assert_select "a[href='#{user_block_path(expired_block)}']", 1
80 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
85 # test the index action with multiple pages
87 create_list(:user_block, 50)
90 assert_response :success
91 assert_select "table#block_list", :count => 1 do
92 assert_select "tr", :count => 21
95 get user_blocks_path(:page => 2)
96 assert_response :success
97 assert_select "table#block_list", :count => 1 do
98 assert_select "tr", :count => 21
103 # test the show action
105 active_block = create(:user_block, :needs_view)
106 expired_block = create(:user_block, :expired)
107 revoked_block = create(:user_block, :revoked)
109 # Viewing a block should fail when a bogus ID is given
110 get user_block_path(:id => 99999)
111 assert_response :not_found
112 assert_template "not_found"
113 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
115 # Viewing an expired block should work
116 get user_block_path(:id => expired_block)
117 assert_response :success
119 # Viewing a revoked block should work
120 get user_block_path(:id => revoked_block)
121 assert_response :success
123 # Viewing an active block should work, but shouldn't mark it as seen
124 get user_block_path(:id => active_block)
125 assert_response :success
126 assert UserBlock.find(active_block.id).needs_view
128 # Login as the blocked user
129 session_for(active_block.user)
131 # Now viewing it should mark it as seen
132 get user_block_path(:id => active_block)
133 assert_response :success
134 assert_not UserBlock.find(active_block.id).needs_view
138 # test the new action
140 target_user = create(:user)
142 # Check that the block creation page requires us to login
143 get new_user_block_path(:display_name => target_user.display_name)
144 assert_redirected_to login_path(:referer => new_user_block_path(:display_name => target_user.display_name))
146 # Login as a normal user
147 session_for(create(:user))
149 # Check that normal users can't load the block creation page
150 get new_user_block_path(:display_name => target_user.display_name)
151 assert_response :redirect
152 assert_redirected_to :controller => "errors", :action => "forbidden"
154 # Login as a moderator
155 session_for(create(:moderator_user))
157 # Check that the block creation page loads for moderators
158 get new_user_block_path(:display_name => target_user.display_name)
159 assert_response :success
160 assert_select "form#new_user_block", :count => 1 do
161 assert_select "textarea#user_block_reason", :count => 1
162 assert_select "select#user_block_period", :count => 1
163 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
164 assert_select "input#display_name[type='hidden']", :count => 1
165 assert_select "input[type='submit'][value='Create block']", :count => 1
168 # We should get an error if the user doesn't exist
169 get new_user_block_path(:display_name => "non_existent_user")
170 assert_response :not_found
171 assert_template "users/no_such_user"
172 assert_select "h1", "The user non_existent_user does not exist"
176 # test the edit action
178 active_block = create(:user_block)
180 # Check that the block edit page requires us to login
181 get edit_user_block_path(:id => active_block)
182 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
184 # Login as a normal user
185 session_for(create(:user))
187 # Check that normal users can't load the block edit page
188 get edit_user_block_path(:id => active_block)
189 assert_response :redirect
190 assert_redirected_to :controller => "errors", :action => "forbidden"
192 # Login as a moderator
193 session_for(create(:moderator_user))
195 # Check that the block edit page loads for moderators
196 get edit_user_block_path(:id => active_block)
197 assert_response :success
198 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
199 assert_select "textarea#user_block_reason", :count => 1
200 assert_select "select#user_block_period", :count => 1
201 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
202 assert_select "input[type='submit'][value='Update block']", :count => 1
205 # We should get an error if the user doesn't exist
206 get edit_user_block_path(:id => 99999)
207 assert_response :not_found
208 assert_template "not_found"
209 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
213 # test the create action
215 target_user = create(:user)
216 moderator_user = create(:moderator_user)
218 # Not logged in yet, so creating a block should fail
219 post user_blocks_path
220 assert_response :forbidden
222 # Login as a normal user
223 session_for(create(:user))
225 # Check that normal users can't create blocks
226 post user_blocks_path
227 assert_response :redirect
228 assert_redirected_to :controller => "errors", :action => "forbidden"
230 # Login as a moderator
231 session_for(moderator_user)
233 # A bogus block period should result in an error
234 assert_no_difference "UserBlock.count" do
235 post user_blocks_path(:display_name => target_user.display_name,
236 :user_block_period => "99")
238 assert_redirected_to new_user_block_path(:display_name => target_user.display_name)
239 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
241 # Check that creating a block works
242 assert_difference "UserBlock.count", 1 do
243 post user_blocks_path(:display_name => target_user.display_name,
244 :user_block_period => "12",
245 :user_block => { :needs_view => false, :reason => "Vandalism" })
247 id = UserBlock.order(:id).ids.last
248 assert_redirected_to user_block_path(:id => id)
249 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
250 b = UserBlock.find(id)
251 assert_in_delta Time.now.utc, b.created_at, 1
252 assert_in_delta Time.now.utc, b.updated_at, 1
253 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
254 assert_not b.needs_view
255 assert_equal "Vandalism", b.reason
256 assert_equal "markdown", b.reason_format
257 assert_equal moderator_user.id, b.creator_id
259 # We should get an error if no user is specified
260 post user_blocks_path
261 assert_response :not_found
262 assert_template "users/no_such_user"
263 assert_select "h1", "The user does not exist"
265 # We should get an error if the user doesn't exist
266 post user_blocks_path(:display_name => "non_existent_user")
267 assert_response :not_found
268 assert_template "users/no_such_user"
269 assert_select "h1", "The user non_existent_user does not exist"
273 # test the duration of a created block
274 def test_create_duration
275 target_user = create(:user)
276 moderator_user = create(:moderator_user)
278 session_for(moderator_user)
279 post user_blocks_path(:display_name => target_user.display_name,
280 :user_block_period => "336",
281 :user_block => { :needs_view => false, :reason => "Vandalism" })
283 block = UserBlock.order(:id).last
284 assert_equal 1209600, block.ends_at - block.created_at
288 # test the update action
290 moderator_user = create(:moderator_user)
291 second_moderator_user = create(:moderator_user)
292 active_block = create(:user_block, :creator => moderator_user)
294 # Not logged in yet, so updating a block should fail
295 put user_block_path(:id => active_block)
296 assert_response :forbidden
298 # Login as a normal user
299 session_for(create(:user))
301 # Check that normal users can't update blocks
302 put user_block_path(:id => active_block)
303 assert_response :redirect
304 assert_redirected_to :controller => "errors", :action => "forbidden"
306 # Login as the wrong moderator
307 session_for(second_moderator_user)
309 # Check that only the person who created a block can update it
310 assert_no_difference "UserBlock.count" do
311 put user_block_path(:id => active_block,
312 :user_block_period => "12",
313 :user_block => { :needs_view => true, :reason => "Vandalism" })
315 assert_redirected_to edit_user_block_path(active_block)
316 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
318 # Login as the correct moderator
319 session_for(moderator_user)
321 # A bogus block period should result in an error
322 assert_no_difference "UserBlock.count" do
323 put user_block_path(:id => active_block, :user_block_period => "99")
325 assert_redirected_to edit_user_block_path(active_block)
326 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
328 # Check that updating a block works
329 assert_no_difference "UserBlock.count" do
330 put user_block_path(:id => active_block,
331 :user_block_period => "12",
332 :user_block => { :needs_view => true, :reason => "Vandalism" })
334 assert_redirected_to user_block_path(active_block)
335 assert_equal "Block updated.", flash[:notice]
336 b = UserBlock.find(active_block.id)
337 assert_in_delta Time.now.utc, b.updated_at, 1
339 assert_equal "Vandalism", b.reason
341 # We should get an error if the block doesn't exist
342 put user_block_path(:id => 99999)
343 assert_response :not_found
344 assert_template "not_found"
345 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
349 # test the revoke action
351 active_block = create(:user_block)
353 # Check that the block revoke page requires us to login
354 get revoke_user_block_path(:id => active_block)
355 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
357 # Login as a normal user
358 session_for(create(:user))
360 # Check that normal users can't load the block revoke page
361 get revoke_user_block_path(:id => active_block)
362 assert_response :redirect
363 assert_redirected_to :controller => "errors", :action => "forbidden"
365 # Login as a moderator
366 session_for(create(:moderator_user))
368 # Check that the block revoke page loads for moderators
369 get revoke_user_block_path(:id => active_block)
370 assert_response :success
371 assert_template "revoke"
372 assert_select "form", :count => 1 do
373 assert_select "input#confirm[type='checkbox']", :count => 1
374 assert_select "input[type='submit'][value='Revoke!']", :count => 1
377 # Check that revoking a block using GET should fail
378 get revoke_user_block_path(:id => active_block, :confirm => true)
379 assert_response :success
380 assert_template "revoke"
381 b = UserBlock.find(active_block.id)
382 assert_operator b.ends_at - Time.now.utc, :>, 100
384 # Check that revoking a block works using POST
385 post revoke_user_block_path(:id => active_block, :confirm => true)
386 assert_redirected_to user_block_path(active_block)
387 b = UserBlock.find(active_block.id)
388 assert_in_delta Time.now.utc, b.ends_at, 1
390 # We should get an error if the block doesn't exist
391 get revoke_user_block_path(:id => 99999)
392 assert_response :not_found
393 assert_template "not_found"
394 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
398 # test the revoke all page
399 def test_revoke_all_page
400 blocked_user = create(:user)
401 create(:user_block, :user => blocked_user)
403 # Asking for the revoke all blocks page with a bogus user name should fail
404 get user_blocks_on_path(:display_name => "non_existent_user")
405 assert_response :not_found
407 # Check that the revoke all blocks page requires us to login
408 get revoke_all_user_blocks_path(blocked_user)
409 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
411 # Login as a normal user
412 session_for(create(:user))
414 # Check that normal users can't load the revoke all blocks page
415 get revoke_all_user_blocks_path(blocked_user)
416 assert_response :redirect
417 assert_redirected_to :controller => "errors", :action => "forbidden"
419 # Login as a moderator
420 session_for(create(:moderator_user))
422 # Check that the revoke all blocks page loads for moderators
423 get revoke_all_user_blocks_path(blocked_user)
424 assert_response :success
428 # test the revoke all action
429 def test_revoke_all_action
430 blocked_user = create(:user)
431 active_block1 = create(:user_block, :user => blocked_user)
432 active_block2 = create(:user_block, :user => blocked_user)
433 expired_block1 = create(:user_block, :expired, :user => blocked_user)
434 blocks = [active_block1, active_block2, expired_block1]
435 moderator_user = create(:moderator_user)
437 assert_predicate active_block1, :active?
438 assert_predicate active_block2, :active?
439 assert_not_predicate expired_block1, :active?
441 # Login as a normal user
442 session_for(create(:user))
444 # Check that normal users can't load the block revoke page
445 get revoke_all_user_blocks_path(:blocked_user)
446 assert_response :redirect
447 assert_redirected_to :controller => "errors", :action => "forbidden"
449 # Login as a moderator
450 session_for(moderator_user)
452 # Check that revoking blocks using GET should fail
453 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
454 assert_response :success
455 assert_template "revoke_all"
457 blocks.each(&:reload)
458 assert_predicate active_block1, :active?
459 assert_predicate active_block2, :active?
460 assert_not_predicate expired_block1, :active?
462 # Check that revoking blocks works using POST
463 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
464 assert_redirected_to user_blocks_on_path(blocked_user)
466 blocks.each(&:reload)
467 assert_not_predicate active_block1, :active?
468 assert_not_predicate active_block2, :active?
469 assert_not_predicate expired_block1, :active?
470 assert_equal moderator_user, active_block1.revoker
471 assert_equal moderator_user, active_block2.revoker
472 assert_not_equal moderator_user, expired_block1.revoker
476 # test the blocks_on action
478 blocked_user = create(:user)
479 unblocked_user = create(:user)
480 normal_user = create(:user)
481 active_block = create(:user_block, :user => blocked_user)
482 revoked_block = create(:user_block, :revoked, :user => blocked_user)
483 expired_block = create(:user_block, :expired, :user => unblocked_user)
485 # Asking for a list of blocks with a bogus user name should fail
486 get user_blocks_on_path(:display_name => "non_existent_user")
487 assert_response :not_found
488 assert_template "users/no_such_user"
489 assert_select "h1", "The user non_existent_user does not exist"
491 # Check the list of blocks for a user that has never been blocked
492 get user_blocks_on_path(:display_name => normal_user.display_name)
493 assert_response :success
494 assert_select "table#block_list", false
495 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
497 # Check the list of blocks for a user that is currently blocked
498 get user_blocks_on_path(:display_name => blocked_user.display_name)
499 assert_response :success
500 assert_select "table#block_list", :count => 1 do
501 assert_select "tr", 3
502 assert_select "a[href='#{user_block_path(active_block)}']", 1
503 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
506 # Check the list of blocks for a user that has previously been blocked
507 get user_blocks_on_path(:display_name => unblocked_user.display_name)
508 assert_response :success
509 assert_select "table#block_list", :count => 1 do
510 assert_select "tr", 2
511 assert_select "a[href='#{user_block_path(expired_block)}']", 1
516 # test the blocks_on action with multiple pages
517 def test_blocks_on_paged
519 create_list(:user_block, 50, :user => user)
521 get user_blocks_on_path(:display_name => user.display_name)
522 assert_response :success
523 assert_select "table#block_list", :count => 1 do
524 assert_select "tr", :count => 21
527 get user_blocks_on_path(:display_name => user.display_name, :page => 2)
528 assert_response :success
529 assert_select "table#block_list", :count => 1 do
530 assert_select "tr", :count => 21
535 # test the blocks_by action
537 moderator_user = create(:moderator_user)
538 second_moderator_user = create(:moderator_user)
539 normal_user = create(:user)
540 active_block = create(:user_block, :creator => moderator_user)
541 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
542 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
544 # Asking for a list of blocks with a bogus user name should fail
545 get user_blocks_by_path(:display_name => "non_existent_user")
546 assert_response :not_found
547 assert_template "users/no_such_user"
548 assert_select "h1", "The user non_existent_user does not exist"
550 # Check the list of blocks given by one moderator
551 get user_blocks_by_path(:display_name => moderator_user.display_name)
552 assert_response :success
553 assert_select "table#block_list", :count => 1 do
554 assert_select "tr", 2
555 assert_select "a[href='#{user_block_path(active_block)}']", 1
558 # Check the list of blocks given by a different moderator
559 get user_blocks_by_path(:display_name => second_moderator_user.display_name)
560 assert_response :success
561 assert_select "table#block_list", :count => 1 do
562 assert_select "tr", 3
563 assert_select "a[href='#{user_block_path(expired_block)}']", 1
564 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
567 # Check the list of blocks (not) given by a normal user
568 get user_blocks_by_path(:display_name => normal_user.display_name)
569 assert_response :success
570 assert_select "table#block_list", false
571 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
575 # test the blocks_by action with multiple pages
576 def test_blocks_by_paged
577 user = create(:moderator_user)
578 create_list(:user_block, 50, :creator => user)
580 get user_blocks_by_path(:display_name => user.display_name)
581 assert_response :success
582 assert_select "table#block_list", :count => 1 do
583 assert_select "tr", :count => 21
586 get user_blocks_by_path(:display_name => user.display_name, :page => 2)
587 assert_response :success
588 assert_select "table#block_list", :count => 1 do
589 assert_select "tr", :count => 21