]> git.openstreetmap.org Git - rails.git/blob - test/functional/user_blocks_controller_test.rb
Add a routing test for /id
[rails.git] / test / functional / user_blocks_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class UserBlocksControllerTest < ActionController::TestCase
4   fixtures :users, :user_roles, :user_blocks
5
6   ##
7   # test all routes which lead to this controller
8   def test_routes
9     assert_routing(
10       { :path => "/blocks/new/username", :method => :get },
11       { :controller => "user_blocks", :action => "new", :display_name => "username" }
12     )
13
14     assert_routing(
15       { :path => "/user_blocks", :method => :get },
16       { :controller => "user_blocks", :action => "index" }
17     )
18     assert_routing(
19       { :path => "/user_blocks/new", :method => :get },
20       { :controller => "user_blocks", :action => "new" }
21     )
22     assert_routing(
23       { :path => "/user_blocks", :method => :post },
24       { :controller => "user_blocks", :action => "create" }
25     )
26     assert_routing(
27       { :path => "/user_blocks/1", :method => :get },
28       { :controller => "user_blocks", :action => "show", :id => "1" }
29     )
30     assert_routing(
31       { :path => "/user_blocks/1/edit", :method => :get },
32       { :controller => "user_blocks", :action => "edit", :id => "1" }
33     )
34     assert_routing(
35       { :path => "/user_blocks/1", :method => :put },
36       { :controller => "user_blocks", :action => "update", :id => "1" }
37     )
38     assert_routing(
39       { :path => "/user_blocks/1", :method => :delete },
40       { :controller => "user_blocks", :action => "destroy", :id => "1" }
41     )
42     assert_routing(
43       { :path => "/blocks/1/revoke", :method => :get },
44       { :controller => "user_blocks", :action => "revoke", :id => "1" }
45     )
46     assert_routing(
47       { :path => "/blocks/1/revoke", :method => :post },
48       { :controller => "user_blocks", :action => "revoke", :id => "1" }
49     )
50
51     assert_routing(
52       { :path => "/user/username/blocks", :method => :get },
53       { :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
54     )
55     assert_routing(
56       { :path => "/user/username/blocks_by", :method => :get },
57       { :controller => "user_blocks", :action => "blocks_by", :display_name => "username" }
58     )
59   end
60
61   ##
62   # test the index action
63   def test_index
64     # The list of blocks should load
65     get :index
66     assert_response :success
67     assert_select "table#block_list", :count => 1 do
68       assert_select "tr", 4
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
72     end
73   end
74
75   ##
76   # test the show action
77   def test_show
78     # Viewing a block should fail when no ID is given
79     assert_raise ActionController::RoutingError do
80       get :show
81     end
82
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."
88
89     # Viewing an expired block should work
90     get :show, :id => user_blocks(:expired_block)
91     assert_response :success
92
93     # Viewing a revoked block should work
94     get :show, :id => user_blocks(:revoked_block)
95     assert_response :success
96
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
101
102     # Login as the blocked user
103     session[:user] = users(:blocked_user).id
104     cookies["_osm_username"] = users(:blocked_user).display_name
105
106     # Now viewing it should mark it as seen
107     get :show, :id => user_blocks(:active_block)
108     assert_response :success
109     assert_equal false, UserBlock.find(user_blocks(:active_block).id).needs_view
110   end
111
112   ##
113   # test the new action
114   def test_new
115     # Check that the block creation page requires us to login
116     get :new, :display_name => users(:normal_user).display_name
117     assert_redirected_to login_path(:referer => new_user_block_path(:display_name => users(:normal_user).display_name))
118
119     # Login as a normal user
120     session[:user] = users(:public_user).id
121     cookies["_osm_username"] = users(:public_user).display_name
122
123     # Check that normal users can't load the block creation page
124     get :new, :display_name => users(:normal_user).display_name
125     assert_redirected_to user_blocks_path
126     assert_equal "You need to be a moderator to perform that action.", flash[:error]
127
128     # Login as a moderator
129     session[:user] = users(:moderator_user).id
130     cookies["_osm_username"] = users(:moderator_user).display_name
131
132     # Check that the block creation page loads for moderators
133     get :new, :display_name => users(:normal_user).display_name
134     assert_response :success
135     assert_select "form#new_user_block", :count => 1 do
136       assert_select "textarea#user_block_reason", :count => 1
137       assert_select "select#user_block_period", :count => 1
138       assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
139       assert_select "input#display_name[type='hidden']", :count => 1
140       assert_select "input[type='submit'][value='Create block']", :count => 1
141     end
142
143     # We should get an error if no user is specified
144     get :new
145     assert_response :not_found
146     assert_template "user/no_such_user"
147     assert_select "h2", "The user  does not exist"
148
149     # We should get an error if the user doesn't exist
150     get :new, :display_name => "non_existent_user"
151     assert_response :not_found
152     assert_template "user/no_such_user"
153     assert_select "h2", "The user non_existent_user does not exist"
154   end
155
156   ##
157   # test the edit action
158   def test_edit
159     # Check that the block edit page requires us to login
160     get :edit, :id => user_blocks(:active_block).id
161     assert_redirected_to login_path(:referer => edit_user_block_path(:id => user_blocks(:active_block).id))
162
163     # Login as a normal user
164     session[:user] = users(:public_user).id
165     cookies["_osm_username"] = users(:public_user).display_name
166
167     # Check that normal users can't load the block edit page
168     get :edit, :id => user_blocks(:active_block).id
169     assert_redirected_to user_blocks_path
170     assert_equal "You need to be a moderator to perform that action.", flash[:error]
171
172     # Login as a moderator
173     session[:user] = users(:moderator_user).id
174     cookies["_osm_username"] = users(:moderator_user).display_name
175
176     # Check that the block edit page loads for moderators
177     get :edit, :id => user_blocks(:active_block).id
178     assert_response :success
179     assert_select "form#edit_user_block_#{user_blocks(:active_block).id}", :count => 1 do
180       assert_select "textarea#user_block_reason", :count => 1
181       assert_select "select#user_block_period", :count => 1
182       assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
183       assert_select "input[type='submit'][value='Update block']", :count => 1
184     end
185
186     # We should get an error if no user is specified
187     assert_raise ActionController::RoutingError do
188       get :edit
189     end
190
191     # We should get an error if the user doesn't exist
192     get :edit, :id => 99999
193     assert_response :not_found
194     assert_template "not_found"
195     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
196   end
197
198   ##
199   # test the create action
200   def test_create
201     # Not logged in yet, so creating a block should fail
202     post :create
203     assert_response :forbidden
204
205     # Login as a normal user
206     session[:user] = users(:public_user).id
207     cookies["_osm_username"] = users(:public_user).display_name
208
209     # Check that normal users can't create blocks
210     post :create
211     assert_response :forbidden
212
213     # Login as a moderator
214     session[:user] = users(:moderator_user).id
215     cookies["_osm_username"] = users(:moderator_user).display_name
216
217     # A bogus block period should result in an error
218     assert_no_difference "UserBlock.count" do
219       post :create,
220         :display_name => users(:unblocked_user).display_name,
221         :user_block_period => "99"
222     end
223     assert_redirected_to new_user_block_path(:display_name => users(:unblocked_user).display_name)
224     assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
225
226     # Check that creating a block works
227     assert_difference "UserBlock.count", 1 do
228       post :create,
229         :display_name => users(:unblocked_user).display_name,
230         :user_block_period => "12",
231         :user_block => { :needs_view => false, :reason => "Vandalism" }
232     end
233     assert_redirected_to user_block_path(:id => 4)
234     assert_equal "Created a block on user #{users(:unblocked_user).display_name}.", flash[:notice]
235     b = UserBlock.find(4)
236     assert_in_delta Time.now, b.created_at, 1
237     assert_in_delta Time.now, b.updated_at, 1
238     assert_in_delta Time.now + 12.hour, b.ends_at, 1
239     assert_equal false, b.needs_view
240     assert_equal "Vandalism", b.reason
241     assert_equal "markdown", b.reason_format
242     assert_equal users(:moderator_user).id, b.creator_id
243
244     # We should get an error if no user is specified
245     post :create
246     assert_response :not_found
247     assert_template "user/no_such_user"
248     assert_select "h2", "The user  does not exist"
249
250     # We should get an error if the user doesn't exist
251     post :create, :display_name => "non_existent_user"
252     assert_response :not_found
253     assert_template "user/no_such_user"
254     assert_select "h2", "The user non_existent_user does not exist"
255   end
256
257   ##
258   # test the update action
259   def test_update
260     # Not logged in yet, so updating a block should fail
261     put :update, :id => user_blocks(:active_block).id
262     assert_response :forbidden
263
264     # Login as a normal user
265     session[:user] = users(:public_user).id
266     cookies["_osm_username"] = users(:public_user).display_name
267
268     # Check that normal users can't update blocks
269     put :update, :id => user_blocks(:active_block).id
270     assert_response :forbidden
271
272     # Login as the wrong moderator
273     session[:user] = users(:second_moderator_user).id
274     cookies["_osm_username"] = users(:second_moderator_user).display_name
275
276     # Check that only the person who created a block can update it
277     assert_no_difference "UserBlock.count" do
278       put :update,
279         :id => user_blocks(:active_block).id,
280         :user_block_period => "12",
281         :user_block => { :needs_view => true, :reason => "Vandalism" }
282     end
283     assert_redirected_to edit_user_block_path(:id => user_blocks(:active_block).id)
284     assert_equal "Only the moderator who created this block can edit it.", flash[:error]
285
286     # Login as the correct moderator
287     session[:user] = users(:moderator_user).id
288     cookies["_osm_username"] = users(:moderator_user).display_name
289
290     # A bogus block period should result in an error
291     assert_no_difference "UserBlock.count" do
292       put :update,
293         :id => user_blocks(:active_block).id,
294         :user_block_period => "99"
295     end
296     assert_redirected_to edit_user_block_path(:id => user_blocks(:active_block).id)
297     assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
298
299     # Check that updating a block works
300     assert_no_difference "UserBlock.count" do
301       put :update,
302         :id => user_blocks(:active_block).id,
303         :user_block_period => "12",
304         :user_block => { :needs_view => true, :reason => "Vandalism" }
305     end
306     assert_redirected_to user_block_path(:id => user_blocks(:active_block).id)
307     assert_equal "Block updated.", flash[:notice]
308     b = UserBlock.find(user_blocks(:active_block).id)
309     assert_in_delta Time.now, b.updated_at, 1
310     assert_equal true, b.needs_view
311     assert_equal "Vandalism", b.reason
312
313     # We should get an error if no block ID is specified
314     assert_raise ActionController::RoutingError do
315       put :update
316     end
317
318     # We should get an error if the block doesn't exist
319     put :update, :id => 99999
320     assert_response :not_found
321     assert_template "not_found"
322     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
323   end
324
325   ##
326   # test the revoke action
327   def test_revoke
328     # Check that the block revoke page requires us to login
329     get :revoke, :id => user_blocks(:active_block).id
330     assert_redirected_to login_path(:referer => revoke_user_block_path(:id => user_blocks(:active_block).id))
331
332     # Login as a normal user
333     session[:user] = users(:public_user).id
334     cookies["_osm_username"] = users(:public_user).display_name
335
336     # Check that normal users can't load the block revoke page
337     get :revoke, :id => user_blocks(:active_block).id
338     assert_redirected_to user_blocks_path
339     assert_equal "You need to be a moderator to perform that action.", flash[:error]
340
341     # Login as a moderator
342     session[:user] = users(:moderator_user).id
343     cookies["_osm_username"] = users(:moderator_user).display_name
344
345     # Check that the block revoke page loads for moderators
346     get :revoke, :id => user_blocks(:active_block).id
347     assert_response :success
348     assert_template "revoke"
349     assert_select "form", :count => 1 do
350       assert_select "input#confirm[type='checkbox']", :count => 1
351       assert_select "input[type='submit'][value='Revoke!']", :count => 1
352     end
353
354     # Check that revoking a block works
355     post :revoke, :id => user_blocks(:active_block).id, :confirm => true
356     assert_redirected_to user_block_path(:id => user_blocks(:active_block).id)
357     b = UserBlock.find(user_blocks(:active_block).id)
358     assert_in_delta Time.now, b.ends_at, 1
359
360     # We should get an error if no block ID is specified
361     assert_raise ActionController::RoutingError do
362       get :revoke
363     end
364
365     # We should get an error if the block doesn't exist
366     get :revoke, :id => 99999
367     assert_response :not_found
368     assert_template "not_found"
369     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
370   end
371
372   ##
373   # test the blocks_on action
374   def test_blocks_on
375     # Asking for a list of blocks with no user name should fail
376     assert_raise ActionController::RoutingError do
377       get :blocks_on
378     end
379
380     # Asking for a list of blocks with a bogus user name should fail
381     get :blocks_on, :display_name => "non_existent_user"
382     assert_response :not_found
383     assert_template "user/no_such_user"
384     assert_select "h2", "The user non_existent_user does not exist"
385
386     # Check the list of blocks for a user that has never been blocked
387     get :blocks_on, :display_name => users(:normal_user).display_name
388     assert_response :success
389     assert_select "table#block_list", false
390     assert_select "p", "#{users(:normal_user).display_name} has not been blocked yet."
391
392     # Check the list of blocks for a user that is currently blocked
393     get :blocks_on, :display_name => users(:blocked_user).display_name
394     assert_response :success
395     assert_select "table#block_list", :count => 1 do
396       assert_select "tr", 3
397       assert_select "a[href='#{user_block_path(user_blocks(:active_block))}']", 1
398       assert_select "a[href='#{user_block_path(user_blocks(:revoked_block))}']", 1
399     end
400
401     # Check the list of blocks for a user that has previously been blocked
402     get :blocks_on, :display_name => users(:unblocked_user).display_name
403     assert_response :success
404     assert_select "table#block_list", :count => 1 do
405       assert_select "tr", 2
406       assert_select "a[href='#{user_block_path(user_blocks(:expired_block))}']", 1
407     end
408   end
409
410   ##
411   # test the blocks_by action
412   def test_blocks_by
413     # Asking for a list of blocks with no user name should fail
414     assert_raise ActionController::RoutingError do
415       get :blocks_by
416     end
417
418     # Asking for a list of blocks with a bogus user name should fail
419     get :blocks_by, :display_name => "non_existent_user"
420     assert_response :not_found
421     assert_template "user/no_such_user"
422     assert_select "h2", "The user non_existent_user does not exist"
423
424     # Check the list of blocks given by one moderator
425     get :blocks_by, :display_name => users(:moderator_user).display_name
426     assert_response :success
427     assert_select "table#block_list", :count => 1 do
428       assert_select "tr", 2
429       assert_select "a[href='#{user_block_path(user_blocks(:active_block))}']", 1
430     end
431
432     # Check the list of blocks given by a different moderator
433     get :blocks_by, :display_name => users(:second_moderator_user).display_name
434     assert_response :success
435     assert_select "table#block_list", :count => 1 do
436       assert_select "tr", 3
437       assert_select "a[href='#{user_block_path(user_blocks(:expired_block))}']", 1
438       assert_select "a[href='#{user_block_path(user_blocks(:revoked_block))}']", 1
439     end
440
441     # Check the list of blocks (not) given by a normal user
442     get :blocks_by, :display_name => users(:normal_user).display_name
443     assert_response :success
444     assert_select "table#block_list", false
445     assert_select "p", "#{users(:normal_user).display_name} has not made any blocks yet."
446   end
447 end