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