1 # frozen_string_literal: true
6 class UsersControllerTest < ActionDispatch::IntegrationTest
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/user/1", :method => :get },
12 { :controller => "api/users", :action => "show", :id => "1" }
15 { :path => "/api/0.6/user/1.json", :method => :get },
16 { :controller => "api/users", :action => "show", :id => "1", :format => "json" }
19 { :path => "/api/0.6/user/details", :method => :get },
20 { :controller => "api/users", :action => "details" }
23 { :path => "/api/0.6/user/details.json", :method => :get },
24 { :controller => "api/users", :action => "details", :format => "json" }
27 { :path => "/api/0.6/users", :method => :get },
28 { :controller => "api/users", :action => "index" }
31 { :path => "/api/0.6/users.json", :method => :get },
32 { :controller => "api/users", :action => "index", :format => "json" }
38 :description => "test",
39 :terms_agreed => Date.yesterday,
40 :home_lat => 12.1, :home_lon => 23.4,
41 :languages => ["en"]) do |user|
42 create(:social_link, :user => user, :url => "https://twitter.com/testuser")
43 create(:social_link, :user => user, :url => "https://github.com/testuser")
46 # check that a visible user is returned properly
47 get api_user_path(:id => user.id)
48 assert_response :success
49 assert_equal "application/xml", response.media_type
51 # check the data that is returned
52 check_xml_details(user, false, false)
54 # check that a suspended user is not returned
55 get api_user_path(:id => create(:user, :suspended).id)
58 # check that a deleted user is not returned
59 get api_user_path(:id => create(:user, :deleted).id)
62 # check that a non-existent user is not returned
63 get api_user_path(:id => 0)
64 assert_response :not_found
66 # check that a visible user is returned properly in json
67 get api_user_path(:id => user.id, :format => "json")
68 assert_response :success
69 assert_equal "application/json", response.media_type
72 js = ActiveSupport::JSON.decode(@response.body)
75 # check the data that is returned
76 check_json_details(js, user, false, false)
81 :home_lat => 12.1, :home_lon => 23.4,
83 good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
84 bad_auth = bearer_authorization_header(user, :scopes => %w[])
85 other_user = create(:user,
86 :home_lat => 12.1, :home_lon => 23.4,
89 # check that we can fetch our own details as XML with read_prefs
90 get api_user_path(:id => user.id), :headers => good_auth
91 assert_response :success
92 assert_equal "application/xml", response.media_type
94 # check the data that is returned
95 check_xml_details(user, true, false)
97 # check that we can fetch a different user's details as XML with read_prefs
98 get api_user_path(:id => other_user.id), :headers => good_auth
99 assert_response :success
100 assert_equal "application/xml", response.media_type
102 # check the data that is returned
103 check_xml_details(other_user, false, false)
105 # check that we can fetch our own details as XML without read_prefs
106 get api_user_path(:id => user.id), :headers => bad_auth
107 assert_response :success
108 assert_equal "application/xml", response.media_type
110 # check the data that is returned
111 check_xml_details(user, false, false)
113 # check that we can fetch our own details as JSON with read_prefs
114 get api_user_path(:id => user.id, :format => "json"), :headers => good_auth
115 assert_response :success
116 assert_equal "application/json", response.media_type
119 js = ActiveSupport::JSON.decode(@response.body)
122 # check the data that is returned
123 check_json_details(js, user, true, false)
125 # check that we can fetch a different user's details as JSON with read_prefs
126 get api_user_path(:id => other_user.id, :format => "json"), :headers => good_auth
127 assert_response :success
128 assert_equal "application/json", response.media_type
131 js = ActiveSupport::JSON.decode(@response.body)
134 # check the data that is returned
135 check_json_details(js, other_user, false, false)
137 # check that we can fetch our own details as JSON without read_prefs
138 get api_user_path(:id => user.id, :format => "json"), :headers => bad_auth
139 assert_response :success
140 assert_equal "application/json", response.media_type
143 js = ActiveSupport::JSON.decode(@response.body)
146 # check the data that is returned
147 check_json_details(js, user, false, false)
152 :description => "test",
153 :terms_agreed => Date.yesterday,
154 :home_lat => 12.1, :home_lon => 23.4,
155 :languages => ["en"])
156 create(:message, :read, :recipient => user)
157 create(:message, :sender => user)
159 # check that nothing is returned when not logged in
160 get api_user_details_path
161 assert_response :unauthorized
163 # check that we get a response when logged in
164 auth_header = bearer_authorization_header user
165 get api_user_details_path, :headers => auth_header
166 assert_response :success
167 assert_equal "application/xml", response.media_type
169 # check the data that is returned
170 check_xml_details(user, true, false)
172 # check that data is returned properly in json
173 auth_header = bearer_authorization_header user
174 get api_user_details_path(:format => "json"), :headers => auth_header
175 assert_response :success
176 assert_equal "application/json", response.media_type
179 js = ActiveSupport::JSON.decode(@response.body)
182 # check the data that is returned
183 check_json_details(js, user, true, false)
187 # an unexpected error while authenticating should be raised as it
188 # is, and not hidden by an error about authorization not being checked
189 def test_details_unexpected_error
190 auth_header = bearer_authorization_header
192 User.stub(:find, ->(*) { raise ActiveRecord::ConnectionNotEstablished, "database unavailable" }) do
193 assert_raises ActiveRecord::ConnectionNotEstablished do
194 get api_user_details_path, :headers => auth_header
199 def test_details_oauth2
201 :home_lat => 12.1, :home_lon => 23.4,
202 :languages => ["en"])
203 good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
204 bad_auth = bearer_authorization_header(user, :scopes => %w[])
205 email_auth = bearer_authorization_header(user, :scopes => %w[read_prefs read_email])
207 # check that we can't fetch details as XML without read_prefs
208 get api_user_details_path, :headers => bad_auth
209 assert_response :forbidden
211 # check that we can fetch details as XML without read_email
212 get api_user_details_path, :headers => good_auth
213 assert_response :success
214 assert_equal "application/xml", response.media_type
216 # check the data that is returned
217 check_xml_details(user, true, false)
219 # check that we can fetch details as XML with read_email
220 get api_user_details_path, :headers => email_auth
221 assert_response :success
222 assert_equal "application/xml", response.media_type
224 # check the data that is returned
225 check_xml_details(user, true, true)
227 # check that we can't fetch details as JSON without read_prefs
228 get api_user_details_path(:format => "json"), :headers => bad_auth
229 assert_response :forbidden
231 # check that we can fetch details as JSON without read_email
232 get api_user_details_path(:format => "json"), :headers => good_auth
233 assert_response :success
234 assert_equal "application/json", response.media_type
237 js = ActiveSupport::JSON.decode(@response.body)
240 # check the data that is returned
241 check_json_details(js, user, true, false)
243 # check that we can fetch details as JSON with read_email
244 get api_user_details_path(:format => "json"), :headers => email_auth
245 assert_response :success
246 assert_equal "application/json", response.media_type
249 js = ActiveSupport::JSON.decode(@response.body)
252 # check the data that is returned
253 check_json_details(js, user, true, true)
257 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
258 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
259 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
261 get api_users_path, :params => { :users => user1.id }
262 assert_response :success
263 assert_equal "application/xml", response.media_type
264 assert_select "user", :count => 1 do
265 check_xml_details(user1, false, false)
266 assert_select "user[id='#{user2.id}']", :count => 0
267 assert_select "user[id='#{user3.id}']", :count => 0
270 get api_users_path, :params => { :users => user2.id }
271 assert_response :success
272 assert_equal "application/xml", response.media_type
273 assert_select "user", :count => 1 do
274 assert_select "user[id='#{user1.id}']", :count => 0
275 check_xml_details(user2, false, false)
276 assert_select "user[id='#{user3.id}']", :count => 0
279 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }
280 assert_response :success
281 assert_equal "application/xml", response.media_type
282 assert_select "user", :count => 2 do
283 check_xml_details(user1, false, false)
284 assert_select "user[id='#{user2.id}']", :count => 0
285 check_xml_details(user3, false, false)
288 get api_users_path, :params => { :users => user1.id, :format => "json" }
289 assert_response :success
290 assert_equal "application/json", response.media_type
291 js = ActiveSupport::JSON.decode(@response.body)
293 assert_equal 1, js["users"].count
294 check_json_details(js["users"][0], user1, false, false)
296 get api_users_path, :params => { :users => user2.id, :format => "json" }
297 assert_response :success
298 assert_equal "application/json", response.media_type
299 js = ActiveSupport::JSON.decode(@response.body)
301 assert_equal 1, js["users"].count
302 check_json_details(js["users"][0], user2, false, false)
304 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }
305 assert_response :success
306 assert_equal "application/json", response.media_type
307 js = ActiveSupport::JSON.decode(@response.body)
309 assert_equal 2, js["users"].count
310 check_json_details(js["users"][0], user1, false, false)
311 check_json_details(js["users"][1], user3, false, false)
313 get api_users_path, :params => { :users => create(:user, :suspended).id }
314 assert_response :success
315 assert_equal "application/xml", response.media_type
316 assert_select "user", :count => 0
318 get api_users_path, :params => { :users => create(:user, :deleted).id }
319 assert_response :success
320 assert_equal "application/xml", response.media_type
321 assert_select "user", :count => 0
323 get api_users_path, :params => { :users => 0 }
324 assert_response :success
325 assert_equal "application/xml", response.media_type
326 assert_select "user", :count => 0
329 def test_index_oauth2
330 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
331 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
332 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
333 good_auth = bearer_authorization_header(user1, :scopes => %w[read_prefs])
334 bad_auth = bearer_authorization_header(user1, :scopes => %w[])
336 get api_users_path, :params => { :users => user1.id }, :headers => good_auth
337 assert_response :success
338 assert_equal "application/xml", response.media_type
339 assert_select "user", :count => 1 do
340 check_xml_details(user1, true, false)
341 assert_select "user[id='#{user2.id}']", :count => 0
342 assert_select "user[id='#{user3.id}']", :count => 0
345 get api_users_path, :params => { :users => user2.id }, :headers => good_auth
346 assert_response :success
347 assert_equal "application/xml", response.media_type
348 assert_select "user", :count => 1 do
349 assert_select "user[id='#{user1.id}']", :count => 0
350 check_xml_details(user2, false, false)
351 assert_select "user[id='#{user3.id}']", :count => 0
354 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => good_auth
355 assert_response :success
356 assert_equal "application/xml", response.media_type
357 assert_select "user", :count => 2 do
358 check_xml_details(user1, true, false)
359 assert_select "user[id='#{user2.id}']", :count => 0
360 check_xml_details(user3, false, false)
363 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bad_auth
364 assert_response :success
365 assert_equal "application/xml", response.media_type
366 assert_select "user", :count => 2 do
367 check_xml_details(user1, false, false)
368 assert_select "user[id='#{user2.id}']", :count => 0
369 check_xml_details(user3, false, false)
372 get api_users_path, :params => { :users => user1.id, :format => "json" }, :headers => good_auth
373 assert_response :success
374 assert_equal "application/json", response.media_type
375 js = ActiveSupport::JSON.decode(@response.body)
377 assert_equal 1, js["users"].count
378 check_json_details(js["users"][0], user1, true, false)
380 get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => good_auth
381 assert_response :success
382 assert_equal "application/json", response.media_type
383 js = ActiveSupport::JSON.decode(@response.body)
385 assert_equal 1, js["users"].count
386 check_json_details(js["users"][0], user2, false, false)
388 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => good_auth
389 assert_response :success
390 assert_equal "application/json", response.media_type
391 js = ActiveSupport::JSON.decode(@response.body)
393 assert_equal 2, js["users"].count
394 check_json_details(js["users"][0], user1, true, false)
395 check_json_details(js["users"][1], user3, false, false)
397 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bad_auth
398 assert_response :success
399 assert_equal "application/json", response.media_type
400 js = ActiveSupport::JSON.decode(@response.body)
402 assert_equal 2, js["users"].count
403 check_json_details(js["users"][0], user1, false, false)
404 check_json_details(js["users"][1], user3, false, false)
406 get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => good_auth
407 assert_response :success
408 assert_equal "application/xml", response.media_type
409 assert_select "user", :count => 0
411 get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => good_auth
412 assert_response :success
413 assert_equal "application/xml", response.media_type
414 assert_select "user", :count => 0
416 get api_users_path, :params => { :users => 0 }, :headers => good_auth
417 assert_response :success
418 assert_equal "application/xml", response.media_type
419 assert_select "user", :count => 0
424 def check_xml_details(user, include_private, include_email)
425 assert_select "user[id='#{user.id}']", :count => 1 do
426 assert_select "description", :count => 1, :text => user.description
427 assert_select "company", :count => 1, :text => user.company if user.company
429 if user.social_links.present?
430 assert_select "social-links link", :count => user.social_links.count do
431 user.social_links.each do |link|
432 details = link.parsed
433 assert_select "link[platform='#{details[:platform]}']", :count => 1, :text => details[:url]
437 assert_select "social-links link", :count => 0
440 assert_select "contributor-terms", :count => 1 do
441 if user.terms_agreed.present?
442 assert_select "[agreed='true']", :count => 1
444 assert_select "[agreed='false']", :count => 1
448 assert_select "[pd='false']", :count => 1
450 assert_select "[pd]", :count => 0
454 assert_select "img", :count => 0
456 assert_select "roles", :count => 1 do
457 assert_select "role", :count => 0
460 assert_select "changesets", :count => 1 do
461 assert_select "[count='0']", :count => 1
464 assert_select "traces", :count => 1 do
465 assert_select "[count='0']", :count => 1
468 assert_select "blocks", :count => 1 do
469 assert_select "received", :count => 1 do
470 assert_select "[count='0'][active='0']", :count => 1
473 assert_select "issued", :count => 0
476 if include_private && user.home_lat.present? && user.home_lon.present?
477 assert_select "home", :count => 1 do
478 assert_select "[lat='12.1'][lon='23.4'][zoom='3']", :count => 1
481 assert_select "home", :count => 0
485 assert_select "languages", :count => 1 do
486 assert_select "lang", :count => user.languages.count
488 user.languages.each do |language|
489 assert_select "lang", :count => 1, :text => language
493 assert_select "messages", :count => 1 do
494 assert_select "received", :count => 1 do
495 assert_select "[count='#{user.messages.count}'][unread='0']", :count => 1
498 assert_select "sent", :count => 1 do
499 assert_select "[count='#{user.sent_messages.count}']", :count => 1
503 assert_select "languages", :count => 0
504 assert_select "messages", :count => 0
508 assert_select "email", :count => 1, :text => user.email
510 assert_select "email", :count => 0
515 def check_json_details(js, user, include_private, include_email)
516 assert_equal user.id, js["user"]["id"]
517 assert_equal user.description, js["user"]["description"]
519 assert_equal user.company, js["user"]["company"]
521 assert_nil js["user"]["company"]
524 if user.social_links.present?
525 ordered_js_links = js.dig("user", "social_links").sort_by { |link| link["url"] }
526 ordered_user_links = user.social_links.sort_by(&:url).map(&:parsed)
528 assert_equal ordered_user_links.count, ordered_js_links.count
530 ordered_user_links.zip(ordered_js_links).each do |user_link, js_link|
531 assert_equal user_link[:url], js_link["url"]
532 assert_equal user_link[:platform], js_link["platform"]
535 assert_empty js["user"]["social_links"]
538 assert_operator js["user"]["contributor_terms"], :[], "agreed"
541 assert_not js["user"]["contributor_terms"]["pd"]
543 assert_nil js["user"]["contributor_terms"]["pd"]
546 assert_nil js["user"]["img"]
547 assert_empty js["user"]["roles"]
548 assert_equal 0, js["user"]["changesets"]["count"]
549 assert_equal 0, js["user"]["traces"]["count"]
550 assert_equal 0, js["user"]["blocks"]["received"]["count"]
551 assert_equal 0, js["user"]["blocks"]["received"]["active"]
552 assert_nil js["user"]["blocks"]["issued"]
554 if include_private && user.home_lat.present? && user.home_lon.present?
555 assert_in_delta 12.1, js["user"]["home"]["lat"]
556 assert_in_delta 23.4, js["user"]["home"]["lon"]
557 assert_equal 3, js["user"]["home"]["zoom"]
559 assert_nil js["user"]["home"]
562 if include_private && user.languages.present?
563 assert_equal user.languages, js["user"]["languages"]
565 assert_nil js["user"]["languages"]
569 assert_equal user.messages.count, js["user"]["messages"]["received"]["count"]
570 assert_equal 0, js["user"]["messages"]["received"]["unread"]
571 assert_equal user.sent_messages.count, js["user"]["messages"]["sent"]["count"]
573 assert_nil js["user"]["messages"]
577 assert_equal user.email, js["user"]["email"]
579 assert_nil js["user"]["email"]