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/new", :method => :get },
18 { :controller => "user_blocks", :action => "new" }
21 { :path => "/user_blocks", :method => :post },
22 { :controller => "user_blocks", :action => "create" }
25 { :path => "/user_blocks/1", :method => :get },
26 { :controller => "user_blocks", :action => "show", :id => "1" }
29 { :path => "/user_blocks/1/edit", :method => :get },
30 { :controller => "user_blocks", :action => "edit", :id => "1" }
33 { :path => "/user_blocks/1", :method => :put },
34 { :controller => "user_blocks", :action => "update", :id => "1" }
37 { :path => "/user_blocks/1", :method => :delete },
38 { :controller => "user_blocks", :action => "destroy", :id => "1" }
41 { :path => "/blocks/1/revoke", :method => :get },
42 { :controller => "user_blocks", :action => "revoke", :id => "1" }
45 { :path => "/blocks/1/revoke", :method => :post },
46 { :controller => "user_blocks", :action => "revoke", :id => "1" }
50 { :path => "/user/username/blocks", :method => :get },
51 { :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
54 { :path => "/user/username/blocks_by", :method => :get },
55 { :controller => "user_blocks", :action => "blocks_by", :display_name => "username" }
58 { :path => "/user/username/blocks/revoke_all", :method => :get },
59 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
62 { :path => "/user/username/blocks/revoke_all", :method => :post },
63 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
68 # test the index action
70 revoked_block = create(:user_block, :revoked)
73 assert_response :success
74 assert_select "table#block_list tbody tr", :count => 1 do
75 assert_select "a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
76 assert_select "a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
77 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
80 active_block = create(:user_block)
81 expired_block = create(:user_block, :expired)
84 assert_response :success
85 assert_select "table#block_list tbody", :count => 1 do
87 assert_select "a[href='#{user_block_path(active_block)}']", 1
88 assert_select "a[href='#{user_block_path(expired_block)}']", 1
89 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
94 # test the index action with multiple pages
96 user_blocks = create_list(:user_block, 50).reverse
97 next_path = user_blocks_path
100 assert_response :success
101 check_user_blocks_table user_blocks[0...20]
102 check_no_page_link "Newer Blocks"
103 next_path = check_page_link "Older Blocks"
106 assert_response :success
107 check_user_blocks_table user_blocks[20...40]
108 check_page_link "Newer Blocks"
109 next_path = check_page_link "Older Blocks"
112 assert_response :success
113 check_user_blocks_table user_blocks[40...50]
114 check_page_link "Newer Blocks"
115 check_no_page_link "Older Blocks"
119 # test the index action with invalid pages
120 def test_index_invalid_paged
121 %w[-1 0 fred].each do |id|
122 get user_blocks_path(:before => id)
123 assert_redirected_to :controller => :errors, :action => :bad_request
125 get user_blocks_path(:after => id)
126 assert_redirected_to :controller => :errors, :action => :bad_request
131 # test the show action
133 active_block = create(:user_block, :needs_view)
134 expired_block = create(:user_block, :expired)
135 revoked_block = create(:user_block, :revoked)
137 # Viewing a block should fail when a bogus ID is given
138 get user_block_path(:id => 99999)
139 assert_response :not_found
140 assert_template "not_found"
141 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
143 # Viewing an expired block should work
144 get user_block_path(:id => expired_block)
145 assert_response :success
146 assert_select "h1 a[href='#{user_path expired_block.user}']", :text => expired_block.user.display_name
147 assert_select "h1 a[href='#{user_path expired_block.creator}']", :text => expired_block.creator.display_name
149 # Viewing a revoked block should work
150 get user_block_path(:id => revoked_block)
151 assert_response :success
152 assert_select "h1 a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
153 assert_select "h1 a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
154 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
156 # Viewing an active block should work, but shouldn't mark it as seen
157 get user_block_path(:id => active_block)
158 assert_response :success
159 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
160 assert_select "h1 a[href='#{user_path active_block.creator}']", :text => active_block.creator.display_name
161 assert UserBlock.find(active_block.id).needs_view
163 # Login as the blocked user
164 session_for(active_block.user)
166 # Now viewing it should mark it as seen
167 get user_block_path(:id => active_block)
168 assert_response :success
169 assert_not UserBlock.find(active_block.id).needs_view
173 # test edit/revoke link for active blocks
174 def test_active_block_buttons
175 creator_user = create(:moderator_user)
176 other_moderator_user = create(:moderator_user)
177 block = create(:user_block, :creator => creator_user)
179 session_for(other_moderator_user)
180 check_block_buttons block, :revoke => 1
182 session_for(creator_user)
183 check_block_buttons block, :edit => 1, :revoke => 1
187 # test the edit link for expired blocks
188 def test_expired_block_buttons
189 creator_user = create(:moderator_user)
190 other_moderator_user = create(:moderator_user)
191 block = create(:user_block, :expired, :creator => creator_user)
193 session_for(other_moderator_user)
194 check_block_buttons block
196 session_for(creator_user)
197 check_block_buttons block, :edit => 1
201 # test the edit link for revoked blocks
202 def test_revoked_block_buttons
203 creator_user = create(:moderator_user)
204 revoker_user = create(:moderator_user)
205 other_moderator_user = create(:moderator_user)
206 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user)
208 session_for(other_moderator_user)
209 check_block_buttons block
211 session_for(creator_user)
212 check_block_buttons block, :edit => 1
214 session_for(revoker_user)
215 check_block_buttons block, :edit => 1
219 # test the new action
221 target_user = create(:user)
223 # Check that the block creation page requires us to login
224 get new_user_block_path(target_user)
225 assert_redirected_to login_path(:referer => new_user_block_path(target_user))
227 # Login as a normal user
228 session_for(create(:user))
230 # Check that normal users can't load the block creation page
231 get new_user_block_path(target_user)
232 assert_redirected_to :controller => "errors", :action => "forbidden"
234 # Login as a moderator
235 session_for(create(:moderator_user))
237 # Check that the block creation page loads for moderators
238 get new_user_block_path(target_user)
239 assert_response :success
240 assert_select "h1 a[href='#{user_path target_user}']", :text => target_user.display_name
241 assert_select "form#new_user_block", :count => 1 do
242 assert_select "textarea#user_block_reason", :count => 1
243 assert_select "select#user_block_period", :count => 1
244 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
245 assert_select "input#display_name[type='hidden']", :count => 1
246 assert_select "input[type='submit'][value='Create block']", :count => 1
249 # We should get an error if the user doesn't exist
250 get new_user_block_path(:display_name => "non_existent_user")
251 assert_response :not_found
252 assert_template "users/no_such_user"
253 assert_select "h1", "The user non_existent_user does not exist"
257 # test the edit action
259 active_block = create(:user_block)
261 # Check that the block edit page requires us to login
262 get edit_user_block_path(:id => active_block)
263 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
265 # Login as a normal user
266 session_for(create(:user))
268 # Check that normal users can't load the block edit page
269 get edit_user_block_path(:id => active_block)
270 assert_redirected_to :controller => "errors", :action => "forbidden"
272 # Login as a moderator
273 session_for(create(:moderator_user))
275 # Check that the block edit page loads for moderators
276 get edit_user_block_path(:id => active_block)
277 assert_response :success
278 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
279 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
280 assert_select "textarea#user_block_reason", :count => 1
281 assert_select "select#user_block_period", :count => 1
282 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
283 assert_select "input[type='submit'][value='Update block']", :count => 1
286 # We should get an error if the user doesn't exist
287 get edit_user_block_path(:id => 99999)
288 assert_response :not_found
289 assert_template "not_found"
290 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
294 # test the edit action when the remaining block duration doesn't match the available select options
295 def test_edit_duration
296 moderator_user = create(:moderator_user)
299 active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours)
301 session_for(moderator_user)
302 get edit_user_block_path(active_block)
304 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
305 assert_select "select#user_block_period", :count => 1 do
306 assert_select "option[value='96'][selected]", :count => 1
311 get edit_user_block_path(active_block)
313 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
314 assert_select "select#user_block_period", :count => 1 do
315 assert_select "option[value='96'][selected]", :count => 1
322 # test the create action
324 target_user = create(:user)
325 moderator_user = create(:moderator_user)
327 # Not logged in yet, so creating a block should fail
328 post user_blocks_path
329 assert_response :forbidden
331 # Login as a normal user
332 session_for(create(:user))
334 # Check that normal users can't create blocks
335 post user_blocks_path
336 assert_redirected_to :controller => "errors", :action => "forbidden"
338 # Login as a moderator
339 session_for(moderator_user)
341 # A bogus block period should result in an error
342 assert_no_difference "UserBlock.count" do
343 post user_blocks_path(:display_name => target_user.display_name,
344 :user_block_period => "99")
346 assert_redirected_to new_user_block_path(target_user)
347 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
349 # Check that creating a block works
350 assert_difference "UserBlock.count", 1 do
351 post user_blocks_path(:display_name => target_user.display_name,
352 :user_block_period => "12",
353 :user_block => { :needs_view => false, :reason => "Vandalism" })
356 assert_redirected_to user_block_path(:id => b.id)
357 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
358 assert_in_delta Time.now.utc, b.created_at, 1
359 assert_in_delta Time.now.utc, b.updated_at, 1
360 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
361 assert_not b.needs_view
362 assert_equal "Vandalism", b.reason
363 assert_equal "markdown", b.reason_format
364 assert_equal moderator_user.id, b.creator_id
366 # We should get an error if no user is specified
367 post user_blocks_path
368 assert_response :not_found
369 assert_template "users/no_such_user"
370 assert_select "h1", "The user does not exist"
372 # We should get an error if the user doesn't exist
373 post user_blocks_path(:display_name => "non_existent_user")
374 assert_response :not_found
375 assert_template "users/no_such_user"
376 assert_select "h1", "The user non_existent_user does not exist"
380 # test the duration of a created block
381 def test_create_duration
382 target_user = create(:user)
383 moderator_user = create(:moderator_user)
385 session_for(moderator_user)
386 post user_blocks_path(:display_name => target_user.display_name,
387 :user_block_period => "336",
388 :user_block => { :needs_view => false, :reason => "Vandalism" })
390 block = UserBlock.last
391 assert_equal 1209600, block.ends_at - block.created_at
395 # test the update action
397 moderator_user = create(:moderator_user)
398 second_moderator_user = create(:moderator_user)
399 active_block = create(:user_block, :creator => moderator_user)
401 # Not logged in yet, so updating a block should fail
402 put user_block_path(:id => active_block)
403 assert_response :forbidden
405 # Login as a normal user
406 session_for(create(:user))
408 # Check that normal users can't update blocks
409 put user_block_path(:id => active_block)
410 assert_redirected_to :controller => "errors", :action => "forbidden"
412 # Login as the wrong moderator
413 session_for(second_moderator_user)
415 # Check that only the person who created a block can update it
416 assert_no_difference "UserBlock.count" do
417 put user_block_path(:id => active_block,
418 :user_block_period => "12",
419 :user_block => { :needs_view => true, :reason => "Vandalism" })
421 assert_redirected_to edit_user_block_path(active_block)
422 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
424 # Login as the correct moderator
425 session_for(moderator_user)
427 # A bogus block period should result in an error
428 assert_no_difference "UserBlock.count" do
429 put user_block_path(:id => active_block, :user_block_period => "99")
431 assert_redirected_to edit_user_block_path(active_block)
432 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
434 # Check that updating a block works
435 assert_no_difference "UserBlock.count" do
436 put user_block_path(:id => active_block,
437 :user_block_period => "12",
438 :user_block => { :needs_view => true, :reason => "Vandalism" })
440 assert_redirected_to user_block_path(active_block)
441 assert_equal "Block updated.", flash[:notice]
442 b = UserBlock.find(active_block.id)
443 assert_in_delta Time.now.utc, b.updated_at, 1
445 assert_equal "Vandalism", b.reason
447 # We should get an error if the block doesn't exist
448 put user_block_path(:id => 99999)
449 assert_response :not_found
450 assert_template "not_found"
451 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
455 # test the update action on expired blocks
456 def test_update_expired
457 creator_user = create(:moderator_user)
458 other_moderator_user = create(:moderator_user)
459 block = create(:user_block, :expired, :creator => creator_user, :reason => "Original Reason")
461 session_for(other_moderator_user)
462 put user_block_path(block,
463 :user_block_period => "0",
464 :user_block => { :needs_view => false, :reason => "Updated Reason" })
465 assert_redirected_to edit_user_block_path(block)
466 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
468 assert_not block.active?
469 assert_equal "Original Reason", block.reason
471 session_for(creator_user)
472 check_inactive_block_updates(block)
476 # test the update action on revoked blocks
477 def test_update_revoked
478 creator_user = create(:moderator_user)
479 revoker_user = create(:moderator_user)
480 other_moderator_user = create(:moderator_user)
481 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user, :reason => "Original Reason")
483 session_for(other_moderator_user)
484 put user_block_path(block,
485 :user_block_period => "0",
486 :user_block => { :needs_view => false, :reason => "Updated Reason" })
487 assert_redirected_to edit_user_block_path(block)
488 assert_equal "Only the moderators who created or revoked this block can edit it.", flash[:error]
490 assert_not_predicate block, :active?
491 assert_equal "Original Reason", block.reason
493 session_for(creator_user)
494 check_inactive_block_updates(block)
496 session_for(revoker_user)
497 check_inactive_block_updates(block)
501 # test the revoke action
503 active_block = create(:user_block)
505 # Check that the block revoke page requires us to login
506 get revoke_user_block_path(:id => active_block)
507 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
509 # Login as a normal user
510 session_for(create(:user))
512 # Check that normal users can't load the block revoke page
513 get revoke_user_block_path(:id => active_block)
514 assert_redirected_to :controller => "errors", :action => "forbidden"
516 # Login as a moderator
517 session_for(create(:moderator_user))
519 # Check that the block revoke page loads for moderators
520 get revoke_user_block_path(:id => active_block)
521 assert_response :success
522 assert_template "revoke"
523 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
524 assert_select "form", :count => 1 do
525 assert_select "input#confirm[type='checkbox']", :count => 1
526 assert_select "input[type='submit'][value='Revoke!']", :count => 1
529 # Check that revoking a block using GET should fail
530 get revoke_user_block_path(:id => active_block, :confirm => true)
531 assert_response :success
532 assert_template "revoke"
533 b = UserBlock.find(active_block.id)
534 assert_operator b.ends_at - Time.now.utc, :>, 100
536 # Check that revoking a block works using POST
537 post revoke_user_block_path(:id => active_block, :confirm => true)
538 assert_redirected_to user_block_path(active_block)
539 b = UserBlock.find(active_block.id)
540 assert_in_delta Time.now.utc, b.ends_at, 1
542 # We should get an error if the block doesn't exist
543 get revoke_user_block_path(:id => 99999)
544 assert_response :not_found
545 assert_template "not_found"
546 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
550 # test the revoke all page
551 def test_revoke_all_page
552 blocked_user = create(:user)
553 create(:user_block, :user => blocked_user)
555 # Asking for the revoke all blocks page with a bogus user name should fail
556 get user_blocks_on_path("non_existent_user")
557 assert_response :not_found
559 # Check that the revoke all blocks page requires us to login
560 get revoke_all_user_blocks_path(blocked_user)
561 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
563 # Login as a normal user
564 session_for(create(:user))
566 # Check that normal users can't load the revoke all blocks page
567 get revoke_all_user_blocks_path(blocked_user)
568 assert_redirected_to :controller => "errors", :action => "forbidden"
570 # Login as a moderator
571 session_for(create(:moderator_user))
573 # Check that the revoke all blocks page loads for moderators
574 get revoke_all_user_blocks_path(blocked_user)
575 assert_response :success
576 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
580 # test the revoke all action
581 def test_revoke_all_action
582 blocked_user = create(:user)
583 active_block1 = create(:user_block, :user => blocked_user)
584 active_block2 = create(:user_block, :user => blocked_user)
585 expired_block1 = create(:user_block, :expired, :user => blocked_user)
586 blocks = [active_block1, active_block2, expired_block1]
587 moderator_user = create(:moderator_user)
589 assert_predicate active_block1, :active?
590 assert_predicate active_block2, :active?
591 assert_not_predicate expired_block1, :active?
593 # Login as a normal user
594 session_for(create(:user))
596 # Check that normal users can't load the block revoke page
597 get revoke_all_user_blocks_path(:blocked_user)
598 assert_redirected_to :controller => "errors", :action => "forbidden"
600 # Login as a moderator
601 session_for(moderator_user)
603 # Check that revoking blocks using GET should fail
604 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
605 assert_response :success
606 assert_template "revoke_all"
608 blocks.each(&:reload)
609 assert_predicate active_block1, :active?
610 assert_predicate active_block2, :active?
611 assert_not_predicate expired_block1, :active?
613 # Check that revoking blocks works using POST
614 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
615 assert_redirected_to user_blocks_on_path(blocked_user)
617 blocks.each(&:reload)
618 assert_not_predicate active_block1, :active?
619 assert_not_predicate active_block2, :active?
620 assert_not_predicate expired_block1, :active?
621 assert_equal moderator_user, active_block1.revoker
622 assert_equal moderator_user, active_block2.revoker
623 assert_not_equal moderator_user, expired_block1.revoker
627 # test the blocks_on action
629 blocked_user = create(:user)
630 unblocked_user = create(:user)
631 normal_user = create(:user)
632 active_block = create(:user_block, :user => blocked_user)
633 revoked_block = create(:user_block, :revoked, :user => blocked_user)
634 expired_block = create(:user_block, :expired, :user => unblocked_user)
636 # Asking for a list of blocks with a bogus user name should fail
637 get user_blocks_on_path("non_existent_user")
638 assert_response :not_found
639 assert_template "users/no_such_user"
640 assert_select "h1", "The user non_existent_user does not exist"
642 # Check the list of blocks for a user that has never been blocked
643 get user_blocks_on_path(normal_user)
644 assert_response :success
645 assert_select "table#block_list", false
646 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
648 # Check the list of blocks for a user that is currently blocked
649 get user_blocks_on_path(blocked_user)
650 assert_response :success
651 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
652 assert_select "table#block_list tbody", :count => 1 do
653 assert_select "tr", 2
654 assert_select "a[href='#{user_block_path(active_block)}']", 1
655 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
658 # Check the list of blocks for a user that has previously been blocked
659 get user_blocks_on_path(unblocked_user)
660 assert_response :success
661 assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
662 assert_select "table#block_list tbody", :count => 1 do
663 assert_select "tr", 1
664 assert_select "a[href='#{user_block_path(expired_block)}']", 1
669 # test the blocks_on action with multiple pages
670 def test_blocks_on_paged
672 user_blocks = create_list(:user_block, 50, :user => user).reverse
673 next_path = user_blocks_on_path(user)
676 assert_response :success
677 check_user_blocks_table user_blocks[0...20]
678 check_no_page_link "Newer Blocks"
679 next_path = check_page_link "Older Blocks"
682 assert_response :success
683 check_user_blocks_table user_blocks[20...40]
684 check_page_link "Newer Blocks"
685 next_path = check_page_link "Older Blocks"
688 assert_response :success
689 check_user_blocks_table user_blocks[40...50]
690 check_page_link "Newer Blocks"
691 check_no_page_link "Older Blocks"
695 # test the blocks_on action with invalid pages
696 def test_blocks_on_invalid_paged
699 %w[-1 0 fred].each do |id|
700 get user_blocks_on_path(user, :before => id)
701 assert_redirected_to :controller => :errors, :action => :bad_request
703 get user_blocks_on_path(user, :after => id)
704 assert_redirected_to :controller => :errors, :action => :bad_request
709 # test the blocks_by action
711 moderator_user = create(:moderator_user)
712 second_moderator_user = create(:moderator_user)
713 normal_user = create(:user)
714 active_block = create(:user_block, :creator => moderator_user)
715 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
716 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
718 # Asking for a list of blocks with a bogus user name should fail
719 get user_blocks_by_path("non_existent_user")
720 assert_response :not_found
721 assert_template "users/no_such_user"
722 assert_select "h1", "The user non_existent_user does not exist"
724 # Check the list of blocks given by one moderator
725 get user_blocks_by_path(moderator_user)
726 assert_response :success
727 assert_select "h1 a[href='#{user_path moderator_user}']", :text => moderator_user.display_name
728 assert_select "table#block_list tbody", :count => 1 do
729 assert_select "tr", 1
730 assert_select "a[href='#{user_block_path(active_block)}']", 1
733 # Check the list of blocks given by a different moderator
734 get user_blocks_by_path(second_moderator_user)
735 assert_response :success
736 assert_select "h1 a[href='#{user_path second_moderator_user}']", :text => second_moderator_user.display_name
737 assert_select "table#block_list tbody", :count => 1 do
738 assert_select "tr", 2
739 assert_select "a[href='#{user_block_path(expired_block)}']", 1
740 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
743 # Check the list of blocks (not) given by a normal user
744 get user_blocks_by_path(normal_user)
745 assert_response :success
746 assert_select "table#block_list", false
747 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
751 # test the blocks_by action with multiple pages
752 def test_blocks_by_paged
753 user = create(:moderator_user)
754 user_blocks = create_list(:user_block, 50, :creator => user).reverse
755 next_path = user_blocks_by_path(user)
758 assert_response :success
759 check_user_blocks_table user_blocks[0...20]
760 check_no_page_link "Newer Blocks"
761 next_path = check_page_link "Older Blocks"
764 assert_response :success
765 check_user_blocks_table user_blocks[20...40]
766 check_page_link "Newer Blocks"
767 next_path = check_page_link "Older Blocks"
770 assert_response :success
771 check_user_blocks_table user_blocks[40...50]
772 check_page_link "Newer Blocks"
773 check_no_page_link "Older Blocks"
777 # test the blocks_by action with invalid pages
778 def test_blocks_by_invalid_paged
779 user = create(:moderator_user)
781 %w[-1 0 fred].each do |id|
782 get user_blocks_by_path(user, :before => id)
783 assert_redirected_to :controller => :errors, :action => :bad_request
785 get user_blocks_by_path(user, :after => id)
786 assert_redirected_to :controller => :errors, :action => :bad_request
792 def check_block_buttons(block, edit: 0, revoke: 0)
793 [user_blocks_path, user_block_path(block)].each do |path|
795 assert_response :success
796 assert_select "a[href='#{edit_user_block_path block}']", :count => edit
797 assert_select "a[href='#{revoke_user_block_path block}']", :count => revoke
801 def check_inactive_block_updates(block)
802 original_ends_at = block.ends_at
804 put user_block_path(block,
805 :user_block_period => "0",
806 :user_block => { :needs_view => false, :reason => "Updated Reason" })
807 assert_redirected_to user_block_path(block)
808 assert_equal "Block updated.", flash[:notice]
810 assert_not_predicate block, :active?
811 assert_equal "Updated Reason", block.reason
812 assert_equal original_ends_at, block.ends_at
814 put user_block_path(block,
815 :user_block_period => "0",
816 :user_block => { :needs_view => true, :reason => "Updated Reason Needs View" })
817 assert_response :success
818 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
820 assert_not_predicate block, :active?
821 assert_equal "Updated Reason", block.reason
822 assert_equal original_ends_at, block.ends_at
824 put user_block_path(block,
825 :user_block_period => "1",
826 :user_block => { :needs_view => false, :reason => "Updated Reason Duration Extended" })
827 assert_response :success
828 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
830 assert_not_predicate block, :active?
831 assert_equal "Updated Reason", block.reason
832 assert_equal original_ends_at, block.ends_at
834 put user_block_path(block,
835 :user_block_period => "0",
836 :user_block => { :needs_view => false, :reason => "Updated Reason Again" })
837 assert_redirected_to user_block_path(block)
838 assert_equal "Block updated.", flash[:notice]
840 assert_not_predicate block, :active?
841 assert_equal "Updated Reason Again", block.reason
842 assert_equal original_ends_at, block.ends_at
845 def check_user_blocks_table(user_blocks)
846 assert_dom "table#block_list tbody tr" do |rows|
847 assert_equal user_blocks.count, rows.count, "unexpected number of rows in user blocks table"
848 rows.zip(user_blocks).map do |row, user_block|
849 assert_dom row, "a[href='#{user_block_path user_block}']", 1
854 def check_no_page_link(name)
855 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
858 def check_page_link(name)
859 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
860 return buttons.first.attributes["href"].value