X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/0eb2afa83aee217fc2c6399bd23d0d8152c09eb5..fb7762feb979bf7d106d7e87785d6a39cb540405:/test/controllers/api/users_controller_test.rb diff --git a/test/controllers/api/users_controller_test.rb b/test/controllers/api/users_controller_test.rb index cb2c75205..90ef1999d 100644 --- a/test/controllers/api/users_controller_test.rb +++ b/test/controllers/api/users_controller_test.rb @@ -21,10 +21,6 @@ module Api { :path => "/api/0.6/user/details.json", :method => :get }, { :controller => "api/users", :action => "details", :format => "json" } ) - assert_routing( - { :path => "/api/0.6/user/gpx_files", :method => :get }, - { :controller => "api/users", :action => "gpx_files" } - ) assert_routing( { :path => "/api/0.6/users", :method => :get }, { :controller => "api/users", :action => "index" } @@ -75,96 +71,18 @@ module Api check_json_details(js, user, false, false) end - def test_show_oauth1 - user = create(:user, - :home_lat => 12.1, :home_lon => 23.4, - :languages => ["en"]) - good_token = create(:access_token, - :user => user, - :allow_read_prefs => true) - bad_token = create(:access_token, - :user => user) - other_user = create(:user, - :home_lat => 12.1, :home_lon => 23.4, - :languages => ["en"]) - - # check that we can fetch our own details as XML with read_prefs - signed_get api_user_path(:id => user.id), :oauth => { :token => good_token } - assert_response :success - assert_equal "application/xml", response.media_type - - # check the data that is returned - check_xml_details(user, true, false) - - # check that we can fetch a different user's details as XML with read_prefs - signed_get api_user_path(:id => other_user.id), :oauth => { :token => good_token } - assert_response :success - assert_equal "application/xml", response.media_type - - # check the data that is returned - check_xml_details(other_user, false, false) - - # check that we can fetch our own details as XML without read_prefs - signed_get api_user_path(:id => user.id), :oauth => { :token => bad_token } - assert_response :success - assert_equal "application/xml", response.media_type - - # check the data that is returned - check_xml_details(user, false, false) - - # check that we can fetch our own details as JSON with read_prefs - signed_get api_user_path(:id => user.id, :format => "json"), :oauth => { :token => good_token } - assert_response :success - assert_equal "application/json", response.media_type - - # parse the response - js = ActiveSupport::JSON.decode(@response.body) - assert_not_nil js - - # check the data that is returned - check_json_details(js, user, true, false) - - # check that we can fetch a different user's details as JSON with read_prefs - signed_get api_user_path(:id => other_user.id, :format => "json"), :oauth => { :token => good_token } - assert_response :success - assert_equal "application/json", response.media_type - - # parse the response - js = ActiveSupport::JSON.decode(@response.body) - assert_not_nil js - - # check the data that is returned - check_json_details(js, other_user, false, false) - - # check that we can fetch our own details as JSON without read_prefs - signed_get api_user_path(:id => other_user.id, :format => "json"), :oauth => { :token => bad_token } - assert_response :success - assert_equal "application/json", response.media_type - - # parse the response - js = ActiveSupport::JSON.decode(@response.body) - assert_not_nil js - - # check the data that is returned - check_json_details(js, other_user, false, false) - end - def test_show_oauth2 user = create(:user, :home_lat => 12.1, :home_lon => 23.4, :languages => ["en"]) - good_token = create(:oauth_access_token, - :resource_owner_id => user.id, - :scopes => %w[read_prefs]) - bad_token = create(:oauth_access_token, - :resource_owner_id => user.id, - :scopes => %w[]) + good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs]) + bad_auth = bearer_authorization_header(user, :scopes => %w[]) other_user = create(:user, :home_lat => 12.1, :home_lon => 23.4, :languages => ["en"]) # check that we can fetch our own details as XML with read_prefs - get api_user_path(:id => user.id), :headers => bearer_authorization_header(good_token.token) + get api_user_path(:id => user.id), :headers => good_auth assert_response :success assert_equal "application/xml", response.media_type @@ -172,7 +90,7 @@ module Api check_xml_details(user, true, false) # check that we can fetch a different user's details as XML with read_prefs - get api_user_path(:id => other_user.id), :headers => bearer_authorization_header(good_token.token) + get api_user_path(:id => other_user.id), :headers => good_auth assert_response :success assert_equal "application/xml", response.media_type @@ -180,7 +98,7 @@ module Api check_xml_details(other_user, false, false) # check that we can fetch our own details as XML without read_prefs - get api_user_path(:id => user.id), :headers => bearer_authorization_header(bad_token.token) + get api_user_path(:id => user.id), :headers => bad_auth assert_response :success assert_equal "application/xml", response.media_type @@ -188,7 +106,7 @@ module Api check_xml_details(user, false, false) # check that we can fetch our own details as JSON with read_prefs - get api_user_path(:id => user.id, :format => "json"), :headers => bearer_authorization_header(good_token.token) + get api_user_path(:id => user.id, :format => "json"), :headers => good_auth assert_response :success assert_equal "application/json", response.media_type @@ -200,7 +118,7 @@ module Api check_json_details(js, user, true, false) # check that we can fetch a different user's details as JSON with read_prefs - get api_user_path(:id => other_user.id, :format => "json"), :headers => bearer_authorization_header(good_token.token) + get api_user_path(:id => other_user.id, :format => "json"), :headers => good_auth assert_response :success assert_equal "application/json", response.media_type @@ -212,7 +130,7 @@ module Api check_json_details(js, other_user, false, false) # check that we can fetch our own details as JSON without read_prefs - get api_user_path(:id => user.id, :format => "json"), :headers => bearer_authorization_header(bad_token.token) + get api_user_path(:id => user.id, :format => "json"), :headers => bad_auth assert_response :success assert_equal "application/json", response.media_type @@ -234,12 +152,12 @@ module Api create(:message, :sender => user) # check that nothing is returned when not logged in - get user_details_path + get api_user_details_path assert_response :unauthorized # check that we get a response when logged in - auth_header = basic_authorization_header user.email, "test" - get user_details_path, :headers => auth_header + auth_header = bearer_authorization_header user + get api_user_details_path, :headers => auth_header assert_response :success assert_equal "application/xml", response.media_type @@ -247,47 +165,8 @@ module Api check_xml_details(user, true, false) # check that data is returned properly in json - auth_header = basic_authorization_header user.email, "test" - get user_details_path(:format => "json"), :headers => auth_header - assert_response :success - assert_equal "application/json", response.media_type - - # parse the response - js = ActiveSupport::JSON.decode(@response.body) - assert_not_nil js - - # check the data that is returned - check_json_details(js, user, true, false) - end - - def test_details_oauth1 - user = create(:user, - :home_lat => 12.1, :home_lon => 23.4, - :languages => ["en"]) - good_token = create(:access_token, - :user => user, - :allow_read_prefs => true) - bad_token = create(:access_token, - :user => user) - - # check that we can't fetch details as XML without read_prefs - signed_get user_details_path, :oauth => { :token => bad_token } - assert_response :forbidden - - # check that we can fetch details as XML - signed_get user_details_path, :oauth => { :token => good_token } - assert_response :success - assert_equal "application/xml", response.media_type - - # check the data that is returned - check_xml_details(user, true, false) - - # check that we can't fetch details as JSON without read_prefs - signed_get user_details_path(:format => "json"), :oauth => { :token => bad_token } - assert_response :forbidden - - # check that we can fetch details as JSON - signed_get user_details_path(:format => "json"), :oauth => { :token => good_token } + auth_header = bearer_authorization_header user + get api_user_details_path(:format => "json"), :headers => auth_header assert_response :success assert_equal "application/json", response.media_type @@ -303,21 +182,16 @@ module Api user = create(:user, :home_lat => 12.1, :home_lon => 23.4, :languages => ["en"]) - good_token = create(:oauth_access_token, - :resource_owner_id => user.id, - :scopes => %w[read_prefs]) - bad_token = create(:oauth_access_token, - :resource_owner_id => user.id) - email_token = create(:oauth_access_token, - :resource_owner_id => user.id, - :scopes => %w[read_prefs read_email]) + good_auth = bearer_authorization_header(user, :scopes => %w[read_prefs]) + bad_auth = bearer_authorization_header(user, :scopes => %w[]) + email_auth = bearer_authorization_header(user, :scopes => %w[read_prefs read_email]) # check that we can't fetch details as XML without read_prefs - get user_details_path, :headers => bearer_authorization_header(bad_token.token) + get api_user_details_path, :headers => bad_auth assert_response :forbidden # check that we can fetch details as XML without read_email - get user_details_path, :headers => bearer_authorization_header(good_token.token) + get api_user_details_path, :headers => good_auth assert_response :success assert_equal "application/xml", response.media_type @@ -325,7 +199,7 @@ module Api check_xml_details(user, true, false) # check that we can fetch details as XML with read_email - get user_details_path, :headers => bearer_authorization_header(email_token.token) + get api_user_details_path, :headers => email_auth assert_response :success assert_equal "application/xml", response.media_type @@ -333,11 +207,11 @@ module Api check_xml_details(user, true, true) # check that we can't fetch details as JSON without read_prefs - get user_details_path(:format => "json"), :headers => bearer_authorization_header(bad_token.token) + get api_user_details_path(:format => "json"), :headers => bad_auth assert_response :forbidden # check that we can fetch details as JSON without read_email - get user_details_path(:format => "json"), :headers => bearer_authorization_header(good_token.token) + get api_user_details_path(:format => "json"), :headers => good_auth assert_response :success assert_equal "application/json", response.media_type @@ -349,7 +223,7 @@ module Api check_json_details(js, user, true, false) # check that we can fetch details as JSON with read_email - get user_details_path(:format => "json"), :headers => bearer_authorization_header(email_token.token) + get api_user_details_path(:format => "json"), :headers => email_auth assert_response :success assert_equal "application/json", response.media_type @@ -419,110 +293,29 @@ module Api check_json_details(js["users"][1], user3, false, false) get api_users_path, :params => { :users => create(:user, :suspended).id } - assert_response :not_found - - get api_users_path, :params => { :users => create(:user, :deleted).id } - assert_response :not_found - - get api_users_path, :params => { :users => 0 } - assert_response :not_found - end - - def test_index_oauth1 - user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday) - user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday) - user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday) - good_token = create(:access_token, :user => user1, :allow_read_prefs => true) - bad_token = create(:access_token, :user => user1) - - signed_get api_users_path, :params => { :users => user1.id }, :oauth => { :token => good_token } assert_response :success assert_equal "application/xml", response.media_type - assert_select "user", :count => 1 do - check_xml_details(user1, true, false) - assert_select "user[id='#{user2.id}']", :count => 0 - assert_select "user[id='#{user3.id}']", :count => 0 - end - - signed_get api_users_path, :params => { :users => user2.id }, :oauth => { :token => good_token } - assert_response :success - assert_equal "application/xml", response.media_type - assert_select "user", :count => 1 do - assert_select "user[id='#{user1.id}']", :count => 0 - check_xml_details(user2, false, false) - assert_select "user[id='#{user3.id}']", :count => 0 - end + assert_select "user", :count => 0 - signed_get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :oauth => { :token => good_token } + get api_users_path, :params => { :users => create(:user, :deleted).id } assert_response :success assert_equal "application/xml", response.media_type - assert_select "user", :count => 2 do - check_xml_details(user1, true, false) - assert_select "user[id='#{user2.id}']", :count => 0 - check_xml_details(user3, false, false) - end + assert_select "user", :count => 0 - signed_get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :oauth => { :token => bad_token } + get api_users_path, :params => { :users => 0 } assert_response :success assert_equal "application/xml", response.media_type - assert_select "user", :count => 2 do - check_xml_details(user1, false, false) - assert_select "user[id='#{user2.id}']", :count => 0 - check_xml_details(user3, false, false) - end - - signed_get api_users_path, :params => { :users => user1.id, :format => "json" }, :oauth => { :token => good_token } - assert_response :success - assert_equal "application/json", response.media_type - js = ActiveSupport::JSON.decode(@response.body) - assert_not_nil js - assert_equal 1, js["users"].count - check_json_details(js["users"][0], user1, true, false) - - signed_get api_users_path, :params => { :users => user2.id, :format => "json" }, :oauth => { :token => good_token } - assert_response :success - assert_equal "application/json", response.media_type - js = ActiveSupport::JSON.decode(@response.body) - assert_not_nil js - assert_equal 1, js["users"].count - check_json_details(js["users"][0], user2, false, false) - - signed_get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :oauth => { :token => good_token } - assert_response :success - assert_equal "application/json", response.media_type - js = ActiveSupport::JSON.decode(@response.body) - assert_not_nil js - assert_equal 2, js["users"].count - check_json_details(js["users"][0], user1, true, false) - check_json_details(js["users"][1], user3, false, false) - - signed_get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :oauth => { :token => bad_token } - assert_response :success - assert_equal "application/json", response.media_type - js = ActiveSupport::JSON.decode(@response.body) - assert_not_nil js - assert_equal 2, js["users"].count - check_json_details(js["users"][0], user1, false, false) - check_json_details(js["users"][1], user3, false, false) - - signed_get api_users_path, :params => { :users => create(:user, :suspended).id }, :oauth => { :token => good_token } - assert_response :not_found - - signed_get api_users_path, :params => { :users => create(:user, :deleted).id }, :oauth => { :token => good_token } - assert_response :not_found - - signed_get api_users_path, :params => { :users => 0 }, :oauth => { :token => good_token } - assert_response :not_found + assert_select "user", :count => 0 end def test_index_oauth2 user1 = create(:user, :description => "test1", :terms_agreed => Date.yesterday) user2 = create(:user, :description => "test2", :terms_agreed => Date.yesterday) user3 = create(:user, :description => "test3", :terms_agreed => Date.yesterday) - good_token = create(:oauth_access_token, :resource_owner_id => user1.id, :scopes => %w[read_prefs]) - bad_token = create(:oauth_access_token, :resource_owner_id => user1.id, :scopes => %w[]) + good_auth = bearer_authorization_header(user1, :scopes => %w[read_prefs]) + bad_auth = bearer_authorization_header(user1, :scopes => %w[]) - get api_users_path, :params => { :users => user1.id }, :headers => bearer_authorization_header(good_token.token) + get api_users_path, :params => { :users => user1.id }, :headers => good_auth assert_response :success assert_equal "application/xml", response.media_type assert_select "user", :count => 1 do @@ -531,7 +324,7 @@ module Api assert_select "user[id='#{user3.id}']", :count => 0 end - get api_users_path, :params => { :users => user2.id }, :headers => bearer_authorization_header(good_token.token) + get api_users_path, :params => { :users => user2.id }, :headers => good_auth assert_response :success assert_equal "application/xml", response.media_type assert_select "user", :count => 1 do @@ -540,7 +333,7 @@ module Api assert_select "user[id='#{user3.id}']", :count => 0 end - get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bearer_authorization_header(good_token.token) + get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => good_auth assert_response :success assert_equal "application/xml", response.media_type assert_select "user", :count => 2 do @@ -549,7 +342,7 @@ module Api check_xml_details(user3, false, false) end - get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bearer_authorization_header(bad_token.token) + get api_users_path, :params => { :users => "#{user1.id},#{user3.id}" }, :headers => bad_auth assert_response :success assert_equal "application/xml", response.media_type assert_select "user", :count => 2 do @@ -558,7 +351,7 @@ module Api check_xml_details(user3, false, false) end - get api_users_path, :params => { :users => user1.id, :format => "json" }, :headers => bearer_authorization_header(good_token.token) + get api_users_path, :params => { :users => user1.id, :format => "json" }, :headers => good_auth assert_response :success assert_equal "application/json", response.media_type js = ActiveSupport::JSON.decode(@response.body) @@ -566,7 +359,7 @@ module Api assert_equal 1, js["users"].count check_json_details(js["users"][0], user1, true, false) - get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => bearer_authorization_header(good_token.token) + get api_users_path, :params => { :users => user2.id, :format => "json" }, :headers => good_auth assert_response :success assert_equal "application/json", response.media_type js = ActiveSupport::JSON.decode(@response.body) @@ -574,7 +367,7 @@ module Api assert_equal 1, js["users"].count check_json_details(js["users"][0], user2, false, false) - get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bearer_authorization_header(good_token.token) + get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => good_auth assert_response :success assert_equal "application/json", response.media_type js = ActiveSupport::JSON.decode(@response.body) @@ -583,7 +376,7 @@ module Api check_json_details(js["users"][0], user1, true, false) check_json_details(js["users"][1], user3, false, false) - get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bearer_authorization_header(bad_token.token) + get api_users_path, :params => { :users => "#{user1.id},#{user3.id}", :format => "json" }, :headers => bad_auth assert_response :success assert_equal "application/json", response.media_type js = ActiveSupport::JSON.decode(@response.body) @@ -592,41 +385,20 @@ module Api check_json_details(js["users"][0], user1, false, false) check_json_details(js["users"][1], user3, false, false) - get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => bearer_authorization_header(good_token.token) - assert_response :not_found - - get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => bearer_authorization_header(good_token.token) - assert_response :not_found - - get api_users_path, :params => { :users => 0 }, :headers => bearer_authorization_header(good_token.token) - assert_response :not_found - end - - def test_gpx_files - user = create(:user) - trace1 = create(:trace, :user => user) do |trace| - create(:tracetag, :trace => trace, :tag => "London") - end - trace2 = create(:trace, :user => user) do |trace| - create(:tracetag, :trace => trace, :tag => "Birmingham") - end - # check that nothing is returned when not logged in - get user_gpx_files_path - assert_response :unauthorized + get api_users_path, :params => { :users => create(:user, :suspended).id }, :headers => good_auth + assert_response :success + assert_equal "application/xml", response.media_type + assert_select "user", :count => 0 - # check that we get a response when logged in - auth_header = basic_authorization_header user.email, "test" - get user_gpx_files_path, :headers => auth_header + get api_users_path, :params => { :users => create(:user, :deleted).id }, :headers => good_auth assert_response :success assert_equal "application/xml", response.media_type + assert_select "user", :count => 0 - # check the data that is returned - assert_select "gpx_file[id='#{trace1.id}']", 1 do - assert_select "tag", "London" - end - assert_select "gpx_file[id='#{trace2.id}']", 1 do - assert_select "tag", "Birmingham" - end + get api_users_path, :params => { :users => 0 }, :headers => good_auth + assert_response :success + assert_equal "application/xml", response.media_type + assert_select "user", :count => 0 end private @@ -713,7 +485,7 @@ module Api def check_json_details(js, user, include_private, include_email) assert_equal user.id, js["user"]["id"] assert_equal user.description, js["user"]["description"] - assert js["user"]["contributor_terms"]["agreed"] + assert_operator js["user"]["contributor_terms"], :[], "agreed" if include_private assert_not js["user"]["contributor_terms"]["pd"]