Commit 69bf9241 authored by Quang-Minh Nguyen's avatar Quang-Minh Nguyen

Refactor rack attack test assessments

parent 0abeefaf
......@@ -29,15 +29,11 @@ RSpec.describe Gitlab::RackAttack::InstrumentedCacheStore do
with_them do
it 'publishes a notification' do
published = false
event = nil
begin
subscriber = ActiveSupport::Notifications.subscribe("redis.rack_attack") do |*args|
published = true
event = ActiveSupport::Notifications::Event.new(*args)
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
subject.send(operation, *params) {}
......@@ -45,35 +41,39 @@ RSpec.describe Gitlab::RackAttack::InstrumentedCacheStore do
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end
expect(published).to be(true)
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("Some thing went wrong")
published = false
exception = false
allow(store).to receive(operation).and_raise('Something went wrong')
event = nil
exception = nil
begin
subscriber = ActiveSupport::Notifications.subscribe("redis.rack_attack") do |*args|
published = true
event = ActiveSupport::Notifications::Event.new(*args)
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
begin
subject.send(operation, *params) {}
rescue
# Ignore the error
exception = true
rescue => e
exception = e
end
ensure
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end
expect(published).to be(true)
expect(exception).to be(true)
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
......
......@@ -158,7 +158,7 @@ RSpec.describe 'Rack Attack global throttles', :use_clean_rails_memory_store_cac
matched: 'throttle_unauthenticated'
})
expect(Gitlab::AuthLogger).to receive(:error).with(arguments).at_least(:once)
expect(Gitlab::AuthLogger).to receive(:error).with(arguments)
get url_that_does_not_require_authentication
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment