track api call counts that require auth

This commit is contained in:
Kyle Drake 2015-04-04 17:11:30 -07:00
parent cd94d2ce36
commit 02cea425e9
3 changed files with 29 additions and 2 deletions

View file

@ -131,6 +131,8 @@ def init_api_credentials
api_error_invalid_auth api_error_invalid_auth
end end
DB['update sites set api_calls=api_calls+1 where id=?', site.id].first
session[:id] = site.id session[:id] = site.id
else else
api_error_invalid_auth api_error_invalid_auth
@ -163,4 +165,4 @@ end
def api_not_found def api_not_found
api_error 404, 'not_found', 'the requested api call does not exist' api_error 404, 'not_found', 'the requested api call does not exist'
end end

View file

@ -0,0 +1,9 @@
Sequel.migration do
up {
DB.add_column :sites, :api_calls, Integer, default: 0, index: true
}
down {
DB.drop_column :sites, :api_calls
}
end

View file

@ -25,6 +25,7 @@ describe 'api info' do
@site.update is_banned: true @site.update is_banned: true
get '/api/info', sitename: @site.username get '/api/info', sitename: @site.username
res[:error_type].must_equal 'site_not_found' res[:error_type].must_equal 'site_not_found'
@site.reload.api_calls.must_equal 0
end end
it 'fails for nonexistent site' do it 'fails for nonexistent site' do
@ -43,6 +44,7 @@ describe 'api info' do
res[:info][:last_updated].must_equal nil res[:info][:last_updated].must_equal nil
res[:info][:domain].must_equal 'derp.com' res[:info][:domain].must_equal 'derp.com'
res[:info][:tags].must_equal ['derpie', 'man'] res[:info][:tags].must_equal ['derpie', 'man']
@site.reload.api_calls.must_equal 0
end end
it 'fails for bad auth' do it 'fails for bad auth' do
@ -145,6 +147,7 @@ describe 'api upload' do
} }
res[:result].must_equal 'success' res[:result].must_equal 'success'
File.exist?(File.join(Site::SITE_FILES_ROOT, @site.username, 'lol.jpg')).must_equal true File.exist?(File.join(Site::SITE_FILES_ROOT, @site.username, 'lol.jpg')).must_equal true
@site.reload.api_calls.must_equal 1
end end
it 'scrubs root path slash' do it 'scrubs root path slash' do
@ -193,6 +196,19 @@ describe 'api upload' do
File.exist?(@site.files_path('derpie/derpingtons/lol.jpg')).must_equal true File.exist?(@site.files_path('derpie/derpingtons/lol.jpg')).must_equal true
end end
it 'records api calls that require auth' do
create_site
basic_authorize @user, @pass
2.times {
post '/api/upload', {
'derpie/derpingtons/lol.jpg' => Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg')
}
}
@site.reload.api_calls.must_equal 2
end
it 'fails for invalid files' do it 'fails for invalid files' do
create_site create_site
basic_authorize @user, @pass basic_authorize @user, @pass
@ -252,4 +268,4 @@ end
def res def res
JSON.parse last_response.body, symbolize_names: true JSON.parse last_response.body, symbolize_names: true
end end