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" }
42 # test the index action
44 revoked_block = create(:user_block, :revoked)
47 assert_response :success
48 assert_select "table#block_list tbody tr", :count => 1 do
49 assert_select "a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
50 assert_select "a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
51 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
54 active_block = create(:user_block)
55 expired_block = create(:user_block, :expired)
58 assert_response :success
59 assert_select "table#block_list tbody", :count => 1 do
61 assert_select "a[href='#{user_block_path(active_block)}']", 1
62 assert_select "a[href='#{user_block_path(expired_block)}']", 1
63 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
68 # test the index action with multiple pages
70 user_blocks = create_list(:user_block, 50).reverse
71 next_path = user_blocks_path
74 assert_response :success
75 check_user_blocks_table user_blocks[0...20]
76 check_no_page_link "Newer Blocks"
77 next_path = check_page_link "Older Blocks"
80 assert_response :success
81 check_user_blocks_table user_blocks[20...40]
82 check_page_link "Newer Blocks"
83 next_path = check_page_link "Older Blocks"
86 assert_response :success
87 check_user_blocks_table user_blocks[40...50]
88 check_page_link "Newer Blocks"
89 check_no_page_link "Older Blocks"
93 # test the index action with invalid pages
94 def test_index_invalid_paged
95 %w[-1 0 fred].each do |id|
96 get user_blocks_path(:before => id)
97 assert_redirected_to :controller => :errors, :action => :bad_request
99 get user_blocks_path(:after => id)
100 assert_redirected_to :controller => :errors, :action => :bad_request
105 # test the show action
107 active_block = create(:user_block, :needs_view)
108 expired_block = create(:user_block, :expired)
109 revoked_block = create(:user_block, :revoked)
111 # Viewing a block should fail when a bogus ID is given
112 get user_block_path(99999)
113 assert_response :not_found
114 assert_template "not_found"
115 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
117 # Viewing an expired block should work
118 get user_block_path(expired_block)
119 assert_response :success
120 assert_select "h1 a[href='#{user_path expired_block.user}']", :text => expired_block.user.display_name
121 assert_select "h1 a[href='#{user_path expired_block.creator}']", :text => expired_block.creator.display_name
123 # Viewing a revoked block should work
124 get user_block_path(revoked_block)
125 assert_response :success
126 assert_select "h1 a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
127 assert_select "h1 a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
128 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
130 # Viewing an active block should work, but shouldn't mark it as seen
131 get user_block_path(active_block)
132 assert_response :success
133 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
134 assert_select "h1 a[href='#{user_path active_block.creator}']", :text => active_block.creator.display_name
135 assert UserBlock.find(active_block.id).needs_view
139 # test clearing needs_view by showing a zero-hour block to the blocked user
140 def test_show_sets_deactivates_at_for_zero_hour_block
145 block = create(:user_block, :needs_view, :zero_hour, :user => user)
146 assert block.needs_view
147 assert_nil block.deactivates_at
151 get user_block_path(block)
152 assert_response :success
154 assert_not block.needs_view
155 assert_equal Time.now.utc, block.deactivates_at
159 get user_block_path(block)
160 assert_response :success
162 assert_not block.needs_view
163 assert_equal Time.now.utc - 1.hour, block.deactivates_at
168 # test clearing needs_view by showing a timed block to the blocked user
169 def test_show_sets_deactivates_at_for_timed_block
174 block = create(:user_block, :needs_view, :created_at => Time.now.utc, :ends_at => Time.now.utc + 24.hours, :user => user)
175 assert block.needs_view
176 assert_nil block.deactivates_at
180 get user_block_path(block)
181 assert_response :success
183 assert_not block.needs_view
184 assert_equal Time.now.utc + 23.hours, block.deactivates_at
188 get user_block_path(block)
189 assert_response :success
191 assert_not block.needs_view
192 assert_equal Time.now.utc + 22.hours, block.deactivates_at
196 get user_block_path(block)
197 assert_response :success
199 assert_not block.needs_view
200 assert_equal Time.now.utc - 2.hours, block.deactivates_at
205 # test edit/revoke link for active blocks
206 def test_active_block_buttons
207 creator_user = create(:moderator_user)
208 other_moderator_user = create(:moderator_user)
209 block = create(:user_block, :creator => creator_user)
211 session_for(other_moderator_user)
212 check_block_buttons block, :edit => 1
214 session_for(creator_user)
215 check_block_buttons block, :edit => 1
219 # test the edit link for expired blocks
220 def test_expired_block_buttons
221 creator_user = create(:moderator_user)
222 other_moderator_user = create(:moderator_user)
223 block = create(:user_block, :expired, :creator => creator_user)
225 session_for(other_moderator_user)
226 check_block_buttons block
228 session_for(creator_user)
229 check_block_buttons block, :edit => 1
233 # test the edit link for revoked blocks
234 def test_revoked_block_buttons
235 creator_user = create(:moderator_user)
236 revoker_user = create(:moderator_user)
237 other_moderator_user = create(:moderator_user)
238 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user)
240 session_for(other_moderator_user)
241 check_block_buttons block
243 session_for(creator_user)
244 check_block_buttons block, :edit => 1
246 session_for(revoker_user)
247 check_block_buttons block, :edit => 1
251 # test the new action
253 target_user = create(:user)
255 # Check that the block creation page requires us to login
256 get new_user_block_path(target_user)
257 assert_redirected_to login_path(:referer => new_user_block_path(target_user))
259 # Login as a normal user
260 session_for(create(:user))
262 # Check that normal users can't load the block creation page
263 get new_user_block_path(target_user)
264 assert_redirected_to :controller => "errors", :action => "forbidden"
266 # Login as a moderator
267 session_for(create(:moderator_user))
269 # Check that the block creation page loads for moderators
270 get new_user_block_path(target_user)
271 assert_response :success
272 assert_select "h1 a[href='#{user_path target_user}']", :text => target_user.display_name
273 assert_select "form#new_user_block", :count => 1 do
274 assert_select "textarea#user_block_reason", :count => 1
275 assert_select "select#user_block_period", :count => 1
276 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
277 assert_select "input#display_name[type='hidden']", :count => 1
278 assert_select "input[type='submit'][value='Create block']", :count => 1
281 # We should get an error if the user doesn't exist
282 get new_user_block_path("non_existent_user")
283 assert_response :not_found
284 assert_template "users/no_such_user"
285 assert_select "h1", "The user non_existent_user does not exist"
289 # test the edit action
291 creator_user = create(:moderator_user)
292 other_moderator_user = create(:moderator_user)
293 active_block = create(:user_block, :creator => creator_user)
295 # Check that the block edit page requires us to login
296 get edit_user_block_path(active_block)
297 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
299 # Login as a normal user
300 session_for(create(:user))
302 # Check that normal users can't load the block edit page
303 get edit_user_block_path(active_block)
304 assert_redirected_to :controller => "errors", :action => "forbidden"
306 # Login as a moderator
307 session_for(other_moderator_user)
309 # Check that the block edit page loads for moderators
310 get edit_user_block_path(active_block)
311 assert_response :success
312 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
313 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
314 assert_select "textarea#user_block_reason", :count => 1
315 assert_select "select#user_block_period", :count => 0
316 assert_select "input#user_block_needs_view[type='checkbox']", :count => 0
317 assert_select "input[type='submit'][value='Update block']", :count => 0
318 assert_select "input#user_block_period[type='hidden']", :count => 1
319 assert_select "input#user_block_needs_view[type='hidden']", :count => 1
320 assert_select "input[type='submit'][value='Revoke block']", :count => 1
323 # Login as the block creator
324 session_for(creator_user)
326 # Check that the block edit page loads for the creator
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 => 1
333 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
334 assert_select "input[type='submit'][value='Update block']", :count => 1
335 assert_select "input#user_block_period[type='hidden']", :count => 0
336 assert_select "input#user_block_needs_view[type='hidden']", :count => 0
337 assert_select "input[type='submit'][value='Revoke block']", :count => 0
340 # We should get an error if the user doesn't exist
341 get edit_user_block_path(99999)
342 assert_response :not_found
343 assert_template "not_found"
344 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
348 # test the edit action when the remaining block duration doesn't match the available select options
349 def test_edit_duration
350 moderator_user = create(:moderator_user)
353 active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours)
355 session_for(moderator_user)
356 get edit_user_block_path(active_block)
358 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
359 assert_select "select#user_block_period", :count => 1 do
360 assert_select "option[value='96'][selected]", :count => 1
365 get edit_user_block_path(active_block)
367 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
368 assert_select "select#user_block_period", :count => 1 do
369 assert_select "option[value='96'][selected]", :count => 1
376 # test the create action
378 target_user = create(:user)
379 moderator_user = create(:moderator_user)
381 # Not logged in yet, so creating a block should fail
382 post user_blocks_path
383 assert_response :forbidden
385 # Login as a normal user
386 session_for(create(:user))
388 # Check that normal users can't create blocks
389 post user_blocks_path
390 assert_redirected_to :controller => "errors", :action => "forbidden"
392 # Login as a moderator
393 session_for(moderator_user)
395 # A bogus block period should result in an error
396 assert_no_difference "UserBlock.count" do
397 post user_blocks_path(:display_name => target_user.display_name,
398 :user_block_period => "99")
400 assert_redirected_to new_user_block_path(target_user)
401 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
403 # Check that creating a block works
404 assert_difference "UserBlock.count", 1 do
405 post user_blocks_path(:display_name => target_user.display_name,
406 :user_block_period => "12",
407 :user_block => { :needs_view => false, :reason => "Vandalism" })
410 assert_redirected_to user_block_path(b)
411 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
412 assert_in_delta Time.now.utc, b.created_at, 1
413 assert_in_delta Time.now.utc, b.updated_at, 1
414 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
415 assert_not b.needs_view
416 assert_equal "Vandalism", b.reason
417 assert_equal "markdown", b.reason_format
418 assert_equal moderator_user.id, b.creator_id
420 # We should get an error if no user is specified
421 post user_blocks_path
422 assert_response :not_found
423 assert_template "users/no_such_user"
424 assert_select "h1", "The user does not exist"
426 # We should get an error if the user doesn't exist
427 post user_blocks_path(:display_name => "non_existent_user")
428 assert_response :not_found
429 assert_template "users/no_such_user"
430 assert_select "h1", "The user non_existent_user does not exist"
434 # test the duration of a created block
435 def test_create_duration
436 target_user = create(:user)
437 moderator_user = create(:moderator_user)
439 session_for(moderator_user)
440 post user_blocks_path(:display_name => target_user.display_name,
441 :user_block_period => "336",
442 :user_block => { :needs_view => false, :reason => "Vandalism" })
444 block = UserBlock.last
445 assert_equal 1209600, block.ends_at - block.created_at
449 # test the update action
451 moderator_user = create(:moderator_user)
452 active_block = create(:user_block, :creator => moderator_user)
454 # Not logged in yet, so updating a block should fail
455 put user_block_path(active_block)
456 assert_response :forbidden
458 # Login as a normal user
459 session_for(create(:user))
461 # Check that normal users can't update blocks
462 put user_block_path(active_block)
463 assert_redirected_to :controller => "errors", :action => "forbidden"
465 # Login as the moderator
466 session_for(moderator_user)
468 # A bogus block period should result in an error
469 assert_no_difference "UserBlock.count" do
470 put user_block_path(active_block, :user_block_period => "99")
472 assert_redirected_to edit_user_block_path(active_block)
473 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
475 # Check that updating a block works
476 assert_no_difference "UserBlock.count" do
477 put user_block_path(active_block,
478 :user_block_period => "12",
479 :user_block => { :needs_view => true, :reason => "Vandalism" })
481 assert_redirected_to user_block_path(active_block)
482 assert_equal "Block updated.", flash[:notice]
483 b = UserBlock.find(active_block.id)
484 assert_in_delta Time.now.utc, b.updated_at, 1
486 assert_equal "Vandalism", b.reason
488 # We should get an error if the block doesn't exist
489 put user_block_path(99999)
490 assert_response :not_found
491 assert_template "not_found"
492 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
496 # test the update action on expired blocks
497 def test_update_expired
498 creator_user = create(:moderator_user)
499 other_moderator_user = create(:moderator_user)
500 block = create(:user_block, :expired, :creator => creator_user, :reason => "Original Reason")
502 session_for(other_moderator_user)
503 put user_block_path(block,
504 :user_block_period => "0",
505 :user_block => { :needs_view => false, :reason => "Updated Reason" })
506 assert_redirected_to edit_user_block_path(block)
507 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
509 assert_not block.active?
510 assert_equal "Original Reason", block.reason
512 session_for(creator_user)
513 check_inactive_block_updates(block)
517 # test the update action on revoked blocks
518 def test_update_revoked
519 creator_user = create(:moderator_user)
520 revoker_user = create(:moderator_user)
521 other_moderator_user = create(:moderator_user)
522 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user, :reason => "Original Reason")
524 session_for(other_moderator_user)
525 put user_block_path(block,
526 :user_block_period => "0",
527 :user_block => { :needs_view => false, :reason => "Updated Reason" })
528 assert_redirected_to edit_user_block_path(block)
529 assert_equal "Only the moderators who created or revoked this block can edit it.", flash[:error]
531 assert_not_predicate block, :active?
532 assert_equal "Original Reason", block.reason
534 session_for(creator_user)
535 check_inactive_block_updates(block)
537 session_for(revoker_user)
538 check_inactive_block_updates(block)
542 # test the update action revoking the block
543 def test_revoke_using_update_by_creator
544 moderator_user = create(:moderator_user)
545 block = create(:user_block, :creator => moderator_user)
547 session_for(moderator_user)
548 put user_block_path(block,
549 :user_block_period => "24",
550 :user_block => { :needs_view => false, :reason => "Updated Reason" })
551 assert_redirected_to user_block_path(block)
552 assert_equal "Block updated.", flash[:notice]
554 assert_predicate block, :active?
555 assert_nil block.revoker
557 put user_block_path(block,
558 :user_block_period => "0",
559 :user_block => { :needs_view => false, :reason => "Updated Reason" })
560 assert_redirected_to user_block_path(block)
561 assert_equal "Block updated.", flash[:notice]
563 assert_not_predicate block, :active?
564 assert_equal moderator_user, block.revoker
567 def test_revoke_using_update_by_other_moderator
568 creator_user = create(:moderator_user)
569 other_moderator_user = create(:moderator_user)
570 block = create(:user_block, :creator => creator_user)
572 session_for(other_moderator_user)
573 put user_block_path(block,
574 :user_block_period => "24",
575 :user_block => { :needs_view => false, :reason => "Updated Reason" })
576 assert_response :success
577 assert_equal "Only the moderator who created this block can edit it without revoking.", flash[:error]
579 assert_predicate block, :active?
580 assert_nil block.revoker
582 put user_block_path(block,
583 :user_block_period => "0",
584 :user_block => { :needs_view => false, :reason => "Updated Reason" })
585 assert_redirected_to user_block_path(block)
586 assert_equal "Block updated.", flash[:notice]
588 assert_not_predicate block, :active?
589 assert_equal other_moderator_user, block.revoker
593 # test changes to end/deactivation dates
594 def test_dates_when_viewed_before_end
595 blocked_user = create(:user)
596 moderator_user = create(:moderator_user)
599 session_for(moderator_user)
600 assert_difference "UserBlock.count", 1 do
601 post user_blocks_path(:display_name => blocked_user.display_name,
602 :user_block_period => "48",
603 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
605 block = UserBlock.last
606 assert_equal Time.now.utc + 48.hours, block.ends_at
607 assert_nil block.deactivates_at
610 session_for(blocked_user)
611 get user_block_path(block)
613 assert_equal Time.now.utc + 24.hours, block.ends_at
614 assert_equal Time.now.utc + 24.hours, block.deactivates_at
618 def test_dates_when_viewed_after_end
619 blocked_user = create(:user)
620 moderator_user = create(:moderator_user)
623 session_for(moderator_user)
624 assert_difference "UserBlock.count", 1 do
625 post user_blocks_path(:display_name => blocked_user.display_name,
626 :user_block_period => "24",
627 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
629 block = UserBlock.last
630 assert_equal Time.now.utc + 24.hours, block.ends_at
631 assert_nil block.deactivates_at
634 session_for(blocked_user)
635 get user_block_path(block)
637 assert_equal Time.now.utc - 24.hours, block.ends_at
638 assert_equal Time.now.utc, block.deactivates_at
642 def test_dates_when_edited_before_end
643 blocked_user = create(:user)
644 moderator_user = create(:moderator_user)
647 session_for(moderator_user)
648 assert_difference "UserBlock.count", 1 do
649 post user_blocks_path(:display_name => blocked_user.display_name,
650 :user_block_period => "48",
651 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
653 block = UserBlock.last
654 assert_equal Time.now.utc + 48.hours, block.ends_at
655 assert_equal Time.now.utc + 48.hours, block.deactivates_at
658 put user_block_path(block,
659 :user_block_period => "48",
660 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
662 assert_equal Time.now.utc + 48.hours, block.ends_at
663 assert_equal Time.now.utc + 48.hours, block.deactivates_at
667 def test_dates_when_edited_after_end
668 blocked_user = create(:user)
669 moderator_user = create(:moderator_user)
672 session_for(moderator_user)
673 assert_difference "UserBlock.count", 1 do
674 post user_blocks_path(:display_name => blocked_user.display_name,
675 :user_block_period => "24",
676 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
678 block = UserBlock.last
679 assert_equal Time.now.utc + 24.hours, block.ends_at
680 assert_equal Time.now.utc + 24.hours, block.deactivates_at
683 put user_block_path(block,
684 :user_block_period => "0",
685 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
687 assert_equal Time.now.utc - 24.hours, block.ends_at
688 assert_equal Time.now.utc - 24.hours, block.deactivates_at
693 # test updates on legacy records without correctly initialized deactivates_at
694 def test_update_legacy_deactivates_at
695 blocked_user = create(:user)
696 moderator_user = create(:moderator_user)
699 block = UserBlock.new :user => blocked_user,
700 :creator => moderator_user,
701 :reason => "because",
702 :ends_at => Time.now.utc + 24.hours,
705 assert_difference "UserBlock.count", 1 do
706 block.save :validate => false
710 session_for(moderator_user)
711 put user_block_path(block,
712 :user_block_period => "0",
713 :user_block => { :needs_view => false, :reason => "Testing legacy block update" })
715 assert_equal Time.now.utc - 24.hours, block.ends_at
716 assert_equal Time.now.utc - 24.hours, block.deactivates_at
722 def check_block_buttons(block, edit: 0)
723 [user_blocks_path, user_block_path(block)].each do |path|
725 assert_response :success
726 assert_select "a[href='#{edit_user_block_path block}']", :count => edit
730 def check_inactive_block_updates(block)
731 original_ends_at = block.ends_at
733 put user_block_path(block,
734 :user_block_period => "0",
735 :user_block => { :needs_view => false, :reason => "Updated Reason" })
736 assert_redirected_to user_block_path(block)
737 assert_equal "Block updated.", flash[:notice]
739 assert_not_predicate block, :active?
740 assert_equal "Updated Reason", block.reason
741 assert_equal original_ends_at, block.ends_at
743 put user_block_path(block,
744 :user_block_period => "0",
745 :user_block => { :needs_view => true, :reason => "Updated Reason Needs View" })
746 assert_response :success
747 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
749 assert_not_predicate block, :active?
750 assert_equal "Updated Reason", block.reason
751 assert_equal original_ends_at, block.ends_at
753 put user_block_path(block,
754 :user_block_period => "1",
755 :user_block => { :needs_view => false, :reason => "Updated Reason Duration Extended" })
756 assert_response :success
757 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
759 assert_not_predicate block, :active?
760 assert_equal "Updated Reason", block.reason
761 assert_equal original_ends_at, block.ends_at
763 put user_block_path(block,
764 :user_block_period => "0",
765 :user_block => { :needs_view => false, :reason => "Updated Reason Again" })
766 assert_redirected_to user_block_path(block)
767 assert_equal "Block updated.", flash[:notice]
769 assert_not_predicate block, :active?
770 assert_equal "Updated Reason Again", block.reason
771 assert_equal original_ends_at, block.ends_at