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,
43 # check that a visible user is returned properly
44 get api_user_path(:id => user.id)
45 assert_response :success
46 assert_equal "application/xml", response.media_type
48 # check the data that is returned
49 check_xml_details(user, false, false)
51 # check that a suspended user is not returned
52 get api_user_path(:id => create(:user, :suspended).id)
55 # check that a deleted user is not returned
56 get api_user_path(:id => create(:user, :deleted).id)
59 # check that a non-existent user is not returned
60 get api_user_path(:id => 0)
61 assert_response :not_found
63 # check that a visible user is returned properly in json
64 get api_user_path(:id => user.id, :format => "json")
65 assert_response :success
66 assert_equal "application/json", response.media_type
69 js = ActiveSupport::JSON.decode(@response.body)
72 # check the data that is returned
73 check_json_details(js, user, false, false)
78 :home_lat => 12.1, :home_lon => 23.4,
80 good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
81 bad_auth = bearer_authorization_header(user, :scopes => %w[])
82 other_user = create(:user,
83 :home_lat => 12.1, :home_lon => 23.4,
86 # check that we can fetch our own details as XML with read_prefs
87 get api_user_path(:id => user.id), :headers => good_auth
88 assert_response :success
89 assert_equal "application/xml", response.media_type
91 # check the data that is returned
92 check_xml_details(user, true, false)
94 # check that we can fetch a different user's details as XML with read_prefs
95 get api_user_path(:id => other_user.id), :headers => good_auth
96 assert_response :success
97 assert_equal "application/xml", response.media_type
99 # check the data that is returned
100 check_xml_details(other_user, false, false)
102 # check that we can fetch our own details as XML without read_prefs
103 get api_user_path(:id => user.id), :headers => bad_auth
104 assert_response :success
105 assert_equal "application/xml", response.media_type
107 # check the data that is returned
108 check_xml_details(user, false, false)
110 # check that we can fetch our own details as JSON with read_prefs
111 get api_user_path(:id => user.id, :format => "json"), :headers => good_auth
112 assert_response :success
113 assert_equal "application/json", response.media_type
116 js = ActiveSupport::JSON.decode(@response.body)
119 # check the data that is returned
120 check_json_details(js, user, true, false)
122 # check that we can fetch a different user's details as JSON with read_prefs
123 get api_user_path(:id => other_user.id, :format => "json"), :headers => good_auth
124 assert_response :success
125 assert_equal "application/json", response.media_type
128 js = ActiveSupport::JSON.decode(@response.body)
131 # check the data that is returned
132 check_json_details(js, other_user, false, false)
134 # check that we can fetch our own details as JSON without read_prefs
135 get api_user_path(:id => user.id, :format => "json"), :headers => bad_auth
136 assert_response :success
137 assert_equal "application/json", response.media_type
140 js = ActiveSupport::JSON.decode(@response.body)
143 # check the data that is returned
144 check_json_details(js, user, false, false)
149 :description => "test",
150 :terms_agreed => Date.yesterday,
151 :home_lat => 12.1, :home_lon => 23.4,
152 :languages => ["en"])
153 create(:message, :read, :recipient => user)
154 create(:message, :sender => user)
156 # check that nothing is returned when not logged in
157 get api_user_details_path
158 assert_response :unauthorized
160 # check that we get a response when logged in
161 auth_header = bearer_authorization_header user
162 get api_user_details_path, :headers => auth_header
163 assert_response :success
164 assert_equal "application/xml", response.media_type
166 # check the data that is returned
167 check_xml_details(user, true, false)
169 # check that data is returned properly in json
170 auth_header = bearer_authorization_header user
171 get api_user_details_path(:format => "json"), :headers => auth_header
172 assert_response :success
173 assert_equal "application/json", response.media_type
176 js = ActiveSupport::JSON.decode(@response.body)
179 # check the data that is returned
180 check_json_details(js, user, true, false)
183 def test_details_oauth2
185 :home_lat => 12.1, :home_lon => 23.4,
186 :languages => ["en"])
187 good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
188 bad_auth = bearer_authorization_header(user, :scopes => %w[])
189 email_auth = bearer_authorization_header(user, :scopes => %w[read_prefs read_email])
191 # check that we can't fetch details as XML without read_prefs
192 get api_user_details_path, :headers => bad_auth
193 assert_response :forbidden
195 # check that we can fetch details as XML without read_email
196 get api_user_details_path, :headers => good_auth
197 assert_response :success
198 assert_equal "application/xml", response.media_type
200 # check the data that is returned
201 check_xml_details(user, true, false)
203 # check that we can fetch details as XML with read_email
204 get api_user_details_path, :headers => email_auth
205 assert_response :success
206 assert_equal "application/xml", response.media_type
208 # check the data that is returned
209 check_xml_details(user, true, true)
211 # check that we can't fetch details as JSON without read_prefs
212 get api_user_details_path(:format => "json"), :headers => bad_auth
213 assert_response :forbidden
215 # check that we can fetch details as JSON without read_email
216 get api_user_details_path(:format => "json"), :headers => good_auth
217 assert_response :success
218 assert_equal "application/json", response.media_type
221 js = ActiveSupport::JSON.decode(@response.body)
224 # check the data that is returned
225 check_json_details(js, user, true, false)
227 # check that we can fetch details as JSON with read_email
228 get api_user_details_path(:format => "json"), :headers => email_auth
229 assert_response :success
230 assert_equal "application/json", response.media_type
233 js = ActiveSupport::JSON.decode(@response.body)
236 # check the data that is returned
237 check_json_details(js, user, true, true)
241 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
242 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
243 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
245 get api_users_path, :params => { :users => user1.id }
246 assert_response :success
247 assert_equal "application/xml", response.media_type
248 assert_select "user", :count => 1 do
249 check_xml_details(user1, false, false)
250 assert_select "user[id='#{user2.id}']", :count => 0
251 assert_select "user[id='#{user3.id}']", :count => 0
254 get api_users_path, :params => { :users => user2.id }
255 assert_response :success
256 assert_equal "application/xml", response.media_type
257 assert_select "user", :count => 1 do
258 assert_select "user[id='#{user1.id}']", :count => 0
259 check_xml_details(user2, false, false)
260 assert_select "user[id='#{user3.id}']", :count => 0
263 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }
264 assert_response :success
265 assert_equal "application/xml", response.media_type
266 assert_select "user", :count => 2 do
267 check_xml_details(user1, false, false)
268 assert_select "user[id='#{user2.id}']", :count => 0
269 check_xml_details(user3, false, false)
272 get api_users_path, :params => { :users => user1.id, :format => "json" }
273 assert_response :success
274 assert_equal "application/json", response.media_type
275 js = ActiveSupport::JSON.decode(@response.body)
277 assert_equal 1, js["users"].count
278 check_json_details(js["users"][0], user1, false, false)
280 get api_users_path, :params => { :users => user2.id, :format => "json" }
281 assert_response :success
282 assert_equal "application/json", response.media_type
283 js = ActiveSupport::JSON.decode(@response.body)
285 assert_equal 1, js["users"].count
286 check_json_details(js["users"][0], user2, false, false)
288 get api_users_path, :params => { :users => "#{user1.id},#{user3.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 2, js["users"].count
294 check_json_details(js["users"][0], user1, false, false)
295 check_json_details(js["users"][1], user3, false, false)
297 get api_users_path, :params => { :users => create(:user, :suspended).id }
298 assert_response :success
299 assert_equal "application/xml", response.media_type
300 assert_select "user", :count => 0
302 get api_users_path, :params => { :users => create(:user, :deleted).id }
303 assert_response :success
304 assert_equal "application/xml", response.media_type
305 assert_select "user", :count => 0
307 get api_users_path, :params => { :users => 0 }
308 assert_response :success
309 assert_equal "application/xml", response.media_type
310 assert_select "user", :count => 0
313 def test_index_oauth2
314 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
315 user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
316 user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
317 good_auth = bearer_authorization_header(user1, :scopes => %w[read_prefs])
318 bad_auth = bearer_authorization_header(user1, :scopes => %w[])
320 get api_users_path, :params => { :users => user1.id }, :headers => good_auth
321 assert_response :success
322 assert_equal "application/xml", response.media_type
323 assert_select "user", :count => 1 do
324 check_xml_details(user1, true, false)
325 assert_select "user[id='#{user2.id}']", :count => 0
326 assert_select "user[id='#{user3.id}']", :count => 0
329 get api_users_path, :params => { :users => user2.id }, :headers => good_auth
330 assert_response :success
331 assert_equal "application/xml", response.media_type
332 assert_select "user", :count => 1 do
333 assert_select "user[id='#{user1.id}']", :count => 0
334 check_xml_details(user2, false, false)
335 assert_select "user[id='#{user3.id}']", :count => 0
338 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => good_auth
339 assert_response :success
340 assert_equal "application/xml", response.media_type
341 assert_select "user", :count => 2 do
342 check_xml_details(user1, true, false)
343 assert_select "user[id='#{user2.id}']", :count => 0
344 check_xml_details(user3, false, false)
347 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bad_auth
348 assert_response :success
349 assert_equal "application/xml", response.media_type
350 assert_select "user", :count => 2 do
351 check_xml_details(user1, false, false)
352 assert_select "user[id='#{user2.id}']", :count => 0
353 check_xml_details(user3, false, false)
356 get api_users_path, :params => { :users => user1.id, :format => "json" }, :headers => good_auth
357 assert_response :success
358 assert_equal "application/json", response.media_type
359 js = ActiveSupport::JSON.decode(@response.body)
361 assert_equal 1, js["users"].count
362 check_json_details(js["users"][0], user1, true, false)
364 get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => good_auth
365 assert_response :success
366 assert_equal "application/json", response.media_type
367 js = ActiveSupport::JSON.decode(@response.body)
369 assert_equal 1, js["users"].count
370 check_json_details(js["users"][0], user2, false, false)
372 get api_users_path, :params => { :users => "#{user1.id},#{user3.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 2, js["users"].count
378 check_json_details(js["users"][0], user1, true, false)
379 check_json_details(js["users"][1], user3, false, false)
381 get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bad_auth
382 assert_response :success
383 assert_equal "application/json", response.media_type
384 js = ActiveSupport::JSON.decode(@response.body)
386 assert_equal 2, js["users"].count
387 check_json_details(js["users"][0], user1, false, false)
388 check_json_details(js["users"][1], user3, false, false)
390 get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => good_auth
391 assert_response :success
392 assert_equal "application/xml", response.media_type
393 assert_select "user", :count => 0
395 get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => good_auth
396 assert_response :success
397 assert_equal "application/xml", response.media_type
398 assert_select "user", :count => 0
400 get api_users_path, :params => { :users => 0 }, :headers => good_auth
401 assert_response :success
402 assert_equal "application/xml", response.media_type
403 assert_select "user", :count => 0
408 def check_xml_details(user, include_private, include_email)
409 assert_select "user[id='#{user.id}']", :count => 1 do
410 assert_select "description", :count => 1, :text => user.description
412 assert_select "contributor-terms", :count => 1 do
413 if user.terms_agreed.present?
414 assert_select "[agreed='true']", :count => 1
416 assert_select "[agreed='false']", :count => 1
420 assert_select "[pd='false']", :count => 1
422 assert_select "[pd]", :count => 0
426 assert_select "img", :count => 0
428 assert_select "roles", :count => 1 do
429 assert_select "role", :count => 0
432 assert_select "changesets", :count => 1 do
433 assert_select "[count='0']", :count => 1
436 assert_select "traces", :count => 1 do
437 assert_select "[count='0']", :count => 1
440 assert_select "blocks", :count => 1 do
441 assert_select "received", :count => 1 do
442 assert_select "[count='0'][active='0']", :count => 1
445 assert_select "issued", :count => 0
448 if include_private && user.home_lat.present? && user.home_lon.present?
449 assert_select "home", :count => 1 do
450 assert_select "[lat='12.1'][lon='23.4'][zoom='3']", :count => 1
453 assert_select "home", :count => 0
457 assert_select "languages", :count => 1 do
458 assert_select "lang", :count => user.languages.count
460 user.languages.each do |language|
461 assert_select "lang", :count => 1, :text => language
465 assert_select "messages", :count => 1 do
466 assert_select "received", :count => 1 do
467 assert_select "[count='#{user.messages.count}'][unread='0']", :count => 1
470 assert_select "sent", :count => 1 do
471 assert_select "[count='#{user.sent_messages.count}']", :count => 1
475 assert_select "languages", :count => 0
476 assert_select "messages", :count => 0
480 assert_select "email", :count => 1, :text => user.email
482 assert_select "email", :count => 0
487 def check_json_details(js, user, include_private, include_email)
488 assert_equal user.id, js["user"]["id"]
489 assert_equal user.description, js["user"]["description"]
490 assert_operator js["user"]["contributor_terms"], :[], "agreed"
493 assert_not js["user"]["contributor_terms"]["pd"]
495 assert_nil js["user"]["contributor_terms"]["pd"]
498 assert_nil js["user"]["img"]
499 assert_empty js["user"]["roles"]
500 assert_equal 0, js["user"]["changesets"]["count"]
501 assert_equal 0, js["user"]["traces"]["count"]
502 assert_equal 0, js["user"]["blocks"]["received"]["count"]
503 assert_equal 0, js["user"]["blocks"]["received"]["active"]
504 assert_nil js["user"]["blocks"]["issued"]
506 if include_private && user.home_lat.present? && user.home_lon.present?
507 assert_in_delta 12.1, js["user"]["home"]["lat"]
508 assert_in_delta 23.4, js["user"]["home"]["lon"]
509 assert_equal 3, js["user"]["home"]["zoom"]
511 assert_nil js["user"]["home"]
514 if include_private && user.languages.present?
515 assert_equal user.languages, js["user"]["languages"]
517 assert_nil js["user"]["languages"]
521 assert_equal user.messages.count, js["user"]["messages"]["received"]["count"]
522 assert_equal 0, js["user"]["messages"]["received"]["unread"]
523 assert_equal user.sent_messages.count, js["user"]["messages"]["sent"]["count"]
525 assert_nil js["user"]["messages"]
529 assert_equal user.email, js["user"]["email"]
531 assert_nil js["user"]["email"]