From 02cea425e9e428d9f78c5084752345d2e0f4a818 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Sat, 4 Apr 2015 17:11:30 -0700 Subject: [PATCH] track api call counts that require auth --- app/api.rb | 4 +++- migrations/056_add_api_calls.rb | 9 +++++++++ tests/api_tests.rb | 18 +++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 migrations/056_add_api_calls.rb diff --git a/app/api.rb b/app/api.rb index 4c4c8d34..b2907260 100644 --- a/app/api.rb +++ b/app/api.rb @@ -131,6 +131,8 @@ def init_api_credentials api_error_invalid_auth end + DB['update sites set api_calls=api_calls+1 where id=?', site.id].first + session[:id] = site.id else api_error_invalid_auth @@ -163,4 +165,4 @@ end def api_not_found api_error 404, 'not_found', 'the requested api call does not exist' -end \ No newline at end of file +end diff --git a/migrations/056_add_api_calls.rb b/migrations/056_add_api_calls.rb new file mode 100644 index 00000000..ffa8d317 --- /dev/null +++ b/migrations/056_add_api_calls.rb @@ -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 diff --git a/tests/api_tests.rb b/tests/api_tests.rb index 02b327cf..5f94665a 100644 --- a/tests/api_tests.rb +++ b/tests/api_tests.rb @@ -25,6 +25,7 @@ describe 'api info' do @site.update is_banned: true get '/api/info', sitename: @site.username res[:error_type].must_equal 'site_not_found' + @site.reload.api_calls.must_equal 0 end it 'fails for nonexistent site' do @@ -43,6 +44,7 @@ describe 'api info' do res[:info][:last_updated].must_equal nil res[:info][:domain].must_equal 'derp.com' res[:info][:tags].must_equal ['derpie', 'man'] + @site.reload.api_calls.must_equal 0 end it 'fails for bad auth' do @@ -145,6 +147,7 @@ describe 'api upload' do } res[:result].must_equal 'success' File.exist?(File.join(Site::SITE_FILES_ROOT, @site.username, 'lol.jpg')).must_equal true + @site.reload.api_calls.must_equal 1 end 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 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 create_site basic_authorize @user, @pass @@ -252,4 +268,4 @@ end def res JSON.parse last_response.body, symbolize_names: true -end \ No newline at end of file +end