X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/363155a2a86796d27ee9161f1ee9b74d6fa307e5..7a1615bc55aee72dd03a5dd43c08b4f13f669f06:/test/controllers/api/ways_controller_test.rb diff --git a/test/controllers/api/ways_controller_test.rb b/test/controllers/api/ways_controller_test.rb index 3d04ef958..ae7f18161 100644 --- a/test/controllers/api/ways_controller_test.rb +++ b/test/controllers/api/ways_controller_test.rb @@ -11,11 +11,19 @@ module Api ) assert_routing( { :path => "/api/0.6/way/1/full", :method => :get }, - { :controller => "api/ways", :action => "full", :id => "1", :format => "xml" } + { :controller => "api/ways", :action => "full", :id => "1" } + ) + assert_routing( + { :path => "/api/0.6/way/1/full.json", :method => :get }, + { :controller => "api/ways", :action => "full", :id => "1", :format => "json" } ) assert_routing( { :path => "/api/0.6/way/1", :method => :get }, - { :controller => "api/ways", :action => "show", :id => "1", :format => "xml" } + { :controller => "api/ways", :action => "show", :id => "1" } + ) + assert_routing( + { :path => "/api/0.6/way/1.json", :method => :get }, + { :controller => "api/ways", :action => "show", :id => "1", :format => "json" } ) assert_routing( { :path => "/api/0.6/way/1", :method => :put }, @@ -27,7 +35,11 @@ module Api ) assert_routing( { :path => "/api/0.6/ways", :method => :get }, - { :controller => "api/ways", :action => "index", :format => "xml" } + { :controller => "api/ways", :action => "index" } + ) + assert_routing( + { :path => "/api/0.6/ways.json", :method => :get }, + { :controller => "api/ways", :action => "index", :format => "json" } ) end @@ -37,15 +49,15 @@ module Api def test_show # check that a visible way is returned properly - get :show, :params => { :id => create(:way).id }, :format => :xml + get :show, :params => { :id => create(:way).id } assert_response :success # check that an invisible way is not returned - get :show, :params => { :id => create(:way, :deleted).id }, :format => :xml + get :show, :params => { :id => create(:way, :deleted).id } assert_response :gone # check chat a non-existent way is not returned - get :show, :params => { :id => 0 }, :format => :xml + get :show, :params => { :id => 0 } assert_response :not_found end @@ -86,15 +98,15 @@ module Api way4 = create(:way) # check error when no parameter provided - get :index, :format => :xml + get :index assert_response :bad_request # check error when no parameter value provided - get :index, :params => { :ways => "" }, :format => :xml + get :index, :params => { :ways => "" } assert_response :bad_request # test a working call - get :index, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}" }, :format => :xml + get :index, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}" } assert_response :success assert_select "osm" do assert_select "way", :count => 4 @@ -104,8 +116,20 @@ module Api assert_select "way[id='#{way4.id}'][visible='true']", :count => 1 end + # test a working call with json format + get :index, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}", :format => "json" } + + js = ActiveSupport::JSON.decode(@response.body) + assert_not_nil js + assert_equal 4, js["elements"].count + assert_equal 4, (js["elements"].count { |a| a["type"] == "way" }) + assert_equal 1, (js["elements"].count { |a| a["id"] == way1.id && a["visible"].nil? }) + assert_equal 1, (js["elements"].count { |a| a["id"] == way2.id && a["visible"] == false }) + assert_equal 1, (js["elements"].count { |a| a["id"] == way3.id && a["visible"].nil? }) + assert_equal 1, (js["elements"].count { |a| a["id"] == way4.id && a["visible"].nil? }) + # check error when a non-existent way is included - get :index, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0" }, :format => :xml + get :index, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0" } assert_response :not_found end @@ -166,8 +190,8 @@ module Api "saved way does not belong to the correct changeset" assert_equal user.id, checkway.changeset.user_id, "saved way does not belong to user that created it" - assert_equal true, checkway.visible, - "saved way is not visible" + assert checkway.visible, + "saved way is not visible" end # ------------------------------------- @@ -290,17 +314,17 @@ module Api assert_response :forbidden # try to delete with an invalid (closed) changeset - xml = update_changeset(private_way.to_xml, private_closed_changeset.id) + xml = update_changeset(xml_for_way(private_way), private_closed_changeset.id) delete :delete, :params => { :id => private_way.id }, :body => xml.to_s assert_response :forbidden # try to delete with an invalid (non-existent) changeset - xml = update_changeset(private_way.to_xml, 0) + xml = update_changeset(xml_for_way(private_way), 0) delete :delete, :params => { :id => private_way.id }, :body => xml.to_s assert_response :forbidden # Now try with a valid changeset - xml = private_way.to_xml + xml = xml_for_way(private_way) delete :delete, :params => { :id => private_way.id }, :body => xml.to_s assert_response :forbidden @@ -311,12 +335,12 @@ module Api # "delete request should return a new version number for way" # this won't work since the way is already deleted - xml = private_deleted_way.to_xml + xml = xml_for_way(private_deleted_way) delete :delete, :params => { :id => private_deleted_way.id }, :body => xml.to_s assert_response :forbidden # this shouldn't work as the way is used in a relation - xml = private_used_way.to_xml + xml = xml_for_way(private_used_way) delete :delete, :params => { :id => private_used_way.id }, :body => xml.to_s assert_response :forbidden, "shouldn't be able to delete a way used in a relation (#{@response.body}), when done by a private user" @@ -339,17 +363,17 @@ module Api assert_response :bad_request # try to delete with an invalid (closed) changeset - xml = update_changeset(way.to_xml, closed_changeset.id) + xml = update_changeset(xml_for_way(way), closed_changeset.id) delete :delete, :params => { :id => way.id }, :body => xml.to_s assert_response :conflict # try to delete with an invalid (non-existent) changeset - xml = update_changeset(way.to_xml, 0) + xml = update_changeset(xml_for_way(way), 0) delete :delete, :params => { :id => way.id }, :body => xml.to_s assert_response :conflict # Now try with a valid changeset - xml = way.to_xml + xml = xml_for_way(way) delete :delete, :params => { :id => way.id }, :body => xml.to_s assert_response :success @@ -360,12 +384,12 @@ module Api "delete request should return a new version number for way" # this won't work since the way is already deleted - xml = deleted_way.to_xml + xml = xml_for_way(deleted_way) delete :delete, :params => { :id => deleted_way.id }, :body => xml.to_s assert_response :gone # this shouldn't work as the way is used in a relation - xml = used_way.to_xml + xml = xml_for_way(used_way) delete :delete, :params => { :id => used_way.id }, :body => xml.to_s assert_response :precondition_failed, "shouldn't be able to delete a way used in a relation (#{@response.body})" @@ -390,7 +414,7 @@ module Api ## First test with no user credentials # try and update a way without authorisation - xml = way.to_xml + xml = xml_for_way(way) put :update, :params => { :id => way.id }, :body => xml.to_s assert_response :unauthorized @@ -402,33 +426,33 @@ module Api ## trying to break changesets # try and update in someone else's changeset - xml = update_changeset(private_way.to_xml, + xml = update_changeset(xml_for_way(private_way), create(:changeset).id) put :update, :params => { :id => private_way.id }, :body => xml.to_s assert_require_public_data "update with other user's changeset should be forbidden when date isn't public" # try and update in a closed changeset - xml = update_changeset(private_way.to_xml, + xml = update_changeset(xml_for_way(private_way), create(:changeset, :closed, :user => private_user).id) put :update, :params => { :id => private_way.id }, :body => xml.to_s assert_require_public_data "update with closed changeset should be forbidden, when data isn't public" # try and update in a non-existant changeset - xml = update_changeset(private_way.to_xml, 0) + xml = update_changeset(xml_for_way(private_way), 0) put :update, :params => { :id => private_way.id }, :body => xml.to_s assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public") ## try and submit invalid updates - xml = xml_replace_node(private_way.to_xml, node.id, 9999) + xml = xml_replace_node(xml_for_way(private_way), node.id, 9999) put :update, :params => { :id => private_way.id }, :body => xml.to_s assert_require_public_data "way with non-existent node should be forbidden, when data isn't public" - xml = xml_replace_node(private_way.to_xml, node.id, create(:node, :deleted).id) + xml = xml_replace_node(xml_for_way(private_way), node.id, create(:node, :deleted).id) put :update, :params => { :id => private_way.id }, :body => xml.to_s assert_require_public_data "way with deleted node should be forbidden, when data isn't public" ## finally, produce a good request which will still not work - xml = private_way.to_xml + xml = xml_for_way(private_way) put :update, :params => { :id => private_way.id }, :body => xml.to_s assert_require_public_data "should have failed with a forbidden when data isn't public" @@ -440,28 +464,28 @@ module Api ## trying to break changesets # try and update in someone else's changeset - xml = update_changeset(way.to_xml, + xml = update_changeset(xml_for_way(way), create(:changeset).id) put :update, :params => { :id => way.id }, :body => xml.to_s assert_response :conflict, "update with other user's changeset should be rejected" # try and update in a closed changeset - xml = update_changeset(way.to_xml, + xml = update_changeset(xml_for_way(way), create(:changeset, :closed, :user => user).id) put :update, :params => { :id => way.id }, :body => xml.to_s assert_response :conflict, "update with closed changeset should be rejected" # try and update in a non-existant changeset - xml = update_changeset(way.to_xml, 0) + xml = update_changeset(xml_for_way(way), 0) put :update, :params => { :id => way.id }, :body => xml.to_s assert_response :conflict, "update with changeset=0 should be rejected" ## try and submit invalid updates - xml = xml_replace_node(way.to_xml, node.id, 9999) + xml = xml_replace_node(xml_for_way(way), node.id, 9999) put :update, :params => { :id => way.id }, :body => xml.to_s assert_response :precondition_failed, "way with non-existent node should be rejected" - xml = xml_replace_node(way.to_xml, node.id, create(:node, :deleted).id) + xml = xml_replace_node(xml_for_way(way), node.id, create(:node, :deleted).id) put :update, :params => { :id => way.id }, :body => xml.to_s assert_response :precondition_failed, "way with deleted node should be rejected" @@ -469,26 +493,26 @@ module Api current_way_version = way.version # try and submit a version behind - xml = xml_attr_rewrite(way.to_xml, + xml = xml_attr_rewrite(xml_for_way(way), "version", current_way_version - 1) put :update, :params => { :id => way.id }, :body => xml.to_s assert_response :conflict, "should have failed on old version number" # try and submit a version ahead - xml = xml_attr_rewrite(way.to_xml, + xml = xml_attr_rewrite(xml_for_way(way), "version", current_way_version + 1) put :update, :params => { :id => way.id }, :body => xml.to_s assert_response :conflict, "should have failed on skipped version number" # try and submit total crap in the version field - xml = xml_attr_rewrite(way.to_xml, + xml = xml_attr_rewrite(xml_for_way(way), "version", "p1r4t3s!") put :update, :params => { :id => way.id }, :body => xml.to_s assert_response :conflict, "should not be able to put 'p1r4at3s!' in the version field" ## try an update with the wrong ID - xml = create(:way).to_xml + xml = xml_for_way(create(:way)) put :update, :params => { :id => way.id }, :body => xml.to_s assert_response :bad_request, "should not be able to update a way with a different ID from the XML" @@ -500,7 +524,7 @@ module Api "should not be able to update a way with non-OSM XML doc." ## finally, produce a good request which should work - xml = way.to_xml + xml = xml_for_way(way) put :update, :params => { :id => way.id }, :body => xml.to_s assert_response :success, "a valid update request failed" end @@ -527,7 +551,7 @@ module Api tag_xml["v"] = "yes" # add the tag into the existing xml - way_xml = private_way.to_xml + way_xml = xml_for_way(private_way) way_xml.find("//osm/way").first << tag_xml # try and upload it @@ -545,7 +569,7 @@ module Api tag_xml["v"] = "yes" # add the tag into the existing xml - way_xml = way.to_xml + way_xml = xml_for_way(way) way_xml.find("//osm/way").first << tag_xml # try and upload it @@ -575,7 +599,7 @@ module Api tag_xml["v"] = private_existing_tag.v # add the tag into the existing xml - way_xml = private_way.to_xml + way_xml = xml_for_way(private_way) way_xml.find("//osm/way").first << tag_xml # try and upload it @@ -593,7 +617,7 @@ module Api tag_xml["v"] = existing_tag.v # add the tag into the existing xml - way_xml = way.to_xml + way_xml = xml_for_way(way) way_xml.find("//osm/way").first << tag_xml # try and upload it @@ -621,7 +645,7 @@ module Api tag_xml["v"] = "foobar" # add the tag into the existing xml - way_xml = private_way.to_xml + way_xml = xml_for_way(private_way) # add two copies of the tag way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml @@ -641,7 +665,7 @@ module Api tag_xml["v"] = "foobar" # add the tag into the existing xml - way_xml = way.to_xml + way_xml = xml_for_way(way) # add two copies of the tag way_xml.find("//osm/way").first << tag_xml.copy(true) << tag_xml @@ -711,7 +735,7 @@ module Api _way3_v2 = create(:old_way, :current_way => way3_v1.current_way, :version => 2) create(:old_way_node, :old_way => way3_v1, :node => node) - get :ways_for_node, :params => { :id => node.id }, :format => :xml + get :ways_for_node, :params => { :id => node.id } assert_response :success ways_xml = XML::Parser.string(@response.body).parse assert_not_nil ways_xml, "failed to parse ways_for_node response"