implement token bucket rate limiting

This commit is contained in:
Albert Yi
2017-01-06 15:40:49 -08:00
parent 49a72e4bf6
commit f2a5d45db0
20 changed files with 203 additions and 125 deletions

View File

@@ -19,16 +19,18 @@ class PostsControllerTest < ActionController::TestCase
context "for api calls" do
context "passing the api limit" do
setup do
User.any_instance.stubs(:api_hourly_limit).returns(5)
@post = FactoryGirl.create(:post)
@bucket = TokenBucket.create(user_id: @user.id, token_count: 5, last_touched_at: Time.now)
User.any_instance.stubs(:api_burst_limit).returns(5)
end
should "work" do
CurrentUser.user.api_hourly_limit.times do
get :index, {:format => "json", :login => @user.name, :api_key => @user.api_key.key}
@user.api_burst_limit.times do
post :update, {:format => "json", :id => @post.id, :post => {:rating => "q"}, :login => @user.name, :api_key => @user.api_key.key}
assert_response :success
end
get :index, {:format => "json", :login => @user.name, :api_key => @user.api_key.key}
post :update, {:format => "json", :id => @post.id, :post => {:rating => "q"}, :login => @user.name, :api_key => @user.api_key.key}
assert_response 429
end
end