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