]> git.openstreetmap.org Git - rails.git/blob - test/controllers/user_blocks_controller_test.rb
Merge remote-tracking branch 'upstream/pull/2994'
[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/new", :method => :get },
18       { :controller => "user_blocks", :action => "new" }
19     )
20     assert_routing(
21       { :path => "/user_blocks", :method => :post },
22       { :controller => "user_blocks", :action => "create" }
23     )
24     assert_routing(
25       { :path => "/user_blocks/1", :method => :get },
26       { :controller => "user_blocks", :action => "show", :id => "1" }
27     )
28     assert_routing(
29       { :path => "/user_blocks/1/edit", :method => :get },
30       { :controller => "user_blocks", :action => "edit", :id => "1" }
31     )
32     assert_routing(
33       { :path => "/user_blocks/1", :method => :put },
34       { :controller => "user_blocks", :action => "update", :id => "1" }
35     )
36     assert_routing(
37       { :path => "/user_blocks/1", :method => :delete },
38       { :controller => "user_blocks", :action => "destroy", :id => "1" }
39     )
40     assert_routing(
41       { :path => "/blocks/1/revoke", :method => :get },
42       { :controller => "user_blocks", :action => "revoke", :id => "1" }
43     )
44     assert_routing(
45       { :path => "/blocks/1/revoke", :method => :post },
46       { :controller => "user_blocks", :action => "revoke", :id => "1" }
47     )
48
49     assert_routing(
50       { :path => "/user/username/blocks", :method => :get },
51       { :controller => "user_blocks", :action => "blocks_on", :display_name => "username" }
52     )
53     assert_routing(
54       { :path => "/user/username/blocks_by", :method => :get },
55       { :controller => "user_blocks", :action => "blocks_by", :display_name => "username" }
56     )
57   end
58
59   ##
60   # test the index action
61   def test_index
62     active_block = create(:user_block)
63     expired_block = create(:user_block, :expired)
64     revoked_block = create(:user_block, :revoked)
65
66     get user_blocks_path
67     assert_response :success
68     assert_select "table#block_list", :count => 1 do
69       assert_select "tr", 4
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     create_list(:user_block, 50)
80
81     get user_blocks_path
82     assert_response :success
83     assert_select "table#block_list", :count => 1 do
84       assert_select "tr", :count => 21
85     end
86
87     get user_blocks_path(:page => 2)
88     assert_response :success
89     assert_select "table#block_list", :count => 1 do
90       assert_select "tr", :count => 21
91     end
92   end
93
94   ##
95   # test the show action
96   def test_show
97     active_block = create(:user_block, :needs_view)
98     expired_block = create(:user_block, :expired)
99     revoked_block = create(:user_block, :revoked)
100
101     # Viewing a block should fail when a bogus ID is given
102     get user_block_path(:id => 99999)
103     assert_response :not_found
104     assert_template "not_found"
105     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
106
107     # Viewing an expired block should work
108     get user_block_path(:id => expired_block)
109     assert_response :success
110
111     # Viewing a revoked block should work
112     get user_block_path(:id => revoked_block)
113     assert_response :success
114
115     # Viewing an active block should work, but shouldn't mark it as seen
116     get user_block_path(:id => active_block)
117     assert_response :success
118     assert UserBlock.find(active_block.id).needs_view
119
120     # Login as the blocked user
121     session_for(active_block.user)
122
123     # Now viewing it should mark it as seen
124     get user_block_path(:id => active_block)
125     assert_response :success
126     assert_not UserBlock.find(active_block.id).needs_view
127   end
128
129   ##
130   # test the new action
131   def test_new
132     target_user = create(:user)
133
134     # Check that the block creation page requires us to login
135     get new_user_block_path(:display_name => target_user.display_name)
136     assert_redirected_to login_path(:referer => new_user_block_path(:display_name => target_user.display_name))
137
138     # Login as a normal user
139     session_for(create(:user))
140
141     # Check that normal users can't load the block creation page
142     get new_user_block_path(:display_name => target_user.display_name)
143     assert_response :redirect
144     assert_redirected_to :controller => "errors", :action => "forbidden"
145
146     # Login as a moderator
147     session_for(create(:moderator_user))
148
149     # Check that the block creation page loads for moderators
150     get new_user_block_path(:display_name => target_user.display_name)
151     assert_response :success
152     assert_select "form#new_user_block", :count => 1 do
153       assert_select "textarea#user_block_reason", :count => 1
154       assert_select "select#user_block_period", :count => 1
155       assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
156       assert_select "input#display_name[type='hidden']", :count => 1
157       assert_select "input[type='submit'][value='Create block']", :count => 1
158     end
159
160     # We should get an error if the user doesn't exist
161     get new_user_block_path(:display_name => "non_existent_user")
162     assert_response :not_found
163     assert_template "users/no_such_user"
164     assert_select "h1", "The user non_existent_user does not exist"
165   end
166
167   ##
168   # test the edit action
169   def test_edit
170     active_block = create(:user_block)
171
172     # Check that the block edit page requires us to login
173     get edit_user_block_path(:id => active_block)
174     assert_redirected_to login_path(:referer => edit_user_block_path(active_block))
175
176     # Login as a normal user
177     session_for(create(:user))
178
179     # Check that normal users can't load the block edit page
180     get edit_user_block_path(:id => active_block)
181     assert_response :redirect
182     assert_redirected_to :controller => "errors", :action => "forbidden"
183
184     # Login as a moderator
185     session_for(create(:moderator_user))
186
187     # Check that the block edit page loads for moderators
188     get edit_user_block_path(:id => active_block)
189     assert_response :success
190     assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
191       assert_select "textarea#user_block_reason", :count => 1
192       assert_select "select#user_block_period", :count => 1
193       assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
194       assert_select "input[type='submit'][value='Update block']", :count => 1
195     end
196
197     # We should get an error if the user doesn't exist
198     get edit_user_block_path(:id => 99999)
199     assert_response :not_found
200     assert_template "not_found"
201     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
202   end
203
204   ##
205   # test the create action
206   def test_create
207     target_user = create(:user)
208     moderator_user = create(:moderator_user)
209
210     # Not logged in yet, so creating a block should fail
211     post user_blocks_path
212     assert_response :forbidden
213
214     # Login as a normal user
215     session_for(create(:user))
216
217     # Check that normal users can't create blocks
218     post user_blocks_path
219     assert_response :redirect
220     assert_redirected_to :controller => "errors", :action => "forbidden"
221
222     # Login as a moderator
223     session_for(moderator_user)
224
225     # A bogus block period should result in an error
226     assert_no_difference "UserBlock.count" do
227       post user_blocks_path(:display_name => target_user.display_name,
228                             :user_block_period => "99")
229     end
230     assert_redirected_to new_user_block_path(:display_name => target_user.display_name)
231     assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
232
233     # Check that creating a block works
234     assert_difference "UserBlock.count", 1 do
235       post user_blocks_path(:display_name => target_user.display_name,
236                             :user_block_period => "12",
237                             :user_block => { :needs_view => false, :reason => "Vandalism" })
238     end
239     id = UserBlock.order(:id).ids.last
240     assert_redirected_to user_block_path(:id => id)
241     assert_equal "Created a block on user #{target_user.display_name}.", flash[:notice]
242     b = UserBlock.find(id)
243     assert_in_delta Time.now, b.created_at, 1
244     assert_in_delta Time.now, b.updated_at, 1
245     assert_in_delta Time.now + 12.hours, b.ends_at, 1
246     assert_not b.needs_view
247     assert_equal "Vandalism", b.reason
248     assert_equal "markdown", b.reason_format
249     assert_equal moderator_user.id, b.creator_id
250
251     # We should get an error if no user is specified
252     post user_blocks_path
253     assert_response :not_found
254     assert_template "users/no_such_user"
255     assert_select "h1", "The user  does not exist"
256
257     # We should get an error if the user doesn't exist
258     post user_blocks_path(:display_name => "non_existent_user")
259     assert_response :not_found
260     assert_template "users/no_such_user"
261     assert_select "h1", "The user non_existent_user does not exist"
262   end
263
264   ##
265   # test the update action
266   def test_update
267     moderator_user = create(:moderator_user)
268     second_moderator_user = create(:moderator_user)
269     active_block = create(:user_block, :creator => moderator_user)
270
271     # Not logged in yet, so updating a block should fail
272     put user_block_path(:id => active_block)
273     assert_response :forbidden
274
275     # Login as a normal user
276     session_for(create(:user))
277
278     # Check that normal users can't update blocks
279     put user_block_path(:id => active_block)
280     assert_response :redirect
281     assert_redirected_to :controller => "errors", :action => "forbidden"
282
283     # Login as the wrong moderator
284     session_for(second_moderator_user)
285
286     # Check that only the person who created a block can update it
287     assert_no_difference "UserBlock.count" do
288       put user_block_path(:id => active_block,
289                           :user_block_period => "12",
290                           :user_block => { :needs_view => true, :reason => "Vandalism" })
291     end
292     assert_redirected_to edit_user_block_path(active_block)
293     assert_equal "Only the moderator who created this block can edit it.", flash[:error]
294
295     # Login as the correct moderator
296     session_for(moderator_user)
297
298     # A bogus block period should result in an error
299     assert_no_difference "UserBlock.count" do
300       put user_block_path(:id => active_block, :user_block_period => "99")
301     end
302     assert_redirected_to edit_user_block_path(active_block)
303     assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
304
305     # Check that updating a block works
306     assert_no_difference "UserBlock.count" do
307       put user_block_path(:id => active_block,
308                           :user_block_period => "12",
309                           :user_block => { :needs_view => true, :reason => "Vandalism" })
310     end
311     assert_redirected_to user_block_path(active_block)
312     assert_equal "Block updated.", flash[:notice]
313     b = UserBlock.find(active_block.id)
314     assert_in_delta Time.now, b.updated_at, 1
315     assert b.needs_view
316     assert_equal "Vandalism", b.reason
317
318     # We should get an error if the block doesn't exist
319     put user_block_path(:id => 99999)
320     assert_response :not_found
321     assert_template "not_found"
322     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
323   end
324
325   ##
326   # test the revoke action
327   def test_revoke
328     active_block = create(:user_block)
329
330     # Check that the block revoke page requires us to login
331     get revoke_user_block_path(:id => active_block)
332     assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block))
333
334     # Login as a normal user
335     session_for(create(:user))
336
337     # Check that normal users can't load the block revoke page
338     get revoke_user_block_path(:id => active_block)
339     assert_response :redirect
340     assert_redirected_to :controller => "errors", :action => "forbidden"
341
342     # Login as a moderator
343     session_for(create(:moderator_user))
344
345     # Check that the block revoke page loads for moderators
346     get revoke_user_block_path(:id => active_block)
347     assert_response :success
348     assert_template "revoke"
349     assert_select "form", :count => 1 do
350       assert_select "input#confirm[type='checkbox']", :count => 1
351       assert_select "input[type='submit'][value='Revoke!']", :count => 1
352     end
353
354     # Check that revoking a block works
355     post revoke_user_block_path(:id => active_block, :confirm => true)
356     assert_redirected_to user_block_path(active_block)
357     b = UserBlock.find(active_block.id)
358     assert_in_delta Time.now, b.ends_at, 1
359
360     # We should get an error if the block doesn't exist
361     get revoke_user_block_path(:id => 99999)
362     assert_response :not_found
363     assert_template "not_found"
364     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
365   end
366
367   ##
368   # test the blocks_on action
369   def test_blocks_on
370     blocked_user = create(:user)
371     unblocked_user = create(:user)
372     normal_user = create(:user)
373     active_block = create(:user_block, :user => blocked_user)
374     revoked_block = create(:user_block, :revoked, :user => blocked_user)
375     expired_block = create(:user_block, :expired, :user => unblocked_user)
376
377     # Asking for a list of blocks with a bogus user name should fail
378     get user_blocks_on_path(:display_name => "non_existent_user")
379     assert_response :not_found
380     assert_template "users/no_such_user"
381     assert_select "h1", "The user non_existent_user does not exist"
382
383     # Check the list of blocks for a user that has never been blocked
384     get user_blocks_on_path(:display_name => normal_user.display_name)
385     assert_response :success
386     assert_select "table#block_list", false
387     assert_select "p", "#{normal_user.display_name} has not been blocked yet."
388
389     # Check the list of blocks for a user that is currently blocked
390     get user_blocks_on_path(:display_name => blocked_user.display_name)
391     assert_response :success
392     assert_select "table#block_list", :count => 1 do
393       assert_select "tr", 3
394       assert_select "a[href='#{user_block_path(active_block)}']", 1
395       assert_select "a[href='#{user_block_path(revoked_block)}']", 1
396     end
397
398     # Check the list of blocks for a user that has previously been blocked
399     get user_blocks_on_path(:display_name => unblocked_user.display_name)
400     assert_response :success
401     assert_select "table#block_list", :count => 1 do
402       assert_select "tr", 2
403       assert_select "a[href='#{user_block_path(expired_block)}']", 1
404     end
405   end
406
407   ##
408   # test the blocks_on action with multiple pages
409   def test_blocks_on_paged
410     user = create(:user)
411     create_list(:user_block, 50, :user => user)
412
413     get user_blocks_on_path(:display_name => user.display_name)
414     assert_response :success
415     assert_select "table#block_list", :count => 1 do
416       assert_select "tr", :count => 21
417     end
418
419     get user_blocks_on_path(:display_name => user.display_name, :page => 2)
420     assert_response :success
421     assert_select "table#block_list", :count => 1 do
422       assert_select "tr", :count => 21
423     end
424   end
425
426   ##
427   # test the blocks_by action
428   def test_blocks_by
429     moderator_user = create(:moderator_user)
430     second_moderator_user = create(:moderator_user)
431     normal_user = create(:user)
432     active_block = create(:user_block, :creator => moderator_user)
433     expired_block = create(:user_block, :expired, :creator => second_moderator_user)
434     revoked_block = create(:user_block, :revoked, :creator => second_moderator_user)
435
436     # Asking for a list of blocks with a bogus user name should fail
437     get user_blocks_by_path(:display_name => "non_existent_user")
438     assert_response :not_found
439     assert_template "users/no_such_user"
440     assert_select "h1", "The user non_existent_user does not exist"
441
442     # Check the list of blocks given by one moderator
443     get user_blocks_by_path(:display_name => moderator_user.display_name)
444     assert_response :success
445     assert_select "table#block_list", :count => 1 do
446       assert_select "tr", 2
447       assert_select "a[href='#{user_block_path(active_block)}']", 1
448     end
449
450     # Check the list of blocks given by a different moderator
451     get user_blocks_by_path(:display_name => second_moderator_user.display_name)
452     assert_response :success
453     assert_select "table#block_list", :count => 1 do
454       assert_select "tr", 3
455       assert_select "a[href='#{user_block_path(expired_block)}']", 1
456       assert_select "a[href='#{user_block_path(revoked_block)}']", 1
457     end
458
459     # Check the list of blocks (not) given by a normal user
460     get user_blocks_by_path(:display_name => normal_user.display_name)
461     assert_response :success
462     assert_select "table#block_list", false
463     assert_select "p", "#{normal_user.display_name} has not made any blocks yet."
464   end
465
466   ##
467   # test the blocks_by action with multiple pages
468   def test_blocks_by_paged
469     user = create(:moderator_user)
470     create_list(:user_block, 50, :creator => user)
471
472     get user_blocks_by_path(:display_name => user.display_name)
473     assert_response :success
474     assert_select "table#block_list", :count => 1 do
475       assert_select "tr", :count => 21
476     end
477
478     get user_blocks_by_path(:display_name => user.display_name, :page => 2)
479     assert_response :success
480     assert_select "table#block_list", :count => 1 do
481       assert_select "tr", :count => 21
482     end
483   end
484 end