2017-12-11 01:52:48 -05:00
|
|
|
# encoding: UTF-8
|
2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
2017-12-11 01:52:48 -05:00
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2018-02-09 19:09:54 -05:00
|
|
|
describe 'rate limiter integration' do
|
2017-12-11 01:52:48 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
RateLimiter.enable
|
2018-02-09 19:09:54 -05:00
|
|
|
RateLimiter.clear_all!
|
2017-12-11 01:52:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
RateLimiter.disable
|
|
|
|
end
|
|
|
|
|
2019-08-09 03:47:44 -04:00
|
|
|
it "will rate limit message bus requests once queueing" do
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
global_setting :reject_message_bus_queue_seconds, 0.1
|
|
|
|
|
|
|
|
post "/message-bus/#{SecureRandom.hex}/poll", headers: {
|
|
|
|
"HTTP_X_REQUEST_START" => "t=#{Time.now.to_f - 0.2}"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(429)
|
|
|
|
expect(response.headers['Retry-After']).to be > 29
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will not rate limit when all is good" do
|
|
|
|
freeze_time
|
|
|
|
|
|
|
|
global_setting :reject_message_bus_queue_seconds, 0.1
|
|
|
|
|
|
|
|
post "/message-bus/#{SecureRandom.hex}/poll", headers: {
|
|
|
|
"HTTP_X_REQUEST_START" => "t=#{Time.now.to_f - 0.05}"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
2018-02-09 19:09:54 -05:00
|
|
|
it "will clear the token cookie if invalid" do
|
|
|
|
name = Auth::DefaultCurrentUserProvider::TOKEN_COOKIE
|
|
|
|
|
|
|
|
# we try 11 times because the rate limit is 10
|
|
|
|
11.times {
|
|
|
|
cookies[name] = SecureRandom.hex
|
|
|
|
get '/categories.json'
|
|
|
|
expect(response.cookies.has_key?(name)).to eq(true)
|
|
|
|
expect(response.cookies[name]).to be_nil
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-03-13 11:12:41 -04:00
|
|
|
it 'can cleanly limit requests and sets a Retry-After header' do
|
2018-02-13 23:29:50 -05:00
|
|
|
freeze_time
|
2018-01-11 22:15:10 -05:00
|
|
|
#request.set_header("action_dispatch.show_exceptions", true)
|
2017-12-11 01:52:48 -05:00
|
|
|
|
|
|
|
admin = Fabricate(:admin)
|
2019-11-29 10:16:06 -05:00
|
|
|
api_key = Fabricate(:api_key, user: admin)
|
2017-12-11 01:52:48 -05:00
|
|
|
|
|
|
|
global_setting :max_admin_api_reqs_per_key_per_minute, 1
|
|
|
|
|
2018-01-11 22:15:10 -05:00
|
|
|
get '/admin/api/keys.json', params: {
|
2017-12-11 01:52:48 -05:00
|
|
|
api_key: api_key.key,
|
|
|
|
api_username: admin.username
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2018-01-11 22:15:10 -05:00
|
|
|
get '/admin/api/keys.json', params: {
|
2017-12-11 01:52:48 -05:00
|
|
|
api_key: api_key.key,
|
|
|
|
api_username: admin.username
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(429)
|
2018-02-13 23:29:50 -05:00
|
|
|
|
|
|
|
data = JSON.parse(response.body)
|
|
|
|
|
2018-03-13 11:12:41 -04:00
|
|
|
expect(response.headers['Retry-After']).to eq(60)
|
2018-02-13 23:29:50 -05:00
|
|
|
expect(data["extras"]["wait_seconds"]).to eq(60)
|
2017-12-11 01:52:48 -05:00
|
|
|
end
|
|
|
|
end
|