From 35d52765872eaaf35793c71de699aee3d2bf2a38 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sat, 1 Feb 2025 13:00:39 +0300 Subject: [PATCH 1/5] Make api element index paths resourceful --- config/routes.rb | 7 ++++--- test/controllers/api/nodes_controller_test.rb | 10 +++++----- test/controllers/api/relations_controller_test.rb | 10 +++++----- test/controllers/api/ways_controller_test.rb | 10 +++++----- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 19dfc9a77..3079da18f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -39,7 +39,6 @@ OpenStreetMap::Application.routes.draw do get "node/:id" => "nodes#show", :as => :api_node, :id => /\d+/ put "node/:id" => "nodes#update", :id => /\d+/ delete "node/:id" => "nodes#delete", :id => /\d+/ - get "nodes" => "nodes#index" put "way/create" => "ways#create" get "way/:id/history" => "old_ways#history", :as => :api_way_history, :id => /\d+/ @@ -50,7 +49,6 @@ OpenStreetMap::Application.routes.draw do get "way/:id" => "ways#show", :as => :api_way, :id => /\d+/ put "way/:id" => "ways#update", :id => /\d+/ delete "way/:id" => "ways#delete", :id => /\d+/ - get "ways" => "ways#index" put "relation/create" => "relations#create" get "relation/:id/relations" => "relations#relations_for_relation", :as => :relation_relations, :id => /\d+/ @@ -61,10 +59,13 @@ OpenStreetMap::Application.routes.draw do get "relation/:id" => "relations#show", :as => :api_relation, :id => /\d+/ put "relation/:id" => "relations#update", :id => /\d+/ delete "relation/:id" => "relations#delete", :id => /\d+/ - get "relations" => "relations#index" end namespace :api, :path => "api/0.6" do + resources :nodes, :only => :index + resources :ways, :only => :index + resources :relations, :only => :index + resource :map, :only => :show resources :tracepoints, :path => "trackpoints", :only => :index diff --git a/test/controllers/api/nodes_controller_test.rb b/test/controllers/api/nodes_controller_test.rb index 9896c34a5..e70b431e6 100644 --- a/test/controllers/api/nodes_controller_test.rb +++ b/test/controllers/api/nodes_controller_test.rb @@ -434,15 +434,15 @@ module Api node5 = create(:node, :deleted, :with_history, :version => 2) # check error when no parameter provided - get nodes_path + get api_nodes_path assert_response :bad_request # check error when no parameter value provided - get nodes_path(:nodes => "") + get api_nodes_path(:nodes => "") assert_response :bad_request # test a working call - get nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}") + get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}") assert_response :success assert_select "osm" do assert_select "node", :count => 5 @@ -454,7 +454,7 @@ module Api end # test a working call with json format - get nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}", :format => "json") + get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}", :format => "json") js = ActiveSupport::JSON.decode(@response.body) assert_not_nil js @@ -467,7 +467,7 @@ module Api assert_equal 1, (js["elements"].count { |a| a["id"] == node5.id && a["visible"] == false }) # check error when a non-existent node is included - get nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0") + get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0") assert_response :not_found end diff --git a/test/controllers/api/relations_controller_test.rb b/test/controllers/api/relations_controller_test.rb index 5fb62d29f..2919641a9 100644 --- a/test/controllers/api/relations_controller_test.rb +++ b/test/controllers/api/relations_controller_test.rb @@ -174,15 +174,15 @@ module Api relation4.old_relations.find_by(:version => 1).redact!(create(:redaction)) # check error when no parameter provided - get relations_path + get api_relations_path assert_response :bad_request # check error when no parameter value provided - get relations_path(:relations => "") + get api_relations_path(:relations => "") assert_response :bad_request # test a working call - get relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}") + get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}") assert_response :success assert_select "osm" do assert_select "relation", :count => 4 @@ -193,7 +193,7 @@ module Api end # test a working call with json format - get relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}", :format => "json") + get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}", :format => "json") js = ActiveSupport::JSON.decode(@response.body) assert_not_nil js @@ -205,7 +205,7 @@ module Api assert_equal 1, (js["elements"].count { |a| a["id"] == relation4.id && a["visible"].nil? }) # check error when a non-existent relation is included - get relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0") + get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0") assert_response :not_found end diff --git a/test/controllers/api/ways_controller_test.rb b/test/controllers/api/ways_controller_test.rb index 6aa4bdfa5..b3dab44db 100644 --- a/test/controllers/api/ways_controller_test.rb +++ b/test/controllers/api/ways_controller_test.rb @@ -98,15 +98,15 @@ module Api way4 = create(:way) # check error when no parameter provided - get ways_path + get api_ways_path assert_response :bad_request # check error when no parameter value provided - get ways_path(:ways => "") + get api_ways_path(:ways => "") assert_response :bad_request # test a working call - get ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}") + get api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}") assert_response :success assert_select "osm" do assert_select "way", :count => 4 @@ -117,7 +117,7 @@ module Api end # test a working call with json format - get ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}", :format => "json") + get api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}", :format => "json") js = ActiveSupport::JSON.decode(@response.body) assert_not_nil js @@ -129,7 +129,7 @@ module Api assert_equal 1, (js["elements"].count { |a| a["id"] == way4.id && a["visible"].nil? }) # check error when a non-existent way is included - get ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0") + get api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0") assert_response :not_found end From 1d04b69e86315ee4cab6ac28387c84596bae7da0 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sat, 1 Feb 2025 13:02:31 +0300 Subject: [PATCH 2/5] Move api element index tests up --- test/controllers/api/nodes_controller_test.rb | 110 +++++++++--------- .../api/relations_controller_test.rb | 106 ++++++++--------- test/controllers/api/ways_controller_test.rb | 104 ++++++++--------- 3 files changed, 160 insertions(+), 160 deletions(-) diff --git a/test/controllers/api/nodes_controller_test.rb b/test/controllers/api/nodes_controller_test.rb index e70b431e6..075b8b61b 100644 --- a/test/controllers/api/nodes_controller_test.rb +++ b/test/controllers/api/nodes_controller_test.rb @@ -5,6 +5,14 @@ module Api ## # test all routes which lead to this controller def test_routes + assert_routing( + { :path => "/api/0.6/nodes", :method => :get }, + { :controller => "api/nodes", :action => "index" } + ) + assert_routing( + { :path => "/api/0.6/nodes.json", :method => :get }, + { :controller => "api/nodes", :action => "index", :format => "json" } + ) assert_routing( { :path => "/api/0.6/node/create", :method => :put }, { :controller => "api/nodes", :action => "create" } @@ -25,14 +33,53 @@ module Api { :path => "/api/0.6/node/1", :method => :delete }, { :controller => "api/nodes", :action => "delete", :id => "1" } ) - assert_routing( - { :path => "/api/0.6/nodes", :method => :get }, - { :controller => "api/nodes", :action => "index" } - ) - assert_routing( - { :path => "/api/0.6/nodes.json", :method => :get }, - { :controller => "api/nodes", :action => "index", :format => "json" } - ) + end + + ## + # test fetching multiple nodes + def test_index + node1 = create(:node) + node2 = create(:node, :deleted) + node3 = create(:node) + node4 = create(:node, :with_history, :version => 2) + node5 = create(:node, :deleted, :with_history, :version => 2) + + # check error when no parameter provided + get api_nodes_path + assert_response :bad_request + + # check error when no parameter value provided + get api_nodes_path(:nodes => "") + assert_response :bad_request + + # test a working call + get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}") + assert_response :success + assert_select "osm" do + assert_select "node", :count => 5 + assert_select "node[id='#{node1.id}'][visible='true']", :count => 1 + assert_select "node[id='#{node2.id}'][visible='false']", :count => 1 + assert_select "node[id='#{node3.id}'][visible='true']", :count => 1 + assert_select "node[id='#{node4.id}'][visible='true']", :count => 1 + assert_select "node[id='#{node5.id}'][visible='false']", :count => 1 + end + + # test a working call with json format + get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}", :format => "json") + + js = ActiveSupport::JSON.decode(@response.body) + assert_not_nil js + assert_equal 5, js["elements"].count + assert_equal 5, (js["elements"].count { |a| a["type"] == "node" }) + assert_equal 1, (js["elements"].count { |a| a["id"] == node1.id && a["visible"].nil? }) + assert_equal 1, (js["elements"].count { |a| a["id"] == node2.id && a["visible"] == false }) + assert_equal 1, (js["elements"].count { |a| a["id"] == node3.id && a["visible"].nil? }) + assert_equal 1, (js["elements"].count { |a| a["id"] == node4.id && a["visible"].nil? }) + assert_equal 1, (js["elements"].count { |a| a["id"] == node5.id && a["visible"] == false }) + + # check error when a non-existent node is included + get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0") + assert_response :not_found end def test_create @@ -424,53 +471,6 @@ module Api assert_response :success, "a valid update request failed" end - ## - # test fetching multiple nodes - def test_index - node1 = create(:node) - node2 = create(:node, :deleted) - node3 = create(:node) - node4 = create(:node, :with_history, :version => 2) - node5 = create(:node, :deleted, :with_history, :version => 2) - - # check error when no parameter provided - get api_nodes_path - assert_response :bad_request - - # check error when no parameter value provided - get api_nodes_path(:nodes => "") - assert_response :bad_request - - # test a working call - get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}") - assert_response :success - assert_select "osm" do - assert_select "node", :count => 5 - assert_select "node[id='#{node1.id}'][visible='true']", :count => 1 - assert_select "node[id='#{node2.id}'][visible='false']", :count => 1 - assert_select "node[id='#{node3.id}'][visible='true']", :count => 1 - assert_select "node[id='#{node4.id}'][visible='true']", :count => 1 - assert_select "node[id='#{node5.id}'][visible='false']", :count => 1 - end - - # test a working call with json format - get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}", :format => "json") - - js = ActiveSupport::JSON.decode(@response.body) - assert_not_nil js - assert_equal 5, js["elements"].count - assert_equal 5, (js["elements"].count { |a| a["type"] == "node" }) - assert_equal 1, (js["elements"].count { |a| a["id"] == node1.id && a["visible"].nil? }) - assert_equal 1, (js["elements"].count { |a| a["id"] == node2.id && a["visible"] == false }) - assert_equal 1, (js["elements"].count { |a| a["id"] == node3.id && a["visible"].nil? }) - assert_equal 1, (js["elements"].count { |a| a["id"] == node4.id && a["visible"].nil? }) - assert_equal 1, (js["elements"].count { |a| a["id"] == node5.id && a["visible"] == false }) - - # check error when a non-existent node is included - get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0") - assert_response :not_found - end - ## # test adding tags to a node def test_duplicate_tags diff --git a/test/controllers/api/relations_controller_test.rb b/test/controllers/api/relations_controller_test.rb index 2919641a9..efe17f885 100644 --- a/test/controllers/api/relations_controller_test.rb +++ b/test/controllers/api/relations_controller_test.rb @@ -5,6 +5,14 @@ module Api ## # test all routes which lead to this controller def test_routes + assert_routing( + { :path => "/api/0.6/relations", :method => :get }, + { :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/relation/create", :method => :put }, { :controller => "api/relations", :action => "create" } @@ -33,14 +41,6 @@ module Api { :path => "/api/0.6/relation/1", :method => :delete }, { :controller => "api/relations", :action => "delete", :id => "1" } ) - assert_routing( - { :path => "/api/0.6/relations", :method => :get }, - { :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 }, @@ -68,6 +68,51 @@ module Api ) end + ## + # test fetching multiple relations + def test_index + relation1 = create(:relation) + relation2 = create(:relation, :deleted) + relation3 = create(:relation, :with_history, :version => 2) + relation4 = create(:relation, :with_history, :version => 2) + relation4.old_relations.find_by(:version => 1).redact!(create(:redaction)) + + # check error when no parameter provided + get api_relations_path + assert_response :bad_request + + # check error when no parameter value provided + get api_relations_path(:relations => "") + assert_response :bad_request + + # test a working call + get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}") + assert_response :success + assert_select "osm" do + assert_select "relation", :count => 4 + assert_select "relation[id='#{relation1.id}'][visible='true']", :count => 1 + assert_select "relation[id='#{relation2.id}'][visible='false']", :count => 1 + assert_select "relation[id='#{relation3.id}'][visible='true']", :count => 1 + assert_select "relation[id='#{relation4.id}'][visible='true']", :count => 1 + end + + # test a working call with json format + get api_relations_path(: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 api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0") + assert_response :not_found + end + # ------------------------------------- # Test showing relations. # ------------------------------------- @@ -164,51 +209,6 @@ module Api # FIXME: check whether this contains the stuff we want! end - ## - # test fetching multiple relations - def test_index - relation1 = create(:relation) - relation2 = create(:relation, :deleted) - relation3 = create(:relation, :with_history, :version => 2) - relation4 = create(:relation, :with_history, :version => 2) - relation4.old_relations.find_by(:version => 1).redact!(create(:redaction)) - - # check error when no parameter provided - get api_relations_path - assert_response :bad_request - - # check error when no parameter value provided - get api_relations_path(:relations => "") - assert_response :bad_request - - # test a working call - get api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}") - assert_response :success - assert_select "osm" do - assert_select "relation", :count => 4 - assert_select "relation[id='#{relation1.id}'][visible='true']", :count => 1 - assert_select "relation[id='#{relation2.id}'][visible='false']", :count => 1 - assert_select "relation[id='#{relation3.id}'][visible='true']", :count => 1 - assert_select "relation[id='#{relation4.id}'][visible='true']", :count => 1 - end - - # test a working call with json format - get api_relations_path(: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 api_relations_path(:relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0") - assert_response :not_found - end - # ------------------------------------- # Test simple relation creation. # ------------------------------------- diff --git a/test/controllers/api/ways_controller_test.rb b/test/controllers/api/ways_controller_test.rb index b3dab44db..a63b6a156 100644 --- a/test/controllers/api/ways_controller_test.rb +++ b/test/controllers/api/ways_controller_test.rb @@ -5,6 +5,14 @@ module Api ## # test all routes which lead to this controller def test_routes + assert_routing( + { :path => "/api/0.6/ways", :method => :get }, + { :controller => "api/ways", :action => "index" } + ) + assert_routing( + { :path => "/api/0.6/ways.json", :method => :get }, + { :controller => "api/ways", :action => "index", :format => "json" } + ) assert_routing( { :path => "/api/0.6/way/create", :method => :put }, { :controller => "api/ways", :action => "create" } @@ -33,14 +41,50 @@ module Api { :path => "/api/0.6/way/1", :method => :delete }, { :controller => "api/ways", :action => "delete", :id => "1" } ) - assert_routing( - { :path => "/api/0.6/ways", :method => :get }, - { :controller => "api/ways", :action => "index" } - ) - assert_routing( - { :path => "/api/0.6/ways.json", :method => :get }, - { :controller => "api/ways", :action => "index", :format => "json" } - ) + end + + ## + # test fetching multiple ways + def test_index + way1 = create(:way) + way2 = create(:way, :deleted) + way3 = create(:way) + way4 = create(:way) + + # check error when no parameter provided + get api_ways_path + assert_response :bad_request + + # check error when no parameter value provided + get api_ways_path(:ways => "") + assert_response :bad_request + + # test a working call + get api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}") + assert_response :success + assert_select "osm" do + assert_select "way", :count => 4 + assert_select "way[id='#{way1.id}'][visible='true']", :count => 1 + assert_select "way[id='#{way2.id}'][visible='false']", :count => 1 + assert_select "way[id='#{way3.id}'][visible='true']", :count => 1 + assert_select "way[id='#{way4.id}'][visible='true']", :count => 1 + end + + # test a working call with json format + get api_ways_path(: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 api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0") + assert_response :not_found end # ------------------------------------- @@ -89,50 +133,6 @@ module Api assert_response :gone end - ## - # test fetching multiple ways - def test_index - way1 = create(:way) - way2 = create(:way, :deleted) - way3 = create(:way) - way4 = create(:way) - - # check error when no parameter provided - get api_ways_path - assert_response :bad_request - - # check error when no parameter value provided - get api_ways_path(:ways => "") - assert_response :bad_request - - # test a working call - get api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}") - assert_response :success - assert_select "osm" do - assert_select "way", :count => 4 - assert_select "way[id='#{way1.id}'][visible='true']", :count => 1 - assert_select "way[id='#{way2.id}'][visible='false']", :count => 1 - assert_select "way[id='#{way3.id}'][visible='true']", :count => 1 - assert_select "way[id='#{way4.id}'][visible='true']", :count => 1 - end - - # test a working call with json format - get api_ways_path(: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 api_ways_path(:ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0") - assert_response :not_found - end - # ------------------------------------- # Test simple way creation. # ------------------------------------- From 96671cf0c0f014e61ac1eb3f11460376e067f0c2 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sat, 1 Feb 2025 13:19:07 +0300 Subject: [PATCH 3/5] Make api create node path resourceful --- config/routes.rb | 6 ++- .../api/changesets_controller_test.rb | 6 +-- test/controllers/api/nodes_controller_test.rb | 37 +++++++++++-------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 3079da18f..75e05d2ca 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -30,7 +30,6 @@ OpenStreetMap::Application.routes.draw do post "changeset/comment/:id/hide" => "changeset_comments#destroy", :as => :changeset_comment_hide, :id => /\d+/ post "changeset/comment/:id/unhide" => "changeset_comments#restore", :as => :changeset_comment_unhide, :id => /\d+/ - put "node/create" => "nodes#create" get "node/:id/ways" => "ways#ways_for_node", :as => :node_ways, :id => /\d+/ get "node/:id/relations" => "relations#relations_for_node", :as => :node_relations, :id => /\d+/ get "node/:id/history" => "old_nodes#history", :as => :api_node_history, :id => /\d+/ @@ -62,8 +61,11 @@ OpenStreetMap::Application.routes.draw do end namespace :api, :path => "api/0.6" do - resources :nodes, :only => :index + resources :nodes, :only => [:index, :create] + put "node/create" => "nodes#create", :as => nil + resources :ways, :only => :index + resources :relations, :only => :index resource :map, :only => :show diff --git a/test/controllers/api/changesets_controller_test.rb b/test/controllers/api/changesets_controller_test.rb index c0be42a21..d9afb94a2 100644 --- a/test/controllers/api/changesets_controller_test.rb +++ b/test/controllers/api/changesets_controller_test.rb @@ -2115,7 +2115,7 @@ module Api # add a single node to it with_controller(NodesController.new) do xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header assert_response :success, "Couldn't create node." end @@ -2130,7 +2130,7 @@ module Api # add another node to it with_controller(NodesController.new) do xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header assert_response :success, "Couldn't create second node." end @@ -2500,7 +2500,7 @@ module Api with_controller(NodesController.new) do # create a new node xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header assert_response :success, "can't create a new node" node_id = @response.body.to_i diff --git a/test/controllers/api/nodes_controller_test.rb b/test/controllers/api/nodes_controller_test.rb index 075b8b61b..04120dace 100644 --- a/test/controllers/api/nodes_controller_test.rb +++ b/test/controllers/api/nodes_controller_test.rb @@ -14,7 +14,7 @@ module Api { :controller => "api/nodes", :action => "index", :format => "json" } ) assert_routing( - { :path => "/api/0.6/node/create", :method => :put }, + { :path => "/api/0.6/nodes", :method => :post }, { :controller => "api/nodes", :action => "create" } ) assert_routing( @@ -33,6 +33,11 @@ module Api { :path => "/api/0.6/node/1", :method => :delete }, { :controller => "api/nodes", :action => "delete", :id => "1" } ) + + assert_recognizes( + { :controller => "api/nodes", :action => "create" }, + { :path => "/api/0.6/node/create", :method => :put } + ) end ## @@ -96,7 +101,7 @@ module Api # create a minimal xml file xml = "" assert_difference("OldNode.count", 0) do - put node_create_path, :params => xml + post api_nodes_path, :params => xml end # hope for unauthorized assert_response :unauthorized, "node upload did not return unauthorized status" @@ -107,7 +112,7 @@ module Api # create a minimal xml file xml = "" assert_difference("Node.count", 0) do - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header end # hope for success assert_require_public_data "node create did not return forbidden status" @@ -117,7 +122,7 @@ module Api # create a minimal xml file xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header # hope for success assert_response :success, "node upload did not return success status" @@ -145,14 +150,14 @@ module Api # test that the upload is rejected when xml is valid, but osm doc isn't xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header assert_response :bad_request, "node upload did not return bad_request status" assert_equal "Cannot parse valid node from xml string . XML doesn't contain an osm/node element.", @response.body # test that the upload is rejected when no lat is supplied # create a minimal xml file xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header # hope for success assert_response :bad_request, "node upload did not return bad_request status" assert_equal "Cannot parse valid node from xml string . lat missing", @response.body @@ -160,7 +165,7 @@ module Api # test that the upload is rejected when no lon is supplied # create a minimal xml file xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header # hope for success assert_response :bad_request, "node upload did not return bad_request status" assert_equal "Cannot parse valid node from xml string . lon missing", @response.body @@ -168,7 +173,7 @@ module Api # test that the upload is rejected when lat is non-numeric # create a minimal xml file xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header # hope for success assert_response :bad_request, "node upload did not return bad_request status" assert_equal "Cannot parse valid node from xml string . lat not a number", @response.body @@ -176,14 +181,14 @@ module Api # test that the upload is rejected when lon is non-numeric # create a minimal xml file xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header # hope for success assert_response :bad_request, "node upload did not return bad_request status" assert_equal "Cannot parse valid node from xml string . lon not a number", @response.body # test that the upload is rejected when we have a tag which is too long xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header assert_response :bad_request, "node upload did not return bad_request status" assert_match(/ v: is too long \(maximum is 255 characters\) /, @response.body) end @@ -510,7 +515,7 @@ module Api xml = "" \ "" \ "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header assert_require_public_data "Shouldn't be able to create with non-public user" ## Then try with the public data user @@ -521,7 +526,7 @@ module Api xml = "" \ "" \ "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header assert_response :success nodeid = @response.body @@ -556,7 +561,7 @@ module Api # try creating a node xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header assert_response :success, "node create did not return success status" # get the id of the node we created @@ -574,7 +579,7 @@ module Api # try creating a node, which should be rate limited xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header assert_response :too_many_requests, "node create did not hit rate limit" end @@ -603,7 +608,7 @@ module Api # try creating a node xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header assert_response :success, "node create did not return success status" # get the id of the node we created @@ -621,7 +626,7 @@ module Api # try creating a node, which should be rate limited xml = "" - put node_create_path, :params => xml, :headers => auth_header + post api_nodes_path, :params => xml, :headers => auth_header assert_response :too_many_requests, "node create did not hit rate limit" end From 2ad75d72af79fe03a31e4a9a750604d90b4b9ffa Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sat, 1 Feb 2025 13:25:23 +0300 Subject: [PATCH 4/5] Make api create way path resourceful --- config/routes.rb | 4 +-- test/controllers/api/ways_controller_test.rb | 37 +++++++++++--------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 75e05d2ca..b420dcedb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -39,7 +39,6 @@ OpenStreetMap::Application.routes.draw do put "node/:id" => "nodes#update", :id => /\d+/ delete "node/:id" => "nodes#delete", :id => /\d+/ - put "way/create" => "ways#create" get "way/:id/history" => "old_ways#history", :as => :api_way_history, :id => /\d+/ get "way/:id/full" => "ways#full", :as => :way_full, :id => /\d+/ get "way/:id/relations" => "relations#relations_for_way", :as => :way_relations, :id => /\d+/ @@ -64,7 +63,8 @@ OpenStreetMap::Application.routes.draw do resources :nodes, :only => [:index, :create] put "node/create" => "nodes#create", :as => nil - resources :ways, :only => :index + resources :ways, :only => [:index, :create] + put "way/create" => "ways#create", :as => nil resources :relations, :only => :index diff --git a/test/controllers/api/ways_controller_test.rb b/test/controllers/api/ways_controller_test.rb index a63b6a156..e81b49b81 100644 --- a/test/controllers/api/ways_controller_test.rb +++ b/test/controllers/api/ways_controller_test.rb @@ -14,7 +14,7 @@ module Api { :controller => "api/ways", :action => "index", :format => "json" } ) assert_routing( - { :path => "/api/0.6/way/create", :method => :put }, + { :path => "/api/0.6/ways", :method => :post }, { :controller => "api/ways", :action => "create" } ) assert_routing( @@ -41,6 +41,11 @@ module Api { :path => "/api/0.6/way/1", :method => :delete }, { :controller => "api/ways", :action => "delete", :id => "1" } ) + + assert_recognizes( + { :controller => "api/ways", :action => "create" }, + { :path => "/api/0.6/way/create", :method => :put } + ) end ## @@ -155,7 +160,7 @@ module Api xml = "" \ "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header # hope for failure assert_response :forbidden, "way upload did not return forbidden status" @@ -170,7 +175,7 @@ module Api xml = "" \ "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header # hope for success assert_response :success, "way upload did not return success status" @@ -213,7 +218,7 @@ module Api # create a way with non-existing node xml = "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header # expect failure assert_response :forbidden, "way upload with invalid node using a private user did not return 'forbidden'" @@ -221,7 +226,7 @@ module Api # create a way with no nodes xml = "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header # expect failure assert_response :forbidden, "way upload with no node using a private userdid not return 'forbidden'" @@ -229,7 +234,7 @@ module Api # create a way inside a closed changeset xml = "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header # expect failure assert_response :forbidden, "way upload to closed changeset with a private user did not return 'forbidden'" @@ -241,7 +246,7 @@ module Api # create a way with non-existing node xml = "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header # expect failure assert_response :precondition_failed, "way upload with invalid node did not return 'precondition failed'" @@ -250,7 +255,7 @@ module Api # create a way with no nodes xml = "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header # expect failure assert_response :precondition_failed, "way upload with no node did not return 'precondition failed'" @@ -259,7 +264,7 @@ module Api # create a way inside a closed changeset xml = "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header # expect failure assert_response :conflict, "way upload to closed changeset did not return 'conflict'" @@ -269,7 +274,7 @@ module Api "" \ "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header # expect failure assert_response :bad_request, "way upload to with too long tag did not return 'bad_request'" @@ -696,7 +701,7 @@ module Api way_str << "" # try and upload it - put way_create_path, :params => way_str, :headers => auth_header + post api_ways_path, :params => way_str, :headers => auth_header assert_response :forbidden, "adding new duplicate tags to a way with a non-public user should fail with 'forbidden'" @@ -711,7 +716,7 @@ module Api way_str << "" # try and upload it - put way_create_path, :params => way_str, :headers => auth_header + post api_ways_path, :params => way_str, :headers => auth_header assert_response :bad_request, "adding new duplicate tags to a way should fail with 'bad request'" assert_equal "Element way/ has duplicate tags with key addr:housenumber", @response.body @@ -775,7 +780,7 @@ module Api xml = "" \ "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header assert_response :success, "way create did not return success status" # get the id of the way we created @@ -797,7 +802,7 @@ module Api xml = "" \ "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header assert_response :too_many_requests, "way create did not hit rate limit" end @@ -832,7 +837,7 @@ module Api xml = "" \ "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header assert_response :success, "way create did not return success status" # get the id of the way we created @@ -854,7 +859,7 @@ module Api xml = "" \ "" \ "" - put way_create_path, :params => xml, :headers => auth_header + post api_ways_path, :params => xml, :headers => auth_header assert_response :too_many_requests, "way create did not hit rate limit" end From b6c2a3936350e52cdd5f4900d5752ed326804e09 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sat, 1 Feb 2025 13:45:57 +0300 Subject: [PATCH 5/5] Make api create relation path resourceful --- config/routes.rb | 4 +- .../api/relations_controller_test.rb | 43 +++++++++++-------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index b420dcedb..2ba9bdb93 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -48,7 +48,6 @@ OpenStreetMap::Application.routes.draw do put "way/:id" => "ways#update", :id => /\d+/ delete "way/:id" => "ways#delete", :id => /\d+/ - put "relation/create" => "relations#create" get "relation/:id/relations" => "relations#relations_for_relation", :as => :relation_relations, :id => /\d+/ get "relation/:id/history" => "old_relations#history", :as => :api_relation_history, :id => /\d+/ get "relation/:id/full" => "relations#full", :as => :relation_full, :id => /\d+/ @@ -66,7 +65,8 @@ OpenStreetMap::Application.routes.draw do resources :ways, :only => [:index, :create] put "way/create" => "ways#create", :as => nil - resources :relations, :only => :index + resources :relations, :only => [:index, :create] + put "relation/create" => "relations#create", :as => nil resource :map, :only => :show diff --git a/test/controllers/api/relations_controller_test.rb b/test/controllers/api/relations_controller_test.rb index efe17f885..5147d851f 100644 --- a/test/controllers/api/relations_controller_test.rb +++ b/test/controllers/api/relations_controller_test.rb @@ -14,7 +14,7 @@ module Api { :controller => "api/relations", :action => "index", :format => "json" } ) assert_routing( - { :path => "/api/0.6/relation/create", :method => :put }, + { :path => "/api/0.6/relations", :method => :post }, { :controller => "api/relations", :action => "create" } ) assert_routing( @@ -66,6 +66,11 @@ module Api { :path => "/api/0.6/relation/1/relations.json", :method => :get }, { :controller => "api/relations", :action => "relations_for_relation", :id => "1", :format => "json" } ) + + assert_recognizes( + { :controller => "api/relations", :action => "create" }, + { :path => "/api/0.6/relation/create", :method => :put } + ) end ## @@ -225,7 +230,7 @@ module Api # create an relation without members xml = "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header # hope for forbidden, due to user assert_response :forbidden, "relation upload should have failed with forbidden" @@ -236,7 +241,7 @@ module Api xml = "" \ "" \ "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header # hope for forbidden due to user assert_response :forbidden, "relation upload did not return forbidden status" @@ -246,7 +251,7 @@ module Api # need a role attribute to be included xml = "" \ "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header # hope for forbidden due to user assert_response :forbidden, "relation upload did not return forbidden status" @@ -257,7 +262,7 @@ module Api "" \ "" \ "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header # hope for forbidden, due to user assert_response :forbidden, "relation upload did not return success status" @@ -267,7 +272,7 @@ module Api # create an relation without members xml = "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header # hope for success assert_response :success, "relation upload did not return success status" @@ -295,7 +300,7 @@ module Api xml = "" \ "" \ "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header # hope for success assert_response :success, "relation upload did not return success status" @@ -323,7 +328,7 @@ module Api # need a role attribute to be included xml = "" \ "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header # hope for success assert_response :success, "relation upload did not return success status" @@ -352,7 +357,7 @@ module Api "" \ "" \ "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header # hope for success assert_response :success, "relation upload did not return success status" @@ -472,7 +477,7 @@ module Api xml = "" \ "" \ "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header # expect failure assert_response :precondition_failed, "relation upload with invalid node did not return 'precondition failed'" @@ -493,7 +498,7 @@ module Api xml = "" \ "" \ "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header # expect failure assert_response :bad_request assert_match(/Cannot parse valid relation from xml string/, @response.body) @@ -757,7 +762,7 @@ module Api OSM doc = XML::Parser.string(doc_str).parse - put relation_create_path, :params => doc.to_s, :headers => auth_header + post api_relations_path, :params => doc.to_s, :headers => auth_header assert_response :success, "can't create a relation: #{@response.body}" relation_id = @response.body.to_i @@ -818,13 +823,13 @@ module Api ## First try with the private user auth_header = bearer_authorization_header private_user - put relation_create_path, :params => doc.to_s, :headers => auth_header + post api_relations_path, :params => doc.to_s, :headers => auth_header assert_response :forbidden ## Now try with the public user auth_header = bearer_authorization_header user - put relation_create_path, :params => doc.to_s, :headers => auth_header + post api_relations_path, :params => doc.to_s, :headers => auth_header assert_response :success, "can't create a relation: #{@response.body}" relation_id = @response.body.to_i @@ -857,7 +862,7 @@ module Api doc = XML::Parser.string(doc_str).parse auth_header = bearer_authorization_header user - put relation_create_path, :params => doc.to_s, :headers => auth_header + post api_relations_path, :params => doc.to_s, :headers => auth_header assert_response :success, "can't create a relation: #{@response.body}" relation_id = @response.body.to_i @@ -929,7 +934,7 @@ module Api "" \ "" \ "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header assert_response :success, "relation create did not return success status" # get the id of the relation we created @@ -953,7 +958,7 @@ module Api "" \ "" \ "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header assert_response :too_many_requests, "relation create did not hit rate limit" end @@ -989,7 +994,7 @@ module Api "" \ "" \ "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header assert_response :success, "relation create did not return success status" # get the id of the relation we created @@ -1013,7 +1018,7 @@ module Api "" \ "" \ "" - put relation_create_path, :params => xml, :headers => auth_header + post api_relations_path, :params => xml, :headers => auth_header assert_response :too_many_requests, "relation create did not hit rate limit" end