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