3 class UserBlocksControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/blocks/new/username", :method => :get },
9 { :controller => "user_blocks", :action => "new", :display_name => "username" }
13 { :path => "/user_blocks", :method => :get },
14 { :controller => "user_blocks", :action => "index" }
17 { :path => "/user_blocks", :method => :post },
18 { :controller => "user_blocks", :action => "create" }
21 { :path => "/user_blocks/1", :method => :get },
22 { :controller => "user_blocks", :action => "show", :id => "1" }
25 { :path => "/user_blocks/1/edit", :method => :get },
26 { :controller => "user_blocks", :action => "edit", :id => "1" }
29 { :path => "/user_blocks/1", :method => :put },
30 { :controller => "user_blocks", :action => "update", :id => "1" }
33 { :path => "/user_blocks/1", :method => :delete },
34 { :controller => "user_blocks", :action => "destroy", :id => "1" }
38 { :path => "/user/username/blocks", :method => :get },
39 { :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
42 { :path => "/user/username/blocks_by", :method => :get },
43 { :controller => "user_blocks", :action => "blocks_by", :display_name => "username" }
46 { :path => "/user/username/blocks/revoke_all", :method => :get },
47 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
50 { :path => "/user/username/blocks/revoke_all", :method => :post },
51 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
56 # test the index action
58 revoked_block = create(:user_block, :revoked)
61 assert_response :success
62 assert_select "table#block_list tbody tr", :count => 1 do
63 assert_select "a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
64 assert_select "a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
65 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
68 active_block = create(:user_block)
69 expired_block = create(:user_block, :expired)
72 assert_response :success
73 assert_select "table#block_list tbody", :count => 1 do
75 assert_select "a[href='#{user_block_path(active_block)}']", 1
76 assert_select "a[href='#{user_block_path(expired_block)}']", 1
77 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
82 # test the index action with multiple pages
84 user_blocks = create_list(:user_block, 50).reverse
85 next_path = user_blocks_path
88 assert_response :success
89 check_user_blocks_table user_blocks[0...20]
90 check_no_page_link "Newer Blocks"
91 next_path = check_page_link "Older Blocks"
94 assert_response :success
95 check_user_blocks_table user_blocks[20...40]
96 check_page_link "Newer Blocks"
97 next_path = check_page_link "Older Blocks"
100 assert_response :success
101 check_user_blocks_table user_blocks[40...50]
102 check_page_link "Newer Blocks"
103 check_no_page_link "Older Blocks"
107 # test the index action with invalid pages
108 def test_index_invalid_paged
109 %w[-1 0 fred].each do |id|
110 get user_blocks_path(:before => id)
111 assert_redirected_to :controller => :errors, :action => :bad_request
113 get user_blocks_path(:after => id)
114 assert_redirected_to :controller => :errors, :action => :bad_request
119 # test the show action
121 active_block = create(:user_block, :needs_view)
122 expired_block = create(:user_block, :expired)
123 revoked_block = create(:user_block, :revoked)
125 # Viewing a block should fail when a bogus ID is given
126 get user_block_path(:id => 99999)
127 assert_response :not_found
128 assert_template "not_found"
129 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
131 # Viewing an expired block should work
132 get user_block_path(:id => expired_block)
133 assert_response :success
134 assert_select "h1 a[href='#{user_path expired_block.user}']", :text => expired_block.user.display_name
135 assert_select "h1 a[href='#{user_path expired_block.creator}']", :text => expired_block.creator.display_name
137 # Viewing a revoked block should work
138 get user_block_path(:id => revoked_block)
139 assert_response :success
140 assert_select "h1 a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
141 assert_select "h1 a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
142 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
144 # Viewing an active block should work, but shouldn't mark it as seen
145 get user_block_path(:id => active_block)
146 assert_response :success
147 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
148 assert_select "h1 a[href='#{user_path active_block.creator}']", :text => active_block.creator.display_name
149 assert UserBlock.find(active_block.id).needs_view
151 # Login as the blocked user
152 session_for(active_block.user)
154 # Now viewing it should mark it as seen
155 get user_block_path(:id => active_block)
156 assert_response :success
157 assert_not UserBlock.find(active_block.id).needs_view
161 # test edit/revoke link for active blocks
162 def test_active_block_buttons
163 creator_user = create(:moderator_user)
164 other_moderator_user = create(:moderator_user)
165 block = create(:user_block, :creator => creator_user)
167 session_for(other_moderator_user)
168 check_block_buttons block, :edit => 1
170 session_for(creator_user)
171 check_block_buttons block, :edit => 1
175 # test the edit link for expired blocks
176 def test_expired_block_buttons
177 creator_user = create(:moderator_user)
178 other_moderator_user = create(:moderator_user)
179 block = create(:user_block, :expired, :creator => creator_user)
181 session_for(other_moderator_user)
182 check_block_buttons block
184 session_for(creator_user)
185 check_block_buttons block, :edit => 1
189 # test the edit link for revoked blocks
190 def test_revoked_block_buttons
191 creator_user = create(:moderator_user)
192 revoker_user = create(:moderator_user)
193 other_moderator_user = create(:moderator_user)
194 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user)
196 session_for(other_moderator_user)
197 check_block_buttons block
199 session_for(creator_user)
200 check_block_buttons block, :edit => 1
202 session_for(revoker_user)
203 check_block_buttons block, :edit => 1
207 # test the new action
209 target_user = create(:user)
211 # Check that the block creation page requires us to login
212 get new_user_block_path(target_user)
213 assert_redirected_to login_path(:referer => new_user_block_path(target_user))
215 # Login as a normal user
216 session_for(create(:user))
218 # Check that normal users can't load the block creation page
219 get new_user_block_path(target_user)
220 assert_redirected_to :controller => "errors", :action => "forbidden"
222 # Login as a moderator
223 session_for(create(:moderator_user))
225 # Check that the block creation page loads for moderators
226 get new_user_block_path(target_user)
227 assert_response :success
228 assert_select "h1 a[href='#{user_path target_user}']", :text => target_user.display_name
229 assert_select "form#new_user_block", :count => 1 do
230 assert_select "textarea#user_block_reason", :count => 1
231 assert_select "select#user_block_period", :count => 1
232 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
233 assert_select "input#display_name[type='hidden']", :count => 1
234 assert_select "input[type='submit'][value='Create block']", :count => 1
237 # We should get an error if the user doesn't exist
238 get new_user_block_path(:display_name => "non_existent_user")
239 assert_response :not_found
240 assert_template "users/no_such_user"
241 assert_select "h1", "The user non_existent_user does not exist"
245 # test the edit action
247 creator_user = create(:moderator_user)
248 other_moderator_user = create(:moderator_user)
249 active_block = create(:user_block, :creator => creator_user)
251 # Check that the block edit page requires us to login
252 get edit_user_block_path(:id => active_block)
253 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
255 # Login as a normal user
256 session_for(create(:user))
258 # Check that normal users can't load the block edit page
259 get edit_user_block_path(:id => active_block)
260 assert_redirected_to :controller => "errors", :action => "forbidden"
262 # Login as a moderator
263 session_for(other_moderator_user)
265 # Check that the block edit page loads for moderators
266 get edit_user_block_path(:id => active_block)
267 assert_response :success
268 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
269 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
270 assert_select "textarea#user_block_reason", :count => 1
271 assert_select "select#user_block_period", :count => 0
272 assert_select "input#user_block_needs_view[type='checkbox']", :count => 0
273 assert_select "input[type='submit'][value='Update block']", :count => 0
274 assert_select "input#user_block_period[type='hidden']", :count => 1
275 assert_select "input#user_block_needs_view[type='hidden']", :count => 1
276 assert_select "input[type='submit'][value='Revoke block']", :count => 1
279 # Login as the block creator
280 session_for(creator_user)
282 # Check that the block edit page loads for the creator
283 get edit_user_block_path(:id => active_block)
284 assert_response :success
285 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
286 assert_select "form#edit_user_block_#{active_block.id}", :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[type='submit'][value='Update block']", :count => 1
291 assert_select "input#user_block_period[type='hidden']", :count => 0
292 assert_select "input#user_block_needs_view[type='hidden']", :count => 0
293 assert_select "input[type='submit'][value='Revoke block']", :count => 0
296 # We should get an error if the user doesn't exist
297 get edit_user_block_path(:id => 99999)
298 assert_response :not_found
299 assert_template "not_found"
300 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
304 # test the edit action when the remaining block duration doesn't match the available select options
305 def test_edit_duration
306 moderator_user = create(:moderator_user)
309 active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours)
311 session_for(moderator_user)
312 get edit_user_block_path(active_block)
314 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
315 assert_select "select#user_block_period", :count => 1 do
316 assert_select "option[value='96'][selected]", :count => 1
321 get edit_user_block_path(active_block)
323 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
324 assert_select "select#user_block_period", :count => 1 do
325 assert_select "option[value='96'][selected]", :count => 1
332 # test the create action
334 target_user = create(:user)
335 moderator_user = create(:moderator_user)
337 # Not logged in yet, so creating a block should fail
338 post user_blocks_path
339 assert_response :forbidden
341 # Login as a normal user
342 session_for(create(:user))
344 # Check that normal users can't create blocks
345 post user_blocks_path
346 assert_redirected_to :controller => "errors", :action => "forbidden"
348 # Login as a moderator
349 session_for(moderator_user)
351 # A bogus block period should result in an error
352 assert_no_difference "UserBlock.count" do
353 post user_blocks_path(:display_name => target_user.display_name,
354 :user_block_period => "99")
356 assert_redirected_to new_user_block_path(target_user)
357 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
359 # Check that creating a block works
360 assert_difference "UserBlock.count", 1 do
361 post user_blocks_path(:display_name => target_user.display_name,
362 :user_block_period => "12",
363 :user_block => { :needs_view => false, :reason => "Vandalism" })
366 assert_redirected_to user_block_path(:id => b.id)
367 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
368 assert_in_delta Time.now.utc, b.created_at, 1
369 assert_in_delta Time.now.utc, b.updated_at, 1
370 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
371 assert_not b.needs_view
372 assert_equal "Vandalism", b.reason
373 assert_equal "markdown", b.reason_format
374 assert_equal moderator_user.id, b.creator_id
376 # We should get an error if no user is specified
377 post user_blocks_path
378 assert_response :not_found
379 assert_template "users/no_such_user"
380 assert_select "h1", "The user does not exist"
382 # We should get an error if the user doesn't exist
383 post user_blocks_path(:display_name => "non_existent_user")
384 assert_response :not_found
385 assert_template "users/no_such_user"
386 assert_select "h1", "The user non_existent_user does not exist"
390 # test the duration of a created block
391 def test_create_duration
392 target_user = create(:user)
393 moderator_user = create(:moderator_user)
395 session_for(moderator_user)
396 post user_blocks_path(:display_name => target_user.display_name,
397 :user_block_period => "336",
398 :user_block => { :needs_view => false, :reason => "Vandalism" })
400 block = UserBlock.last
401 assert_equal 1209600, block.ends_at - block.created_at
405 # test the update action
407 moderator_user = create(:moderator_user)
408 active_block = create(:user_block, :creator => moderator_user)
410 # Not logged in yet, so updating a block should fail
411 put user_block_path(:id => active_block)
412 assert_response :forbidden
414 # Login as a normal user
415 session_for(create(:user))
417 # Check that normal users can't update blocks
418 put user_block_path(:id => active_block)
419 assert_redirected_to :controller => "errors", :action => "forbidden"
421 # Login as the moderator
422 session_for(moderator_user)
424 # A bogus block period should result in an error
425 assert_no_difference "UserBlock.count" do
426 put user_block_path(:id => active_block, :user_block_period => "99")
428 assert_redirected_to edit_user_block_path(active_block)
429 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
431 # Check that updating a block works
432 assert_no_difference "UserBlock.count" do
433 put user_block_path(:id => active_block,
434 :user_block_period => "12",
435 :user_block => { :needs_view => true, :reason => "Vandalism" })
437 assert_redirected_to user_block_path(active_block)
438 assert_equal "Block updated.", flash[:notice]
439 b = UserBlock.find(active_block.id)
440 assert_in_delta Time.now.utc, b.updated_at, 1
442 assert_equal "Vandalism", b.reason
444 # We should get an error if the block doesn't exist
445 put user_block_path(:id => 99999)
446 assert_response :not_found
447 assert_template "not_found"
448 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
452 # test the update action on expired blocks
453 def test_update_expired
454 creator_user = create(:moderator_user)
455 other_moderator_user = create(:moderator_user)
456 block = create(:user_block, :expired, :creator => creator_user, :reason => "Original Reason")
458 session_for(other_moderator_user)
459 put user_block_path(block,
460 :user_block_period => "0",
461 :user_block => { :needs_view => false, :reason => "Updated Reason" })
462 assert_redirected_to edit_user_block_path(block)
463 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
465 assert_not block.active?
466 assert_equal "Original Reason", block.reason
468 session_for(creator_user)
469 check_inactive_block_updates(block)
473 # test the update action on revoked blocks
474 def test_update_revoked
475 creator_user = create(:moderator_user)
476 revoker_user = create(:moderator_user)
477 other_moderator_user = create(:moderator_user)
478 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user, :reason => "Original Reason")
480 session_for(other_moderator_user)
481 put user_block_path(block,
482 :user_block_period => "0",
483 :user_block => { :needs_view => false, :reason => "Updated Reason" })
484 assert_redirected_to edit_user_block_path(block)
485 assert_equal "Only the moderators who created or revoked this block can edit it.", flash[:error]
487 assert_not_predicate block, :active?
488 assert_equal "Original Reason", block.reason
490 session_for(creator_user)
491 check_inactive_block_updates(block)
493 session_for(revoker_user)
494 check_inactive_block_updates(block)
498 # test the update action revoking the block
499 def test_revoke_using_update_by_creator
500 moderator_user = create(:moderator_user)
501 block = create(:user_block, :creator => moderator_user)
503 session_for(moderator_user)
504 put user_block_path(block,
505 :user_block_period => "24",
506 :user_block => { :needs_view => false, :reason => "Updated Reason" })
507 assert_redirected_to user_block_path(block)
508 assert_equal "Block updated.", flash[:notice]
510 assert_predicate block, :active?
511 assert_nil block.revoker
513 put user_block_path(block,
514 :user_block_period => "0",
515 :user_block => { :needs_view => false, :reason => "Updated Reason" })
516 assert_redirected_to user_block_path(block)
517 assert_equal "Block updated.", flash[:notice]
519 assert_not_predicate block, :active?
520 assert_equal moderator_user, block.revoker
523 def test_revoke_using_update_by_other_moderator
524 creator_user = create(:moderator_user)
525 other_moderator_user = create(:moderator_user)
526 block = create(:user_block, :creator => creator_user)
528 session_for(other_moderator_user)
529 put user_block_path(block,
530 :user_block_period => "24",
531 :user_block => { :needs_view => false, :reason => "Updated Reason" })
532 assert_response :success
533 assert_equal "Only the moderator who created this block can edit it without revoking.", flash[:error]
535 assert_predicate block, :active?
536 assert_nil block.revoker
538 put user_block_path(block,
539 :user_block_period => "0",
540 :user_block => { :needs_view => false, :reason => "Updated Reason" })
541 assert_redirected_to user_block_path(block)
542 assert_equal "Block updated.", flash[:notice]
544 assert_not_predicate block, :active?
545 assert_equal other_moderator_user, block.revoker
549 # test the revoke all page
550 def test_revoke_all_page
551 blocked_user = create(:user)
552 create(:user_block, :user => blocked_user)
554 # Asking for the revoke all blocks page with a bogus user name should fail
555 get user_blocks_on_path("non_existent_user")
556 assert_response :not_found
558 # Check that the revoke all blocks page requires us to login
559 get revoke_all_user_blocks_path(blocked_user)
560 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
562 # Login as a normal user
563 session_for(create(:user))
565 # Check that normal users can't load the revoke all blocks page
566 get revoke_all_user_blocks_path(blocked_user)
567 assert_redirected_to :controller => "errors", :action => "forbidden"
569 # Login as a moderator
570 session_for(create(:moderator_user))
572 # Check that the revoke all blocks page loads for moderators
573 get revoke_all_user_blocks_path(blocked_user)
574 assert_response :success
575 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
579 # test the revoke all action
580 def test_revoke_all_action
581 blocked_user = create(:user)
582 active_block1 = create(:user_block, :user => blocked_user)
583 active_block2 = create(:user_block, :user => blocked_user)
584 expired_block1 = create(:user_block, :expired, :user => blocked_user)
585 blocks = [active_block1, active_block2, expired_block1]
586 moderator_user = create(:moderator_user)
588 assert_predicate active_block1, :active?
589 assert_predicate active_block2, :active?
590 assert_not_predicate expired_block1, :active?
592 # Login as a normal user
593 session_for(create(:user))
595 # Check that normal users can't load the block revoke page
596 get revoke_all_user_blocks_path(:blocked_user)
597 assert_redirected_to :controller => "errors", :action => "forbidden"
599 # Login as a moderator
600 session_for(moderator_user)
602 # Check that revoking blocks using GET should fail
603 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
604 assert_response :success
605 assert_template "revoke_all"
607 blocks.each(&:reload)
608 assert_predicate active_block1, :active?
609 assert_predicate active_block2, :active?
610 assert_not_predicate expired_block1, :active?
612 # Check that revoking blocks works using POST
613 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
614 assert_redirected_to user_blocks_on_path(blocked_user)
616 blocks.each(&:reload)
617 assert_not_predicate active_block1, :active?
618 assert_not_predicate active_block2, :active?
619 assert_not_predicate expired_block1, :active?
620 assert_equal moderator_user, active_block1.revoker
621 assert_equal moderator_user, active_block2.revoker
622 assert_not_equal moderator_user, expired_block1.revoker
626 # test changes to end/deactivation dates
627 def test_dates_when_viewed_before_end
628 blocked_user = create(:user)
629 moderator_user = create(:moderator_user)
632 session_for(moderator_user)
633 assert_difference "UserBlock.count", 1 do
634 post user_blocks_path(:display_name => blocked_user.display_name,
635 :user_block_period => "48",
636 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
638 block = UserBlock.last
639 assert_equal Time.now.utc + 2.days, block.ends_at
640 assert_nil block.deactivates_at
643 session_for(blocked_user)
644 get user_block_path(block)
646 assert_equal Time.now.utc + 1.day, block.ends_at
647 assert_equal Time.now.utc + 1.day, block.deactivates_at
651 def test_dates_when_viewed_after_end
652 blocked_user = create(:user)
653 moderator_user = create(:moderator_user)
656 session_for(moderator_user)
657 assert_difference "UserBlock.count", 1 do
658 post user_blocks_path(:display_name => blocked_user.display_name,
659 :user_block_period => "24",
660 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
662 block = UserBlock.last
663 assert_equal Time.now.utc + 1.day, block.ends_at
664 assert_nil block.deactivates_at
667 session_for(blocked_user)
668 get user_block_path(block)
670 assert_equal Time.now.utc - 1.day, block.ends_at
671 assert_equal Time.now.utc, block.deactivates_at
675 def test_dates_when_edited_before_end
676 blocked_user = create(:user)
677 moderator_user = create(:moderator_user)
680 session_for(moderator_user)
681 assert_difference "UserBlock.count", 1 do
682 post user_blocks_path(:display_name => blocked_user.display_name,
683 :user_block_period => "48",
684 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
686 block = UserBlock.last
687 assert_equal Time.now.utc + 2.days, block.ends_at
688 assert_equal Time.now.utc + 2.days, block.deactivates_at
691 put user_block_path(block,
692 :user_block_period => "48",
693 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
695 assert_equal Time.now.utc + 2.days, block.ends_at
696 assert_equal Time.now.utc + 2.days, block.deactivates_at
700 def test_dates_when_edited_after_end
701 blocked_user = create(:user)
702 moderator_user = create(:moderator_user)
705 session_for(moderator_user)
706 assert_difference "UserBlock.count", 1 do
707 post user_blocks_path(:display_name => blocked_user.display_name,
708 :user_block_period => "24",
709 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
711 block = UserBlock.last
712 assert_equal Time.now.utc + 1.day, block.ends_at
713 assert_equal Time.now.utc + 1.day, block.deactivates_at
716 put user_block_path(block,
717 :user_block_period => "0",
718 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
720 assert_equal Time.now.utc - 1.day, block.ends_at
721 assert_equal Time.now.utc - 1.day, block.deactivates_at
726 # test updates on legacy records without correctly initialized deactivates_at
727 def test_update_legacy_deactivates_at
728 blocked_user = create(:user)
729 moderator_user = create(:moderator_user)
732 block = UserBlock.new :user => blocked_user,
733 :creator => moderator_user,
734 :reason => "because",
735 :ends_at => Time.now.utc + 1.day,
738 assert_difference "UserBlock.count", 1 do
739 block.save :validate => false
743 session_for(moderator_user)
744 put user_block_path(block,
745 :user_block_period => "0",
746 :user_block => { :needs_view => false, :reason => "Testing legacy block update" })
748 assert_equal Time.now.utc - 1.day, block.ends_at
749 assert_equal Time.now.utc - 1.day, block.deactivates_at
754 # test the blocks_on action
756 blocked_user = create(:user)
757 unblocked_user = create(:user)
758 normal_user = create(:user)
759 active_block = create(:user_block, :user => blocked_user)
760 revoked_block = create(:user_block, :revoked, :user => blocked_user)
761 expired_block = create(:user_block, :expired, :user => unblocked_user)
763 # Asking for a list of blocks with a bogus user name should fail
764 get user_blocks_on_path("non_existent_user")
765 assert_response :not_found
766 assert_template "users/no_such_user"
767 assert_select "h1", "The user non_existent_user does not exist"
769 # Check the list of blocks for a user that has never been blocked
770 get user_blocks_on_path(normal_user)
771 assert_response :success
772 assert_select "table#block_list", false
773 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
775 # Check the list of blocks for a user that is currently blocked
776 get user_blocks_on_path(blocked_user)
777 assert_response :success
778 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
779 assert_select "table#block_list tbody", :count => 1 do
780 assert_select "tr", 2
781 assert_select "a[href='#{user_block_path(active_block)}']", 1
782 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
785 # Check the list of blocks for a user that has previously been blocked
786 get user_blocks_on_path(unblocked_user)
787 assert_response :success
788 assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
789 assert_select "table#block_list tbody", :count => 1 do
790 assert_select "tr", 1
791 assert_select "a[href='#{user_block_path(expired_block)}']", 1
796 # test the blocks_on action with multiple pages
797 def test_blocks_on_paged
799 user_blocks = create_list(:user_block, 50, :user => user).reverse
800 next_path = user_blocks_on_path(user)
803 assert_response :success
804 check_user_blocks_table user_blocks[0...20]
805 check_no_page_link "Newer Blocks"
806 next_path = check_page_link "Older Blocks"
809 assert_response :success
810 check_user_blocks_table user_blocks[20...40]
811 check_page_link "Newer Blocks"
812 next_path = check_page_link "Older Blocks"
815 assert_response :success
816 check_user_blocks_table user_blocks[40...50]
817 check_page_link "Newer Blocks"
818 check_no_page_link "Older Blocks"
822 # test the blocks_on action with invalid pages
823 def test_blocks_on_invalid_paged
826 %w[-1 0 fred].each do |id|
827 get user_blocks_on_path(user, :before => id)
828 assert_redirected_to :controller => :errors, :action => :bad_request
830 get user_blocks_on_path(user, :after => id)
831 assert_redirected_to :controller => :errors, :action => :bad_request
836 # test the blocks_by action
838 moderator_user = create(:moderator_user)
839 second_moderator_user = create(:moderator_user)
840 normal_user = create(:user)
841 active_block = create(:user_block, :creator => moderator_user)
842 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
843 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
845 # Asking for a list of blocks with a bogus user name should fail
846 get user_blocks_by_path("non_existent_user")
847 assert_response :not_found
848 assert_template "users/no_such_user"
849 assert_select "h1", "The user non_existent_user does not exist"
851 # Check the list of blocks given by one moderator
852 get user_blocks_by_path(moderator_user)
853 assert_response :success
854 assert_select "h1 a[href='#{user_path moderator_user}']", :text => moderator_user.display_name
855 assert_select "table#block_list tbody", :count => 1 do
856 assert_select "tr", 1
857 assert_select "a[href='#{user_block_path(active_block)}']", 1
860 # Check the list of blocks given by a different moderator
861 get user_blocks_by_path(second_moderator_user)
862 assert_response :success
863 assert_select "h1 a[href='#{user_path second_moderator_user}']", :text => second_moderator_user.display_name
864 assert_select "table#block_list tbody", :count => 1 do
865 assert_select "tr", 2
866 assert_select "a[href='#{user_block_path(expired_block)}']", 1
867 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
870 # Check the list of blocks (not) given by a normal user
871 get user_blocks_by_path(normal_user)
872 assert_response :success
873 assert_select "table#block_list", false
874 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
878 # test the blocks_by action with multiple pages
879 def test_blocks_by_paged
880 user = create(:moderator_user)
881 user_blocks = create_list(:user_block, 50, :creator => user).reverse
882 next_path = user_blocks_by_path(user)
885 assert_response :success
886 check_user_blocks_table user_blocks[0...20]
887 check_no_page_link "Newer Blocks"
888 next_path = check_page_link "Older Blocks"
891 assert_response :success
892 check_user_blocks_table user_blocks[20...40]
893 check_page_link "Newer Blocks"
894 next_path = check_page_link "Older Blocks"
897 assert_response :success
898 check_user_blocks_table user_blocks[40...50]
899 check_page_link "Newer Blocks"
900 check_no_page_link "Older Blocks"
904 # test the blocks_by action with invalid pages
905 def test_blocks_by_invalid_paged
906 user = create(:moderator_user)
908 %w[-1 0 fred].each do |id|
909 get user_blocks_by_path(user, :before => id)
910 assert_redirected_to :controller => :errors, :action => :bad_request
912 get user_blocks_by_path(user, :after => id)
913 assert_redirected_to :controller => :errors, :action => :bad_request
919 def check_block_buttons(block, edit: 0)
920 [user_blocks_path, user_block_path(block)].each do |path|
922 assert_response :success
923 assert_select "a[href='#{edit_user_block_path block}']", :count => edit
927 def check_inactive_block_updates(block)
928 original_ends_at = block.ends_at
930 put user_block_path(block,
931 :user_block_period => "0",
932 :user_block => { :needs_view => false, :reason => "Updated Reason" })
933 assert_redirected_to user_block_path(block)
934 assert_equal "Block updated.", flash[:notice]
936 assert_not_predicate block, :active?
937 assert_equal "Updated Reason", block.reason
938 assert_equal original_ends_at, block.ends_at
940 put user_block_path(block,
941 :user_block_period => "0",
942 :user_block => { :needs_view => true, :reason => "Updated Reason Needs View" })
943 assert_response :success
944 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
946 assert_not_predicate block, :active?
947 assert_equal "Updated Reason", block.reason
948 assert_equal original_ends_at, block.ends_at
950 put user_block_path(block,
951 :user_block_period => "1",
952 :user_block => { :needs_view => false, :reason => "Updated Reason Duration Extended" })
953 assert_response :success
954 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
956 assert_not_predicate block, :active?
957 assert_equal "Updated Reason", block.reason
958 assert_equal original_ends_at, block.ends_at
960 put user_block_path(block,
961 :user_block_period => "0",
962 :user_block => { :needs_view => false, :reason => "Updated Reason Again" })
963 assert_redirected_to user_block_path(block)
964 assert_equal "Block updated.", flash[:notice]
966 assert_not_predicate block, :active?
967 assert_equal "Updated Reason Again", block.reason
968 assert_equal original_ends_at, block.ends_at
971 def check_user_blocks_table(user_blocks)
972 assert_dom "table#block_list tbody tr" do |rows|
973 assert_equal user_blocks.count, rows.count, "unexpected number of rows in user blocks table"
974 rows.zip(user_blocks).map do |row, user_block|
975 assert_dom row, "a[href='#{user_block_path user_block}']", 1
980 def check_no_page_link(name)
981 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
984 def check_page_link(name)
985 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
986 return buttons.first.attributes["href"].value