3 class UsersControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/user/new", :method => :get },
9 { :controller => "users", :action => "new" }
13 { :path => "/user/new", :method => :post },
14 { :controller => "users", :action => "create" }
18 { :path => "/user/terms", :method => :get },
19 { :controller => "users", :action => "terms" }
23 { :path => "/user/save", :method => :post },
24 { :controller => "users", :action => "save" }
28 { :path => "/user/go_public", :method => :post },
29 { :controller => "users", :action => "go_public" }
33 { :path => "/user/suspended", :method => :get },
34 { :controller => "users", :action => "suspended" }
38 { :path => "/user/username", :method => :get },
39 { :controller => "users", :action => "show", :display_name => "username" }
43 { :path => "/user/username/set_status", :method => :post },
44 { :controller => "users", :action => "set_status", :display_name => "username" }
47 { :path => "/user/username", :method => :delete },
48 { :controller => "users", :action => "destroy", :display_name => "username" }
52 # The user creation page loads
55 assert_redirected_to user_new_path(:cookie_test => "true")
57 get user_new_path, :params => { :cookie_test => "true" }
58 assert_response :success
60 assert_select "html", :count => 1 do
61 assert_select "head", :count => 1 do
62 assert_select "title", :text => /Sign Up/, :count => 1
64 assert_select "body", :count => 1 do
65 assert_select "div#content", :count => 1 do
66 assert_select "form[action='/user/new'][method='post']", :count => 1 do
67 assert_select "input[id='user_email']", :count => 1
68 assert_select "input[id='user_display_name']", :count => 1
69 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
70 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
71 assert_select "input[type='submit'][value='Sign Up']", :count => 1
78 def test_new_view_logged_in
79 session_for(create(:user))
82 assert_redirected_to root_path
84 get user_new_path, :params => { :referer => "/test" }
85 assert_redirected_to "/test"
89 user = build(:user, :pending)
91 assert_difference "User.count", 1 do
92 assert_difference "ActionMailer::Base.deliveries.size", 1 do
93 perform_enqueued_jobs do
94 post user_new_path, :params => { :user => user.attributes }
100 register_email = ActionMailer::Base.deliveries.first
102 assert_equal register_email.to[0], user.email
103 assert_match(/#{@url}/, register_email.body.to_s)
106 assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => user.display_name
108 ActionMailer::Base.deliveries.clear
111 def test_new_duplicate_email
112 user = build(:user, :pending)
113 create(:user, :email => user.email)
115 assert_no_difference "User.count" do
116 assert_no_difference "ActionMailer::Base.deliveries.size" do
117 perform_enqueued_jobs do
118 post user_new_path, :params => { :user => user.attributes }
123 assert_response :success
124 assert_template "new"
125 assert_select "form > div > input.is-invalid#user_email"
128 def test_new_duplicate_email_uppercase
129 user = build(:user, :pending)
130 create(:user, :email => user.email.upcase)
132 assert_no_difference "User.count" do
133 assert_no_difference "ActionMailer::Base.deliveries.size" do
134 perform_enqueued_jobs do
135 post user_new_path, :params => { :user => user.attributes }
140 assert_response :success
141 assert_template "new"
142 assert_select "form > div > input.is-invalid#user_email"
145 def test_new_duplicate_name
146 user = build(:user, :pending)
147 create(:user, :display_name => user.display_name)
149 assert_no_difference "User.count" do
150 assert_no_difference "ActionMailer::Base.deliveries.size" do
151 perform_enqueued_jobs do
152 post user_new_path, :params => { :user => user.attributes }
157 assert_response :success
158 assert_template "new"
159 assert_select "form > div > input.is-invalid#user_display_name"
162 def test_new_duplicate_name_uppercase
163 user = build(:user, :pending)
164 create(:user, :display_name => user.display_name.upcase)
166 assert_no_difference "User.count" do
167 assert_no_difference "ActionMailer::Base.deliveries.size" do
168 perform_enqueued_jobs do
169 post user_new_path, :params => { :user => user.attributes }
174 assert_response :success
175 assert_template "new"
176 assert_select "form > div > input.is-invalid#user_display_name"
179 def test_new_blocked_domain
180 user = build(:user, :pending, :email => "user@example.net")
182 # Now block that domain
183 create(:acl, :domain => "example.net", :k => "no_account_creation")
185 # Check that the second half of registration fails
186 assert_no_difference "User.count" do
187 assert_no_difference "ActionMailer::Base.deliveries.size" do
188 perform_enqueued_jobs do
189 post user_new_path, :params => { :user => user.attributes }
194 assert_response :success
195 assert_template "blocked"
198 def test_save_referer_params
199 user = build(:user, :pending)
201 assert_difference "User.count", 1 do
202 assert_difference "ActionMailer::Base.deliveries.size", 1 do
203 post user_new_path, :params => { :user => user.attributes, :referer => "/edit?editor=id#map=1/2/3" }
204 assert_enqueued_with :job => ActionMailer::MailDeliveryJob,
205 :args => proc { |args| args[3][:args][2] == welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3) }
206 perform_enqueued_jobs
210 ActionMailer::Base.deliveries.clear
213 def test_terms_agreed
214 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
219 assert_redirected_to edit_account_path
222 def test_terms_not_seen_without_referer
223 user = create(:user, :terms_seen => false, :terms_agreed => nil)
228 assert_response :success
229 assert_template :terms
231 post user_save_path, :params => { :user => { :consider_pd => true }, :read_ct => 1, :read_tou => 1 }
232 assert_redirected_to edit_account_path
233 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
237 assert user.consider_pd
238 assert_not_nil user.terms_agreed
239 assert user.terms_seen
242 def test_terms_not_seen_with_referer
243 user = create(:user, :terms_seen => false, :terms_agreed => nil)
247 get user_terms_path, :params => { :referer => "/test" }
248 assert_response :success
249 assert_template :terms
251 post user_save_path, :params => { :user => { :consider_pd => true }, :referer => "/test", :read_ct => 1, :read_tou => 1 }
252 assert_redirected_to "/test"
253 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
257 assert user.consider_pd
258 assert_not_nil user.terms_agreed
259 assert user.terms_seen
262 # Check that if you haven't seen the terms, and make a request that requires authentication,
263 # that your request is redirected to view the terms
264 def test_terms_not_seen_redirection
265 user = create(:user, :terms_seen => false, :terms_agreed => nil)
268 get edit_account_path
269 assert_redirected_to :controller => :users, :action => :terms, :referer => "/account/edit"
272 def test_terms_not_logged_in
275 assert_redirected_to login_path(:referer => "/user/terms")
279 user = create(:user, :data_public => false)
282 post user_go_public_path
284 assert_redirected_to edit_account_path
285 assert User.find(user.id).data_public
288 # Check that the user account page will display and contains some relevant
289 # information for the user
291 # Test a non-existent user
292 get user_path("unknown")
293 assert_response :not_found
299 assert_response :success
300 assert_select "div.content-heading" do
301 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
302 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
303 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
304 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
305 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
306 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
307 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
308 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
311 # Test a user who has been blocked
312 blocked_user = create(:user)
313 create(:user_block, :user => blocked_user)
314 get user_path(blocked_user)
315 assert_response :success
316 assert_select "div.content-heading" do
317 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
318 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
319 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
320 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
321 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
322 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
323 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
324 assert_select "a[href='/blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
327 # Test a moderator who has applied blocks
328 moderator_user = create(:moderator_user)
329 create(:user_block, :creator => moderator_user)
330 get user_path(moderator_user)
331 assert_response :success
332 assert_select "div.content-heading" do
333 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
334 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
335 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
336 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
337 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
338 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
339 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
340 assert_select "a[href='/blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
343 # Login as a normal user
346 # Test the normal user
348 assert_response :success
349 assert_select "div.content-heading" do
350 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
351 assert_select "a[href='/traces/mine']", 1
352 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
353 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
354 assert_select "a[href='/account/edit']", 1
355 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
356 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
357 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 0
358 assert_select "a[href='/api/0.6/user/#{ERB::Util.u(user.id)}']", 0
361 # Login as a moderator
362 session_for(create(:moderator_user))
364 # Test the normal user
366 assert_response :success
367 assert_select "div.content-heading" do
368 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
369 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
370 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
371 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
372 assert_select "a[href='/account/edit']", 0
373 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
374 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
375 assert_select "a[href='/blocks/new/#{ERB::Util.u(user.display_name)}']", 1
376 assert_select "a[href='/api/0.6/user/#{ERB::Util.u(user.id)}']", 1
380 # Test whether information about contributor terms is shown for users who haven't agreed
381 def test_terms_not_agreed
382 agreed_user = create(:user, :terms_agreed => 3.days.ago)
383 seen_user = create(:user, :terms_seen => true, :terms_agreed => nil)
384 not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil)
386 get user_path(agreed_user)
387 assert_response :success
388 assert_select "div.content-heading" do
389 assert_select "dt", :count => 0, :text => /Contributor terms/
392 get user_path(seen_user)
393 assert_response :success
394 assert_select "div.content-heading" do
395 assert_select "dt", :count => 1, :text => /Contributor terms/
396 assert_select "dd", /Declined/
399 get user_path(not_seen_user)
400 assert_response :success
401 assert_select "div.content-heading" do
402 assert_select "dt", :count => 1, :text => /Contributor terms/
403 assert_select "dd", /Undecided/
410 # Try without logging in
411 post set_status_user_path(user), :params => { :event => "confirm" }
412 assert_response :forbidden
414 # Now try as a normal user
416 post set_status_user_path(user), :params => { :event => "confirm" }
417 assert_redirected_to :controller => :errors, :action => :forbidden
419 # Finally try as an administrator
420 session_for(create(:administrator_user))
421 post set_status_user_path(user), :params => { :event => "confirm" }
422 assert_redirected_to :action => :show, :display_name => user.display_name
423 assert_equal "confirmed", User.find(user.id).status
427 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
429 # Try without logging in
430 delete user_path(user)
431 assert_response :forbidden
433 # Now try as a normal user
435 delete user_path(user)
436 assert_redirected_to :controller => :errors, :action => :forbidden
438 # Finally try as an administrator
439 session_for(create(:administrator_user))
440 delete user_path(user)
441 assert_redirected_to :action => :show, :display_name => user.display_name
443 # Check that the user was deleted properly
445 assert_equal "user_#{user.id}", user.display_name
446 assert_equal "", user.description
447 assert_nil user.home_lat
448 assert_nil user.home_lon
449 assert_not user.avatar.attached?
450 assert_not user.email_valid
451 assert_nil user.new_email
452 assert_nil user.auth_provider
453 assert_nil user.auth_uid
454 assert_equal "deleted", user.status
457 def test_auth_failure_callback
458 get auth_failure_path
459 assert_redirected_to login_path
461 get auth_failure_path, :params => { :origin => "/" }
462 assert_redirected_to root_path
464 get auth_failure_path, :params => { :origin => "http://www.google.com" }
465 assert_redirected_to login_path