4   class NodesControllerTest < ActionDispatch::IntegrationTest
 
   6     # test all routes which lead to this controller
 
   9         { :path => "/api/0.6/nodes", :method => :get },
 
  10         { :controller => "api/nodes", :action => "index" }
 
  13         { :path => "/api/0.6/nodes.json", :method => :get },
 
  14         { :controller => "api/nodes", :action => "index", :format => "json" }
 
  17         { :path => "/api/0.6/nodes", :method => :post },
 
  18         { :controller => "api/nodes", :action => "create" }
 
  21         { :path => "/api/0.6/node/1", :method => :get },
 
  22         { :controller => "api/nodes", :action => "show", :id => "1" }
 
  25         { :path => "/api/0.6/node/1.json", :method => :get },
 
  26         { :controller => "api/nodes", :action => "show", :id => "1", :format => "json" }
 
  29         { :path => "/api/0.6/node/1", :method => :put },
 
  30         { :controller => "api/nodes", :action => "update", :id => "1" }
 
  33         { :path => "/api/0.6/node/1", :method => :delete },
 
  34         { :controller => "api/nodes", :action => "destroy", :id => "1" }
 
  38         { :controller => "api/nodes", :action => "create" },
 
  39         { :path => "/api/0.6/node/create", :method => :put }
 
  44     # test fetching multiple nodes
 
  47       node2 = create(:node, :deleted)
 
  49       node4 = create(:node, :with_history, :version => 2)
 
  50       node5 = create(:node, :deleted, :with_history, :version => 2)
 
  52       # check error when no parameter provided
 
  54       assert_response :bad_request
 
  56       # check error when no parameter value provided
 
  57       get api_nodes_path(:nodes => "")
 
  58       assert_response :bad_request
 
  61       get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}")
 
  62       assert_response :success
 
  63       assert_select "osm" do
 
  64         assert_select "node", :count => 5
 
  65         assert_select "node[id='#{node1.id}'][visible='true']", :count => 1
 
  66         assert_select "node[id='#{node2.id}'][visible='false']", :count => 1
 
  67         assert_select "node[id='#{node3.id}'][visible='true']", :count => 1
 
  68         assert_select "node[id='#{node4.id}'][visible='true']", :count => 1
 
  69         assert_select "node[id='#{node5.id}'][visible='false']", :count => 1
 
  72       # test a working call with json format
 
  73       get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}", :format => "json")
 
  75       js = ActiveSupport::JSON.decode(@response.body)
 
  77       assert_equal 5, js["elements"].count
 
  78       assert_equal 5, (js["elements"].count { |a| a["type"] == "node" })
 
  79       assert_equal 1, (js["elements"].count { |a| a["id"] == node1.id && a["visible"].nil? })
 
  80       assert_equal 1, (js["elements"].count { |a| a["id"] == node2.id && a["visible"] == false })
 
  81       assert_equal 1, (js["elements"].count { |a| a["id"] == node3.id && a["visible"].nil? })
 
  82       assert_equal 1, (js["elements"].count { |a| a["id"] == node4.id && a["visible"].nil? })
 
  83       assert_equal 1, (js["elements"].count { |a| a["id"] == node5.id && a["visible"] == false })
 
  85       # check error when a non-existent node is included
 
  86       get api_nodes_path(:nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0")
 
  87       assert_response :not_found
 
  91       private_user = create(:user, :data_public => false)
 
  92       private_changeset = create(:changeset, :user => private_user)
 
  94       changeset = create(:changeset, :user => user)
 
  96       # create a node with random lat/lon
 
  97       lat = rand(-50..50) + rand
 
  98       lon = rand(-50..50) + rand
 
 100       ## First try with no auth
 
 101       # create a minimal xml file
 
 102       xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
 
 103       assert_difference("OldNode.count", 0) do
 
 104         post api_nodes_path, :params => xml
 
 106       # hope for unauthorized
 
 107       assert_response :unauthorized, "node upload did not return unauthorized status"
 
 109       ## Now try with the user which doesn't have their data public
 
 110       auth_header = bearer_authorization_header private_user
 
 112       # create a minimal xml file
 
 113       xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{private_changeset.id}'/></osm>"
 
 114       assert_difference("Node.count", 0) do
 
 115         post api_nodes_path, :params => xml, :headers => auth_header
 
 118       assert_require_public_data "node create did not return forbidden status"
 
 120       ## Now try with the user that has the public data
 
 121       auth_header = bearer_authorization_header user
 
 123       # create a minimal xml file
 
 124       xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
 
 125       post api_nodes_path, :params => xml, :headers => auth_header
 
 127       assert_response :success, "node upload did not return success status"
 
 129       # read id of created node and search for it
 
 130       nodeid = @response.body
 
 131       checknode = Node.find(nodeid)
 
 132       assert_not_nil checknode, "uploaded node not found in data base after upload"
 
 134       assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
 
 135       assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
 
 136       assert_equal changeset.id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
 
 137       assert checknode.visible, "saved node is not visible"
 
 140     def test_create_invalid_xml
 
 141       ## Only test public user here, as test_create should cover what's the forbidden
 
 142       ## that would occur here
 
 145       changeset = create(:changeset, :user => user)
 
 147       auth_header = bearer_authorization_header user
 
 151       # test that the upload is rejected when xml is valid, but osm doc isn't
 
 153       post api_nodes_path, :params => xml, :headers => auth_header
 
 154       assert_response :bad_request, "node upload did not return bad_request status"
 
 155       assert_equal "Cannot parse valid node from xml string <create/>. XML doesn't contain an osm/node element.", @response.body
 
 157       # test that the upload is rejected when no lat is supplied
 
 158       # create a minimal xml file
 
 159       xml = "<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>"
 
 160       post api_nodes_path, :params => xml, :headers => auth_header
 
 162       assert_response :bad_request, "node upload did not return bad_request status"
 
 163       assert_equal "Cannot parse valid node from xml string <node lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat missing", @response.body
 
 165       # test that the upload is rejected when no lon is supplied
 
 166       # create a minimal xml file
 
 167       xml = "<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>"
 
 168       post api_nodes_path, :params => xml, :headers => auth_header
 
 170       assert_response :bad_request, "node upload did not return bad_request status"
 
 171       assert_equal "Cannot parse valid node from xml string <node lat=\"3.434\" changeset=\"#{changeset.id}\"/>. lon missing", @response.body
 
 173       # test that the upload is rejected when lat is non-numeric
 
 174       # create a minimal xml file
 
 175       xml = "<osm><node lat='abc' lon='#{lon}' changeset='#{changeset.id}'/></osm>"
 
 176       post api_nodes_path, :params => xml, :headers => auth_header
 
 178       assert_response :bad_request, "node upload did not return bad_request status"
 
 179       assert_equal "Cannot parse valid node from xml string <node lat=\"abc\" lon=\"#{lon}\" changeset=\"#{changeset.id}\"/>. lat not a number", @response.body
 
 181       # test that the upload is rejected when lon is non-numeric
 
 182       # create a minimal xml file
 
 183       xml = "<osm><node lat='#{lat}' lon='abc' changeset='#{changeset.id}'/></osm>"
 
 184       post api_nodes_path, :params => xml, :headers => auth_header
 
 186       assert_response :bad_request, "node upload did not return bad_request status"
 
 187       assert_equal "Cannot parse valid node from xml string <node lat=\"#{lat}\" lon=\"abc\" changeset=\"#{changeset.id}\"/>. lon not a number", @response.body
 
 189       # test that the upload is rejected when we have a tag which is too long
 
 190       xml = "<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'><tag k='foo' v='#{'x' * 256}'/></node></osm>"
 
 191       post api_nodes_path, :params => xml, :headers => auth_header
 
 192       assert_response :bad_request, "node upload did not return bad_request status"
 
 193       assert_match(/ v: is too long \(maximum is 255 characters\) /, @response.body)
 
 196     def test_show_not_found
 
 198       assert_response :not_found
 
 201     def test_show_deleted
 
 202       get api_node_path(create(:node, :deleted))
 
 203       assert_response :gone
 
 207       node = create(:node, :timestamp => "2021-02-03T00:00:00Z")
 
 209       get api_node_path(node)
 
 211       assert_response :success
 
 212       assert_not_nil @response.header["Last-Modified"]
 
 213       assert_equal "2021-02-03T00:00:00Z", Time.parse(@response.header["Last-Modified"]).utc.xmlschema
 
 216     # Ensure the lat/lon is formatted as a decimal e.g. not 4.0e-05
 
 217     def test_lat_lon_xml_format
 
 218       node = create(:node, :latitude => (0.00004 * OldNode::SCALE).to_i, :longitude => (0.00008 * OldNode::SCALE).to_i)
 
 220       get api_node_path(node)
 
 221       assert_match(/lat="0.0000400"/, response.body)
 
 222       assert_match(/lon="0.0000800"/, response.body)
 
 225     # this tests deletion restrictions - basic deletion is tested in the unit
 
 228       private_user = create(:user, :data_public => false)
 
 229       private_user_changeset = create(:changeset, :user => private_user)
 
 230       private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
 
 231       private_node = create(:node, :changeset => private_user_changeset)
 
 232       private_deleted_node = create(:node, :deleted, :changeset => private_user_changeset)
 
 234       ## first try to delete node without auth
 
 235       delete api_node_path(private_node)
 
 236       assert_response :unauthorized
 
 238       ## now set auth for the non-data public user
 
 239       auth_header = bearer_authorization_header private_user
 
 241       # try to delete with an invalid (closed) changeset
 
 242       xml = update_changeset(xml_for_node(private_node), private_user_closed_changeset.id)
 
 243       delete api_node_path(private_node), :params => xml.to_s, :headers => auth_header
 
 244       assert_require_public_data("non-public user shouldn't be able to delete node")
 
 246       # try to delete with an invalid (non-existent) changeset
 
 247       xml = update_changeset(xml_for_node(private_node), 0)
 
 248       delete api_node_path(private_node), :params => xml.to_s, :headers => auth_header
 
 249       assert_require_public_data("shouldn't be able to delete node, when user's data is private")
 
 251       # valid delete now takes a payload
 
 252       xml = xml_for_node(private_node)
 
 253       delete api_node_path(private_node), :params => xml.to_s, :headers => auth_header
 
 254       assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
 
 256       # this won't work since the node is already deleted
 
 257       xml = xml_for_node(private_deleted_node)
 
 258       delete api_node_path(private_deleted_node), :params => xml.to_s, :headers => auth_header
 
 259       assert_require_public_data
 
 261       # this won't work since the node never existed
 
 262       delete api_node_path(0), :headers => auth_header
 
 263       assert_require_public_data
 
 265       ## these test whether nodes which are in-use can be deleted:
 
 267       private_used_node = create(:node, :changeset => private_user_changeset)
 
 268       create(:way_node, :node => private_used_node)
 
 270       xml = xml_for_node(private_used_node)
 
 271       delete api_node_path(private_used_node), :params => xml.to_s, :headers => auth_header
 
 272       assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
 
 275       private_used_node2 = create(:node, :changeset => private_user_changeset)
 
 276       create(:relation_member, :member => private_used_node2)
 
 278       xml = xml_for_node(private_used_node2)
 
 279       delete api_node_path(private_used_node2), :params => xml.to_s, :headers => auth_header
 
 280       assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
 
 282       ## now setup for the public data user
 
 283       user = create(:user, :data_public => true)
 
 284       changeset = create(:changeset, :user => user)
 
 285       closed_changeset = create(:changeset, :closed, :user => user)
 
 286       node = create(:node, :changeset => changeset)
 
 287       auth_header = bearer_authorization_header user
 
 289       # try to delete with an invalid (closed) changeset
 
 290       xml = update_changeset(xml_for_node(node), closed_changeset.id)
 
 291       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 292       assert_response :conflict
 
 294       # try to delete with an invalid (non-existent) changeset
 
 295       xml = update_changeset(xml_for_node(node), 0)
 
 296       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 297       assert_response :conflict
 
 299       # try to delete a node with a different ID
 
 300       other_node = create(:node)
 
 301       xml = xml_for_node(other_node)
 
 302       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 303       assert_response :bad_request,
 
 304                       "should not be able to delete a node with a different ID from the XML"
 
 306       # try to delete a node rubbish in the payloads
 
 308       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 309       assert_response :bad_request,
 
 310                       "should not be able to delete a node without a valid XML payload"
 
 312       # valid delete now takes a payload
 
 313       xml = xml_for_node(node)
 
 314       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 315       assert_response :success
 
 317       # valid delete should return the new version number, which should
 
 318       # be greater than the old version number
 
 319       assert_operator @response.body.to_i, :>, node.version, "delete request should return a new version number for node"
 
 321       # deleting the same node twice doesn't work
 
 322       xml = xml_for_node(node)
 
 323       delete api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 324       assert_response :gone
 
 326       # this won't work since the node never existed
 
 327       delete api_node_path(0), :headers => auth_header
 
 328       assert_response :not_found
 
 330       ## these test whether nodes which are in-use can be deleted:
 
 332       used_node = create(:node, :changeset => create(:changeset, :user => user))
 
 333       way_node = create(:way_node, :node => used_node)
 
 334       way_node2 = create(:way_node, :node => used_node)
 
 336       xml = xml_for_node(used_node)
 
 337       delete api_node_path(used_node), :params => xml.to_s, :headers => auth_header
 
 338       assert_response :precondition_failed,
 
 339                       "shouldn't be able to delete a node used in a way (#{@response.body})"
 
 340       assert_equal "Precondition failed: Node #{used_node.id} is still used by ways #{way_node.way.id},#{way_node2.way.id}.", @response.body
 
 343       used_node2 = create(:node, :changeset => create(:changeset, :user => user))
 
 344       relation_member = create(:relation_member, :member => used_node2)
 
 345       relation_member2 = create(:relation_member, :member => used_node2)
 
 347       xml = xml_for_node(used_node2)
 
 348       delete api_node_path(used_node2), :params => xml.to_s, :headers => auth_header
 
 349       assert_response :precondition_failed,
 
 350                       "shouldn't be able to delete a node used in a relation (#{@response.body})"
 
 351       assert_equal "Precondition failed: Node #{used_node2.id} is still used by relations #{relation_member.relation.id},#{relation_member2.relation.id}.", @response.body
 
 355     # tests whether the API works and prevents incorrect use while trying
 
 358       invalid_attr_values = [["lat", 91.0], ["lat", -91.0], ["lon", 181.0], ["lon", -181.0]]
 
 360       ## First test with no user credentials
 
 361       # try and update a node without authorisation
 
 362       # first try to delete node without auth
 
 363       private_user = create(:user, :data_public => false)
 
 364       private_node = create(:node, :changeset => create(:changeset, :user => private_user))
 
 366       node = create(:node, :changeset => create(:changeset, :user => user))
 
 368       xml = xml_for_node(node)
 
 369       put api_node_path(node), :params => xml.to_s
 
 370       assert_response :unauthorized
 
 372       ## Second test with the private user
 
 375       auth_header = bearer_authorization_header private_user
 
 377       ## trying to break changesets
 
 379       # try and update in someone else's changeset
 
 380       xml = update_changeset(xml_for_node(private_node),
 
 381                              create(:changeset).id)
 
 382       put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
 
 383       assert_require_public_data "update with other user's changeset should be forbidden when data isn't public"
 
 385       # try and update in a closed changeset
 
 386       xml = update_changeset(xml_for_node(private_node),
 
 387                              create(:changeset, :closed, :user => private_user).id)
 
 388       put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
 
 389       assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
 
 391       # try and update in a non-existant changeset
 
 392       xml = update_changeset(xml_for_node(private_node), 0)
 
 393       put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
 
 394       assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
 
 396       ## try and submit invalid updates
 
 397       invalid_attr_values.each do |name, value|
 
 398         xml = xml_attr_rewrite(xml_for_node(private_node), name, value)
 
 399         put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
 
 400         assert_require_public_data "node at #{name}=#{value} should be forbidden, when data isn't public"
 
 403       ## finally, produce a good request which still won't work
 
 404       xml = xml_for_node(private_node)
 
 405       put api_node_path(private_node), :params => xml.to_s, :headers => auth_header
 
 406       assert_require_public_data "should have failed with a forbidden when data isn't public"
 
 408       ## Finally test with the public user
 
 410       # try and update a node without authorisation
 
 411       # first try to update node without auth
 
 412       xml = xml_for_node(node)
 
 413       put api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 414       assert_response :forbidden
 
 417       auth_header = bearer_authorization_header user
 
 419       ## trying to break changesets
 
 421       # try and update in someone else's changeset
 
 422       xml = update_changeset(xml_for_node(node),
 
 423                              create(:changeset).id)
 
 424       put api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 425       assert_response :conflict, "update with other user's changeset should be rejected"
 
 427       # try and update in a closed changeset
 
 428       xml = update_changeset(xml_for_node(node),
 
 429                              create(:changeset, :closed, :user => user).id)
 
 430       put api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 431       assert_response :conflict, "update with closed changeset should be rejected"
 
 433       # try and update in a non-existant changeset
 
 434       xml = update_changeset(xml_for_node(node), 0)
 
 435       put api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 436       assert_response :conflict, "update with changeset=0 should be rejected"
 
 438       ## try and submit invalid updates
 
 439       invalid_attr_values.each do |name, value|
 
 440         xml = xml_attr_rewrite(xml_for_node(node), name, value)
 
 441         put api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 442         assert_response :bad_request, "node at #{name}=#{value} should be rejected"
 
 445       ## next, attack the versioning
 
 446       current_node_version = node.version
 
 448       # try and submit a version behind
 
 449       xml = xml_attr_rewrite(xml_for_node(node),
 
 450                              "version", current_node_version - 1)
 
 451       put api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 452       assert_response :conflict, "should have failed on old version number"
 
 454       # try and submit a version ahead
 
 455       xml = xml_attr_rewrite(xml_for_node(node),
 
 456                              "version", current_node_version + 1)
 
 457       put api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 458       assert_response :conflict, "should have failed on skipped version number"
 
 460       # try and submit total crap in the version field
 
 461       xml = xml_attr_rewrite(xml_for_node(node),
 
 462                              "version", "p1r4t3s!")
 
 463       put api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 464       assert_response :conflict,
 
 465                       "should not be able to put 'p1r4at3s!' in the version field"
 
 467       ## try an update with the wrong ID
 
 468       xml = xml_for_node(create(:node))
 
 469       put api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 470       assert_response :bad_request,
 
 471                       "should not be able to update a node with a different ID from the XML"
 
 473       ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
 
 475       put api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 476       assert_response :bad_request,
 
 477                       "should not be able to update a node with non-OSM XML doc."
 
 479       ## finally, produce a good request which should work
 
 480       xml = xml_for_node(node)
 
 481       put api_node_path(node), :params => xml.to_s, :headers => auth_header
 
 482       assert_response :success, "a valid update request failed"
 
 486     # test adding tags to a node
 
 487     def test_duplicate_tags
 
 488       existing_tag = create(:node_tag)
 
 489       assert existing_tag.node.changeset.user.data_public
 
 491       auth_header = bearer_authorization_header existing_tag.node.changeset.user
 
 493       # add an identical tag to the node
 
 494       tag_xml = XML::Node.new("tag")
 
 495       tag_xml["k"] = existing_tag.k
 
 496       tag_xml["v"] = existing_tag.v
 
 498       # add the tag into the existing xml
 
 499       node_xml = xml_for_node(existing_tag.node)
 
 500       node_xml.find("//osm/node").first << tag_xml
 
 503       put api_node_path(existing_tag.node), :params => node_xml.to_s, :headers => auth_header
 
 504       assert_response :bad_request,
 
 505                       "adding duplicate tags to a node should fail with 'bad request'"
 
 506       assert_equal "Element node/#{existing_tag.node.id} has duplicate tags with key #{existing_tag.k}", @response.body
 
 509     # test whether string injection is possible
 
 510     def test_string_injection
 
 511       private_user = create(:user, :data_public => false)
 
 512       private_changeset = create(:changeset, :user => private_user)
 
 514       changeset = create(:changeset, :user => user)
 
 516       ## First try with the non-data public user
 
 517       auth_header = bearer_authorization_header private_user
 
 519       # try and put something into a string that the API might
 
 520       # use unquoted and therefore allow code injection...
 
 521       xml = "<osm><node lat='0' lon='0' changeset='#{private_changeset.id}'>" \
 
 522             "<tag k='\#{@user.inspect}' v='0'/>" \
 
 524       post api_nodes_path, :params => xml, :headers => auth_header
 
 525       assert_require_public_data "Shouldn't be able to create with non-public user"
 
 527       ## Then try with the public data user
 
 528       auth_header = bearer_authorization_header user
 
 530       # try and put something into a string that the API might
 
 531       # use unquoted and therefore allow code injection...
 
 532       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'>" \
 
 533             "<tag k='\#{@user.inspect}' v='0'/>" \
 
 535       post api_nodes_path, :params => xml, :headers => auth_header
 
 536       assert_response :success
 
 537       nodeid = @response.body
 
 539       # find the node in the database
 
 540       checknode = Node.find(nodeid)
 
 541       assert_not_nil checknode, "node not found in data base after upload"
 
 543       # and grab it using the api
 
 544       get api_node_path(nodeid)
 
 545       assert_response :success
 
 546       apinode = Node.from_xml(@response.body)
 
 547       assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
 
 549       # check the tags are not corrupted
 
 550       assert_equal checknode.tags, apinode.tags
 
 551       assert_includes apinode.tags, "\#{@user.inspect}"
 
 555     # test initial rate limit
 
 556     def test_initial_rate_limit
 
 560       # create a changeset that puts us near the initial rate limit
 
 561       changeset = create(:changeset, :user => user,
 
 562                                      :created_at => Time.now.utc - 5.minutes,
 
 563                                      :num_changes => Settings.initial_changes_per_hour - 1)
 
 565       # create authentication header
 
 566       auth_header = bearer_authorization_header user
 
 568       # try creating a node
 
 569       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
 
 570       post api_nodes_path, :params => xml, :headers => auth_header
 
 571       assert_response :success, "node create did not return success status"
 
 573       # get the id of the node we created
 
 574       nodeid = @response.body
 
 576       # try updating the node, which should be rate limited
 
 577       xml = "<osm><node id='#{nodeid}' version='1' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
 
 578       put api_node_path(nodeid), :params => xml, :headers => auth_header
 
 579       assert_response :too_many_requests, "node update did not hit rate limit"
 
 581       # try deleting the node, which should be rate limited
 
 582       xml = "<osm><node id='#{nodeid}' version='2' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
 
 583       delete api_node_path(nodeid), :params => xml, :headers => auth_header
 
 584       assert_response :too_many_requests, "node delete did not hit rate limit"
 
 586       # try creating a node, which should be rate limited
 
 587       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
 
 588       post api_nodes_path, :params => xml, :headers => auth_header
 
 589       assert_response :too_many_requests, "node create did not hit rate limit"
 
 593     # test maximum rate limit
 
 594     def test_maximum_rate_limit
 
 598       # create a changeset to establish our initial edit time
 
 599       changeset = create(:changeset, :user => user,
 
 600                                      :created_at => Time.now.utc - 28.days)
 
 602       # create changeset to put us near the maximum rate limit
 
 603       total_changes = Settings.max_changes_per_hour - 1
 
 604       while total_changes.positive?
 
 605         changes = [total_changes, Changeset::MAX_ELEMENTS].min
 
 606         changeset = create(:changeset, :user => user,
 
 607                                        :created_at => Time.now.utc - 5.minutes,
 
 608                                        :num_changes => changes)
 
 609         total_changes -= changes
 
 612       # create authentication header
 
 613       auth_header = bearer_authorization_header user
 
 615       # try creating a node
 
 616       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
 
 617       post api_nodes_path, :params => xml, :headers => auth_header
 
 618       assert_response :success, "node create did not return success status"
 
 620       # get the id of the node we created
 
 621       nodeid = @response.body
 
 623       # try updating the node, which should be rate limited
 
 624       xml = "<osm><node id='#{nodeid}' version='1' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
 
 625       put api_node_path(nodeid), :params => xml, :headers => auth_header
 
 626       assert_response :too_many_requests, "node update did not hit rate limit"
 
 628       # try deleting the node, which should be rate limited
 
 629       xml = "<osm><node id='#{nodeid}' version='2' lat='1' lon='1' changeset='#{changeset.id}'/></osm>"
 
 630       delete api_node_path(nodeid), :params => xml, :headers => auth_header
 
 631       assert_response :too_many_requests, "node delete did not hit rate limit"
 
 633       # try creating a node, which should be rate limited
 
 634       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'/></osm>"
 
 635       post api_nodes_path, :params => xml, :headers => auth_header
 
 636       assert_response :too_many_requests, "node create did not hit rate limit"
 
 642     # update the changeset_id of a node element
 
 643     def update_changeset(xml, changeset_id)
 
 644       xml_attr_rewrite(xml, "changeset", changeset_id)
 
 648     # update an attribute in the node element
 
 649     def xml_attr_rewrite(xml, name, value)
 
 650       xml.find("//osm/node").first[name] = value.to_s