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
178 session_for(creator_user)
179 check_block_buttons block, :edit => 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 creator_user = create(:moderator_user)
256 other_moderator_user = create(:moderator_user)
257 active_block = create(:user_block, :creator => creator_user)
259 # Check that the block edit page requires us to login
260 get edit_user_block_path(:id => active_block)
261 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
263 # Login as a normal user
264 session_for(create(:user))
266 # Check that normal users can't load the block edit page
267 get edit_user_block_path(:id => active_block)
268 assert_redirected_to :controller => "errors", :action => "forbidden"
270 # Login as a moderator
271 session_for(other_moderator_user)
273 # Check that the block edit page loads for moderators
274 get edit_user_block_path(:id => active_block)
275 assert_response :success
276 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
277 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
278 assert_select "textarea#user_block_reason", :count => 1
279 assert_select "select#user_block_period", :count => 0
280 assert_select "input#user_block_needs_view[type='checkbox']", :count => 0
281 assert_select "input[type='submit'][value='Update block']", :count => 0
282 assert_select "input#user_block_period[type='hidden']", :count => 1
283 assert_select "input#user_block_needs_view[type='hidden']", :count => 1
284 assert_select "input[type='submit'][value='Revoke block']", :count => 1
287 # Login as the block creator
288 session_for(creator_user)
290 # Check that the block edit page loads for the creator
291 get edit_user_block_path(:id => active_block)
292 assert_response :success
293 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
294 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
295 assert_select "textarea#user_block_reason", :count => 1
296 assert_select "select#user_block_period", :count => 1
297 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
298 assert_select "input[type='submit'][value='Update block']", :count => 1
299 assert_select "input#user_block_period[type='hidden']", :count => 0
300 assert_select "input#user_block_needs_view[type='hidden']", :count => 0
301 assert_select "input[type='submit'][value='Revoke block']", :count => 0
304 # We should get an error if the user doesn't exist
305 get edit_user_block_path(:id => 99999)
306 assert_response :not_found
307 assert_template "not_found"
308 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
312 # test the edit action when the remaining block duration doesn't match the available select options
313 def test_edit_duration
314 moderator_user = create(:moderator_user)
317 active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours)
319 session_for(moderator_user)
320 get edit_user_block_path(active_block)
322 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
323 assert_select "select#user_block_period", :count => 1 do
324 assert_select "option[value='96'][selected]", :count => 1
329 get edit_user_block_path(active_block)
331 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
332 assert_select "select#user_block_period", :count => 1 do
333 assert_select "option[value='96'][selected]", :count => 1
340 # test the create action
342 target_user = create(:user)
343 moderator_user = create(:moderator_user)
345 # Not logged in yet, so creating a block should fail
346 post user_blocks_path
347 assert_response :forbidden
349 # Login as a normal user
350 session_for(create(:user))
352 # Check that normal users can't create blocks
353 post user_blocks_path
354 assert_redirected_to :controller => "errors", :action => "forbidden"
356 # Login as a moderator
357 session_for(moderator_user)
359 # A bogus block period should result in an error
360 assert_no_difference "UserBlock.count" do
361 post user_blocks_path(:display_name => target_user.display_name,
362 :user_block_period => "99")
364 assert_redirected_to new_user_block_path(target_user)
365 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
367 # Check that creating a block works
368 assert_difference "UserBlock.count", 1 do
369 post user_blocks_path(:display_name => target_user.display_name,
370 :user_block_period => "12",
371 :user_block => { :needs_view => false, :reason => "Vandalism" })
374 assert_redirected_to user_block_path(:id => b.id)
375 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
376 assert_in_delta Time.now.utc, b.created_at, 1
377 assert_in_delta Time.now.utc, b.updated_at, 1
378 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
379 assert_not b.needs_view
380 assert_equal "Vandalism", b.reason
381 assert_equal "markdown", b.reason_format
382 assert_equal moderator_user.id, b.creator_id
384 # We should get an error if no user is specified
385 post user_blocks_path
386 assert_response :not_found
387 assert_template "users/no_such_user"
388 assert_select "h1", "The user does not exist"
390 # We should get an error if the user doesn't exist
391 post user_blocks_path(:display_name => "non_existent_user")
392 assert_response :not_found
393 assert_template "users/no_such_user"
394 assert_select "h1", "The user non_existent_user does not exist"
398 # test the duration of a created block
399 def test_create_duration
400 target_user = create(:user)
401 moderator_user = create(:moderator_user)
403 session_for(moderator_user)
404 post user_blocks_path(:display_name => target_user.display_name,
405 :user_block_period => "336",
406 :user_block => { :needs_view => false, :reason => "Vandalism" })
408 block = UserBlock.last
409 assert_equal 1209600, block.ends_at - block.created_at
413 # test the update action
415 moderator_user = create(:moderator_user)
416 active_block = create(:user_block, :creator => moderator_user)
418 # Not logged in yet, so updating a block should fail
419 put user_block_path(:id => active_block)
420 assert_response :forbidden
422 # Login as a normal user
423 session_for(create(:user))
425 # Check that normal users can't update blocks
426 put user_block_path(:id => active_block)
427 assert_redirected_to :controller => "errors", :action => "forbidden"
429 # Login as the moderator
430 session_for(moderator_user)
432 # A bogus block period should result in an error
433 assert_no_difference "UserBlock.count" do
434 put user_block_path(:id => active_block, :user_block_period => "99")
436 assert_redirected_to edit_user_block_path(active_block)
437 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
439 # Check that updating a block works
440 assert_no_difference "UserBlock.count" do
441 put user_block_path(:id => active_block,
442 :user_block_period => "12",
443 :user_block => { :needs_view => true, :reason => "Vandalism" })
445 assert_redirected_to user_block_path(active_block)
446 assert_equal "Block updated.", flash[:notice]
447 b = UserBlock.find(active_block.id)
448 assert_in_delta Time.now.utc, b.updated_at, 1
450 assert_equal "Vandalism", b.reason
452 # We should get an error if the block doesn't exist
453 put user_block_path(:id => 99999)
454 assert_response :not_found
455 assert_template "not_found"
456 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
460 # test the update action on expired blocks
461 def test_update_expired
462 creator_user = create(:moderator_user)
463 other_moderator_user = create(:moderator_user)
464 block = create(:user_block, :expired, :creator => creator_user, :reason => "Original Reason")
466 session_for(other_moderator_user)
467 put user_block_path(block,
468 :user_block_period => "0",
469 :user_block => { :needs_view => false, :reason => "Updated Reason" })
470 assert_redirected_to edit_user_block_path(block)
471 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
473 assert_not block.active?
474 assert_equal "Original Reason", block.reason
476 session_for(creator_user)
477 check_inactive_block_updates(block)
481 # test the update action on revoked blocks
482 def test_update_revoked
483 creator_user = create(:moderator_user)
484 revoker_user = create(:moderator_user)
485 other_moderator_user = create(:moderator_user)
486 block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user, :reason => "Original Reason")
488 session_for(other_moderator_user)
489 put user_block_path(block,
490 :user_block_period => "0",
491 :user_block => { :needs_view => false, :reason => "Updated Reason" })
492 assert_redirected_to edit_user_block_path(block)
493 assert_equal "Only the moderators who created or revoked this block can edit it.", flash[:error]
495 assert_not_predicate block, :active?
496 assert_equal "Original Reason", block.reason
498 session_for(creator_user)
499 check_inactive_block_updates(block)
501 session_for(revoker_user)
502 check_inactive_block_updates(block)
506 # test the update action revoking the block
507 def test_revoke_using_update_by_creator
508 moderator_user = create(:moderator_user)
509 block = create(:user_block, :creator => moderator_user)
511 session_for(moderator_user)
512 put user_block_path(block,
513 :user_block_period => "24",
514 :user_block => { :needs_view => false, :reason => "Updated Reason" })
515 assert_redirected_to user_block_path(block)
516 assert_equal "Block updated.", flash[:notice]
518 assert_predicate block, :active?
519 assert_nil block.revoker
521 put user_block_path(block,
522 :user_block_period => "0",
523 :user_block => { :needs_view => false, :reason => "Updated Reason" })
524 assert_redirected_to user_block_path(block)
525 assert_equal "Block updated.", flash[:notice]
527 assert_not_predicate block, :active?
528 assert_equal moderator_user, block.revoker
531 def test_revoke_using_update_by_other_moderator
532 creator_user = create(:moderator_user)
533 other_moderator_user = create(:moderator_user)
534 block = create(:user_block, :creator => creator_user)
536 session_for(other_moderator_user)
537 put user_block_path(block,
538 :user_block_period => "24",
539 :user_block => { :needs_view => false, :reason => "Updated Reason" })
540 assert_response :success
541 assert_equal "Only the moderator who created this block can edit it without revoking.", flash[:error]
543 assert_predicate block, :active?
544 assert_nil block.revoker
546 put user_block_path(block,
547 :user_block_period => "0",
548 :user_block => { :needs_view => false, :reason => "Updated Reason" })
549 assert_redirected_to user_block_path(block)
550 assert_equal "Block updated.", flash[:notice]
552 assert_not_predicate block, :active?
553 assert_equal other_moderator_user, block.revoker
557 # test the revoke action
559 active_block = create(:user_block)
561 # Check that the block revoke page requires us to login
562 get revoke_user_block_path(:id => active_block)
563 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
565 # Login as a normal user
566 session_for(create(:user))
568 # Check that normal users can't load the block revoke page
569 get revoke_user_block_path(:id => active_block)
570 assert_redirected_to :controller => "errors", :action => "forbidden"
572 # Login as a moderator
573 session_for(create(:moderator_user))
575 # Check that the block revoke page loads for moderators
576 get revoke_user_block_path(:id => active_block)
577 assert_response :success
578 assert_template "revoke"
579 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
580 assert_select "form", :count => 1 do
581 assert_select "input#confirm[type='checkbox']", :count => 1
582 assert_select "input[type='submit'][value='Revoke!']", :count => 1
585 # Check that revoking a block using GET should fail
586 get revoke_user_block_path(:id => active_block, :confirm => true)
587 assert_response :success
588 assert_template "revoke"
589 b = UserBlock.find(active_block.id)
590 assert_operator b.ends_at - Time.now.utc, :>, 100
592 # Check that revoking a block works using POST
593 post revoke_user_block_path(:id => active_block, :confirm => true)
594 assert_redirected_to user_block_path(active_block)
595 b = UserBlock.find(active_block.id)
596 assert_in_delta Time.now.utc, b.ends_at, 1
598 # We should get an error if the block doesn't exist
599 get revoke_user_block_path(:id => 99999)
600 assert_response :not_found
601 assert_template "not_found"
602 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
606 # test the revoke all page
607 def test_revoke_all_page
608 blocked_user = create(:user)
609 create(:user_block, :user => blocked_user)
611 # Asking for the revoke all blocks page with a bogus user name should fail
612 get user_blocks_on_path("non_existent_user")
613 assert_response :not_found
615 # Check that the revoke all blocks page requires us to login
616 get revoke_all_user_blocks_path(blocked_user)
617 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
619 # Login as a normal user
620 session_for(create(:user))
622 # Check that normal users can't load the revoke all blocks page
623 get revoke_all_user_blocks_path(blocked_user)
624 assert_redirected_to :controller => "errors", :action => "forbidden"
626 # Login as a moderator
627 session_for(create(:moderator_user))
629 # Check that the revoke all blocks page loads for moderators
630 get revoke_all_user_blocks_path(blocked_user)
631 assert_response :success
632 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
636 # test the revoke all action
637 def test_revoke_all_action
638 blocked_user = create(:user)
639 active_block1 = create(:user_block, :user => blocked_user)
640 active_block2 = create(:user_block, :user => blocked_user)
641 expired_block1 = create(:user_block, :expired, :user => blocked_user)
642 blocks = [active_block1, active_block2, expired_block1]
643 moderator_user = create(:moderator_user)
645 assert_predicate active_block1, :active?
646 assert_predicate active_block2, :active?
647 assert_not_predicate expired_block1, :active?
649 # Login as a normal user
650 session_for(create(:user))
652 # Check that normal users can't load the block revoke page
653 get revoke_all_user_blocks_path(:blocked_user)
654 assert_redirected_to :controller => "errors", :action => "forbidden"
656 # Login as a moderator
657 session_for(moderator_user)
659 # Check that revoking blocks using GET should fail
660 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
661 assert_response :success
662 assert_template "revoke_all"
664 blocks.each(&:reload)
665 assert_predicate active_block1, :active?
666 assert_predicate active_block2, :active?
667 assert_not_predicate expired_block1, :active?
669 # Check that revoking blocks works using POST
670 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
671 assert_redirected_to user_blocks_on_path(blocked_user)
673 blocks.each(&:reload)
674 assert_not_predicate active_block1, :active?
675 assert_not_predicate active_block2, :active?
676 assert_not_predicate expired_block1, :active?
677 assert_equal moderator_user, active_block1.revoker
678 assert_equal moderator_user, active_block2.revoker
679 assert_not_equal moderator_user, expired_block1.revoker
683 # test changes to end/deactivation dates
684 def test_dates_when_viewed_before_end
685 blocked_user = create(:user)
686 moderator_user = create(:moderator_user)
689 session_for(moderator_user)
690 assert_difference "UserBlock.count", 1 do
691 post user_blocks_path(:display_name => blocked_user.display_name,
692 :user_block_period => "48",
693 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
695 block = UserBlock.last
696 assert_equal Time.now.utc + 2.days, block.ends_at
697 assert_nil block.deactivates_at
700 session_for(blocked_user)
701 get user_block_path(block)
703 assert_equal Time.now.utc + 1.day, block.ends_at
704 assert_equal Time.now.utc + 1.day, block.deactivates_at
708 def test_dates_when_viewed_after_end
709 blocked_user = create(:user)
710 moderator_user = create(:moderator_user)
713 session_for(moderator_user)
714 assert_difference "UserBlock.count", 1 do
715 post user_blocks_path(:display_name => blocked_user.display_name,
716 :user_block_period => "24",
717 :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
719 block = UserBlock.last
720 assert_equal Time.now.utc + 1.day, block.ends_at
721 assert_nil block.deactivates_at
724 session_for(blocked_user)
725 get user_block_path(block)
727 assert_equal Time.now.utc - 1.day, block.ends_at
728 assert_equal Time.now.utc, block.deactivates_at
732 def test_dates_when_edited_before_end
733 blocked_user = create(:user)
734 moderator_user = create(:moderator_user)
737 session_for(moderator_user)
738 assert_difference "UserBlock.count", 1 do
739 post user_blocks_path(:display_name => blocked_user.display_name,
740 :user_block_period => "48",
741 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
743 block = UserBlock.last
744 assert_equal Time.now.utc + 2.days, block.ends_at
745 assert_equal Time.now.utc + 2.days, block.deactivates_at
748 put user_block_path(block,
749 :user_block_period => "48",
750 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
752 assert_equal Time.now.utc + 2.days, block.ends_at
753 assert_equal Time.now.utc + 2.days, block.deactivates_at
757 def test_dates_when_edited_after_end
758 blocked_user = create(:user)
759 moderator_user = create(:moderator_user)
762 session_for(moderator_user)
763 assert_difference "UserBlock.count", 1 do
764 post user_blocks_path(:display_name => blocked_user.display_name,
765 :user_block_period => "24",
766 :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
768 block = UserBlock.last
769 assert_equal Time.now.utc + 1.day, block.ends_at
770 assert_equal Time.now.utc + 1.day, block.deactivates_at
773 put user_block_path(block,
774 :user_block_period => "0",
775 :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
777 assert_equal Time.now.utc - 1.day, block.ends_at
778 assert_equal Time.now.utc - 1.day, block.deactivates_at
783 # test updates on legacy records without correctly initialized deactivates_at
784 def test_update_legacy_deactivates_at
785 blocked_user = create(:user)
786 moderator_user = create(:moderator_user)
789 block = UserBlock.new :user => blocked_user,
790 :creator => moderator_user,
791 :reason => "because",
792 :ends_at => Time.now.utc + 1.day,
795 assert_difference "UserBlock.count", 1 do
796 block.save :validate => false
800 session_for(moderator_user)
801 put user_block_path(block,
802 :user_block_period => "0",
803 :user_block => { :needs_view => false, :reason => "Testing legacy block update" })
805 assert_equal Time.now.utc - 1.day, block.ends_at
806 assert_equal Time.now.utc - 1.day, block.deactivates_at
811 # test the blocks_on action
813 blocked_user = create(:user)
814 unblocked_user = create(:user)
815 normal_user = create(:user)
816 active_block = create(:user_block, :user => blocked_user)
817 revoked_block = create(:user_block, :revoked, :user => blocked_user)
818 expired_block = create(:user_block, :expired, :user => unblocked_user)
820 # Asking for a list of blocks with a bogus user name should fail
821 get user_blocks_on_path("non_existent_user")
822 assert_response :not_found
823 assert_template "users/no_such_user"
824 assert_select "h1", "The user non_existent_user does not exist"
826 # Check the list of blocks for a user that has never been blocked
827 get user_blocks_on_path(normal_user)
828 assert_response :success
829 assert_select "table#block_list", false
830 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
832 # Check the list of blocks for a user that is currently blocked
833 get user_blocks_on_path(blocked_user)
834 assert_response :success
835 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
836 assert_select "table#block_list tbody", :count => 1 do
837 assert_select "tr", 2
838 assert_select "a[href='#{user_block_path(active_block)}']", 1
839 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
842 # Check the list of blocks for a user that has previously been blocked
843 get user_blocks_on_path(unblocked_user)
844 assert_response :success
845 assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
846 assert_select "table#block_list tbody", :count => 1 do
847 assert_select "tr", 1
848 assert_select "a[href='#{user_block_path(expired_block)}']", 1
853 # test the blocks_on action with multiple pages
854 def test_blocks_on_paged
856 user_blocks = create_list(:user_block, 50, :user => user).reverse
857 next_path = user_blocks_on_path(user)
860 assert_response :success
861 check_user_blocks_table user_blocks[0...20]
862 check_no_page_link "Newer Blocks"
863 next_path = check_page_link "Older Blocks"
866 assert_response :success
867 check_user_blocks_table user_blocks[20...40]
868 check_page_link "Newer Blocks"
869 next_path = check_page_link "Older Blocks"
872 assert_response :success
873 check_user_blocks_table user_blocks[40...50]
874 check_page_link "Newer Blocks"
875 check_no_page_link "Older Blocks"
879 # test the blocks_on action with invalid pages
880 def test_blocks_on_invalid_paged
883 %w[-1 0 fred].each do |id|
884 get user_blocks_on_path(user, :before => id)
885 assert_redirected_to :controller => :errors, :action => :bad_request
887 get user_blocks_on_path(user, :after => id)
888 assert_redirected_to :controller => :errors, :action => :bad_request
893 # test the blocks_by action
895 moderator_user = create(:moderator_user)
896 second_moderator_user = create(:moderator_user)
897 normal_user = create(:user)
898 active_block = create(:user_block, :creator => moderator_user)
899 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
900 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
902 # Asking for a list of blocks with a bogus user name should fail
903 get user_blocks_by_path("non_existent_user")
904 assert_response :not_found
905 assert_template "users/no_such_user"
906 assert_select "h1", "The user non_existent_user does not exist"
908 # Check the list of blocks given by one moderator
909 get user_blocks_by_path(moderator_user)
910 assert_response :success
911 assert_select "h1 a[href='#{user_path moderator_user}']", :text => moderator_user.display_name
912 assert_select "table#block_list tbody", :count => 1 do
913 assert_select "tr", 1
914 assert_select "a[href='#{user_block_path(active_block)}']", 1
917 # Check the list of blocks given by a different moderator
918 get user_blocks_by_path(second_moderator_user)
919 assert_response :success
920 assert_select "h1 a[href='#{user_path second_moderator_user}']", :text => second_moderator_user.display_name
921 assert_select "table#block_list tbody", :count => 1 do
922 assert_select "tr", 2
923 assert_select "a[href='#{user_block_path(expired_block)}']", 1
924 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
927 # Check the list of blocks (not) given by a normal user
928 get user_blocks_by_path(normal_user)
929 assert_response :success
930 assert_select "table#block_list", false
931 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
935 # test the blocks_by action with multiple pages
936 def test_blocks_by_paged
937 user = create(:moderator_user)
938 user_blocks = create_list(:user_block, 50, :creator => user).reverse
939 next_path = user_blocks_by_path(user)
942 assert_response :success
943 check_user_blocks_table user_blocks[0...20]
944 check_no_page_link "Newer Blocks"
945 next_path = check_page_link "Older Blocks"
948 assert_response :success
949 check_user_blocks_table user_blocks[20...40]
950 check_page_link "Newer Blocks"
951 next_path = check_page_link "Older Blocks"
954 assert_response :success
955 check_user_blocks_table user_blocks[40...50]
956 check_page_link "Newer Blocks"
957 check_no_page_link "Older Blocks"
961 # test the blocks_by action with invalid pages
962 def test_blocks_by_invalid_paged
963 user = create(:moderator_user)
965 %w[-1 0 fred].each do |id|
966 get user_blocks_by_path(user, :before => id)
967 assert_redirected_to :controller => :errors, :action => :bad_request
969 get user_blocks_by_path(user, :after => id)
970 assert_redirected_to :controller => :errors, :action => :bad_request
976 def check_block_buttons(block, edit: 0)
977 [user_blocks_path, user_block_path(block)].each do |path|
979 assert_response :success
980 assert_select "a[href='#{edit_user_block_path block}']", :count => edit
984 def check_inactive_block_updates(block)
985 original_ends_at = block.ends_at
987 put user_block_path(block,
988 :user_block_period => "0",
989 :user_block => { :needs_view => false, :reason => "Updated Reason" })
990 assert_redirected_to user_block_path(block)
991 assert_equal "Block updated.", flash[:notice]
993 assert_not_predicate block, :active?
994 assert_equal "Updated Reason", block.reason
995 assert_equal original_ends_at, block.ends_at
997 put user_block_path(block,
998 :user_block_period => "0",
999 :user_block => { :needs_view => true, :reason => "Updated Reason Needs View" })
1000 assert_response :success
1001 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
1003 assert_not_predicate block, :active?
1004 assert_equal "Updated Reason", block.reason
1005 assert_equal original_ends_at, block.ends_at
1007 put user_block_path(block,
1008 :user_block_period => "1",
1009 :user_block => { :needs_view => false, :reason => "Updated Reason Duration Extended" })
1010 assert_response :success
1011 assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
1013 assert_not_predicate block, :active?
1014 assert_equal "Updated Reason", block.reason
1015 assert_equal original_ends_at, block.ends_at
1017 put user_block_path(block,
1018 :user_block_period => "0",
1019 :user_block => { :needs_view => false, :reason => "Updated Reason Again" })
1020 assert_redirected_to user_block_path(block)
1021 assert_equal "Block updated.", flash[:notice]
1023 assert_not_predicate block, :active?
1024 assert_equal "Updated Reason Again", block.reason
1025 assert_equal original_ends_at, block.ends_at
1028 def check_user_blocks_table(user_blocks)
1029 assert_dom "table#block_list tbody tr" do |rows|
1030 assert_equal user_blocks.count, rows.count, "unexpected number of rows in user blocks table"
1031 rows.zip(user_blocks).map do |row, user_block|
1032 assert_dom row, "a[href='#{user_block_path user_block}']", 1
1037 def check_no_page_link(name)
1038 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
1041 def check_page_link(name)
1042 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
1043 return buttons.first.attributes["href"].value