implement api limiting

This commit is contained in:
albert
2013-03-20 16:35:35 -07:00
parent 7470d189c3
commit 2ac22d07cd
7 changed files with 73 additions and 14 deletions

View File

@@ -16,16 +16,32 @@ class PostsControllerTest < ActionController::TestCase
end
context "for api calls" do
context "passing the api limit" do
setup do
User.any_instance.stubs(:api_hourly_limit).returns(5)
end
should "work" do
CurrentUser.user.api_hourly_limit.times do
get :index, {:format => "json", :login => @user.name, :api_key => @user.bcrypt_cookie_password_hash}
assert_response :success
end
get :index, {:format => "json", :login => @user.name, :api_key => @user.bcrypt_cookie_password_hash}
assert_response 421
end
end
context "using http basic auth" do
should "succeed for password matches" do
@basic_auth_string = "Basic #{ActiveSupport::Base64.encode64("#{@user.name}:#{@user.bcrypt_cookie_password_hash}")}"
@basic_auth_string = "Basic #{::Base64.encode64("#{@user.name}:#{@user.bcrypt_cookie_password_hash}")}"
@request.env['HTTP_AUTHORIZATION'] = @basic_auth_string
get :index, {:format => "json"}
assert_response :success
end
should "fail for password mismatches" do
@basic_auth_string = "Basic #{ActiveSupport::Base64.encode64("#{@user.name}:badpassword")}"
@basic_auth_string = "Basic #{::Base64.encode64("#{@user.name}:badpassword")}"
@request.env['HTTP_AUTHORIZATION'] = @basic_auth_string
get :index, {:format => "json"}
assert_response 401

View File

@@ -53,6 +53,23 @@ class MockMemcache
def flush_all
@memory = {}
end
def fetch key, expiry = 0, raw = false
if @memory.has_key?(key)
@memory[key]
else
@memory[key] = yield
end
@memory[key]
end
def incr key
@memory[key] += 1
end
def decr key
@memory[key] -= 1
end
def set key, value, expiry = 0
@memory[key] = value
@@ -62,7 +79,7 @@ class MockMemcache
@memory[key]
end
def delete key, delay
def delete key, delay = 0
@memory.delete key
end