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
220 def check_block_buttons(block, edit: 0, revoke: 0)
221 [user_blocks_path, user_block_path(block)].each do |path|
223 assert_response :success
224 assert_select "a[href='#{edit_user_block_path block}']", :count => edit
225 assert_select "a[href='#{revoke_user_block_path block}']", :count => revoke
232 # test the new action
234 target_user = create(:user)
236 # Check that the block creation page requires us to login
237 get new_user_block_path(target_user)
238 assert_redirected_to login_path(:referer => new_user_block_path(target_user))
240 # Login as a normal user
241 session_for(create(:user))
243 # Check that normal users can't load the block creation page
244 get new_user_block_path(target_user)
245 assert_redirected_to :controller => "errors", :action => "forbidden"
247 # Login as a moderator
248 session_for(create(:moderator_user))
250 # Check that the block creation page loads for moderators
251 get new_user_block_path(target_user)
252 assert_response :success
253 assert_select "h1 a[href='#{user_path target_user}']", :text => target_user.display_name
254 assert_select "form#new_user_block", :count => 1 do
255 assert_select "textarea#user_block_reason", :count => 1
256 assert_select "select#user_block_period", :count => 1
257 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
258 assert_select "input#display_name[type='hidden']", :count => 1
259 assert_select "input[type='submit'][value='Create block']", :count => 1
262 # We should get an error if the user doesn't exist
263 get new_user_block_path(:display_name => "non_existent_user")
264 assert_response :not_found
265 assert_template "users/no_such_user"
266 assert_select "h1", "The user non_existent_user does not exist"
270 # test the edit action
272 active_block = create(:user_block)
274 # Check that the block edit page requires us to login
275 get edit_user_block_path(:id => active_block)
276 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
278 # Login as a normal user
279 session_for(create(:user))
281 # Check that normal users can't load the block edit page
282 get edit_user_block_path(:id => active_block)
283 assert_redirected_to :controller => "errors", :action => "forbidden"
285 # Login as a moderator
286 session_for(create(:moderator_user))
288 # Check that the block edit page loads for moderators
289 get edit_user_block_path(:id => active_block)
290 assert_response :success
291 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
292 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
293 assert_select "textarea#user_block_reason", :count => 1
294 assert_select "select#user_block_period", :count => 1
295 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
296 assert_select "input[type='submit'][value='Update block']", :count => 1
299 # We should get an error if the user doesn't exist
300 get edit_user_block_path(:id => 99999)
301 assert_response :not_found
302 assert_template "not_found"
303 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
307 # test the edit action when the remaining block duration doesn't match the available select options
308 def test_edit_duration
309 moderator_user = create(:moderator_user)
312 active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours)
314 session_for(moderator_user)
315 get edit_user_block_path(active_block)
317 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
318 assert_select "select#user_block_period", :count => 1 do
319 assert_select "option[value='96'][selected]", :count => 1
324 get edit_user_block_path(active_block)
326 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
327 assert_select "select#user_block_period", :count => 1 do
328 assert_select "option[value='96'][selected]", :count => 1
335 # test the create action
337 target_user = create(:user)
338 moderator_user = create(:moderator_user)
340 # Not logged in yet, so creating a block should fail
341 post user_blocks_path
342 assert_response :forbidden
344 # Login as a normal user
345 session_for(create(:user))
347 # Check that normal users can't create blocks
348 post user_blocks_path
349 assert_redirected_to :controller => "errors", :action => "forbidden"
351 # Login as a moderator
352 session_for(moderator_user)
354 # A bogus block period should result in an error
355 assert_no_difference "UserBlock.count" do
356 post user_blocks_path(:display_name => target_user.display_name,
357 :user_block_period => "99")
359 assert_redirected_to new_user_block_path(target_user)
360 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
362 # Check that creating a block works
363 assert_difference "UserBlock.count", 1 do
364 post user_blocks_path(:display_name => target_user.display_name,
365 :user_block_period => "12",
366 :user_block => { :needs_view => false, :reason => "Vandalism" })
368 id = UserBlock.order(:id).ids.last
369 assert_redirected_to user_block_path(:id => id)
370 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
371 b = UserBlock.find(id)
372 assert_in_delta Time.now.utc, b.created_at, 1
373 assert_in_delta Time.now.utc, b.updated_at, 1
374 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
375 assert_not b.needs_view
376 assert_equal "Vandalism", b.reason
377 assert_equal "markdown", b.reason_format
378 assert_equal moderator_user.id, b.creator_id
380 # We should get an error if no user is specified
381 post user_blocks_path
382 assert_response :not_found
383 assert_template "users/no_such_user"
384 assert_select "h1", "The user does not exist"
386 # We should get an error if the user doesn't exist
387 post user_blocks_path(:display_name => "non_existent_user")
388 assert_response :not_found
389 assert_template "users/no_such_user"
390 assert_select "h1", "The user non_existent_user does not exist"
394 # test the duration of a created block
395 def test_create_duration
396 target_user = create(:user)
397 moderator_user = create(:moderator_user)
399 session_for(moderator_user)
400 post user_blocks_path(:display_name => target_user.display_name,
401 :user_block_period => "336",
402 :user_block => { :needs_view => false, :reason => "Vandalism" })
404 block = UserBlock.order(:id).last
405 assert_equal 1209600, block.ends_at - block.created_at
409 # test the update action
411 moderator_user = create(:moderator_user)
412 second_moderator_user = create(:moderator_user)
413 active_block = create(:user_block, :creator => moderator_user)
415 # Not logged in yet, so updating a block should fail
416 put user_block_path(:id => active_block)
417 assert_response :forbidden
419 # Login as a normal user
420 session_for(create(:user))
422 # Check that normal users can't update blocks
423 put user_block_path(:id => active_block)
424 assert_redirected_to :controller => "errors", :action => "forbidden"
426 # Login as the wrong moderator
427 session_for(second_moderator_user)
429 # Check that only the person who created a block can update it
430 assert_no_difference "UserBlock.count" do
431 put user_block_path(:id => active_block,
432 :user_block_period => "12",
433 :user_block => { :needs_view => true, :reason => "Vandalism" })
435 assert_redirected_to edit_user_block_path(active_block)
436 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
438 # Login as the correct moderator
439 session_for(moderator_user)
441 # A bogus block period should result in an error
442 assert_no_difference "UserBlock.count" do
443 put user_block_path(:id => active_block, :user_block_period => "99")
445 assert_redirected_to edit_user_block_path(active_block)
446 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
448 # Check that updating a block works
449 assert_no_difference "UserBlock.count" do
450 put user_block_path(:id => active_block,
451 :user_block_period => "12",
452 :user_block => { :needs_view => true, :reason => "Vandalism" })
454 assert_redirected_to user_block_path(active_block)
455 assert_equal "Block updated.", flash[:notice]
456 b = UserBlock.find(active_block.id)
457 assert_in_delta Time.now.utc, b.updated_at, 1
459 assert_equal "Vandalism", b.reason
461 # We should get an error if the block doesn't exist
462 put user_block_path(:id => 99999)
463 assert_response :not_found
464 assert_template "not_found"
465 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
469 # test the update action on expired blocks
470 def test_update_expired
471 creator_user = create(:moderator_user)
472 other_moderator_user = create(:moderator_user)
473 block = create(:user_block, :expired, :creator => creator_user, :reason => "Original Reason")
475 session_for(other_moderator_user)
476 put user_block_path(block,
477 :user_block_period => "0",
478 :user_block => { :needs_view => false, :reason => "Updated Reason" })
479 assert_redirected_to edit_user_block_path(block)
480 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
482 assert_not block.active?
483 assert_equal "Original Reason", block.reason
485 session_for(creator_user)
486 check_block_updates(block)
490 # test the update action on revoked blocks
491 def test_update_revoked
492 creator_user = create(:moderator_user)
493 revoker_user = create(:moderator_user)
494 other_moderator_user = create(:moderator_user)
495 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user, :reason => "Original Reason")
497 session_for(other_moderator_user)
498 put user_block_path(block,
499 :user_block_period => "0",
500 :user_block => { :needs_view => false, :reason => "Updated Reason" })
501 assert_redirected_to edit_user_block_path(block)
502 assert_equal "Only the moderators who created or revoked this block can edit it.", flash[:error]
504 assert_not_predicate block, :active?
505 assert_equal "Original Reason", block.reason
507 session_for(creator_user)
508 check_block_updates(block)
510 session_for(revoker_user)
511 check_block_updates(block)
515 # test the revoke action
517 active_block = create(:user_block)
519 # Check that the block revoke page requires us to login
520 get revoke_user_block_path(:id => active_block)
521 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
523 # Login as a normal user
524 session_for(create(:user))
526 # Check that normal users can't load the block revoke page
527 get revoke_user_block_path(:id => active_block)
528 assert_redirected_to :controller => "errors", :action => "forbidden"
530 # Login as a moderator
531 session_for(create(:moderator_user))
533 # Check that the block revoke page loads for moderators
534 get revoke_user_block_path(:id => active_block)
535 assert_response :success
536 assert_template "revoke"
537 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
538 assert_select "form", :count => 1 do
539 assert_select "input#confirm[type='checkbox']", :count => 1
540 assert_select "input[type='submit'][value='Revoke!']", :count => 1
543 # Check that revoking a block using GET should fail
544 get revoke_user_block_path(:id => active_block, :confirm => true)
545 assert_response :success
546 assert_template "revoke"
547 b = UserBlock.find(active_block.id)
548 assert_operator b.ends_at - Time.now.utc, :>, 100
550 # Check that revoking a block works using POST
551 post revoke_user_block_path(:id => active_block, :confirm => true)
552 assert_redirected_to user_block_path(active_block)
553 b = UserBlock.find(active_block.id)
554 assert_in_delta Time.now.utc, b.ends_at, 1
556 # We should get an error if the block doesn't exist
557 get revoke_user_block_path(:id => 99999)
558 assert_response :not_found
559 assert_template "not_found"
560 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
564 # test the revoke all page
565 def test_revoke_all_page
566 blocked_user = create(:user)
567 create(:user_block, :user => blocked_user)
569 # Asking for the revoke all blocks page with a bogus user name should fail
570 get user_blocks_on_path("non_existent_user")
571 assert_response :not_found
573 # Check that the revoke all blocks page requires us to login
574 get revoke_all_user_blocks_path(blocked_user)
575 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
577 # Login as a normal user
578 session_for(create(:user))
580 # Check that normal users can't load the revoke all blocks page
581 get revoke_all_user_blocks_path(blocked_user)
582 assert_redirected_to :controller => "errors", :action => "forbidden"
584 # Login as a moderator
585 session_for(create(:moderator_user))
587 # Check that the revoke all blocks page loads for moderators
588 get revoke_all_user_blocks_path(blocked_user)
589 assert_response :success
590 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
594 # test the revoke all action
595 def test_revoke_all_action
596 blocked_user = create(:user)
597 active_block1 = create(:user_block, :user => blocked_user)
598 active_block2 = create(:user_block, :user => blocked_user)
599 expired_block1 = create(:user_block, :expired, :user => blocked_user)
600 blocks = [active_block1, active_block2, expired_block1]
601 moderator_user = create(:moderator_user)
603 assert_predicate active_block1, :active?
604 assert_predicate active_block2, :active?
605 assert_not_predicate expired_block1, :active?
607 # Login as a normal user
608 session_for(create(:user))
610 # Check that normal users can't load the block revoke page
611 get revoke_all_user_blocks_path(:blocked_user)
612 assert_redirected_to :controller => "errors", :action => "forbidden"
614 # Login as a moderator
615 session_for(moderator_user)
617 # Check that revoking blocks using GET should fail
618 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
619 assert_response :success
620 assert_template "revoke_all"
622 blocks.each(&:reload)
623 assert_predicate active_block1, :active?
624 assert_predicate active_block2, :active?
625 assert_not_predicate expired_block1, :active?
627 # Check that revoking blocks works using POST
628 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
629 assert_redirected_to user_blocks_on_path(blocked_user)
631 blocks.each(&:reload)
632 assert_not_predicate active_block1, :active?
633 assert_not_predicate active_block2, :active?
634 assert_not_predicate expired_block1, :active?
635 assert_equal moderator_user, active_block1.revoker
636 assert_equal moderator_user, active_block2.revoker
637 assert_not_equal moderator_user, expired_block1.revoker
641 # test the blocks_on action
643 blocked_user = create(:user)
644 unblocked_user = create(:user)
645 normal_user = create(:user)
646 active_block = create(:user_block, :user => blocked_user)
647 revoked_block = create(:user_block, :revoked, :user => blocked_user)
648 expired_block = create(:user_block, :expired, :user => unblocked_user)
650 # Asking for a list of blocks with a bogus user name should fail
651 get user_blocks_on_path("non_existent_user")
652 assert_response :not_found
653 assert_template "users/no_such_user"
654 assert_select "h1", "The user non_existent_user does not exist"
656 # Check the list of blocks for a user that has never been blocked
657 get user_blocks_on_path(normal_user)
658 assert_response :success
659 assert_select "table#block_list", false
660 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
662 # Check the list of blocks for a user that is currently blocked
663 get user_blocks_on_path(blocked_user)
664 assert_response :success
665 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
666 assert_select "table#block_list tbody", :count => 1 do
667 assert_select "tr", 2
668 assert_select "a[href='#{user_block_path(active_block)}']", 1
669 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
672 # Check the list of blocks for a user that has previously been blocked
673 get user_blocks_on_path(unblocked_user)
674 assert_response :success
675 assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
676 assert_select "table#block_list tbody", :count => 1 do
677 assert_select "tr", 1
678 assert_select "a[href='#{user_block_path(expired_block)}']", 1
683 # test the blocks_on action with multiple pages
684 def test_blocks_on_paged
686 user_blocks = create_list(:user_block, 50, :user => user).reverse
687 next_path = user_blocks_on_path(user)
690 assert_response :success
691 check_user_blocks_table user_blocks[0...20]
692 check_no_page_link "Newer Blocks"
693 next_path = check_page_link "Older Blocks"
696 assert_response :success
697 check_user_blocks_table user_blocks[20...40]
698 check_page_link "Newer Blocks"
699 next_path = check_page_link "Older Blocks"
702 assert_response :success
703 check_user_blocks_table user_blocks[40...50]
704 check_page_link "Newer Blocks"
705 check_no_page_link "Older Blocks"
709 # test the blocks_on action with invalid pages
710 def test_blocks_on_invalid_paged
713 %w[-1 0 fred].each do |id|
714 get user_blocks_on_path(user, :before => id)
715 assert_redirected_to :controller => :errors, :action => :bad_request
717 get user_blocks_on_path(user, :after => id)
718 assert_redirected_to :controller => :errors, :action => :bad_request
723 # test the blocks_by action
725 moderator_user = create(:moderator_user)
726 second_moderator_user = create(:moderator_user)
727 normal_user = create(:user)
728 active_block = create(:user_block, :creator => moderator_user)
729 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
730 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
732 # Asking for a list of blocks with a bogus user name should fail
733 get user_blocks_by_path("non_existent_user")
734 assert_response :not_found
735 assert_template "users/no_such_user"
736 assert_select "h1", "The user non_existent_user does not exist"
738 # Check the list of blocks given by one moderator
739 get user_blocks_by_path(moderator_user)
740 assert_response :success
741 assert_select "h1 a[href='#{user_path moderator_user}']", :text => moderator_user.display_name
742 assert_select "table#block_list tbody", :count => 1 do
743 assert_select "tr", 1
744 assert_select "a[href='#{user_block_path(active_block)}']", 1
747 # Check the list of blocks given by a different moderator
748 get user_blocks_by_path(second_moderator_user)
749 assert_response :success
750 assert_select "h1 a[href='#{user_path second_moderator_user}']", :text => second_moderator_user.display_name
751 assert_select "table#block_list tbody", :count => 1 do
752 assert_select "tr", 2
753 assert_select "a[href='#{user_block_path(expired_block)}']", 1
754 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
757 # Check the list of blocks (not) given by a normal user
758 get user_blocks_by_path(normal_user)
759 assert_response :success
760 assert_select "table#block_list", false
761 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
765 # test the blocks_by action with multiple pages
766 def test_blocks_by_paged
767 user = create(:moderator_user)
768 user_blocks = create_list(:user_block, 50, :creator => user).reverse
769 next_path = user_blocks_by_path(user)
772 assert_response :success
773 check_user_blocks_table user_blocks[0...20]
774 check_no_page_link "Newer Blocks"
775 next_path = check_page_link "Older Blocks"
778 assert_response :success
779 check_user_blocks_table user_blocks[20...40]
780 check_page_link "Newer Blocks"
781 next_path = check_page_link "Older Blocks"
784 assert_response :success
785 check_user_blocks_table user_blocks[40...50]
786 check_page_link "Newer Blocks"
787 check_no_page_link "Older Blocks"
791 # test the blocks_by action with invalid pages
792 def test_blocks_by_invalid_paged
793 user = create(:moderator_user)
795 %w[-1 0 fred].each do |id|
796 get user_blocks_by_path(user, :before => id)
797 assert_redirected_to :controller => :errors, :action => :bad_request
799 get user_blocks_by_path(user, :after => id)
800 assert_redirected_to :controller => :errors, :action => :bad_request
806 def check_block_updates(block)
807 put user_block_path(block,
808 :user_block_period => "0",
809 :user_block => { :needs_view => false, :reason => "Updated Reason" })
810 assert_redirected_to user_block_path(block)
811 assert_equal "Block updated.", flash[:notice]
813 assert_not_predicate block, :active?
814 assert_equal "Updated Reason", block.reason
816 put user_block_path(block,
817 :user_block_period => "0",
818 :user_block => { :needs_view => true, :reason => "Updated Reason 2" })
819 assert_redirected_to user_block_path(block)
820 assert_equal "Block updated.", flash[:notice]
822 assert_predicate block, :active?
823 assert_equal "Updated Reason 2", block.reason
826 def check_user_blocks_table(user_blocks)
827 assert_dom "table#block_list tbody tr" do |rows|
828 assert_equal user_blocks.count, rows.count, "unexpected number of rows in user blocks table"
829 rows.zip(user_blocks).map do |row, user_block|
830 assert_dom row, "a[href='#{user_block_path user_block}']", 1
835 def check_no_page_link(name)
836 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
839 def check_page_link(name)
840 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
841 return buttons.first.attributes["href"].value