X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/e22589f946307570cacda4d7afa129f1dcc91bf8..927cc9f45e6626e2a82403d1263b4a16d601e30b:/test/controllers/api/changesets_controller_test.rb?ds=sidebyside diff --git a/test/controllers/api/changesets_controller_test.rb b/test/controllers/api/changesets_controller_test.rb index 97ce5f7c9..50ced0ff1 100644 --- a/test/controllers/api/changesets_controller_test.rb +++ b/test/controllers/api/changesets_controller_test.rb @@ -134,12 +134,14 @@ module Api def test_create_wrong_method auth_header = basic_authorization_header create(:user).email, "test" - assert_raise ActionController::RoutingError do - get changeset_create_path, :headers => auth_header - end - assert_raise ActionController::RoutingError do - post changeset_create_path, :headers => auth_header - end + + get changeset_create_path, :headers => auth_header + assert_response :not_found + assert_template "rescues/routing_error" + + post changeset_create_path, :headers => auth_header + assert_response :not_found + assert_template "rescues/routing_error" end ## @@ -279,11 +281,11 @@ module Api assert_single_changeset_json changeset, js assert_equal 3, js["changeset"]["comments"].count assert_equal comment0.id, js["changeset"]["comments"][0]["id"] - assert_operator true, :==, js["changeset"]["comments"][0]["visible"] + assert js["changeset"]["comments"][0]["visible"] assert_equal comment1.id, js["changeset"]["comments"][1]["id"] - assert_operator true, :==, js["changeset"]["comments"][1]["visible"] + assert js["changeset"]["comments"][1]["visible"] assert_equal comment2.id, js["changeset"]["comments"][2]["id"] - assert_operator true, :==, js["changeset"]["comments"][2]["visible"] + assert js["changeset"]["comments"][2]["visible"] # one hidden comment not included because not asked for comment1.update(:visible => false) @@ -298,9 +300,9 @@ module Api assert_single_changeset_json changeset, js assert_equal 2, js["changeset"]["comments"].count assert_equal comment0.id, js["changeset"]["comments"][0]["id"] - assert_operator true, :==, js["changeset"]["comments"][0]["visible"] + assert js["changeset"]["comments"][0]["visible"] assert_equal comment2.id, js["changeset"]["comments"][1]["id"] - assert_operator true, :==, js["changeset"]["comments"][1]["visible"] + assert js["changeset"]["comments"][1]["visible"] # one hidden comment not included because no permissions get changeset_show_path(changeset), :params => { :format => "json", :include_discussion => true, :show_hidden_comments => true } @@ -313,10 +315,10 @@ module Api assert_single_changeset_json changeset, js assert_equal 2, js["changeset"]["comments"].count assert_equal comment0.id, js["changeset"]["comments"][0]["id"] - assert_operator true, :==, js["changeset"]["comments"][0]["visible"] + assert js["changeset"]["comments"][0]["visible"] # maybe will show an empty comment element with visible=false in the future assert_equal comment2.id, js["changeset"]["comments"][1]["id"] - assert_operator true, :==, js["changeset"]["comments"][1]["visible"] + assert js["changeset"]["comments"][1]["visible"] # one hidden comment shown to moderators moderator_user = create(:moderator_user) @@ -332,11 +334,11 @@ module Api assert_single_changeset_json changeset, js assert_equal 3, js["changeset"]["comments"].count assert_equal comment0.id, js["changeset"]["comments"][0]["id"] - assert_operator true, :==, js["changeset"]["comments"][0]["visible"] + assert js["changeset"]["comments"][0]["visible"] assert_equal comment1.id, js["changeset"]["comments"][1]["id"] - assert_operator false, :==, js["changeset"]["comments"][1]["visible"] + assert_not js["changeset"]["comments"][1]["visible"] assert_equal comment2.id, js["changeset"]["comments"][2]["id"] - assert_operator true, :==, js["changeset"]["comments"][2]["visible"] + assert js["changeset"]["comments"][2]["visible"] end def test_show_tag_and_discussion_json @@ -452,13 +454,13 @@ module Api auth_header = basic_authorization_header user.email, "test" - assert_raise ActionController::RoutingError do - get changeset_close_path(changeset), :headers => auth_header - end + get changeset_close_path(changeset), :headers => auth_header + assert_response :not_found + assert_template "rescues/routing_error" - assert_raise ActionController::RoutingError do - post changeset_close_path(changeset), :headers => auth_header - end + post changeset_close_path(changeset), :headers => auth_header + assert_response :not_found + assert_template "rescues/routing_error" end ## @@ -1695,6 +1697,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. @@ -2272,7 +2375,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 = "" @@ -2354,14 +2461,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 @@ -2374,7 +2481,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 @@ -2382,7 +2489,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 @@ -2390,7 +2497,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 @@ -2404,7 +2511,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 @@ -2413,7 +2520,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 @@ -2424,7 +2531,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 @@ -2432,14 +2539,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 @@ -2465,10 +2572,10 @@ module Api assert_equal changeset.id, js["changeset"]["id"] assert_equal changeset.created_at.xmlschema, js["changeset"]["created_at"] if changeset.open? - assert_operator true, :==, js["changeset"]["open"] + assert js["changeset"]["open"] assert_nil js["changeset"]["closed_at"] else - assert_operator false, :==, js["changeset"]["open"] + assert_not js["changeset"]["open"] assert_equal changeset.closed_at.xmlschema, js["changeset"]["closed_at"] end end