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