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 => :post },
38 { :controller => "user_blocks", :action => "revoke", :id => "1" }
42 { :path => "/user/username/blocks", :method => :get },
43 { :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
46 { :path => "/user/username/blocks_by", :method => :get },
47 { :controller => "user_blocks", :action => "blocks_by", :display_name => "username" }
50 { :path => "/user/username/blocks/revoke_all", :method => :get },
51 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
54 { :path => "/user/username/blocks/revoke_all", :method => :post },
55 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
60 # test the index action
62 revoked_block = create(:user_block, :revoked)
65 assert_response :success
66 assert_select "table#block_list tbody tr", :count => 1 do
67 assert_select "a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
68 assert_select "a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
69 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
72 active_block = create(:user_block)
73 expired_block = create(:user_block, :expired)
76 assert_response :success
77 assert_select "table#block_list tbody", :count => 1 do
79 assert_select "a[href='#{user_block_path(active_block)}']", 1
80 assert_select "a[href='#{user_block_path(expired_block)}']", 1
81 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
86 # test the index action with multiple pages
88 user_blocks = create_list(:user_block, 50).reverse
89 next_path = user_blocks_path
92 assert_response :success
93 check_user_blocks_table user_blocks[0...20]
94 check_no_page_link "Newer Blocks"
95 next_path = check_page_link "Older Blocks"
98 assert_response :success
99 check_user_blocks_table user_blocks[20...40]
100 check_page_link "Newer Blocks"
101 next_path = check_page_link "Older Blocks"
104 assert_response :success
105 check_user_blocks_table user_blocks[40...50]
106 check_page_link "Newer Blocks"
107 check_no_page_link "Older Blocks"
111 # test the index action with invalid pages
112 def test_index_invalid_paged
113 %w[-1 0 fred].each do |id|
114 get user_blocks_path(:before => id)
115 assert_redirected_to :controller => :errors, :action => :bad_request
117 get user_blocks_path(:after => id)
118 assert_redirected_to :controller => :errors, :action => :bad_request
123 # test the show action
125 active_block = create(:user_block, :needs_view)
126 expired_block = create(:user_block, :expired)
127 revoked_block = create(:user_block, :revoked)
129 # Viewing a block should fail when a bogus ID is given
130 get user_block_path(:id => 99999)
131 assert_response :not_found
132 assert_template "not_found"
133 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
135 # Viewing an expired block should work
136 get user_block_path(:id => expired_block)
137 assert_response :success
138 assert_select "h1 a[href='#{user_path expired_block.user}']", :text => expired_block.user.display_name
139 assert_select "h1 a[href='#{user_path expired_block.creator}']", :text => expired_block.creator.display_name
141 # Viewing a revoked block should work
142 get user_block_path(:id => revoked_block)
143 assert_response :success
144 assert_select "h1 a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
145 assert_select "h1 a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
146 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
148 # Viewing an active block should work, but shouldn't mark it as seen
149 get user_block_path(:id => active_block)
150 assert_response :success
151 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
152 assert_select "h1 a[href='#{user_path active_block.creator}']", :text => active_block.creator.display_name
153 assert UserBlock.find(active_block.id).needs_view
155 # Login as the blocked user
156 session_for(active_block.user)
158 # Now viewing it should mark it as seen
159 get user_block_path(:id => active_block)
160 assert_response :success
161 assert_not UserBlock.find(active_block.id).needs_view
165 # test edit/revoke link for active blocks
166 def test_active_block_buttons
167 creator_user = create(:moderator_user)
168 other_moderator_user = create(:moderator_user)
169 block = create(:user_block, :creator => creator_user)
171 session_for(other_moderator_user)
172 check_block_buttons block, :edit => 1
174 session_for(creator_user)
175 check_block_buttons block, :edit => 1
179 # test the edit link for expired blocks
180 def test_expired_block_buttons
181 creator_user = create(:moderator_user)
182 other_moderator_user = create(:moderator_user)
183 block = create(:user_block, :expired, :creator => creator_user)
185 session_for(other_moderator_user)
186 check_block_buttons block
188 session_for(creator_user)
189 check_block_buttons block, :edit => 1
193 # test the edit link for revoked blocks
194 def test_revoked_block_buttons
195 creator_user = create(:moderator_user)
196 revoker_user = create(:moderator_user)
197 other_moderator_user = create(:moderator_user)
198 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user)
200 session_for(other_moderator_user)
201 check_block_buttons block
203 session_for(creator_user)
204 check_block_buttons block, :edit => 1
206 session_for(revoker_user)
207 check_block_buttons block, :edit => 1
211 # test the new action
213 target_user = create(:user)
215 # Check that the block creation page requires us to login
216 get new_user_block_path(target_user)
217 assert_redirected_to login_path(:referer => new_user_block_path(target_user))
219 # Login as a normal user
220 session_for(create(:user))
222 # Check that normal users can't load the block creation page
223 get new_user_block_path(target_user)
224 assert_redirected_to :controller => "errors", :action => "forbidden"
226 # Login as a moderator
227 session_for(create(:moderator_user))
229 # Check that the block creation page loads for moderators
230 get new_user_block_path(target_user)
231 assert_response :success
232 assert_select "h1 a[href='#{user_path target_user}']", :text => target_user.display_name
233 assert_select "form#new_user_block", :count => 1 do
234 assert_select "textarea#user_block_reason", :count => 1
235 assert_select "select#user_block_period", :count => 1
236 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
237 assert_select "input#display_name[type='hidden']", :count => 1
238 assert_select "input[type='submit'][value='Create block']", :count => 1
241 # We should get an error if the user doesn't exist
242 get new_user_block_path(:display_name => "non_existent_user")
243 assert_response :not_found
244 assert_template "users/no_such_user"
245 assert_select "h1", "The user non_existent_user does not exist"
249 # test the edit action
251 creator_user = create(:moderator_user)
252 other_moderator_user = create(:moderator_user)
253 active_block = create(:user_block, :creator => creator_user)
255 # Check that the block edit page requires us to login
256 get edit_user_block_path(:id => active_block)
257 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
259 # Login as a normal user
260 session_for(create(:user))
262 # Check that normal users can't load the block edit page
263 get edit_user_block_path(:id => active_block)
264 assert_redirected_to :controller => "errors", :action => "forbidden"
266 # Login as a moderator
267 session_for(other_moderator_user)
269 # Check that the block edit page loads for moderators
270 get edit_user_block_path(:id => active_block)
271 assert_response :success
272 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
273 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
274 assert_select "textarea#user_block_reason", :count => 1
275 assert_select "select#user_block_period", :count => 0
276 assert_select "input#user_block_needs_view[type='checkbox']", :count => 0
277 assert_select "input[type='submit'][value='Update block']", :count => 0
278 assert_select "input#user_block_period[type='hidden']", :count => 1
279 assert_select "input#user_block_needs_view[type='hidden']", :count => 1
280 assert_select "input[type='submit'][value='Revoke block']", :count => 1
283 # Login as the block creator
284 session_for(creator_user)
286 # Check that the block edit page loads for the creator
287 get edit_user_block_path(:id => active_block)
288 assert_response :success
289 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
290 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
291 assert_select "textarea#user_block_reason", :count => 1
292 assert_select "select#user_block_period", :count => 1
293 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
294 assert_select "input[type='submit'][value='Update block']", :count => 1
295 assert_select "input#user_block_period[type='hidden']", :count => 0
296 assert_select "input#user_block_needs_view[type='hidden']", :count => 0
297 assert_select "input[type='submit'][value='Revoke block']", :count => 0
300 # We should get an error if the user doesn't exist
301 get edit_user_block_path(:id => 99999)
302 assert_response :not_found
303 assert_template "not_found"
304 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
308 # test the edit action when the remaining block duration doesn't match the available select options
309 def test_edit_duration
310 moderator_user = create(:moderator_user)
313 active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours)
315 session_for(moderator_user)
316 get edit_user_block_path(active_block)
318 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
319 assert_select "select#user_block_period", :count => 1 do
320 assert_select "option[value='96'][selected]", :count => 1
325 get edit_user_block_path(active_block)
327 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
328 assert_select "select#user_block_period", :count => 1 do
329 assert_select "option[value='96'][selected]", :count => 1
336 # test the create action
338 target_user = create(:user)
339 moderator_user = create(:moderator_user)
341 # Not logged in yet, so creating a block should fail
342 post user_blocks_path
343 assert_response :forbidden
345 # Login as a normal user
346 session_for(create(:user))
348 # Check that normal users can't create blocks
349 post user_blocks_path
350 assert_redirected_to :controller => "errors", :action => "forbidden"
352 # Login as a moderator
353 session_for(moderator_user)
355 # A bogus block period should result in an error
356 assert_no_difference "UserBlock.count" do
357 post user_blocks_path(:display_name => target_user.display_name,
358 :user_block_period => "99")
360 assert_redirected_to new_user_block_path(target_user)
361 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
363 # Check that creating a block works
364 assert_difference "UserBlock.count", 1 do
365 post user_blocks_path(:display_name => target_user.display_name,
366 :user_block_period => "12",
367 :user_block => { :needs_view => false, :reason => "Vandalism" })
370 assert_redirected_to user_block_path(:id => b.id)
371 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
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.last
405 assert_equal 1209600, block.ends_at - block.created_at
409 # test the update action
411 moderator_user = create(:moderator_user)
412 active_block = create(:user_block, :creator => moderator_user)
414 # Not logged in yet, so updating a block should fail
415 put user_block_path(:id => active_block)
416 assert_response :forbidden
418 # Login as a normal user
419 session_for(create(:user))
421 # Check that normal users can't update blocks
422 put user_block_path(:id => active_block)
423 assert_redirected_to :controller => "errors", :action => "forbidden"
425 # Login as the moderator
426 session_for(moderator_user)
428 # A bogus block period should result in an error
429 assert_no_difference "UserBlock.count" do
430 put user_block_path(:id => active_block, :user_block_period => "99")
432 assert_redirected_to edit_user_block_path(active_block)
433 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
435 # Check that updating a block works
436 assert_no_difference "UserBlock.count" do
437 put user_block_path(:id => active_block,
438 :user_block_period => "12",
439 :user_block => { :needs_view => true, :reason => "Vandalism" })
441 assert_redirected_to user_block_path(active_block)
442 assert_equal "Block updated.", flash[:notice]
443 b = UserBlock.find(active_block.id)
444 assert_in_delta Time.now.utc, b.updated_at, 1
446 assert_equal "Vandalism", b.reason
448 # We should get an error if the block doesn't exist
449 put user_block_path(:id => 99999)
450 assert_response :not_found
451 assert_template "not_found"
452 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
456 # test the update action on expired blocks
457 def test_update_expired
458 creator_user = create(:moderator_user)
459 other_moderator_user = create(:moderator_user)
460 block = create(:user_block, :expired, :creator => creator_user, :reason => "Original Reason")
462 session_for(other_moderator_user)
463 put user_block_path(block,
464 :user_block_period => "0",
465 :user_block => { :needs_view => false, :reason => "Updated Reason" })
466 assert_redirected_to edit_user_block_path(block)
467 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
469 assert_not block.active?
470 assert_equal "Original Reason", block.reason
472 session_for(creator_user)
473 check_inactive_block_updates(block)
477 # test the update action on revoked blocks
478 def test_update_revoked
479 creator_user = create(:moderator_user)
480 revoker_user = create(:moderator_user)
481 other_moderator_user = create(:moderator_user)
482 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user, :reason => "Original Reason")
484 session_for(other_moderator_user)
485 put user_block_path(block,
486 :user_block_period => "0",
487 :user_block => { :needs_view => false, :reason => "Updated Reason" })
488 assert_redirected_to edit_user_block_path(block)
489 assert_equal "Only the moderators who created or revoked this block can edit it.", flash[:error]
491 assert_not_predicate block, :active?
492 assert_equal "Original Reason", block.reason
494 session_for(creator_user)
495 check_inactive_block_updates(block)
497 session_for(revoker_user)
498 check_inactive_block_updates(block)
502 # test the update action revoking the block
503 def test_revoke_using_update_by_creator
504 moderator_user = create(:moderator_user)
505 block = create(:user_block, :creator => moderator_user)
507 session_for(moderator_user)
508 put user_block_path(block,
509 :user_block_period => "24",
510 :user_block => { :needs_view => false, :reason => "Updated Reason" })
511 assert_redirected_to user_block_path(block)
512 assert_equal "Block updated.", flash[:notice]
514 assert_predicate block, :active?
515 assert_nil block.revoker
517 put user_block_path(block,
518 :user_block_period => "0",
519 :user_block => { :needs_view => false, :reason => "Updated Reason" })
520 assert_redirected_to user_block_path(block)
521 assert_equal "Block updated.", flash[:notice]
523 assert_not_predicate block, :active?
524 assert_equal moderator_user, block.revoker
527 def test_revoke_using_update_by_other_moderator
528 creator_user = create(:moderator_user)
529 other_moderator_user = create(:moderator_user)
530 block = create(:user_block, :creator => creator_user)
532 session_for(other_moderator_user)
533 put user_block_path(block,
534 :user_block_period => "24",
535 :user_block => { :needs_view => false, :reason => "Updated Reason" })
536 assert_response :success
537 assert_equal "Only the moderator who created this block can edit it without revoking.", flash[:error]
539 assert_predicate block, :active?
540 assert_nil block.revoker
542 put user_block_path(block,
543 :user_block_period => "0",
544 :user_block => { :needs_view => false, :reason => "Updated Reason" })
545 assert_redirected_to user_block_path(block)
546 assert_equal "Block updated.", flash[:notice]
548 assert_not_predicate block, :active?
549 assert_equal other_moderator_user, block.revoker
553 # test the revoke action
555 active_block = create(:user_block)
557 # Login as a moderator
558 session_for(create(:moderator_user))
560 # Check that revoking a block works using POST
561 post revoke_user_block_path(:id => active_block, :confirm => true)
562 assert_redirected_to user_block_path(active_block)
563 b = UserBlock.find(active_block.id)
564 assert_in_delta Time.now.utc, b.ends_at, 1
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)
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
946 def check_inactive_block_updates(block)
947 original_ends_at = block.ends_at
949 put user_block_path(block,
950 :user_block_period => "0",
951 :user_block => { :needs_view => false, :reason => "Updated Reason" })
952 assert_redirected_to user_block_path(block)
953 assert_equal "Block updated.", flash[:notice]
955 assert_not_predicate block, :active?
956 assert_equal "Updated Reason", block.reason
957 assert_equal original_ends_at, block.ends_at
959 put user_block_path(block,
960 :user_block_period => "0",
961 :user_block => { :needs_view => true, :reason => "Updated Reason Needs View" })
962 assert_response :success
963 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
965 assert_not_predicate block, :active?
966 assert_equal "Updated Reason", block.reason
967 assert_equal original_ends_at, block.ends_at
969 put user_block_path(block,
970 :user_block_period => "1",
971 :user_block => { :needs_view => false, :reason => "Updated Reason Duration Extended" })
972 assert_response :success
973 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
975 assert_not_predicate block, :active?
976 assert_equal "Updated Reason", block.reason
977 assert_equal original_ends_at, block.ends_at
979 put user_block_path(block,
980 :user_block_period => "0",
981 :user_block => { :needs_view => false, :reason => "Updated Reason Again" })
982 assert_redirected_to user_block_path(block)
983 assert_equal "Block updated.", flash[:notice]
985 assert_not_predicate block, :active?
986 assert_equal "Updated Reason Again", block.reason
987 assert_equal original_ends_at, block.ends_at
990 def check_user_blocks_table(user_blocks)
991 assert_dom "table#block_list tbody tr" do |rows|
992 assert_equal user_blocks.count, rows.count, "unexpected number of rows in user blocks table"
993 rows.zip(user_blocks).map do |row, user_block|
994 assert_dom row, "a[href='#{user_block_path user_block}']", 1
999 def check_no_page_link(name)
1000 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
1003 def check_page_link(name)
1004 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
1005 return buttons.first.attributes["href"].value