]> git.openstreetmap.org Git - rails.git/blob - test/controllers/user_blocks_controller_test.rb
dbeb7569f4c7ab8b692ad6563ded69841fdcd642
[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", :method => :post },
18       { :controller => "user_blocks", :action => "create" }
19     )
20     assert_routing(
21       { :path => "/user_blocks/1", :method => :get },
22       { :controller => "user_blocks", :action => "show", :id => "1" }
23     )
24     assert_routing(
25       { :path => "/user_blocks/1/edit", :method => :get },
26       { :controller => "user_blocks", :action => "edit", :id => "1" }
27     )
28     assert_routing(
29       { :path => "/user_blocks/1", :method => :put },
30       { :controller => "user_blocks", :action => "update", :id => "1" }
31     )
32     assert_routing(
33       { :path => "/user_blocks/1", :method => :delete },
34       { :controller => "user_blocks", :action => "destroy", :id => "1" }
35     )
36     assert_routing(
37       { :path => "/blocks/1/revoke", :method => :get },
38       { :controller => "user_blocks", :action => "revoke", :id => "1" }
39     )
40     assert_routing(
41       { :path => "/blocks/1/revoke", :method => :post },
42       { :controller => "user_blocks", :action => "revoke", :id => "1" }
43     )
44
45     assert_routing(
46       { :path => "/user/username/blocks", :method => :get },
47       { :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
48     )
49     assert_routing(
50       { :path => "/user/username/blocks_by", :method => :get },
51       { :controller => "user_blocks", :action => "blocks_by", :display_name => "username" }
52     )
53     assert_routing(
54       { :path => "/user/username/blocks/revoke_all", :method => :get },
55       { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
56     )
57     assert_routing(
58       { :path => "/user/username/blocks/revoke_all", :method => :post },
59       { :controller => "user_blocks", :action => "revoke_all", :display_name => "username" }
60     )
61   end
62
63   ##
64   # test the index action
65   def test_index
66     revoked_block = create(:user_block, :revoked)
67
68     get user_blocks_path
69     assert_response :success
70     assert_select "table#block_list tbody tr", :count => 1 do
71       assert_select "a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
72       assert_select "a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
73       assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
74     end
75
76     active_block = create(:user_block)
77     expired_block = create(:user_block, :expired)
78
79     get user_blocks_path
80     assert_response :success
81     assert_select "table#block_list tbody", :count => 1 do
82       assert_select "tr", 3
83       assert_select "a[href='#{user_block_path(active_block)}']", 1
84       assert_select "a[href='#{user_block_path(expired_block)}']", 1
85       assert_select "a[href='#{user_block_path(revoked_block)}']", 1
86     end
87   end
88
89   ##
90   # test the index action with multiple pages
91   def test_index_paged
92     user_blocks = create_list(:user_block, 50).reverse
93     next_path = user_blocks_path
94
95     get next_path
96     assert_response :success
97     check_user_blocks_table user_blocks[0...20]
98     check_no_page_link "Newer Blocks"
99     next_path = check_page_link "Older Blocks"
100
101     get next_path
102     assert_response :success
103     check_user_blocks_table user_blocks[20...40]
104     check_page_link "Newer Blocks"
105     next_path = check_page_link "Older Blocks"
106
107     get next_path
108     assert_response :success
109     check_user_blocks_table user_blocks[40...50]
110     check_page_link "Newer Blocks"
111     check_no_page_link "Older Blocks"
112   end
113
114   ##
115   # test the index action with invalid pages
116   def test_index_invalid_paged
117     %w[-1 0 fred].each do |id|
118       get user_blocks_path(:before => id)
119       assert_redirected_to :controller => :errors, :action => :bad_request
120
121       get user_blocks_path(:after => id)
122       assert_redirected_to :controller => :errors, :action => :bad_request
123     end
124   end
125
126   ##
127   # test the show action
128   def test_show
129     active_block = create(:user_block, :needs_view)
130     expired_block = create(:user_block, :expired)
131     revoked_block = create(:user_block, :revoked)
132
133     # Viewing a block should fail when a bogus ID is given
134     get user_block_path(:id => 99999)
135     assert_response :not_found
136     assert_template "not_found"
137     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
138
139     # Viewing an expired block should work
140     get user_block_path(:id => expired_block)
141     assert_response :success
142     assert_select "h1 a[href='#{user_path expired_block.user}']", :text => expired_block.user.display_name
143     assert_select "h1 a[href='#{user_path expired_block.creator}']", :text => expired_block.creator.display_name
144
145     # Viewing a revoked block should work
146     get user_block_path(:id => revoked_block)
147     assert_response :success
148     assert_select "h1 a[href='#{user_path revoked_block.user}']", :text => revoked_block.user.display_name
149     assert_select "h1 a[href='#{user_path revoked_block.creator}']", :text => revoked_block.creator.display_name
150     assert_select "a[href='#{user_path revoked_block.revoker}']", :text => revoked_block.revoker.display_name
151
152     # Viewing an active block should work, but shouldn't mark it as seen
153     get user_block_path(:id => active_block)
154     assert_response :success
155     assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
156     assert_select "h1 a[href='#{user_path active_block.creator}']", :text => active_block.creator.display_name
157     assert UserBlock.find(active_block.id).needs_view
158
159     # Login as the blocked user
160     session_for(active_block.user)
161
162     # Now viewing it should mark it as seen
163     get user_block_path(:id => active_block)
164     assert_response :success
165     assert_not UserBlock.find(active_block.id).needs_view
166   end
167
168   ##
169   # test edit/revoke link for active blocks
170   def test_active_block_buttons
171     creator_user = create(:moderator_user)
172     other_moderator_user = create(:moderator_user)
173     block = create(:user_block, :creator => creator_user)
174
175     session_for(other_moderator_user)
176     check_block_buttons block, :edit => 1, :revoke => 1
177
178     session_for(creator_user)
179     check_block_buttons block, :edit => 1, :revoke => 1
180   end
181
182   ##
183   # test the edit link for expired blocks
184   def test_expired_block_buttons
185     creator_user = create(:moderator_user)
186     other_moderator_user = create(:moderator_user)
187     block = create(:user_block, :expired, :creator => creator_user)
188
189     session_for(other_moderator_user)
190     check_block_buttons block
191
192     session_for(creator_user)
193     check_block_buttons block, :edit => 1
194   end
195
196   ##
197   # test the edit link for revoked blocks
198   def test_revoked_block_buttons
199     creator_user = create(:moderator_user)
200     revoker_user = create(:moderator_user)
201     other_moderator_user = create(:moderator_user)
202     block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user)
203
204     session_for(other_moderator_user)
205     check_block_buttons block
206
207     session_for(creator_user)
208     check_block_buttons block, :edit => 1
209
210     session_for(revoker_user)
211     check_block_buttons block, :edit => 1
212   end
213
214   ##
215   # test the new action
216   def test_new
217     target_user = create(:user)
218
219     # Check that the block creation page requires us to login
220     get new_user_block_path(target_user)
221     assert_redirected_to login_path(:referer => new_user_block_path(target_user))
222
223     # Login as a normal user
224     session_for(create(:user))
225
226     # Check that normal users can't load the block creation page
227     get new_user_block_path(target_user)
228     assert_redirected_to :controller => "errors", :action => "forbidden"
229
230     # Login as a moderator
231     session_for(create(:moderator_user))
232
233     # Check that the block creation page loads for moderators
234     get new_user_block_path(target_user)
235     assert_response :success
236     assert_select "h1 a[href='#{user_path target_user}']", :text => target_user.display_name
237     assert_select "form#new_user_block", :count => 1 do
238       assert_select "textarea#user_block_reason", :count => 1
239       assert_select "select#user_block_period", :count => 1
240       assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
241       assert_select "input#display_name[type='hidden']", :count => 1
242       assert_select "input[type='submit'][value='Create block']", :count => 1
243     end
244
245     # We should get an error if the user doesn't exist
246     get new_user_block_path(:display_name => "non_existent_user")
247     assert_response :not_found
248     assert_template "users/no_such_user"
249     assert_select "h1", "The user non_existent_user does not exist"
250   end
251
252   ##
253   # test the edit action
254   def test_edit
255     active_block = create(:user_block)
256
257     # Check that the block edit page requires us to login
258     get edit_user_block_path(:id => active_block)
259     assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
260
261     # Login as a normal user
262     session_for(create(:user))
263
264     # Check that normal users can't load the block edit page
265     get edit_user_block_path(:id => active_block)
266     assert_redirected_to :controller => "errors", :action => "forbidden"
267
268     # Login as a moderator
269     session_for(create(:moderator_user))
270
271     # Check that the block edit page loads for moderators
272     get edit_user_block_path(:id => active_block)
273     assert_response :success
274     assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
275     assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
276       assert_select "textarea#user_block_reason", :count => 1
277       assert_select "select#user_block_period", :count => 1
278       assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
279       assert_select "input[type='submit'][value='Update block']", :count => 1
280     end
281
282     # We should get an error if the user doesn't exist
283     get edit_user_block_path(:id => 99999)
284     assert_response :not_found
285     assert_template "not_found"
286     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
287   end
288
289   ##
290   # test the edit action when the remaining block duration doesn't match the available select options
291   def test_edit_duration
292     moderator_user = create(:moderator_user)
293
294     freeze_time do
295       active_block = create(:user_block, :creator => moderator_user, :ends_at => Time.now.utc + 96.hours)
296
297       session_for(moderator_user)
298       get edit_user_block_path(active_block)
299
300       assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
301         assert_select "select#user_block_period", :count => 1 do
302           assert_select "option[value='96'][selected]", :count => 1
303         end
304       end
305
306       travel 2.hours
307       get edit_user_block_path(active_block)
308
309       assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
310         assert_select "select#user_block_period", :count => 1 do
311           assert_select "option[value='96'][selected]", :count => 1
312         end
313       end
314     end
315   end
316
317   ##
318   # test the create action
319   def test_create
320     target_user = create(:user)
321     moderator_user = create(:moderator_user)
322
323     # Not logged in yet, so creating a block should fail
324     post user_blocks_path
325     assert_response :forbidden
326
327     # Login as a normal user
328     session_for(create(:user))
329
330     # Check that normal users can't create blocks
331     post user_blocks_path
332     assert_redirected_to :controller => "errors", :action => "forbidden"
333
334     # Login as a moderator
335     session_for(moderator_user)
336
337     # A bogus block period should result in an error
338     assert_no_difference "UserBlock.count" do
339       post user_blocks_path(:display_name => target_user.display_name,
340                             :user_block_period => "99")
341     end
342     assert_redirected_to new_user_block_path(target_user)
343     assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
344
345     # Check that creating a block works
346     assert_difference "UserBlock.count", 1 do
347       post user_blocks_path(:display_name => target_user.display_name,
348                             :user_block_period => "12",
349                             :user_block => { :needs_view => false, :reason => "Vandalism" })
350     end
351     b = UserBlock.last
352     assert_redirected_to user_block_path(:id => b.id)
353     assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
354     assert_in_delta Time.now.utc, b.created_at, 1
355     assert_in_delta Time.now.utc, b.updated_at, 1
356     assert_in_delta Time.now.utc + 12.hours, b.ends_at, 1
357     assert_not b.needs_view
358     assert_equal "Vandalism", b.reason
359     assert_equal "markdown", b.reason_format
360     assert_equal moderator_user.id, b.creator_id
361
362     # We should get an error if no user is specified
363     post user_blocks_path
364     assert_response :not_found
365     assert_template "users/no_such_user"
366     assert_select "h1", "The user  does not exist"
367
368     # We should get an error if the user doesn't exist
369     post user_blocks_path(:display_name => "non_existent_user")
370     assert_response :not_found
371     assert_template "users/no_such_user"
372     assert_select "h1", "The user non_existent_user does not exist"
373   end
374
375   ##
376   # test the duration of a created block
377   def test_create_duration
378     target_user = create(:user)
379     moderator_user = create(:moderator_user)
380
381     session_for(moderator_user)
382     post user_blocks_path(:display_name => target_user.display_name,
383                           :user_block_period => "336",
384                           :user_block => { :needs_view => false, :reason => "Vandalism" })
385
386     block = UserBlock.last
387     assert_equal 1209600, block.ends_at - block.created_at
388   end
389
390   ##
391   # test the update action
392   def test_update
393     moderator_user = create(:moderator_user)
394     active_block = create(:user_block, :creator => moderator_user)
395
396     # Not logged in yet, so updating a block should fail
397     put user_block_path(:id => active_block)
398     assert_response :forbidden
399
400     # Login as a normal user
401     session_for(create(:user))
402
403     # Check that normal users can't update blocks
404     put user_block_path(:id => active_block)
405     assert_redirected_to :controller => "errors", :action => "forbidden"
406
407     # Login as the moderator
408     session_for(moderator_user)
409
410     # A bogus block period should result in an error
411     assert_no_difference "UserBlock.count" do
412       put user_block_path(:id => active_block, :user_block_period => "99")
413     end
414     assert_redirected_to edit_user_block_path(active_block)
415     assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
416
417     # Check that updating a block works
418     assert_no_difference "UserBlock.count" do
419       put user_block_path(:id => active_block,
420                           :user_block_period => "12",
421                           :user_block => { :needs_view => true, :reason => "Vandalism" })
422     end
423     assert_redirected_to user_block_path(active_block)
424     assert_equal "Block updated.", flash[:notice]
425     b = UserBlock.find(active_block.id)
426     assert_in_delta Time.now.utc, b.updated_at, 1
427     assert b.needs_view
428     assert_equal "Vandalism", b.reason
429
430     # We should get an error if the block doesn't exist
431     put user_block_path(:id => 99999)
432     assert_response :not_found
433     assert_template "not_found"
434     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
435   end
436
437   ##
438   # test the update action on expired blocks
439   def test_update_expired
440     creator_user = create(:moderator_user)
441     other_moderator_user = create(:moderator_user)
442     block = create(:user_block, :expired, :creator => creator_user, :reason => "Original Reason")
443
444     session_for(other_moderator_user)
445     put user_block_path(block,
446                         :user_block_period => "0",
447                         :user_block => { :needs_view => false, :reason => "Updated Reason" })
448     assert_redirected_to edit_user_block_path(block)
449     assert_equal "Only the moderator who created this block can edit it.", flash[:error]
450     block.reload
451     assert_not block.active?
452     assert_equal "Original Reason", block.reason
453
454     session_for(creator_user)
455     check_inactive_block_updates(block)
456   end
457
458   ##
459   # test the update action on revoked blocks
460   def test_update_revoked
461     creator_user = create(:moderator_user)
462     revoker_user = create(:moderator_user)
463     other_moderator_user = create(:moderator_user)
464     block = create(:user_block, :revoked, :creator => creator_user, :revoker => revoker_user, :reason => "Original Reason")
465
466     session_for(other_moderator_user)
467     put user_block_path(block,
468                         :user_block_period => "0",
469                         :user_block => { :needs_view => false, :reason => "Updated Reason" })
470     assert_redirected_to edit_user_block_path(block)
471     assert_equal "Only the moderators who created or revoked this block can edit it.", flash[:error]
472     block.reload
473     assert_not_predicate block, :active?
474     assert_equal "Original Reason", block.reason
475
476     session_for(creator_user)
477     check_inactive_block_updates(block)
478
479     session_for(revoker_user)
480     check_inactive_block_updates(block)
481   end
482
483   ##
484   # test the update action revoking the block
485   def test_revoke_using_update_by_creator
486     moderator_user = create(:moderator_user)
487     block = create(:user_block, :creator => moderator_user)
488
489     session_for(moderator_user)
490     put user_block_path(block,
491                         :user_block_period => "24",
492                         :user_block => { :needs_view => false, :reason => "Updated Reason" })
493     assert_redirected_to user_block_path(block)
494     assert_equal "Block updated.", flash[:notice]
495     block.reload
496     assert_predicate block, :active?
497     assert_nil block.revoker
498
499     put user_block_path(block,
500                         :user_block_period => "0",
501                         :user_block => { :needs_view => false, :reason => "Updated Reason" })
502     assert_redirected_to user_block_path(block)
503     assert_equal "Block updated.", flash[:notice]
504     block.reload
505     assert_not_predicate block, :active?
506     assert_equal moderator_user, block.revoker
507   end
508
509   def test_revoke_using_update_by_other_moderator
510     creator_user = create(:moderator_user)
511     other_moderator_user = create(:moderator_user)
512     block = create(:user_block, :creator => creator_user)
513
514     session_for(other_moderator_user)
515     put user_block_path(block,
516                         :user_block_period => "24",
517                         :user_block => { :needs_view => false, :reason => "Updated Reason" })
518     assert_response :success
519     assert_equal "Only the moderator who created this block can edit it without revoking.", flash[:error]
520     block.reload
521     assert_predicate block, :active?
522     assert_nil block.revoker
523
524     put user_block_path(block,
525                         :user_block_period => "0",
526                         :user_block => { :needs_view => false, :reason => "Updated Reason" })
527     assert_redirected_to user_block_path(block)
528     assert_equal "Block updated.", flash[:notice]
529     block.reload
530     assert_not_predicate block, :active?
531     assert_equal other_moderator_user, block.revoker
532   end
533
534   ##
535   # test the revoke action
536   def test_revoke
537     active_block = create(:user_block)
538
539     # Check that the block revoke page requires us to login
540     get revoke_user_block_path(:id => active_block)
541     assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
542
543     # Login as a normal user
544     session_for(create(:user))
545
546     # Check that normal users can't load the block revoke page
547     get revoke_user_block_path(:id => active_block)
548     assert_redirected_to :controller => "errors", :action => "forbidden"
549
550     # Login as a moderator
551     session_for(create(:moderator_user))
552
553     # Check that the block revoke page loads for moderators
554     get revoke_user_block_path(:id => active_block)
555     assert_response :success
556     assert_template "revoke"
557     assert_select "h1 a[href='#{user_path active_block.user}']", :text => active_block.user.display_name
558     assert_select "form", :count => 1 do
559       assert_select "input#confirm[type='checkbox']", :count => 1
560       assert_select "input[type='submit'][value='Revoke!']", :count => 1
561     end
562
563     # Check that revoking a block using GET should fail
564     get revoke_user_block_path(:id => active_block, :confirm => true)
565     assert_response :success
566     assert_template "revoke"
567     b = UserBlock.find(active_block.id)
568     assert_operator b.ends_at - Time.now.utc, :>, 100
569
570     # Check that revoking a block works using POST
571     post revoke_user_block_path(:id => active_block, :confirm => true)
572     assert_redirected_to user_block_path(active_block)
573     b = UserBlock.find(active_block.id)
574     assert_in_delta Time.now.utc, b.ends_at, 1
575
576     # We should get an error if the block doesn't exist
577     get revoke_user_block_path(:id => 99999)
578     assert_response :not_found
579     assert_template "not_found"
580     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
581   end
582
583   ##
584   # test the revoke all page
585   def test_revoke_all_page
586     blocked_user = create(:user)
587     create(:user_block, :user => blocked_user)
588
589     # Asking for the revoke all blocks page with a bogus user name should fail
590     get user_blocks_on_path("non_existent_user")
591     assert_response :not_found
592
593     # Check that the revoke all blocks page requires us to login
594     get revoke_all_user_blocks_path(blocked_user)
595     assert_redirected_to login_path(:referer => revoke_all_user_blocks_path(blocked_user))
596
597     # Login as a normal user
598     session_for(create(:user))
599
600     # Check that normal users can't load the revoke all blocks page
601     get revoke_all_user_blocks_path(blocked_user)
602     assert_redirected_to :controller => "errors", :action => "forbidden"
603
604     # Login as a moderator
605     session_for(create(:moderator_user))
606
607     # Check that the revoke all blocks page loads for moderators
608     get revoke_all_user_blocks_path(blocked_user)
609     assert_response :success
610     assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
611   end
612
613   ##
614   # test the revoke all action
615   def test_revoke_all_action
616     blocked_user = create(:user)
617     active_block1 = create(:user_block, :user => blocked_user)
618     active_block2 = create(:user_block, :user => blocked_user)
619     expired_block1 = create(:user_block, :expired, :user => blocked_user)
620     blocks = [active_block1, active_block2, expired_block1]
621     moderator_user = create(:moderator_user)
622
623     assert_predicate active_block1, :active?
624     assert_predicate active_block2, :active?
625     assert_not_predicate expired_block1, :active?
626
627     # Login as a normal user
628     session_for(create(:user))
629
630     # Check that normal users can't load the block revoke page
631     get revoke_all_user_blocks_path(:blocked_user)
632     assert_redirected_to :controller => "errors", :action => "forbidden"
633
634     # Login as a moderator
635     session_for(moderator_user)
636
637     # Check that revoking blocks using GET should fail
638     get revoke_all_user_blocks_path(blocked_user, :confirm => true)
639     assert_response :success
640     assert_template "revoke_all"
641
642     blocks.each(&:reload)
643     assert_predicate active_block1, :active?
644     assert_predicate active_block2, :active?
645     assert_not_predicate expired_block1, :active?
646
647     # Check that revoking blocks works using POST
648     post revoke_all_user_blocks_path(blocked_user, :confirm => true)
649     assert_redirected_to user_blocks_on_path(blocked_user)
650
651     blocks.each(&:reload)
652     assert_not_predicate active_block1, :active?
653     assert_not_predicate active_block2, :active?
654     assert_not_predicate expired_block1, :active?
655     assert_equal moderator_user, active_block1.revoker
656     assert_equal moderator_user, active_block2.revoker
657     assert_not_equal moderator_user, expired_block1.revoker
658   end
659
660   ##
661   # test changes to end/deactivation dates
662   def test_dates_when_viewed_before_end
663     blocked_user = create(:user)
664     moderator_user = create(:moderator_user)
665
666     freeze_time do
667       session_for(moderator_user)
668       assert_difference "UserBlock.count", 1 do
669         post user_blocks_path(:display_name => blocked_user.display_name,
670                               :user_block_period => "48",
671                               :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
672       end
673       block = UserBlock.last
674       assert_equal Time.now.utc + 2.days, block.ends_at
675       assert_nil block.deactivates_at
676
677       travel 1.day
678       session_for(blocked_user)
679       get user_block_path(block)
680       block.reload
681       assert_equal Time.now.utc + 1.day, block.ends_at
682       assert_equal Time.now.utc + 1.day, block.deactivates_at
683     end
684   end
685
686   def test_dates_when_viewed_after_end
687     blocked_user = create(:user)
688     moderator_user = create(:moderator_user)
689
690     freeze_time do
691       session_for(moderator_user)
692       assert_difference "UserBlock.count", 1 do
693         post user_blocks_path(:display_name => blocked_user.display_name,
694                               :user_block_period => "24",
695                               :user_block => { :needs_view => true, :reason => "Testing deactivates_at" })
696       end
697       block = UserBlock.last
698       assert_equal Time.now.utc + 1.day, block.ends_at
699       assert_nil block.deactivates_at
700
701       travel 2.days
702       session_for(blocked_user)
703       get user_block_path(block)
704       block.reload
705       assert_equal Time.now.utc - 1.day, block.ends_at
706       assert_equal Time.now.utc, block.deactivates_at
707     end
708   end
709
710   def test_dates_when_edited_before_end
711     blocked_user = create(:user)
712     moderator_user = create(:moderator_user)
713
714     freeze_time do
715       session_for(moderator_user)
716       assert_difference "UserBlock.count", 1 do
717         post user_blocks_path(:display_name => blocked_user.display_name,
718                               :user_block_period => "48",
719                               :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
720       end
721       block = UserBlock.last
722       assert_equal Time.now.utc + 2.days, block.ends_at
723       assert_equal Time.now.utc + 2.days, block.deactivates_at
724
725       travel 1.day
726       put user_block_path(block,
727                           :user_block_period => "48",
728                           :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
729       block.reload
730       assert_equal Time.now.utc + 2.days, block.ends_at
731       assert_equal Time.now.utc + 2.days, block.deactivates_at
732     end
733   end
734
735   def test_dates_when_edited_after_end
736     blocked_user = create(:user)
737     moderator_user = create(:moderator_user)
738
739     freeze_time do
740       session_for(moderator_user)
741       assert_difference "UserBlock.count", 1 do
742         post user_blocks_path(:display_name => blocked_user.display_name,
743                               :user_block_period => "24",
744                               :user_block => { :needs_view => false, :reason => "Testing deactivates_at" })
745       end
746       block = UserBlock.last
747       assert_equal Time.now.utc + 1.day, block.ends_at
748       assert_equal Time.now.utc + 1.day, block.deactivates_at
749
750       travel 2.days
751       put user_block_path(block,
752                           :user_block_period => "0",
753                           :user_block => { :needs_view => false, :reason => "Testing deactivates_at updated" })
754       block.reload
755       assert_equal Time.now.utc - 1.day, block.ends_at
756       assert_equal Time.now.utc - 1.day, block.deactivates_at
757     end
758   end
759
760   ##
761   # test updates on legacy records without correctly initialized deactivates_at
762   def test_update_legacy_deactivates_at
763     blocked_user = create(:user)
764     moderator_user = create(:moderator_user)
765
766     freeze_time do
767       block = UserBlock.new :user => blocked_user,
768                             :creator => moderator_user,
769                             :reason => "because",
770                             :ends_at => Time.now.utc + 1.day,
771                             :needs_view => false
772
773       assert_difference "UserBlock.count", 1 do
774         block.save :validate => false
775       end
776
777       travel 2.days
778       session_for(moderator_user)
779       put user_block_path(block,
780                           :user_block_period => "0",
781                           :user_block => { :needs_view => false, :reason => "Testing legacy block update" })
782       block.reload
783       assert_equal Time.now.utc - 1.day, block.ends_at
784       assert_equal Time.now.utc - 1.day, block.deactivates_at
785     end
786   end
787
788   ##
789   # test the blocks_on action
790   def test_blocks_on
791     blocked_user = create(:user)
792     unblocked_user = create(:user)
793     normal_user = create(:user)
794     active_block = create(:user_block, :user => blocked_user)
795     revoked_block = create(:user_block, :revoked, :user => blocked_user)
796     expired_block = create(:user_block, :expired, :user => unblocked_user)
797
798     # Asking for a list of blocks with a bogus user name should fail
799     get user_blocks_on_path("non_existent_user")
800     assert_response :not_found
801     assert_template "users/no_such_user"
802     assert_select "h1", "The user non_existent_user does not exist"
803
804     # Check the list of blocks for a user that has never been blocked
805     get user_blocks_on_path(normal_user)
806     assert_response :success
807     assert_select "table#block_list", false
808     assert_select "p", "#{normal_user.display_name} has not been blocked yet."
809
810     # Check the list of blocks for a user that is currently blocked
811     get user_blocks_on_path(blocked_user)
812     assert_response :success
813     assert_select "h1 a[href='#{user_path blocked_user}']", :text => blocked_user.display_name
814     assert_select "table#block_list tbody", :count => 1 do
815       assert_select "tr", 2
816       assert_select "a[href='#{user_block_path(active_block)}']", 1
817       assert_select "a[href='#{user_block_path(revoked_block)}']", 1
818     end
819
820     # Check the list of blocks for a user that has previously been blocked
821     get user_blocks_on_path(unblocked_user)
822     assert_response :success
823     assert_select "h1 a[href='#{user_path unblocked_user}']", :text => unblocked_user.display_name
824     assert_select "table#block_list tbody", :count => 1 do
825       assert_select "tr", 1
826       assert_select "a[href='#{user_block_path(expired_block)}']", 1
827     end
828   end
829
830   ##
831   # test the blocks_on action with multiple pages
832   def test_blocks_on_paged
833     user = create(:user)
834     user_blocks = create_list(:user_block, 50, :user => user).reverse
835     next_path = user_blocks_on_path(user)
836
837     get next_path
838     assert_response :success
839     check_user_blocks_table user_blocks[0...20]
840     check_no_page_link "Newer Blocks"
841     next_path = check_page_link "Older Blocks"
842
843     get next_path
844     assert_response :success
845     check_user_blocks_table user_blocks[20...40]
846     check_page_link "Newer Blocks"
847     next_path = check_page_link "Older Blocks"
848
849     get next_path
850     assert_response :success
851     check_user_blocks_table user_blocks[40...50]
852     check_page_link "Newer Blocks"
853     check_no_page_link "Older Blocks"
854   end
855
856   ##
857   # test the blocks_on action with invalid pages
858   def test_blocks_on_invalid_paged
859     user = create(:user)
860
861     %w[-1 0 fred].each do |id|
862       get user_blocks_on_path(user, :before => id)
863       assert_redirected_to :controller => :errors, :action => :bad_request
864
865       get user_blocks_on_path(user, :after => id)
866       assert_redirected_to :controller => :errors, :action => :bad_request
867     end
868   end
869
870   ##
871   # test the blocks_by action
872   def test_blocks_by
873     moderator_user = create(:moderator_user)
874     second_moderator_user = create(:moderator_user)
875     normal_user = create(:user)
876     active_block = create(:user_block, :creator => moderator_user)
877     expired_block = create(:user_block, :expired, :creator => second_moderator_user)
878     revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
879
880     # Asking for a list of blocks with a bogus user name should fail
881     get user_blocks_by_path("non_existent_user")
882     assert_response :not_found
883     assert_template "users/no_such_user"
884     assert_select "h1", "The user non_existent_user does not exist"
885
886     # Check the list of blocks given by one moderator
887     get user_blocks_by_path(moderator_user)
888     assert_response :success
889     assert_select "h1 a[href='#{user_path moderator_user}']", :text => moderator_user.display_name
890     assert_select "table#block_list tbody", :count => 1 do
891       assert_select "tr", 1
892       assert_select "a[href='#{user_block_path(active_block)}']", 1
893     end
894
895     # Check the list of blocks given by a different moderator
896     get user_blocks_by_path(second_moderator_user)
897     assert_response :success
898     assert_select "h1 a[href='#{user_path second_moderator_user}']", :text => second_moderator_user.display_name
899     assert_select "table#block_list tbody", :count => 1 do
900       assert_select "tr", 2
901       assert_select "a[href='#{user_block_path(expired_block)}']", 1
902       assert_select "a[href='#{user_block_path(revoked_block)}']", 1
903     end
904
905     # Check the list of blocks (not) given by a normal user
906     get user_blocks_by_path(normal_user)
907     assert_response :success
908     assert_select "table#block_list", false
909     assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
910   end
911
912   ##
913   # test the blocks_by action with multiple pages
914   def test_blocks_by_paged
915     user = create(:moderator_user)
916     user_blocks = create_list(:user_block, 50, :creator => user).reverse
917     next_path = user_blocks_by_path(user)
918
919     get next_path
920     assert_response :success
921     check_user_blocks_table user_blocks[0...20]
922     check_no_page_link "Newer Blocks"
923     next_path = check_page_link "Older Blocks"
924
925     get next_path
926     assert_response :success
927     check_user_blocks_table user_blocks[20...40]
928     check_page_link "Newer Blocks"
929     next_path = check_page_link "Older Blocks"
930
931     get next_path
932     assert_response :success
933     check_user_blocks_table user_blocks[40...50]
934     check_page_link "Newer Blocks"
935     check_no_page_link "Older Blocks"
936   end
937
938   ##
939   # test the blocks_by action with invalid pages
940   def test_blocks_by_invalid_paged
941     user = create(:moderator_user)
942
943     %w[-1 0 fred].each do |id|
944       get user_blocks_by_path(user, :before => id)
945       assert_redirected_to :controller => :errors, :action => :bad_request
946
947       get user_blocks_by_path(user, :after => id)
948       assert_redirected_to :controller => :errors, :action => :bad_request
949     end
950   end
951
952   private
953
954   def check_block_buttons(block, edit: 0, revoke: 0)
955     [user_blocks_path, user_block_path(block)].each do |path|
956       get path
957       assert_response :success
958       assert_select "a[href='#{edit_user_block_path block}']", :count => edit
959       assert_select "a[href='#{revoke_user_block_path block}']", :count => revoke
960     end
961   end
962
963   def check_inactive_block_updates(block)
964     original_ends_at = block.ends_at
965
966     put user_block_path(block,
967                         :user_block_period => "0",
968                         :user_block => { :needs_view => false, :reason => "Updated Reason" })
969     assert_redirected_to user_block_path(block)
970     assert_equal "Block updated.", flash[:notice]
971     block.reload
972     assert_not_predicate block, :active?
973     assert_equal "Updated Reason", block.reason
974     assert_equal original_ends_at, block.ends_at
975
976     put user_block_path(block,
977                         :user_block_period => "0",
978                         :user_block => { :needs_view => true, :reason => "Updated Reason Needs View" })
979     assert_response :success
980     assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
981     block.reload
982     assert_not_predicate block, :active?
983     assert_equal "Updated Reason", block.reason
984     assert_equal original_ends_at, block.ends_at
985
986     put user_block_path(block,
987                         :user_block_period => "1",
988                         :user_block => { :needs_view => false, :reason => "Updated Reason Duration Extended" })
989     assert_response :success
990     assert_equal "This block is inactive and cannot be reactivated.", flash[:error]
991     block.reload
992     assert_not_predicate block, :active?
993     assert_equal "Updated Reason", block.reason
994     assert_equal original_ends_at, block.ends_at
995
996     put user_block_path(block,
997                         :user_block_period => "0",
998                         :user_block => { :needs_view => false, :reason => "Updated Reason Again" })
999     assert_redirected_to user_block_path(block)
1000     assert_equal "Block updated.", flash[:notice]
1001     block.reload
1002     assert_not_predicate block, :active?
1003     assert_equal "Updated Reason Again", block.reason
1004     assert_equal original_ends_at, block.ends_at
1005   end
1006
1007   def check_user_blocks_table(user_blocks)
1008     assert_dom "table#block_list tbody tr" do |rows|
1009       assert_equal user_blocks.count, rows.count, "unexpected number of rows in user blocks table"
1010       rows.zip(user_blocks).map do |row, user_block|
1011         assert_dom row, "a[href='#{user_block_path user_block}']", 1
1012       end
1013     end
1014   end
1015
1016   def check_no_page_link(name)
1017     assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
1018   end
1019
1020   def check_page_link(name)
1021     assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
1022       return buttons.first.attributes["href"].value
1023     end
1024   end
1025 end