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" }
37 { :path => "/blocks/1/revoke", :method => :get },
38 { :controller => "user_blocks", :action => "revoke", :id => "1" }
41 { :path => "/blocks/1/revoke", :method => :post },
42 { :controller => "user_blocks", :action => "revoke", :id => "1" }
46 { :path => "/user/username/blocks", :method => :get },
47 { :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
50 { :path => "/user/username/blocks_by", :method => :get },
51 { :controller => "user_blocks", :action => "blocks_by", :display_name => "username" }
54 { :path => "/user/username/blocks/revoke_all", :method => :get },
55 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
58 { :path => "/user/username/blocks/revoke_all", :method => :post },
59 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
64 # test the index action
66 revoked_block = create(:user_block, :revoked)
69 assert_response :success
70 assert_select "table#block_list tbody tr", :count => 1 do
71 assert_select "a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
72 assert_select "a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
73 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
76 active_block = create(:user_block)
77 expired_block = create(:user_block, :expired)
80 assert_response :success
81 assert_select "table#block_list tbody", :count => 1 do
83 assert_select "a[href='#{user_block_path(active_block)}']", 1
84 assert_select "a[href='#{user_block_path(expired_block)}']", 1
85 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
90 # test the index action with multiple pages
92 user_blocks = create_list(:user_block, 50).reverse
93 next_path = user_blocks_path
96 assert_response :success
97 check_user_blocks_table user_blocks[0...20]
98 check_no_page_link "Newer Blocks"
99 next_path = check_page_link "Older Blocks"
102 assert_response :success
103 check_user_blocks_table user_blocks[20...40]
104 check_page_link "Newer Blocks"
105 next_path = check_page_link "Older Blocks"
108 assert_response :success
109 check_user_blocks_table user_blocks[40...50]
110 check_page_link "Newer Blocks"
111 check_no_page_link "Older Blocks"
115 # test the index action with invalid pages
116 def test_index_invalid_paged
117 %w[-1 0 fred].each do |id|
118 get user_blocks_path(:before => id)
119 assert_redirected_to :controller => :errors, :action => :bad_request
121 get user_blocks_path(:after => id)
122 assert_redirected_to :controller => :errors, :action => :bad_request
127 # test the show action
129 active_block = create(:user_block, :needs_view)
130 expired_block = create(:user_block, :expired)
131 revoked_block = create(:user_block, :revoked)
133 # Viewing a block should fail when a bogus ID is given
134 get user_block_path(:id => 99999)
135 assert_response :not_found
136 assert_template "not_found"
137 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
139 # Viewing an expired block should work
140 get user_block_path(:id => expired_block)
141 assert_response :success
142 assert_select "h1 a[href='#{user_path expired_block.user}']", :text => expired_block.user.display_name
143 assert_select "h1 a[href='#{user_path expired_block.creator}']", :text => expired_block.creator.display_name
145 # Viewing a revoked block should work
146 get user_block_path(:id => revoked_block)
147 assert_response :success
148 assert_select "h1 a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
149 assert_select "h1 a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
150 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
152 # Viewing an active block should work, but shouldn't mark it as seen
153 get user_block_path(:id => active_block)
154 assert_response :success
155 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
156 assert_select "h1 a[href='#{user_path active_block.creator}']", :text => active_block.creator.display_name
157 assert UserBlock.find(active_block.id).needs_view
159 # Login as the blocked user
160 session_for(active_block.user)
162 # Now viewing it should mark it as seen
163 get user_block_path(:id => active_block)
164 assert_response :success
165 assert_not UserBlock.find(active_block.id).needs_view
169 # test edit/revoke link for active blocks
170 def test_active_block_buttons
171 creator_user = create(:moderator_user)
172 other_moderator_user = create(:moderator_user)
173 block = create(:user_block, :creator => creator_user)
175 session_for(other_moderator_user)
176 check_block_buttons block, :edit => 1, :revoke => 1
178 session_for(creator_user)
179 check_block_buttons block, :edit => 1, :revoke => 1
183 # test the edit link for expired blocks
184 def test_expired_block_buttons
185 creator_user = create(:moderator_user)
186 other_moderator_user = create(:moderator_user)
187 block = create(:user_block, :expired, :creator => creator_user)
189 session_for(other_moderator_user)
190 check_block_buttons block
192 session_for(creator_user)
193 check_block_buttons block, :edit => 1
197 # test the edit link for revoked blocks
198 def test_revoked_block_buttons
199 creator_user = create(:moderator_user)
200 revoker_user = create(:moderator_user)
201 other_moderator_user = create(:moderator_user)
202 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user)
204 session_for(other_moderator_user)
205 check_block_buttons block
207 session_for(creator_user)
208 check_block_buttons block, :edit => 1
210 session_for(revoker_user)
211 check_block_buttons block, :edit => 1
215 # test the new action
217 target_user = create(:user)
219 # Check that the block creation page requires us to login
220 get new_user_block_path(target_user)
221 assert_redirected_to login_path(:referer => new_user_block_path(target_user))
223 # Login as a normal user
224 session_for(create(:user))
226 # Check that normal users can't load the block creation page
227 get new_user_block_path(target_user)
228 assert_redirected_to :controller => "errors", :action => "forbidden"
230 # Login as a moderator
231 session_for(create(:moderator_user))
233 # Check that the block creation page loads for moderators
234 get new_user_block_path(target_user)
235 assert_response :success
236 assert_select "h1 a[href='#{user_path target_user}']", :text => target_user.display_name
237 assert_select "form#new_user_block", :count => 1 do
238 assert_select "textarea#user_block_reason", :count => 1
239 assert_select "select#user_block_period", :count => 1
240 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
241 assert_select "input#display_name[type='hidden']", :count => 1
242 assert_select "input[type='submit'][value='Create block']", :count => 1
245 # We should get an error if the user doesn't exist
246 get new_user_block_path(:display_name => "non_existent_user")
247 assert_response :not_found
248 assert_template "users/no_such_user"
249 assert_select "h1", "The user non_existent_user does not exist"
253 # test the edit action
255 creator_user = create(:moderator_user)
256 other_moderator_user = create(:moderator_user)
257 active_block = create(:user_block, :creator => creator_user)
259 # Check that the block edit page requires us to login
260 get edit_user_block_path(:id => active_block)
261 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
263 # Login as a normal user
264 session_for(create(:user))
266 # Check that normal users can't load the block edit page
267 get edit_user_block_path(:id => active_block)
268 assert_redirected_to :controller => "errors", :action => "forbidden"
270 # Login as a moderator
271 session_for(other_moderator_user)
273 # Check that the block edit page loads for moderators
274 get edit_user_block_path(:id => active_block)
275 assert_response :success
276 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
277 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
278 assert_select "textarea#user_block_reason", :count => 1
279 assert_select "select#user_block_period", :count => 0
280 assert_select "input#user_block_needs_view[type='checkbox']", :count => 0
281 assert_select "input#user_block_period[type='hidden']", :count => 1
282 assert_select "input#user_block_needs_view[type='hidden']", :count => 1
283 assert_select "input[type='submit'][value='Update block']", :count => 1
286 # Login as the block creator
287 session_for(creator_user)
289 # Check that the block edit page loads for the creator
290 get edit_user_block_path(:id => active_block)
291 assert_response :success
292 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
293 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
294 assert_select "textarea#user_block_reason", :count => 1
295 assert_select "select#user_block_period", :count => 1
296 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
297 assert_select "input#user_block_period[type='hidden']", :count => 0
298 assert_select "input#user_block_needs_view[type='hidden']", :count => 0
299 assert_select "input[type='submit'][value='Update block']", :count => 1
302 # We should get an error if the user doesn't exist
303 get edit_user_block_path(:id => 99999)
304 assert_response :not_found
305 assert_template "not_found"
306 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
310 # test the edit action when the remaining block duration doesn't match the available select options
311 def test_edit_duration
312 moderator_user = create(:moderator_user)
315 active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours)
317 session_for(moderator_user)
318 get edit_user_block_path(active_block)
320 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
321 assert_select "select#user_block_period", :count => 1 do
322 assert_select "option[value='96'][selected]", :count => 1
327 get edit_user_block_path(active_block)
329 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
330 assert_select "select#user_block_period", :count => 1 do
331 assert_select "option[value='96'][selected]", :count => 1
338 # test the create action
340 target_user = create(:user)
341 moderator_user = create(:moderator_user)
343 # Not logged in yet, so creating a block should fail
344 post user_blocks_path
345 assert_response :forbidden
347 # Login as a normal user
348 session_for(create(:user))
350 # Check that normal users can't create blocks
351 post user_blocks_path
352 assert_redirected_to :controller => "errors", :action => "forbidden"
354 # Login as a moderator
355 session_for(moderator_user)
357 # A bogus block period should result in an error
358 assert_no_difference "UserBlock.count" do
359 post user_blocks_path(:display_name => target_user.display_name,
360 :user_block_period => "99")
362 assert_redirected_to new_user_block_path(target_user)
363 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
365 # Check that creating a block works
366 assert_difference "UserBlock.count", 1 do
367 post user_blocks_path(:display_name => target_user.display_name,
368 :user_block_period => "12",
369 :user_block => { :needs_view => false, :reason => "Vandalism" })
372 assert_redirected_to user_block_path(:id => b.id)
373 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
374 assert_in_delta Time.now.utc, b.created_at, 1
375 assert_in_delta Time.now.utc, b.updated_at, 1
376 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
377 assert_not b.needs_view
378 assert_equal "Vandalism", b.reason
379 assert_equal "markdown", b.reason_format
380 assert_equal moderator_user.id, b.creator_id
382 # We should get an error if no user is specified
383 post user_blocks_path
384 assert_response :not_found
385 assert_template "users/no_such_user"
386 assert_select "h1", "The user does not exist"
388 # We should get an error if the user doesn't exist
389 post user_blocks_path(:display_name => "non_existent_user")
390 assert_response :not_found
391 assert_template "users/no_such_user"
392 assert_select "h1", "The user non_existent_user does not exist"
396 # test the duration of a created block
397 def test_create_duration
398 target_user = create(:user)
399 moderator_user = create(:moderator_user)
401 session_for(moderator_user)
402 post user_blocks_path(:display_name => target_user.display_name,
403 :user_block_period => "336",
404 :user_block => { :needs_view => false, :reason => "Vandalism" })
406 block = UserBlock.last
407 assert_equal 1209600, block.ends_at - block.created_at
411 # test the update action
413 moderator_user = create(:moderator_user)
414 active_block = create(:user_block, :creator => moderator_user)
416 # Not logged in yet, so updating a block should fail
417 put user_block_path(:id => active_block)
418 assert_response :forbidden
420 # Login as a normal user
421 session_for(create(:user))
423 # Check that normal users can't update blocks
424 put user_block_path(:id => active_block)
425 assert_redirected_to :controller => "errors", :action => "forbidden"
427 # Login as the moderator
428 session_for(moderator_user)
430 # A bogus block period should result in an error
431 assert_no_difference "UserBlock.count" do
432 put user_block_path(:id => active_block, :user_block_period => "99")
434 assert_redirected_to edit_user_block_path(active_block)
435 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
437 # Check that updating a block works
438 assert_no_difference "UserBlock.count" do
439 put user_block_path(:id => active_block,
440 :user_block_period => "12",
441 :user_block => { :needs_view => true, :reason => "Vandalism" })
443 assert_redirected_to user_block_path(active_block)
444 assert_equal "Block updated.", flash[:notice]
445 b = UserBlock.find(active_block.id)
446 assert_in_delta Time.now.utc, b.updated_at, 1
448 assert_equal "Vandalism", b.reason
450 # We should get an error if the block doesn't exist
451 put user_block_path(:id => 99999)
452 assert_response :not_found
453 assert_template "not_found"
454 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
458 # test the update action on expired blocks
459 def test_update_expired
460 creator_user = create(:moderator_user)
461 other_moderator_user = create(:moderator_user)
462 block = create(:user_block, :expired, :creator => creator_user, :reason => "Original Reason")
464 session_for(other_moderator_user)
465 put user_block_path(block,
466 :user_block_period => "0",
467 :user_block => { :needs_view => false, :reason => "Updated Reason" })
468 assert_redirected_to edit_user_block_path(block)
469 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
471 assert_not block.active?
472 assert_equal "Original Reason", block.reason
474 session_for(creator_user)
475 check_inactive_block_updates(block)
479 # test the update action on revoked blocks
480 def test_update_revoked
481 creator_user = create(:moderator_user)
482 revoker_user = create(:moderator_user)
483 other_moderator_user = create(:moderator_user)
484 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user, :reason => "Original Reason")
486 session_for(other_moderator_user)
487 put user_block_path(block,
488 :user_block_period => "0",
489 :user_block => { :needs_view => false, :reason => "Updated Reason" })
490 assert_redirected_to edit_user_block_path(block)
491 assert_equal "Only the moderators who created or revoked this block can edit it.", flash[:error]
493 assert_not_predicate block, :active?
494 assert_equal "Original Reason", block.reason
496 session_for(creator_user)
497 check_inactive_block_updates(block)
499 session_for(revoker_user)
500 check_inactive_block_updates(block)
504 # test the update action revoking the block
505 def test_revoke_using_update_by_creator
506 moderator_user = create(:moderator_user)
507 block = create(:user_block, :creator => moderator_user)
509 session_for(moderator_user)
510 put user_block_path(block,
511 :user_block_period => "24",
512 :user_block => { :needs_view => false, :reason => "Updated Reason" })
513 assert_redirected_to user_block_path(block)
514 assert_equal "Block updated.", flash[:notice]
516 assert_predicate block, :active?
517 assert_nil block.revoker
519 put user_block_path(block,
520 :user_block_period => "0",
521 :user_block => { :needs_view => false, :reason => "Updated Reason" })
522 assert_redirected_to user_block_path(block)
523 assert_equal "Block updated.", flash[:notice]
525 assert_not_predicate block, :active?
526 assert_equal moderator_user, block.revoker
529 def test_revoke_using_update_by_other_moderator
530 creator_user = create(:moderator_user)
531 other_moderator_user = create(:moderator_user)
532 block = create(:user_block, :creator => creator_user)
534 session_for(other_moderator_user)
535 put user_block_path(block,
536 :user_block_period => "24",
537 :user_block => { :needs_view => false, :reason => "Updated Reason" })
538 assert_response :success
539 assert_equal "Only the moderator who created this block can edit it without revoking.", flash[:error]
541 assert_predicate block, :active?
542 assert_nil block.revoker
544 put user_block_path(block,
545 :user_block_period => "0",
546 :user_block => { :needs_view => false, :reason => "Updated Reason" })
547 assert_redirected_to user_block_path(block)
548 assert_equal "Block updated.", flash[:notice]
550 assert_not_predicate block, :active?
551 assert_equal other_moderator_user, block.revoker
555 # test the revoke action
557 active_block = create(:user_block)
559 # Check that the block revoke page requires us to login
560 get revoke_user_block_path(:id => active_block)
561 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
563 # Login as a normal user
564 session_for(create(:user))
566 # Check that normal users can't load the block revoke page
567 get revoke_user_block_path(:id => active_block)
568 assert_redirected_to :controller => "errors", :action => "forbidden"
570 # Login as a moderator
571 session_for(create(:moderator_user))
573 # Check that the block revoke page loads for moderators
574 get revoke_user_block_path(:id => active_block)
575 assert_response :success
576 assert_template "revoke"
577 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
578 assert_select "form", :count => 1 do
579 assert_select "input#confirm[type='checkbox']", :count => 1
580 assert_select "input[type='submit'][value='Revoke!']", :count => 1
583 # Check that revoking a block using GET should fail
584 get revoke_user_block_path(:id => active_block, :confirm => true)
585 assert_response :success
586 assert_template "revoke"
587 b = UserBlock.find(active_block.id)
588 assert_operator b.ends_at - Time.now.utc, :>, 100
590 # Check that revoking a block works using POST
591 post revoke_user_block_path(:id => active_block, :confirm => true)
592 assert_redirected_to user_block_path(active_block)
593 b = UserBlock.find(active_block.id)
594 assert_in_delta Time.now.utc, b.ends_at, 1
596 # We should get an error if the block doesn't exist
597 get revoke_user_block_path(:id => 99999)
598 assert_response :not_found
599 assert_template "not_found"
600 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
604 # test the revoke all page
605 def test_revoke_all_page
606 blocked_user = create(:user)
607 create(:user_block, :user => blocked_user)
609 # Asking for the revoke all blocks page with a bogus user name should fail
610 get user_blocks_on_path("non_existent_user")
611 assert_response :not_found
613 # Check that the revoke all blocks page requires us to login
614 get revoke_all_user_blocks_path(blocked_user)
615 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
617 # Login as a normal user
618 session_for(create(:user))
620 # Check that normal users can't load the revoke all blocks page
621 get revoke_all_user_blocks_path(blocked_user)
622 assert_redirected_to :controller => "errors", :action => "forbidden"
624 # Login as a moderator
625 session_for(create(:moderator_user))
627 # Check that the revoke all blocks page loads for moderators
628 get revoke_all_user_blocks_path(blocked_user)
629 assert_response :success
630 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
634 # test the revoke all action
635 def test_revoke_all_action
636 blocked_user = create(:user)
637 active_block1 = create(:user_block, :user => blocked_user)
638 active_block2 = create(:user_block, :user => blocked_user)
639 expired_block1 = create(:user_block, :expired, :user => blocked_user)
640 blocks = [active_block1, active_block2, expired_block1]
641 moderator_user = create(:moderator_user)
643 assert_predicate active_block1, :active?
644 assert_predicate active_block2, :active?
645 assert_not_predicate expired_block1, :active?
647 # Login as a normal user
648 session_for(create(:user))
650 # Check that normal users can't load the block revoke page
651 get revoke_all_user_blocks_path(:blocked_user)
652 assert_redirected_to :controller => "errors", :action => "forbidden"
654 # Login as a moderator
655 session_for(moderator_user)
657 # Check that revoking blocks using GET should fail
658 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
659 assert_response :success
660 assert_template "revoke_all"
662 blocks.each(&:reload)
663 assert_predicate active_block1, :active?
664 assert_predicate active_block2, :active?
665 assert_not_predicate expired_block1, :active?
667 # Check that revoking blocks works using POST
668 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
669 assert_redirected_to user_blocks_on_path(blocked_user)
671 blocks.each(&:reload)
672 assert_not_predicate active_block1, :active?
673 assert_not_predicate active_block2, :active?
674 assert_not_predicate expired_block1, :active?
675 assert_equal moderator_user, active_block1.revoker
676 assert_equal moderator_user, active_block2.revoker
677 assert_not_equal moderator_user, expired_block1.revoker
681 # test changes to end/deactivation dates
682 def test_dates_when_viewed_before_end
683 blocked_user = create(:user)
684 moderator_user = create(:moderator_user)
687 session_for(moderator_user)
688 assert_difference "UserBlock.count", 1 do
689 post user_blocks_path(:display_name => blocked_user.display_name,
690 :user_block_period => "48",
691 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
693 block = UserBlock.last
694 assert_equal Time.now.utc + 2.days, block.ends_at
695 assert_nil block.deactivates_at
698 session_for(blocked_user)
699 get user_block_path(block)
701 assert_equal Time.now.utc + 1.day, block.ends_at
702 assert_equal Time.now.utc + 1.day, block.deactivates_at
706 def test_dates_when_viewed_after_end
707 blocked_user = create(:user)
708 moderator_user = create(:moderator_user)
711 session_for(moderator_user)
712 assert_difference "UserBlock.count", 1 do
713 post user_blocks_path(:display_name => blocked_user.display_name,
714 :user_block_period => "24",
715 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
717 block = UserBlock.last
718 assert_equal Time.now.utc + 1.day, block.ends_at
719 assert_nil block.deactivates_at
722 session_for(blocked_user)
723 get user_block_path(block)
725 assert_equal Time.now.utc - 1.day, block.ends_at
726 assert_equal Time.now.utc, block.deactivates_at
730 def test_dates_when_edited_before_end
731 blocked_user = create(:user)
732 moderator_user = create(:moderator_user)
735 session_for(moderator_user)
736 assert_difference "UserBlock.count", 1 do
737 post user_blocks_path(:display_name => blocked_user.display_name,
738 :user_block_period => "48",
739 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
741 block = UserBlock.last
742 assert_equal Time.now.utc + 2.days, block.ends_at
743 assert_equal Time.now.utc + 2.days, block.deactivates_at
746 put user_block_path(block,
747 :user_block_period => "48",
748 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
750 assert_equal Time.now.utc + 2.days, block.ends_at
751 assert_equal Time.now.utc + 2.days, block.deactivates_at
755 def test_dates_when_edited_after_end
756 blocked_user = create(:user)
757 moderator_user = create(:moderator_user)
760 session_for(moderator_user)
761 assert_difference "UserBlock.count", 1 do
762 post user_blocks_path(:display_name => blocked_user.display_name,
763 :user_block_period => "24",
764 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
766 block = UserBlock.last
767 assert_equal Time.now.utc + 1.day, block.ends_at
768 assert_equal Time.now.utc + 1.day, block.deactivates_at
771 put user_block_path(block,
772 :user_block_period => "0",
773 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
775 assert_equal Time.now.utc - 1.day, block.ends_at
776 assert_equal Time.now.utc - 1.day, block.deactivates_at
781 # test updates on legacy records without correctly initialized deactivates_at
782 def test_update_legacy_deactivates_at
783 blocked_user = create(:user)
784 moderator_user = create(:moderator_user)
787 block = UserBlock.new :user => blocked_user,
788 :creator => moderator_user,
789 :reason => "because",
790 :ends_at => Time.now.utc + 1.day,
793 assert_difference "UserBlock.count", 1 do
794 block.save :validate => false
798 session_for(moderator_user)
799 put user_block_path(block,
800 :user_block_period => "0",
801 :user_block => { :needs_view => false, :reason => "Testing legacy block update" })
803 assert_equal Time.now.utc - 1.day, block.ends_at
804 assert_equal Time.now.utc - 1.day, block.deactivates_at
809 # test the blocks_on action
811 blocked_user = create(:user)
812 unblocked_user = create(:user)
813 normal_user = create(:user)
814 active_block = create(:user_block, :user => blocked_user)
815 revoked_block = create(:user_block, :revoked, :user => blocked_user)
816 expired_block = create(:user_block, :expired, :user => unblocked_user)
818 # Asking for a list of blocks with a bogus user name should fail
819 get user_blocks_on_path("non_existent_user")
820 assert_response :not_found
821 assert_template "users/no_such_user"
822 assert_select "h1", "The user non_existent_user does not exist"
824 # Check the list of blocks for a user that has never been blocked
825 get user_blocks_on_path(normal_user)
826 assert_response :success
827 assert_select "table#block_list", false
828 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
830 # Check the list of blocks for a user that is currently blocked
831 get user_blocks_on_path(blocked_user)
832 assert_response :success
833 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
834 assert_select "table#block_list tbody", :count => 1 do
835 assert_select "tr", 2
836 assert_select "a[href='#{user_block_path(active_block)}']", 1
837 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
840 # Check the list of blocks for a user that has previously been blocked
841 get user_blocks_on_path(unblocked_user)
842 assert_response :success
843 assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
844 assert_select "table#block_list tbody", :count => 1 do
845 assert_select "tr", 1
846 assert_select "a[href='#{user_block_path(expired_block)}']", 1
851 # test the blocks_on action with multiple pages
852 def test_blocks_on_paged
854 user_blocks = create_list(:user_block, 50, :user => user).reverse
855 next_path = user_blocks_on_path(user)
858 assert_response :success
859 check_user_blocks_table user_blocks[0...20]
860 check_no_page_link "Newer Blocks"
861 next_path = check_page_link "Older Blocks"
864 assert_response :success
865 check_user_blocks_table user_blocks[20...40]
866 check_page_link "Newer Blocks"
867 next_path = check_page_link "Older Blocks"
870 assert_response :success
871 check_user_blocks_table user_blocks[40...50]
872 check_page_link "Newer Blocks"
873 check_no_page_link "Older Blocks"
877 # test the blocks_on action with invalid pages
878 def test_blocks_on_invalid_paged
881 %w[-1 0 fred].each do |id|
882 get user_blocks_on_path(user, :before => id)
883 assert_redirected_to :controller => :errors, :action => :bad_request
885 get user_blocks_on_path(user, :after => id)
886 assert_redirected_to :controller => :errors, :action => :bad_request
891 # test the blocks_by action
893 moderator_user = create(:moderator_user)
894 second_moderator_user = create(:moderator_user)
895 normal_user = create(:user)
896 active_block = create(:user_block, :creator => moderator_user)
897 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
898 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
900 # Asking for a list of blocks with a bogus user name should fail
901 get user_blocks_by_path("non_existent_user")
902 assert_response :not_found
903 assert_template "users/no_such_user"
904 assert_select "h1", "The user non_existent_user does not exist"
906 # Check the list of blocks given by one moderator
907 get user_blocks_by_path(moderator_user)
908 assert_response :success
909 assert_select "h1 a[href='#{user_path moderator_user}']", :text => moderator_user.display_name
910 assert_select "table#block_list tbody", :count => 1 do
911 assert_select "tr", 1
912 assert_select "a[href='#{user_block_path(active_block)}']", 1
915 # Check the list of blocks given by a different moderator
916 get user_blocks_by_path(second_moderator_user)
917 assert_response :success
918 assert_select "h1 a[href='#{user_path second_moderator_user}']", :text => second_moderator_user.display_name
919 assert_select "table#block_list tbody", :count => 1 do
920 assert_select "tr", 2
921 assert_select "a[href='#{user_block_path(expired_block)}']", 1
922 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
925 # Check the list of blocks (not) given by a normal user
926 get user_blocks_by_path(normal_user)
927 assert_response :success
928 assert_select "table#block_list", false
929 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
933 # test the blocks_by action with multiple pages
934 def test_blocks_by_paged
935 user = create(:moderator_user)
936 user_blocks = create_list(:user_block, 50, :creator => user).reverse
937 next_path = user_blocks_by_path(user)
940 assert_response :success
941 check_user_blocks_table user_blocks[0...20]
942 check_no_page_link "Newer Blocks"
943 next_path = check_page_link "Older Blocks"
946 assert_response :success
947 check_user_blocks_table user_blocks[20...40]
948 check_page_link "Newer Blocks"
949 next_path = check_page_link "Older Blocks"
952 assert_response :success
953 check_user_blocks_table user_blocks[40...50]
954 check_page_link "Newer Blocks"
955 check_no_page_link "Older Blocks"
959 # test the blocks_by action with invalid pages
960 def test_blocks_by_invalid_paged
961 user = create(:moderator_user)
963 %w[-1 0 fred].each do |id|
964 get user_blocks_by_path(user, :before => id)
965 assert_redirected_to :controller => :errors, :action => :bad_request
967 get user_blocks_by_path(user, :after => id)
968 assert_redirected_to :controller => :errors, :action => :bad_request
974 def check_block_buttons(block, edit: 0, revoke: 0)
975 [user_blocks_path, user_block_path(block)].each do |path|
977 assert_response :success
978 assert_select "a[href='#{edit_user_block_path block}']", :count => edit
979 assert_select "a[href='#{revoke_user_block_path block}']", :count => revoke
983 def check_inactive_block_updates(block)
984 original_ends_at = block.ends_at
986 put user_block_path(block,
987 :user_block_period => "0",
988 :user_block => { :needs_view => false, :reason => "Updated Reason" })
989 assert_redirected_to user_block_path(block)
990 assert_equal "Block updated.", flash[:notice]
992 assert_not_predicate block, :active?
993 assert_equal "Updated Reason", block.reason
994 assert_equal original_ends_at, block.ends_at
996 put user_block_path(block,
997 :user_block_period => "0",
998 :user_block => { :needs_view => true, :reason => "Updated Reason Needs View" })
999 assert_response :success
1000 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
1002 assert_not_predicate block, :active?
1003 assert_equal "Updated Reason", block.reason
1004 assert_equal original_ends_at, block.ends_at
1006 put user_block_path(block,
1007 :user_block_period => "1",
1008 :user_block => { :needs_view => false, :reason => "Updated Reason Duration Extended" })
1009 assert_response :success
1010 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
1012 assert_not_predicate block, :active?
1013 assert_equal "Updated Reason", block.reason
1014 assert_equal original_ends_at, block.ends_at
1016 put user_block_path(block,
1017 :user_block_period => "0",
1018 :user_block => { :needs_view => false, :reason => "Updated Reason Again" })
1019 assert_redirected_to user_block_path(block)
1020 assert_equal "Block updated.", flash[:notice]
1022 assert_not_predicate block, :active?
1023 assert_equal "Updated Reason Again", block.reason
1024 assert_equal original_ends_at, block.ends_at
1027 def check_user_blocks_table(user_blocks)
1028 assert_dom "table#block_list tbody tr" do |rows|
1029 assert_equal user_blocks.count, rows.count, "unexpected number of rows in user blocks table"
1030 rows.zip(user_blocks).map do |row, user_block|
1031 assert_dom row, "a[href='#{user_block_path user_block}']", 1
1036 def check_no_page_link(name)
1037 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
1040 def check_page_link(name)
1041 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
1042 return buttons.first.attributes["href"].value