]> git.openstreetmap.org Git - rails.git/blob - test/functional/user_blocks_controller_test.rb
Add functional tests for messages
[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 expired block should work
91     get :show, :id => user_blocks(:expired_block)
92     assert_response :success
93
94     # Viewing a revoked block should work
95     get :show, :id => user_blocks(:revoked_block)
96     assert_response :success
97
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
102
103     # Login as the blocked user
104     session[:user] = users(:blocked_user).id
105     cookies["_osm_username"] = users(:blocked_user).display_name
106
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
111   end
112
113   ##
114   # test the new action
115   def test_new
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))
119
120     # Login as a normal user
121     session[:user] = users(:public_user).id
122     cookies["_osm_username"] = users(:public_user).display_name
123
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]
128
129     # Login as a moderator
130     session[:user] = users(:moderator_user).id
131     cookies["_osm_username"] = users(:moderator_user).display_name
132
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
142     end
143
144     # We should get an error if no user is specified
145     get :new
146     assert_response :not_found
147     assert_template "user/no_such_user"
148     assert_select "h2", "The user  does not exist"
149
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"
155   end
156
157   ##
158   # test the edit action
159   def test_edit
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))
163
164     # Login as a normal user
165     session[:user] = users(:public_user).id
166     cookies["_osm_username"] = users(:public_user).display_name
167
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]
172
173     # Login as a moderator
174     session[:user] = users(:moderator_user).id
175     cookies["_osm_username"] = users(:moderator_user).display_name
176
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
185     end
186
187     # We should get an error if no user is specified
188     get :edit
189     assert_response :not_found
190     assert_template "not_found"
191     assert_select "p", "Sorry, the user block with ID  could not be found."
192
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."
198   end
199
200   ##
201   # test the create action
202   def test_create
203     # Not logged in yet, so creating a block should fail
204     post :create
205     assert_response :forbidden
206
207     # Login as a normal user
208     session[:user] = users(:public_user).id
209     cookies["_osm_username"] = users(:public_user).display_name
210
211     # Check that normal users can't create blocks
212     post :create
213     assert_response :forbidden
214
215     # Login as a moderator
216     session[:user] = users(:moderator_user).id
217     cookies["_osm_username"] = users(:moderator_user).display_name
218
219     # A bogus block period should result in an error
220     assert_no_difference "UserBlock.count" do
221       post :create,
222         :display_name => users(:unblocked_user).display_name,
223         :user_block_period => "99"
224     end
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]
227
228     # Check that creating a block works
229     assert_difference "UserBlock.count", 1 do
230       post :create,
231         :display_name => users(:unblocked_user).display_name,
232         :user_block_period => "12",
233         :user_block => { :needs_view => false, :reason => "Vandalism" }
234     end
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
245
246     # We should get an error if no user is specified
247     post :create
248     assert_response :not_found
249     assert_template "user/no_such_user"
250     assert_select "h2", "The user  does not exist"
251
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"
257   end
258
259   ##
260   # test the update action
261   def test_update
262     # Not logged in yet, so updating a block should fail
263     put :update
264     assert_response :forbidden
265
266     # Login as a normal user
267     session[:user] = users(:public_user).id
268     cookies["_osm_username"] = users(:public_user).display_name
269
270     # Check that normal users can't update blocks
271     put :update
272     assert_response :forbidden
273
274     # Login as the wrong moderator
275     session[:user] = users(:second_moderator_user).id
276     cookies["_osm_username"] = users(:second_moderator_user).display_name
277
278     # Check that only the person who created a block can update it
279     assert_no_difference "UserBlock.count" do
280       put :update,
281         :id => user_blocks(:active_block).id,
282         :user_block_period => "12",
283         :user_block => { :needs_view => true, :reason => "Vandalism" }
284     end
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]
287
288     # Login as the correct moderator
289     session[:user] = users(:moderator_user).id
290     cookies["_osm_username"] = users(:moderator_user).display_name
291
292     # A bogus block period should result in an error
293     assert_no_difference "UserBlock.count" do
294       put :update,
295         :id => user_blocks(:active_block).id,
296         :user_block_period => "99"
297     end
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]
300
301     # Check that updating a block works
302     assert_no_difference "UserBlock.count" do
303       put :update,
304         :id => user_blocks(:active_block).id,
305         :user_block_period => "12",
306         :user_block => { :needs_view => true, :reason => "Vandalism" }
307     end
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
314
315     # We should get an error if no block ID is specified
316     put :update
317     assert_response :not_found
318     assert_template "not_found"
319     assert_select "p", "Sorry, the user block with ID  could not be found."
320
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."
326   end
327
328   ##
329   # test the revoke action
330   def test_revoke
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))
334
335     # Login as a normal user
336     session[:user] = users(:public_user).id
337     cookies["_osm_username"] = users(:public_user).display_name
338
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]
343
344     # Login as a moderator
345     session[:user] = users(:moderator_user).id
346     cookies["_osm_username"] = users(:moderator_user).display_name
347
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
355     end
356
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
362
363     # We should get an error if no block ID is specified
364     get :revoke
365     assert_response :not_found
366     assert_template "not_found"
367     assert_select "p", "Sorry, the user block with ID  could not be found."
368
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."
374   end
375
376   ##
377   # test the blocks_on action
378   def test_blocks_on
379     # Asking for a list of blocks with no user name should fail
380     get :blocks_on
381     assert_response :not_found
382     assert_template "user/no_such_user"
383     assert_select "h2", "The user  does not exist"
384
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"
390
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."
396
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
404     end
405
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
412     end
413   end
414
415   ##
416   # test the blocks_by action
417   def test_blocks_by
418     # Asking for a list of blocks with no user name should fail
419     get :blocks_by
420     assert_response :not_found
421     assert_template "user/no_such_user"
422     assert_select "h2", "The user  does not exist"
423
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"
429
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
436     end
437
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
445     end
446
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."
452   end
453 end