X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/363155a2a86796d27ee9161f1ee9b74d6fa307e5..91b31f03de0143c4e207fb3a001b32859f560388:/test/controllers/api/relations_controller_test.rb diff --git a/test/controllers/api/relations_controller_test.rb b/test/controllers/api/relations_controller_test.rb index f16a85b3e..da18099bd 100644 --- a/test/controllers/api/relations_controller_test.rb +++ b/test/controllers/api/relations_controller_test.rb @@ -11,11 +11,19 @@ module Api ) assert_routing( { :path => "/api/0.6/relation/1/full", :method => :get }, - { :controller => "api/relations", :action => "full", :id => "1", :format => "xml" } + { :controller => "api/relations", :action => "full", :id => "1" } + ) + assert_routing( + { :path => "/api/0.6/relation/1/full.json", :method => :get }, + { :controller => "api/relations", :action => "full", :id => "1", :format => "json" } ) assert_routing( { :path => "/api/0.6/relation/1", :method => :get }, - { :controller => "api/relations", :action => "show", :id => "1", :format => "xml" } + { :controller => "api/relations", :action => "show", :id => "1" } + ) + assert_routing( + { :path => "/api/0.6/relation/1.json", :method => :get }, + { :controller => "api/relations", :action => "show", :id => "1", :format => "json" } ) assert_routing( { :path => "/api/0.6/relation/1", :method => :put }, @@ -27,20 +35,36 @@ module Api ) assert_routing( { :path => "/api/0.6/relations", :method => :get }, - { :controller => "api/relations", :action => "index", :format => "xml" } + { :controller => "api/relations", :action => "index" } + ) + assert_routing( + { :path => "/api/0.6/relations.json", :method => :get }, + { :controller => "api/relations", :action => "index", :format => "json" } ) assert_routing( { :path => "/api/0.6/node/1/relations", :method => :get }, - { :controller => "api/relations", :action => "relations_for_node", :id => "1", :format => "xml" } + { :controller => "api/relations", :action => "relations_for_node", :id => "1" } ) assert_routing( { :path => "/api/0.6/way/1/relations", :method => :get }, - { :controller => "api/relations", :action => "relations_for_way", :id => "1", :format => "xml" } + { :controller => "api/relations", :action => "relations_for_way", :id => "1" } ) assert_routing( { :path => "/api/0.6/relation/1/relations", :method => :get }, - { :controller => "api/relations", :action => "relations_for_relation", :id => "1", :format => "xml" } + { :controller => "api/relations", :action => "relations_for_relation", :id => "1" } + ) + assert_routing( + { :path => "/api/0.6/node/1/relations.json", :method => :get }, + { :controller => "api/relations", :action => "relations_for_node", :id => "1", :format => "json" } + ) + assert_routing( + { :path => "/api/0.6/way/1/relations.json", :method => :get }, + { :controller => "api/relations", :action => "relations_for_way", :id => "1", :format => "json" } + ) + assert_routing( + { :path => "/api/0.6/relation/1/relations.json", :method => :get }, + { :controller => "api/relations", :action => "relations_for_relation", :id => "1", :format => "json" } ) end @@ -50,15 +74,15 @@ module Api def test_show # check that a visible relation is returned properly - get :show, :params => { :id => create(:relation).id }, :format => :xml + get :show, :params => { :id => create(:relation).id } assert_response :success # check that an invisible relation is not returned - get :show, :params => { :id => create(:relation, :deleted).id }, :format => :xml + get :show, :params => { :id => create(:relation, :deleted).id } assert_response :gone # check chat a non-existent relation is not returned - get :show, :params => { :id => 0 }, :format => :xml + get :show, :params => { :id => 0 } assert_response :not_found end @@ -129,7 +153,7 @@ module Api def check_relations_for_element(method, type, id, expected_relations) # check the "relations for relation" mode - get method, :params => { :id => id }, :format => :xml + get method, :params => { :id => id } assert_response :success # count one osm element @@ -148,13 +172,13 @@ module Api def test_full # check the "full" mode - get :full, :params => { :id => 999999 }, :format => :xml + get :full, :params => { :id => 999999 } assert_response :not_found - get :full, :params => { :id => create(:relation, :deleted).id }, :format => :xml + get :full, :params => { :id => create(:relation, :deleted).id } assert_response :gone - get :full, :params => { :id => create(:relation).id }, :format => :xml + get :full, :params => { :id => create(:relation).id } assert_response :success # FIXME: check whether this contains the stuff we want! end @@ -169,15 +193,15 @@ module Api relation4.old_relations.find_by(:version => 1).redact!(create(:redaction)) # 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 => { :relations => "" }, :format => :xml + get :index, :params => { :relations => "" } assert_response :bad_request # test a working call - get :index, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}" }, :format => :xml + get :index, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}" } assert_response :success assert_select "osm" do assert_select "relation", :count => 4 @@ -187,8 +211,20 @@ module Api assert_select "relation[id='#{relation4.id}'][visible='true']", :count => 1 end + # test a working call with json format + get :index, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.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"] == "relation" }) + assert_equal 1, (js["elements"].count { |a| a["id"] == relation1.id && a["visible"].nil? }) + assert_equal 1, (js["elements"].count { |a| a["id"] == relation2.id && a["visible"] == false }) + assert_equal 1, (js["elements"].count { |a| a["id"] == relation3.id && a["visible"].nil? }) + assert_equal 1, (js["elements"].count { |a| a["id"] == relation4.id && a["visible"].nil? }) + # check error when a non-existent relation is included - get :index, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0" }, :format => :xml + get :index, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0" } assert_response :not_found end @@ -271,7 +307,7 @@ module Api assert_equal true, checkrelation.visible, "saved relation is not visible" # ok the relation is there but can we also retrieve it? - get :show, :params => { :id => relationid }, :format => :xml + get :show, :params => { :id => relationid } assert_response :success ### @@ -302,7 +338,7 @@ module Api "saved relation is not visible" # ok the relation is there but can we also retrieve it? - get :show, :params => { :id => relationid }, :format => :xml + get :show, :params => { :id => relationid } assert_response :success ### @@ -332,7 +368,7 @@ module Api "saved relation is not visible" # ok the relation is there but can we also retrieve it? - get :show, :params => { :id => relationid }, :format => :xml + get :show, :params => { :id => relationid } assert_response :success ### @@ -362,7 +398,7 @@ module Api assert_equal true, checkrelation.visible, "saved relation is not visible" # ok the relation is there but can we also retrieve it? - get :show, :params => { :id => relationid }, :format => :xml + get :show, :params => { :id => relationid } assert_response :success end @@ -525,28 +561,28 @@ module Api assert_response :forbidden # try to delete with an invalid (closed) changeset - xml = update_changeset(relation.to_xml, + xml = update_changeset(xml_for_relation(relation), private_user_closed_changeset.id) delete :delete, :params => { :id => relation.id }, :body => xml.to_s assert_response :forbidden # try to delete with an invalid (non-existent) changeset - xml = update_changeset(relation.to_xml, 0) + xml = update_changeset(xml_for_relation(relation), 0) delete :delete, :params => { :id => relation.id }, :body => xml.to_s assert_response :forbidden # this won't work because the relation is in-use by another relation - xml = used_relation.to_xml + xml = xml_for_relation(used_relation) delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s assert_response :forbidden # this should work when we provide the appropriate payload... - xml = relation.to_xml + xml = xml_for_relation(relation) delete :delete, :params => { :id => relation.id }, :body => xml.to_s assert_response :forbidden # this won't work since the relation is already deleted - xml = deleted_relation.to_xml + xml = xml_for_relation(deleted_relation) delete :delete, :params => { :id => deleted_relation.id }, :body => xml.to_s assert_response :forbidden @@ -568,36 +604,36 @@ module Api assert_match(/Changeset id is missing/, @response.body) # try to delete with an invalid (closed) changeset - xml = update_changeset(relation.to_xml, + xml = update_changeset(xml_for_relation(relation), closed_changeset.id) delete :delete, :params => { :id => relation.id }, :body => xml.to_s assert_response :conflict # try to delete with an invalid (non-existent) changeset - xml = update_changeset(relation.to_xml, 0) + xml = update_changeset(xml_for_relation(relation), 0) delete :delete, :params => { :id => relation.id }, :body => xml.to_s assert_response :conflict # this won't work because the relation is in a changeset owned by someone else - xml = update_changeset(relation.to_xml, create(:changeset).id) + xml = update_changeset(xml_for_relation(relation), create(:changeset).id) delete :delete, :params => { :id => relation.id }, :body => xml.to_s assert_response :conflict, "shouldn't be able to delete a relation in a changeset owned by someone else (#{@response.body})" # this won't work because the relation in the payload is different to that passed - xml = update_changeset(relation.to_xml, changeset.id) + xml = update_changeset(xml_for_relation(relation), changeset.id) delete :delete, :params => { :id => create(:relation).id }, :body => xml.to_s assert_response :bad_request, "shouldn't be able to delete a relation when payload is different to the url" # this won't work because the relation is in-use by another relation - xml = update_changeset(used_relation.to_xml, changeset.id) + xml = update_changeset(xml_for_relation(used_relation), changeset.id) delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s assert_response :precondition_failed, "shouldn't be able to delete a relation used in a relation (#{@response.body})" assert_equal "Precondition failed: The relation #{used_relation.id} is used in relation #{super_relation.id}.", @response.body # this should work when we provide the appropriate payload... - xml = update_changeset(multi_tag_relation.to_xml, changeset.id) + xml = update_changeset(xml_for_relation(multi_tag_relation), changeset.id) delete :delete, :params => { :id => multi_tag_relation.id }, :body => xml.to_s assert_response :success @@ -607,18 +643,18 @@ module Api "delete request should return a new version number for relation" # this won't work since the relation is already deleted - xml = update_changeset(deleted_relation.to_xml, changeset.id) + xml = update_changeset(xml_for_relation(deleted_relation), changeset.id) delete :delete, :params => { :id => deleted_relation.id }, :body => xml.to_s assert_response :gone # Public visible relation needs to be deleted - xml = update_changeset(super_relation.to_xml, changeset.id) + xml = update_changeset(xml_for_relation(super_relation), changeset.id) delete :delete, :params => { :id => super_relation.id }, :body => xml.to_s assert_response :success # this works now because the relation which was using this one # has been deleted. - xml = update_changeset(used_relation.to_xml, changeset.id) + xml = update_changeset(xml_for_relation(used_relation), changeset.id) delete :delete, :params => { :id => used_relation.id }, :body => xml.to_s assert_response :success, "should be able to delete a relation used in an old relation (#{@response.body})" @@ -643,7 +679,7 @@ module Api # indirectly via the way), so the bbox should be [3,3,5,5]. check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id| # add a tag to an existing relation - relation_xml = relation.to_xml + relation_xml = xml_for_relation(relation) relation_element = relation_xml.find("//osm/relation").first new_tag = XML::Node.new("tag") new_tag["k"] = "some_new_tag" @@ -675,7 +711,7 @@ module Api [node1, node2, way1, way2].each do |element| bbox = element.bbox.to_unscaled check_changeset_modify(bbox) do |changeset_id| - relation_xml = Relation.find(relation.id).to_xml + relation_xml = xml_for_relation(Relation.find(relation.id)) relation_element = relation_xml.find("//osm/relation").first new_member = XML::Node.new("member") new_member["ref"] = element.id.to_s @@ -691,7 +727,7 @@ module Api assert_response :success, "can't update relation for add #{element.class}/bbox test: #{@response.body}" # get it back and check the ordering - get :show, :params => { :id => relation.id }, :format => :xml + get :show, :params => { :id => relation.id } assert_response :success, "can't read back the relation: #{@response.body}" check_ordering(relation_xml, @response.body) end @@ -710,7 +746,7 @@ module Api check_changeset_modify(BoundingBox.new(5, 5, 5, 5)) do |changeset_id| # remove node 5 (5,5) from an existing relation - relation_xml = relation.to_xml + relation_xml = xml_for_relation(relation) relation_xml .find("//osm/relation/member[@type='node'][@ref='#{node2.id}']") .first.remove! @@ -737,7 +773,7 @@ module Api basic_authorization user.email, "test" - doc_str = < @@ -746,7 +782,7 @@ module Api -OSM + OSM doc = XML::Parser.string(doc_str).parse put :create, :body => doc.to_s @@ -754,7 +790,7 @@ OSM relation_id = @response.body.to_i # get it back and check the ordering - get :show, :params => { :id => relation_id }, :format => :xml + get :show, :params => { :id => relation_id } assert_response :success, "can't read back the relation: #{@response.body}" check_ordering(doc, @response.body) @@ -774,13 +810,13 @@ OSM assert_equal 2, @response.body.to_i # get it back again and check the ordering again - get :show, :params => { :id => relation_id }, :format => :xml + get :show, :params => { :id => relation_id } assert_response :success, "can't read back the relation: #{@response.body}" check_ordering(doc, @response.body) # check the ordering in the history tables: with_controller(OldRelationsController.new) do - get :version, :params => { :id => relation_id, :version => 2 }, :format => :xml + get :version, :params => { :id => relation_id, :version => 2 } assert_response :success, "can't read back version 2 of the relation #{relation_id}" check_ordering(doc, @response.body) end @@ -795,7 +831,7 @@ OSM node1 = create(:node) node2 = create(:node) - doc_str = < @@ -804,7 +840,7 @@ OSM -OSM + OSM doc = XML::Parser.string(doc_str).parse ## First try with the private user @@ -821,7 +857,7 @@ OSM relation_id = @response.body.to_i # get it back and check the ordering - get :show, :params => { :id => relation_id }, :format => :xml + get :show, :params => { :id => relation_id } assert_response :success, "can't read back the relation: #{relation_id}" check_ordering(doc, @response.body) end @@ -836,7 +872,7 @@ OSM node3 = create(:node) node4 = create(:node) - doc_str = < @@ -845,7 +881,7 @@ OSM -OSM + OSM doc = XML::Parser.string(doc_str).parse basic_authorization user.email, "test" @@ -854,13 +890,13 @@ OSM relation_id = @response.body.to_i # check the ordering in the current tables: - get :show, :params => { :id => relation_id }, :format => :xml + get :show, :params => { :id => relation_id } assert_response :success, "can't read back the relation: #{@response.body}" check_ordering(doc, @response.body) # check the ordering in the history tables: with_controller(OldRelationsController.new) do - get :version, :params => { :id => relation_id, :version => 1 }, :format => :xml + get :version, :params => { :id => relation_id, :version => 1 } assert_response :success, "can't read back version 1 of the relation: #{@response.body}" check_ordering(doc, @response.body) end @@ -879,7 +915,7 @@ OSM create(:relation_member, :relation => relation, :member => node2) check_changeset_modify(BoundingBox.new(3, 3, 5, 5)) do |changeset_id| - relation_xml = relation.to_xml + relation_xml = xml_for_relation(relation) relation_xml .find("//osm/relation/member") .each(&:remove!) @@ -953,7 +989,7 @@ OSM # now download the changeset to check its bounding box with_controller(Api::ChangesetsController.new) do - get :show, :params => { :id => changeset_id }, :format => :xml + get :show, :params => { :id => changeset_id } assert_response :success, "can't re-read changeset for modify test" assert_select "osm>changeset", 1, "Changeset element doesn't exist in #{@response.body}" assert_select "osm>changeset[id='#{changeset_id}']", 1, "Changeset id=#{changeset_id} doesn't exist in #{@response.body}" @@ -970,10 +1006,10 @@ OSM # doc is returned. def with_relation(id, ver = nil) if ver.nil? - get :show, :params => { :id => id }, :format => :xml + get :show, :params => { :id => id } else with_controller(OldRelationsController.new) do - get :version, :params => { :id => id, :version => ver }, :format => :xml + get :version, :params => { :id => id, :version => ver } end end assert_response :success @@ -991,7 +1027,7 @@ OSM version = @response.body.to_i # now get the new version - get :show, :params => { :id => rel_id }, :format => :xml + get :show, :params => { :id => rel_id } assert_response :success new_rel = xml_parse(@response.body) @@ -1023,7 +1059,7 @@ OSM end # now get the new version - get :show, :params => { :id => rel_id }, :format => :xml + get :show, :params => { :id => rel_id } assert_response :success new_rel = xml_parse(@response.body)