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