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