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
65 assert_response :success
66 assert_select "table#block_list", :count => 1 do
68 assert_select "a[href='#{user_block_path(user_blocks(:active_block))}']", 1
69 assert_select "a[href='#{user_block_path(user_blocks(:expired_block))}']", 1
70 assert_select "a[href='#{user_block_path(user_blocks(:revoked_block))}']", 1
75 # test the show action
78 assert_response :not_found
79 assert_template "not_found"
80 assert_select "p", "Sorry, the user block with ID could not be found."
82 get :show, :id => 99999
83 assert_response :not_found
84 assert_template "not_found"
85 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
87 get :show, :id => user_blocks(:active_block)
88 assert_response :success
90 get :show, :id => user_blocks(:expired_block)
91 assert_response :success
93 get :show, :id => user_blocks(:revoked_block)
94 assert_response :success
100 get :new, :display_name => users(:normal_user).display_name
101 assert_redirected_to login_path(:referer => new_user_block_path(:display_name => users(:normal_user).display_name))
103 session[:user] = users(:public_user).id
104 cookies["_osm_username"] = users(:public_user).display_name
106 get :new, :display_name => users(:normal_user).display_name
107 assert_redirected_to user_blocks_path
108 assert_equal "You need to be a moderator to perform that action.", flash[:error]
110 session[:user] = users(:moderator_user).id
111 cookies["_osm_username"] = users(:moderator_user).display_name
113 get :new, :display_name => users(:normal_user).display_name
114 assert_response :success
115 assert_select "form#new_user_block", :count => 1 do
116 assert_select "textarea#user_block_reason", :count => 1
117 assert_select "select#user_block_period", :count => 1
118 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
119 assert_select "input#display_name[type='hidden']", :count => 1
120 assert_select "input[type='submit'][value='Create block']", :count => 1
124 assert_response :not_found
125 assert_template "user/no_such_user"
126 assert_select "h2", "The user does not exist"
128 get :new, :display_name => "non_existent_user"
129 assert_response :not_found
130 assert_template "user/no_such_user"
131 assert_select "h2", "The user non_existent_user does not exist"
135 # test the edit action
137 get :edit, :id => user_blocks(:active_block).id
138 assert_redirected_to login_path(:referer => edit_user_block_path(:id => user_blocks(:active_block).id))
140 session[:user] = users(:public_user).id
141 cookies["_osm_username"] = users(:public_user).display_name
143 get :edit, :id => user_blocks(:active_block).id
144 assert_redirected_to user_blocks_path
145 assert_equal "You need to be a moderator to perform that action.", flash[:error]
147 session[:user] = users(:moderator_user).id
148 cookies["_osm_username"] = users(:moderator_user).display_name
150 get :edit, :id => user_blocks(:active_block).id
151 assert_response :success
152 assert_select "form#edit_user_block_#{user_blocks(:active_block).id}", :count => 1 do
153 assert_select "textarea#user_block_reason", :count => 1
154 assert_select "select#user_block_period", :count => 1
155 assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
156 assert_select "input[type='submit'][value='Update block']", :count => 1
160 assert_response :not_found
161 assert_template "not_found"
162 assert_select "p", "Sorry, the user block with ID could not be found."
164 get :edit, :id => 99999
165 assert_response :not_found
166 assert_template "not_found"
167 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
171 # test the create action
174 assert_response :forbidden
176 session[:user] = users(:public_user).id
177 cookies["_osm_username"] = users(:public_user).display_name
180 assert_response :forbidden
182 session[:user] = users(:moderator_user).id
183 cookies["_osm_username"] = users(:moderator_user).display_name
185 assert_no_difference "UserBlock.count" do
187 :display_name => users(:unblocked_user).display_name,
188 :user_block_period => "99"
190 assert_redirected_to new_user_block_path(:display_name => users(:unblocked_user).display_name)
191 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
193 assert_difference "UserBlock.count", 1 do
195 :display_name => users(:unblocked_user).display_name,
196 :user_block_period => "12",
197 :user_block => { :needs_view => false, :reason => "Vandalism" }
199 assert_redirected_to user_block_path(:id => 4)
200 assert_equal "Created a block on user #{users(:unblocked_user).display_name}.", flash[:notice]
201 b = UserBlock.find(4)
202 assert_in_delta Time.now, b.created_at, 1
203 assert_in_delta Time.now, b.updated_at, 1
204 assert_in_delta Time.now + 12.hour, b.ends_at, 1
205 assert_equal false, b.needs_view
206 assert_equal "Vandalism", b.reason
207 assert_equal "markdown", b.reason_format
208 assert_equal users(:moderator_user).id, b.creator_id
211 assert_response :not_found
212 assert_template "user/no_such_user"
213 assert_select "h2", "The user does not exist"
215 post :create, :display_name => "non_existent_user"
216 assert_response :not_found
217 assert_template "user/no_such_user"
218 assert_select "h2", "The user non_existent_user does not exist"
222 # test the update action
225 assert_response :forbidden
227 session[:user] = users(:public_user).id
228 cookies["_osm_username"] = users(:public_user).display_name
231 assert_response :forbidden
233 session[:user] = users(:second_moderator_user).id
234 cookies["_osm_username"] = users(:second_moderator_user).display_name
236 assert_no_difference "UserBlock.count" do
238 :id => user_blocks(:active_block).id,
239 :user_block_period => "12",
240 :user_block => { :needs_view => true, :reason => "Vandalism" }
242 assert_redirected_to edit_user_block_path(:id => user_blocks(:active_block).id)
243 assert_equal "Only the moderator who created this block can edit it.", flash[:error]
245 session[:user] = users(:moderator_user).id
246 cookies["_osm_username"] = users(:moderator_user).display_name
248 assert_no_difference "UserBlock.count" do
250 :id => user_blocks(:active_block).id,
251 :user_block_period => "99"
253 assert_redirected_to edit_user_block_path(:id => user_blocks(:active_block).id)
254 assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
256 assert_no_difference "UserBlock.count" do
258 :id => user_blocks(:active_block).id,
259 :user_block_period => "12",
260 :user_block => { :needs_view => true, :reason => "Vandalism" }
262 assert_redirected_to user_block_path(:id => user_blocks(:active_block).id)
263 assert_equal "Block updated.", flash[:notice]
264 b = UserBlock.find(user_blocks(:active_block).id)
265 assert_in_delta Time.now, b.updated_at, 1
266 assert_equal true, b.needs_view
267 assert_equal "Vandalism", b.reason
270 assert_response :not_found
271 assert_template "not_found"
272 assert_select "p", "Sorry, the user block with ID could not be found."
274 put :update, :id => 99999
275 assert_response :not_found
276 assert_template "not_found"
277 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
281 # test the revoke action
283 get :revoke, :id => user_blocks(:active_block).id
284 assert_redirected_to login_path(:referer => revoke_user_block_path(:id => user_blocks(:active_block).id))
286 session[:user] = users(:public_user).id
287 cookies["_osm_username"] = users(:public_user).display_name
289 get :revoke, :id => user_blocks(:active_block).id
290 assert_redirected_to user_blocks_path
291 assert_equal "You need to be a moderator to perform that action.", flash[:error]
293 session[:user] = users(:moderator_user).id
294 cookies["_osm_username"] = users(:moderator_user).display_name
296 get :revoke, :id => user_blocks(:active_block).id
297 assert_response :success
298 assert_template "revoke"
299 assert_select "form", :count => 1 do
300 assert_select "input#confirm[type='checkbox']", :count => 1
301 assert_select "input[type='submit'][value='Revoke!']", :count => 1
304 post :revoke, :id => user_blocks(:active_block).id, :confirm => true
305 assert_redirected_to user_block_path(:id => user_blocks(:active_block).id)
306 b = UserBlock.find(user_blocks(:active_block).id)
307 assert_in_delta Time.now, b.ends_at, 1
310 assert_response :not_found
311 assert_template "not_found"
312 assert_select "p", "Sorry, the user block with ID could not be found."
314 get :revoke, :id => 99999
315 assert_response :not_found
316 assert_template "not_found"
317 assert_select "p", "Sorry, the user block with ID 99999 could not be found."
321 # test the blocks_on action
324 assert_response :not_found
325 assert_template "user/no_such_user"
326 assert_select "h2", "The user does not exist"
328 get :blocks_on, :display_name => "non_existent_user"
329 assert_response :not_found
330 assert_template "user/no_such_user"
331 assert_select "h2", "The user non_existent_user does not exist"
333 get :blocks_on, :display_name => users(:normal_user).display_name
334 assert_response :success
335 assert_select "table#block_list", false
336 assert_select "p", "#{users(:normal_user).display_name} has not been blocked yet."
338 get :blocks_on, :display_name => users(:blocked_user).display_name
339 assert_response :success
340 assert_select "table#block_list", :count => 1 do
341 assert_select "tr", 3
342 assert_select "a[href='#{user_block_path(user_blocks(:active_block))}']", 1
343 assert_select "a[href='#{user_block_path(user_blocks(:revoked_block))}']", 1
346 get :blocks_on, :display_name => users(:unblocked_user).display_name
347 assert_response :success
348 assert_select "table#block_list", :count => 1 do
349 assert_select "tr", 2
350 assert_select "a[href='#{user_block_path(user_blocks(:expired_block))}']", 1
355 # test the blocks_by action
358 assert_response :not_found
359 assert_template "user/no_such_user"
360 assert_select "h2", "The user does not exist"
362 get :blocks_by, :display_name => "non_existent_user"
363 assert_response :not_found
364 assert_template "user/no_such_user"
365 assert_select "h2", "The user non_existent_user does not exist"
367 get :blocks_by, :display_name => users(:moderator_user).display_name
368 assert_response :success
369 assert_select "table#block_list", :count => 1 do
370 assert_select "tr", 2
371 assert_select "a[href='#{user_block_path(user_blocks(:active_block))}']", 1
374 get :blocks_by, :display_name => users(:second_moderator_user).display_name
375 assert_response :success
376 assert_select "table#block_list", :count => 1 do
377 assert_select "tr", 3
378 assert_select "a[href='#{user_block_path(user_blocks(:expired_block))}']", 1
379 assert_select "a[href='#{user_block_path(user_blocks(:revoked_block))}']", 1
382 get :blocks_by, :display_name => users(:normal_user).display_name
383 assert_response :success
384 assert_select "table#block_list", false
385 assert_select "p", "#{users(:normal_user).display_name} has not made any blocks yet."