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