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