1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::RackAttack, :aggregate_failures do
describe '.configure' do
let(:fake_rack_attack) { class_double("Rack::Attack") }
let(:fake_rack_attack_request) { class_double("Rack::Attack::Request") }
let(:throttles) do
{
throttle_unauthenticated: Gitlab::Throttle.unauthenticated_options,
throttle_authenticated_api: Gitlab::Throttle.authenticated_api_options,
throttle_product_analytics_collector: { limit: 100, period: 60 },
throttle_unauthenticated_protected_paths: Gitlab::Throttle.unauthenticated_options,
throttle_authenticated_protected_paths_api: Gitlab::Throttle.authenticated_api_options,
throttle_authenticated_protected_paths_web: Gitlab::Throttle.authenticated_web_options
}
end
before do
stub_const("Rack::Attack", fake_rack_attack)
stub_const("Rack::Attack::Request", fake_rack_attack_request)
allow(fake_rack_attack).to receive(:throttle)
allow(fake_rack_attack).to receive(:track)
allow(fake_rack_attack).to receive(:safelist)
allow(fake_rack_attack).to receive(:blocklist)
end
it 'extends the request class' do
described_class.configure(fake_rack_attack)
expect(fake_rack_attack_request).to include(described_class::Request)
end
it 'configures the safelist' do
described_class.configure(fake_rack_attack)
expect(fake_rack_attack).to have_received(:safelist).with('throttle_bypass_header')
end
it 'configures throttles if no dry-run was configured' do
described_class.configure(fake_rack_attack)
throttles.each do |throttle, options|
expect(fake_rack_attack).to have_received(:throttle).with(throttle.to_s, options)
end
end
it 'configures tracks if dry-run was configured for all throttles' do
stub_env('GITLAB_THROTTLE_DRY_RUN', '*')
described_class.configure(fake_rack_attack)
throttles.each do |throttle, options|
expect(fake_rack_attack).to have_received(:track).with(throttle.to_s, options)
end
expect(fake_rack_attack).not_to have_received(:throttle)
end
it 'configures tracks and throttles with a selected set of dry-runs' do
dry_run_throttles = throttles.each_key.first(2)
regular_throttles = throttles.keys[2..-1]
stub_env('GITLAB_THROTTLE_DRY_RUN', dry_run_throttles.join(','))
described_class.configure(fake_rack_attack)
dry_run_throttles.each do |throttle|
expect(fake_rack_attack).to have_received(:track).with(throttle.to_s, throttles[throttle])
end
regular_throttles.each do |throttle|
expect(fake_rack_attack).to have_received(:throttle).with(throttle.to_s, throttles[throttle])
end
end
end
end
|