2 require_relative "user_blocks/table_test_helper"
4 class UserBlocksControllerTest < ActionDispatch::IntegrationTest
5 include UserBlocks::TableTestHelper
8 # test all routes which lead to this controller
11 { :path => "/user_blocks/new/username", :method => :get },
12 { :controller => "user_blocks", :action => "new", :display_name => "username" }
16 { :path => "/user_blocks", :method => :get },
17 { :controller => "user_blocks", :action => "index" }
20 { :path => "/user_blocks", :method => :post },
21 { :controller => "user_blocks", :action => "create" }
24 { :path => "/user_blocks/1", :method => :get },
25 { :controller => "user_blocks", :action => "show", :id => "1" }
28 { :path => "/user_blocks/1/edit", :method => :get },
29 { :controller => "user_blocks", :action => "edit", :id => "1" }
32 { :path => "/user_blocks/1", :method => :put },
33 { :controller => "user_blocks", :action => "update", :id => "1" }
36 { :path => "/user_blocks/1", :method => :delete },
37 { :controller => "user_blocks", :action => "destroy", :id => "1" }
41 { :path => "/user/username/blocks", :method => :get },
42 { :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
45 { :path => "/user/username/blocks_by", :method => :get },
46 { :controller => "user_blocks", :action => "blocks_by", :display_name => "username" }
49 { :path => "/user/username/blocks/revoke_all", :method => :get },
50 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
53 { :path => "/user/username/blocks/revoke_all", :method => :post },
54 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
59 # test the index action
61 revoked_block = create(:user_block, :revoked)
64 assert_response :success
65 assert_select "table#block_list tbody tr", :count => 1 do
66 assert_select "a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
67 assert_select "a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
68 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
71 active_block = create(:user_block)
72 expired_block = create(:user_block, :expired)
75 assert_response :success
76 assert_select "table#block_list tbody", :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 user_blocks = create_list(:user_block, 50).reverse
88 next_path = user_blocks_path
91 assert_response :success
92 check_user_blocks_table user_blocks[0...20]
93 check_no_page_link "Newer Blocks"
94 next_path = check_page_link "Older Blocks"
97 assert_response :success
98 check_user_blocks_table user_blocks[20...40]
99 check_page_link "Newer Blocks"
100 next_path = check_page_link "Older Blocks"
103 assert_response :success
104 check_user_blocks_table user_blocks[40...50]
105 check_page_link "Newer Blocks"
106 check_no_page_link "Older Blocks"
110 # test the index action with invalid pages
111 def test_index_invalid_paged
112 %w[-1 0 fred].each do |id|
113 get user_blocks_path(:before => id)
114 assert_redirected_to :controller => :errors, :action => :bad_request
116 get user_blocks_path(:after => id)
117 assert_redirected_to :controller => :errors, :action => :bad_request
122 # test the show action
124 active_block = create(:user_block, :needs_view)
125 expired_block = create(:user_block, :expired)
126 revoked_block = create(:user_block, :revoked)
128 # Viewing a block should fail when a bogus ID is given
129 get user_block_path(99999)
130 assert_response :not_found
131 assert_template "not_found"
132 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
134 # Viewing an expired block should work
135 get user_block_path(expired_block)
136 assert_response :success
137 assert_select "h1 a[href='#{user_path expired_block.user}']", :text => expired_block.user.display_name
138 assert_select "h1 a[href='#{user_path expired_block.creator}']", :text => expired_block.creator.display_name
140 # Viewing a revoked block should work
141 get user_block_path(revoked_block)
142 assert_response :success
143 assert_select "h1 a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
144 assert_select "h1 a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
145 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
147 # Viewing an active block should work, but shouldn't mark it as seen
148 get user_block_path(active_block)
149 assert_response :success
150 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
151 assert_select "h1 a[href='#{user_path active_block.creator}']", :text => active_block.creator.display_name
152 assert UserBlock.find(active_block.id).needs_view
156 # test clearing needs_view by showing a zero-hour block to the blocked user
157 def test_show_sets_deactivates_at_for_zero_hour_block
162 block = create(:user_block, :needs_view, :zero_hour, :user => user)
163 assert block.needs_view
164 assert_nil block.deactivates_at
168 get user_block_path(block)
169 assert_response :success
171 assert_not block.needs_view
172 assert_equal Time.now.utc, block.deactivates_at
176 get user_block_path(block)
177 assert_response :success
179 assert_not block.needs_view
180 assert_equal Time.now.utc - 1.hour, block.deactivates_at
185 # test clearing needs_view by showing a timed block to the blocked user
186 def test_show_sets_deactivates_at_for_timed_block
191 block = create(:user_block, :needs_view, :created_at => Time.now.utc, :ends_at => Time.now.utc + 24.hours, :user => user)
192 assert block.needs_view
193 assert_nil block.deactivates_at
197 get user_block_path(block)
198 assert_response :success
200 assert_not block.needs_view
201 assert_equal Time.now.utc + 23.hours, block.deactivates_at
205 get user_block_path(block)
206 assert_response :success
208 assert_not block.needs_view
209 assert_equal Time.now.utc + 22.hours, block.deactivates_at
213 get user_block_path(block)
214 assert_response :success
216 assert_not block.needs_view
217 assert_equal Time.now.utc - 2.hours, block.deactivates_at
222 # test edit/revoke link for active blocks
223 def test_active_block_buttons
224 creator_user = create(:moderator_user)
225 other_moderator_user = create(:moderator_user)
226 block = create(:user_block, :creator => creator_user)
228 session_for(other_moderator_user)
229 check_block_buttons block, :edit => 1
231 session_for(creator_user)
232 check_block_buttons block, :edit => 1
236 # test the edit link for expired blocks
237 def test_expired_block_buttons
238 creator_user = create(:moderator_user)
239 other_moderator_user = create(:moderator_user)
240 block = create(:user_block, :expired, :creator => creator_user)
242 session_for(other_moderator_user)
243 check_block_buttons block
245 session_for(creator_user)
246 check_block_buttons block, :edit => 1
250 # test the edit link for revoked blocks
251 def test_revoked_block_buttons
252 creator_user = create(:moderator_user)
253 revoker_user = create(:moderator_user)
254 other_moderator_user = create(:moderator_user)
255 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user)
257 session_for(other_moderator_user)
258 check_block_buttons block
260 session_for(creator_user)
261 check_block_buttons block, :edit => 1
263 session_for(revoker_user)
264 check_block_buttons block, :edit => 1
268 # test the new action
270 target_user = create(:user)
272 # Check that the block creation page requires us to login
273 get new_user_block_path(target_user)
274 assert_redirected_to login_path(:referer => new_user_block_path(target_user))
276 # Login as a normal user
277 session_for(create(:user))
279 # Check that normal users can't load the block creation page
280 get new_user_block_path(target_user)
281 assert_redirected_to :controller => "errors", :action => "forbidden"
283 # Login as a moderator
284 session_for(create(:moderator_user))
286 # Check that the block creation page loads for moderators
287 get new_user_block_path(target_user)
288 assert_response :success
289 assert_select "h1 a[href='#{user_path target_user}']", :text => target_user.display_name
290 assert_select "form#new_user_block", :count => 1 do
291 assert_select "textarea#user_block_reason", :count => 1
292 assert_select "select#user_block_period", :count => 1
293 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
294 assert_select "input#display_name[type='hidden']", :count => 1
295 assert_select "input[type='submit'][value='Create block']", :count => 1
298 # We should get an error if the user doesn't exist
299 get new_user_block_path("non_existent_user")
300 assert_response :not_found
301 assert_template "users/no_such_user"
302 assert_select "h1", "The user non_existent_user does not exist"
306 # test the edit action
308 creator_user = create(:moderator_user)
309 other_moderator_user = create(:moderator_user)
310 active_block = create(:user_block, :creator => creator_user)
312 # Check that the block edit page requires us to login
313 get edit_user_block_path(active_block)
314 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
316 # Login as a normal user
317 session_for(create(:user))
319 # Check that normal users can't load the block edit page
320 get edit_user_block_path(active_block)
321 assert_redirected_to :controller => "errors", :action => "forbidden"
323 # Login as a moderator
324 session_for(other_moderator_user)
326 # Check that the block edit page loads for moderators
327 get edit_user_block_path(active_block)
328 assert_response :success
329 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
330 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
331 assert_select "textarea#user_block_reason", :count => 1
332 assert_select "select#user_block_period", :count => 0
333 assert_select "input#user_block_needs_view[type='checkbox']", :count => 0
334 assert_select "input[type='submit'][value='Update block']", :count => 0
335 assert_select "input#user_block_period[type='hidden']", :count => 1
336 assert_select "input#user_block_needs_view[type='hidden']", :count => 1
337 assert_select "input[type='submit'][value='Revoke block']", :count => 1
340 # Login as the block creator
341 session_for(creator_user)
343 # Check that the block edit page loads for the creator
344 get edit_user_block_path(active_block)
345 assert_response :success
346 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
347 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
348 assert_select "textarea#user_block_reason", :count => 1
349 assert_select "select#user_block_period", :count => 1
350 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
351 assert_select "input[type='submit'][value='Update block']", :count => 1
352 assert_select "input#user_block_period[type='hidden']", :count => 0
353 assert_select "input#user_block_needs_view[type='hidden']", :count => 0
354 assert_select "input[type='submit'][value='Revoke block']", :count => 0
357 # We should get an error if the user doesn't exist
358 get edit_user_block_path(99999)
359 assert_response :not_found
360 assert_template "not_found"
361 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
365 # test the edit action when the remaining block duration doesn't match the available select options
366 def test_edit_duration
367 moderator_user = create(:moderator_user)
370 active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours)
372 session_for(moderator_user)
373 get edit_user_block_path(active_block)
375 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
376 assert_select "select#user_block_period", :count => 1 do
377 assert_select "option[value='96'][selected]", :count => 1
382 get edit_user_block_path(active_block)
384 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
385 assert_select "select#user_block_period", :count => 1 do
386 assert_select "option[value='96'][selected]", :count => 1
393 # test the create action
395 target_user = create(:user)
396 moderator_user = create(:moderator_user)
398 # Not logged in yet, so creating a block should fail
399 post user_blocks_path
400 assert_response :forbidden
402 # Login as a normal user
403 session_for(create(:user))
405 # Check that normal users can't create blocks
406 post user_blocks_path
407 assert_redirected_to :controller => "errors", :action => "forbidden"
409 # Login as a moderator
410 session_for(moderator_user)
412 # A bogus block period should result in an error
413 assert_no_difference "UserBlock.count" do
414 post user_blocks_path(:display_name => target_user.display_name,
415 :user_block_period => "99")
417 assert_redirected_to new_user_block_path(target_user)
418 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
420 # Check that creating a block works
421 assert_difference "UserBlock.count", 1 do
422 post user_blocks_path(:display_name => target_user.display_name,
423 :user_block_period => "12",
424 :user_block => { :needs_view => false, :reason => "Vandalism" })
427 assert_redirected_to user_block_path(b)
428 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
429 assert_in_delta Time.now.utc, b.created_at, 1
430 assert_in_delta Time.now.utc, b.updated_at, 1
431 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
432 assert_not b.needs_view
433 assert_equal "Vandalism", b.reason
434 assert_equal "markdown", b.reason_format
435 assert_equal moderator_user.id, b.creator_id
437 # We should get an error if no user is specified
438 post user_blocks_path
439 assert_response :not_found
440 assert_template "users/no_such_user"
441 assert_select "h1", "The user does not exist"
443 # We should get an error if the user doesn't exist
444 post user_blocks_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"
451 # test the duration of a created block
452 def test_create_duration
453 target_user = create(:user)
454 moderator_user = create(:moderator_user)
456 session_for(moderator_user)
457 post user_blocks_path(:display_name => target_user.display_name,
458 :user_block_period => "336",
459 :user_block => { :needs_view => false, :reason => "Vandalism" })
461 block = UserBlock.last
462 assert_equal 1209600, block.ends_at - block.created_at
466 # test the update action
468 moderator_user = create(:moderator_user)
469 active_block = create(:user_block, :creator => moderator_user)
471 # Not logged in yet, so updating a block should fail
472 put user_block_path(active_block)
473 assert_response :forbidden
475 # Login as a normal user
476 session_for(create(:user))
478 # Check that normal users can't update blocks
479 put user_block_path(active_block)
480 assert_redirected_to :controller => "errors", :action => "forbidden"
482 # Login as the moderator
483 session_for(moderator_user)
485 # A bogus block period should result in an error
486 assert_no_difference "UserBlock.count" do
487 put user_block_path(active_block, :user_block_period => "99")
489 assert_redirected_to edit_user_block_path(active_block)
490 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
492 # Check that updating a block works
493 assert_no_difference "UserBlock.count" do
494 put user_block_path(active_block,
495 :user_block_period => "12",
496 :user_block => { :needs_view => true, :reason => "Vandalism" })
498 assert_redirected_to user_block_path(active_block)
499 assert_equal "Block updated.", flash[:notice]
500 b = UserBlock.find(active_block.id)
501 assert_in_delta Time.now.utc, b.updated_at, 1
503 assert_equal "Vandalism", b.reason
505 # We should get an error if the block doesn't exist
506 put user_block_path(99999)
507 assert_response :not_found
508 assert_template "not_found"
509 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
513 # test the update action on expired blocks
514 def test_update_expired
515 creator_user = create(:moderator_user)
516 other_moderator_user = create(:moderator_user)
517 block = create(:user_block, :expired, :creator => creator_user, :reason => "Original Reason")
519 session_for(other_moderator_user)
520 put user_block_path(block,
521 :user_block_period => "0",
522 :user_block => { :needs_view => false, :reason => "Updated Reason" })
523 assert_redirected_to edit_user_block_path(block)
524 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
526 assert_not block.active?
527 assert_equal "Original Reason", block.reason
529 session_for(creator_user)
530 check_inactive_block_updates(block)
534 # test the update action on revoked blocks
535 def test_update_revoked
536 creator_user = create(:moderator_user)
537 revoker_user = create(:moderator_user)
538 other_moderator_user = create(:moderator_user)
539 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user, :reason => "Original Reason")
541 session_for(other_moderator_user)
542 put user_block_path(block,
543 :user_block_period => "0",
544 :user_block => { :needs_view => false, :reason => "Updated Reason" })
545 assert_redirected_to edit_user_block_path(block)
546 assert_equal "Only the moderators who created or revoked this block can edit it.", flash[:error]
548 assert_not_predicate block, :active?
549 assert_equal "Original Reason", block.reason
551 session_for(creator_user)
552 check_inactive_block_updates(block)
554 session_for(revoker_user)
555 check_inactive_block_updates(block)
559 # test the update action revoking the block
560 def test_revoke_using_update_by_creator
561 moderator_user = create(:moderator_user)
562 block = create(:user_block, :creator => moderator_user)
564 session_for(moderator_user)
565 put user_block_path(block,
566 :user_block_period => "24",
567 :user_block => { :needs_view => false, :reason => "Updated Reason" })
568 assert_redirected_to user_block_path(block)
569 assert_equal "Block updated.", flash[:notice]
571 assert_predicate block, :active?
572 assert_nil block.revoker
574 put user_block_path(block,
575 :user_block_period => "0",
576 :user_block => { :needs_view => false, :reason => "Updated Reason" })
577 assert_redirected_to user_block_path(block)
578 assert_equal "Block updated.", flash[:notice]
580 assert_not_predicate block, :active?
581 assert_equal moderator_user, block.revoker
584 def test_revoke_using_update_by_other_moderator
585 creator_user = create(:moderator_user)
586 other_moderator_user = create(:moderator_user)
587 block = create(:user_block, :creator => creator_user)
589 session_for(other_moderator_user)
590 put user_block_path(block,
591 :user_block_period => "24",
592 :user_block => { :needs_view => false, :reason => "Updated Reason" })
593 assert_response :success
594 assert_equal "Only the moderator who created this block can edit it without revoking.", flash[:error]
596 assert_predicate block, :active?
597 assert_nil block.revoker
599 put user_block_path(block,
600 :user_block_period => "0",
601 :user_block => { :needs_view => false, :reason => "Updated Reason" })
602 assert_redirected_to user_block_path(block)
603 assert_equal "Block updated.", flash[:notice]
605 assert_not_predicate block, :active?
606 assert_equal other_moderator_user, block.revoker
610 # test the revoke all page
611 def test_revoke_all_page
612 blocked_user = create(:user)
613 create(:user_block, :user => blocked_user)
615 # Asking for the revoke all blocks page with a bogus user name should fail
616 get user_blocks_on_path("non_existent_user")
617 assert_response :not_found
619 # Check that the revoke all blocks page requires us to login
620 get revoke_all_user_blocks_path(blocked_user)
621 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
623 # Login as a normal user
624 session_for(create(:user))
626 # Check that normal users can't load the revoke all blocks page
627 get revoke_all_user_blocks_path(blocked_user)
628 assert_redirected_to :controller => "errors", :action => "forbidden"
630 # Login as a moderator
631 session_for(create(:moderator_user))
633 # Check that the revoke all blocks page loads for moderators
634 get revoke_all_user_blocks_path(blocked_user)
635 assert_response :success
636 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
640 # test the revoke all action
641 def test_revoke_all_action
642 blocked_user = create(:user)
643 active_block1 = create(:user_block, :user => blocked_user)
644 active_block2 = create(:user_block, :user => blocked_user)
645 expired_block1 = create(:user_block, :expired, :user => blocked_user)
646 blocks = [active_block1, active_block2, expired_block1]
647 moderator_user = create(:moderator_user)
649 assert_predicate active_block1, :active?
650 assert_predicate active_block2, :active?
651 assert_not_predicate expired_block1, :active?
653 # Login as a normal user
654 session_for(create(:user))
656 # Check that normal users can't load the block revoke page
657 get revoke_all_user_blocks_path(:blocked_user)
658 assert_redirected_to :controller => "errors", :action => "forbidden"
660 # Login as a moderator
661 session_for(moderator_user)
663 # Check that revoking blocks using GET should fail
664 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
665 assert_response :success
666 assert_template "revoke_all"
668 blocks.each(&:reload)
669 assert_predicate active_block1, :active?
670 assert_predicate active_block2, :active?
671 assert_not_predicate expired_block1, :active?
673 # Check that revoking blocks works using POST
674 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
675 assert_redirected_to user_blocks_on_path(blocked_user)
677 blocks.each(&:reload)
678 assert_not_predicate active_block1, :active?
679 assert_not_predicate active_block2, :active?
680 assert_not_predicate expired_block1, :active?
681 assert_equal moderator_user, active_block1.revoker
682 assert_equal moderator_user, active_block2.revoker
683 assert_not_equal moderator_user, expired_block1.revoker
687 # test changes to end/deactivation dates
688 def test_dates_when_viewed_before_end
689 blocked_user = create(:user)
690 moderator_user = create(:moderator_user)
693 session_for(moderator_user)
694 assert_difference "UserBlock.count", 1 do
695 post user_blocks_path(:display_name => blocked_user.display_name,
696 :user_block_period => "48",
697 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
699 block = UserBlock.last
700 assert_equal Time.now.utc + 48.hours, block.ends_at
701 assert_nil block.deactivates_at
704 session_for(blocked_user)
705 get user_block_path(block)
707 assert_equal Time.now.utc + 24.hours, block.ends_at
708 assert_equal Time.now.utc + 24.hours, block.deactivates_at
712 def test_dates_when_viewed_after_end
713 blocked_user = create(:user)
714 moderator_user = create(:moderator_user)
717 session_for(moderator_user)
718 assert_difference "UserBlock.count", 1 do
719 post user_blocks_path(:display_name => blocked_user.display_name,
720 :user_block_period => "24",
721 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
723 block = UserBlock.last
724 assert_equal Time.now.utc + 24.hours, block.ends_at
725 assert_nil block.deactivates_at
728 session_for(blocked_user)
729 get user_block_path(block)
731 assert_equal Time.now.utc - 24.hours, block.ends_at
732 assert_equal Time.now.utc, block.deactivates_at
736 def test_dates_when_edited_before_end
737 blocked_user = create(:user)
738 moderator_user = create(:moderator_user)
741 session_for(moderator_user)
742 assert_difference "UserBlock.count", 1 do
743 post user_blocks_path(:display_name => blocked_user.display_name,
744 :user_block_period => "48",
745 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
747 block = UserBlock.last
748 assert_equal Time.now.utc + 48.hours, block.ends_at
749 assert_equal Time.now.utc + 48.hours, block.deactivates_at
752 put user_block_path(block,
753 :user_block_period => "48",
754 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
756 assert_equal Time.now.utc + 48.hours, block.ends_at
757 assert_equal Time.now.utc + 48.hours, block.deactivates_at
761 def test_dates_when_edited_after_end
762 blocked_user = create(:user)
763 moderator_user = create(:moderator_user)
766 session_for(moderator_user)
767 assert_difference "UserBlock.count", 1 do
768 post user_blocks_path(:display_name => blocked_user.display_name,
769 :user_block_period => "24",
770 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
772 block = UserBlock.last
773 assert_equal Time.now.utc + 24.hours, block.ends_at
774 assert_equal Time.now.utc + 24.hours, block.deactivates_at
777 put user_block_path(block,
778 :user_block_period => "0",
779 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
781 assert_equal Time.now.utc - 24.hours, block.ends_at
782 assert_equal Time.now.utc - 24.hours, block.deactivates_at
787 # test updates on legacy records without correctly initialized deactivates_at
788 def test_update_legacy_deactivates_at
789 blocked_user = create(:user)
790 moderator_user = create(:moderator_user)
793 block = UserBlock.new :user => blocked_user,
794 :creator => moderator_user,
795 :reason => "because",
796 :ends_at => Time.now.utc + 24.hours,
799 assert_difference "UserBlock.count", 1 do
800 block.save :validate => false
804 session_for(moderator_user)
805 put user_block_path(block,
806 :user_block_period => "0",
807 :user_block => { :needs_view => false, :reason => "Testing legacy block update" })
809 assert_equal Time.now.utc - 24.hours, block.ends_at
810 assert_equal Time.now.utc - 24.hours, block.deactivates_at
815 # test the blocks_on action
817 blocked_user = create(:user)
818 unblocked_user = create(:user)
819 normal_user = create(:user)
820 active_block = create(:user_block, :user => blocked_user)
821 revoked_block = create(:user_block, :revoked, :user => blocked_user)
822 expired_block = create(:user_block, :expired, :user => unblocked_user)
824 # Asking for a list of blocks with a bogus user name should fail
825 get user_blocks_on_path("non_existent_user")
826 assert_response :not_found
827 assert_template "users/no_such_user"
828 assert_select "h1", "The user non_existent_user does not exist"
830 # Check the list of blocks for a user that has never been blocked
831 get user_blocks_on_path(normal_user)
832 assert_response :success
833 assert_select "table#block_list", false
834 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
836 # Check the list of blocks for a user that is currently blocked
837 get user_blocks_on_path(blocked_user)
838 assert_response :success
839 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
840 assert_select "a.active[href='#{user_blocks_on_path blocked_user}']"
841 assert_select "table#block_list tbody", :count => 1 do
842 assert_select "tr", 2
843 assert_select "a[href='#{user_block_path(active_block)}']", 1
844 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
847 # Check the list of blocks for a user that has previously been blocked
848 get user_blocks_on_path(unblocked_user)
849 assert_response :success
850 assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
851 assert_select "a.active[href='#{user_blocks_on_path unblocked_user}']"
852 assert_select "table#block_list tbody", :count => 1 do
853 assert_select "tr", 1
854 assert_select "a[href='#{user_block_path(expired_block)}']", 1
859 # test the blocks_on action with multiple pages
860 def test_blocks_on_paged
862 user_blocks = create_list(:user_block, 50, :user => user).reverse
863 next_path = user_blocks_on_path(user)
866 assert_response :success
867 check_user_blocks_table user_blocks[0...20]
868 check_no_page_link "Newer Blocks"
869 next_path = check_page_link "Older Blocks"
872 assert_response :success
873 check_user_blocks_table user_blocks[20...40]
874 check_page_link "Newer Blocks"
875 next_path = check_page_link "Older Blocks"
878 assert_response :success
879 check_user_blocks_table user_blocks[40...50]
880 check_page_link "Newer Blocks"
881 check_no_page_link "Older Blocks"
885 # test the blocks_on action with invalid pages
886 def test_blocks_on_invalid_paged
889 %w[-1 0 fred].each do |id|
890 get user_blocks_on_path(user, :before => id)
891 assert_redirected_to :controller => :errors, :action => :bad_request
893 get user_blocks_on_path(user, :after => id)
894 assert_redirected_to :controller => :errors, :action => :bad_request
899 # test the blocks_by action
901 moderator_user = create(:moderator_user)
902 second_moderator_user = create(:moderator_user)
903 normal_user = create(:user)
904 active_block = create(:user_block, :creator => moderator_user)
905 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
906 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
908 # Asking for a list of blocks with a bogus user name should fail
909 get user_blocks_by_path("non_existent_user")
910 assert_response :not_found
911 assert_template "users/no_such_user"
912 assert_select "h1", "The user non_existent_user does not exist"
914 # Check the list of blocks given by one moderator
915 get user_blocks_by_path(moderator_user)
916 assert_response :success
917 assert_select "h1 a[href='#{user_path moderator_user}']", :text => moderator_user.display_name
918 assert_select "a.active[href='#{user_blocks_by_path moderator_user}']"
919 assert_select "table#block_list tbody", :count => 1 do
920 assert_select "tr", 1
921 assert_select "a[href='#{user_block_path(active_block)}']", 1
924 # Check the list of blocks given by a different moderator
925 get user_blocks_by_path(second_moderator_user)
926 assert_response :success
927 assert_select "h1 a[href='#{user_path second_moderator_user}']", :text => second_moderator_user.display_name
928 assert_select "a.active[href='#{user_blocks_by_path second_moderator_user}']"
929 assert_select "table#block_list tbody", :count => 1 do
930 assert_select "tr", 2
931 assert_select "a[href='#{user_block_path(expired_block)}']", 1
932 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
935 # Check the list of blocks (not) given by a normal user
936 get user_blocks_by_path(normal_user)
937 assert_response :success
938 assert_select "table#block_list", false
939 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
943 # test the blocks_by action with multiple pages
944 def test_blocks_by_paged
945 user = create(:moderator_user)
946 user_blocks = create_list(:user_block, 50, :creator => user).reverse
947 next_path = user_blocks_by_path(user)
950 assert_response :success
951 check_user_blocks_table user_blocks[0...20]
952 check_no_page_link "Newer Blocks"
953 next_path = check_page_link "Older Blocks"
956 assert_response :success
957 check_user_blocks_table user_blocks[20...40]
958 check_page_link "Newer Blocks"
959 next_path = check_page_link "Older Blocks"
962 assert_response :success
963 check_user_blocks_table user_blocks[40...50]
964 check_page_link "Newer Blocks"
965 check_no_page_link "Older Blocks"
969 # test the blocks_by action with invalid pages
970 def test_blocks_by_invalid_paged
971 user = create(:moderator_user)
973 %w[-1 0 fred].each do |id|
974 get user_blocks_by_path(user, :before => id)
975 assert_redirected_to :controller => :errors, :action => :bad_request
977 get user_blocks_by_path(user, :after => id)
978 assert_redirected_to :controller => :errors, :action => :bad_request
984 def check_block_buttons(block, edit: 0)
985 [user_blocks_path, user_block_path(block)].each do |path|
987 assert_response :success
988 assert_select "a[href='#{edit_user_block_path block}']", :count => edit
992 def check_inactive_block_updates(block)
993 original_ends_at = block.ends_at
995 put user_block_path(block,
996 :user_block_period => "0",
997 :user_block => { :needs_view => false, :reason => "Updated Reason" })
998 assert_redirected_to user_block_path(block)
999 assert_equal "Block updated.", flash[:notice]
1001 assert_not_predicate block, :active?
1002 assert_equal "Updated Reason", block.reason
1003 assert_equal original_ends_at, block.ends_at
1005 put user_block_path(block,
1006 :user_block_period => "0",
1007 :user_block => { :needs_view => true, :reason => "Updated Reason Needs View" })
1008 assert_response :success
1009 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
1011 assert_not_predicate block, :active?
1012 assert_equal "Updated Reason", block.reason
1013 assert_equal original_ends_at, block.ends_at
1015 put user_block_path(block,
1016 :user_block_period => "1",
1017 :user_block => { :needs_view => false, :reason => "Updated Reason Duration Extended" })
1018 assert_response :success
1019 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
1021 assert_not_predicate block, :active?
1022 assert_equal "Updated Reason", block.reason
1023 assert_equal original_ends_at, block.ends_at
1025 put user_block_path(block,
1026 :user_block_period => "0",
1027 :user_block => { :needs_view => false, :reason => "Updated Reason Again" })
1028 assert_redirected_to user_block_path(block)
1029 assert_equal "Block updated.", flash[:notice]
1031 assert_not_predicate block, :active?
1032 assert_equal "Updated Reason Again", block.reason
1033 assert_equal original_ends_at, block.ends_at