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