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", :method => :post },
18 { :controller => "user_blocks", :action => "create" }
21 { :path => "/user_blocks/1", :method => :get },
22 { :controller => "user_blocks", :action => "show", :id => "1" }
25 { :path => "/user_blocks/1/edit", :method => :get },
26 { :controller => "user_blocks", :action => "edit", :id => "1" }
29 { :path => "/user_blocks/1", :method => :put },
30 { :controller => "user_blocks", :action => "update", :id => "1" }
33 { :path => "/user_blocks/1", :method => :delete },
34 { :controller => "user_blocks", :action => "destroy", :id => "1" }
38 { :path => "/user/username/blocks", :method => :get },
39 { :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
42 { :path => "/user/username/blocks_by", :method => :get },
43 { :controller => "user_blocks", :action => "blocks_by", :display_name => "username" }
46 { :path => "/user/username/blocks/revoke_all", :method => :get },
47 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
50 { :path => "/user/username/blocks/revoke_all", :method => :post },
51 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
56 # test the index action
58 revoked_block = create(:user_block, :revoked)
61 assert_response :success
62 assert_select "table#block_list tbody tr", :count => 1 do
63 assert_select "a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
64 assert_select "a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
65 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
68 active_block = create(:user_block)
69 expired_block = create(:user_block, :expired)
72 assert_response :success
73 assert_select "table#block_list tbody", :count => 1 do
75 assert_select "a[href='#{user_block_path(active_block)}']", 1
76 assert_select "a[href='#{user_block_path(expired_block)}']", 1
77 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
82 # test the index action with multiple pages
84 user_blocks = create_list(:user_block, 50).reverse
85 next_path = user_blocks_path
88 assert_response :success
89 check_user_blocks_table user_blocks[0...20]
90 check_no_page_link "Newer Blocks"
91 next_path = check_page_link "Older Blocks"
94 assert_response :success
95 check_user_blocks_table user_blocks[20...40]
96 check_page_link "Newer Blocks"
97 next_path = check_page_link "Older Blocks"
100 assert_response :success
101 check_user_blocks_table user_blocks[40...50]
102 check_page_link "Newer Blocks"
103 check_no_page_link "Older Blocks"
107 # test the index action with invalid pages
108 def test_index_invalid_paged
109 %w[-1 0 fred].each do |id|
110 get user_blocks_path(:before => id)
111 assert_redirected_to :controller => :errors, :action => :bad_request
113 get user_blocks_path(:after => id)
114 assert_redirected_to :controller => :errors, :action => :bad_request
119 # test the show action
121 active_block = create(:user_block, :needs_view)
122 expired_block = create(:user_block, :expired)
123 revoked_block = create(:user_block, :revoked)
125 # Viewing a block should fail when a bogus ID is given
126 get user_block_path(:id => 99999)
127 assert_response :not_found
128 assert_template "not_found"
129 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
131 # Viewing an expired block should work
132 get user_block_path(:id => expired_block)
133 assert_response :success
134 assert_select "h1 a[href='#{user_path expired_block.user}']", :text => expired_block.user.display_name
135 assert_select "h1 a[href='#{user_path expired_block.creator}']", :text => expired_block.creator.display_name
137 # Viewing a revoked block should work
138 get user_block_path(:id => revoked_block)
139 assert_response :success
140 assert_select "h1 a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
141 assert_select "h1 a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
142 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
144 # Viewing an active block should work, but shouldn't mark it as seen
145 get user_block_path(:id => active_block)
146 assert_response :success
147 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
148 assert_select "h1 a[href='#{user_path active_block.creator}']", :text => active_block.creator.display_name
149 assert UserBlock.find(active_block.id).needs_view
153 # test clearing needs_view by showing a zero-hour block to the blocked user
154 def test_show_sets_deactivates_at_for_zero_hour_block
159 block = create(:user_block, :needs_view, :zero_hour, :user => user)
160 assert block.needs_view
161 assert_nil block.deactivates_at
165 get user_block_path(block)
166 assert_response :success
168 assert_not block.needs_view
169 assert_equal Time.now.utc, block.deactivates_at
173 get user_block_path(block)
174 assert_response :success
176 assert_not block.needs_view
177 assert_equal Time.now.utc - 1.hour, block.deactivates_at
182 # test clearing needs_view by showing a timed block to the blocked user
183 def test_show_sets_deactivates_at_for_timed_block
188 block = create(:user_block, :needs_view, :created_at => Time.now.utc, :ends_at => Time.now.utc + 24.hours, :user => user)
189 assert block.needs_view
190 assert_nil block.deactivates_at
194 get user_block_path(block)
195 assert_response :success
197 assert_not block.needs_view
198 assert_equal Time.now.utc + 23.hours, block.deactivates_at
202 get user_block_path(block)
203 assert_response :success
205 assert_not block.needs_view
206 assert_equal Time.now.utc + 22.hours, block.deactivates_at
210 get user_block_path(block)
211 assert_response :success
213 assert_not block.needs_view
214 assert_equal Time.now.utc - 2.hours, block.deactivates_at
219 # test edit/revoke link for active blocks
220 def test_active_block_buttons
221 creator_user = create(:moderator_user)
222 other_moderator_user = create(:moderator_user)
223 block = create(:user_block, :creator => creator_user)
225 session_for(other_moderator_user)
226 check_block_buttons block, :edit => 1
228 session_for(creator_user)
229 check_block_buttons block, :edit => 1
233 # test the edit link for expired blocks
234 def test_expired_block_buttons
235 creator_user = create(:moderator_user)
236 other_moderator_user = create(:moderator_user)
237 block = create(:user_block, :expired, :creator => creator_user)
239 session_for(other_moderator_user)
240 check_block_buttons block
242 session_for(creator_user)
243 check_block_buttons block, :edit => 1
247 # test the edit link for revoked blocks
248 def test_revoked_block_buttons
249 creator_user = create(:moderator_user)
250 revoker_user = create(:moderator_user)
251 other_moderator_user = create(:moderator_user)
252 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user)
254 session_for(other_moderator_user)
255 check_block_buttons block
257 session_for(creator_user)
258 check_block_buttons block, :edit => 1
260 session_for(revoker_user)
261 check_block_buttons block, :edit => 1
265 # test the new action
267 target_user = create(:user)
269 # Check that the block creation page requires us to login
270 get new_user_block_path(target_user)
271 assert_redirected_to login_path(:referer => new_user_block_path(target_user))
273 # Login as a normal user
274 session_for(create(:user))
276 # Check that normal users can't load the block creation page
277 get new_user_block_path(target_user)
278 assert_redirected_to :controller => "errors", :action => "forbidden"
280 # Login as a moderator
281 session_for(create(:moderator_user))
283 # Check that the block creation page loads for moderators
284 get new_user_block_path(target_user)
285 assert_response :success
286 assert_select "h1 a[href='#{user_path target_user}']", :text => target_user.display_name
287 assert_select "form#new_user_block", :count => 1 do
288 assert_select "textarea#user_block_reason", :count => 1
289 assert_select "select#user_block_period", :count => 1
290 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
291 assert_select "input#display_name[type='hidden']", :count => 1
292 assert_select "input[type='submit'][value='Create block']", :count => 1
295 # We should get an error if the user doesn't exist
296 get new_user_block_path(:display_name => "non_existent_user")
297 assert_response :not_found
298 assert_template "users/no_such_user"
299 assert_select "h1", "The user non_existent_user does not exist"
303 # test the edit action
305 creator_user = create(:moderator_user)
306 other_moderator_user = create(:moderator_user)
307 active_block = create(:user_block, :creator => creator_user)
309 # Check that the block edit page requires us to login
310 get edit_user_block_path(:id => active_block)
311 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
313 # Login as a normal user
314 session_for(create(:user))
316 # Check that normal users can't load the block edit page
317 get edit_user_block_path(:id => active_block)
318 assert_redirected_to :controller => "errors", :action => "forbidden"
320 # Login as a moderator
321 session_for(other_moderator_user)
323 # Check that the block edit page loads for moderators
324 get edit_user_block_path(:id => active_block)
325 assert_response :success
326 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
327 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
328 assert_select "textarea#user_block_reason", :count => 1
329 assert_select "select#user_block_period", :count => 0
330 assert_select "input#user_block_needs_view[type='checkbox']", :count => 0
331 assert_select "input[type='submit'][value='Update block']", :count => 0
332 assert_select "input#user_block_period[type='hidden']", :count => 1
333 assert_select "input#user_block_needs_view[type='hidden']", :count => 1
334 assert_select "input[type='submit'][value='Revoke block']", :count => 1
337 # Login as the block creator
338 session_for(creator_user)
340 # Check that the block edit page loads for the creator
341 get edit_user_block_path(:id => active_block)
342 assert_response :success
343 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
344 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
345 assert_select "textarea#user_block_reason", :count => 1
346 assert_select "select#user_block_period", :count => 1
347 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
348 assert_select "input[type='submit'][value='Update block']", :count => 1
349 assert_select "input#user_block_period[type='hidden']", :count => 0
350 assert_select "input#user_block_needs_view[type='hidden']", :count => 0
351 assert_select "input[type='submit'][value='Revoke block']", :count => 0
354 # We should get an error if the user doesn't exist
355 get edit_user_block_path(:id => 99999)
356 assert_response :not_found
357 assert_template "not_found"
358 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
362 # test the edit action when the remaining block duration doesn't match the available select options
363 def test_edit_duration
364 moderator_user = create(:moderator_user)
367 active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours)
369 session_for(moderator_user)
370 get edit_user_block_path(active_block)
372 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
373 assert_select "select#user_block_period", :count => 1 do
374 assert_select "option[value='96'][selected]", :count => 1
379 get edit_user_block_path(active_block)
381 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
382 assert_select "select#user_block_period", :count => 1 do
383 assert_select "option[value='96'][selected]", :count => 1
390 # test the create action
392 target_user = create(:user)
393 moderator_user = create(:moderator_user)
395 # Not logged in yet, so creating a block should fail
396 post user_blocks_path
397 assert_response :forbidden
399 # Login as a normal user
400 session_for(create(:user))
402 # Check that normal users can't create blocks
403 post user_blocks_path
404 assert_redirected_to :controller => "errors", :action => "forbidden"
406 # Login as a moderator
407 session_for(moderator_user)
409 # A bogus block period should result in an error
410 assert_no_difference "UserBlock.count" do
411 post user_blocks_path(:display_name => target_user.display_name,
412 :user_block_period => "99")
414 assert_redirected_to new_user_block_path(target_user)
415 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
417 # Check that creating a block works
418 assert_difference "UserBlock.count", 1 do
419 post user_blocks_path(:display_name => target_user.display_name,
420 :user_block_period => "12",
421 :user_block => { :needs_view => false, :reason => "Vandalism" })
424 assert_redirected_to user_block_path(:id => b.id)
425 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
426 assert_in_delta Time.now.utc, b.created_at, 1
427 assert_in_delta Time.now.utc, b.updated_at, 1
428 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
429 assert_not b.needs_view
430 assert_equal "Vandalism", b.reason
431 assert_equal "markdown", b.reason_format
432 assert_equal moderator_user.id, b.creator_id
434 # We should get an error if no user is specified
435 post user_blocks_path
436 assert_response :not_found
437 assert_template "users/no_such_user"
438 assert_select "h1", "The user does not exist"
440 # We should get an error if the user doesn't exist
441 post user_blocks_path(:display_name => "non_existent_user")
442 assert_response :not_found
443 assert_template "users/no_such_user"
444 assert_select "h1", "The user non_existent_user does not exist"
448 # test the duration of a created block
449 def test_create_duration
450 target_user = create(:user)
451 moderator_user = create(:moderator_user)
453 session_for(moderator_user)
454 post user_blocks_path(:display_name => target_user.display_name,
455 :user_block_period => "336",
456 :user_block => { :needs_view => false, :reason => "Vandalism" })
458 block = UserBlock.last
459 assert_equal 1209600, block.ends_at - block.created_at
463 # test the update action
465 moderator_user = create(:moderator_user)
466 active_block = create(:user_block, :creator => moderator_user)
468 # Not logged in yet, so updating a block should fail
469 put user_block_path(:id => active_block)
470 assert_response :forbidden
472 # Login as a normal user
473 session_for(create(:user))
475 # Check that normal users can't update blocks
476 put user_block_path(:id => active_block)
477 assert_redirected_to :controller => "errors", :action => "forbidden"
479 # Login as the moderator
480 session_for(moderator_user)
482 # A bogus block period should result in an error
483 assert_no_difference "UserBlock.count" do
484 put user_block_path(:id => active_block, :user_block_period => "99")
486 assert_redirected_to edit_user_block_path(active_block)
487 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
489 # Check that updating a block works
490 assert_no_difference "UserBlock.count" do
491 put user_block_path(:id => active_block,
492 :user_block_period => "12",
493 :user_block => { :needs_view => true, :reason => "Vandalism" })
495 assert_redirected_to user_block_path(active_block)
496 assert_equal "Block updated.", flash[:notice]
497 b = UserBlock.find(active_block.id)
498 assert_in_delta Time.now.utc, b.updated_at, 1
500 assert_equal "Vandalism", b.reason
502 # We should get an error if the block doesn't exist
503 put user_block_path(:id => 99999)
504 assert_response :not_found
505 assert_template "not_found"
506 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
510 # test the update action on expired blocks
511 def test_update_expired
512 creator_user = create(:moderator_user)
513 other_moderator_user = create(:moderator_user)
514 block = create(:user_block, :expired, :creator => creator_user, :reason => "Original Reason")
516 session_for(other_moderator_user)
517 put user_block_path(block,
518 :user_block_period => "0",
519 :user_block => { :needs_view => false, :reason => "Updated Reason" })
520 assert_redirected_to edit_user_block_path(block)
521 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
523 assert_not block.active?
524 assert_equal "Original Reason", block.reason
526 session_for(creator_user)
527 check_inactive_block_updates(block)
531 # test the update action on revoked blocks
532 def test_update_revoked
533 creator_user = create(:moderator_user)
534 revoker_user = create(:moderator_user)
535 other_moderator_user = create(:moderator_user)
536 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user, :reason => "Original Reason")
538 session_for(other_moderator_user)
539 put user_block_path(block,
540 :user_block_period => "0",
541 :user_block => { :needs_view => false, :reason => "Updated Reason" })
542 assert_redirected_to edit_user_block_path(block)
543 assert_equal "Only the moderators who created or revoked this block can edit it.", flash[:error]
545 assert_not_predicate block, :active?
546 assert_equal "Original Reason", block.reason
548 session_for(creator_user)
549 check_inactive_block_updates(block)
551 session_for(revoker_user)
552 check_inactive_block_updates(block)
556 # test the update action revoking the block
557 def test_revoke_using_update_by_creator
558 moderator_user = create(:moderator_user)
559 block = create(:user_block, :creator => moderator_user)
561 session_for(moderator_user)
562 put user_block_path(block,
563 :user_block_period => "24",
564 :user_block => { :needs_view => false, :reason => "Updated Reason" })
565 assert_redirected_to user_block_path(block)
566 assert_equal "Block updated.", flash[:notice]
568 assert_predicate block, :active?
569 assert_nil block.revoker
571 put user_block_path(block,
572 :user_block_period => "0",
573 :user_block => { :needs_view => false, :reason => "Updated Reason" })
574 assert_redirected_to user_block_path(block)
575 assert_equal "Block updated.", flash[:notice]
577 assert_not_predicate block, :active?
578 assert_equal moderator_user, block.revoker
581 def test_revoke_using_update_by_other_moderator
582 creator_user = create(:moderator_user)
583 other_moderator_user = create(:moderator_user)
584 block = create(:user_block, :creator => creator_user)
586 session_for(other_moderator_user)
587 put user_block_path(block,
588 :user_block_period => "24",
589 :user_block => { :needs_view => false, :reason => "Updated Reason" })
590 assert_response :success
591 assert_equal "Only the moderator who created this block can edit it without revoking.", flash[:error]
593 assert_predicate block, :active?
594 assert_nil block.revoker
596 put user_block_path(block,
597 :user_block_period => "0",
598 :user_block => { :needs_view => false, :reason => "Updated Reason" })
599 assert_redirected_to user_block_path(block)
600 assert_equal "Block updated.", flash[:notice]
602 assert_not_predicate block, :active?
603 assert_equal other_moderator_user, block.revoker
607 # test the revoke all page
608 def test_revoke_all_page
609 blocked_user = create(:user)
610 create(:user_block, :user => blocked_user)
612 # Asking for the revoke all blocks page with a bogus user name should fail
613 get user_blocks_on_path("non_existent_user")
614 assert_response :not_found
616 # Check that the revoke all blocks page requires us to login
617 get revoke_all_user_blocks_path(blocked_user)
618 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
620 # Login as a normal user
621 session_for(create(:user))
623 # Check that normal users can't load the revoke all blocks page
624 get revoke_all_user_blocks_path(blocked_user)
625 assert_redirected_to :controller => "errors", :action => "forbidden"
627 # Login as a moderator
628 session_for(create(:moderator_user))
630 # Check that the revoke all blocks page loads for moderators
631 get revoke_all_user_blocks_path(blocked_user)
632 assert_response :success
633 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
637 # test the revoke all action
638 def test_revoke_all_action
639 blocked_user = create(:user)
640 active_block1 = create(:user_block, :user => blocked_user)
641 active_block2 = create(:user_block, :user => blocked_user)
642 expired_block1 = create(:user_block, :expired, :user => blocked_user)
643 blocks = [active_block1, active_block2, expired_block1]
644 moderator_user = create(:moderator_user)
646 assert_predicate active_block1, :active?
647 assert_predicate active_block2, :active?
648 assert_not_predicate expired_block1, :active?
650 # Login as a normal user
651 session_for(create(:user))
653 # Check that normal users can't load the block revoke page
654 get revoke_all_user_blocks_path(:blocked_user)
655 assert_redirected_to :controller => "errors", :action => "forbidden"
657 # Login as a moderator
658 session_for(moderator_user)
660 # Check that revoking blocks using GET should fail
661 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
662 assert_response :success
663 assert_template "revoke_all"
665 blocks.each(&:reload)
666 assert_predicate active_block1, :active?
667 assert_predicate active_block2, :active?
668 assert_not_predicate expired_block1, :active?
670 # Check that revoking blocks works using POST
671 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
672 assert_redirected_to user_blocks_on_path(blocked_user)
674 blocks.each(&:reload)
675 assert_not_predicate active_block1, :active?
676 assert_not_predicate active_block2, :active?
677 assert_not_predicate expired_block1, :active?
678 assert_equal moderator_user, active_block1.revoker
679 assert_equal moderator_user, active_block2.revoker
680 assert_not_equal moderator_user, expired_block1.revoker
684 # test changes to end/deactivation dates
685 def test_dates_when_viewed_before_end
686 blocked_user = create(:user)
687 moderator_user = create(:moderator_user)
690 session_for(moderator_user)
691 assert_difference "UserBlock.count", 1 do
692 post user_blocks_path(:display_name => blocked_user.display_name,
693 :user_block_period => "48",
694 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
696 block = UserBlock.last
697 assert_equal Time.now.utc + 48.hours, block.ends_at
698 assert_nil block.deactivates_at
701 session_for(blocked_user)
702 get user_block_path(block)
704 assert_equal Time.now.utc + 24.hours, block.ends_at
705 assert_equal Time.now.utc + 24.hours, block.deactivates_at
709 def test_dates_when_viewed_after_end
710 blocked_user = create(:user)
711 moderator_user = create(:moderator_user)
714 session_for(moderator_user)
715 assert_difference "UserBlock.count", 1 do
716 post user_blocks_path(:display_name => blocked_user.display_name,
717 :user_block_period => "24",
718 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
720 block = UserBlock.last
721 assert_equal Time.now.utc + 24.hours, block.ends_at
722 assert_nil block.deactivates_at
725 session_for(blocked_user)
726 get user_block_path(block)
728 assert_equal Time.now.utc - 24.hours, block.ends_at
729 assert_equal Time.now.utc, block.deactivates_at
733 def test_dates_when_edited_before_end
734 blocked_user = create(:user)
735 moderator_user = create(:moderator_user)
738 session_for(moderator_user)
739 assert_difference "UserBlock.count", 1 do
740 post user_blocks_path(:display_name => blocked_user.display_name,
741 :user_block_period => "48",
742 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
744 block = UserBlock.last
745 assert_equal Time.now.utc + 48.hours, block.ends_at
746 assert_equal Time.now.utc + 48.hours, block.deactivates_at
749 put user_block_path(block,
750 :user_block_period => "48",
751 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
753 assert_equal Time.now.utc + 48.hours, block.ends_at
754 assert_equal Time.now.utc + 48.hours, block.deactivates_at
758 def test_dates_when_edited_after_end
759 blocked_user = create(:user)
760 moderator_user = create(:moderator_user)
763 session_for(moderator_user)
764 assert_difference "UserBlock.count", 1 do
765 post user_blocks_path(:display_name => blocked_user.display_name,
766 :user_block_period => "24",
767 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
769 block = UserBlock.last
770 assert_equal Time.now.utc + 24.hours, block.ends_at
771 assert_equal Time.now.utc + 24.hours, block.deactivates_at
774 put user_block_path(block,
775 :user_block_period => "0",
776 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
778 assert_equal Time.now.utc - 24.hours, block.ends_at
779 assert_equal Time.now.utc - 24.hours, block.deactivates_at
784 # test updates on legacy records without correctly initialized deactivates_at
785 def test_update_legacy_deactivates_at
786 blocked_user = create(:user)
787 moderator_user = create(:moderator_user)
790 block = UserBlock.new :user => blocked_user,
791 :creator => moderator_user,
792 :reason => "because",
793 :ends_at => Time.now.utc + 24.hours,
796 assert_difference "UserBlock.count", 1 do
797 block.save :validate => false
801 session_for(moderator_user)
802 put user_block_path(block,
803 :user_block_period => "0",
804 :user_block => { :needs_view => false, :reason => "Testing legacy block update" })
806 assert_equal Time.now.utc - 24.hours, block.ends_at
807 assert_equal Time.now.utc - 24.hours, block.deactivates_at
812 # test the blocks_on action
814 blocked_user = create(:user)
815 unblocked_user = create(:user)
816 normal_user = create(:user)
817 active_block = create(:user_block, :user => blocked_user)
818 revoked_block = create(:user_block, :revoked, :user => blocked_user)
819 expired_block = create(:user_block, :expired, :user => unblocked_user)
821 # Asking for a list of blocks with a bogus user name should fail
822 get user_blocks_on_path("non_existent_user")
823 assert_response :not_found
824 assert_template "users/no_such_user"
825 assert_select "h1", "The user non_existent_user does not exist"
827 # Check the list of blocks for a user that has never been blocked
828 get user_blocks_on_path(normal_user)
829 assert_response :success
830 assert_select "table#block_list", false
831 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
833 # Check the list of blocks for a user that is currently blocked
834 get user_blocks_on_path(blocked_user)
835 assert_response :success
836 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
837 assert_select "table#block_list tbody", :count => 1 do
838 assert_select "tr", 2
839 assert_select "a[href='#{user_block_path(active_block)}']", 1
840 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
843 # Check the list of blocks for a user that has previously been blocked
844 get user_blocks_on_path(unblocked_user)
845 assert_response :success
846 assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
847 assert_select "table#block_list tbody", :count => 1 do
848 assert_select "tr", 1
849 assert_select "a[href='#{user_block_path(expired_block)}']", 1
854 # test the blocks_on action with multiple pages
855 def test_blocks_on_paged
857 user_blocks = create_list(:user_block, 50, :user => user).reverse
858 next_path = user_blocks_on_path(user)
861 assert_response :success
862 check_user_blocks_table user_blocks[0...20]
863 check_no_page_link "Newer Blocks"
864 next_path = check_page_link "Older Blocks"
867 assert_response :success
868 check_user_blocks_table user_blocks[20...40]
869 check_page_link "Newer Blocks"
870 next_path = check_page_link "Older Blocks"
873 assert_response :success
874 check_user_blocks_table user_blocks[40...50]
875 check_page_link "Newer Blocks"
876 check_no_page_link "Older Blocks"
880 # test the blocks_on action with invalid pages
881 def test_blocks_on_invalid_paged
884 %w[-1 0 fred].each do |id|
885 get user_blocks_on_path(user, :before => id)
886 assert_redirected_to :controller => :errors, :action => :bad_request
888 get user_blocks_on_path(user, :after => id)
889 assert_redirected_to :controller => :errors, :action => :bad_request
894 # test the blocks_by action
896 moderator_user = create(:moderator_user)
897 second_moderator_user = create(:moderator_user)
898 normal_user = create(:user)
899 active_block = create(:user_block, :creator => moderator_user)
900 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
901 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
903 # Asking for a list of blocks with a bogus user name should fail
904 get user_blocks_by_path("non_existent_user")
905 assert_response :not_found
906 assert_template "users/no_such_user"
907 assert_select "h1", "The user non_existent_user does not exist"
909 # Check the list of blocks given by one moderator
910 get user_blocks_by_path(moderator_user)
911 assert_response :success
912 assert_select "h1 a[href='#{user_path moderator_user}']", :text => moderator_user.display_name
913 assert_select "table#block_list tbody", :count => 1 do
914 assert_select "tr", 1
915 assert_select "a[href='#{user_block_path(active_block)}']", 1
918 # Check the list of blocks given by a different moderator
919 get user_blocks_by_path(second_moderator_user)
920 assert_response :success
921 assert_select "h1 a[href='#{user_path second_moderator_user}']", :text => second_moderator_user.display_name
922 assert_select "table#block_list tbody", :count => 1 do
923 assert_select "tr", 2
924 assert_select "a[href='#{user_block_path(expired_block)}']", 1
925 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
928 # Check the list of blocks (not) given by a normal user
929 get user_blocks_by_path(normal_user)
930 assert_response :success
931 assert_select "table#block_list", false
932 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
936 # test the blocks_by action with multiple pages
937 def test_blocks_by_paged
938 user = create(:moderator_user)
939 user_blocks = create_list(:user_block, 50, :creator => user).reverse
940 next_path = user_blocks_by_path(user)
943 assert_response :success
944 check_user_blocks_table user_blocks[0...20]
945 check_no_page_link "Newer Blocks"
946 next_path = check_page_link "Older Blocks"
949 assert_response :success
950 check_user_blocks_table user_blocks[20...40]
951 check_page_link "Newer Blocks"
952 next_path = check_page_link "Older Blocks"
955 assert_response :success
956 check_user_blocks_table user_blocks[40...50]
957 check_page_link "Newer Blocks"
958 check_no_page_link "Older Blocks"
962 # test the blocks_by action with invalid pages
963 def test_blocks_by_invalid_paged
964 user = create(:moderator_user)
966 %w[-1 0 fred].each do |id|
967 get user_blocks_by_path(user, :before => id)
968 assert_redirected_to :controller => :errors, :action => :bad_request
970 get user_blocks_by_path(user, :after => id)
971 assert_redirected_to :controller => :errors, :action => :bad_request
977 def check_block_buttons(block, edit: 0)
978 [user_blocks_path, user_block_path(block)].each do |path|
980 assert_response :success
981 assert_select "a[href='#{edit_user_block_path block}']", :count => edit
985 def check_inactive_block_updates(block)
986 original_ends_at = block.ends_at
988 put user_block_path(block,
989 :user_block_period => "0",
990 :user_block => { :needs_view => false, :reason => "Updated Reason" })
991 assert_redirected_to user_block_path(block)
992 assert_equal "Block updated.", flash[:notice]
994 assert_not_predicate block, :active?
995 assert_equal "Updated Reason", block.reason
996 assert_equal original_ends_at, block.ends_at
998 put user_block_path(block,
999 :user_block_period => "0",
1000 :user_block => { :needs_view => true, :reason => "Updated Reason Needs View" })
1001 assert_response :success
1002 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
1004 assert_not_predicate block, :active?
1005 assert_equal "Updated Reason", block.reason
1006 assert_equal original_ends_at, block.ends_at
1008 put user_block_path(block,
1009 :user_block_period => "1",
1010 :user_block => { :needs_view => false, :reason => "Updated Reason Duration Extended" })
1011 assert_response :success
1012 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
1014 assert_not_predicate block, :active?
1015 assert_equal "Updated Reason", block.reason
1016 assert_equal original_ends_at, block.ends_at
1018 put user_block_path(block,
1019 :user_block_period => "0",
1020 :user_block => { :needs_view => false, :reason => "Updated Reason Again" })
1021 assert_redirected_to user_block_path(block)
1022 assert_equal "Block updated.", flash[:notice]
1024 assert_not_predicate block, :active?
1025 assert_equal "Updated Reason Again", block.reason
1026 assert_equal original_ends_at, block.ends_at
1029 def check_user_blocks_table(user_blocks)
1030 assert_dom "table#block_list tbody tr" do |rows|
1031 assert_equal user_blocks.count, rows.count, "unexpected number of rows in user blocks table"
1032 rows.zip(user_blocks).map do |row, user_block|
1033 assert_dom row, "a[href='#{user_block_path user_block}']", 1
1038 def check_no_page_link(name)
1039 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
1042 def check_page_link(name)
1043 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
1044 return buttons.first.attributes["href"].value