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 active_block = create(:user_block)
71 expired_block = create(:user_block, :expired)
72 revoked_block = create(:user_block, :revoked)
75 assert_response :success
76 assert_select "table#block_list", :count => 1 do
78 assert_select "a[href='#{user_block_path(active_block)}']", 1
79 assert_select "a[href='#{user_block_path(expired_block)}']", 1
80 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
85 # test the index action with multiple pages
87 create_list(:user_block, 50)
90 assert_response :success
91 assert_select "table#block_list", :count => 1 do
92 assert_select "tr", :count => 21
95 get user_blocks_path(:page => 2)
96 assert_response :success
97 assert_select "table#block_list", :count => 1 do
98 assert_select "tr", :count => 21
103 # test the show action
105 active_block = create(:user_block, :needs_view)
106 expired_block = create(:user_block, :expired)
107 revoked_block = create(:user_block, :revoked)
109 # Viewing a block should fail when a bogus ID is given
110 get user_block_path(:id => 99999)
111 assert_response :not_found
112 assert_template "not_found"
113 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
115 # Viewing an expired block should work
116 get user_block_path(:id => expired_block)
117 assert_response :success
119 # Viewing a revoked block should work
120 get user_block_path(:id => revoked_block)
121 assert_response :success
123 # Viewing an active block should work, but shouldn't mark it as seen
124 get user_block_path(:id => active_block)
125 assert_response :success
126 assert UserBlock.find(active_block.id).needs_view
128 # Login as the blocked user
129 session_for(active_block.user)
131 # Now viewing it should mark it as seen
132 get user_block_path(:id => active_block)
133 assert_response :success
134 assert_not UserBlock.find(active_block.id).needs_view
138 # test the new action
140 target_user = create(:user)
142 # Check that the block creation page requires us to login
143 get new_user_block_path(:display_name => target_user.display_name)
144 assert_redirected_to login_path(:referer => new_user_block_path(:display_name => target_user.display_name))
146 # Login as a normal user
147 session_for(create(:user))
149 # Check that normal users can't load the block creation page
150 get new_user_block_path(:display_name => target_user.display_name)
151 assert_redirected_to :controller => "errors", :action => "forbidden"
153 # Login as a moderator
154 session_for(create(:moderator_user))
156 # Check that the block creation page loads for moderators
157 get new_user_block_path(:display_name => target_user.display_name)
158 assert_response :success
159 assert_select "form#new_user_block", :count => 1 do
160 assert_select "textarea#user_block_reason", :count => 1
161 assert_select "select#user_block_period", :count => 1
162 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
163 assert_select "input#display_name[type='hidden']", :count => 1
164 assert_select "input[type='submit'][value='Create block']", :count => 1
167 # We should get an error if the user doesn't exist
168 get new_user_block_path(:display_name => "non_existent_user")
169 assert_response :not_found
170 assert_template "users/no_such_user"
171 assert_select "h1", "The user non_existent_user does not exist"
175 # test the edit action
177 active_block = create(:user_block)
179 # Check that the block edit page requires us to login
180 get edit_user_block_path(:id => active_block)
181 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
183 # Login as a normal user
184 session_for(create(:user))
186 # Check that normal users can't load the block edit page
187 get edit_user_block_path(:id => active_block)
188 assert_redirected_to :controller => "errors", :action => "forbidden"
190 # Login as a moderator
191 session_for(create(:moderator_user))
193 # Check that the block edit page loads for moderators
194 get edit_user_block_path(:id => active_block)
195 assert_response :success
196 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
197 assert_select "textarea#user_block_reason", :count => 1
198 assert_select "select#user_block_period", :count => 1
199 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
200 assert_select "input[type='submit'][value='Update block']", :count => 1
203 # We should get an error if the user doesn't exist
204 get edit_user_block_path(:id => 99999)
205 assert_response :not_found
206 assert_template "not_found"
207 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
211 # test the create action
213 target_user = create(:user)
214 moderator_user = create(:moderator_user)
216 # Not logged in yet, so creating a block should fail
217 post user_blocks_path
218 assert_response :forbidden
220 # Login as a normal user
221 session_for(create(:user))
223 # Check that normal users can't create blocks
224 post user_blocks_path
225 assert_redirected_to :controller => "errors", :action => "forbidden"
227 # Login as a moderator
228 session_for(moderator_user)
230 # A bogus block period should result in an error
231 assert_no_difference "UserBlock.count" do
232 post user_blocks_path(:display_name => target_user.display_name,
233 :user_block_period => "99")
235 assert_redirected_to new_user_block_path(:display_name => target_user.display_name)
236 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
238 # Check that creating a block works
239 assert_difference "UserBlock.count", 1 do
240 post user_blocks_path(:display_name => target_user.display_name,
241 :user_block_period => "12",
242 :user_block => { :needs_view => false, :reason => "Vandalism" })
244 id = UserBlock.order(:id).ids.last
245 assert_redirected_to user_block_path(:id => id)
246 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
247 b = UserBlock.find(id)
248 assert_in_delta Time.now.utc, b.created_at, 1
249 assert_in_delta Time.now.utc, b.updated_at, 1
250 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
251 assert_not b.needs_view
252 assert_equal "Vandalism", b.reason
253 assert_equal "markdown", b.reason_format
254 assert_equal moderator_user.id, b.creator_id
256 # We should get an error if no user is specified
257 post user_blocks_path
258 assert_response :not_found
259 assert_template "users/no_such_user"
260 assert_select "h1", "The user does not exist"
262 # We should get an error if the user doesn't exist
263 post user_blocks_path(:display_name => "non_existent_user")
264 assert_response :not_found
265 assert_template "users/no_such_user"
266 assert_select "h1", "The user non_existent_user does not exist"
270 # test the duration of a created block
271 def test_create_duration
272 target_user = create(:user)
273 moderator_user = create(:moderator_user)
275 session_for(moderator_user)
276 post user_blocks_path(:display_name => target_user.display_name,
277 :user_block_period => "336",
278 :user_block => { :needs_view => false, :reason => "Vandalism" })
280 block = UserBlock.order(:id).last
281 assert_equal 1209600, block.ends_at - block.created_at
285 # test the update action
287 moderator_user = create(:moderator_user)
288 second_moderator_user = create(:moderator_user)
289 active_block = create(:user_block, :creator => moderator_user)
291 # Not logged in yet, so updating a block should fail
292 put user_block_path(:id => active_block)
293 assert_response :forbidden
295 # Login as a normal user
296 session_for(create(:user))
298 # Check that normal users can't update blocks
299 put user_block_path(:id => active_block)
300 assert_redirected_to :controller => "errors", :action => "forbidden"
302 # Login as the wrong moderator
303 session_for(second_moderator_user)
305 # Check that only the person who created a block can update it
306 assert_no_difference "UserBlock.count" do
307 put user_block_path(:id => active_block,
308 :user_block_period => "12",
309 :user_block => { :needs_view => true, :reason => "Vandalism" })
311 assert_redirected_to edit_user_block_path(active_block)
312 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
314 # Login as the correct moderator
315 session_for(moderator_user)
317 # A bogus block period should result in an error
318 assert_no_difference "UserBlock.count" do
319 put user_block_path(:id => active_block, :user_block_period => "99")
321 assert_redirected_to edit_user_block_path(active_block)
322 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
324 # Check that updating a block works
325 assert_no_difference "UserBlock.count" do
326 put user_block_path(:id => active_block,
327 :user_block_period => "12",
328 :user_block => { :needs_view => true, :reason => "Vandalism" })
330 assert_redirected_to user_block_path(active_block)
331 assert_equal "Block updated.", flash[:notice]
332 b = UserBlock.find(active_block.id)
333 assert_in_delta Time.now.utc, b.updated_at, 1
335 assert_equal "Vandalism", b.reason
337 # We should get an error if the block doesn't exist
338 put user_block_path(:id => 99999)
339 assert_response :not_found
340 assert_template "not_found"
341 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
345 # test the revoke action
347 active_block = create(:user_block)
349 # Check that the block revoke page requires us to login
350 get revoke_user_block_path(:id => active_block)
351 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
353 # Login as a normal user
354 session_for(create(:user))
356 # Check that normal users can't load the block revoke page
357 get revoke_user_block_path(:id => active_block)
358 assert_redirected_to :controller => "errors", :action => "forbidden"
360 # Login as a moderator
361 session_for(create(:moderator_user))
363 # Check that the block revoke page loads for moderators
364 get revoke_user_block_path(:id => active_block)
365 assert_response :success
366 assert_template "revoke"
367 assert_select "form", :count => 1 do
368 assert_select "input#confirm[type='checkbox']", :count => 1
369 assert_select "input[type='submit'][value='Revoke!']", :count => 1
372 # Check that revoking a block using GET should fail
373 get revoke_user_block_path(:id => active_block, :confirm => true)
374 assert_response :success
375 assert_template "revoke"
376 b = UserBlock.find(active_block.id)
377 assert_operator b.ends_at - Time.now.utc, :>, 100
379 # Check that revoking a block works using POST
380 post revoke_user_block_path(:id => active_block, :confirm => true)
381 assert_redirected_to user_block_path(active_block)
382 b = UserBlock.find(active_block.id)
383 assert_in_delta Time.now.utc, b.ends_at, 1
385 # We should get an error if the block doesn't exist
386 get revoke_user_block_path(:id => 99999)
387 assert_response :not_found
388 assert_template "not_found"
389 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
393 # test the revoke all page
394 def test_revoke_all_page
395 blocked_user = create(:user)
396 create(:user_block, :user => blocked_user)
398 # Asking for the revoke all blocks page with a bogus user name should fail
399 get user_blocks_on_path(:display_name => "non_existent_user")
400 assert_response :not_found
402 # Check that the revoke all blocks page requires us to login
403 get revoke_all_user_blocks_path(blocked_user)
404 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
406 # Login as a normal user
407 session_for(create(:user))
409 # Check that normal users can't load the revoke all blocks page
410 get revoke_all_user_blocks_path(blocked_user)
411 assert_redirected_to :controller => "errors", :action => "forbidden"
413 # Login as a moderator
414 session_for(create(:moderator_user))
416 # Check that the revoke all blocks page loads for moderators
417 get revoke_all_user_blocks_path(blocked_user)
418 assert_response :success
422 # test the revoke all action
423 def test_revoke_all_action
424 blocked_user = create(:user)
425 active_block1 = create(:user_block, :user => blocked_user)
426 active_block2 = create(:user_block, :user => blocked_user)
427 expired_block1 = create(:user_block, :expired, :user => blocked_user)
428 blocks = [active_block1, active_block2, expired_block1]
429 moderator_user = create(:moderator_user)
431 assert_predicate active_block1, :active?
432 assert_predicate active_block2, :active?
433 assert_not_predicate expired_block1, :active?
435 # Login as a normal user
436 session_for(create(:user))
438 # Check that normal users can't load the block revoke page
439 get revoke_all_user_blocks_path(:blocked_user)
440 assert_redirected_to :controller => "errors", :action => "forbidden"
442 # Login as a moderator
443 session_for(moderator_user)
445 # Check that revoking blocks using GET should fail
446 get revoke_all_user_blocks_path(blocked_user, :confirm => true)
447 assert_response :success
448 assert_template "revoke_all"
450 blocks.each(&:reload)
451 assert_predicate active_block1, :active?
452 assert_predicate active_block2, :active?
453 assert_not_predicate expired_block1, :active?
455 # Check that revoking blocks works using POST
456 post revoke_all_user_blocks_path(blocked_user, :confirm => true)
457 assert_redirected_to user_blocks_on_path(blocked_user)
459 blocks.each(&:reload)
460 assert_not_predicate active_block1, :active?
461 assert_not_predicate active_block2, :active?
462 assert_not_predicate expired_block1, :active?
463 assert_equal moderator_user, active_block1.revoker
464 assert_equal moderator_user, active_block2.revoker
465 assert_not_equal moderator_user, expired_block1.revoker
469 # test the blocks_on action
471 blocked_user = create(:user)
472 unblocked_user = create(:user)
473 normal_user = create(:user)
474 active_block = create(:user_block, :user => blocked_user)
475 revoked_block = create(:user_block, :revoked, :user => blocked_user)
476 expired_block = create(:user_block, :expired, :user => unblocked_user)
478 # Asking for a list of blocks with a bogus user name should fail
479 get user_blocks_on_path(:display_name => "non_existent_user")
480 assert_response :not_found
481 assert_template "users/no_such_user"
482 assert_select "h1", "The user non_existent_user does not exist"
484 # Check the list of blocks for a user that has never been blocked
485 get user_blocks_on_path(:display_name => normal_user.display_name)
486 assert_response :success
487 assert_select "table#block_list", false
488 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
490 # Check the list of blocks for a user that is currently blocked
491 get user_blocks_on_path(:display_name => blocked_user.display_name)
492 assert_response :success
493 assert_select "table#block_list", :count => 1 do
494 assert_select "tr", 3
495 assert_select "a[href='#{user_block_path(active_block)}']", 1
496 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
499 # Check the list of blocks for a user that has previously been blocked
500 get user_blocks_on_path(:display_name => unblocked_user.display_name)
501 assert_response :success
502 assert_select "table#block_list", :count => 1 do
503 assert_select "tr", 2
504 assert_select "a[href='#{user_block_path(expired_block)}']", 1
509 # test the blocks_on action with multiple pages
510 def test_blocks_on_paged
512 create_list(:user_block, 50, :user => user)
514 get user_blocks_on_path(:display_name => user.display_name)
515 assert_response :success
516 assert_select "table#block_list", :count => 1 do
517 assert_select "tr", :count => 21
520 get user_blocks_on_path(:display_name => user.display_name, :page => 2)
521 assert_response :success
522 assert_select "table#block_list", :count => 1 do
523 assert_select "tr", :count => 21
528 # test the blocks_by action
530 moderator_user = create(:moderator_user)
531 second_moderator_user = create(:moderator_user)
532 normal_user = create(:user)
533 active_block = create(:user_block, :creator => moderator_user)
534 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
535 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
537 # Asking for a list of blocks with a bogus user name should fail
538 get user_blocks_by_path(:display_name => "non_existent_user")
539 assert_response :not_found
540 assert_template "users/no_such_user"
541 assert_select "h1", "The user non_existent_user does not exist"
543 # Check the list of blocks given by one moderator
544 get user_blocks_by_path(:display_name => moderator_user.display_name)
545 assert_response :success
546 assert_select "table#block_list", :count => 1 do
547 assert_select "tr", 2
548 assert_select "a[href='#{user_block_path(active_block)}']", 1
551 # Check the list of blocks given by a different moderator
552 get user_blocks_by_path(:display_name => second_moderator_user.display_name)
553 assert_response :success
554 assert_select "table#block_list", :count => 1 do
555 assert_select "tr", 3
556 assert_select "a[href='#{user_block_path(expired_block)}']", 1
557 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
560 # Check the list of blocks (not) given by a normal user
561 get user_blocks_by_path(:display_name => normal_user.display_name)
562 assert_response :success
563 assert_select "table#block_list", false
564 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
568 # test the blocks_by action with multiple pages
569 def test_blocks_by_paged
570 user = create(:moderator_user)
571 create_list(:user_block, 50, :creator => user)
573 get user_blocks_by_path(:display_name => user.display_name)
574 assert_response :success
575 assert_select "table#block_list", :count => 1 do
576 assert_select "tr", :count => 21
579 get user_blocks_by_path(:display_name => user.display_name, :page => 2)
580 assert_response :success
581 assert_select "table#block_list", :count => 1 do
582 assert_select "tr", :count => 21