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_response :redirect
152 assert_redirected_to :controller => "errors", :action => "forbidden"
154 # Login as a moderator
155 session_for(create(:moderator_user))
157 # Check that the block creation page loads for moderators
158 get new_user_block_path(:display_name => target_user.display_name)
159 assert_response :success
160 assert_select "form#new_user_block", :count => 1 do
161 assert_select "textarea#user_block_reason", :count => 1
162 assert_select "select#user_block_period", :count => 1
163 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
164 assert_select "input#display_name[type='hidden']", :count => 1
165 assert_select "input[type='submit'][value='Create block']", :count => 1
168 # We should get an error if the user doesn't exist
169 get new_user_block_path(:display_name => "non_existent_user")
170 assert_response :not_found
171 assert_template "users/no_such_user"
172 assert_select "h1", "The user non_existent_user does not exist"
176 # test the edit action
178 active_block = create(:user_block)
180 # Check that the block edit page requires us to login
181 get edit_user_block_path(:id => active_block)
182 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
184 # Login as a normal user
185 session_for(create(:user))
187 # Check that normal users can't load the block edit page
188 get edit_user_block_path(:id => active_block)
189 assert_response :redirect
190 assert_redirected_to :controller => "errors", :action => "forbidden"
192 # Login as a moderator
193 session_for(create(:moderator_user))
195 # Check that the block edit page loads for moderators
196 get edit_user_block_path(:id => active_block)
197 assert_response :success
198 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
199 assert_select "textarea#user_block_reason", :count => 1
200 assert_select "select#user_block_period", :count => 1
201 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
202 assert_select "input[type='submit'][value='Update block']", :count => 1
205 # We should get an error if the user doesn't exist
206 get edit_user_block_path(:id => 99999)
207 assert_response :not_found
208 assert_template "not_found"
209 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
213 # test the create action
215 target_user = create(:user)
216 moderator_user = create(:moderator_user)
218 # Not logged in yet, so creating a block should fail
219 post user_blocks_path
220 assert_response :forbidden
222 # Login as a normal user
223 session_for(create(:user))
225 # Check that normal users can't create blocks
226 post user_blocks_path
227 assert_response :redirect
228 assert_redirected_to :controller => "errors", :action => "forbidden"
230 # Login as a moderator
231 session_for(moderator_user)
233 # A bogus block period should result in an error
234 assert_no_difference "UserBlock.count" do
235 post user_blocks_path(:display_name => target_user.display_name,
236 :user_block_period => "99")
238 assert_redirected_to new_user_block_path(:display_name => target_user.display_name)
239 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
241 # Check that creating a block works
242 assert_difference "UserBlock.count", 1 do
243 post user_blocks_path(:display_name => target_user.display_name,
244 :user_block_period => "12",
245 :user_block => { :needs_view => false, :reason => "Vandalism" })
247 id = UserBlock.order(:id).ids.last
248 assert_redirected_to user_block_path(:id => id)
249 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
250 b = UserBlock.find(id)
251 assert_in_delta Time.now.utc, b.created_at, 1
252 assert_in_delta Time.now.utc, b.updated_at, 1
253 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
254 assert_not b.needs_view
255 assert_equal "Vandalism", b.reason
256 assert_equal "markdown", b.reason_format
257 assert_equal moderator_user.id, b.creator_id
259 # We should get an error if no user is specified
260 post user_blocks_path
261 assert_response :not_found
262 assert_template "users/no_such_user"
263 assert_select "h1", "The user does not exist"
265 # We should get an error if the user doesn't exist
266 post user_blocks_path(:display_name => "non_existent_user")
267 assert_response :not_found
268 assert_template "users/no_such_user"
269 assert_select "h1", "The user non_existent_user does not exist"
273 # test the duration of a created block
274 def test_create_duration
275 target_user = create(:user)
276 moderator_user = create(:moderator_user)
278 session_for(moderator_user)
279 post user_blocks_path(:display_name => target_user.display_name,
280 :user_block_period => "336",
281 :user_block => { :needs_view => false, :reason => "Vandalism" })
283 block = UserBlock.order(:id).last
284 assert_equal 1209600, block.ends_at - block.created_at
288 # test the update action
290 moderator_user = create(:moderator_user)
291 second_moderator_user = create(:moderator_user)
292 active_block = create(:user_block, :creator => moderator_user)
294 # Not logged in yet, so updating a block should fail
295 put user_block_path(:id => active_block)
296 assert_response :forbidden
298 # Login as a normal user
299 session_for(create(:user))
301 # Check that normal users can't update blocks
302 put user_block_path(:id => active_block)
303 assert_response :redirect
304 assert_redirected_to :controller => "errors", :action => "forbidden"
306 # Login as the wrong moderator
307 session_for(second_moderator_user)
309 # Check that only the person who created a block can update it
310 assert_no_difference "UserBlock.count" do
311 put user_block_path(:id => active_block,
312 :user_block_period => "12",
313 :user_block => { :needs_view => true, :reason => "Vandalism" })
315 assert_redirected_to edit_user_block_path(active_block)
316 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
318 # Login as the correct moderator
319 session_for(moderator_user)
321 # A bogus block period should result in an error
322 assert_no_difference "UserBlock.count" do
323 put user_block_path(:id => active_block, :user_block_period => "99")
325 assert_redirected_to edit_user_block_path(active_block)
326 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
328 # Check that updating a block works
329 assert_no_difference "UserBlock.count" do
330 put user_block_path(:id => active_block,
331 :user_block_period => "12",
332 :user_block => { :needs_view => true, :reason => "Vandalism" })
334 assert_redirected_to user_block_path(active_block)
335 assert_equal "Block updated.", flash[:notice]
336 b = UserBlock.find(active_block.id)
337 assert_in_delta Time.now.utc, b.updated_at, 1
339 assert_equal "Vandalism", b.reason
341 # We should get an error if the block doesn't exist
342 put user_block_path(:id => 99999)
343 assert_response :not_found
344 assert_template "not_found"
345 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
349 # test the revoke action
351 active_block = create(:user_block)
353 # Check that the block revoke page requires us to login
354 get revoke_user_block_path(:id => active_block)
355 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
357 # Login as a normal user
358 session_for(create(:user))
360 # Check that normal users can't load the block revoke page
361 get revoke_user_block_path(:id => active_block)
362 assert_response :redirect
363 assert_redirected_to :controller => "errors", :action => "forbidden"
365 # Login as a moderator
366 session_for(create(:moderator_user))
368 # Check that the block revoke page loads for moderators
369 get revoke_user_block_path(:id => active_block)
370 assert_response :success
371 assert_template "revoke"
372 assert_select "form", :count => 1 do
373 assert_select "input#confirm[type='checkbox']", :count => 1
374 assert_select "input[type='submit'][value='Revoke!']", :count => 1
377 # Check that revoking a block using GET should fail
378 get revoke_user_block_path(:id => active_block, :confirm => true)
379 assert_response :success
380 assert_template "revoke"
381 b = UserBlock.find(active_block.id)
382 assert_operator b.ends_at - Time.now.utc, :>, 100
384 # Check that revoking a block works using POST
385 post revoke_user_block_path(:id => active_block, :confirm => true)
386 assert_redirected_to user_block_path(active_block)
387 b = UserBlock.find(active_block.id)
388 assert_in_delta Time.now.utc, b.ends_at, 1
390 # We should get an error if the block doesn't exist
391 get revoke_user_block_path(:id => 99999)
392 assert_response :not_found
393 assert_template "not_found"
394 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
398 # test the revoke all action
400 blocked_user = create(:user)
401 create(:user_block, :user => blocked_user)
403 # Asking for the revoke all blocks page with a bogus user name should fail
404 get user_blocks_on_path(:display_name => "non_existent_user")
405 assert_response :not_found
407 # Check that the revoke all blocks page requires us to login
408 get revoke_all_user_blocks_path(blocked_user)
409 assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
411 # Login as a normal user
412 session_for(create(:user))
414 # Check that normal users can't load the revoke all blocks page
415 get revoke_all_user_blocks_path(blocked_user)
416 assert_response :redirect
417 assert_redirected_to :controller => "errors", :action => "forbidden"
419 # Login as a moderator
420 session_for(create(:moderator_user))
422 # Check that the revoke all blocks page loads for moderators
423 get revoke_all_user_blocks_path(blocked_user)
424 assert_response :success
428 # test the blocks_on action
430 blocked_user = create(:user)
431 unblocked_user = create(:user)
432 normal_user = create(:user)
433 active_block = create(:user_block, :user => blocked_user)
434 revoked_block = create(:user_block, :revoked, :user => blocked_user)
435 expired_block = create(:user_block, :expired, :user => unblocked_user)
437 # Asking for a list of blocks with a bogus user name should fail
438 get user_blocks_on_path(:display_name => "non_existent_user")
439 assert_response :not_found
440 assert_template "users/no_such_user"
441 assert_select "h1", "The user non_existent_user does not exist"
443 # Check the list of blocks for a user that has never been blocked
444 get user_blocks_on_path(:display_name => normal_user.display_name)
445 assert_response :success
446 assert_select "table#block_list", false
447 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
449 # Check the list of blocks for a user that is currently blocked
450 get user_blocks_on_path(:display_name => blocked_user.display_name)
451 assert_response :success
452 assert_select "table#block_list", :count => 1 do
453 assert_select "tr", 3
454 assert_select "a[href='#{user_block_path(active_block)}']", 1
455 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
458 # Check the list of blocks for a user that has previously been blocked
459 get user_blocks_on_path(:display_name => unblocked_user.display_name)
460 assert_response :success
461 assert_select "table#block_list", :count => 1 do
462 assert_select "tr", 2
463 assert_select "a[href='#{user_block_path(expired_block)}']", 1
468 # test the blocks_on action with multiple pages
469 def test_blocks_on_paged
471 create_list(:user_block, 50, :user => user)
473 get user_blocks_on_path(:display_name => user.display_name)
474 assert_response :success
475 assert_select "table#block_list", :count => 1 do
476 assert_select "tr", :count => 21
479 get user_blocks_on_path(:display_name => user.display_name, :page => 2)
480 assert_response :success
481 assert_select "table#block_list", :count => 1 do
482 assert_select "tr", :count => 21
487 # test the blocks_by action
489 moderator_user = create(:moderator_user)
490 second_moderator_user = create(:moderator_user)
491 normal_user = create(:user)
492 active_block = create(:user_block, :creator => moderator_user)
493 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
494 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
496 # Asking for a list of blocks with a bogus user name should fail
497 get user_blocks_by_path(:display_name => "non_existent_user")
498 assert_response :not_found
499 assert_template "users/no_such_user"
500 assert_select "h1", "The user non_existent_user does not exist"
502 # Check the list of blocks given by one moderator
503 get user_blocks_by_path(:display_name => moderator_user.display_name)
504 assert_response :success
505 assert_select "table#block_list", :count => 1 do
506 assert_select "tr", 2
507 assert_select "a[href='#{user_block_path(active_block)}']", 1
510 # Check the list of blocks given by a different moderator
511 get user_blocks_by_path(:display_name => second_moderator_user.display_name)
512 assert_response :success
513 assert_select "table#block_list", :count => 1 do
514 assert_select "tr", 3
515 assert_select "a[href='#{user_block_path(expired_block)}']", 1
516 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
519 # Check the list of blocks (not) given by a normal user
520 get user_blocks_by_path(:display_name => normal_user.display_name)
521 assert_response :success
522 assert_select "table#block_list", false
523 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
527 # test the blocks_by action with multiple pages
528 def test_blocks_by_paged
529 user = create(:moderator_user)
530 create_list(:user_block, 50, :creator => user)
532 get user_blocks_by_path(:display_name => user.display_name)
533 assert_response :success
534 assert_select "table#block_list", :count => 1 do
535 assert_select "tr", :count => 21
538 get user_blocks_by_path(:display_name => user.display_name, :page => 2)
539 assert_response :success
540 assert_select "table#block_list", :count => 1 do
541 assert_select "tr", :count => 21