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_no_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
62 assert_select "html", :count => 1 do
63 assert_select "head", :count => 1 do
64 assert_select "title", :text => /Sign Up/, :count => 1
66 assert_select "body", :count => 1 do
67 assert_select "div#content", :count => 1 do
68 assert_select "form[action='/user/new'][method='post']", :count => 1 do
69 assert_select "input[id='user_email']", :count => 1
70 assert_select "input[id='user_display_name']", :count => 1
71 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
72 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
73 assert_select "input[type='submit'][value='Sign Up']", :count => 1
80 def test_new_view_logged_in
81 session_for(create(:user))
84 assert_redirected_to root_path
86 get user_new_path, :params => { :referer => "/test" }
87 assert_redirected_to "/test"
91 user = build(:user, :pending)
93 assert_difference "User.count", 1 do
94 assert_difference "ActionMailer::Base.deliveries.size", 1 do
95 perform_enqueued_jobs do
96 post user_new_path, :params => { :user => user.attributes }
102 register_email = ActionMailer::Base.deliveries.first
104 assert_equal register_email.to[0], user.email
105 assert_match(/#{@url}/, register_email.body.to_s)
108 assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => user.display_name
110 ActionMailer::Base.deliveries.clear
113 def test_new_duplicate_email
114 user = build(:user, :pending)
115 create(:user, :email => user.email)
117 assert_no_difference "User.count" do
118 assert_no_difference "ActionMailer::Base.deliveries.size" do
119 perform_enqueued_jobs do
120 post user_new_path, :params => { :user => user.attributes }
125 assert_response :success
126 assert_template "new"
127 assert_select "form > div > input.is-invalid#user_email"
130 def test_new_duplicate_email_uppercase
131 user = build(:user, :pending)
132 create(:user, :email => user.email.upcase)
134 assert_no_difference "User.count" do
135 assert_no_difference "ActionMailer::Base.deliveries.size" do
136 perform_enqueued_jobs do
137 post user_new_path, :params => { :user => user.attributes }
142 assert_response :success
143 assert_template "new"
144 assert_select "form > div > input.is-invalid#user_email"
147 def test_new_duplicate_name
148 user = build(:user, :pending)
149 create(:user, :display_name => user.display_name)
151 assert_no_difference "User.count" do
152 assert_no_difference "ActionMailer::Base.deliveries.size" do
153 perform_enqueued_jobs do
154 post user_new_path, :params => { :user => user.attributes }
159 assert_response :success
160 assert_template "new"
161 assert_select "form > div > input.is-invalid#user_display_name"
164 def test_new_duplicate_name_uppercase
165 user = build(:user, :pending)
166 create(:user, :display_name => user.display_name.upcase)
168 assert_no_difference "User.count" do
169 assert_no_difference "ActionMailer::Base.deliveries.size" do
170 perform_enqueued_jobs do
171 post user_new_path, :params => { :user => user.attributes }
176 assert_response :success
177 assert_template "new"
178 assert_select "form > div > input.is-invalid#user_display_name"
181 def test_new_blocked_domain
182 user = build(:user, :pending, :email => "user@example.net")
184 # Now block that domain
185 create(:acl, :domain => "example.net", :k => "no_account_creation")
187 # Check that the second half of registration fails
188 assert_no_difference "User.count" do
189 assert_no_difference "ActionMailer::Base.deliveries.size" do
190 perform_enqueued_jobs do
191 post user_new_path, :params => { :user => user.attributes }
196 assert_response :success
197 assert_template "blocked"
200 def test_save_referer_params
201 user = build(:user, :pending)
203 assert_difference "User.count", 1 do
204 assert_difference "ActionMailer::Base.deliveries.size", 1 do
205 post user_new_path, :params => { :user => user.attributes, :referer => "/edit?editor=id#map=1/2/3" }
206 assert_enqueued_with :job => ActionMailer::MailDeliveryJob,
207 :args => proc { |args| args[3][:args][2] == welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3) }
208 perform_enqueued_jobs
212 ActionMailer::Base.deliveries.clear
215 def test_terms_agreed
216 user = create(:user, :terms_seen => true, :terms_agreed => Date.yesterday)
221 assert_redirected_to edit_account_path
224 def test_terms_not_seen_without_referer
225 user = create(:user, :terms_seen => false, :terms_agreed => nil)
230 assert_response :success
231 assert_template :terms
233 post user_save_path, :params => { :user => { :consider_pd => true }, :read_ct => 1, :read_tou => 1 }
234 assert_redirected_to edit_account_path
235 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
239 assert user.consider_pd
240 assert_not_nil user.terms_agreed
241 assert user.terms_seen
244 def test_terms_not_seen_with_referer
245 user = create(:user, :terms_seen => false, :terms_agreed => nil)
249 get user_terms_path, :params => { :referer => "/test" }
250 assert_response :success
251 assert_template :terms
253 post user_save_path, :params => { :user => { :consider_pd => true }, :referer => "/test", :read_ct => 1, :read_tou => 1 }
254 assert_redirected_to "/test"
255 assert_equal "Thanks for accepting the new contributor terms!", flash[:notice]
259 assert user.consider_pd
260 assert_not_nil user.terms_agreed
261 assert user.terms_seen
264 # Check that if you haven't seen the terms, and make a request that requires authentication,
265 # that your request is redirected to view the terms
266 def test_terms_not_seen_redirection
267 user = create(:user, :terms_seen => false, :terms_agreed => nil)
270 get edit_account_path
271 assert_redirected_to :controller => :users, :action => :terms, :referer => "/account/edit"
274 def test_terms_not_logged_in
277 assert_redirected_to login_path(:referer => "/user/terms")
281 user = create(:user, :data_public => false)
284 post user_go_public_path
286 assert_redirected_to edit_account_path
287 assert User.find(user.id).data_public
290 # Check that the user account page will display and contains some relevant
291 # information for the user
293 # Test a non-existent user
294 get user_path("unknown")
295 assert_response :not_found
301 assert_response :success
302 assert_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
303 assert_select "div.content-heading" do
304 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
305 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
306 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
307 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
308 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
309 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
310 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
311 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(user.display_name)}']", 0
314 # Test a user who has been blocked
315 blocked_user = create(:user)
316 create(:user_block, :user => blocked_user)
317 get user_path(blocked_user)
318 assert_response :success
319 assert_select "div.content-heading" do
320 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
321 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
322 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
323 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
324 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
325 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
326 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
327 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
330 # Test a moderator who has applied blocks
331 moderator_user = create(:moderator_user)
332 create(:user_block, :creator => moderator_user)
333 get user_path(moderator_user)
334 assert_response :success
335 assert_select "div.content-heading" do
336 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
337 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
338 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
339 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
340 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
341 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
342 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
343 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
346 # Login as a normal user
349 # Test the normal user
351 assert_response :success
352 assert_select "div.content-heading" do
353 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
354 assert_select "a[href='/traces/mine']", 1
355 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
356 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
357 assert_select "a[href='/account/edit']", 1
358 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
359 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
360 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(user.display_name)}']", 0
361 assert_select "a[href='/api/0.6/user/#{ERB::Util.u(user.id)}']", 0
364 # Login as a moderator
365 session_for(create(:moderator_user))
367 # Test the normal user
369 assert_response :success
370 assert_select "div.content-heading" do
371 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
372 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
373 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
374 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
375 assert_select "a[href='/account/edit']", 0
376 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
377 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
378 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(user.display_name)}']", 1
379 assert_select "a[href='/api/0.6/user/#{ERB::Util.u(user.id)}']", 1
383 # Test whether information about contributor terms is shown for users who haven't agreed
384 def test_terms_not_agreed
385 agreed_user = create(:user, :terms_agreed => 3.days.ago)
386 seen_user = create(:user, :terms_seen => true, :terms_agreed => nil)
387 not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil)
389 get user_path(agreed_user)
390 assert_response :success
391 assert_select "div.content-heading" do
392 assert_select "dt", :count => 0, :text => /Contributor terms/
395 get user_path(seen_user)
396 assert_response :success
397 assert_select "div.content-heading" do
398 assert_select "dt", :count => 1, :text => /Contributor terms/
399 assert_select "dd", /Declined/
402 get user_path(not_seen_user)
403 assert_response :success
404 assert_select "div.content-heading" do
405 assert_select "dt", :count => 1, :text => /Contributor terms/
406 assert_select "dd", /Undecided/
413 # Try without logging in
414 post set_status_user_path(user), :params => { :event => "confirm" }
415 assert_response :forbidden
417 # Now try as a normal user
419 post set_status_user_path(user), :params => { :event => "confirm" }
420 assert_redirected_to :controller => :errors, :action => :forbidden
422 # Finally try as an administrator
423 session_for(create(:administrator_user))
424 post set_status_user_path(user), :params => { :event => "confirm" }
425 assert_redirected_to :action => :show, :display_name => user.display_name
426 assert_equal "confirmed", User.find(user.id).status
430 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
432 # Try without logging in
433 delete user_path(user)
434 assert_response :forbidden
436 # Now try as a normal user
438 delete user_path(user)
439 assert_redirected_to :controller => :errors, :action => :forbidden
441 # Finally try as an administrator
442 session_for(create(:administrator_user))
443 delete user_path(user)
444 assert_redirected_to :action => :show, :display_name => user.display_name
446 # Check that the user was deleted properly
448 assert_equal "user_#{user.id}", user.display_name
449 assert_equal "", user.description
450 assert_nil user.home_lat
451 assert_nil user.home_lon
452 assert_not user.avatar.attached?
453 assert_not user.email_valid
454 assert_nil user.new_email
455 assert_nil user.auth_provider
456 assert_nil user.auth_uid
457 assert_equal "deleted", user.status
460 def test_auth_failure_callback
461 get auth_failure_path
462 assert_redirected_to login_path
464 get auth_failure_path, :params => { :origin => "/" }
465 assert_redirected_to root_path
467 get auth_failure_path, :params => { :origin => "http://www.google.com" }
468 assert_redirected_to login_path