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 :redirect
149 assert_redirected_to :controller => "errors", :action => "forbidden"
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 "users/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 "users/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(active_block))
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_response :redirect
193 assert_redirected_to :controller => "errors", :action => "forbidden"
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 :redirect
236 assert_redirected_to :controller => "errors", :action => "forbidden"
238 # Login as a moderator
239 session[:user] = moderator_user.id
241 # A bogus block period should result in an error
242 assert_no_difference "UserBlock.count" do
244 :params => { :display_name => target_user.display_name,
245 :user_block_period => "99" }
247 assert_redirected_to new_user_block_path(:display_name => target_user.display_name)
248 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
250 # Check that creating a block works
251 assert_difference "UserBlock.count", 1 do
253 :params => { :display_name => target_user.display_name,
254 :user_block_period => "12",
255 :user_block => { :needs_view => false, :reason => "Vandalism" } }
257 id = UserBlock.order(:id).ids.last
258 assert_redirected_to user_block_path(:id => id)
259 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
260 b = UserBlock.find(id)
261 assert_in_delta Time.now, b.created_at, 1
262 assert_in_delta Time.now, b.updated_at, 1
263 assert_in_delta Time.now + 12.hours, b.ends_at, 1
264 assert_equal false, b.needs_view
265 assert_equal "Vandalism", b.reason
266 assert_equal "markdown", b.reason_format
267 assert_equal moderator_user.id, b.creator_id
269 # We should get an error if no user is specified
271 assert_response :not_found
272 assert_template "users/no_such_user"
273 assert_select "h1", "The user does not exist"
275 # We should get an error if the user doesn't exist
276 post :create, :params => { :display_name => "non_existent_user" }
277 assert_response :not_found
278 assert_template "users/no_such_user"
279 assert_select "h1", "The user non_existent_user does not exist"
283 # test the update action
285 moderator_user = create(:moderator_user)
286 second_moderator_user = create(:moderator_user)
287 active_block = create(:user_block, :creator => moderator_user)
289 # Not logged in yet, so updating a block should fail
290 put :update, :params => { :id => active_block.id }
291 assert_response :forbidden
293 # Login as a normal user
294 session[:user] = create(:user).id
296 # Check that normal users can't update blocks
297 put :update, :params => { :id => active_block.id }
298 assert_response :redirect
299 assert_redirected_to :controller => "errors", :action => "forbidden"
301 # Login as the wrong moderator
302 session[:user] = second_moderator_user.id
304 # Check that only the person who created a block can update it
305 assert_no_difference "UserBlock.count" do
307 :params => { :id => active_block.id,
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[:user] = moderator_user.id
317 # A bogus block period should result in an error
318 assert_no_difference "UserBlock.count" do
320 :params => { :id => active_block.id,
321 :user_block_period => "99" }
323 assert_redirected_to edit_user_block_path(active_block)
324 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
326 # Check that updating a block works
327 assert_no_difference "UserBlock.count" do
329 :params => { :id => active_block.id,
330 :user_block_period => "12",
331 :user_block => { :needs_view => true, :reason => "Vandalism" } }
333 assert_redirected_to user_block_path(active_block)
334 assert_equal "Block updated.", flash[:notice]
335 b = UserBlock.find(active_block.id)
336 assert_in_delta Time.now, b.updated_at, 1
337 assert_equal true, b.needs_view
338 assert_equal "Vandalism", b.reason
340 # We should get an error if no block ID is specified
341 assert_raise ActionController::UrlGenerationError do
345 # We should get an error if the block doesn't exist
346 put :update, :params => { :id => 99999 }
347 assert_response :not_found
348 assert_template "not_found"
349 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
353 # test the revoke action
355 active_block = create(:user_block)
357 # Check that the block revoke page requires us to login
358 get :revoke, :params => { :id => active_block.id }
359 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block.id))
361 # Login as a normal user
362 session[:user] = create(:user).id
364 # Check that normal users can't load the block revoke page
365 get :revoke, :params => { :id => active_block.id }
366 assert_response :redirect
367 assert_redirected_to :controller => "errors", :action => "forbidden"
369 # Login as a moderator
370 session[:user] = create(:moderator_user).id
372 # Check that the block revoke page loads for moderators
373 get :revoke, :params => { :id => active_block.id }
374 assert_response :success
375 assert_template "revoke"
376 assert_select "form", :count => 1 do
377 assert_select "input#confirm[type='checkbox']", :count => 1
378 assert_select "input[type='submit'][value='Revoke!']", :count => 1
381 # Check that revoking a block works
382 post :revoke, :params => { :id => active_block.id, :confirm => true }
383 assert_redirected_to user_block_path(active_block)
384 b = UserBlock.find(active_block.id)
385 assert_in_delta Time.now, b.ends_at, 1
387 # We should get an error if no block ID is specified
388 assert_raise ActionController::UrlGenerationError do
392 # We should get an error if the block doesn't exist
393 get :revoke, :params => { :id => 99999 }
394 assert_response :not_found
395 assert_template "not_found"
396 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
400 # test the blocks_on action
402 blocked_user = create(:user)
403 unblocked_user = create(:user)
404 normal_user = create(:user)
405 active_block = create(:user_block, :user => blocked_user)
406 revoked_block = create(:user_block, :revoked, :user => blocked_user)
407 expired_block = create(:user_block, :expired, :user => unblocked_user)
409 # Asking for a list of blocks with no user name should fail
410 assert_raise ActionController::UrlGenerationError do
414 # Asking for a list of blocks with a bogus user name should fail
415 get :blocks_on, :params => { :display_name => "non_existent_user" }
416 assert_response :not_found
417 assert_template "users/no_such_user"
418 assert_select "h1", "The user non_existent_user does not exist"
420 # Check the list of blocks for a user that has never been blocked
421 get :blocks_on, :params => { :display_name => normal_user.display_name }
422 assert_response :success
423 assert_select "table#block_list", false
424 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
426 # Check the list of blocks for a user that is currently blocked
427 get :blocks_on, :params => { :display_name => blocked_user.display_name }
428 assert_response :success
429 assert_select "table#block_list", :count => 1 do
430 assert_select "tr", 3
431 assert_select "a[href='#{user_block_path(active_block)}']", 1
432 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
435 # Check the list of blocks for a user that has previously been blocked
436 get :blocks_on, :params => { :display_name => unblocked_user.display_name }
437 assert_response :success
438 assert_select "table#block_list", :count => 1 do
439 assert_select "tr", 2
440 assert_select "a[href='#{user_block_path(expired_block)}']", 1
445 # test the blocks_on action with multiple pages
446 def test_blocks_on_paged
448 create_list(:user_block, 50, :user => user)
450 get :blocks_on, :params => { :display_name => user.display_name }
451 assert_response :success
452 assert_select "table#block_list", :count => 1 do
453 assert_select "tr", :count => 21
456 get :blocks_on, :params => { :display_name => user.display_name, :page => 2 }
457 assert_response :success
458 assert_select "table#block_list", :count => 1 do
459 assert_select "tr", :count => 21
464 # test the blocks_by action
466 moderator_user = create(:moderator_user)
467 second_moderator_user = create(:moderator_user)
468 normal_user = create(:user)
469 active_block = create(:user_block, :creator => moderator_user)
470 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
471 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
473 # Asking for a list of blocks with no user name should fail
474 assert_raise ActionController::UrlGenerationError do
478 # Asking for a list of blocks with a bogus user name should fail
479 get :blocks_by, :params => { :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 given by one moderator
485 get :blocks_by, :params => { :display_name => moderator_user.display_name }
486 assert_response :success
487 assert_select "table#block_list", :count => 1 do
488 assert_select "tr", 2
489 assert_select "a[href='#{user_block_path(active_block)}']", 1
492 # Check the list of blocks given by a different moderator
493 get :blocks_by, :params => { :display_name => second_moderator_user.display_name }
494 assert_response :success
495 assert_select "table#block_list", :count => 1 do
496 assert_select "tr", 3
497 assert_select "a[href='#{user_block_path(expired_block)}']", 1
498 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
501 # Check the list of blocks (not) given by a normal user
502 get :blocks_by, :params => { :display_name => normal_user.display_name }
503 assert_response :success
504 assert_select "table#block_list", false
505 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
509 # test the blocks_by action with multiple pages
510 def test_blocks_by_paged
511 user = create(:moderator_user)
512 create_list(:user_block, 50, :creator => user)
514 get :blocks_by, :params => { :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 :blocks_by, :params => { :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