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
79 assert_raise ActionController::UrlGenerationError do
83 # Viewing a block should fail when a bogus ID is given
84 get :show, :id => 99999
85 assert_response :not_found
86 assert_template "not_found"
87 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
89 # Viewing an expired block should work
90 get :show, :id => user_blocks(:expired_block)
91 assert_response :success
93 # Viewing a revoked block should work
94 get :show, :id => user_blocks(:revoked_block)
95 assert_response :success
97 # Viewing an active block should work, but shouldn't mark it as seen
98 get :show, :id => user_blocks(:active_block)
99 assert_response :success
100 assert_equal true, UserBlock.find(user_blocks(:active_block).id).needs_view
102 # Login as the blocked user
103 session[:user] = users(:blocked_user).id
105 # Now viewing it should mark it as seen
106 get :show, :id => user_blocks(:active_block)
107 assert_response :success
108 assert_equal false, UserBlock.find(user_blocks(:active_block).id).needs_view
112 # test the new action
114 # Check that the block creation page requires us to login
115 get :new, :display_name => users(:normal_user).display_name
116 assert_redirected_to login_path(:referer => new_user_block_path(:display_name => users(:normal_user).display_name))
118 # Login as a normal user
119 session[:user] = users(:public_user).id
121 # Check that normal users can't load the block creation page
122 get :new, :display_name => users(:normal_user).display_name
123 assert_redirected_to user_blocks_path
124 assert_equal "You need to be a moderator to perform that action.", flash[:error]
126 # Login as a moderator
127 session[:user] = users(:moderator_user).id
129 # Check that the block creation page loads for moderators
130 get :new, :display_name => users(:normal_user).display_name
131 assert_response :success
132 assert_select "form#new_user_block", :count => 1 do
133 assert_select "textarea#user_block_reason", :count => 1
134 assert_select "select#user_block_period", :count => 1
135 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
136 assert_select "input#display_name[type='hidden']", :count => 1
137 assert_select "input[type='submit'][value='Create block']", :count => 1
140 # We should get an error if no user is specified
142 assert_response :not_found
143 assert_template "user/no_such_user"
144 assert_select "h1", "The user does not exist"
146 # We should get an error if the user doesn't exist
147 get :new, :display_name => "non_existent_user"
148 assert_response :not_found
149 assert_template "user/no_such_user"
150 assert_select "h1", "The user non_existent_user does not exist"
154 # test the edit action
156 # Check that the block edit page requires us to login
157 get :edit, :id => user_blocks(:active_block).id
158 assert_redirected_to login_path(:referer => edit_user_block_path(:id => user_blocks(:active_block).id))
160 # Login as a normal user
161 session[:user] = users(:public_user).id
163 # Check that normal users can't load the block edit page
164 get :edit, :id => user_blocks(:active_block).id
165 assert_redirected_to user_blocks_path
166 assert_equal "You need to be a moderator to perform that action.", flash[:error]
168 # Login as a moderator
169 session[:user] = users(:moderator_user).id
171 # Check that the block edit page loads for moderators
172 get :edit, :id => user_blocks(:active_block).id
173 assert_response :success
174 assert_select "form#edit_user_block_#{user_blocks(:active_block).id}", :count => 1 do
175 assert_select "textarea#user_block_reason", :count => 1
176 assert_select "select#user_block_period", :count => 1
177 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
178 assert_select "input[type='submit'][value='Update block']", :count => 1
181 # We should get an error if no user is specified
182 assert_raise ActionController::UrlGenerationError do
186 # We should get an error if the user doesn't exist
187 get :edit, :id => 99999
188 assert_response :not_found
189 assert_template "not_found"
190 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
194 # test the create action
196 # Not logged in yet, so creating a block should fail
198 assert_response :forbidden
200 # Login as a normal user
201 session[:user] = users(:public_user).id
203 # Check that normal users can't create blocks
205 assert_response :forbidden
207 # Login as a moderator
208 session[:user] = users(:moderator_user).id
210 # A bogus block period should result in an error
211 assert_no_difference "UserBlock.count" do
213 :display_name => users(:unblocked_user).display_name,
214 :user_block_period => "99"
216 assert_redirected_to new_user_block_path(:display_name => users(:unblocked_user).display_name)
217 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
219 # Check that creating a block works
220 assert_difference "UserBlock.count", 1 do
222 :display_name => users(:unblocked_user).display_name,
223 :user_block_period => "12",
224 :user_block => { :needs_view => false, :reason => "Vandalism" }
226 id = UserBlock.order(:id).ids.last
227 assert_redirected_to user_block_path(:id => id)
228 assert_equal "Created a block on user #{users(:unblocked_user).display_name}.", flash[:notice]
229 b = UserBlock.find(id)
230 assert_in_delta Time.now, b.created_at, 1
231 assert_in_delta Time.now, b.updated_at, 1
232 assert_in_delta Time.now + 12.hour, b.ends_at, 1
233 assert_equal false, b.needs_view
234 assert_equal "Vandalism", b.reason
235 assert_equal "markdown", b.reason_format
236 assert_equal users(:moderator_user).id, b.creator_id
238 # We should get an error if no user is specified
240 assert_response :not_found
241 assert_template "user/no_such_user"
242 assert_select "h1", "The user does not exist"
244 # We should get an error if the user doesn't exist
245 post :create, :display_name => "non_existent_user"
246 assert_response :not_found
247 assert_template "user/no_such_user"
248 assert_select "h1", "The user non_existent_user does not exist"
252 # test the update action
254 # Not logged in yet, so updating a block should fail
255 put :update, :id => user_blocks(:active_block).id
256 assert_response :forbidden
258 # Login as a normal user
259 session[:user] = users(:public_user).id
261 # Check that normal users can't update blocks
262 put :update, :id => user_blocks(:active_block).id
263 assert_response :forbidden
265 # Login as the wrong moderator
266 session[:user] = users(:second_moderator_user).id
268 # Check that only the person who created a block can update it
269 assert_no_difference "UserBlock.count" do
271 :id => user_blocks(:active_block).id,
272 :user_block_period => "12",
273 :user_block => { :needs_view => true, :reason => "Vandalism" }
275 assert_redirected_to edit_user_block_path(:id => user_blocks(:active_block).id)
276 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
278 # Login as the correct moderator
279 session[:user] = users(:moderator_user).id
281 # A bogus block period should result in an error
282 assert_no_difference "UserBlock.count" do
284 :id => user_blocks(:active_block).id,
285 :user_block_period => "99"
287 assert_redirected_to edit_user_block_path(:id => user_blocks(:active_block).id)
288 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
290 # Check that updating a block works
291 assert_no_difference "UserBlock.count" do
293 :id => user_blocks(:active_block).id,
294 :user_block_period => "12",
295 :user_block => { :needs_view => true, :reason => "Vandalism" }
297 assert_redirected_to user_block_path(:id => user_blocks(:active_block).id)
298 assert_equal "Block updated.", flash[:notice]
299 b = UserBlock.find(user_blocks(:active_block).id)
300 assert_in_delta Time.now, b.updated_at, 1
301 assert_equal true, b.needs_view
302 assert_equal "Vandalism", b.reason
304 # We should get an error if no block ID is specified
305 assert_raise ActionController::UrlGenerationError do
309 # We should get an error if the block doesn't exist
310 put :update, :id => 99999
311 assert_response :not_found
312 assert_template "not_found"
313 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
317 # test the revoke action
319 # Check that the block revoke page requires us to login
320 get :revoke, :id => user_blocks(:active_block).id
321 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => user_blocks(:active_block).id))
323 # Login as a normal user
324 session[:user] = users(:public_user).id
326 # Check that normal users can't load the block revoke page
327 get :revoke, :id => user_blocks(:active_block).id
328 assert_redirected_to user_blocks_path
329 assert_equal "You need to be a moderator to perform that action.", flash[:error]
331 # Login as a moderator
332 session[:user] = users(:moderator_user).id
334 # Check that the block revoke page loads for moderators
335 get :revoke, :id => user_blocks(:active_block).id
336 assert_response :success
337 assert_template "revoke"
338 assert_select "form", :count => 1 do
339 assert_select "input#confirm[type='checkbox']", :count => 1
340 assert_select "input[type='submit'][value='Revoke!']", :count => 1
343 # Check that revoking a block works
344 post :revoke, :id => user_blocks(:active_block).id, :confirm => true
345 assert_redirected_to user_block_path(:id => user_blocks(:active_block).id)
346 b = UserBlock.find(user_blocks(:active_block).id)
347 assert_in_delta Time.now, b.ends_at, 1
349 # We should get an error if no block ID is specified
350 assert_raise ActionController::UrlGenerationError do
354 # We should get an error if the block doesn't exist
355 get :revoke, :id => 99999
356 assert_response :not_found
357 assert_template "not_found"
358 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
362 # test the blocks_on action
364 # Asking for a list of blocks with no user name should fail
365 assert_raise ActionController::UrlGenerationError do
369 # Asking for a list of blocks with a bogus user name should fail
370 get :blocks_on, :display_name => "non_existent_user"
371 assert_response :not_found
372 assert_template "user/no_such_user"
373 assert_select "h1", "The user non_existent_user does not exist"
375 # Check the list of blocks for a user that has never been blocked
376 get :blocks_on, :display_name => users(:normal_user).display_name
377 assert_response :success
378 assert_select "table#block_list", false
379 assert_select "p", "#{users(:normal_user).display_name} has not been blocked yet."
381 # Check the list of blocks for a user that is currently blocked
382 get :blocks_on, :display_name => users(:blocked_user).display_name
383 assert_response :success
384 assert_select "table#block_list", :count => 1 do
385 assert_select "tr", 3
386 assert_select "a[href='#{user_block_path(user_blocks(:active_block))}']", 1
387 assert_select "a[href='#{user_block_path(user_blocks(:revoked_block))}']", 1
390 # Check the list of blocks for a user that has previously been blocked
391 get :blocks_on, :display_name => users(:unblocked_user).display_name
392 assert_response :success
393 assert_select "table#block_list", :count => 1 do
394 assert_select "tr", 2
395 assert_select "a[href='#{user_block_path(user_blocks(:expired_block))}']", 1
400 # test the blocks_by action
402 # Asking for a list of blocks with no user name should fail
403 assert_raise ActionController::UrlGenerationError do
407 # Asking for a list of blocks with a bogus user name should fail
408 get :blocks_by, :display_name => "non_existent_user"
409 assert_response :not_found
410 assert_template "user/no_such_user"
411 assert_select "h1", "The user non_existent_user does not exist"
413 # Check the list of blocks given by one moderator
414 get :blocks_by, :display_name => users(:moderator_user).display_name
415 assert_response :success
416 assert_select "table#block_list", :count => 1 do
417 assert_select "tr", 2
418 assert_select "a[href='#{user_block_path(user_blocks(:active_block))}']", 1
421 # Check the list of blocks given by a different moderator
422 get :blocks_by, :display_name => users(:second_moderator_user).display_name
423 assert_response :success
424 assert_select "table#block_list", :count => 1 do
425 assert_select "tr", 3
426 assert_select "a[href='#{user_block_path(user_blocks(:expired_block))}']", 1
427 assert_select "a[href='#{user_block_path(user_blocks(:revoked_block))}']", 1
430 # Check the list of blocks (not) given by a normal user
431 get :blocks_by, :display_name => users(:normal_user).display_name
432 assert_response :success
433 assert_select "table#block_list", false
434 assert_select "p", "#{users(:normal_user).display_name} has not made any blocks yet."