1 require File.dirname(__FILE__) + '/../test_helper'
3 class UserBlocksControllerTest < ActionController::TestCase
4 fixtures :users, :user_roles, :user_blocks
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 # The list of blocks should load
66 assert_response :success
67 assert_select "table#block_list", :count => 1 do
69 assert_select "a[href='#{user_block_path(user_blocks(:active_block))}']", 1
70 assert_select "a[href='#{user_block_path(user_blocks(:expired_block))}']", 1
71 assert_select "a[href='#{user_block_path(user_blocks(:revoked_block))}']", 1
76 # test the show action
78 # Viewing a block should fail when no ID is given
80 assert_response :not_found
81 assert_template "not_found"
82 assert_select "p", "Sorry, the user block with ID could not be found."
84 # Viewing a block should fail when a bogus ID is given
85 get :show, :id => 99999
86 assert_response :not_found
87 assert_template "not_found"
88 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
90 # Viewing an expired block should work
91 get :show, :id => user_blocks(:expired_block)
92 assert_response :success
94 # Viewing a revoked block should work
95 get :show, :id => user_blocks(:revoked_block)
96 assert_response :success
98 # Viewing an active block should work, but shouldn't mark it as seen
99 get :show, :id => user_blocks(:active_block)
100 assert_response :success
101 assert_equal true, UserBlock.find(user_blocks(:active_block).id).needs_view
103 # Login as the blocked user
104 session[:user] = users(:blocked_user).id
105 cookies["_osm_username"] = users(:blocked_user).display_name
107 # Now viewing it should mark it as seen
108 get :show, :id => user_blocks(:active_block)
109 assert_response :success
110 assert_equal false, UserBlock.find(user_blocks(:active_block).id).needs_view
114 # test the new action
116 # Check that the block creation page requires us to login
117 get :new, :display_name => users(:normal_user).display_name
118 assert_redirected_to login_path(:referer => new_user_block_path(:display_name => users(:normal_user).display_name))
120 # Login as a normal user
121 session[:user] = users(:public_user).id
122 cookies["_osm_username"] = users(:public_user).display_name
124 # Check that normal users can't load the block creation page
125 get :new, :display_name => users(:normal_user).display_name
126 assert_redirected_to user_blocks_path
127 assert_equal "You need to be a moderator to perform that action.", flash[:error]
129 # Login as a moderator
130 session[:user] = users(:moderator_user).id
131 cookies["_osm_username"] = users(:moderator_user).display_name
133 # Check that the block creation page loads for moderators
134 get :new, :display_name => users(:normal_user).display_name
135 assert_response :success
136 assert_select "form#new_user_block", :count => 1 do
137 assert_select "textarea#user_block_reason", :count => 1
138 assert_select "select#user_block_period", :count => 1
139 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
140 assert_select "input#display_name[type='hidden']", :count => 1
141 assert_select "input[type='submit'][value='Create block']", :count => 1
144 # We should get an error if no user is specified
146 assert_response :not_found
147 assert_template "user/no_such_user"
148 assert_select "h2", "The user does not exist"
150 # We should get an error if the user doesn't exist
151 get :new, :display_name => "non_existent_user"
152 assert_response :not_found
153 assert_template "user/no_such_user"
154 assert_select "h2", "The user non_existent_user does not exist"
158 # test the edit action
160 # Check that the block edit page requires us to login
161 get :edit, :id => user_blocks(:active_block).id
162 assert_redirected_to login_path(:referer => edit_user_block_path(:id => user_blocks(:active_block).id))
164 # Login as a normal user
165 session[:user] = users(:public_user).id
166 cookies["_osm_username"] = users(:public_user).display_name
168 # Check that normal users can't load the block edit page
169 get :edit, :id => user_blocks(:active_block).id
170 assert_redirected_to user_blocks_path
171 assert_equal "You need to be a moderator to perform that action.", flash[:error]
173 # Login as a moderator
174 session[:user] = users(:moderator_user).id
175 cookies["_osm_username"] = users(:moderator_user).display_name
177 # Check that the block edit page loads for moderators
178 get :edit, :id => user_blocks(:active_block).id
179 assert_response :success
180 assert_select "form#edit_user_block_#{user_blocks(:active_block).id}", :count => 1 do
181 assert_select "textarea#user_block_reason", :count => 1
182 assert_select "select#user_block_period", :count => 1
183 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
184 assert_select "input[type='submit'][value='Update block']", :count => 1
187 # We should get an error if no user is specified
189 assert_response :not_found
190 assert_template "not_found"
191 assert_select "p", "Sorry, the user block with ID could not be found."
193 # We should get an error if the user doesn't exist
194 get :edit, :id => 99999
195 assert_response :not_found
196 assert_template "not_found"
197 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
201 # test the create action
203 # Not logged in yet, so creating a block should fail
205 assert_response :forbidden
207 # Login as a normal user
208 session[:user] = users(:public_user).id
209 cookies["_osm_username"] = users(:public_user).display_name
211 # Check that normal users can't create blocks
213 assert_response :forbidden
215 # Login as a moderator
216 session[:user] = users(:moderator_user).id
217 cookies["_osm_username"] = users(:moderator_user).display_name
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 assert_redirected_to user_block_path(:id => 4)
236 assert_equal "Created a block on user #{users(:unblocked_user).display_name}.", flash[:notice]
237 b = UserBlock.find(4)
238 assert_in_delta Time.now, b.created_at, 1
239 assert_in_delta Time.now, b.updated_at, 1
240 assert_in_delta Time.now + 12.hour, b.ends_at, 1
241 assert_equal false, b.needs_view
242 assert_equal "Vandalism", b.reason
243 assert_equal "markdown", b.reason_format
244 assert_equal users(:moderator_user).id, b.creator_id
246 # We should get an error if no user is specified
248 assert_response :not_found
249 assert_template "user/no_such_user"
250 assert_select "h2", "The user does not exist"
252 # We should get an error if the user doesn't exist
253 post :create, :display_name => "non_existent_user"
254 assert_response :not_found
255 assert_template "user/no_such_user"
256 assert_select "h2", "The user non_existent_user does not exist"
260 # test the update action
262 # Not logged in yet, so updating a block should fail
264 assert_response :forbidden
266 # Login as a normal user
267 session[:user] = users(:public_user).id
268 cookies["_osm_username"] = users(:public_user).display_name
270 # Check that normal users can't update blocks
272 assert_response :forbidden
274 # Login as the wrong moderator
275 session[:user] = users(:second_moderator_user).id
276 cookies["_osm_username"] = users(:second_moderator_user).display_name
278 # Check that only the person who created a block can update it
279 assert_no_difference "UserBlock.count" do
281 :id => user_blocks(:active_block).id,
282 :user_block_period => "12",
283 :user_block => { :needs_view => true, :reason => "Vandalism" }
285 assert_redirected_to edit_user_block_path(:id => user_blocks(:active_block).id)
286 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
288 # Login as the correct moderator
289 session[:user] = users(:moderator_user).id
290 cookies["_osm_username"] = users(:moderator_user).display_name
292 # A bogus block period should result in an error
293 assert_no_difference "UserBlock.count" do
295 :id => user_blocks(:active_block).id,
296 :user_block_period => "99"
298 assert_redirected_to edit_user_block_path(:id => user_blocks(: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 => user_blocks(:active_block).id,
305 :user_block_period => "12",
306 :user_block => { :needs_view => true, :reason => "Vandalism" }
308 assert_redirected_to user_block_path(:id => user_blocks(:active_block).id)
309 assert_equal "Block updated.", flash[:notice]
310 b = UserBlock.find(user_blocks(: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
317 assert_response :not_found
318 assert_template "not_found"
319 assert_select "p", "Sorry, the user block with ID could not be found."
321 # We should get an error if the block doesn't exist
322 put :update, :id => 99999
323 assert_response :not_found
324 assert_template "not_found"
325 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
329 # test the revoke action
331 # Check that the block revoke page requires us to login
332 get :revoke, :id => user_blocks(:active_block).id
333 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => user_blocks(:active_block).id))
335 # Login as a normal user
336 session[:user] = users(:public_user).id
337 cookies["_osm_username"] = users(:public_user).display_name
339 # Check that normal users can't load the block revoke page
340 get :revoke, :id => user_blocks(: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
346 cookies["_osm_username"] = users(:moderator_user).display_name
348 # Check that the block revoke page loads for moderators
349 get :revoke, :id => user_blocks(:active_block).id
350 assert_response :success
351 assert_template "revoke"
352 assert_select "form", :count => 1 do
353 assert_select "input#confirm[type='checkbox']", :count => 1
354 assert_select "input[type='submit'][value='Revoke!']", :count => 1
357 # Check that revoking a block works
358 post :revoke, :id => user_blocks(:active_block).id, :confirm => true
359 assert_redirected_to user_block_path(:id => user_blocks(:active_block).id)
360 b = UserBlock.find(user_blocks(:active_block).id)
361 assert_in_delta Time.now, b.ends_at, 1
363 # We should get an error if no block ID is specified
365 assert_response :not_found
366 assert_template "not_found"
367 assert_select "p", "Sorry, the user block with ID could not be found."
369 # We should get an error if the block doesn't exist
370 get :revoke, :id => 99999
371 assert_response :not_found
372 assert_template "not_found"
373 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
377 # test the blocks_on action
379 # Asking for a list of blocks with no user name should fail
381 assert_response :not_found
382 assert_template "user/no_such_user"
383 assert_select "h2", "The user does not exist"
385 # Asking for a list of blocks with a bogus user name should fail
386 get :blocks_on, :display_name => "non_existent_user"
387 assert_response :not_found
388 assert_template "user/no_such_user"
389 assert_select "h2", "The user non_existent_user does not exist"
391 # Check the list of blocks for a user that has never been blocked
392 get :blocks_on, :display_name => users(:normal_user).display_name
393 assert_response :success
394 assert_select "table#block_list", false
395 assert_select "p", "#{users(:normal_user).display_name} has not been blocked yet."
397 # Check the list of blocks for a user that is currently blocked
398 get :blocks_on, :display_name => users(:blocked_user).display_name
399 assert_response :success
400 assert_select "table#block_list", :count => 1 do
401 assert_select "tr", 3
402 assert_select "a[href='#{user_block_path(user_blocks(:active_block))}']", 1
403 assert_select "a[href='#{user_block_path(user_blocks(:revoked_block))}']", 1
406 # Check the list of blocks for a user that has previously been blocked
407 get :blocks_on, :display_name => users(:unblocked_user).display_name
408 assert_response :success
409 assert_select "table#block_list", :count => 1 do
410 assert_select "tr", 2
411 assert_select "a[href='#{user_block_path(user_blocks(:expired_block))}']", 1
416 # test the blocks_by action
418 # Asking for a list of blocks with no user name should fail
420 assert_response :not_found
421 assert_template "user/no_such_user"
422 assert_select "h2", "The user does not exist"
424 # Asking for a list of blocks with a bogus user name should fail
425 get :blocks_by, :display_name => "non_existent_user"
426 assert_response :not_found
427 assert_template "user/no_such_user"
428 assert_select "h2", "The user non_existent_user does not exist"
430 # Check the list of blocks given by one moderator
431 get :blocks_by, :display_name => users(:moderator_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(user_blocks(:active_block))}']", 1
438 # Check the list of blocks given by a different moderator
439 get :blocks_by, :display_name => users(:second_moderator_user).display_name
440 assert_response :success
441 assert_select "table#block_list", :count => 1 do
442 assert_select "tr", 3
443 assert_select "a[href='#{user_block_path(user_blocks(:expired_block))}']", 1
444 assert_select "a[href='#{user_block_path(user_blocks(:revoked_block))}']", 1
447 # Check the list of blocks (not) given by a normal user
448 get :blocks_by, :display_name => users(:normal_user).display_name
449 assert_response :success
450 assert_select "table#block_list", false
451 assert_select "p", "#{users(:normal_user).display_name} has not made any blocks yet."