mirror of
https://github.com/neocities/neocities.git
synced 2025-04-24 17:22:35 +02:00
track api call counts that require auth
This commit is contained in:
parent
cd94d2ce36
commit
02cea425e9
3 changed files with 29 additions and 2 deletions
|
@ -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
|
||||
|
|
9
migrations/056_add_api_calls.rb
Normal file
9
migrations/056_add_api_calls.rb
Normal 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
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue