3 class UserBlocksControllerTest < ActionController::TestCase
4 fixtures :users, :user_roles
7 # test all routes which lead to this controller
10 { :path => "/blocks/new/username", :method => :get },
11 { :controller => "user_blocks", :action => "new", :display_name => "username" }
15 { :path => "/user_blocks", :method => :get },
16 { :controller => "user_blocks", :action => "index" }
19 { :path => "/user_blocks/new", :method => :get },
20 { :controller => "user_blocks", :action => "new" }
23 { :path => "/user_blocks", :method => :post },
24 { :controller => "user_blocks", :action => "create" }
27 { :path => "/user_blocks/1", :method => :get },
28 { :controller => "user_blocks", :action => "show", :id => "1" }
31 { :path => "/user_blocks/1/edit", :method => :get },
32 { :controller => "user_blocks", :action => "edit", :id => "1" }
35 { :path => "/user_blocks/1", :method => :put },
36 { :controller => "user_blocks", :action => "update", :id => "1" }
39 { :path => "/user_blocks/1", :method => :delete },
40 { :controller => "user_blocks", :action => "destroy", :id => "1" }
43 { :path => "/blocks/1/revoke", :method => :get },
44 { :controller => "user_blocks", :action => "revoke", :id => "1" }
47 { :path => "/blocks/1/revoke", :method => :post },
48 { :controller => "user_blocks", :action => "revoke", :id => "1" }
52 { :path => "/user/username/blocks", :method => :get },
53 { :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
56 { :path => "/user/username/blocks_by", :method => :get },
57 { :controller => "user_blocks", :action => "blocks_by", :display_name => "username" }
62 # test the index action
64 active_block = create(:user_block)
65 expired_block = create(:user_block, :expired)
66 revoked_block = create(:user_block, :revoked)
69 assert_response :success
70 assert_select "table#block_list", :count => 1 do
72 assert_select "a[href='#{user_block_path(active_block)}']", 1
73 assert_select "a[href='#{user_block_path(expired_block)}']", 1
74 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
79 # test the show action
81 active_block = create(:user_block, :needs_view)
82 expired_block = create(:user_block, :expired)
83 revoked_block = create(:user_block, :revoked)
85 # Viewing a block should fail when no ID is given
86 assert_raise ActionController::UrlGenerationError do
90 # Viewing a block should fail when a bogus ID is given
91 get :show, :id => 99999
92 assert_response :not_found
93 assert_template "not_found"
94 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
96 # Viewing an expired block should work
97 get :show, :id => expired_block.id
98 assert_response :success
100 # Viewing a revoked block should work
101 get :show, :id => revoked_block.id
102 assert_response :success
104 # Viewing an active block should work, but shouldn't mark it as seen
105 get :show, :id => active_block.id
106 assert_response :success
107 assert_equal true, UserBlock.find(active_block.id).needs_view
109 # Login as the blocked user
110 session[:user] = active_block.user.id
112 # Now viewing it should mark it as seen
113 get :show, :id => active_block.id
114 assert_response :success
115 assert_equal false, UserBlock.find(active_block.id).needs_view
119 # test the new action
121 target_user = create(:user)
123 # Check that the block creation page requires us to login
124 get :new, :display_name => target_user.display_name
125 assert_redirected_to login_path(:referer => new_user_block_path(:display_name => target_user.display_name))
127 # Login as a normal user
128 session[:user] = create(:user).id
130 # Check that normal users can't load the block creation page
131 get :new, :display_name => target_user.display_name
132 assert_redirected_to user_blocks_path
133 assert_equal "You need to be a moderator to perform that action.", flash[:error]
135 # Login as a moderator
136 session[:user] = create(:moderator_user).id
138 # Check that the block creation page loads for moderators
139 get :new, :display_name => target_user.display_name
140 assert_response :success
141 assert_select "form#new_user_block", :count => 1 do
142 assert_select "textarea#user_block_reason", :count => 1
143 assert_select "select#user_block_period", :count => 1
144 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
145 assert_select "input#display_name[type='hidden']", :count => 1
146 assert_select "input[type='submit'][value='Create block']", :count => 1
149 # We should get an error if no user is specified
151 assert_response :not_found
152 assert_template "user/no_such_user"
153 assert_select "h1", "The user does not exist"
155 # We should get an error if the user doesn't exist
156 get :new, :display_name => "non_existent_user"
157 assert_response :not_found
158 assert_template "user/no_such_user"
159 assert_select "h1", "The user non_existent_user does not exist"
163 # test the edit action
165 active_block = create(:user_block)
167 # Check that the block edit page requires us to login
168 get :edit, :id => active_block.id
169 assert_redirected_to login_path(:referer => edit_user_block_path(:id => active_block.id))
171 # Login as a normal user
172 session[:user] = create(:user).id
174 # Check that normal users can't load the block edit page
175 get :edit, :id => active_block.id
176 assert_redirected_to user_blocks_path
177 assert_equal "You need to be a moderator to perform that action.", flash[:error]
179 # Login as a moderator
180 session[:user] = create(:moderator_user).id
182 # Check that the block edit page loads for moderators
183 get :edit, :id => active_block.id
184 assert_response :success
185 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
186 assert_select "textarea#user_block_reason", :count => 1
187 assert_select "select#user_block_period", :count => 1
188 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
189 assert_select "input[type='submit'][value='Update block']", :count => 1
192 # We should get an error if no user is specified
193 assert_raise ActionController::UrlGenerationError do
197 # We should get an error if the user doesn't exist
198 get :edit, :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
212 assert_response :forbidden
214 # Login as a normal user
215 session[:user] = create(:user).id
217 # Check that normal users can't create blocks
219 assert_response :forbidden
221 # Login as a moderator
222 session[:user] = moderator_user.id
224 # A bogus block period should result in an error
225 assert_no_difference "UserBlock.count" do
227 :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
236 :display_name => target_user.display_name,
237 :user_block_period => "12",
238 :user_block => { :needs_view => false, :reason => "Vandalism" }
240 id = UserBlock.order(:id).ids.last
241 assert_redirected_to user_block_path(:id => id)
242 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
243 b = UserBlock.find(id)
244 assert_in_delta Time.now, b.created_at, 1
245 assert_in_delta Time.now, b.updated_at, 1
246 assert_in_delta Time.now + 12.hours, b.ends_at, 1
247 assert_equal false, b.needs_view
248 assert_equal "Vandalism", b.reason
249 assert_equal "markdown", b.reason_format
250 assert_equal moderator_user.id, b.creator_id
252 # We should get an error if no user is specified
254 assert_response :not_found
255 assert_template "user/no_such_user"
256 assert_select "h1", "The user does not exist"
258 # We should get an error if the user doesn't exist
259 post :create, :display_name => "non_existent_user"
260 assert_response :not_found
261 assert_template "user/no_such_user"
262 assert_select "h1", "The user non_existent_user does not exist"
266 # test the update action
268 moderator_user = create(:moderator_user)
269 second_moderator_user = create(:moderator_user)
270 active_block = create(:user_block, :creator => moderator_user)
272 # Not logged in yet, so updating a block should fail
273 put :update, :id => active_block.id
274 assert_response :forbidden
276 # Login as a normal user
277 session[:user] = create(:user).id
279 # Check that normal users can't update blocks
280 put :update, :id => active_block.id
281 assert_response :forbidden
283 # Login as the wrong moderator
284 session[:user] = second_moderator_user.id
286 # Check that only the person who created a block can update it
287 assert_no_difference "UserBlock.count" do
289 :id => active_block.id,
290 :user_block_period => "12",
291 :user_block => { :needs_view => true, :reason => "Vandalism" }
293 assert_redirected_to edit_user_block_path(:id => active_block.id)
294 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
296 # Login as the correct moderator
297 session[:user] = moderator_user.id
299 # A bogus block period should result in an error
300 assert_no_difference "UserBlock.count" do
302 :id => active_block.id,
303 :user_block_period => "99"
305 assert_redirected_to edit_user_block_path(:id => active_block.id)
306 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
308 # Check that updating a block works
309 assert_no_difference "UserBlock.count" do
311 :id => active_block.id,
312 :user_block_period => "12",
313 :user_block => { :needs_view => true, :reason => "Vandalism" }
315 assert_redirected_to user_block_path(:id => active_block.id)
316 assert_equal "Block updated.", flash[:notice]
317 b = UserBlock.find(active_block.id)
318 assert_in_delta Time.now, b.updated_at, 1
319 assert_equal true, b.needs_view
320 assert_equal "Vandalism", b.reason
322 # We should get an error if no block ID is specified
323 assert_raise ActionController::UrlGenerationError do
327 # We should get an error if the block doesn't exist
328 put :update, :id => 99999
329 assert_response :not_found
330 assert_template "not_found"
331 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
335 # test the revoke action
337 active_block = create(:user_block)
339 # Check that the block revoke page requires us to login
340 get :revoke, :id => active_block.id
341 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block.id))
343 # Login as a normal user
344 session[:user] = create(:user).id
346 # Check that normal users can't load the block revoke page
347 get :revoke, :id => active_block.id
348 assert_redirected_to user_blocks_path
349 assert_equal "You need to be a moderator to perform that action.", flash[:error]
351 # Login as a moderator
352 session[:user] = create(:moderator_user).id
354 # Check that the block revoke page loads for moderators
355 get :revoke, :id => active_block.id
356 assert_response :success
357 assert_template "revoke"
358 assert_select "form", :count => 1 do
359 assert_select "input#confirm[type='checkbox']", :count => 1
360 assert_select "input[type='submit'][value='Revoke!']", :count => 1
363 # Check that revoking a block works
364 post :revoke, :id => active_block.id, :confirm => true
365 assert_redirected_to user_block_path(:id => active_block.id)
366 b = UserBlock.find(active_block.id)
367 assert_in_delta Time.now, b.ends_at, 1
369 # We should get an error if no block ID is specified
370 assert_raise ActionController::UrlGenerationError do
374 # We should get an error if the block doesn't exist
375 get :revoke, :id => 99999
376 assert_response :not_found
377 assert_template "not_found"
378 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
382 # test the blocks_on action
384 blocked_user = create(:user)
385 unblocked_user = create(:user)
386 normal_user = create(:user)
387 active_block = create(:user_block, :user => blocked_user)
388 revoked_block = create(:user_block, :revoked, :user => blocked_user)
389 expired_block = create(:user_block, :expired, :user => unblocked_user)
391 # Asking for a list of blocks with no user name should fail
392 assert_raise ActionController::UrlGenerationError do
396 # Asking for a list of blocks with a bogus user name should fail
397 get :blocks_on, :display_name => "non_existent_user"
398 assert_response :not_found
399 assert_template "user/no_such_user"
400 assert_select "h1", "The user non_existent_user does not exist"
402 # Check the list of blocks for a user that has never been blocked
403 get :blocks_on, :display_name => normal_user.display_name
404 assert_response :success
405 assert_select "table#block_list", false
406 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
408 # Check the list of blocks for a user that is currently blocked
409 get :blocks_on, :display_name => blocked_user.display_name
410 assert_response :success
411 assert_select "table#block_list", :count => 1 do
412 assert_select "tr", 3
413 assert_select "a[href='#{user_block_path(active_block)}']", 1
414 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
417 # Check the list of blocks for a user that has previously been blocked
418 get :blocks_on, :display_name => unblocked_user.display_name
419 assert_response :success
420 assert_select "table#block_list", :count => 1 do
421 assert_select "tr", 2
422 assert_select "a[href='#{user_block_path(expired_block)}']", 1
427 # test the blocks_by action
429 moderator_user = create(:moderator_user)
430 second_moderator_user = create(:moderator_user)
431 normal_user = create(:user)
432 active_block = create(:user_block, :creator => moderator_user)
433 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
434 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
436 # Asking for a list of blocks with no user name should fail
437 assert_raise ActionController::UrlGenerationError do
441 # Asking for a list of blocks with a bogus user name should fail
442 get :blocks_by, :display_name => "non_existent_user"
443 assert_response :not_found
444 assert_template "user/no_such_user"
445 assert_select "h1", "The user non_existent_user does not exist"
447 # Check the list of blocks given by one moderator
448 get :blocks_by, :display_name => moderator_user.display_name
449 assert_response :success
450 assert_select "table#block_list", :count => 1 do
451 assert_select "tr", 2
452 assert_select "a[href='#{user_block_path(active_block)}']", 1
455 # Check the list of blocks given by a different moderator
456 get :blocks_by, :display_name => second_moderator_user.display_name
457 assert_response :success
458 assert_select "table#block_list", :count => 1 do
459 assert_select "tr", 3
460 assert_select "a[href='#{user_block_path(expired_block)}']", 1
461 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
464 # Check the list of blocks (not) given by a normal user
465 get :blocks_by, :display_name => normal_user.display_name
466 assert_response :success
467 assert_select "table#block_list", false
468 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."