diff options
Diffstat (limited to 'spec/lib/gitlab/rack_attack')
-rw-r--r-- | spec/lib/gitlab/rack_attack/instrumented_cache_store_spec.rb | 89 | ||||
-rw-r--r-- | spec/lib/gitlab/rack_attack/store_spec.rb | 113 |
2 files changed, 113 insertions, 89 deletions
diff --git a/spec/lib/gitlab/rack_attack/instrumented_cache_store_spec.rb b/spec/lib/gitlab/rack_attack/instrumented_cache_store_spec.rb deleted file mode 100644 index 8151519ddec..00000000000 --- a/spec/lib/gitlab/rack_attack/instrumented_cache_store_spec.rb +++ /dev/null @@ -1,89 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::RackAttack::InstrumentedCacheStore do - using RSpec::Parameterized::TableSyntax - - let(:store) { ::ActiveSupport::Cache::NullStore.new } - - subject { described_class.new(upstream_store: store) } - - where(:operation, :params, :test_proc) do - :fetch | [:key] | ->(s) { s.fetch(:key) } - :read | [:key] | ->(s) { s.read(:key) } - :read_multi | [:key_1, :key_2, :key_3] | ->(s) { s.read_multi(:key_1, :key_2, :key_3) } - :write_multi | [{ key_1: 1, key_2: 2, key_3: 3 }] | ->(s) { s.write_multi(key_1: 1, key_2: 2, key_3: 3) } - :fetch_multi | [:key_1, :key_2, :key_3] | ->(s) { s.fetch_multi(:key_1, :key_2, :key_3) {} } - :write | [:key, :value, { option_1: 1 }] | ->(s) { s.write(:key, :value, option_1: 1) } - :delete | [:key] | ->(s) { s.delete(:key) } - :exist? | [:key, { option_1: 1 }] | ->(s) { s.exist?(:key, option_1: 1) } - :delete_matched | [/^key$/, { option_1: 1 }] | ->(s) { s.delete_matched(/^key$/, option_1: 1 ) } - :increment | [:key, 1] | ->(s) { s.increment(:key, 1) } - :decrement | [:key, 1] | ->(s) { s.decrement(:key, 1) } - :cleanup | [] | ->(s) { s.cleanup } - :clear | [] | ->(s) { s.clear } - end - - with_them do - it 'publishes a notification' do - event = nil - - begin - subscriber = ActiveSupport::Notifications.subscribe("redis.rack_attack") do |*args| - event = ActiveSupport::Notifications::Event.new(*args) - end - - test_proc.call(subject) - ensure - ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber - end - - expect(event).not_to be_nil - expect(event.name).to eq("redis.rack_attack") - expect(event.duration).to be_a(Float).and(be > 0.0) - expect(event.payload[:operation]).to eql(operation) - end - - it 'publishes a notification even if the cache store returns an error' do - allow(store).to receive(operation).and_raise('Something went wrong') - - event = nil - exception = nil - - begin - subscriber = ActiveSupport::Notifications.subscribe("redis.rack_attack") do |*args| - event = ActiveSupport::Notifications::Event.new(*args) - end - - begin - test_proc.call(subject) - rescue StandardError => e - exception = e - end - ensure - ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber - end - - expect(event).not_to be_nil - expect(event.name).to eq("redis.rack_attack") - expect(event.duration).to be_a(Float).and(be > 0.0) - expect(event.payload[:operation]).to eql(operation) - - expect(exception).not_to be_nil - expect(exception.message).to eql('Something went wrong') - end - - it 'delegates to the upstream store' do - allow(store).to receive(operation).and_call_original - - if params.empty? - expect(store).to receive(operation).with(no_args) - else - expect(store).to receive(operation).with(*params) - end - - test_proc.call(subject) - end - end -end diff --git a/spec/lib/gitlab/rack_attack/store_spec.rb b/spec/lib/gitlab/rack_attack/store_spec.rb new file mode 100644 index 00000000000..19b3f239d91 --- /dev/null +++ b/spec/lib/gitlab/rack_attack/store_spec.rb @@ -0,0 +1,113 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::RackAttack::Store, :clean_gitlab_redis_rate_limiting, feature_category: :scalability do + let(:store) { described_class.new } + let(:key) { 'foobar' } + let(:namespaced_key) { "cache:gitlab:#{key}" } + + def with_redis(&block) + Gitlab::Redis::RateLimiting.with(&block) + end + + describe '#increment' do + it 'increments without expiry' do + 5.times do |i| + expect(store.increment(key, 1)).to eq(i + 1) + + with_redis do |redis| + expect(redis.get(namespaced_key).to_i).to eq(i + 1) + expect(redis.ttl(namespaced_key)).to eq(-1) + end + end + end + + it 'rejects amounts other than 1' do + expect { store.increment(key, 2) }.to raise_exception(described_class::InvalidAmount) + end + + context 'with expiry' do + it 'increments and sets expiry' do + 5.times do |i| + expect(store.increment(key, 1, expires_in: 456)).to eq(i + 1) + + with_redis do |redis| + expect(redis.get(namespaced_key).to_i).to eq(i + 1) + expect(redis.ttl(namespaced_key)).to be_within(10).of(456) + end + end + end + end + end + + describe '#read' do + subject { store.read(key) } + + it 'reads the namespaced key' do + with_redis { |r| r.set(namespaced_key, '123') } + + expect(subject).to eq('123') + end + end + + describe '#write' do + subject { store.write(key, '123', options) } + + let(:options) { {} } + + it 'sets the key' do + subject + + with_redis do |redis| + expect(redis.get(namespaced_key)).to eq('123') + expect(redis.ttl(namespaced_key)).to eq(-1) + end + end + + context 'with expiry' do + let(:options) { { expires_in: 456 } } + + it 'sets the key with expiry' do + subject + + with_redis do |redis| + expect(redis.get(namespaced_key)).to eq('123') + expect(redis.ttl(namespaced_key)).to be_within(10).of(456) + end + end + end + end + + describe '#delete' do + subject { store.delete(key) } + + it { expect(subject).to eq(0) } + + context 'when the key exists' do + before do + with_redis { |r| r.set(namespaced_key, '123') } + end + + it { expect(subject).to eq(1) } + end + end + + describe '#with' do + subject { store.send(:with, &:ping) } + + it { expect(subject).to eq('PONG') } + + context 'when redis is unavailable' do + before do + broken_redis = Redis.new( + url: 'redis://127.0.0.0:0', + instrumentation_class: Gitlab::Redis::RateLimiting.instrumentation_class + ) + allow(Gitlab::Redis::RateLimiting).to receive(:with).and_yield(broken_redis) + end + + it { expect(subject).to eq(nil) } + end + end +end |