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