discourse/spec/components/rate_limiter_spec.rb

152 lines
3.7 KiB
Ruby
Raw Normal View History

require 'rails_helper'
2013-02-05 14:16:51 -05:00
require 'rate_limiter'
describe RateLimiter do
let(:user) { Fabricate(:user) }
let(:rate_limiter) { RateLimiter.new(user, "peppermint-butler", 2, 60) }
context 'disabled' do
before do
rate_limiter.performed!
2013-02-25 11:42:20 -05:00
rate_limiter.performed!
2013-02-05 14:16:51 -05:00
end
it "should be disabled" do
expect(RateLimiter.disabled?).to eq(true)
end
2013-02-05 14:16:51 -05:00
it "returns true for can_perform?" do
2015-01-09 11:34:37 -05:00
expect(rate_limiter.can_perform?).to eq(true)
2013-02-05 14:16:51 -05:00
end
it "doesn't raise an error on performed!" do
2015-01-09 11:34:37 -05:00
expect { rate_limiter.performed! }.not_to raise_error
2013-02-05 14:16:51 -05:00
end
end
context 'enabled' do
before do
RateLimiter.enable
2013-02-25 11:42:20 -05:00
rate_limiter.clear!
2013-02-05 14:16:51 -05:00
end
after do
RateLimiter.disable
end
context 'global rate limiter' do
it 'can operate in global mode' do
2017-12-04 05:44:16 -05:00
limiter = RateLimiter.new(nil, "test", 2, 30, global: true)
limiter.clear!
2017-12-04 05:44:16 -05:00
thrown = false
limiter.performed!
limiter.performed!
2017-12-04 05:44:16 -05:00
begin
limiter.performed!
rescue RateLimiter::LimitExceeded => e
2017-12-04 15:04:41 -05:00
expect(Integer === e.available_in).to eq(true)
2017-12-04 05:44:16 -05:00
expect(e.available_in).to be > 28
expect(e.available_in).to be < 32
thrown = true
end
expect(thrown).to be(true)
end
end
context 'handles readonly' do
before do
$redis.without_namespace.slaveof '10.0.0.1', '99999'
end
after do
$redis.without_namespace.slaveof 'no', 'one'
end
it 'does not explode' do
expect { rate_limiter.performed! }.not_to raise_error
end
end
2013-02-05 14:16:51 -05:00
context 'never done' do
it "should perform right away" do
2015-01-09 11:34:37 -05:00
expect(rate_limiter.can_perform?).to eq(true)
2013-02-05 14:16:51 -05:00
end
it "performs without an error" do
2015-01-09 11:34:37 -05:00
expect { rate_limiter.performed! }.not_to raise_error
2013-02-05 14:16:51 -05:00
end
end
context "remaining" do
it "updates correctly" do
expect(rate_limiter.remaining).to eq(2)
rate_limiter.performed!
expect(rate_limiter.remaining).to eq(1)
rate_limiter.performed!
expect(rate_limiter.remaining).to eq(0)
end
end
2013-02-05 14:16:51 -05:00
context "multiple calls" do
before do
rate_limiter.performed!
rate_limiter.performed!
end
it "returns false for can_perform when the limit has been hit" do
2015-01-09 11:34:37 -05:00
expect(rate_limiter.can_perform?).to eq(false)
expect(rate_limiter.remaining).to eq(0)
2013-02-05 14:16:51 -05:00
end
it "raises an error the third time called" do
2015-01-09 11:34:37 -05:00
expect { rate_limiter.performed! }.to raise_error(RateLimiter::LimitExceeded)
2013-02-05 14:16:51 -05:00
end
context "as an admin/moderator" do
it "returns true for can_perform if the user is an admin" do
user.admin = true
2015-01-09 11:34:37 -05:00
expect(rate_limiter.can_perform?).to eq(true)
expect(rate_limiter.remaining).to eq(2)
2013-02-05 14:16:51 -05:00
end
it "doesn't raise an error when an admin performs the task" do
user.admin = true
2015-01-09 11:34:37 -05:00
expect { rate_limiter.performed! }.not_to raise_error
2013-02-05 14:16:51 -05:00
end
it "returns true for can_perform if the user is a mod" do
user.moderator = true
2015-01-09 11:34:37 -05:00
expect(rate_limiter.can_perform?).to eq(true)
2013-02-05 14:16:51 -05:00
end
it "doesn't raise an error when a moderator performs the task" do
user.moderator = true
2015-01-09 11:34:37 -05:00
expect { rate_limiter.performed! }.not_to raise_error
2013-02-05 14:16:51 -05:00
end
end
context "rollback!" do
before do
rate_limiter.rollback!
end
it "returns true for can_perform since there is now room" do
2015-01-09 11:34:37 -05:00
expect(rate_limiter.can_perform?).to eq(true)
2013-02-05 14:16:51 -05:00
end
it "raises no error now that there is room" do
2015-01-09 11:34:37 -05:00
expect { rate_limiter.performed! }.not_to raise_error
2013-02-05 14:16:51 -05:00
end
end
2013-02-25 11:42:20 -05:00
end
2013-02-05 14:16:51 -05:00
end
end