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_response :forbidden
150 # Login as a moderator
151 session[:user] = create(:moderator_user).id
153 # Check that the block creation page loads for moderators
154 get :new, :params => { :display_name => target_user.display_name }
155 assert_response :success
156 assert_select "form#new_user_block", :count => 1 do
157 assert_select "textarea#user_block_reason", :count => 1
158 assert_select "select#user_block_period", :count => 1
159 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
160 assert_select "input#display_name[type='hidden']", :count => 1
161 assert_select "input[type='submit'][value='Create block']", :count => 1
164 # We should get an error if no user is specified
166 assert_response :not_found
167 assert_template "users/no_such_user"
168 assert_select "h1", "The user does not exist"
170 # We should get an error if the user doesn't exist
171 get :new, :params => { :display_name => "non_existent_user" }
172 assert_response :not_found
173 assert_template "users/no_such_user"
174 assert_select "h1", "The user non_existent_user does not exist"
178 # test the edit action
180 active_block = create(:user_block)
182 # Check that the block edit page requires us to login
183 get :edit, :params => { :id => active_block.id }
184 assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
186 # Login as a normal user
187 session[:user] = create(:user).id
189 # Check that normal users can't load the block edit page
190 get :edit, :params => { :id => active_block.id }
191 assert_response :forbidden
193 # Login as a moderator
194 session[:user] = create(:moderator_user).id
196 # Check that the block edit page loads for moderators
197 get :edit, :params => { :id => active_block.id }
198 assert_response :success
199 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
200 assert_select "textarea#user_block_reason", :count => 1
201 assert_select "select#user_block_period", :count => 1
202 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
203 assert_select "input[type='submit'][value='Update block']", :count => 1
206 # We should get an error if no user is specified
207 assert_raise ActionController::UrlGenerationError do
211 # We should get an error if the user doesn't exist
212 get :edit, :params => { :id => 99999 }
213 assert_response :not_found
214 assert_template "not_found"
215 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
219 # test the create action
221 target_user = create(:user)
222 moderator_user = create(:moderator_user)
224 # Not logged in yet, so creating a block should fail
226 assert_response :forbidden
228 # Login as a normal user
229 session[:user] = create(:user).id
231 # Check that normal users can't create blocks
233 assert_response :forbidden
235 # Login as a moderator
236 session[:user] = moderator_user.id
238 # A bogus block period should result in an error
239 assert_no_difference "UserBlock.count" do
241 :params => { :display_name => target_user.display_name,
242 :user_block_period => "99" }
244 assert_redirected_to new_user_block_path(:display_name => target_user.display_name)
245 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
247 # Check that creating a block works
248 assert_difference "UserBlock.count", 1 do
250 :params => { :display_name => target_user.display_name,
251 :user_block_period => "12",
252 :user_block => { :needs_view => false, :reason => "Vandalism" } }
254 id = UserBlock.order(:id).ids.last
255 assert_redirected_to user_block_path(:id => id)
256 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
257 b = UserBlock.find(id)
258 assert_in_delta Time.now, b.created_at, 1
259 assert_in_delta Time.now, b.updated_at, 1
260 assert_in_delta Time.now + 12.hours, b.ends_at, 1
261 assert_equal false, b.needs_view
262 assert_equal "Vandalism", b.reason
263 assert_equal "markdown", b.reason_format
264 assert_equal moderator_user.id, b.creator_id
266 # We should get an error if no user is specified
268 assert_response :not_found
269 assert_template "users/no_such_user"
270 assert_select "h1", "The user does not exist"
272 # We should get an error if the user doesn't exist
273 post :create, :params => { :display_name => "non_existent_user" }
274 assert_response :not_found
275 assert_template "users/no_such_user"
276 assert_select "h1", "The user non_existent_user does not exist"
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 :update, :params => { :id => active_block.id }
288 assert_response :forbidden
290 # Login as a normal user
291 session[:user] = create(:user).id
293 # Check that normal users can't update blocks
294 put :update, :params => { :id => active_block.id }
295 assert_response :forbidden
297 # Login as the wrong moderator
298 session[:user] = second_moderator_user.id
300 # Check that only the person who created a block can update it
301 assert_no_difference "UserBlock.count" do
303 :params => { :id => active_block.id,
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[:user] = moderator_user.id
313 # A bogus block period should result in an error
314 assert_no_difference "UserBlock.count" do
316 :params => { :id => active_block.id,
317 :user_block_period => "99" }
319 assert_redirected_to edit_user_block_path(active_block)
320 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
322 # Check that updating a block works
323 assert_no_difference "UserBlock.count" do
325 :params => { :id => active_block.id,
326 :user_block_period => "12",
327 :user_block => { :needs_view => true, :reason => "Vandalism" } }
329 assert_redirected_to user_block_path(active_block)
330 assert_equal "Block updated.", flash[:notice]
331 b = UserBlock.find(active_block.id)
332 assert_in_delta Time.now, b.updated_at, 1
333 assert_equal true, b.needs_view
334 assert_equal "Vandalism", b.reason
336 # We should get an error if no block ID is specified
337 assert_raise ActionController::UrlGenerationError do
341 # We should get an error if the block doesn't exist
342 put :update, :params => { :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, :params => { :id => active_block.id }
355 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block.id))
357 # Login as a normal user
358 session[:user] = create(:user).id
360 # Check that normal users can't load the block revoke page
361 get :revoke, :params => { :id => active_block.id }
362 assert_response :forbidden
364 # Login as a moderator
365 session[:user] = create(:moderator_user).id
367 # Check that the block revoke page loads for moderators
368 get :revoke, :params => { :id => active_block.id }
369 assert_response :success
370 assert_template "revoke"
371 assert_select "form", :count => 1 do
372 assert_select "input#confirm[type='checkbox']", :count => 1
373 assert_select "input[type='submit'][value='Revoke!']", :count => 1
376 # Check that revoking a block works
377 post :revoke, :params => { :id => active_block.id, :confirm => true }
378 assert_redirected_to user_block_path(active_block)
379 b = UserBlock.find(active_block.id)
380 assert_in_delta Time.now, b.ends_at, 1
382 # We should get an error if no block ID is specified
383 assert_raise ActionController::UrlGenerationError do
387 # We should get an error if the block doesn't exist
388 get :revoke, :params => { :id => 99999 }
389 assert_response :not_found
390 assert_template "not_found"
391 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
395 # test the blocks_on action
397 blocked_user = create(:user)
398 unblocked_user = create(:user)
399 normal_user = create(:user)
400 active_block = create(:user_block, :user => blocked_user)
401 revoked_block = create(:user_block, :revoked, :user => blocked_user)
402 expired_block = create(:user_block, :expired, :user => unblocked_user)
404 # Asking for a list of blocks with no user name should fail
405 assert_raise ActionController::UrlGenerationError do
409 # Asking for a list of blocks with a bogus user name should fail
410 get :blocks_on, :params => { :display_name => "non_existent_user" }
411 assert_response :not_found
412 assert_template "users/no_such_user"
413 assert_select "h1", "The user non_existent_user does not exist"
415 # Check the list of blocks for a user that has never been blocked
416 get :blocks_on, :params => { :display_name => normal_user.display_name }
417 assert_response :success
418 assert_select "table#block_list", false
419 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
421 # Check the list of blocks for a user that is currently blocked
422 get :blocks_on, :params => { :display_name => blocked_user.display_name }
423 assert_response :success
424 assert_select "table#block_list", :count => 1 do
425 assert_select "tr", 3
426 assert_select "a[href='#{user_block_path(active_block)}']", 1
427 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
430 # Check the list of blocks for a user that has previously been blocked
431 get :blocks_on, :params => { :display_name => unblocked_user.display_name }
432 assert_response :success
433 assert_select "table#block_list", :count => 1 do
434 assert_select "tr", 2
435 assert_select "a[href='#{user_block_path(expired_block)}']", 1
440 # test the blocks_on action with multiple pages
441 def test_blocks_on_paged
443 create_list(:user_block, 50, :user => user)
445 get :blocks_on, :params => { :display_name => user.display_name }
446 assert_response :success
447 assert_select "table#block_list", :count => 1 do
448 assert_select "tr", :count => 21
451 get :blocks_on, :params => { :display_name => user.display_name, :page => 2 }
452 assert_response :success
453 assert_select "table#block_list", :count => 1 do
454 assert_select "tr", :count => 21
459 # test the blocks_by action
461 moderator_user = create(:moderator_user)
462 second_moderator_user = create(:moderator_user)
463 normal_user = create(:user)
464 active_block = create(:user_block, :creator => moderator_user)
465 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
466 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
468 # Asking for a list of blocks with no user name should fail
469 assert_raise ActionController::UrlGenerationError do
473 # Asking for a list of blocks with a bogus user name should fail
474 get :blocks_by, :params => { :display_name => "non_existent_user" }
475 assert_response :not_found
476 assert_template "users/no_such_user"
477 assert_select "h1", "The user non_existent_user does not exist"
479 # Check the list of blocks given by one moderator
480 get :blocks_by, :params => { :display_name => moderator_user.display_name }
481 assert_response :success
482 assert_select "table#block_list", :count => 1 do
483 assert_select "tr", 2
484 assert_select "a[href='#{user_block_path(active_block)}']", 1
487 # Check the list of blocks given by a different moderator
488 get :blocks_by, :params => { :display_name => second_moderator_user.display_name }
489 assert_response :success
490 assert_select "table#block_list", :count => 1 do
491 assert_select "tr", 3
492 assert_select "a[href='#{user_block_path(expired_block)}']", 1
493 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
496 # Check the list of blocks (not) given by a normal user
497 get :blocks_by, :params => { :display_name => normal_user.display_name }
498 assert_response :success
499 assert_select "table#block_list", false
500 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
504 # test the blocks_by action with multiple pages
505 def test_blocks_by_paged
506 user = create(:moderator_user)
507 create_list(:user_block, 50, :creator => user)
509 get :blocks_by, :params => { :display_name => user.display_name }
510 assert_response :success
511 assert_select "table#block_list", :count => 1 do
512 assert_select "tr", :count => 21
515 get :blocks_by, :params => { :display_name => user.display_name, :page => 2 }
516 assert_response :success
517 assert_select "table#block_list", :count => 1 do
518 assert_select "tr", :count => 21