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 show action
79 active_block = create(:user_block, :needs_view)
80 expired_block = create(:user_block, :expired)
81 revoked_block = create(:user_block, :revoked)
83 # Viewing a block should fail when no ID is given
84 assert_raise ActionController::UrlGenerationError do
88 # Viewing a block should fail when a bogus ID is given
89 get :show, :id => 99999
90 assert_response :not_found
91 assert_template "not_found"
92 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
94 # Viewing an expired block should work
95 get :show, :id => expired_block.id
96 assert_response :success
98 # Viewing a revoked block should work
99 get :show, :id => revoked_block.id
100 assert_response :success
102 # Viewing an active block should work, but shouldn't mark it as seen
103 get :show, :id => active_block.id
104 assert_response :success
105 assert_equal true, UserBlock.find(active_block.id).needs_view
107 # Login as the blocked user
108 session[:user] = active_block.user.id
110 # Now viewing it should mark it as seen
111 get :show, :id => active_block.id
112 assert_response :success
113 assert_equal false, UserBlock.find(active_block.id).needs_view
117 # test the new action
119 target_user = create(:user)
121 # Check that the block creation page requires us to login
122 get :new, :display_name => target_user.display_name
123 assert_redirected_to login_path(:referer => new_user_block_path(:display_name => target_user.display_name))
125 # Login as a normal user
126 session[:user] = create(:user).id
128 # Check that normal users can't load the block creation page
129 get :new, :display_name => target_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] = create(:moderator_user).id
136 # Check that the block creation page loads for moderators
137 get :new, :display_name => target_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] = create(: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] = create(: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 target_user = create(:user)
206 moderator_user = create(:moderator_user)
208 # Not logged in yet, so creating a block should fail
210 assert_response :forbidden
212 # Login as a normal user
213 session[:user] = create(:user).id
215 # Check that normal users can't create blocks
217 assert_response :forbidden
219 # Login as a moderator
220 session[:user] = moderator_user.id
222 # A bogus block period should result in an error
223 assert_no_difference "UserBlock.count" do
225 :display_name => target_user.display_name,
226 :user_block_period => "99"
228 assert_redirected_to new_user_block_path(:display_name => target_user.display_name)
229 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
231 # Check that creating a block works
232 assert_difference "UserBlock.count", 1 do
234 :display_name => target_user.display_name,
235 :user_block_period => "12",
236 :user_block => { :needs_view => false, :reason => "Vandalism" }
238 id = UserBlock.order(:id).ids.last
239 assert_redirected_to user_block_path(:id => id)
240 assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
241 b = UserBlock.find(id)
242 assert_in_delta Time.now, b.created_at, 1
243 assert_in_delta Time.now, b.updated_at, 1
244 assert_in_delta Time.now + 12.hours, b.ends_at, 1
245 assert_equal false, b.needs_view
246 assert_equal "Vandalism", b.reason
247 assert_equal "markdown", b.reason_format
248 assert_equal moderator_user.id, b.creator_id
250 # We should get an error if no user is specified
252 assert_response :not_found
253 assert_template "user/no_such_user"
254 assert_select "h1", "The user does not exist"
256 # We should get an error if the user doesn't exist
257 post :create, :display_name => "non_existent_user"
258 assert_response :not_found
259 assert_template "user/no_such_user"
260 assert_select "h1", "The user non_existent_user does not exist"
264 # test the update action
266 moderator_user = create(:moderator_user)
267 second_moderator_user = create(:moderator_user)
268 active_block = create(:user_block, :creator => moderator_user)
270 # Not logged in yet, so updating a block should fail
271 put :update, :id => active_block.id
272 assert_response :forbidden
274 # Login as a normal user
275 session[:user] = create(:user).id
277 # Check that normal users can't update blocks
278 put :update, :id => active_block.id
279 assert_response :forbidden
281 # Login as the wrong moderator
282 session[:user] = second_moderator_user.id
284 # Check that only the person who created a block can update it
285 assert_no_difference "UserBlock.count" do
287 :id => active_block.id,
288 :user_block_period => "12",
289 :user_block => { :needs_view => true, :reason => "Vandalism" }
291 assert_redirected_to edit_user_block_path(:id => active_block.id)
292 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
294 # Login as the correct moderator
295 session[:user] = moderator_user.id
297 # A bogus block period should result in an error
298 assert_no_difference "UserBlock.count" do
300 :id => active_block.id,
301 :user_block_period => "99"
303 assert_redirected_to edit_user_block_path(:id => active_block.id)
304 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
306 # Check that updating a block works
307 assert_no_difference "UserBlock.count" do
309 :id => active_block.id,
310 :user_block_period => "12",
311 :user_block => { :needs_view => true, :reason => "Vandalism" }
313 assert_redirected_to user_block_path(:id => active_block.id)
314 assert_equal "Block updated.", flash[:notice]
315 b = UserBlock.find(active_block.id)
316 assert_in_delta Time.now, b.updated_at, 1
317 assert_equal true, b.needs_view
318 assert_equal "Vandalism", b.reason
320 # We should get an error if no block ID is specified
321 assert_raise ActionController::UrlGenerationError do
325 # We should get an error if the block doesn't exist
326 put :update, :id => 99999
327 assert_response :not_found
328 assert_template "not_found"
329 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
333 # test the revoke action
335 active_block = create(:user_block)
337 # Check that the block revoke page requires us to login
338 get :revoke, :id => active_block.id
339 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block.id))
341 # Login as a normal user
342 session[:user] = create(:user).id
344 # Check that normal users can't load the block revoke page
345 get :revoke, :id => active_block.id
346 assert_redirected_to user_blocks_path
347 assert_equal "You need to be a moderator to perform that action.", flash[:error]
349 # Login as a moderator
350 session[:user] = create(:moderator_user).id
352 # Check that the block revoke page loads for moderators
353 get :revoke, :id => active_block.id
354 assert_response :success
355 assert_template "revoke"
356 assert_select "form", :count => 1 do
357 assert_select "input#confirm[type='checkbox']", :count => 1
358 assert_select "input[type='submit'][value='Revoke!']", :count => 1
361 # Check that revoking a block works
362 post :revoke, :id => active_block.id, :confirm => true
363 assert_redirected_to user_block_path(:id => active_block.id)
364 b = UserBlock.find(active_block.id)
365 assert_in_delta Time.now, b.ends_at, 1
367 # We should get an error if no block ID is specified
368 assert_raise ActionController::UrlGenerationError do
372 # We should get an error if the block doesn't exist
373 get :revoke, :id => 99999
374 assert_response :not_found
375 assert_template "not_found"
376 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
380 # test the blocks_on action
382 blocked_user = create(:user)
383 unblocked_user = create(:user)
384 normal_user = create(:user)
385 active_block = create(:user_block, :user => blocked_user)
386 revoked_block = create(:user_block, :revoked, :user => blocked_user)
387 expired_block = create(:user_block, :expired, :user => unblocked_user)
389 # Asking for a list of blocks with no user name should fail
390 assert_raise ActionController::UrlGenerationError do
394 # Asking for a list of blocks with a bogus user name should fail
395 get :blocks_on, :display_name => "non_existent_user"
396 assert_response :not_found
397 assert_template "user/no_such_user"
398 assert_select "h1", "The user non_existent_user does not exist"
400 # Check the list of blocks for a user that has never been blocked
401 get :blocks_on, :display_name => normal_user.display_name
402 assert_response :success
403 assert_select "table#block_list", false
404 assert_select "p", "#{normal_user.display_name} has not been blocked yet."
406 # Check the list of blocks for a user that is currently blocked
407 get :blocks_on, :display_name => blocked_user.display_name
408 assert_response :success
409 assert_select "table#block_list", :count => 1 do
410 assert_select "tr", 3
411 assert_select "a[href='#{user_block_path(active_block)}']", 1
412 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
415 # Check the list of blocks for a user that has previously been blocked
416 get :blocks_on, :display_name => unblocked_user.display_name
417 assert_response :success
418 assert_select "table#block_list", :count => 1 do
419 assert_select "tr", 2
420 assert_select "a[href='#{user_block_path(expired_block)}']", 1
425 # test the blocks_by action
427 moderator_user = create(:moderator_user)
428 second_moderator_user = create(:moderator_user)
429 normal_user = create(:user)
430 active_block = create(:user_block, :creator => moderator_user)
431 expired_block = create(:user_block, :expired, :creator => second_moderator_user)
432 revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
434 # Asking for a list of blocks with no user name should fail
435 assert_raise ActionController::UrlGenerationError do
439 # Asking for a list of blocks with a bogus user name should fail
440 get :blocks_by, :display_name => "non_existent_user"
441 assert_response :not_found
442 assert_template "user/no_such_user"
443 assert_select "h1", "The user non_existent_user does not exist"
445 # Check the list of blocks given by one moderator
446 get :blocks_by, :display_name => moderator_user.display_name
447 assert_response :success
448 assert_select "table#block_list", :count => 1 do
449 assert_select "tr", 2
450 assert_select "a[href='#{user_block_path(active_block)}']", 1
453 # Check the list of blocks given by a different moderator
454 get :blocks_by, :display_name => second_moderator_user.display_name
455 assert_response :success
456 assert_select "table#block_list", :count => 1 do
457 assert_select "tr", 3
458 assert_select "a[href='#{user_block_path(expired_block)}']", 1
459 assert_select "a[href='#{user_block_path(revoked_block)}']", 1
462 # Check the list of blocks (not) given by a normal user
463 get :blocks_by, :display_name => normal_user.display_name
464 assert_response :success
465 assert_select "table#block_list", false
466 assert_select "p", "#{normal_user.display_name} has not made any blocks yet."