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 active_block = create(:user_block)
257 # Check that the block edit page requires us to login
258 get edit_user_block_path(:id => active_block)
259 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
261 # Login as a normal user
262 session_for(create(:user))
264 # Check that normal users can't load the block edit page
265 get edit_user_block_path(:id => active_block)
266 assert_redirected_to :controller => "errors", :action => "forbidden"
268 # Login as a moderator
269 session_for(create(:moderator_user))
271 # Check that the block edit page loads for moderators
272 get edit_user_block_path(:id => active_block)
273 assert_response :success
274 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
275 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
276 assert_select "textarea#user_block_reason", :count => 1
277 assert_select "select#user_block_period", :count => 1
278 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
279 assert_select "input[type='submit'][value='Update block']", :count => 1
282 # We should get an error if the user doesn't exist
283 get edit_user_block_path(:id => 99999)
284 assert_response :not_found
285 assert_template "not_found"
286 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
290 # test the edit action when the remaining block duration doesn't match the available select options
291 def test_edit_duration
292 moderator_user = create(:moderator_user)
295 active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours)
297 session_for(moderator_user)
298 get edit_user_block_path(active_block)
300 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
301 assert_select "select#user_block_period", :count => 1 do
302 assert_select "option[value='96'][selected]", :count => 1
307 get edit_user_block_path(active_block)
309 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
310 assert_select "select#user_block_period", :count => 1 do
311 assert_select "option[value='96'][selected]", :count => 1
318 # test the create action
320 target_user = create(:user)
321 moderator_user = create(:moderator_user)
323 # Not logged in yet, so creating a block should fail
324 post user_blocks_path
325 assert_response :forbidden
327 # Login as a normal user
328 session_for(create(:user))
330 # Check that normal users can't create blocks
331 post user_blocks_path
332 assert_redirected_to :controller => "errors", :action => "forbidden"
334 # Login as a moderator
335 session_for(moderator_user)
337 # A bogus block period should result in an error
338 assert_no_difference "UserBlock.count" do
339 post user_blocks_path(:display_name => target_user.display_name,
340 :user_block_period => "99")
342 assert_redirected_to new_user_block_path(target_user)
343 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
345 # Check that creating a block works
346 assert_difference "UserBlock.count", 1 do
347 post user_blocks_path(:display_name => target_user.display_name,
348 :user_block_period => "12",
349 :user_block => { :needs_view => false, :reason => "Vandalism" })
352 assert_redirected_to user_block_path(:id => b.id)
353 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
354 assert_in_delta Time.now.utc, b.created_at, 1
355 assert_in_delta Time.now.utc, b.updated_at, 1
356 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
357 assert_not b.needs_view
358 assert_equal "Vandalism", b.reason
359 assert_equal "markdown", b.reason_format
360 assert_equal moderator_user.id, b.creator_id
362 # We should get an error if no user is specified
363 post user_blocks_path
364 assert_response :not_found
365 assert_template "users/no_such_user"
366 assert_select "h1", "The user does not exist"
368 # We should get an error if the user doesn't exist
369 post user_blocks_path(:display_name => "non_existent_user")
370 assert_response :not_found
371 assert_template "users/no_such_user"
372 assert_select "h1", "The user non_existent_user does not exist"
376 # test the duration of a created block
377 def test_create_duration
378 target_user = create(:user)
379 moderator_user = create(:moderator_user)
381 session_for(moderator_user)
382 post user_blocks_path(:display_name => target_user.display_name,
383 :user_block_period => "336",
384 :user_block => { :needs_view => false, :reason => "Vandalism" })
386 block = UserBlock.last
387 assert_equal 1209600, block.ends_at - block.created_at
391 # test the update action
393 moderator_user = create(:moderator_user)
394 second_moderator_user = create(:moderator_user)
395 active_block = create(:user_block, :creator => moderator_user)
397 # Not logged in yet, so updating a block should fail
398 put user_block_path(:id => active_block)
399 assert_response :forbidden
401 # Login as a normal user
402 session_for(create(:user))
404 # Check that normal users can't update blocks
405 put user_block_path(:id => active_block)
406 assert_redirected_to :controller => "errors", :action => "forbidden"
408 # Login as the wrong moderator
409 session_for(second_moderator_user)
411 # Check that only the person who created a block can update it
412 assert_no_difference "UserBlock.count" do
413 put user_block_path(:id => active_block,
414 :user_block_period => "12",
415 :user_block => { :needs_view => true, :reason => "Vandalism" })
417 assert_redirected_to edit_user_block_path(active_block)
418 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
420 # Login as the correct moderator
421 session_for(moderator_user)
423 # A bogus block period should result in an error
424 assert_no_difference "UserBlock.count" do
425 put user_block_path(:id => active_block, :user_block_period => "99")
427 assert_redirected_to edit_user_block_path(active_block)
428 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
430 # Check that updating a block works
431 assert_no_difference "UserBlock.count" do
432 put user_block_path(:id => active_block,
433 :user_block_period => "12",
434 :user_block => { :needs_view => true, :reason => "Vandalism" })
436 assert_redirected_to user_block_path(active_block)
437 assert_equal "Block updated.", flash[:notice]
438 b = UserBlock.find(active_block.id)
439 assert_in_delta Time.now.utc, b.updated_at, 1
441 assert_equal "Vandalism", b.reason
443 # We should get an error if the block doesn't exist
444 put user_block_path(:id => 99999)
445 assert_response :not_found
446 assert_template "not_found"
447 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
451 # test the update action on expired blocks
452 def test_update_expired
453 creator_user = create(:moderator_user)
454 other_moderator_user = create(:moderator_user)
455 block = create(:user_block, :expired, :creator => creator_user, :reason => "Original Reason")
457 session_for(other_moderator_user)
458 put user_block_path(block,
459 :user_block_period => "0",
460 :user_block => { :needs_view => false, :reason => "Updated Reason" })
461 assert_redirected_to edit_user_block_path(block)
462 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
464 assert_not block.active?
465 assert_equal "Original Reason", block.reason
467 session_for(creator_user)
468 check_inactive_block_updates(block)
472 # test the update action on revoked blocks
473 def test_update_revoked
474 creator_user = create(:moderator_user)
475 revoker_user = create(:moderator_user)
476 other_moderator_user = create(:moderator_user)
477 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user, :reason => "Original Reason")
479 session_for(other_moderator_user)
480 put user_block_path(block,
481 :user_block_period => "0",
482 :user_block => { :needs_view => false, :reason => "Updated Reason" })
483 assert_redirected_to edit_user_block_path(block)
484 assert_equal "Only the moderators who created or revoked this block can edit it.", flash[:error]
486 assert_not_predicate block, :active?
487 assert_equal "Original Reason", block.reason
489 session_for(creator_user)
490 check_inactive_block_updates(block)
492 session_for(revoker_user)
493 check_inactive_block_updates(block)
497 # test the update action revoking the block
498 def test_revoke_using_update
499 moderator_user = create(:moderator_user)
500 block = create(:user_block, :creator => moderator_user)
502 session_for(moderator_user)
503 put user_block_path(block,
504 :user_block_period => "24",
505 :user_block => { :needs_view => false, :reason => "Updated Reason" })
507 assert_predicate block, :active?
508 assert_nil block.revoker
510 put user_block_path(block,
511 :user_block_period => "0",
512 :user_block => { :needs_view => false, :reason => "Updated Reason" })
514 assert_not_predicate block, :active?
515 assert_equal moderator_user, block.revoker
519 # test the revoke action
521 active_block = create(:user_block)
523 # Check that the block revoke page requires us to login
524 get revoke_user_block_path(:id => active_block)
525 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
527 # Login as a normal user
528 session_for(create(:user))
530 # Check that normal users can't load the block revoke page
531 get revoke_user_block_path(:id => active_block)
532 assert_redirected_to :controller => "errors", :action => "forbidden"
534 # Login as a moderator
535 session_for(create(:moderator_user))
537 # Check that the block revoke page loads for moderators
538 get revoke_user_block_path(:id => active_block)
539 assert_response :success
540 assert_template "revoke"
541 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
542 assert_select "form", :count => 1 do
543 assert_select "input#confirm[type='checkbox']", :count => 1
544 assert_select "input[type='submit'][value='Revoke!']", :count => 1
547 # Check that revoking a block using GET should fail
548 get revoke_user_block_path(:id => active_block, :confirm => true)
549 assert_response :success
550 assert_template "revoke"
551 b = UserBlock.find(active_block.id)
552 assert_operator b.ends_at - Time.now.utc, :>, 100
554 # Check that revoking a block works using POST
555 post revoke_user_block_path(:id => active_block, :confirm => true)
556 assert_redirected_to user_block_path(active_block)
557 b = UserBlock.find(active_block.id)
558 assert_in_delta Time.now.utc, b.ends_at, 1
560 # We should get an error if the block doesn't exist
561 get revoke_user_block_path(:id => 99999)
562 assert_response :not_found
563 assert_template "not_found"
564 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
568 # test the revoke all page
569 def test_revoke_all_page
570 blocked_user = create(:user)
571 create(:user_block, :user => blocked_user)
573 # Asking for the revoke all blocks page with a bogus user name should fail
574 get user_blocks_on_path("non_existent_user")
575 assert_response :not_found
577 # Check that the revoke all blocks page requires us to login
578 get revoke_all_user_blocks_path(blocked_user)
579 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
581 # Login as a normal user
582 session_for(create(:user))
584 # Check that normal users can't load the revoke all blocks page
585 get revoke_all_user_blocks_path(blocked_user)
586 assert_redirected_to :controller => "errors", :action => "forbidden"
588 # Login as a moderator
589 session_for(create(:moderator_user))
591 # Check that the revoke all blocks page loads for moderators
592 get revoke_all_user_blocks_path(blocked_user)
593 assert_response :success
594 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
598 # test the revoke all action
599 def test_revoke_all_action
600 blocked_user = create(:user)
601 active_block1 = create(:user_block, :user => blocked_user)
602 active_block2 = create(:user_block, :user => blocked_user)
603 expired_block1 = create(:user_block, :expired, :user => blocked_user)
604 blocks = [active_block1, active_block2, expired_block1]
605 moderator_user = create(:moderator_user)
607 assert_predicate active_block1, :active?
608 assert_predicate active_block2, :active?
609 assert_not_predicate expired_block1, :active?
611 # Login as a normal user
612 session_for(create(:user))
614 # Check that normal users can't load the block revoke page
615 get revoke_all_user_blocks_path(:blocked_user)
616 assert_redirected_to :controller => "errors", :action => "forbidden"
618 # Login as a moderator
619 session_for(moderator_user)
621 # Check that revoking blocks using GET should fail
622 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
623 assert_response :success
624 assert_template "revoke_all"
626 blocks.each(&:reload)
627 assert_predicate active_block1, :active?
628 assert_predicate active_block2, :active?
629 assert_not_predicate expired_block1, :active?
631 # Check that revoking blocks works using POST
632 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
633 assert_redirected_to user_blocks_on_path(blocked_user)
635 blocks.each(&:reload)
636 assert_not_predicate active_block1, :active?
637 assert_not_predicate active_block2, :active?
638 assert_not_predicate expired_block1, :active?
639 assert_equal moderator_user, active_block1.revoker
640 assert_equal moderator_user, active_block2.revoker
641 assert_not_equal moderator_user, expired_block1.revoker
645 # test changes to end/deactivation dates
646 def test_dates_when_viewed_before_end
647 blocked_user = create(:user)
648 moderator_user = create(:moderator_user)
651 session_for(moderator_user)
652 assert_difference "UserBlock.count", 1 do
653 post user_blocks_path(:display_name => blocked_user.display_name,
654 :user_block_period => "48",
655 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
657 block = UserBlock.last
658 assert_equal Time.now.utc + 2.days, block.ends_at
659 assert_nil block.deactivates_at
662 session_for(blocked_user)
663 get user_block_path(block)
665 assert_equal Time.now.utc + 1.day, block.ends_at
666 assert_equal Time.now.utc + 1.day, block.deactivates_at
670 def test_dates_when_viewed_after_end
671 blocked_user = create(:user)
672 moderator_user = create(:moderator_user)
675 session_for(moderator_user)
676 assert_difference "UserBlock.count", 1 do
677 post user_blocks_path(:display_name => blocked_user.display_name,
678 :user_block_period => "24",
679 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
681 block = UserBlock.last
682 assert_equal Time.now.utc + 1.day, block.ends_at
683 assert_nil block.deactivates_at
686 session_for(blocked_user)
687 get user_block_path(block)
689 assert_equal Time.now.utc - 1.day, block.ends_at
690 assert_equal Time.now.utc, block.deactivates_at
694 def test_dates_when_edited_before_end
695 blocked_user = create(:user)
696 moderator_user = create(:moderator_user)
699 session_for(moderator_user)
700 assert_difference "UserBlock.count", 1 do
701 post user_blocks_path(:display_name => blocked_user.display_name,
702 :user_block_period => "48",
703 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
705 block = UserBlock.last
706 assert_equal Time.now.utc + 2.days, block.ends_at
707 assert_equal Time.now.utc + 2.days, block.deactivates_at
710 put user_block_path(block,
711 :user_block_period => "48",
712 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
714 assert_equal Time.now.utc + 2.days, block.ends_at
715 assert_equal Time.now.utc + 2.days, block.deactivates_at
719 def test_dates_when_edited_after_end
720 blocked_user = create(:user)
721 moderator_user = create(:moderator_user)
724 session_for(moderator_user)
725 assert_difference "UserBlock.count", 1 do
726 post user_blocks_path(:display_name => blocked_user.display_name,
727 :user_block_period => "24",
728 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
730 block = UserBlock.last
731 assert_equal Time.now.utc + 1.day, block.ends_at
732 assert_equal Time.now.utc + 1.day, block.deactivates_at
735 put user_block_path(block,
736 :user_block_period => "0",
737 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
739 assert_equal Time.now.utc - 1.day, block.ends_at
740 assert_equal Time.now.utc - 1.day, block.deactivates_at
745 # test updates on legacy records without correctly initialized deactivates_at
746 def test_update_legacy_deactivates_at
747 blocked_user = create(:user)
748 moderator_user = create(:moderator_user)
751 block = UserBlock.new :user => blocked_user,
752 :creator => moderator_user,
753 :reason => "because",
754 :ends_at => Time.now.utc + 1.day,
757 assert_difference "UserBlock.count", 1 do
758 block.save :validate => false
762 session_for(moderator_user)
763 put user_block_path(block,
764 :user_block_period => "0",
765 :user_block => { :needs_view => false, :reason => "Testing legacy block update" })
767 assert_equal Time.now.utc - 1.day, block.ends_at
768 assert_equal Time.now.utc - 1.day, block.deactivates_at
773 # test the blocks_on action
775 blocked_user = create(:user)
776 unblocked_user = create(:user)
777 normal_user = create(:user)
778 active_block = create(:user_block, :user => blocked_user)
779 revoked_block = create(:user_block, :revoked, :user => blocked_user)
780 expired_block = create(:user_block, :expired, :user => unblocked_user)
782 # Asking for a list of blocks with a bogus user name should fail
783 get user_blocks_on_path("non_existent_user")
784 assert_response :not_found
785 assert_template "users/no_such_user"
786 assert_select "h1", "The user non_existent_user does not exist"
788 # Check the list of blocks for a user that has never been blocked
789 get user_blocks_on_path(normal_user)
790 assert_response :success
791 assert_select "table#block_list", false
792 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
794 # Check the list of blocks for a user that is currently blocked
795 get user_blocks_on_path(blocked_user)
796 assert_response :success
797 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
798 assert_select "table#block_list tbody", :count => 1 do
799 assert_select "tr", 2
800 assert_select "a[href='#{user_block_path(active_block)}']", 1
801 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
804 # Check the list of blocks for a user that has previously been blocked
805 get user_blocks_on_path(unblocked_user)
806 assert_response :success
807 assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
808 assert_select "table#block_list tbody", :count => 1 do
809 assert_select "tr", 1
810 assert_select "a[href='#{user_block_path(expired_block)}']", 1
815 # test the blocks_on action with multiple pages
816 def test_blocks_on_paged
818 user_blocks = create_list(:user_block, 50, :user => user).reverse
819 next_path = user_blocks_on_path(user)
822 assert_response :success
823 check_user_blocks_table user_blocks[0...20]
824 check_no_page_link "Newer Blocks"
825 next_path = check_page_link "Older Blocks"
828 assert_response :success
829 check_user_blocks_table user_blocks[20...40]
830 check_page_link "Newer Blocks"
831 next_path = check_page_link "Older Blocks"
834 assert_response :success
835 check_user_blocks_table user_blocks[40...50]
836 check_page_link "Newer Blocks"
837 check_no_page_link "Older Blocks"
841 # test the blocks_on action with invalid pages
842 def test_blocks_on_invalid_paged
845 %w[-1 0 fred].each do |id|
846 get user_blocks_on_path(user, :before => id)
847 assert_redirected_to :controller => :errors, :action => :bad_request
849 get user_blocks_on_path(user, :after => id)
850 assert_redirected_to :controller => :errors, :action => :bad_request
855 # test the blocks_by action
857 moderator_user = create(:moderator_user)
858 second_moderator_user = create(:moderator_user)
859 normal_user = create(:user)
860 active_block = create(:user_block, :creator => moderator_user)
861 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
862 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
864 # Asking for a list of blocks with a bogus user name should fail
865 get user_blocks_by_path("non_existent_user")
866 assert_response :not_found
867 assert_template "users/no_such_user"
868 assert_select "h1", "The user non_existent_user does not exist"
870 # Check the list of blocks given by one moderator
871 get user_blocks_by_path(moderator_user)
872 assert_response :success
873 assert_select "h1 a[href='#{user_path moderator_user}']", :text => moderator_user.display_name
874 assert_select "table#block_list tbody", :count => 1 do
875 assert_select "tr", 1
876 assert_select "a[href='#{user_block_path(active_block)}']", 1
879 # Check the list of blocks given by a different moderator
880 get user_blocks_by_path(second_moderator_user)
881 assert_response :success
882 assert_select "h1 a[href='#{user_path second_moderator_user}']", :text => second_moderator_user.display_name
883 assert_select "table#block_list tbody", :count => 1 do
884 assert_select "tr", 2
885 assert_select "a[href='#{user_block_path(expired_block)}']", 1
886 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
889 # Check the list of blocks (not) given by a normal user
890 get user_blocks_by_path(normal_user)
891 assert_response :success
892 assert_select "table#block_list", false
893 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
897 # test the blocks_by action with multiple pages
898 def test_blocks_by_paged
899 user = create(:moderator_user)
900 user_blocks = create_list(:user_block, 50, :creator => user).reverse
901 next_path = user_blocks_by_path(user)
904 assert_response :success
905 check_user_blocks_table user_blocks[0...20]
906 check_no_page_link "Newer Blocks"
907 next_path = check_page_link "Older Blocks"
910 assert_response :success
911 check_user_blocks_table user_blocks[20...40]
912 check_page_link "Newer Blocks"
913 next_path = check_page_link "Older Blocks"
916 assert_response :success
917 check_user_blocks_table user_blocks[40...50]
918 check_page_link "Newer Blocks"
919 check_no_page_link "Older Blocks"
923 # test the blocks_by action with invalid pages
924 def test_blocks_by_invalid_paged
925 user = create(:moderator_user)
927 %w[-1 0 fred].each do |id|
928 get user_blocks_by_path(user, :before => id)
929 assert_redirected_to :controller => :errors, :action => :bad_request
931 get user_blocks_by_path(user, :after => id)
932 assert_redirected_to :controller => :errors, :action => :bad_request
938 def check_block_buttons(block, edit: 0, revoke: 0)
939 [user_blocks_path, user_block_path(block)].each do |path|
941 assert_response :success
942 assert_select "a[href='#{edit_user_block_path block}']", :count => edit
943 assert_select "a[href='#{revoke_user_block_path block}']", :count => revoke
947 def check_inactive_block_updates(block)
948 original_ends_at = block.ends_at
950 put user_block_path(block,
951 :user_block_period => "0",
952 :user_block => { :needs_view => false, :reason => "Updated Reason" })
953 assert_redirected_to user_block_path(block)
954 assert_equal "Block updated.", flash[:notice]
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 => true, :reason => "Updated Reason Needs View" })
963 assert_response :success
964 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
966 assert_not_predicate block, :active?
967 assert_equal "Updated Reason", block.reason
968 assert_equal original_ends_at, block.ends_at
970 put user_block_path(block,
971 :user_block_period => "1",
972 :user_block => { :needs_view => false, :reason => "Updated Reason Duration Extended" })
973 assert_response :success
974 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
976 assert_not_predicate block, :active?
977 assert_equal "Updated Reason", block.reason
978 assert_equal original_ends_at, block.ends_at
980 put user_block_path(block,
981 :user_block_period => "0",
982 :user_block => { :needs_view => false, :reason => "Updated Reason Again" })
983 assert_redirected_to user_block_path(block)
984 assert_equal "Block updated.", flash[:notice]
986 assert_not_predicate block, :active?
987 assert_equal "Updated Reason Again", block.reason
988 assert_equal original_ends_at, block.ends_at
991 def check_user_blocks_table(user_blocks)
992 assert_dom "table#block_list tbody tr" do |rows|
993 assert_equal user_blocks.count, rows.count, "unexpected number of rows in user blocks table"
994 rows.zip(user_blocks).map do |row, user_block|
995 assert_dom row, "a[href='#{user_block_path user_block}']", 1
1000 def check_no_page_link(name)
1001 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
1004 def check_page_link(name)
1005 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
1006 return buttons.first.attributes["href"].value