]> git.openstreetmap.org Git - rails.git/blobdiff - test/controllers/api/ways_controller_test.rb
Add tests for API change rate limits
[rails.git] / test / controllers / api / ways_controller_test.rb
index fa0c47792031367c00316d24d01e83cfbab68137..791da8029f8725e4a6209267641155f9610120ba 100644 (file)
@@ -64,31 +64,31 @@ module Api
     ##
     # check the "full" mode
     def test_full
-      Way.all.each do |way|
-        get way_full_path(way)
-
-        # full call should say "gone" for non-visible ways...
-        unless way.visible
-          assert_response :gone
-          next
-        end
-
-        # otherwise it should say success
-        assert_response :success
-
-        # Check the way is correctly returned
-        assert_select "osm way[id='#{way.id}'][version='#{way.version}'][visible='#{way.visible}']", 1
-
-        # check that each node in the way appears once in the output as a
-        # reference and as the node element.
-        way.nodes.each do |n|
-          count = (way.nodes - (way.nodes - [n])).length
-          assert_select "osm way nd[ref='#{n.id}']", count
-          assert_select "osm node[id='#{n.id}'][version='#{n.version}'][lat='#{format('%.7f', n.lat)}'][lon='#{format('%.7f', n.lon)}']", 1
-        end
+      way = create(:way_with_nodes, :nodes_count => 3)
+
+      get way_full_path(way)
+
+      assert_response :success
+
+      # Check the way is correctly returned
+      assert_select "osm way[id='#{way.id}'][version='1'][visible='true']", 1
+
+      # check that each node in the way appears once in the output as a
+      # reference and as the node element.
+      way.nodes.each do |n|
+        assert_select "osm way nd[ref='#{n.id}']", 1
+        assert_select "osm node[id='#{n.id}'][version='1'][lat='#{format('%<lat>.7f', :lat => n.lat)}'][lon='#{format('%<lon>.7f', :lon => n.lon)}']", 1
       end
     end
 
+    def test_full_deleted
+      way = create(:way, :deleted)
+
+      get way_full_path(way)
+
+      assert_response :gone
+    end
+
     ##
     # test fetching multiple ways
     def test_index
@@ -180,8 +180,7 @@ module Api
       assert_not_nil checkway,
                      "uploaded way not found in data base after upload"
       # compare values
-      assert_equal checkway.nds.length, 2,
-                   "saved way does not contain exactly one node"
+      assert_equal(2, checkway.nds.length, "saved way does not contain exactly one node")
       assert_equal checkway.nds[0], node1.id,
                    "saved way does not contain the right node on pos 0"
       assert_equal checkway.nds[1], node2.id,
@@ -380,8 +379,7 @@ module Api
       # check the returned value - should be the new version number
       # valid delete should return the new version number, which should
       # be greater than the old version number
-      assert @response.body.to_i > way.version,
-             "delete request should return a new version number for way"
+      assert_operator @response.body.to_i, :>, way.version, "delete request should return a new version number for way"
 
       # this won't work since the way is already deleted
       xml = xml_for_way(deleted_way)
@@ -755,6 +753,113 @@ module Api
       end
     end
 
+    ##
+    # test initial rate limit
+    def test_initial_rate_limit
+      # create a user
+      user = create(:user)
+
+      # create some nodes
+      node1 = create(:node)
+      node2 = create(:node)
+
+      # create a changeset that puts us near the initial rate limit
+      changeset = create(:changeset, :user => user,
+                                     :created_at => Time.now.utc - 5.minutes,
+                                     :num_changes => Settings.initial_changes_per_hour - 1)
+
+      # create authentication header
+      auth_header = basic_authorization_header user.email, "test"
+
+      # try creating a way
+      xml = "<osm><way changeset='#{changeset.id}'>" \
+            "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
+            "<tag k='test' v='yes' /></way></osm>"
+      put way_create_path, :params => xml, :headers => auth_header
+      assert_response :success, "way create did not return success status"
+
+      # get the id of the way we created
+      wayid = @response.body
+
+      # try updating the way, which should be rate limited
+      xml = "<osm><way id='#{wayid}' version='1' changeset='#{changeset.id}'>" \
+            "<nd ref='#{node2.id}'/><nd ref='#{node1.id}'/>" \
+            "<tag k='test' v='yes' /></way></osm>"
+      put api_way_path(wayid), :params => xml, :headers => auth_header
+      assert_response :too_many_requests, "way update did not hit rate limit"
+
+      # try deleting the way, which should be rate limited
+      xml = "<osm><way id='#{wayid}' version='2' changeset='#{changeset.id}'/></osm>"
+      delete api_way_path(wayid), :params => xml, :headers => auth_header
+      assert_response :too_many_requests, "way delete did not hit rate limit"
+
+      # try creating a way, which should be rate limited
+      xml = "<osm><way changeset='#{changeset.id}'>" \
+            "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
+            "<tag k='test' v='yes' /></way></osm>"
+      put way_create_path, :params => xml, :headers => auth_header
+      assert_response :too_many_requests, "way create did not hit rate limit"
+    end
+
+    ##
+    # test maximum rate limit
+    def test_maximum_rate_limit
+      # create a user
+      user = create(:user)
+
+      # create some nodes
+      node1 = create(:node)
+      node2 = create(:node)
+
+      # create a changeset to establish our initial edit time
+      changeset = create(:changeset, :user => user,
+                                     :created_at => Time.now.utc - 28.days)
+
+      # create changeset to put us near the maximum rate limit
+      total_changes = Settings.max_changes_per_hour - 1
+      while total_changes.positive?
+        changes = [total_changes, Changeset::MAX_ELEMENTS].min
+        changeset = create(:changeset, :user => user,
+                                       :created_at => Time.now.utc - 5.minutes,
+                                       :num_changes => changes)
+        total_changes -= changes
+      end
+
+      # create authentication header
+      auth_header = basic_authorization_header user.email, "test"
+
+      # try creating a way
+      xml = "<osm><way changeset='#{changeset.id}'>" \
+            "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
+            "<tag k='test' v='yes' /></way></osm>"
+      put way_create_path, :params => xml, :headers => auth_header
+      assert_response :success, "way create did not return success status"
+
+      # get the id of the way we created
+      wayid = @response.body
+
+      # try updating the way, which should be rate limited
+      xml = "<osm><way id='#{wayid}' version='1' changeset='#{changeset.id}'>" \
+            "<nd ref='#{node2.id}'/><nd ref='#{node1.id}'/>" \
+            "<tag k='test' v='yes' /></way></osm>"
+      put api_way_path(wayid), :params => xml, :headers => auth_header
+      assert_response :too_many_requests, "way update did not hit rate limit"
+
+      # try deleting the way, which should be rate limited
+      xml = "<osm><way id='#{wayid}' version='2' changeset='#{changeset.id}'/></osm>"
+      delete api_way_path(wayid), :params => xml, :headers => auth_header
+      assert_response :too_many_requests, "way delete did not hit rate limit"
+
+      # try creating a way, which should be rate limited
+      xml = "<osm><way changeset='#{changeset.id}'>" \
+            "<nd ref='#{node1.id}'/><nd ref='#{node2.id}'/>" \
+            "<tag k='test' v='yes' /></way></osm>"
+      put way_create_path, :params => xml, :headers => auth_header
+      assert_response :too_many_requests, "way create did not hit rate limit"
+    end
+
+    private
+
     ##
     # update the changeset_id of a way element
     def update_changeset(xml, changeset_id)