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 # Check that the block creation page requires us to login
122 get :new, :display_name => users(:normal_user).display_name
123 assert_redirected_to login_path(:referer => new_user_block_path(:display_name => users(:normal_user).display_name))
125 # Login as a normal user
126 session[:user] = users(:public_user).id
128 # Check that normal users can't load the block creation page
129 get :new, :display_name => users(:normal_user).display_name
130 assert_redirected_to user_blocks_path
131 assert_equal "You need to be a moderator to perform that action.", flash[:error]
133 # Login as a moderator
134 session[:user] = users(:moderator_user).id
136 # Check that the block creation page loads for moderators
137 get :new, :display_name => users(:normal_user).display_name
138 assert_response :success
139 assert_select "form#new_user_block", :count => 1 do
140 assert_select "textarea#user_block_reason", :count => 1
141 assert_select "select#user_block_period", :count => 1
142 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
143 assert_select "input#display_name[type='hidden']", :count => 1
144 assert_select "input[type='submit'][value='Create block']", :count => 1
147 # We should get an error if no user is specified
149 assert_response :not_found
150 assert_template "user/no_such_user"
151 assert_select "h1", "The user does not exist"
153 # We should get an error if the user doesn't exist
154 get :new, :display_name => "non_existent_user"
155 assert_response :not_found
156 assert_template "user/no_such_user"
157 assert_select "h1", "The user non_existent_user does not exist"
161 # test the edit action
163 active_block = create(:user_block)
165 # Check that the block edit page requires us to login
166 get :edit, :id => active_block.id
167 assert_redirected_to login_path(:referer => edit_user_block_path(:id => active_block.id))
169 # Login as a normal user
170 session[:user] = users(:public_user).id
172 # Check that normal users can't load the block edit page
173 get :edit, :id => active_block.id
174 assert_redirected_to user_blocks_path
175 assert_equal "You need to be a moderator to perform that action.", flash[:error]
177 # Login as a moderator
178 session[:user] = users(:moderator_user).id
180 # Check that the block edit page loads for moderators
181 get :edit, :id => active_block.id
182 assert_response :success
183 assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
184 assert_select "textarea#user_block_reason", :count => 1
185 assert_select "select#user_block_period", :count => 1
186 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
187 assert_select "input[type='submit'][value='Update block']", :count => 1
190 # We should get an error if no user is specified
191 assert_raise ActionController::UrlGenerationError do
195 # We should get an error if the user doesn't exist
196 get :edit, :id => 99999
197 assert_response :not_found
198 assert_template "not_found"
199 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
203 # test the create action
205 # Not logged in yet, so creating a block should fail
207 assert_response :forbidden
209 # Login as a normal user
210 session[:user] = users(:public_user).id
212 # Check that normal users can't create blocks
214 assert_response :forbidden
216 # Login as a moderator
217 session[:user] = users(:moderator_user).id
219 # A bogus block period should result in an error
220 assert_no_difference "UserBlock.count" do
222 :display_name => users(:unblocked_user).display_name,
223 :user_block_period => "99"
225 assert_redirected_to new_user_block_path(:display_name => users(:unblocked_user).display_name)
226 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
228 # Check that creating a block works
229 assert_difference "UserBlock.count", 1 do
231 :display_name => users(:unblocked_user).display_name,
232 :user_block_period => "12",
233 :user_block => { :needs_view => false, :reason => "Vandalism" }
235 id = UserBlock.order(:id).ids.last
236 assert_redirected_to user_block_path(:id => id)
237 assert_equal "Created a block on user #{users(:unblocked_user).display_name}.", flash[:notice]
238 b = UserBlock.find(id)
239 assert_in_delta Time.now, b.created_at, 1
240 assert_in_delta Time.now, b.updated_at, 1
241 assert_in_delta Time.now + 12.hours, b.ends_at, 1
242 assert_equal false, b.needs_view
243 assert_equal "Vandalism", b.reason
244 assert_equal "markdown", b.reason_format
245 assert_equal users(:moderator_user).id, b.creator_id
247 # We should get an error if no user is specified
249 assert_response :not_found
250 assert_template "user/no_such_user"
251 assert_select "h1", "The user does not exist"
253 # We should get an error if the user doesn't exist
254 post :create, :display_name => "non_existent_user"
255 assert_response :not_found
256 assert_template "user/no_such_user"
257 assert_select "h1", "The user non_existent_user does not exist"
261 # test the update action
263 active_block = create(:user_block, :creator => users(:moderator_user))
265 # Not logged in yet, so updating a block should fail
266 put :update, :id => active_block.id
267 assert_response :forbidden
269 # Login as a normal user
270 session[:user] = users(:public_user).id
272 # Check that normal users can't update blocks
273 put :update, :id => active_block.id
274 assert_response :forbidden
276 # Login as the wrong moderator
277 session[:user] = users(:second_moderator_user).id
279 # Check that only the person who created a block can update it
280 assert_no_difference "UserBlock.count" do
282 :id => active_block.id,
283 :user_block_period => "12",
284 :user_block => { :needs_view => true, :reason => "Vandalism" }
286 assert_redirected_to edit_user_block_path(:id => active_block.id)
287 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
289 # Login as the correct moderator
290 session[:user] = users(:moderator_user).id
292 # A bogus block period should result in an error
293 assert_no_difference "UserBlock.count" do
295 :id => active_block.id,
296 :user_block_period => "99"
298 assert_redirected_to edit_user_block_path(:id => active_block.id)
299 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
301 # Check that updating a block works
302 assert_no_difference "UserBlock.count" do
304 :id => active_block.id,
305 :user_block_period => "12",
306 :user_block => { :needs_view => true, :reason => "Vandalism" }
308 assert_redirected_to user_block_path(:id => active_block.id)
309 assert_equal "Block updated.", flash[:notice]
310 b = UserBlock.find(active_block.id)
311 assert_in_delta Time.now, b.updated_at, 1
312 assert_equal true, b.needs_view
313 assert_equal "Vandalism", b.reason
315 # We should get an error if no block ID is specified
316 assert_raise ActionController::UrlGenerationError do
320 # We should get an error if the block doesn't exist
321 put :update, :id => 99999
322 assert_response :not_found
323 assert_template "not_found"
324 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
328 # test the revoke action
330 active_block = create(:user_block)
332 # Check that the block revoke page requires us to login
333 get :revoke, :id => active_block.id
334 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block.id))
336 # Login as a normal user
337 session[:user] = users(:public_user).id
339 # Check that normal users can't load the block revoke page
340 get :revoke, :id => active_block.id
341 assert_redirected_to user_blocks_path
342 assert_equal "You need to be a moderator to perform that action.", flash[:error]
344 # Login as a moderator
345 session[:user] = users(:moderator_user).id
347 # Check that the block revoke page loads for moderators
348 get :revoke, :id => active_block.id
349 assert_response :success
350 assert_template "revoke"
351 assert_select "form", :count => 1 do
352 assert_select "input#confirm[type='checkbox']", :count => 1
353 assert_select "input[type='submit'][value='Revoke!']", :count => 1
356 # Check that revoking a block works
357 post :revoke, :id => active_block.id, :confirm => true
358 assert_redirected_to user_block_path(:id => active_block.id)
359 b = UserBlock.find(active_block.id)
360 assert_in_delta Time.now, b.ends_at, 1
362 # We should get an error if no block ID is specified
363 assert_raise ActionController::UrlGenerationError do
367 # We should get an error if the block doesn't exist
368 get :revoke, :id => 99999
369 assert_response :not_found
370 assert_template "not_found"
371 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
375 # test the blocks_on action
377 active_block = create(:user_block, :user => users(:blocked_user))
378 revoked_block = create(:user_block, :revoked, :user => users(:blocked_user))
379 expired_block = create(:user_block, :expired, :user => users(:unblocked_user))
381 # Asking for a list of blocks with no user name should fail
382 assert_raise ActionController::UrlGenerationError do
386 # Asking for a list of blocks with a bogus user name should fail
387 get :blocks_on, :display_name => "non_existent_user"
388 assert_response :not_found
389 assert_template "user/no_such_user"
390 assert_select "h1", "The user non_existent_user does not exist"
392 # Check the list of blocks for a user that has never been blocked
393 get :blocks_on, :display_name => users(:normal_user).display_name
394 assert_response :success
395 assert_select "table#block_list", false
396 assert_select "p", "#{users(:normal_user).display_name} has not been blocked yet."
398 # Check the list of blocks for a user that is currently blocked
399 get :blocks_on, :display_name => users(:blocked_user).display_name
400 assert_response :success
401 assert_select "table#block_list", :count => 1 do
402 assert_select "tr", 3
403 assert_select "a[href='#{user_block_path(active_block)}']", 1
404 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
407 # Check the list of blocks for a user that has previously been blocked
408 get :blocks_on, :display_name => users(:unblocked_user).display_name
409 assert_response :success
410 assert_select "table#block_list", :count => 1 do
411 assert_select "tr", 2
412 assert_select "a[href='#{user_block_path(expired_block)}']", 1
417 # test the blocks_by action
419 active_block = create(:user_block, :creator => users(:moderator_user))
420 expired_block = create(:user_block, :expired, :creator => users(:second_moderator_user))
421 revoked_block = create(:user_block, :revoked, :creator => users(:second_moderator_user))
423 # Asking for a list of blocks with no user name should fail
424 assert_raise ActionController::UrlGenerationError do
428 # Asking for a list of blocks with a bogus user name should fail
429 get :blocks_by, :display_name => "non_existent_user"
430 assert_response :not_found
431 assert_template "user/no_such_user"
432 assert_select "h1", "The user non_existent_user does not exist"
434 # Check the list of blocks given by one moderator
435 get :blocks_by, :display_name => users(:moderator_user).display_name
436 assert_response :success
437 assert_select "table#block_list", :count => 1 do
438 assert_select "tr", 2
439 assert_select "a[href='#{user_block_path(active_block)}']", 1
442 # Check the list of blocks given by a different moderator
443 get :blocks_by, :display_name => users(:second_moderator_user).display_name
444 assert_response :success
445 assert_select "table#block_list", :count => 1 do
446 assert_select "tr", 3
447 assert_select "a[href='#{user_block_path(expired_block)}']", 1
448 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
451 # Check the list of blocks (not) given by a normal user
452 get :blocks_by, :display_name => users(:normal_user).display_name
453 assert_response :success
454 assert_select "table#block_list", false
455 assert_select "p", "#{users(:normal_user).display_name} has not made any blocks yet."