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