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 create_list(:user_block, 50)
99 assert_response :success
100 assert_select "table#block_list tbody", :count => 1 do
101 assert_select "tr", :count => 20
104 get user_blocks_path(:page => 2)
105 assert_response :success
106 assert_select "table#block_list tbody", :count => 1 do
107 assert_select "tr", :count => 20
112 # test the show action
114 active_block = create(:user_block, :needs_view)
115 expired_block = create(:user_block, :expired)
116 revoked_block = create(:user_block, :revoked)
118 # Viewing a block should fail when a bogus ID is given
119 get user_block_path(:id => 99999)
120 assert_response :not_found
121 assert_template "not_found"
122 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
124 # Viewing an expired block should work
125 get user_block_path(:id => expired_block)
126 assert_response :success
127 assert_select "h1 a[href='#{user_path expired_block.user}']", :text => expired_block.user.display_name
128 assert_select "h1 a[href='#{user_path expired_block.creator}']", :text => expired_block.creator.display_name
130 # Viewing a revoked block should work
131 get user_block_path(:id => revoked_block)
132 assert_response :success
133 assert_select "h1 a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
134 assert_select "h1 a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
135 assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
137 # Viewing an active block should work, but shouldn't mark it as seen
138 get user_block_path(:id => active_block)
139 assert_response :success
140 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
141 assert_select "h1 a[href='#{user_path active_block.creator}']", :text => active_block.creator.display_name
142 assert UserBlock.find(active_block.id).needs_view
144 # Login as the blocked user
145 session_for(active_block.user)
147 # Now viewing it should mark it as seen
148 get user_block_path(:id => active_block)
149 assert_response :success
150 assert_not UserBlock.find(active_block.id).needs_view
154 # test the new action
156 target_user = create(:user)
158 # Check that the block creation page requires us to login
159 get new_user_block_path(target_user)
160 assert_redirected_to login_path(:referer => new_user_block_path(target_user))
162 # Login as a normal user
163 session_for(create(:user))
165 # Check that normal users can't load the block creation page
166 get new_user_block_path(target_user)
167 assert_redirected_to :controller => "errors", :action => "forbidden"
169 # Login as a moderator
170 session_for(create(:moderator_user))
172 # Check that the block creation page loads for moderators
173 get new_user_block_path(target_user)
174 assert_response :success
175 assert_select "h1 a[href='#{user_path target_user}']", :text => target_user.display_name
176 assert_select "form#new_user_block", :count => 1 do
177 assert_select "textarea#user_block_reason", :count => 1
178 assert_select "select#user_block_period", :count => 1
179 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
180 assert_select "input#display_name[type='hidden']", :count => 1
181 assert_select "input[type='submit'][value='Create block']", :count => 1
184 # We should get an error if the user doesn't exist
185 get new_user_block_path(:display_name => "non_existent_user")
186 assert_response :not_found
187 assert_template "users/no_such_user"
188 assert_select "h1", "The user non_existent_user does not exist"
192 # test the edit action
194 active_block = create(:user_block)
196 # Check that the block edit page requires us to login
197 get edit_user_block_path(:id => active_block)
198 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
200 # Login as a normal user
201 session_for(create(:user))
203 # Check that normal users can't load the block edit page
204 get edit_user_block_path(:id => active_block)
205 assert_redirected_to :controller => "errors", :action => "forbidden"
207 # Login as a moderator
208 session_for(create(:moderator_user))
210 # Check that the block edit page loads for moderators
211 get edit_user_block_path(:id => active_block)
212 assert_response :success
213 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
214 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
215 assert_select "textarea#user_block_reason", :count => 1
216 assert_select "select#user_block_period", :count => 1
217 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
218 assert_select "input[type='submit'][value='Update block']", :count => 1
221 # We should get an error if the user doesn't exist
222 get edit_user_block_path(:id => 99999)
223 assert_response :not_found
224 assert_template "not_found"
225 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
229 # test the create action
231 target_user = create(:user)
232 moderator_user = create(:moderator_user)
234 # Not logged in yet, so creating a block should fail
235 post user_blocks_path
236 assert_response :forbidden
238 # Login as a normal user
239 session_for(create(:user))
241 # Check that normal users can't create blocks
242 post user_blocks_path
243 assert_redirected_to :controller => "errors", :action => "forbidden"
245 # Login as a moderator
246 session_for(moderator_user)
248 # A bogus block period should result in an error
249 assert_no_difference "UserBlock.count" do
250 post user_blocks_path(:display_name => target_user.display_name,
251 :user_block_period => "99")
253 assert_redirected_to new_user_block_path(target_user)
254 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
256 # Check that creating a block works
257 assert_difference "UserBlock.count", 1 do
258 post user_blocks_path(:display_name => target_user.display_name,
259 :user_block_period => "12",
260 :user_block => { :needs_view => false, :reason => "Vandalism" })
262 id = UserBlock.order(:id).ids.last
263 assert_redirected_to user_block_path(:id => id)
264 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
265 b = UserBlock.find(id)
266 assert_in_delta Time.now.utc, b.created_at, 1
267 assert_in_delta Time.now.utc, b.updated_at, 1
268 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
269 assert_not b.needs_view
270 assert_equal "Vandalism", b.reason
271 assert_equal "markdown", b.reason_format
272 assert_equal moderator_user.id, b.creator_id
274 # We should get an error if no user is specified
275 post user_blocks_path
276 assert_response :not_found
277 assert_template "users/no_such_user"
278 assert_select "h1", "The user does not exist"
280 # We should get an error if the user doesn't exist
281 post user_blocks_path(:display_name => "non_existent_user")
282 assert_response :not_found
283 assert_template "users/no_such_user"
284 assert_select "h1", "The user non_existent_user does not exist"
288 # test the duration of a created block
289 def test_create_duration
290 target_user = create(:user)
291 moderator_user = create(:moderator_user)
293 session_for(moderator_user)
294 post user_blocks_path(:display_name => target_user.display_name,
295 :user_block_period => "336",
296 :user_block => { :needs_view => false, :reason => "Vandalism" })
298 block = UserBlock.order(:id).last
299 assert_equal 1209600, block.ends_at - block.created_at
303 # test the update action
305 moderator_user = create(:moderator_user)
306 second_moderator_user = create(:moderator_user)
307 active_block = create(:user_block, :creator => moderator_user)
309 # Not logged in yet, so updating a block should fail
310 put user_block_path(:id => active_block)
311 assert_response :forbidden
313 # Login as a normal user
314 session_for(create(:user))
316 # Check that normal users can't update blocks
317 put user_block_path(:id => active_block)
318 assert_redirected_to :controller => "errors", :action => "forbidden"
320 # Login as the wrong moderator
321 session_for(second_moderator_user)
323 # Check that only the person who created a block can update it
324 assert_no_difference "UserBlock.count" do
325 put user_block_path(:id => active_block,
326 :user_block_period => "12",
327 :user_block => { :needs_view => true, :reason => "Vandalism" })
329 assert_redirected_to edit_user_block_path(active_block)
330 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
332 # Login as the correct moderator
333 session_for(moderator_user)
335 # A bogus block period should result in an error
336 assert_no_difference "UserBlock.count" do
337 put user_block_path(:id => active_block, :user_block_period => "99")
339 assert_redirected_to edit_user_block_path(active_block)
340 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
342 # Check that updating a block works
343 assert_no_difference "UserBlock.count" do
344 put user_block_path(:id => active_block,
345 :user_block_period => "12",
346 :user_block => { :needs_view => true, :reason => "Vandalism" })
348 assert_redirected_to user_block_path(active_block)
349 assert_equal "Block updated.", flash[:notice]
350 b = UserBlock.find(active_block.id)
351 assert_in_delta Time.now.utc, b.updated_at, 1
353 assert_equal "Vandalism", b.reason
355 # We should get an error if the block doesn't exist
356 put user_block_path(:id => 99999)
357 assert_response :not_found
358 assert_template "not_found"
359 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
363 # test the revoke action
365 active_block = create(:user_block)
367 # Check that the block revoke page requires us to login
368 get revoke_user_block_path(:id => active_block)
369 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
371 # Login as a normal user
372 session_for(create(:user))
374 # Check that normal users can't load the block revoke page
375 get revoke_user_block_path(:id => active_block)
376 assert_redirected_to :controller => "errors", :action => "forbidden"
378 # Login as a moderator
379 session_for(create(:moderator_user))
381 # Check that the block revoke page loads for moderators
382 get revoke_user_block_path(:id => active_block)
383 assert_response :success
384 assert_template "revoke"
385 assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
386 assert_select "form", :count => 1 do
387 assert_select "input#confirm[type='checkbox']", :count => 1
388 assert_select "input[type='submit'][value='Revoke!']", :count => 1
391 # Check that revoking a block using GET should fail
392 get revoke_user_block_path(:id => active_block, :confirm => true)
393 assert_response :success
394 assert_template "revoke"
395 b = UserBlock.find(active_block.id)
396 assert_operator b.ends_at - Time.now.utc, :>, 100
398 # Check that revoking a block works using POST
399 post revoke_user_block_path(:id => active_block, :confirm => true)
400 assert_redirected_to user_block_path(active_block)
401 b = UserBlock.find(active_block.id)
402 assert_in_delta Time.now.utc, b.ends_at, 1
404 # We should get an error if the block doesn't exist
405 get revoke_user_block_path(:id => 99999)
406 assert_response :not_found
407 assert_template "not_found"
408 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
412 # test the revoke all page
413 def test_revoke_all_page
414 blocked_user = create(:user)
415 create(:user_block, :user => blocked_user)
417 # Asking for the revoke all blocks page with a bogus user name should fail
418 get user_blocks_on_path("non_existent_user")
419 assert_response :not_found
421 # Check that the revoke all blocks page requires us to login
422 get revoke_all_user_blocks_path(blocked_user)
423 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
425 # Login as a normal user
426 session_for(create(:user))
428 # Check that normal users can't load the revoke all blocks page
429 get revoke_all_user_blocks_path(blocked_user)
430 assert_redirected_to :controller => "errors", :action => "forbidden"
432 # Login as a moderator
433 session_for(create(:moderator_user))
435 # Check that the revoke all blocks page loads for moderators
436 get revoke_all_user_blocks_path(blocked_user)
437 assert_response :success
438 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
442 # test the revoke all action
443 def test_revoke_all_action
444 blocked_user = create(:user)
445 active_block1 = create(:user_block, :user => blocked_user)
446 active_block2 = create(:user_block, :user => blocked_user)
447 expired_block1 = create(:user_block, :expired, :user => blocked_user)
448 blocks = [active_block1, active_block2, expired_block1]
449 moderator_user = create(:moderator_user)
451 assert_predicate active_block1, :active?
452 assert_predicate active_block2, :active?
453 assert_not_predicate expired_block1, :active?
455 # Login as a normal user
456 session_for(create(:user))
458 # Check that normal users can't load the block revoke page
459 get revoke_all_user_blocks_path(:blocked_user)
460 assert_redirected_to :controller => "errors", :action => "forbidden"
462 # Login as a moderator
463 session_for(moderator_user)
465 # Check that revoking blocks using GET should fail
466 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
467 assert_response :success
468 assert_template "revoke_all"
470 blocks.each(&:reload)
471 assert_predicate active_block1, :active?
472 assert_predicate active_block2, :active?
473 assert_not_predicate expired_block1, :active?
475 # Check that revoking blocks works using POST
476 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
477 assert_redirected_to user_blocks_on_path(blocked_user)
479 blocks.each(&:reload)
480 assert_not_predicate active_block1, :active?
481 assert_not_predicate active_block2, :active?
482 assert_not_predicate expired_block1, :active?
483 assert_equal moderator_user, active_block1.revoker
484 assert_equal moderator_user, active_block2.revoker
485 assert_not_equal moderator_user, expired_block1.revoker
489 # test the blocks_on action
491 blocked_user = create(:user)
492 unblocked_user = create(:user)
493 normal_user = create(:user)
494 active_block = create(:user_block, :user => blocked_user)
495 revoked_block = create(:user_block, :revoked, :user => blocked_user)
496 expired_block = create(:user_block, :expired, :user => unblocked_user)
498 # Asking for a list of blocks with a bogus user name should fail
499 get user_blocks_on_path("non_existent_user")
500 assert_response :not_found
501 assert_template "users/no_such_user"
502 assert_select "h1", "The user non_existent_user does not exist"
504 # Check the list of blocks for a user that has never been blocked
505 get user_blocks_on_path(normal_user)
506 assert_response :success
507 assert_select "table#block_list", false
508 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
510 # Check the list of blocks for a user that is currently blocked
511 get user_blocks_on_path(blocked_user)
512 assert_response :success
513 assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
514 assert_select "table#block_list tbody", :count => 1 do
515 assert_select "tr", 2
516 assert_select "a[href='#{user_block_path(active_block)}']", 1
517 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
520 # Check the list of blocks for a user that has previously been blocked
521 get user_blocks_on_path(unblocked_user)
522 assert_response :success
523 assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
524 assert_select "table#block_list tbody", :count => 1 do
525 assert_select "tr", 1
526 assert_select "a[href='#{user_block_path(expired_block)}']", 1
531 # test the blocks_on action with multiple pages
532 def test_blocks_on_paged
534 create_list(:user_block, 50, :user => user)
536 get user_blocks_on_path(user)
537 assert_response :success
538 assert_select "table#block_list tbody", :count => 1 do
539 assert_select "tr", :count => 20
542 get user_blocks_on_path(user, :page => 2)
543 assert_response :success
544 assert_select "table#block_list tbody", :count => 1 do
545 assert_select "tr", :count => 20
550 # test the blocks_by action
552 moderator_user = create(:moderator_user)
553 second_moderator_user = create(:moderator_user)
554 normal_user = create(:user)
555 active_block = create(:user_block, :creator => moderator_user)
556 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
557 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
559 # Asking for a list of blocks with a bogus user name should fail
560 get user_blocks_by_path("non_existent_user")
561 assert_response :not_found
562 assert_template "users/no_such_user"
563 assert_select "h1", "The user non_existent_user does not exist"
565 # Check the list of blocks given by one moderator
566 get user_blocks_by_path(moderator_user)
567 assert_response :success
568 assert_select "h1 a[href='#{user_path moderator_user}']", :text => moderator_user.display_name
569 assert_select "table#block_list tbody", :count => 1 do
570 assert_select "tr", 1
571 assert_select "a[href='#{user_block_path(active_block)}']", 1
574 # Check the list of blocks given by a different moderator
575 get user_blocks_by_path(second_moderator_user)
576 assert_response :success
577 assert_select "h1 a[href='#{user_path second_moderator_user}']", :text => second_moderator_user.display_name
578 assert_select "table#block_list tbody", :count => 1 do
579 assert_select "tr", 2
580 assert_select "a[href='#{user_block_path(expired_block)}']", 1
581 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
584 # Check the list of blocks (not) given by a normal user
585 get user_blocks_by_path(normal_user)
586 assert_response :success
587 assert_select "table#block_list", false
588 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
592 # test the blocks_by action with multiple pages
593 def test_blocks_by_paged
594 user = create(:moderator_user)
595 create_list(:user_block, 50, :creator => user)
597 get user_blocks_by_path(user)
598 assert_response :success
599 assert_select "table#block_list tbody", :count => 1 do
600 assert_select "tr", :count => 20
603 get user_blocks_by_path(user, :page => 2)
604 assert_response :success
605 assert_select "table#block_list tbody", :count => 1 do
606 assert_select "tr", :count => 20