X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/f31c81c3f3200b2fb3b2703caa35238652e18e74..d00a0667bc6e156061a6c6428b0070ced6acda87:/test/controllers/api/changesets_controller_test.rb diff --git a/test/controllers/api/changesets_controller_test.rb b/test/controllers/api/changesets_controller_test.rb index 50ced0ff1..17a9ad9d3 100644 --- a/test/controllers/api/changesets_controller_test.rb +++ b/test/controllers/api/changesets_controller_test.rb @@ -51,11 +51,11 @@ module Api ) assert_routing( { :path => "/api/0.6/changesets", :method => :get }, - { :controller => "api/changesets", :action => "query" } + { :controller => "api/changesets", :action => "index" } ) assert_routing( { :path => "/api/0.6/changesets.json", :method => :get }, - { :controller => "api/changesets", :action => "query", :format => "json" } + { :controller => "api/changesets", :action => "index", :format => "json" } ) end @@ -749,7 +749,11 @@ module Api end def test_upload_large_changeset - auth_header = basic_authorization_header create(:user).email, "test" + user = create(:user) + auth_header = basic_authorization_header user.email, "test" + + # create an old changeset to ensure we have the maximum rate limit + create(:changeset, :user => user, :created_at => Time.now.utc - 28.days) # create a changeset put changeset_create_path, :params => "", :headers => auth_header @@ -1048,14 +1052,14 @@ module Api diff = <<~CHANGESET - - - - - - - - + + + + + + + + CHANGESET @@ -1329,9 +1333,9 @@ module Api diff = <<~CHANGESET - - - + + + @@ -1352,9 +1356,9 @@ module Api diff = <<~CHANGESET - - - + + + @@ -1384,9 +1388,9 @@ module Api diff = <<~CHANGESET - - - + + + @@ -1407,9 +1411,9 @@ module Api diff = <<~CHANGESET - - - + + + @@ -1478,14 +1482,14 @@ module Api changeset_id = @response.body.to_i old_way = create(:way) - create(:way_node, :way => old_way, :node => create(:node, :lat => 1, :lon => 1)) + create(:way_node, :way => old_way, :node => create(:node, :lat => 0.1, :lon => 0.1)) diff = XML::Document.new diff.root = XML::Node.new "osmChange" modify = XML::Node.new "modify" xml_old_way = xml_node_for_way(old_way) nd_ref = XML::Node.new "nd" - nd_ref["ref"] = create(:node, :lat => 3, :lon => 3).id.to_s + nd_ref["ref"] = create(:node, :lat => 0.3, :lon => 0.3).id.to_s xml_old_way << nd_ref xml_old_way["changeset"] = changeset_id.to_s modify << xml_old_way @@ -1498,10 +1502,10 @@ module Api # check the bbox changeset = Changeset.find(changeset_id) - assert_equal 1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 1 degree" - assert_equal 3 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 3 degrees" - assert_equal 1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 1 degree" - assert_equal 3 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 3 degrees" + assert_equal 0.1 * GeoRecord::SCALE, changeset.min_lon, "min_lon should be 0.1 degree" + assert_equal 0.3 * GeoRecord::SCALE, changeset.max_lon, "max_lon should be 0.3 degrees" + assert_equal 0.1 * GeoRecord::SCALE, changeset.min_lat, "min_lat should be 0.1 degree" + assert_equal 0.3 * GeoRecord::SCALE, changeset.max_lat, "max_lat should be 0.3 degrees" end ## @@ -1798,6 +1802,71 @@ module Api assert_response :too_many_requests, "upload did not hit rate limit" end + ## + # test initial size limit + def test_upload_initial_size_limit + # create a user + user = create(:user) + + # create a changeset that puts us near the initial size limit + changeset = create(:changeset, :user => user, + :min_lat => (-0.5 * GeoRecord::SCALE).round, :min_lon => (0.5 * GeoRecord::SCALE).round, + :max_lat => (0.5 * GeoRecord::SCALE).round, :max_lon => (2.5 * GeoRecord::SCALE).round) + + # create authentication header + auth_header = basic_authorization_header user.email, "test" + + # simple diff to create a node + diff = <<~CHANGESET + + + + + + + + + CHANGESET + + # upload it + post changeset_upload_path(changeset), :params => diff, :headers => auth_header + assert_response :payload_too_large, "upload did not hit size limit" + end + + ## + # test size limit after one week + def test_upload_week_size_limit + # create a user + user = create(:user) + + # create a changeset to establish our initial edit time + create(:changeset, :user => user, :created_at => Time.now.utc - 7.days) + + # create a changeset that puts us near the initial size limit + changeset = create(:changeset, :user => user, + :min_lat => (-0.5 * GeoRecord::SCALE).round, :min_lon => (0.5 * GeoRecord::SCALE).round, + :max_lat => (0.5 * GeoRecord::SCALE).round, :max_lon => (2.5 * GeoRecord::SCALE).round) + + # create authentication header + auth_header = basic_authorization_header user.email, "test" + + # simple diff to create a node way and relation using placeholders + diff = <<~CHANGESET + + + + + + + + + CHANGESET + + # upload it + post changeset_upload_path(changeset), :params => diff, :headers => auth_header + assert_response :payload_too_large, "upload did not hit size limit" + end + ## # when we make some simple changes we get the same changes back from the # diff download. @@ -1829,14 +1898,14 @@ module Api diff = <<~CHANGESET - - - - - - - - + + + + + + + + CHANGESET @@ -1935,15 +2004,15 @@ module Api diff = <<~CHANGESET - + - - - + + + - + @@ -1995,12 +2064,46 @@ module Api end end + test "sorts downloaded elements by timestamp" do + changeset = create(:changeset) + node1 = create(:old_node, :version => 2, :timestamp => "2020-02-01", :changeset => changeset) + node0 = create(:old_node, :version => 2, :timestamp => "2020-01-01", :changeset => changeset) + + get changeset_download_path(changeset) + assert_response :success + assert_dom "modify", :count => 2 do |modify| + assert_dom modify[0], ">node", :count => 1 do |node| + assert_dom node, ">@id", node0.node_id.to_s + end + assert_dom modify[1], ">node", :count => 1 do |node| + assert_dom node, ">@id", node1.node_id.to_s + end + end + end + + test "sorts downloaded elements by version" do + changeset = create(:changeset) + node1 = create(:old_node, :version => 3, :timestamp => "2020-01-01", :changeset => changeset) + node0 = create(:old_node, :version => 2, :timestamp => "2020-01-01", :changeset => changeset) + + get changeset_download_path(changeset) + assert_response :success + assert_dom "modify", :count => 2 do |modify| + assert_dom modify[0], ">node", :count => 1 do |node| + assert_dom node, ">@id", node0.node_id.to_s + end + assert_dom modify[1], ">node", :count => 1 do |node| + assert_dom node, ">@id", node1.node_id.to_s + end + end + end + ## # check that the bounding box of a changeset gets updated correctly # FIXME: This should really be moded to a integration test due to the with_controller def test_changeset_bbox way = create(:way) - create(:way_node, :way => way, :node => create(:node, :lat => 3, :lon => 3)) + create(:way_node, :way => way, :node => create(:node, :lat => 0.3, :lon => 0.3)) auth_header = basic_authorization_header create(:user).email, "test" @@ -2012,7 +2115,7 @@ module Api # add a single node to it with_controller(NodesController.new) do - xml = "" + xml = "" put node_create_path, :params => xml, :headers => auth_header assert_response :success, "Couldn't create node." end @@ -2020,14 +2123,14 @@ module Api # get the bounding box back from the changeset get changeset_show_path(:id => changeset_id) assert_response :success, "Couldn't read back changeset." - assert_select "osm>changeset[min_lon='1.0000000']", 1 - assert_select "osm>changeset[max_lon='1.0000000']", 1 - assert_select "osm>changeset[min_lat='2.0000000']", 1 - assert_select "osm>changeset[max_lat='2.0000000']", 1 + assert_select "osm>changeset[min_lon='0.1000000']", 1 + assert_select "osm>changeset[max_lon='0.1000000']", 1 + assert_select "osm>changeset[min_lat='0.2000000']", 1 + assert_select "osm>changeset[max_lat='0.2000000']", 1 # add another node to it with_controller(NodesController.new) do - xml = "" + xml = "" put node_create_path, :params => xml, :headers => auth_header assert_response :success, "Couldn't create second node." end @@ -2035,10 +2138,10 @@ module Api # get the bounding box back from the changeset get changeset_show_path(:id => changeset_id) assert_response :success, "Couldn't read back changeset for the second time." - assert_select "osm>changeset[min_lon='1.0000000']", 1 - assert_select "osm>changeset[max_lon='2.0000000']", 1 - assert_select "osm>changeset[min_lat='1.0000000']", 1 - assert_select "osm>changeset[max_lat='2.0000000']", 1 + assert_select "osm>changeset[min_lon='0.1000000']", 1 + assert_select "osm>changeset[max_lon='0.2000000']", 1 + assert_select "osm>changeset[min_lat='0.1000000']", 1 + assert_select "osm>changeset[max_lat='0.2000000']", 1 # add (delete) a way to it, which contains a point at (3,3) with_controller(WaysController.new) do @@ -2050,10 +2153,10 @@ module Api # get the bounding box back from the changeset get changeset_show_path(:id => changeset_id) assert_response :success, "Couldn't read back changeset for the third time." - assert_select "osm>changeset[min_lon='1.0000000']", 1 - assert_select "osm>changeset[max_lon='3.0000000']", 1 - assert_select "osm>changeset[min_lat='1.0000000']", 1 - assert_select "osm>changeset[max_lat='3.0000000']", 1 + assert_select "osm>changeset[min_lon='0.1000000']", 1 + assert_select "osm>changeset[max_lon='0.3000000']", 1 + assert_select "osm>changeset[min_lat='0.1000000']", 1 + assert_select "osm>changeset[max_lat='0.3000000']", 1 end ## @@ -2402,7 +2505,7 @@ module Api assert_response :success, "can't create a new node" node_id = @response.body.to_i - get api_node_path(:id => node_id) + get api_node_path(node_id) assert_response :success, "can't read back new node" node_doc = XML::Parser.string(@response.body).parse node_xml = node_doc.find("//osm/node").first @@ -2413,7 +2516,7 @@ module Api node_xml["lon"] = rand.to_s node_xml["version"] = (i + 1).to_s - put api_node_path(:id => node_id), :params => node_doc.to_s, :headers => auth_header + put api_node_path(node_id), :params => node_doc.to_s, :headers => auth_header assert_response :success, "attempt #{i} should have succeeded" end @@ -2422,7 +2525,7 @@ module Api node_xml["lon"] = rand.to_s node_xml["version"] = offset.to_s - put api_node_path(:id => node_id), :params => node_doc.to_s, :headers => auth_header + put api_node_path(node_id), :params => node_doc.to_s, :headers => auth_header assert_response :conflict, "final attempt should have failed" end