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" }
60 # test the index action
62 active_block = create(:user_block)
63 expired_block = create(:user_block, :expired)
64 revoked_block = create(:user_block, :revoked)
67 assert_response :success
68 assert_select "table#block_list", :count => 1 do
70 assert_select "a[href='#{user_block_path(active_block)}']", 1
71 assert_select "a[href='#{user_block_path(expired_block)}']", 1
72 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
77 # test the index action with multiple pages
79 create_list(:user_block, 50)
82 assert_response :success
83 assert_select "table#block_list", :count => 1 do
84 assert_select "tr", :count => 21
87 get user_blocks_path(:page => 2)
88 assert_response :success
89 assert_select "table#block_list", :count => 1 do
90 assert_select "tr", :count => 21
95 # test the show action
97 active_block = create(:user_block, :needs_view)
98 expired_block = create(:user_block, :expired)
99 revoked_block = create(:user_block, :revoked)
101 # Viewing a block should fail when a bogus ID is given
102 get user_block_path(:id => 99999)
103 assert_response :not_found
104 assert_template "not_found"
105 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
107 # Viewing an expired block should work
108 get user_block_path(:id => expired_block)
109 assert_response :success
111 # Viewing a revoked block should work
112 get user_block_path(:id => revoked_block)
113 assert_response :success
115 # Viewing an active block should work, but shouldn't mark it as seen
116 get user_block_path(:id => active_block)
117 assert_response :success
118 assert UserBlock.find(active_block.id).needs_view
120 # Login as the blocked user
121 session_for(active_block.user)
123 # Now viewing it should mark it as seen
124 get user_block_path(:id => active_block)
125 assert_response :success
126 assert_not UserBlock.find(active_block.id).needs_view
130 # test the new action
132 target_user = create(:user)
134 # Check that the block creation page requires us to login
135 get new_user_block_path(:display_name => target_user.display_name)
136 assert_redirected_to login_path(:referer => new_user_block_path(:display_name => target_user.display_name))
138 # Login as a normal user
139 session_for(create(:user))
141 # Check that normal users can't load the block creation page
142 get new_user_block_path(:display_name => target_user.display_name)
143 assert_response :redirect
144 assert_redirected_to :controller => "errors", :action => "forbidden"
146 # Login as a moderator
147 session_for(create(:moderator_user))
149 # Check that the block creation page loads for moderators
150 get new_user_block_path(:display_name => target_user.display_name)
151 assert_response :success
152 assert_select "form#new_user_block", :count => 1 do
153 assert_select "textarea#user_block_reason", :count => 1
154 assert_select "select#user_block_period", :count => 1
155 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
156 assert_select "input#display_name[type='hidden']", :count => 1
157 assert_select "input[type='submit'][value='Create block']", :count => 1
160 # We should get an error if the user doesn't exist
161 get new_user_block_path(:display_name => "non_existent_user")
162 assert_response :not_found
163 assert_template "users/no_such_user"
164 assert_select "h1", "The user non_existent_user does not exist"
168 # test the edit action
170 active_block = create(:user_block)
172 # Check that the block edit page requires us to login
173 get edit_user_block_path(:id => active_block)
174 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
176 # Login as a normal user
177 session_for(create(:user))
179 # Check that normal users can't load the block edit page
180 get edit_user_block_path(:id => active_block)
181 assert_response :redirect
182 assert_redirected_to :controller => "errors", :action => "forbidden"
184 # Login as a moderator
185 session_for(create(:moderator_user))
187 # Check that the block edit page loads for moderators
188 get edit_user_block_path(:id => active_block)
189 assert_response :success
190 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
191 assert_select "textarea#user_block_reason", :count => 1
192 assert_select "select#user_block_period", :count => 1
193 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
194 assert_select "input[type='submit'][value='Update block']", :count => 1
197 # We should get an error if the user doesn't exist
198 get edit_user_block_path(:id => 99999)
199 assert_response :not_found
200 assert_template "not_found"
201 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
205 # test the create action
207 target_user = create(:user)
208 moderator_user = create(:moderator_user)
210 # Not logged in yet, so creating a block should fail
211 post user_blocks_path
212 assert_response :forbidden
214 # Login as a normal user
215 session_for(create(:user))
217 # Check that normal users can't create blocks
218 post user_blocks_path
219 assert_response :redirect
220 assert_redirected_to :controller => "errors", :action => "forbidden"
222 # Login as a moderator
223 session_for(moderator_user)
225 # A bogus block period should result in an error
226 assert_no_difference "UserBlock.count" do
227 post user_blocks_path(:display_name => target_user.display_name,
228 :user_block_period => "99")
230 assert_redirected_to new_user_block_path(:display_name => target_user.display_name)
231 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
233 # Check that creating a block works
234 assert_difference "UserBlock.count", 1 do
235 post user_blocks_path(:display_name => target_user.display_name,
236 :user_block_period => "12",
237 :user_block => { :needs_view => false, :reason => "Vandalism" })
239 id = UserBlock.order(:id).ids.last
240 assert_redirected_to user_block_path(:id => id)
241 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
242 b = UserBlock.find(id)
243 assert_in_delta Time.now.utc, b.created_at, 1
244 assert_in_delta Time.now.utc, b.updated_at, 1
245 assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
246 assert_not b.needs_view
247 assert_equal "Vandalism", b.reason
248 assert_equal "markdown", b.reason_format
249 assert_equal moderator_user.id, b.creator_id
251 # We should get an error if no user is specified
252 post user_blocks_path
253 assert_response :not_found
254 assert_template "users/no_such_user"
255 assert_select "h1", "The user does not exist"
257 # We should get an error if the user doesn't exist
258 post user_blocks_path(:display_name => "non_existent_user")
259 assert_response :not_found
260 assert_template "users/no_such_user"
261 assert_select "h1", "The user non_existent_user does not exist"
265 # test the duration of a created block
266 def test_create_duration
267 target_user = create(:user)
268 moderator_user = create(:moderator_user)
270 session_for(moderator_user)
271 post user_blocks_path(:display_name => target_user.display_name,
272 :user_block_period => "336",
273 :user_block => { :needs_view => false, :reason => "Vandalism" })
275 block = UserBlock.order(:id).last
276 assert_equal 1209600, block.ends_at - block.created_at
280 # test the update action
282 moderator_user = create(:moderator_user)
283 second_moderator_user = create(:moderator_user)
284 active_block = create(:user_block, :creator => moderator_user)
286 # Not logged in yet, so updating a block should fail
287 put user_block_path(:id => active_block)
288 assert_response :forbidden
290 # Login as a normal user
291 session_for(create(:user))
293 # Check that normal users can't update blocks
294 put user_block_path(:id => active_block)
295 assert_response :redirect
296 assert_redirected_to :controller => "errors", :action => "forbidden"
298 # Login as the wrong moderator
299 session_for(second_moderator_user)
301 # Check that only the person who created a block can update it
302 assert_no_difference "UserBlock.count" do
303 put user_block_path(:id => active_block,
304 :user_block_period => "12",
305 :user_block => { :needs_view => true, :reason => "Vandalism" })
307 assert_redirected_to edit_user_block_path(active_block)
308 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
310 # Login as the correct moderator
311 session_for(moderator_user)
313 # A bogus block period should result in an error
314 assert_no_difference "UserBlock.count" do
315 put user_block_path(:id => active_block, :user_block_period => "99")
317 assert_redirected_to edit_user_block_path(active_block)
318 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
320 # Check that updating a block works
321 assert_no_difference "UserBlock.count" do
322 put user_block_path(:id => active_block,
323 :user_block_period => "12",
324 :user_block => { :needs_view => true, :reason => "Vandalism" })
326 assert_redirected_to user_block_path(active_block)
327 assert_equal "Block updated.", flash[:notice]
328 b = UserBlock.find(active_block.id)
329 assert_in_delta Time.now.utc, b.updated_at, 1
331 assert_equal "Vandalism", b.reason
333 # We should get an error if the block doesn't exist
334 put user_block_path(:id => 99999)
335 assert_response :not_found
336 assert_template "not_found"
337 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
341 # test the revoke action
343 active_block = create(:user_block)
345 # Check that the block revoke page requires us to login
346 get revoke_user_block_path(:id => active_block)
347 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
349 # Login as a normal user
350 session_for(create(:user))
352 # Check that normal users can't load the block revoke page
353 get revoke_user_block_path(:id => active_block)
354 assert_response :redirect
355 assert_redirected_to :controller => "errors", :action => "forbidden"
357 # Login as a moderator
358 session_for(create(:moderator_user))
360 # Check that the block revoke page loads for moderators
361 get revoke_user_block_path(:id => active_block)
362 assert_response :success
363 assert_template "revoke"
364 assert_select "form", :count => 1 do
365 assert_select "input#confirm[type='checkbox']", :count => 1
366 assert_select "input[type='submit'][value='Revoke!']", :count => 1
369 # Check that revoking a block using GET should fail
370 get revoke_user_block_path(:id => active_block, :confirm => true)
371 assert_response :success
372 assert_template "revoke"
373 b = UserBlock.find(active_block.id)
374 assert_operator b.ends_at - Time.now.utc, :>, 100
376 # Check that revoking a block works using POST
377 post revoke_user_block_path(:id => active_block, :confirm => true)
378 assert_redirected_to user_block_path(active_block)
379 b = UserBlock.find(active_block.id)
380 assert_in_delta Time.now.utc, b.ends_at, 1
382 # We should get an error if the block doesn't exist
383 get revoke_user_block_path(:id => 99999)
384 assert_response :not_found
385 assert_template "not_found"
386 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
390 # test the blocks_on action
392 blocked_user = create(:user)
393 unblocked_user = create(:user)
394 normal_user = create(:user)
395 active_block = create(:user_block, :user => blocked_user)
396 revoked_block = create(:user_block, :revoked, :user => blocked_user)
397 expired_block = create(:user_block, :expired, :user => unblocked_user)
399 # Asking for a list of blocks with a bogus user name should fail
400 get user_blocks_on_path(:display_name => "non_existent_user")
401 assert_response :not_found
402 assert_template "users/no_such_user"
403 assert_select "h1", "The user non_existent_user does not exist"
405 # Check the list of blocks for a user that has never been blocked
406 get user_blocks_on_path(:display_name => normal_user.display_name)
407 assert_response :success
408 assert_select "table#block_list", false
409 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
411 # Check the list of blocks for a user that is currently blocked
412 get user_blocks_on_path(:display_name => blocked_user.display_name)
413 assert_response :success
414 assert_select "table#block_list", :count => 1 do
415 assert_select "tr", 3
416 assert_select "a[href='#{user_block_path(active_block)}']", 1
417 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
420 # Check the list of blocks for a user that has previously been blocked
421 get user_blocks_on_path(:display_name => unblocked_user.display_name)
422 assert_response :success
423 assert_select "table#block_list", :count => 1 do
424 assert_select "tr", 2
425 assert_select "a[href='#{user_block_path(expired_block)}']", 1
430 # test the blocks_on action with multiple pages
431 def test_blocks_on_paged
433 create_list(:user_block, 50, :user => user)
435 get user_blocks_on_path(:display_name => user.display_name)
436 assert_response :success
437 assert_select "table#block_list", :count => 1 do
438 assert_select "tr", :count => 21
441 get user_blocks_on_path(:display_name => user.display_name, :page => 2)
442 assert_response :success
443 assert_select "table#block_list", :count => 1 do
444 assert_select "tr", :count => 21
449 # test the blocks_by action
451 moderator_user = create(:moderator_user)
452 second_moderator_user = create(:moderator_user)
453 normal_user = create(:user)
454 active_block = create(:user_block, :creator => moderator_user)
455 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
456 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
458 # Asking for a list of blocks with a bogus user name should fail
459 get user_blocks_by_path(:display_name => "non_existent_user")
460 assert_response :not_found
461 assert_template "users/no_such_user"
462 assert_select "h1", "The user non_existent_user does not exist"
464 # Check the list of blocks given by one moderator
465 get user_blocks_by_path(:display_name => moderator_user.display_name)
466 assert_response :success
467 assert_select "table#block_list", :count => 1 do
468 assert_select "tr", 2
469 assert_select "a[href='#{user_block_path(active_block)}']", 1
472 # Check the list of blocks given by a different moderator
473 get user_blocks_by_path(:display_name => second_moderator_user.display_name)
474 assert_response :success
475 assert_select "table#block_list", :count => 1 do
476 assert_select "tr", 3
477 assert_select "a[href='#{user_block_path(expired_block)}']", 1
478 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
481 # Check the list of blocks (not) given by a normal user
482 get user_blocks_by_path(:display_name => normal_user.display_name)
483 assert_response :success
484 assert_select "table#block_list", false
485 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
489 # test the blocks_by action with multiple pages
490 def test_blocks_by_paged
491 user = create(:moderator_user)
492 create_list(:user_block, 50, :creator => user)
494 get user_blocks_by_path(:display_name => user.display_name)
495 assert_response :success
496 assert_select "table#block_list", :count => 1 do
497 assert_select "tr", :count => 21
500 get user_blocks_by_path(:display_name => user.display_name, :page => 2)
501 assert_response :success
502 assert_select "table#block_list", :count => 1 do
503 assert_select "tr", :count => 21