]> git.openstreetmap.org Git - rails.git/blob - test/controllers/user_blocks_controller_test.rb
Convert ACL fixture to a factory, and add some tests
[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     # Check that the block creation page requires us to login
122     get :new, :display_name => users(:normal_user).display_name
123     assert_redirected_to login_path(:referer => new_user_block_path(:display_name => users(:normal_user).display_name))
124
125     # Login as a normal user
126     session[:user] = users(:public_user).id
127
128     # Check that normal users can't load the block creation page
129     get :new, :display_name => users(:normal_user).display_name
130     assert_redirected_to user_blocks_path
131     assert_equal "You need to be a moderator to perform that action.", flash[:error]
132
133     # Login as a moderator
134     session[:user] = users(:moderator_user).id
135
136     # Check that the block creation page loads for moderators
137     get :new, :display_name => users(:normal_user).display_name
138     assert_response :success
139     assert_select "form#new_user_block", :count => 1 do
140       assert_select "textarea#user_block_reason", :count => 1
141       assert_select "select#user_block_period", :count => 1
142       assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
143       assert_select "input#display_name[type='hidden']", :count => 1
144       assert_select "input[type='submit'][value='Create block']", :count => 1
145     end
146
147     # We should get an error if no user is specified
148     get :new
149     assert_response :not_found
150     assert_template "user/no_such_user"
151     assert_select "h1", "The user  does not exist"
152
153     # We should get an error if the user doesn't exist
154     get :new, :display_name => "non_existent_user"
155     assert_response :not_found
156     assert_template "user/no_such_user"
157     assert_select "h1", "The user non_existent_user does not exist"
158   end
159
160   ##
161   # test the edit action
162   def test_edit
163     active_block = create(:user_block)
164
165     # Check that the block edit page requires us to login
166     get :edit, :id => active_block.id
167     assert_redirected_to login_path(:referer => edit_user_block_path(:id => active_block.id))
168
169     # Login as a normal user
170     session[:user] = users(:public_user).id
171
172     # Check that normal users can't load the block edit page
173     get :edit, :id => active_block.id
174     assert_redirected_to user_blocks_path
175     assert_equal "You need to be a moderator to perform that action.", flash[:error]
176
177     # Login as a moderator
178     session[:user] = users(:moderator_user).id
179
180     # Check that the block edit page loads for moderators
181     get :edit, :id => active_block.id
182     assert_response :success
183     assert_select "form#edit_user_block_#{active_block.id}", :count => 1 do
184       assert_select "textarea#user_block_reason", :count => 1
185       assert_select "select#user_block_period", :count => 1
186       assert_select "input#user_block_needs_view[type='checkbox']", :count => 1
187       assert_select "input[type='submit'][value='Update block']", :count => 1
188     end
189
190     # We should get an error if no user is specified
191     assert_raise ActionController::UrlGenerationError do
192       get :edit
193     end
194
195     # We should get an error if the user doesn't exist
196     get :edit, :id => 99999
197     assert_response :not_found
198     assert_template "not_found"
199     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
200   end
201
202   ##
203   # test the create action
204   def test_create
205     # Not logged in yet, so creating a block should fail
206     post :create
207     assert_response :forbidden
208
209     # Login as a normal user
210     session[:user] = users(:public_user).id
211
212     # Check that normal users can't create blocks
213     post :create
214     assert_response :forbidden
215
216     # Login as a moderator
217     session[:user] = users(:moderator_user).id
218
219     # A bogus block period should result in an error
220     assert_no_difference "UserBlock.count" do
221       post :create,
222            :display_name => users(:unblocked_user).display_name,
223            :user_block_period => "99"
224     end
225     assert_redirected_to new_user_block_path(:display_name => users(:unblocked_user).display_name)
226     assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
227
228     # Check that creating a block works
229     assert_difference "UserBlock.count", 1 do
230       post :create,
231            :display_name => users(:unblocked_user).display_name,
232            :user_block_period => "12",
233            :user_block => { :needs_view => false, :reason => "Vandalism" }
234     end
235     id = UserBlock.order(:id).ids.last
236     assert_redirected_to user_block_path(:id => id)
237     assert_equal "Created a block on user #{users(:unblocked_user).display_name}.", flash[:notice]
238     b = UserBlock.find(id)
239     assert_in_delta Time.now, b.created_at, 1
240     assert_in_delta Time.now, b.updated_at, 1
241     assert_in_delta Time.now + 12.hours, b.ends_at, 1
242     assert_equal false, b.needs_view
243     assert_equal "Vandalism", b.reason
244     assert_equal "markdown", b.reason_format
245     assert_equal users(:moderator_user).id, b.creator_id
246
247     # We should get an error if no user is specified
248     post :create
249     assert_response :not_found
250     assert_template "user/no_such_user"
251     assert_select "h1", "The user  does not exist"
252
253     # We should get an error if the user doesn't exist
254     post :create, :display_name => "non_existent_user"
255     assert_response :not_found
256     assert_template "user/no_such_user"
257     assert_select "h1", "The user non_existent_user does not exist"
258   end
259
260   ##
261   # test the update action
262   def test_update
263     active_block = create(:user_block, :creator => users(:moderator_user))
264
265     # Not logged in yet, so updating a block should fail
266     put :update, :id => active_block.id
267     assert_response :forbidden
268
269     # Login as a normal user
270     session[:user] = users(:public_user).id
271
272     # Check that normal users can't update blocks
273     put :update, :id => active_block.id
274     assert_response :forbidden
275
276     # Login as the wrong moderator
277     session[:user] = users(:second_moderator_user).id
278
279     # Check that only the person who created a block can update it
280     assert_no_difference "UserBlock.count" do
281       put :update,
282           :id => active_block.id,
283           :user_block_period => "12",
284           :user_block => { :needs_view => true, :reason => "Vandalism" }
285     end
286     assert_redirected_to edit_user_block_path(:id => active_block.id)
287     assert_equal "Only the moderator who created this block can edit it.", flash[:error]
288
289     # Login as the correct moderator
290     session[:user] = users(:moderator_user).id
291
292     # A bogus block period should result in an error
293     assert_no_difference "UserBlock.count" do
294       put :update,
295           :id => active_block.id,
296           :user_block_period => "99"
297     end
298     assert_redirected_to edit_user_block_path(:id => active_block.id)
299     assert_equal "The blocking period must be one of the values selectable in the drop-down list.", flash[:error]
300
301     # Check that updating a block works
302     assert_no_difference "UserBlock.count" do
303       put :update,
304           :id => active_block.id,
305           :user_block_period => "12",
306           :user_block => { :needs_view => true, :reason => "Vandalism" }
307     end
308     assert_redirected_to user_block_path(:id => active_block.id)
309     assert_equal "Block updated.", flash[:notice]
310     b = UserBlock.find(active_block.id)
311     assert_in_delta Time.now, b.updated_at, 1
312     assert_equal true, b.needs_view
313     assert_equal "Vandalism", b.reason
314
315     # We should get an error if no block ID is specified
316     assert_raise ActionController::UrlGenerationError do
317       put :update
318     end
319
320     # We should get an error if the block doesn't exist
321     put :update, :id => 99999
322     assert_response :not_found
323     assert_template "not_found"
324     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
325   end
326
327   ##
328   # test the revoke action
329   def test_revoke
330     active_block = create(:user_block)
331
332     # Check that the block revoke page requires us to login
333     get :revoke, :id => active_block.id
334     assert_redirected_to login_path(:referer => revoke_user_block_path(:id => active_block.id))
335
336     # Login as a normal user
337     session[:user] = users(:public_user).id
338
339     # Check that normal users can't load the block revoke page
340     get :revoke, :id => active_block.id
341     assert_redirected_to user_blocks_path
342     assert_equal "You need to be a moderator to perform that action.", flash[:error]
343
344     # Login as a moderator
345     session[:user] = users(:moderator_user).id
346
347     # Check that the block revoke page loads for moderators
348     get :revoke, :id => active_block.id
349     assert_response :success
350     assert_template "revoke"
351     assert_select "form", :count => 1 do
352       assert_select "input#confirm[type='checkbox']", :count => 1
353       assert_select "input[type='submit'][value='Revoke!']", :count => 1
354     end
355
356     # Check that revoking a block works
357     post :revoke, :id => active_block.id, :confirm => true
358     assert_redirected_to user_block_path(:id => active_block.id)
359     b = UserBlock.find(active_block.id)
360     assert_in_delta Time.now, b.ends_at, 1
361
362     # We should get an error if no block ID is specified
363     assert_raise ActionController::UrlGenerationError do
364       get :revoke
365     end
366
367     # We should get an error if the block doesn't exist
368     get :revoke, :id => 99999
369     assert_response :not_found
370     assert_template "not_found"
371     assert_select "p", "Sorry, the user block with ID 99999 could not be found."
372   end
373
374   ##
375   # test the blocks_on action
376   def test_blocks_on
377     active_block = create(:user_block, :user => users(:blocked_user))
378     revoked_block = create(:user_block, :revoked, :user => users(:blocked_user))
379     expired_block = create(:user_block, :expired, :user => users(:unblocked_user))
380
381     # Asking for a list of blocks with no user name should fail
382     assert_raise ActionController::UrlGenerationError do
383       get :blocks_on
384     end
385
386     # Asking for a list of blocks with a bogus user name should fail
387     get :blocks_on, :display_name => "non_existent_user"
388     assert_response :not_found
389     assert_template "user/no_such_user"
390     assert_select "h1", "The user non_existent_user does not exist"
391
392     # Check the list of blocks for a user that has never been blocked
393     get :blocks_on, :display_name => users(:normal_user).display_name
394     assert_response :success
395     assert_select "table#block_list", false
396     assert_select "p", "#{users(:normal_user).display_name} has not been blocked yet."
397
398     # Check the list of blocks for a user that is currently blocked
399     get :blocks_on, :display_name => users(:blocked_user).display_name
400     assert_response :success
401     assert_select "table#block_list", :count => 1 do
402       assert_select "tr", 3
403       assert_select "a[href='#{user_block_path(active_block)}']", 1
404       assert_select "a[href='#{user_block_path(revoked_block)}']", 1
405     end
406
407     # Check the list of blocks for a user that has previously been blocked
408     get :blocks_on, :display_name => users(:unblocked_user).display_name
409     assert_response :success
410     assert_select "table#block_list", :count => 1 do
411       assert_select "tr", 2
412       assert_select "a[href='#{user_block_path(expired_block)}']", 1
413     end
414   end
415
416   ##
417   # test the blocks_by action
418   def test_blocks_by
419     active_block = create(:user_block, :creator => users(:moderator_user))
420     expired_block = create(:user_block, :expired, :creator => users(:second_moderator_user))
421     revoked_block = create(:user_block, :revoked, :creator => users(:second_moderator_user))
422
423     # Asking for a list of blocks with no user name should fail
424     assert_raise ActionController::UrlGenerationError do
425       get :blocks_by
426     end
427
428     # Asking for a list of blocks with a bogus user name should fail
429     get :blocks_by, :display_name => "non_existent_user"
430     assert_response :not_found
431     assert_template "user/no_such_user"
432     assert_select "h1", "The user non_existent_user does not exist"
433
434     # Check the list of blocks given by one moderator
435     get :blocks_by, :display_name => users(:moderator_user).display_name
436     assert_response :success
437     assert_select "table#block_list", :count => 1 do
438       assert_select "tr", 2
439       assert_select "a[href='#{user_block_path(active_block)}']", 1
440     end
441
442     # Check the list of blocks given by a different moderator
443     get :blocks_by, :display_name => users(:second_moderator_user).display_name
444     assert_response :success
445     assert_select "table#block_list", :count => 1 do
446       assert_select "tr", 3
447       assert_select "a[href='#{user_block_path(expired_block)}']", 1
448       assert_select "a[href='#{user_block_path(revoked_block)}']", 1
449     end
450
451     # Check the list of blocks (not) given by a normal user
452     get :blocks_by, :display_name => users(:normal_user).display_name
453     assert_response :success
454     assert_select "table#block_list", false
455     assert_select "p", "#{users(:normal_user).display_name} has not made any blocks yet."
456   end
457 end