3 class UserBlocksControllerTest < ActionController::TestCase
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 :index, :params => { :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 no ID is given
102 assert_raise ActionController::UrlGenerationError do
106 # Viewing a block should fail when a bogus ID is given
107 get :show, :params => { :id => 99999 }
108 assert_response :not_found
109 assert_template "not_found"
110 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
112 # Viewing an expired block should work
113 get :show, :params => { :id => expired_block.id }
114 assert_response :success
116 # Viewing a revoked block should work
117 get :show, :params => { :id => revoked_block.id }
118 assert_response :success
120 # Viewing an active block should work, but shouldn't mark it as seen
121 get :show, :params => { :id => active_block.id }
122 assert_response :success
123 assert_equal true, UserBlock.find(active_block.id).needs_view
125 # Login as the blocked user
126 session[:user] = active_block.user.id
128 # Now viewing it should mark it as seen
129 get :show, :params => { :id => active_block.id }
130 assert_response :success
131 assert_equal false, UserBlock.find(active_block.id).needs_view
135 # test the new action
137 target_user = create(:user)
139 # Check that the block creation page requires us to login
140 get :new, :params => { :display_name => target_user.display_name }
141 assert_redirected_to login_path(:referer => new_user_block_path(:display_name => target_user.display_name))
143 # Login as a normal user
144 session[:user] = create(:user).id
146 # Check that normal users can't load the block creation page
147 get :new, :params => { :display_name => target_user.display_name }
148 assert_redirected_to user_blocks_path
149 assert_equal "You need to be a moderator to perform that action.", flash[:error]
151 # Login as a moderator
152 session[:user] = create(:moderator_user).id
154 # Check that the block creation page loads for moderators
155 get :new, :params => { :display_name => target_user.display_name }
156 assert_response :success
157 assert_select "form#new_user_block", :count => 1 do
158 assert_select "textarea#user_block_reason", :count => 1
159 assert_select "select#user_block_period", :count => 1
160 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
161 assert_select "input#display_name[type='hidden']", :count => 1
162 assert_select "input[type='submit'][value='Create block']", :count => 1
165 # We should get an error if no user is specified
167 assert_response :not_found
168 assert_template "user/no_such_user"
169 assert_select "h1", "The user does not exist"
171 # We should get an error if the user doesn't exist
172 get :new, :params => { :display_name => "non_existent_user" }
173 assert_response :not_found
174 assert_template "user/no_such_user"
175 assert_select "h1", "The user non_existent_user does not exist"
179 # test the edit action
181 active_block = create(:user_block)
183 # Check that the block edit page requires us to login
184 get :edit, :params => { :id => active_block.id }
185 assert_redirected_to login_path(:referer => edit_user_block_path(:id => active_block.id))
187 # Login as a normal user
188 session[:user] = create(:user).id
190 # Check that normal users can't load the block edit page
191 get :edit, :params => { :id => active_block.id }
192 assert_redirected_to user_blocks_path
193 assert_equal "You need to be a moderator to perform that action.", flash[:error]
195 # Login as a moderator
196 session[:user] = create(:moderator_user).id
198 # Check that the block edit page loads for moderators
199 get :edit, :params => { :id => active_block.id }
200 assert_response :success
201 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
202 assert_select "textarea#user_block_reason", :count => 1
203 assert_select "select#user_block_period", :count => 1
204 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
205 assert_select "input[type='submit'][value='Update block']", :count => 1
208 # We should get an error if no user is specified
209 assert_raise ActionController::UrlGenerationError do
213 # We should get an error if the user doesn't exist
214 get :edit, :params => { :id => 99999 }
215 assert_response :not_found
216 assert_template "not_found"
217 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
221 # test the create action
223 target_user = create(:user)
224 moderator_user = create(:moderator_user)
226 # Not logged in yet, so creating a block should fail
228 assert_response :forbidden
230 # Login as a normal user
231 session[:user] = create(:user).id
233 # Check that normal users can't create blocks
235 assert_response :forbidden
237 # Login as a moderator
238 session[:user] = moderator_user.id
240 # A bogus block period should result in an error
241 assert_no_difference "UserBlock.count" do
243 :params => { :display_name => target_user.display_name,
244 :user_block_period => "99" }
246 assert_redirected_to new_user_block_path(:display_name => target_user.display_name)
247 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
249 # Check that creating a block works
250 assert_difference "UserBlock.count", 1 do
252 :params => { :display_name => target_user.display_name,
253 :user_block_period => "12",
254 :user_block => { :needs_view => false, :reason => "Vandalism" } }
256 id = UserBlock.order(:id).ids.last
257 assert_redirected_to user_block_path(:id => id)
258 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
259 b = UserBlock.find(id)
260 assert_in_delta Time.now, b.created_at, 1
261 assert_in_delta Time.now, b.updated_at, 1
262 assert_in_delta Time.now + 12.hours, b.ends_at, 1
263 assert_equal false, b.needs_view
264 assert_equal "Vandalism", b.reason
265 assert_equal "markdown", b.reason_format
266 assert_equal moderator_user.id, b.creator_id
268 # We should get an error if no user is specified
270 assert_response :not_found
271 assert_template "user/no_such_user"
272 assert_select "h1", "The user does not exist"
274 # We should get an error if the user doesn't exist
275 post :create, :params => { :display_name => "non_existent_user" }
276 assert_response :not_found
277 assert_template "user/no_such_user"
278 assert_select "h1", "The user non_existent_user does not exist"
282 # test the update action
284 moderator_user = create(:moderator_user)
285 second_moderator_user = create(:moderator_user)
286 active_block = create(:user_block, :creator => moderator_user)
288 # Not logged in yet, so updating a block should fail
289 put :update, :params => { :id => active_block.id }
290 assert_response :forbidden
292 # Login as a normal user
293 session[:user] = create(:user).id
295 # Check that normal users can't update blocks
296 put :update, :params => { :id => active_block.id }
297 assert_response :forbidden
299 # Login as the wrong moderator
300 session[:user] = second_moderator_user.id
302 # Check that only the person who created a block can update it
303 assert_no_difference "UserBlock.count" do
305 :params => { :id => active_block.id,
306 :user_block_period => "12",
307 :user_block => { :needs_view => true, :reason => "Vandalism" } }
309 assert_redirected_to edit_user_block_path(:id => active_block.id)
310 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
312 # Login as the correct moderator
313 session[:user] = moderator_user.id
315 # A bogus block period should result in an error
316 assert_no_difference "UserBlock.count" do
318 :params => { :id => active_block.id,
319 :user_block_period => "99" }
321 assert_redirected_to edit_user_block_path(:id => active_block.id)
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
327 :params => { :id => active_block.id,
328 :user_block_period => "12",
329 :user_block => { :needs_view => true, :reason => "Vandalism" } }
331 assert_redirected_to user_block_path(:id => active_block.id)
332 assert_equal "Block updated.", flash[:notice]
333 b = UserBlock.find(active_block.id)
334 assert_in_delta Time.now, b.updated_at, 1
335 assert_equal true, b.needs_view
336 assert_equal "Vandalism", b.reason
338 # We should get an error if no block ID is specified
339 assert_raise ActionController::UrlGenerationError do
343 # We should get an error if the block doesn't exist
344 put :update, :params => { :id => 99999 }
345 assert_response :not_found
346 assert_template "not_found"
347 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
351 # test the revoke action
353 active_block = create(:user_block)
355 # Check that the block revoke page requires us to login
356 get :revoke, :params => { :id => active_block.id }
357 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block.id))
359 # Login as a normal user
360 session[:user] = create(:user).id
362 # Check that normal users can't load the block revoke page
363 get :revoke, :params => { :id => active_block.id }
364 assert_redirected_to user_blocks_path
365 assert_equal "You need to be a moderator to perform that action.", flash[:error]
367 # Login as a moderator
368 session[:user] = create(:moderator_user).id
370 # Check that the block revoke page loads for moderators
371 get :revoke, :params => { :id => active_block.id }
372 assert_response :success
373 assert_template "revoke"
374 assert_select "form", :count => 1 do
375 assert_select "input#confirm[type='checkbox']", :count => 1
376 assert_select "input[type='submit'][value='Revoke!']", :count => 1
379 # Check that revoking a block works
380 post :revoke, :params => { :id => active_block.id, :confirm => true }
381 assert_redirected_to user_block_path(:id => active_block.id)
382 b = UserBlock.find(active_block.id)
383 assert_in_delta Time.now, b.ends_at, 1
385 # We should get an error if no block ID is specified
386 assert_raise ActionController::UrlGenerationError do
390 # We should get an error if the block doesn't exist
391 get :revoke, :params => { :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 blocks_on action
400 blocked_user = create(:user)
401 unblocked_user = create(:user)
402 normal_user = create(:user)
403 active_block = create(:user_block, :user => blocked_user)
404 revoked_block = create(:user_block, :revoked, :user => blocked_user)
405 expired_block = create(:user_block, :expired, :user => unblocked_user)
407 # Asking for a list of blocks with no user name should fail
408 assert_raise ActionController::UrlGenerationError do
412 # Asking for a list of blocks with a bogus user name should fail
413 get :blocks_on, :params => { :display_name => "non_existent_user" }
414 assert_response :not_found
415 assert_template "user/no_such_user"
416 assert_select "h1", "The user non_existent_user does not exist"
418 # Check the list of blocks for a user that has never been blocked
419 get :blocks_on, :params => { :display_name => normal_user.display_name }
420 assert_response :success
421 assert_select "table#block_list", false
422 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
424 # Check the list of blocks for a user that is currently blocked
425 get :blocks_on, :params => { :display_name => blocked_user.display_name }
426 assert_response :success
427 assert_select "table#block_list", :count => 1 do
428 assert_select "tr", 3
429 assert_select "a[href='#{user_block_path(active_block)}']", 1
430 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
433 # Check the list of blocks for a user that has previously been blocked
434 get :blocks_on, :params => { :display_name => unblocked_user.display_name }
435 assert_response :success
436 assert_select "table#block_list", :count => 1 do
437 assert_select "tr", 2
438 assert_select "a[href='#{user_block_path(expired_block)}']", 1
443 # test the blocks_on action with multiple pages
444 def test_blocks_on_paged
446 create_list(:user_block, 50, :user => user)
448 get :blocks_on, :params => { :display_name => user.display_name }
449 assert_response :success
450 assert_select "table#block_list", :count => 1 do
451 assert_select "tr", :count => 21
454 get :blocks_on, :params => { :display_name => user.display_name, :page => 2 }
455 assert_response :success
456 assert_select "table#block_list", :count => 1 do
457 assert_select "tr", :count => 21
462 # test the blocks_by action
464 moderator_user = create(:moderator_user)
465 second_moderator_user = create(:moderator_user)
466 normal_user = create(:user)
467 active_block = create(:user_block, :creator => moderator_user)
468 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
469 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
471 # Asking for a list of blocks with no user name should fail
472 assert_raise ActionController::UrlGenerationError do
476 # Asking for a list of blocks with a bogus user name should fail
477 get :blocks_by, :params => { :display_name => "non_existent_user" }
478 assert_response :not_found
479 assert_template "user/no_such_user"
480 assert_select "h1", "The user non_existent_user does not exist"
482 # Check the list of blocks given by one moderator
483 get :blocks_by, :params => { :display_name => moderator_user.display_name }
484 assert_response :success
485 assert_select "table#block_list", :count => 1 do
486 assert_select "tr", 2
487 assert_select "a[href='#{user_block_path(active_block)}']", 1
490 # Check the list of blocks given by a different moderator
491 get :blocks_by, :params => { :display_name => second_moderator_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(expired_block)}']", 1
496 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
499 # Check the list of blocks (not) given by a normal user
500 get :blocks_by, :params => { :display_name => normal_user.display_name }
501 assert_response :success
502 assert_select "table#block_list", false
503 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
507 # test the blocks_by action with multiple pages
508 def test_blocks_by_paged
509 user = create(:moderator_user)
510 create_list(:user_block, 50, :creator => user)
512 get :blocks_by, :params => { :display_name => user.display_name }
513 assert_response :success
514 assert_select "table#block_list", :count => 1 do
515 assert_select "tr", :count => 21
518 get :blocks_by, :params => { :display_name => user.display_name, :page => 2 }
519 assert_response :success
520 assert_select "table#block_list", :count => 1 do
521 assert_select "tr", :count => 21