4   class UsersControllerTest < ActionDispatch::IntegrationTest
 
   6     # test all routes which lead to this controller
 
   9         { :path => "/api/0.6/user/1", :method => :get },
 
  10         { :controller => "api/users", :action => "show", :id => "1" }
 
  13         { :path => "/api/0.6/user/1.json", :method => :get },
 
  14         { :controller => "api/users", :action => "show", :id => "1", :format => "json" }
 
  17         { :path => "/api/0.6/user/details", :method => :get },
 
  18         { :controller => "api/users", :action => "details" }
 
  21         { :path => "/api/0.6/user/details.json", :method => :get },
 
  22         { :controller => "api/users", :action => "details", :format => "json" }
 
  25         { :path => "/api/0.6/users", :method => :get },
 
  26         { :controller => "api/users", :action => "index" }
 
  29         { :path => "/api/0.6/users.json", :method => :get },
 
  30         { :controller => "api/users", :action => "index", :format => "json" }
 
  36                     :description => "test",
 
  37                     :terms_agreed => Date.yesterday,
 
  38                     :home_lat => 12.1, :home_lon => 23.4,
 
  41       # check that a visible user is returned properly
 
  42       get api_user_path(:id => user.id)
 
  43       assert_response :success
 
  44       assert_equal "application/xml", response.media_type
 
  46       # check the data that is returned
 
  47       check_xml_details(user, false, false)
 
  49       # check that a suspended user is not returned
 
  50       get api_user_path(:id => create(:user, :suspended).id)
 
  53       # check that a deleted user is not returned
 
  54       get api_user_path(:id => create(:user, :deleted).id)
 
  57       # check that a non-existent user is not returned
 
  58       get api_user_path(:id => 0)
 
  59       assert_response :not_found
 
  61       # check that a visible user is returned properly in json
 
  62       get api_user_path(:id => user.id, :format => "json")
 
  63       assert_response :success
 
  64       assert_equal "application/json", response.media_type
 
  67       js = ActiveSupport::JSON.decode(@response.body)
 
  70       # check the data that is returned
 
  71       check_json_details(js, user, false, false)
 
  76                     :home_lat => 12.1, :home_lon => 23.4,
 
  78       good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
 
  79       bad_auth = bearer_authorization_header(user, :scopes => %w[])
 
  80       other_user = create(:user,
 
  81                           :home_lat => 12.1, :home_lon => 23.4,
 
  84       # check that we can fetch our own details as XML with read_prefs
 
  85       get api_user_path(:id => user.id), :headers => good_auth
 
  86       assert_response :success
 
  87       assert_equal "application/xml", response.media_type
 
  89       # check the data that is returned
 
  90       check_xml_details(user, true, false)
 
  92       # check that we can fetch a different user's details as XML with read_prefs
 
  93       get api_user_path(:id => other_user.id), :headers => good_auth
 
  94       assert_response :success
 
  95       assert_equal "application/xml", response.media_type
 
  97       # check the data that is returned
 
  98       check_xml_details(other_user, false, false)
 
 100       # check that we can fetch our own details as XML without read_prefs
 
 101       get api_user_path(:id => user.id), :headers => bad_auth
 
 102       assert_response :success
 
 103       assert_equal "application/xml", response.media_type
 
 105       # check the data that is returned
 
 106       check_xml_details(user, false, false)
 
 108       # check that we can fetch our own details as JSON with read_prefs
 
 109       get api_user_path(:id => user.id, :format => "json"), :headers => good_auth
 
 110       assert_response :success
 
 111       assert_equal "application/json", response.media_type
 
 114       js = ActiveSupport::JSON.decode(@response.body)
 
 117       # check the data that is returned
 
 118       check_json_details(js, user, true, false)
 
 120       # check that we can fetch a different user's details as JSON with read_prefs
 
 121       get api_user_path(:id => other_user.id, :format => "json"), :headers => good_auth
 
 122       assert_response :success
 
 123       assert_equal "application/json", response.media_type
 
 126       js = ActiveSupport::JSON.decode(@response.body)
 
 129       # check the data that is returned
 
 130       check_json_details(js, other_user, false, false)
 
 132       # check that we can fetch our own details as JSON without read_prefs
 
 133       get api_user_path(:id => user.id, :format => "json"), :headers => bad_auth
 
 134       assert_response :success
 
 135       assert_equal "application/json", response.media_type
 
 138       js = ActiveSupport::JSON.decode(@response.body)
 
 141       # check the data that is returned
 
 142       check_json_details(js, user, false, false)
 
 147                     :description => "test",
 
 148                     :terms_agreed => Date.yesterday,
 
 149                     :home_lat => 12.1, :home_lon => 23.4,
 
 150                     :languages => ["en"])
 
 151       create(:message, :read, :recipient => user)
 
 152       create(:message, :sender => user)
 
 154       # check that nothing is returned when not logged in
 
 155       get api_user_details_path
 
 156       assert_response :unauthorized
 
 158       # check that we get a response when logged in
 
 159       auth_header = bearer_authorization_header user
 
 160       get api_user_details_path, :headers => auth_header
 
 161       assert_response :success
 
 162       assert_equal "application/xml", response.media_type
 
 164       # check the data that is returned
 
 165       check_xml_details(user, true, false)
 
 167       # check that data is returned properly in json
 
 168       auth_header = bearer_authorization_header user
 
 169       get api_user_details_path(:format => "json"), :headers => auth_header
 
 170       assert_response :success
 
 171       assert_equal "application/json", response.media_type
 
 174       js = ActiveSupport::JSON.decode(@response.body)
 
 177       # check the data that is returned
 
 178       check_json_details(js, user, true, false)
 
 181     def test_details_oauth2
 
 183                     :home_lat => 12.1, :home_lon => 23.4,
 
 184                     :languages => ["en"])
 
 185       good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs])
 
 186       bad_auth = bearer_authorization_header(user, :scopes => %w[])
 
 187       email_auth = bearer_authorization_header(user, :scopes => %w[read_prefs read_email])
 
 189       # check that we can't fetch details as XML without read_prefs
 
 190       get api_user_details_path, :headers => bad_auth
 
 191       assert_response :forbidden
 
 193       # check that we can fetch details as XML without read_email
 
 194       get api_user_details_path, :headers => good_auth
 
 195       assert_response :success
 
 196       assert_equal "application/xml", response.media_type
 
 198       # check the data that is returned
 
 199       check_xml_details(user, true, false)
 
 201       # check that we can fetch details as XML with read_email
 
 202       get api_user_details_path, :headers => email_auth
 
 203       assert_response :success
 
 204       assert_equal "application/xml", response.media_type
 
 206       # check the data that is returned
 
 207       check_xml_details(user, true, true)
 
 209       # check that we can't fetch details as JSON without read_prefs
 
 210       get api_user_details_path(:format => "json"), :headers => bad_auth
 
 211       assert_response :forbidden
 
 213       # check that we can fetch details as JSON without read_email
 
 214       get api_user_details_path(:format => "json"), :headers => good_auth
 
 215       assert_response :success
 
 216       assert_equal "application/json", response.media_type
 
 219       js = ActiveSupport::JSON.decode(@response.body)
 
 222       # check the data that is returned
 
 223       check_json_details(js, user, true, false)
 
 225       # check that we can fetch details as JSON with read_email
 
 226       get api_user_details_path(:format => "json"), :headers => email_auth
 
 227       assert_response :success
 
 228       assert_equal "application/json", response.media_type
 
 231       js = ActiveSupport::JSON.decode(@response.body)
 
 234       # check the data that is returned
 
 235       check_json_details(js, user, true, true)
 
 239       user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
 
 240       user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
 
 241       user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
 
 243       get api_users_path, :params => { :users => user1.id }
 
 244       assert_response :success
 
 245       assert_equal "application/xml", response.media_type
 
 246       assert_select "user", :count => 1 do
 
 247         check_xml_details(user1, false, false)
 
 248         assert_select "user[id='#{user2.id}']", :count => 0
 
 249         assert_select "user[id='#{user3.id}']", :count => 0
 
 252       get api_users_path, :params => { :users => user2.id }
 
 253       assert_response :success
 
 254       assert_equal "application/xml", response.media_type
 
 255       assert_select "user", :count => 1 do
 
 256         assert_select "user[id='#{user1.id}']", :count => 0
 
 257         check_xml_details(user2, false, false)
 
 258         assert_select "user[id='#{user3.id}']", :count => 0
 
 261       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }
 
 262       assert_response :success
 
 263       assert_equal "application/xml", response.media_type
 
 264       assert_select "user", :count => 2 do
 
 265         check_xml_details(user1, false, false)
 
 266         assert_select "user[id='#{user2.id}']", :count => 0
 
 267         check_xml_details(user3, false, false)
 
 270       get api_users_path, :params => { :users => user1.id, :format => "json" }
 
 271       assert_response :success
 
 272       assert_equal "application/json", response.media_type
 
 273       js = ActiveSupport::JSON.decode(@response.body)
 
 275       assert_equal 1, js["users"].count
 
 276       check_json_details(js["users"][0], user1, false, false)
 
 278       get api_users_path, :params => { :users => user2.id, :format => "json" }
 
 279       assert_response :success
 
 280       assert_equal "application/json", response.media_type
 
 281       js = ActiveSupport::JSON.decode(@response.body)
 
 283       assert_equal 1, js["users"].count
 
 284       check_json_details(js["users"][0], user2, false, false)
 
 286       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }
 
 287       assert_response :success
 
 288       assert_equal "application/json", response.media_type
 
 289       js = ActiveSupport::JSON.decode(@response.body)
 
 291       assert_equal 2, js["users"].count
 
 292       check_json_details(js["users"][0], user1, false, false)
 
 293       check_json_details(js["users"][1], user3, false, false)
 
 295       get api_users_path, :params => { :users => create(:user, :suspended).id }
 
 296       assert_response :success
 
 297       assert_equal "application/xml", response.media_type
 
 298       assert_select "user", :count => 0
 
 300       get api_users_path, :params => { :users => create(:user, :deleted).id }
 
 301       assert_response :success
 
 302       assert_equal "application/xml", response.media_type
 
 303       assert_select "user", :count => 0
 
 305       get api_users_path, :params => { :users => 0 }
 
 306       assert_response :success
 
 307       assert_equal "application/xml", response.media_type
 
 308       assert_select "user", :count => 0
 
 311     def test_index_oauth2
 
 312       user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday)
 
 313       user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday)
 
 314       user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday)
 
 315       good_auth = bearer_authorization_header(user1, :scopes => %w[read_prefs])
 
 316       bad_auth = bearer_authorization_header(user1, :scopes => %w[])
 
 318       get api_users_path, :params => { :users => user1.id }, :headers => good_auth
 
 319       assert_response :success
 
 320       assert_equal "application/xml", response.media_type
 
 321       assert_select "user", :count => 1 do
 
 322         check_xml_details(user1, true, false)
 
 323         assert_select "user[id='#{user2.id}']", :count => 0
 
 324         assert_select "user[id='#{user3.id}']", :count => 0
 
 327       get api_users_path, :params => { :users => user2.id }, :headers => good_auth
 
 328       assert_response :success
 
 329       assert_equal "application/xml", response.media_type
 
 330       assert_select "user", :count => 1 do
 
 331         assert_select "user[id='#{user1.id}']", :count => 0
 
 332         check_xml_details(user2, false, false)
 
 333         assert_select "user[id='#{user3.id}']", :count => 0
 
 336       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => good_auth
 
 337       assert_response :success
 
 338       assert_equal "application/xml", response.media_type
 
 339       assert_select "user", :count => 2 do
 
 340         check_xml_details(user1, true, false)
 
 341         assert_select "user[id='#{user2.id}']", :count => 0
 
 342         check_xml_details(user3, false, false)
 
 345       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bad_auth
 
 346       assert_response :success
 
 347       assert_equal "application/xml", response.media_type
 
 348       assert_select "user", :count => 2 do
 
 349         check_xml_details(user1, false, false)
 
 350         assert_select "user[id='#{user2.id}']", :count => 0
 
 351         check_xml_details(user3, false, false)
 
 354       get api_users_path, :params => { :users => user1.id, :format => "json" }, :headers => good_auth
 
 355       assert_response :success
 
 356       assert_equal "application/json", response.media_type
 
 357       js = ActiveSupport::JSON.decode(@response.body)
 
 359       assert_equal 1, js["users"].count
 
 360       check_json_details(js["users"][0], user1, true, false)
 
 362       get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => good_auth
 
 363       assert_response :success
 
 364       assert_equal "application/json", response.media_type
 
 365       js = ActiveSupport::JSON.decode(@response.body)
 
 367       assert_equal 1, js["users"].count
 
 368       check_json_details(js["users"][0], user2, false, false)
 
 370       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => good_auth
 
 371       assert_response :success
 
 372       assert_equal "application/json", response.media_type
 
 373       js = ActiveSupport::JSON.decode(@response.body)
 
 375       assert_equal 2, js["users"].count
 
 376       check_json_details(js["users"][0], user1, true, false)
 
 377       check_json_details(js["users"][1], user3, false, false)
 
 379       get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bad_auth
 
 380       assert_response :success
 
 381       assert_equal "application/json", response.media_type
 
 382       js = ActiveSupport::JSON.decode(@response.body)
 
 384       assert_equal 2, js["users"].count
 
 385       check_json_details(js["users"][0], user1, false, false)
 
 386       check_json_details(js["users"][1], user3, false, false)
 
 388       get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => good_auth
 
 389       assert_response :success
 
 390       assert_equal "application/xml", response.media_type
 
 391       assert_select "user", :count => 0
 
 393       get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => good_auth
 
 394       assert_response :success
 
 395       assert_equal "application/xml", response.media_type
 
 396       assert_select "user", :count => 0
 
 398       get api_users_path, :params => { :users => 0 }, :headers => good_auth
 
 399       assert_response :success
 
 400       assert_equal "application/xml", response.media_type
 
 401       assert_select "user", :count => 0
 
 406     def check_xml_details(user, include_private, include_email)
 
 407       assert_select "user[id='#{user.id}']", :count => 1 do
 
 408         assert_select "description", :count => 1, :text => user.description
 
 410         assert_select "contributor-terms", :count => 1 do
 
 411           if user.terms_agreed.present?
 
 412             assert_select "[agreed='true']", :count => 1
 
 414             assert_select "[agreed='false']", :count => 1
 
 418             assert_select "[pd='false']", :count => 1
 
 420             assert_select "[pd]", :count => 0
 
 424         assert_select "img", :count => 0
 
 426         assert_select "roles", :count => 1 do
 
 427           assert_select "role", :count => 0
 
 430         assert_select "changesets", :count => 1 do
 
 431           assert_select "[count='0']", :count => 1
 
 434         assert_select "traces", :count => 1 do
 
 435           assert_select "[count='0']", :count => 1
 
 438         assert_select "blocks", :count => 1 do
 
 439           assert_select "received", :count => 1 do
 
 440             assert_select "[count='0'][active='0']", :count => 1
 
 443           assert_select "issued", :count => 0
 
 446         if include_private && user.home_lat.present? && user.home_lon.present?
 
 447           assert_select "home", :count => 1 do
 
 448             assert_select "[lat='12.1'][lon='23.4'][zoom='3']", :count => 1
 
 451           assert_select "home", :count => 0
 
 455           assert_select "languages", :count => 1 do
 
 456             assert_select "lang", :count => user.languages.count
 
 458             user.languages.each do |language|
 
 459               assert_select "lang", :count => 1, :text => language
 
 463           assert_select "messages", :count => 1 do
 
 464             assert_select "received", :count => 1 do
 
 465               assert_select "[count='#{user.messages.count}'][unread='0']", :count => 1
 
 468             assert_select "sent", :count => 1 do
 
 469               assert_select "[count='#{user.sent_messages.count}']", :count => 1
 
 473           assert_select "languages", :count => 0
 
 474           assert_select "messages", :count => 0
 
 478           assert_select "email", :count => 1, :text => user.email
 
 480           assert_select "email", :count => 0
 
 485     def check_json_details(js, user, include_private, include_email)
 
 486       assert_equal user.id, js["user"]["id"]
 
 487       assert_equal user.description, js["user"]["description"]
 
 488       assert_operator js["user"]["contributor_terms"], :[], "agreed"
 
 491         assert_not js["user"]["contributor_terms"]["pd"]
 
 493         assert_nil js["user"]["contributor_terms"]["pd"]
 
 496       assert_nil js["user"]["img"]
 
 497       assert_empty js["user"]["roles"]
 
 498       assert_equal 0, js["user"]["changesets"]["count"]
 
 499       assert_equal 0, js["user"]["traces"]["count"]
 
 500       assert_equal 0, js["user"]["blocks"]["received"]["count"]
 
 501       assert_equal 0, js["user"]["blocks"]["received"]["active"]
 
 502       assert_nil js["user"]["blocks"]["issued"]
 
 504       if include_private && user.home_lat.present? && user.home_lon.present?
 
 505         assert_in_delta 12.1, js["user"]["home"]["lat"]
 
 506         assert_in_delta 23.4, js["user"]["home"]["lon"]
 
 507         assert_equal 3, js["user"]["home"]["zoom"]
 
 509         assert_nil js["user"]["home"]
 
 512       if include_private && user.languages.present?
 
 513         assert_equal user.languages, js["user"]["languages"]
 
 515         assert_nil js["user"]["languages"]
 
 519         assert_equal user.messages.count, js["user"]["messages"]["received"]["count"]
 
 520         assert_equal 0, js["user"]["messages"]["received"]["unread"]
 
 521         assert_equal user.sent_messages.count, js["user"]["messages"]["sent"]["count"]
 
 523         assert_nil js["user"]["messages"]
 
 527         assert_equal user.email, js["user"]["email"]
 
 529         assert_nil js["user"]["email"]