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