3 class UserBlocksControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/blocks/new/username", :method => :get },
9 { :controller => "user_blocks", :action => "new", :display_name => "username" }
13 { :path => "/user_blocks", :method => :get },
14 { :controller => "user_blocks", :action => "index" }
17 { :path => "/user_blocks/new", :method => :get },
18 { :controller => "user_blocks", :action => "new" }
21 { :path => "/user_blocks", :method => :post },
22 { :controller => "user_blocks", :action => "create" }
25 { :path => "/user_blocks/1", :method => :get },
26 { :controller => "user_blocks", :action => "show", :id => "1" }
29 { :path => "/user_blocks/1/edit", :method => :get },
30 { :controller => "user_blocks", :action => "edit", :id => "1" }
33 { :path => "/user_blocks/1", :method => :put },
34 { :controller => "user_blocks", :action => "update", :id => "1" }
37 { :path => "/user_blocks/1", :method => :delete },
38 { :controller => "user_blocks", :action => "destroy", :id => "1" }
41 { :path => "/blocks/1/revoke", :method => :get },
42 { :controller => "user_blocks", :action => "revoke", :id => "1" }
45 { :path => "/blocks/1/revoke", :method => :post },
46 { :controller => "user_blocks", :action => "revoke", :id => "1" }
50 { :path => "/user/username/blocks", :method => :get },
51 { :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
54 { :path => "/user/username/blocks_by", :method => :get },
55 { :controller => "user_blocks", :action => "blocks_by", :display_name => "username" }
58 { :path => "/user/username/blocks/revoke_all", :method => :get },
59 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
62 { :path => "/user/username/blocks/revoke_all", :method => :post },
63 { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
68 # test the index action
70 revoked_block = create(:user_block, :revoked)
73 assert_response :success
74 assert_select "table#block_list tbody tr", :count => 1 do
75 assert_select "a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
76 assert_select "a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
77 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
80 active_block = create(:user_block)
81 expired_block = create(:user_block, :expired)
84 assert_response :success
85 assert_select "table#block_list tbody", :count => 1 do
87 assert_select "a[href='#{user_block_path(active_block)}']", 1
88 assert_select "a[href='#{user_block_path(expired_block)}']", 1
89 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
94 # test the index action with multiple pages
96 user_blocks = create_list(:user_block, 50).reverse
97 next_path = user_blocks_path
100 assert_response :success
101 check_user_blocks_table user_blocks[0...20]
102 check_no_page_link "Newer Blocks"
103 next_path = check_page_link "Older Blocks"
106 assert_response :success
107 check_user_blocks_table user_blocks[20...40]
108 check_page_link "Newer Blocks"
109 next_path = check_page_link "Older Blocks"
112 assert_response :success
113 check_user_blocks_table user_blocks[40...50]
114 check_page_link "Newer Blocks"
115 check_no_page_link "Older Blocks"
119 # test the show action
121 active_block = create(:user_block, :needs_view)
122 expired_block = create(:user_block, :expired)
123 revoked_block = create(:user_block, :revoked)
125 # Viewing a block should fail when a bogus ID is given
126 get user_block_path(:id => 99999)
127 assert_response :not_found
128 assert_template "not_found"
129 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
131 # Viewing an expired block should work
132 get user_block_path(:id => expired_block)
133 assert_response :success
134 assert_select "h1 a[href='#{user_path expired_block.user}']", :text => expired_block.user.display_name
135 assert_select "h1 a[href='#{user_path expired_block.creator}']", :text => expired_block.creator.display_name
137 # Viewing a revoked block should work
138 get user_block_path(:id => revoked_block)
139 assert_response :success
140 assert_select "h1 a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
141 assert_select "h1 a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
142 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
144 # Viewing an active block should work, but shouldn't mark it as seen
145 get user_block_path(:id => active_block)
146 assert_response :success
147 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
148 assert_select "h1 a[href='#{user_path active_block.creator}']", :text => active_block.creator.display_name
149 assert UserBlock.find(active_block.id).needs_view
151 # Login as the blocked user
152 session_for(active_block.user)
154 # Now viewing it should mark it as seen
155 get user_block_path(:id => active_block)
156 assert_response :success
157 assert_not UserBlock.find(active_block.id).needs_view
161 # test the new action
163 target_user = create(:user)
165 # Check that the block creation page requires us to login
166 get new_user_block_path(target_user)
167 assert_redirected_to login_path(:referer => new_user_block_path(target_user))
169 # Login as a normal user
170 session_for(create(:user))
172 # Check that normal users can't load the block creation page
173 get new_user_block_path(target_user)
174 assert_redirected_to :controller => "errors", :action => "forbidden"
176 # Login as a moderator
177 session_for(create(:moderator_user))
179 # Check that the block creation page loads for moderators
180 get new_user_block_path(target_user)
181 assert_response :success
182 assert_select "h1 a[href='#{user_path target_user}']", :text => target_user.display_name
183 assert_select "form#new_user_block", :count => 1 do
184 assert_select "textarea#user_block_reason", :count => 1
185 assert_select "select#user_block_period", :count => 1
186 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
187 assert_select "input#display_name[type='hidden']", :count => 1
188 assert_select "input[type='submit'][value='Create block']", :count => 1
191 # We should get an error if the user doesn't exist
192 get new_user_block_path(:display_name => "non_existent_user")
193 assert_response :not_found
194 assert_template "users/no_such_user"
195 assert_select "h1", "The user non_existent_user does not exist"
199 # test the edit action
201 active_block = create(:user_block)
203 # Check that the block edit page requires us to login
204 get edit_user_block_path(:id => active_block)
205 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
207 # Login as a normal user
208 session_for(create(:user))
210 # Check that normal users can't load the block edit page
211 get edit_user_block_path(:id => active_block)
212 assert_redirected_to :controller => "errors", :action => "forbidden"
214 # Login as a moderator
215 session_for(create(:moderator_user))
217 # Check that the block edit page loads for moderators
218 get edit_user_block_path(:id => active_block)
219 assert_response :success
220 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
221 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
222 assert_select "textarea#user_block_reason", :count => 1
223 assert_select "select#user_block_period", :count => 1
224 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
225 assert_select "input[type='submit'][value='Update block']", :count => 1
228 # We should get an error if the user doesn't exist
229 get edit_user_block_path(:id => 99999)
230 assert_response :not_found
231 assert_template "not_found"
232 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
236 # test the create action
238 target_user = create(:user)
239 moderator_user = create(:moderator_user)
241 # Not logged in yet, so creating a block should fail
242 post user_blocks_path
243 assert_response :forbidden
245 # Login as a normal user
246 session_for(create(:user))
248 # Check that normal users can't create blocks
249 post user_blocks_path
250 assert_redirected_to :controller => "errors", :action => "forbidden"
252 # Login as a moderator
253 session_for(moderator_user)
255 # A bogus block period should result in an error
256 assert_no_difference "UserBlock.count" do
257 post user_blocks_path(:display_name => target_user.display_name,
258 :user_block_period => "99")
260 assert_redirected_to new_user_block_path(target_user)
261 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
263 # Check that creating a block works
264 assert_difference "UserBlock.count", 1 do
265 post user_blocks_path(:display_name => target_user.display_name,
266 :user_block_period => "12",
267 :user_block => { :needs_view => false, :reason => "Vandalism" })
269 id = UserBlock.order(:id).ids.last
270 assert_redirected_to user_block_path(:id => id)
271 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
272 b = UserBlock.find(id)
273 assert_in_delta Time.now.utc, b.created_at, 1
274 assert_in_delta Time.now.utc, b.updated_at, 1
275 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
276 assert_not b.needs_view
277 assert_equal "Vandalism", b.reason
278 assert_equal "markdown", b.reason_format
279 assert_equal moderator_user.id, b.creator_id
281 # We should get an error if no user is specified
282 post user_blocks_path
283 assert_response :not_found
284 assert_template "users/no_such_user"
285 assert_select "h1", "The user does not exist"
287 # We should get an error if the user doesn't exist
288 post user_blocks_path(:display_name => "non_existent_user")
289 assert_response :not_found
290 assert_template "users/no_such_user"
291 assert_select "h1", "The user non_existent_user does not exist"
295 # test the duration of a created block
296 def test_create_duration
297 target_user = create(:user)
298 moderator_user = create(:moderator_user)
300 session_for(moderator_user)
301 post user_blocks_path(:display_name => target_user.display_name,
302 :user_block_period => "336",
303 :user_block => { :needs_view => false, :reason => "Vandalism" })
305 block = UserBlock.order(:id).last
306 assert_equal 1209600, block.ends_at - block.created_at
310 # test the update action
312 moderator_user = create(:moderator_user)
313 second_moderator_user = create(:moderator_user)
314 active_block = create(:user_block, :creator => moderator_user)
316 # Not logged in yet, so updating a block should fail
317 put user_block_path(:id => active_block)
318 assert_response :forbidden
320 # Login as a normal user
321 session_for(create(:user))
323 # Check that normal users can't update blocks
324 put user_block_path(:id => active_block)
325 assert_redirected_to :controller => "errors", :action => "forbidden"
327 # Login as the wrong moderator
328 session_for(second_moderator_user)
330 # Check that only the person who created a block can update it
331 assert_no_difference "UserBlock.count" do
332 put user_block_path(:id => active_block,
333 :user_block_period => "12",
334 :user_block => { :needs_view => true, :reason => "Vandalism" })
336 assert_redirected_to edit_user_block_path(active_block)
337 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
339 # Login as the correct moderator
340 session_for(moderator_user)
342 # A bogus block period should result in an error
343 assert_no_difference "UserBlock.count" do
344 put user_block_path(:id => active_block, :user_block_period => "99")
346 assert_redirected_to edit_user_block_path(active_block)
347 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
349 # Check that updating a block works
350 assert_no_difference "UserBlock.count" do
351 put user_block_path(:id => active_block,
352 :user_block_period => "12",
353 :user_block => { :needs_view => true, :reason => "Vandalism" })
355 assert_redirected_to user_block_path(active_block)
356 assert_equal "Block updated.", flash[:notice]
357 b = UserBlock.find(active_block.id)
358 assert_in_delta Time.now.utc, b.updated_at, 1
360 assert_equal "Vandalism", b.reason
362 # We should get an error if the block doesn't exist
363 put user_block_path(:id => 99999)
364 assert_response :not_found
365 assert_template "not_found"
366 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
370 # test the revoke action
372 active_block = create(:user_block)
374 # Check that the block revoke page requires us to login
375 get revoke_user_block_path(:id => active_block)
376 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
378 # Login as a normal user
379 session_for(create(:user))
381 # Check that normal users can't load the block revoke page
382 get revoke_user_block_path(:id => active_block)
383 assert_redirected_to :controller => "errors", :action => "forbidden"
385 # Login as a moderator
386 session_for(create(:moderator_user))
388 # Check that the block revoke page loads for moderators
389 get revoke_user_block_path(:id => active_block)
390 assert_response :success
391 assert_template "revoke"
392 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
393 assert_select "form", :count => 1 do
394 assert_select "input#confirm[type='checkbox']", :count => 1
395 assert_select "input[type='submit'][value='Revoke!']", :count => 1
398 # Check that revoking a block using GET should fail
399 get revoke_user_block_path(:id => active_block, :confirm => true)
400 assert_response :success
401 assert_template "revoke"
402 b = UserBlock.find(active_block.id)
403 assert_operator b.ends_at - Time.now.utc, :>, 100
405 # Check that revoking a block works using POST
406 post revoke_user_block_path(:id => active_block, :confirm => true)
407 assert_redirected_to user_block_path(active_block)
408 b = UserBlock.find(active_block.id)
409 assert_in_delta Time.now.utc, b.ends_at, 1
411 # We should get an error if the block doesn't exist
412 get revoke_user_block_path(:id => 99999)
413 assert_response :not_found
414 assert_template "not_found"
415 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
419 # test the revoke all page
420 def test_revoke_all_page
421 blocked_user = create(:user)
422 create(:user_block, :user => blocked_user)
424 # Asking for the revoke all blocks page with a bogus user name should fail
425 get user_blocks_on_path("non_existent_user")
426 assert_response :not_found
428 # Check that the revoke all blocks page requires us to login
429 get revoke_all_user_blocks_path(blocked_user)
430 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
432 # Login as a normal user
433 session_for(create(:user))
435 # Check that normal users can't load the revoke all blocks page
436 get revoke_all_user_blocks_path(blocked_user)
437 assert_redirected_to :controller => "errors", :action => "forbidden"
439 # Login as a moderator
440 session_for(create(:moderator_user))
442 # Check that the revoke all blocks page loads for moderators
443 get revoke_all_user_blocks_path(blocked_user)
444 assert_response :success
445 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
449 # test the revoke all action
450 def test_revoke_all_action
451 blocked_user = create(:user)
452 active_block1 = create(:user_block, :user => blocked_user)
453 active_block2 = create(:user_block, :user => blocked_user)
454 expired_block1 = create(:user_block, :expired, :user => blocked_user)
455 blocks = [active_block1, active_block2, expired_block1]
456 moderator_user = create(:moderator_user)
458 assert_predicate active_block1, :active?
459 assert_predicate active_block2, :active?
460 assert_not_predicate expired_block1, :active?
462 # Login as a normal user
463 session_for(create(:user))
465 # Check that normal users can't load the block revoke page
466 get revoke_all_user_blocks_path(:blocked_user)
467 assert_redirected_to :controller => "errors", :action => "forbidden"
469 # Login as a moderator
470 session_for(moderator_user)
472 # Check that revoking blocks using GET should fail
473 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
474 assert_response :success
475 assert_template "revoke_all"
477 blocks.each(&:reload)
478 assert_predicate active_block1, :active?
479 assert_predicate active_block2, :active?
480 assert_not_predicate expired_block1, :active?
482 # Check that revoking blocks works using POST
483 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
484 assert_redirected_to user_blocks_on_path(blocked_user)
486 blocks.each(&:reload)
487 assert_not_predicate active_block1, :active?
488 assert_not_predicate active_block2, :active?
489 assert_not_predicate expired_block1, :active?
490 assert_equal moderator_user, active_block1.revoker
491 assert_equal moderator_user, active_block2.revoker
492 assert_not_equal moderator_user, expired_block1.revoker
496 # test the blocks_on action
498 blocked_user = create(:user)
499 unblocked_user = create(:user)
500 normal_user = create(:user)
501 active_block = create(:user_block, :user => blocked_user)
502 revoked_block = create(:user_block, :revoked, :user => blocked_user)
503 expired_block = create(:user_block, :expired, :user => unblocked_user)
505 # Asking for a list of blocks with a bogus user name should fail
506 get user_blocks_on_path("non_existent_user")
507 assert_response :not_found
508 assert_template "users/no_such_user"
509 assert_select "h1", "The user non_existent_user does not exist"
511 # Check the list of blocks for a user that has never been blocked
512 get user_blocks_on_path(normal_user)
513 assert_response :success
514 assert_select "table#block_list", false
515 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
517 # Check the list of blocks for a user that is currently blocked
518 get user_blocks_on_path(blocked_user)
519 assert_response :success
520 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
521 assert_select "table#block_list tbody", :count => 1 do
522 assert_select "tr", 2
523 assert_select "a[href='#{user_block_path(active_block)}']", 1
524 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
527 # Check the list of blocks for a user that has previously been blocked
528 get user_blocks_on_path(unblocked_user)
529 assert_response :success
530 assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
531 assert_select "table#block_list tbody", :count => 1 do
532 assert_select "tr", 1
533 assert_select "a[href='#{user_block_path(expired_block)}']", 1
538 # test the blocks_on action with multiple pages
539 def test_blocks_on_paged
541 user_blocks = create_list(:user_block, 50, :user => user).reverse
542 next_path = user_blocks_on_path(user)
545 assert_response :success
546 check_user_blocks_table user_blocks[0...20]
547 check_no_page_link "Newer Blocks"
548 next_path = check_page_link "Older Blocks"
551 assert_response :success
552 check_user_blocks_table user_blocks[20...40]
553 check_page_link "Newer Blocks"
554 next_path = check_page_link "Older Blocks"
557 assert_response :success
558 check_user_blocks_table user_blocks[40...50]
559 check_page_link "Newer Blocks"
560 check_no_page_link "Older Blocks"
564 # test the blocks_by action
566 moderator_user = create(:moderator_user)
567 second_moderator_user = create(:moderator_user)
568 normal_user = create(:user)
569 active_block = create(:user_block, :creator => moderator_user)
570 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
571 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
573 # Asking for a list of blocks with a bogus user name should fail
574 get user_blocks_by_path("non_existent_user")
575 assert_response :not_found
576 assert_template "users/no_such_user"
577 assert_select "h1", "The user non_existent_user does not exist"
579 # Check the list of blocks given by one moderator
580 get user_blocks_by_path(moderator_user)
581 assert_response :success
582 assert_select "h1 a[href='#{user_path moderator_user}']", :text => moderator_user.display_name
583 assert_select "table#block_list tbody", :count => 1 do
584 assert_select "tr", 1
585 assert_select "a[href='#{user_block_path(active_block)}']", 1
588 # Check the list of blocks given by a different moderator
589 get user_blocks_by_path(second_moderator_user)
590 assert_response :success
591 assert_select "h1 a[href='#{user_path second_moderator_user}']", :text => second_moderator_user.display_name
592 assert_select "table#block_list tbody", :count => 1 do
593 assert_select "tr", 2
594 assert_select "a[href='#{user_block_path(expired_block)}']", 1
595 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
598 # Check the list of blocks (not) given by a normal user
599 get user_blocks_by_path(normal_user)
600 assert_response :success
601 assert_select "table#block_list", false
602 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
606 # test the blocks_by action with multiple pages
607 def test_blocks_by_paged
608 user = create(:moderator_user)
609 user_blocks = create_list(:user_block, 50, :creator => user).reverse
610 next_path = user_blocks_by_path(user)
613 assert_response :success
614 check_user_blocks_table user_blocks[0...20]
615 check_no_page_link "Newer Blocks"
616 next_path = check_page_link "Older Blocks"
619 assert_response :success
620 check_user_blocks_table user_blocks[20...40]
621 check_page_link "Newer Blocks"
622 next_path = check_page_link "Older Blocks"
625 assert_response :success
626 check_user_blocks_table user_blocks[40...50]
627 check_page_link "Newer Blocks"
628 check_no_page_link "Older Blocks"
633 def check_user_blocks_table(user_blocks)
634 assert_dom "table#block_list tbody tr" do |rows|
635 assert_equal user_blocks.count, rows.count, "unexpected number of rows in user blocks table"
636 rows.zip(user_blocks).map do |row, user_block|
637 assert_dom row, "a[href='#{user_block_path user_block}']", 1
642 def check_no_page_link(name)
643 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
646 def check_page_link(name)
647 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
648 return buttons.first.attributes["href"].value