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