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
+ <osmChange>
+ <create>
+ <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
+ <tag k='foo' v='bar'/>
+ <tag k='baz' v='bat'/>
+ </node>
+ <way id='-1' changeset='#{changeset.id}'>
+ <nd ref='#{node.id}'/>
+ </way>
+ </create>
+ <create>
+ <relation id='-1' changeset='#{changeset.id}'>
+ <member type='way' role='some' ref='#{way.id}'/>
+ <member type='node' role='some' ref='#{node.id}'/>
+ <member type='relation' role='some' ref='#{relation.id}'/>
+ </relation>
+ </create>
+ </osmChange>
+ 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
+ <osmChange>
+ <create>
+ <node id='-1' lon='0' lat='0' changeset='#{changeset.id}'>
+ <tag k='foo' v='bar'/>
+ <tag k='baz' v='bat'/>
+ </node>
+ <way id='-1' changeset='#{changeset.id}'>
+ <nd ref='#{node.id}'/>
+ </way>
+ </create>
+ <create>
+ <relation id='-1' changeset='#{changeset.id}'>
+ <member type='way' role='some' ref='#{way.id}'/>
+ <member type='node' role='some' ref='#{node.id}'/>
+ <member type='relation' role='some' ref='#{relation.id}'/>
+ </relation>
+ </create>
+ </osmChange>
+ 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.
# 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 = "<osm><changeset/></osm>"
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
# 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
# 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
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
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
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
# 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
# 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