X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/dd905b5aa893918168c644045aa1b6069e95d48e..f5aa031b584b36a96664d7c0e159329c6ebf5861:/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 802e006e1..3278b2101 100644 --- a/test/controllers/api/changesets_controller_test.rb +++ b/test/controllers/api/changesets_controller_test.rb @@ -1606,6 +1606,107 @@ module Api assert_equal "Precondition failed: Node #{node.id} is still used by ways #{way.id}.", @response.body end + ## + # test initial rate limit + def test_upload_initial_rate_limit + # create a user + user = create(:user) + + # create some objects to use + node = create(:node) + way = create(:way_with_nodes, :nodes_count => 2) + relation = create(:relation) + + # 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 - 2) + + # 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 :too_many_requests, "upload did not hit rate limit" + end + + ## + # test maximum rate limit + def test_upload_maximum_rate_limit + # create a user + user = create(:user) + + # create some objects to use + node = create(:node) + way = create(:way_with_nodes, :nodes_count => 2) + relation = create(:relation) + + # 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 - 2 + 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" + + # 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 :too_many_requests, "upload did not hit rate limit" + end + ## # when we make some simple changes we get the same changes back from the # diff download. @@ -2183,7 +2284,11 @@ module Api # check that a changeset can contain a certain max number of changes. ## FIXME should be changed to an integration test due to the with_controller def test_changeset_limits - 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) # open a new changeset xml = "" @@ -2265,14 +2370,14 @@ module Api changeset = create(:changeset, :closed) assert_difference "changeset.subscribers.count", 1 do - post changeset_subscribe_path(changeset), :headers => auth_header + post api_changeset_subscribe_path(changeset), :headers => auth_header end assert_response :success # not closed changeset changeset = create(:changeset) assert_difference "changeset.subscribers.count", 1 do - post changeset_subscribe_path(changeset), :headers => auth_header + post api_changeset_subscribe_path(changeset), :headers => auth_header end assert_response :success end @@ -2285,7 +2390,7 @@ module Api # unauthorized changeset = create(:changeset, :closed) assert_no_difference "changeset.subscribers.count" do - post changeset_subscribe_path(changeset) + post api_changeset_subscribe_path(changeset) end assert_response :unauthorized @@ -2293,7 +2398,7 @@ module Api # bad changeset id assert_no_difference "changeset.subscribers.count" do - post changeset_subscribe_path(:id => 999111), :headers => auth_header + post api_changeset_subscribe_path(:id => 999111), :headers => auth_header end assert_response :not_found @@ -2301,7 +2406,7 @@ module Api changeset = create(:changeset, :closed) changeset.subscribers.push(user) assert_no_difference "changeset.subscribers.count" do - post changeset_subscribe_path(changeset), :headers => auth_header + post api_changeset_subscribe_path(changeset), :headers => auth_header end assert_response :conflict end @@ -2315,7 +2420,7 @@ module Api changeset.subscribers.push(user) assert_difference "changeset.subscribers.count", -1 do - post changeset_unsubscribe_path(changeset), :headers => auth_header + post api_changeset_unsubscribe_path(changeset), :headers => auth_header end assert_response :success @@ -2324,7 +2429,7 @@ module Api changeset.subscribers.push(user) assert_difference "changeset.subscribers.count", -1 do - post changeset_unsubscribe_path(changeset), :headers => auth_header + post api_changeset_unsubscribe_path(changeset), :headers => auth_header end assert_response :success end @@ -2335,7 +2440,7 @@ module Api # unauthorized changeset = create(:changeset, :closed) assert_no_difference "changeset.subscribers.count" do - post changeset_unsubscribe_path(changeset) + post api_changeset_unsubscribe_path(changeset) end assert_response :unauthorized @@ -2343,14 +2448,14 @@ module Api # bad changeset id assert_no_difference "changeset.subscribers.count" do - post changeset_unsubscribe_path(:id => 999111), :headers => auth_header + post api_changeset_unsubscribe_path(:id => 999111), :headers => auth_header end assert_response :not_found # trying to unsubscribe when not subscribed changeset = create(:changeset, :closed) assert_no_difference "changeset.subscribers.count" do - post changeset_unsubscribe_path(changeset), :headers => auth_header + post api_changeset_unsubscribe_path(changeset), :headers => auth_header end assert_response :not_found end