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", :method => :post },
14 { :controller => "users", :action => "create" }
18 { :path => "/user/go_public", :method => :post },
19 { :controller => "users", :action => "go_public" }
23 { :path => "/user/suspended", :method => :get },
24 { :controller => "users", :action => "suspended" }
28 { :path => "/user/username", :method => :get },
29 { :controller => "users", :action => "show", :display_name => "username" }
33 { :path => "/user/username/set_status", :method => :post },
34 { :controller => "users", :action => "set_status", :display_name => "username" }
37 { :path => "/user/username", :method => :delete },
38 { :controller => "users", :action => "destroy", :display_name => "username" }
42 # The user creation page loads
45 assert_redirected_to new_user_path(:cookie_test => "true")
47 get new_user_path, :params => { :cookie_test => "true" }
48 assert_response :success
50 assert_no_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
52 assert_select "html", :count => 1 do
53 assert_select "head", :count => 1 do
54 assert_select "title", :text => /Sign Up/, :count => 1
56 assert_select "body", :count => 1 do
57 assert_select "div#content", :count => 1 do
58 assert_select "form[action='/user'][method='post']", :count => 1 do
59 assert_select "input[id='user_email']", :count => 1
60 assert_select "input[id='user_display_name']", :count => 1
61 assert_select "input[id='user_pass_crypt'][type='password']", :count => 1
62 assert_select "input[id='user_pass_crypt_confirmation'][type='password']", :count => 1
63 assert_select "input[type='submit'][value='Sign Up']", :count => 1
70 def test_new_logged_in
71 session_for(create(:user))
74 assert_redirected_to root_path
76 get new_user_path, :params => { :referer => "/test" }
77 assert_redirected_to "/test"
80 def test_create_success
81 user = build(:user, :pending)
83 assert_difference "User.count", 1 do
84 assert_difference "ActionMailer::Base.deliveries.size", 1 do
85 perform_enqueued_jobs do
86 post users_path, :params => { :user => user.attributes }
92 register_email = ActionMailer::Base.deliveries.first
94 assert_equal register_email.to[0], user.email
95 assert_match(/#{@url}/, register_email.body.to_s)
98 assert_redirected_to :controller => :confirmations, :action => :confirm, :display_name => user.display_name
100 ActionMailer::Base.deliveries.clear
103 def test_create_duplicate_email
104 user = build(:user, :pending)
105 create(:user, :email => user.email)
107 assert_no_difference "User.count" do
108 assert_no_difference "ActionMailer::Base.deliveries.size" do
109 perform_enqueued_jobs do
110 post users_path, :params => { :user => user.attributes }
115 assert_response :success
116 assert_template "new"
117 assert_select "form > div > input.is-invalid#user_email"
120 def test_create_duplicate_email_uppercase
121 user = build(:user, :pending)
122 create(:user, :email => user.email.upcase)
124 assert_no_difference "User.count" do
125 assert_no_difference "ActionMailer::Base.deliveries.size" do
126 perform_enqueued_jobs do
127 post users_path, :params => { :user => user.attributes }
132 assert_response :success
133 assert_template "new"
134 assert_select "form > div > input.is-invalid#user_email"
137 def test_create_duplicate_name
138 user = build(:user, :pending)
139 create(:user, :display_name => user.display_name)
141 assert_no_difference "User.count" do
142 assert_no_difference "ActionMailer::Base.deliveries.size" do
143 perform_enqueued_jobs do
144 post users_path, :params => { :user => user.attributes }
149 assert_response :success
150 assert_template "new"
151 assert_select "form > div > input.is-invalid#user_display_name"
154 def test_create_duplicate_name_uppercase
155 user = build(:user, :pending)
156 create(:user, :display_name => user.display_name.upcase)
158 assert_no_difference "User.count" do
159 assert_no_difference "ActionMailer::Base.deliveries.size" do
160 perform_enqueued_jobs do
161 post users_path, :params => { :user => user.attributes }
166 assert_response :success
167 assert_template "new"
168 assert_select "form > div > input.is-invalid#user_display_name"
171 def test_create_blocked_domain
172 user = build(:user, :pending, :email => "user@example.net")
174 # Now block that domain
175 create(:acl, :domain => "example.net", :k => "no_account_creation")
177 # Check that the second half of registration fails
178 assert_no_difference "User.count" do
179 assert_no_difference "ActionMailer::Base.deliveries.size" do
180 perform_enqueued_jobs do
181 post users_path, :params => { :user => user.attributes }
186 assert_response :success
187 assert_template "blocked"
190 def test_create_referer_params
191 user = build(:user, :pending)
193 assert_difference "User.count", 1 do
194 assert_difference "ActionMailer::Base.deliveries.size", 1 do
195 post users_path, :params => { :user => user.attributes, :referer => "/edit?editor=id#map=1/2/3" }
196 assert_enqueued_with :job => ActionMailer::MailDeliveryJob,
197 :args => proc { |args| args[3][:args][2] == welcome_path(:editor => "id", :zoom => 1, :lat => 2, :lon => 3) }
198 perform_enqueued_jobs
202 ActionMailer::Base.deliveries.clear
206 user = create(:user, :data_public => false)
209 post user_go_public_path
211 assert_redirected_to edit_account_path
212 assert User.find(user.id).data_public
215 # Check that the user account page will display and contains some relevant
216 # information for the user
218 # Test a non-existent user
219 get user_path("unknown")
220 assert_response :not_found
226 assert_response :success
227 assert_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
228 assert_select "div.content-heading" do
229 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
230 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
231 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
232 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
233 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/account']", 0
234 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
235 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
236 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(user.display_name)}']", 0
239 # Test a user who has been blocked
240 blocked_user = create(:user)
241 create(:user_block, :user => blocked_user)
242 get user_path(blocked_user)
243 assert_response :success
244 assert_select "div.content-heading" do
245 assert_select "a[href^='/user/#{ERB::Util.u(blocked_user.display_name)}/history']", 1
246 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/traces']", 1
247 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary']", 1
248 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/diary/comments']", 1
249 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/account']", 0
250 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks']", 1
251 assert_select "a[href='/user/#{ERB::Util.u(blocked_user.display_name)}/blocks_by']", 0
252 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(blocked_user.display_name)}']", 0
255 # Test a moderator who has applied blocks
256 moderator_user = create(:moderator_user)
257 create(:user_block, :creator => moderator_user)
258 get user_path(moderator_user)
259 assert_response :success
260 assert_select "div.content-heading" do
261 assert_select "a[href^='/user/#{ERB::Util.u(moderator_user.display_name)}/history']", 1
262 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/traces']", 1
263 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary']", 1
264 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/diary/comments']", 1
265 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/account']", 0
266 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks']", 0
267 assert_select "a[href='/user/#{ERB::Util.u(moderator_user.display_name)}/blocks_by']", 1
268 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(moderator_user.display_name)}']", 0
271 # Login as a normal user
274 # Test the normal user
276 assert_response :success
277 assert_select "div.content-heading" do
278 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
279 assert_select "a[href='/traces/mine']", 1
280 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
281 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
282 assert_select "a[href='/account/edit']", 1
283 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
284 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
285 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(user.display_name)}']", 0
286 assert_select "a[href='/api/0.6/user/#{ERB::Util.u(user.id)}']", 0
289 # Login as a moderator
290 session_for(create(:moderator_user))
292 # Test the normal user
294 assert_response :success
295 assert_select "div.content-heading" do
296 assert_select "a[href^='/user/#{ERB::Util.u(user.display_name)}/history']", 1
297 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/traces']", 1
298 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary']", 1
299 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/diary/comments']", 1
300 assert_select "a[href='/account/edit']", 0
301 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks']", 0
302 assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/blocks_by']", 0
303 assert_select "a[href='/user_blocks/new/#{ERB::Util.u(user.display_name)}']", 1
304 assert_select "a[href='/api/0.6/user/#{ERB::Util.u(user.id)}']", 1
308 # Test whether information about contributor terms is shown for users who haven't agreed
309 def test_terms_not_agreed
310 agreed_user = create(:user, :terms_agreed => 3.days.ago)
311 seen_user = create(:user, :terms_seen => true, :terms_agreed => nil)
312 not_seen_user = create(:user, :terms_seen => false, :terms_agreed => nil)
314 get user_path(agreed_user)
315 assert_response :success
316 assert_select "div.content-heading" do
317 assert_select "dt", :count => 0, :text => /Contributor terms/
320 get user_path(seen_user)
321 assert_response :success
322 assert_select "div.content-heading" do
323 assert_select "dt", :count => 1, :text => /Contributor terms/
324 assert_select "dd", /Declined/
327 get user_path(not_seen_user)
328 assert_response :success
329 assert_select "div.content-heading" do
330 assert_select "dt", :count => 1, :text => /Contributor terms/
331 assert_select "dd", /Undecided/
338 # Try without logging in
339 post set_status_user_path(user), :params => { :event => "confirm" }
340 assert_response :forbidden
342 # Now try as a normal user
344 post set_status_user_path(user), :params => { :event => "confirm" }
345 assert_redirected_to :controller => :errors, :action => :forbidden
347 # Finally try as an administrator
348 session_for(create(:administrator_user))
349 post set_status_user_path(user), :params => { :event => "confirm" }
350 assert_redirected_to :action => :show, :display_name => user.display_name
351 assert_equal "confirmed", User.find(user.id).status
355 user = create(:user, :home_lat => 12.1, :home_lon => 12.1, :description => "test")
357 # Try without logging in
358 delete user_path(user)
359 assert_response :forbidden
361 # Now try as a normal user
363 delete user_path(user)
364 assert_redirected_to :controller => :errors, :action => :forbidden
366 # Finally try as an administrator
367 session_for(create(:administrator_user))
368 delete user_path(user)
369 assert_redirected_to :action => :show, :display_name => user.display_name
371 # Check that the user was deleted properly
373 assert_equal "user_#{user.id}", user.display_name
374 assert_equal "", user.description
375 assert_nil user.home_lat
376 assert_nil user.home_lon
377 assert_not user.avatar.attached?
378 assert_not user.email_valid
379 assert_nil user.new_email
380 assert_nil user.auth_provider
381 assert_nil user.auth_uid
382 assert_equal "deleted", user.status
385 def test_auth_failure_callback
386 get auth_failure_path
387 assert_redirected_to login_path
389 get auth_failure_path, :params => { :origin => "/" }
390 assert_redirected_to root_path
392 get auth_failure_path, :params => { :origin => "http://www.google.com" }
393 assert_redirected_to login_path