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/revoke_all", :method => :get },
46 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
49 { :path => "/user/username/blocks/revoke_all", :method => :post },
50 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
55 # test the index action
57 revoked_block = create(:user_block, :revoked)
60 assert_response :success
61 assert_select "table#block_list tbody tr", :count => 1 do
62 assert_select "a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
63 assert_select "a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
64 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
67 active_block = create(:user_block)
68 expired_block = create(:user_block, :expired)
71 assert_response :success
72 assert_select "table#block_list tbody", :count => 1 do
74 assert_select "a[href='#{user_block_path(active_block)}']", 1
75 assert_select "a[href='#{user_block_path(expired_block)}']", 1
76 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
81 # test the index action with multiple pages
83 user_blocks = create_list(:user_block, 50).reverse
84 next_path = user_blocks_path
87 assert_response :success
88 check_user_blocks_table user_blocks[0...20]
89 check_no_page_link "Newer Blocks"
90 next_path = check_page_link "Older Blocks"
93 assert_response :success
94 check_user_blocks_table user_blocks[20...40]
95 check_page_link "Newer Blocks"
96 next_path = check_page_link "Older Blocks"
99 assert_response :success
100 check_user_blocks_table user_blocks[40...50]
101 check_page_link "Newer Blocks"
102 check_no_page_link "Older Blocks"
106 # test the index action with invalid pages
107 def test_index_invalid_paged
108 %w[-1 0 fred].each do |id|
109 get user_blocks_path(:before => id)
110 assert_redirected_to :controller => :errors, :action => :bad_request
112 get user_blocks_path(:after => id)
113 assert_redirected_to :controller => :errors, :action => :bad_request
118 # test the show action
120 active_block = create(:user_block, :needs_view)
121 expired_block = create(:user_block, :expired)
122 revoked_block = create(:user_block, :revoked)
124 # Viewing a block should fail when a bogus ID is given
125 get user_block_path(99999)
126 assert_response :not_found
127 assert_template "not_found"
128 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
130 # Viewing an expired block should work
131 get user_block_path(expired_block)
132 assert_response :success
133 assert_select "h1 a[href='#{user_path expired_block.user}']", :text => expired_block.user.display_name
134 assert_select "h1 a[href='#{user_path expired_block.creator}']", :text => expired_block.creator.display_name
136 # Viewing a revoked block should work
137 get user_block_path(revoked_block)
138 assert_response :success
139 assert_select "h1 a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
140 assert_select "h1 a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
141 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
143 # Viewing an active block should work, but shouldn't mark it as seen
144 get user_block_path(active_block)
145 assert_response :success
146 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
147 assert_select "h1 a[href='#{user_path active_block.creator}']", :text => active_block.creator.display_name
148 assert UserBlock.find(active_block.id).needs_view
152 # test clearing needs_view by showing a zero-hour block to the blocked user
153 def test_show_sets_deactivates_at_for_zero_hour_block
158 block = create(:user_block, :needs_view, :zero_hour, :user => user)
159 assert block.needs_view
160 assert_nil block.deactivates_at
164 get user_block_path(block)
165 assert_response :success
167 assert_not block.needs_view
168 assert_equal Time.now.utc, block.deactivates_at
172 get user_block_path(block)
173 assert_response :success
175 assert_not block.needs_view
176 assert_equal Time.now.utc - 1.hour, block.deactivates_at
181 # test clearing needs_view by showing a timed block to the blocked user
182 def test_show_sets_deactivates_at_for_timed_block
187 block = create(:user_block, :needs_view, :created_at => Time.now.utc, :ends_at => Time.now.utc + 24.hours, :user => user)
188 assert block.needs_view
189 assert_nil block.deactivates_at
193 get user_block_path(block)
194 assert_response :success
196 assert_not block.needs_view
197 assert_equal Time.now.utc + 23.hours, block.deactivates_at
201 get user_block_path(block)
202 assert_response :success
204 assert_not block.needs_view
205 assert_equal Time.now.utc + 22.hours, block.deactivates_at
209 get user_block_path(block)
210 assert_response :success
212 assert_not block.needs_view
213 assert_equal Time.now.utc - 2.hours, block.deactivates_at
218 # test edit/revoke link for active blocks
219 def test_active_block_buttons
220 creator_user = create(:moderator_user)
221 other_moderator_user = create(:moderator_user)
222 block = create(:user_block, :creator => creator_user)
224 session_for(other_moderator_user)
225 check_block_buttons block, :edit => 1
227 session_for(creator_user)
228 check_block_buttons block, :edit => 1
232 # test the edit link for expired blocks
233 def test_expired_block_buttons
234 creator_user = create(:moderator_user)
235 other_moderator_user = create(:moderator_user)
236 block = create(:user_block, :expired, :creator => creator_user)
238 session_for(other_moderator_user)
239 check_block_buttons block
241 session_for(creator_user)
242 check_block_buttons block, :edit => 1
246 # test the edit link for revoked blocks
247 def test_revoked_block_buttons
248 creator_user = create(:moderator_user)
249 revoker_user = create(:moderator_user)
250 other_moderator_user = create(:moderator_user)
251 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user)
253 session_for(other_moderator_user)
254 check_block_buttons block
256 session_for(creator_user)
257 check_block_buttons block, :edit => 1
259 session_for(revoker_user)
260 check_block_buttons block, :edit => 1
264 # test the new action
266 target_user = create(:user)
268 # Check that the block creation page requires us to login
269 get new_user_block_path(target_user)
270 assert_redirected_to login_path(:referer => new_user_block_path(target_user))
272 # Login as a normal user
273 session_for(create(:user))
275 # Check that normal users can't load the block creation page
276 get new_user_block_path(target_user)
277 assert_redirected_to :controller => "errors", :action => "forbidden"
279 # Login as a moderator
280 session_for(create(:moderator_user))
282 # Check that the block creation page loads for moderators
283 get new_user_block_path(target_user)
284 assert_response :success
285 assert_select "h1 a[href='#{user_path target_user}']", :text => target_user.display_name
286 assert_select "form#new_user_block", :count => 1 do
287 assert_select "textarea#user_block_reason", :count => 1
288 assert_select "select#user_block_period", :count => 1
289 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
290 assert_select "input#display_name[type='hidden']", :count => 1
291 assert_select "input[type='submit'][value='Create block']", :count => 1
294 # We should get an error if the user doesn't exist
295 get new_user_block_path("non_existent_user")
296 assert_response :not_found
297 assert_template "users/no_such_user"
298 assert_select "h1", "The user non_existent_user does not exist"
302 # test the edit action
304 creator_user = create(:moderator_user)
305 other_moderator_user = create(:moderator_user)
306 active_block = create(:user_block, :creator => creator_user)
308 # Check that the block edit page requires us to login
309 get edit_user_block_path(active_block)
310 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
312 # Login as a normal user
313 session_for(create(:user))
315 # Check that normal users can't load the block edit page
316 get edit_user_block_path(active_block)
317 assert_redirected_to :controller => "errors", :action => "forbidden"
319 # Login as a moderator
320 session_for(other_moderator_user)
322 # Check that the block edit page loads for moderators
323 get edit_user_block_path(active_block)
324 assert_response :success
325 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
326 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
327 assert_select "textarea#user_block_reason", :count => 1
328 assert_select "select#user_block_period", :count => 0
329 assert_select "input#user_block_needs_view[type='checkbox']", :count => 0
330 assert_select "input[type='submit'][value='Update block']", :count => 0
331 assert_select "input#user_block_period[type='hidden']", :count => 1
332 assert_select "input#user_block_needs_view[type='hidden']", :count => 1
333 assert_select "input[type='submit'][value='Revoke block']", :count => 1
336 # Login as the block creator
337 session_for(creator_user)
339 # Check that the block edit page loads for the creator
340 get edit_user_block_path(active_block)
341 assert_response :success
342 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
343 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
344 assert_select "textarea#user_block_reason", :count => 1
345 assert_select "select#user_block_period", :count => 1
346 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
347 assert_select "input[type='submit'][value='Update block']", :count => 1
348 assert_select "input#user_block_period[type='hidden']", :count => 0
349 assert_select "input#user_block_needs_view[type='hidden']", :count => 0
350 assert_select "input[type='submit'][value='Revoke block']", :count => 0
353 # We should get an error if the user doesn't exist
354 get edit_user_block_path(99999)
355 assert_response :not_found
356 assert_template "not_found"
357 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
361 # test the edit action when the remaining block duration doesn't match the available select options
362 def test_edit_duration
363 moderator_user = create(:moderator_user)
366 active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours)
368 session_for(moderator_user)
369 get edit_user_block_path(active_block)
371 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
372 assert_select "select#user_block_period", :count => 1 do
373 assert_select "option[value='96'][selected]", :count => 1
378 get edit_user_block_path(active_block)
380 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
381 assert_select "select#user_block_period", :count => 1 do
382 assert_select "option[value='96'][selected]", :count => 1
389 # test the create action
391 target_user = create(:user)
392 moderator_user = create(:moderator_user)
394 # Not logged in yet, so creating a block should fail
395 post user_blocks_path
396 assert_response :forbidden
398 # Login as a normal user
399 session_for(create(:user))
401 # Check that normal users can't create blocks
402 post user_blocks_path
403 assert_redirected_to :controller => "errors", :action => "forbidden"
405 # Login as a moderator
406 session_for(moderator_user)
408 # A bogus block period should result in an error
409 assert_no_difference "UserBlock.count" do
410 post user_blocks_path(:display_name => target_user.display_name,
411 :user_block_period => "99")
413 assert_redirected_to new_user_block_path(target_user)
414 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
416 # Check that creating a block works
417 assert_difference "UserBlock.count", 1 do
418 post user_blocks_path(:display_name => target_user.display_name,
419 :user_block_period => "12",
420 :user_block => { :needs_view => false, :reason => "Vandalism" })
423 assert_redirected_to user_block_path(b)
424 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
425 assert_in_delta Time.now.utc, b.created_at, 1
426 assert_in_delta Time.now.utc, b.updated_at, 1
427 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
428 assert_not b.needs_view
429 assert_equal "Vandalism", b.reason
430 assert_equal "markdown", b.reason_format
431 assert_equal moderator_user.id, b.creator_id
433 # We should get an error if no user is specified
434 post user_blocks_path
435 assert_response :not_found
436 assert_template "users/no_such_user"
437 assert_select "h1", "The user does not exist"
439 # We should get an error if the user doesn't exist
440 post user_blocks_path(:display_name => "non_existent_user")
441 assert_response :not_found
442 assert_template "users/no_such_user"
443 assert_select "h1", "The user non_existent_user does not exist"
447 # test the duration of a created block
448 def test_create_duration
449 target_user = create(:user)
450 moderator_user = create(:moderator_user)
452 session_for(moderator_user)
453 post user_blocks_path(:display_name => target_user.display_name,
454 :user_block_period => "336",
455 :user_block => { :needs_view => false, :reason => "Vandalism" })
457 block = UserBlock.last
458 assert_equal 1209600, block.ends_at - block.created_at
462 # test the update action
464 moderator_user = create(:moderator_user)
465 active_block = create(:user_block, :creator => moderator_user)
467 # Not logged in yet, so updating a block should fail
468 put user_block_path(active_block)
469 assert_response :forbidden
471 # Login as a normal user
472 session_for(create(:user))
474 # Check that normal users can't update blocks
475 put user_block_path(active_block)
476 assert_redirected_to :controller => "errors", :action => "forbidden"
478 # Login as the moderator
479 session_for(moderator_user)
481 # A bogus block period should result in an error
482 assert_no_difference "UserBlock.count" do
483 put user_block_path(active_block, :user_block_period => "99")
485 assert_redirected_to edit_user_block_path(active_block)
486 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
488 # Check that updating a block works
489 assert_no_difference "UserBlock.count" do
490 put user_block_path(active_block,
491 :user_block_period => "12",
492 :user_block => { :needs_view => true, :reason => "Vandalism" })
494 assert_redirected_to user_block_path(active_block)
495 assert_equal "Block updated.", flash[:notice]
496 b = UserBlock.find(active_block.id)
497 assert_in_delta Time.now.utc, b.updated_at, 1
499 assert_equal "Vandalism", b.reason
501 # We should get an error if the block doesn't exist
502 put user_block_path(99999)
503 assert_response :not_found
504 assert_template "not_found"
505 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
509 # test the update action on expired blocks
510 def test_update_expired
511 creator_user = create(:moderator_user)
512 other_moderator_user = create(:moderator_user)
513 block = create(:user_block, :expired, :creator => creator_user, :reason => "Original Reason")
515 session_for(other_moderator_user)
516 put user_block_path(block,
517 :user_block_period => "0",
518 :user_block => { :needs_view => false, :reason => "Updated Reason" })
519 assert_redirected_to edit_user_block_path(block)
520 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
522 assert_not block.active?
523 assert_equal "Original Reason", block.reason
525 session_for(creator_user)
526 check_inactive_block_updates(block)
530 # test the update action on revoked blocks
531 def test_update_revoked
532 creator_user = create(:moderator_user)
533 revoker_user = create(:moderator_user)
534 other_moderator_user = create(:moderator_user)
535 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user, :reason => "Original Reason")
537 session_for(other_moderator_user)
538 put user_block_path(block,
539 :user_block_period => "0",
540 :user_block => { :needs_view => false, :reason => "Updated Reason" })
541 assert_redirected_to edit_user_block_path(block)
542 assert_equal "Only the moderators who created or revoked this block can edit it.", flash[:error]
544 assert_not_predicate block, :active?
545 assert_equal "Original Reason", block.reason
547 session_for(creator_user)
548 check_inactive_block_updates(block)
550 session_for(revoker_user)
551 check_inactive_block_updates(block)
555 # test the update action revoking the block
556 def test_revoke_using_update_by_creator
557 moderator_user = create(:moderator_user)
558 block = create(:user_block, :creator => moderator_user)
560 session_for(moderator_user)
561 put user_block_path(block,
562 :user_block_period => "24",
563 :user_block => { :needs_view => false, :reason => "Updated Reason" })
564 assert_redirected_to user_block_path(block)
565 assert_equal "Block updated.", flash[:notice]
567 assert_predicate block, :active?
568 assert_nil block.revoker
570 put user_block_path(block,
571 :user_block_period => "0",
572 :user_block => { :needs_view => false, :reason => "Updated Reason" })
573 assert_redirected_to user_block_path(block)
574 assert_equal "Block updated.", flash[:notice]
576 assert_not_predicate block, :active?
577 assert_equal moderator_user, block.revoker
580 def test_revoke_using_update_by_other_moderator
581 creator_user = create(:moderator_user)
582 other_moderator_user = create(:moderator_user)
583 block = create(:user_block, :creator => creator_user)
585 session_for(other_moderator_user)
586 put user_block_path(block,
587 :user_block_period => "24",
588 :user_block => { :needs_view => false, :reason => "Updated Reason" })
589 assert_response :success
590 assert_equal "Only the moderator who created this block can edit it without revoking.", flash[:error]
592 assert_predicate block, :active?
593 assert_nil block.revoker
595 put user_block_path(block,
596 :user_block_period => "0",
597 :user_block => { :needs_view => false, :reason => "Updated Reason" })
598 assert_redirected_to user_block_path(block)
599 assert_equal "Block updated.", flash[:notice]
601 assert_not_predicate block, :active?
602 assert_equal other_moderator_user, block.revoker
606 # test the revoke all page
607 def test_revoke_all_page
608 blocked_user = create(:user)
609 create(:user_block, :user => blocked_user)
611 # Asking for the revoke all blocks page with a bogus user name should fail
612 get user_blocks_on_path("non_existent_user")
613 assert_response :not_found
615 # Check that the revoke all blocks page requires us to login
616 get revoke_all_user_blocks_path(blocked_user)
617 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
619 # Login as a normal user
620 session_for(create(:user))
622 # Check that normal users can't load the revoke all blocks page
623 get revoke_all_user_blocks_path(blocked_user)
624 assert_redirected_to :controller => "errors", :action => "forbidden"
626 # Login as a moderator
627 session_for(create(:moderator_user))
629 # Check that the revoke all blocks page loads for moderators
630 get revoke_all_user_blocks_path(blocked_user)
631 assert_response :success
632 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
636 # test the revoke all action
637 def test_revoke_all_action
638 blocked_user = create(:user)
639 active_block1 = create(:user_block, :user => blocked_user)
640 active_block2 = create(:user_block, :user => blocked_user)
641 expired_block1 = create(:user_block, :expired, :user => blocked_user)
642 blocks = [active_block1, active_block2, expired_block1]
643 moderator_user = create(:moderator_user)
645 assert_predicate active_block1, :active?
646 assert_predicate active_block2, :active?
647 assert_not_predicate expired_block1, :active?
649 # Login as a normal user
650 session_for(create(:user))
652 # Check that normal users can't load the block revoke page
653 get revoke_all_user_blocks_path(:blocked_user)
654 assert_redirected_to :controller => "errors", :action => "forbidden"
656 # Login as a moderator
657 session_for(moderator_user)
659 # Check that revoking blocks using GET should fail
660 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
661 assert_response :success
662 assert_template "revoke_all"
664 blocks.each(&:reload)
665 assert_predicate active_block1, :active?
666 assert_predicate active_block2, :active?
667 assert_not_predicate expired_block1, :active?
669 # Check that revoking blocks works using POST
670 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
671 assert_redirected_to user_blocks_on_path(blocked_user)
673 blocks.each(&:reload)
674 assert_not_predicate active_block1, :active?
675 assert_not_predicate active_block2, :active?
676 assert_not_predicate expired_block1, :active?
677 assert_equal moderator_user, active_block1.revoker
678 assert_equal moderator_user, active_block2.revoker
679 assert_not_equal moderator_user, expired_block1.revoker
683 # test changes to end/deactivation dates
684 def test_dates_when_viewed_before_end
685 blocked_user = create(:user)
686 moderator_user = create(:moderator_user)
689 session_for(moderator_user)
690 assert_difference "UserBlock.count", 1 do
691 post user_blocks_path(:display_name => blocked_user.display_name,
692 :user_block_period => "48",
693 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
695 block = UserBlock.last
696 assert_equal Time.now.utc + 48.hours, block.ends_at
697 assert_nil block.deactivates_at
700 session_for(blocked_user)
701 get user_block_path(block)
703 assert_equal Time.now.utc + 24.hours, block.ends_at
704 assert_equal Time.now.utc + 24.hours, block.deactivates_at
708 def test_dates_when_viewed_after_end
709 blocked_user = create(:user)
710 moderator_user = create(:moderator_user)
713 session_for(moderator_user)
714 assert_difference "UserBlock.count", 1 do
715 post user_blocks_path(:display_name => blocked_user.display_name,
716 :user_block_period => "24",
717 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
719 block = UserBlock.last
720 assert_equal Time.now.utc + 24.hours, block.ends_at
721 assert_nil block.deactivates_at
724 session_for(blocked_user)
725 get user_block_path(block)
727 assert_equal Time.now.utc - 24.hours, block.ends_at
728 assert_equal Time.now.utc, block.deactivates_at
732 def test_dates_when_edited_before_end
733 blocked_user = create(:user)
734 moderator_user = create(:moderator_user)
737 session_for(moderator_user)
738 assert_difference "UserBlock.count", 1 do
739 post user_blocks_path(:display_name => blocked_user.display_name,
740 :user_block_period => "48",
741 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
743 block = UserBlock.last
744 assert_equal Time.now.utc + 48.hours, block.ends_at
745 assert_equal Time.now.utc + 48.hours, block.deactivates_at
748 put user_block_path(block,
749 :user_block_period => "48",
750 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
752 assert_equal Time.now.utc + 48.hours, block.ends_at
753 assert_equal Time.now.utc + 48.hours, block.deactivates_at
757 def test_dates_when_edited_after_end
758 blocked_user = create(:user)
759 moderator_user = create(:moderator_user)
762 session_for(moderator_user)
763 assert_difference "UserBlock.count", 1 do
764 post user_blocks_path(:display_name => blocked_user.display_name,
765 :user_block_period => "24",
766 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
768 block = UserBlock.last
769 assert_equal Time.now.utc + 24.hours, block.ends_at
770 assert_equal Time.now.utc + 24.hours, block.deactivates_at
773 put user_block_path(block,
774 :user_block_period => "0",
775 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
777 assert_equal Time.now.utc - 24.hours, block.ends_at
778 assert_equal Time.now.utc - 24.hours, block.deactivates_at
783 # test updates on legacy records without correctly initialized deactivates_at
784 def test_update_legacy_deactivates_at
785 blocked_user = create(:user)
786 moderator_user = create(:moderator_user)
789 block = UserBlock.new :user => blocked_user,
790 :creator => moderator_user,
791 :reason => "because",
792 :ends_at => Time.now.utc + 24.hours,
795 assert_difference "UserBlock.count", 1 do
796 block.save :validate => false
800 session_for(moderator_user)
801 put user_block_path(block,
802 :user_block_period => "0",
803 :user_block => { :needs_view => false, :reason => "Testing legacy block update" })
805 assert_equal Time.now.utc - 24.hours, block.ends_at
806 assert_equal Time.now.utc - 24.hours, block.deactivates_at
811 # test the blocks_on action
813 blocked_user = create(:user)
814 unblocked_user = create(:user)
815 normal_user = create(:user)
816 active_block = create(:user_block, :user => blocked_user)
817 revoked_block = create(:user_block, :revoked, :user => blocked_user)
818 expired_block = create(:user_block, :expired, :user => unblocked_user)
820 # Asking for a list of blocks with a bogus user name should fail
821 get user_blocks_on_path("non_existent_user")
822 assert_response :not_found
823 assert_template "users/no_such_user"
824 assert_select "h1", "The user non_existent_user does not exist"
826 # Check the list of blocks for a user that has never been blocked
827 get user_blocks_on_path(normal_user)
828 assert_response :success
829 assert_select "table#block_list", false
830 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
832 # Check the list of blocks for a user that is currently blocked
833 get user_blocks_on_path(blocked_user)
834 assert_response :success
835 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
836 assert_select "a.active[href='#{user_blocks_on_path blocked_user}']"
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 "a.active[href='#{user_blocks_on_path unblocked_user}']"
848 assert_select "table#block_list tbody", :count => 1 do
849 assert_select "tr", 1
850 assert_select "a[href='#{user_block_path(expired_block)}']", 1
855 # test the blocks_on action with multiple pages
856 def test_blocks_on_paged
858 user_blocks = create_list(:user_block, 50, :user => user).reverse
859 next_path = user_blocks_on_path(user)
862 assert_response :success
863 check_user_blocks_table user_blocks[0...20]
864 check_no_page_link "Newer Blocks"
865 next_path = check_page_link "Older Blocks"
868 assert_response :success
869 check_user_blocks_table user_blocks[20...40]
870 check_page_link "Newer Blocks"
871 next_path = check_page_link "Older Blocks"
874 assert_response :success
875 check_user_blocks_table user_blocks[40...50]
876 check_page_link "Newer Blocks"
877 check_no_page_link "Older Blocks"
881 # test the blocks_on action with invalid pages
882 def test_blocks_on_invalid_paged
885 %w[-1 0 fred].each do |id|
886 get user_blocks_on_path(user, :before => id)
887 assert_redirected_to :controller => :errors, :action => :bad_request
889 get user_blocks_on_path(user, :after => id)
890 assert_redirected_to :controller => :errors, :action => :bad_request
896 def check_block_buttons(block, edit: 0)
897 [user_blocks_path, user_block_path(block)].each do |path|
899 assert_response :success
900 assert_select "a[href='#{edit_user_block_path block}']", :count => edit
904 def check_inactive_block_updates(block)
905 original_ends_at = block.ends_at
907 put user_block_path(block,
908 :user_block_period => "0",
909 :user_block => { :needs_view => false, :reason => "Updated Reason" })
910 assert_redirected_to user_block_path(block)
911 assert_equal "Block updated.", flash[:notice]
913 assert_not_predicate block, :active?
914 assert_equal "Updated Reason", block.reason
915 assert_equal original_ends_at, block.ends_at
917 put user_block_path(block,
918 :user_block_period => "0",
919 :user_block => { :needs_view => true, :reason => "Updated Reason Needs View" })
920 assert_response :success
921 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
923 assert_not_predicate block, :active?
924 assert_equal "Updated Reason", block.reason
925 assert_equal original_ends_at, block.ends_at
927 put user_block_path(block,
928 :user_block_period => "1",
929 :user_block => { :needs_view => false, :reason => "Updated Reason Duration Extended" })
930 assert_response :success
931 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
933 assert_not_predicate block, :active?
934 assert_equal "Updated Reason", block.reason
935 assert_equal original_ends_at, block.ends_at
937 put user_block_path(block,
938 :user_block_period => "0",
939 :user_block => { :needs_view => false, :reason => "Updated Reason Again" })
940 assert_redirected_to user_block_path(block)
941 assert_equal "Block updated.", flash[:notice]
943 assert_not_predicate block, :active?
944 assert_equal "Updated Reason Again", block.reason
945 assert_equal original_ends_at, block.ends_at