diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 18:12:25 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-20 18:12:25 +0300 |
commit | 068b3a417794ab8506b2e149301b3a60c01df078 (patch) | |
tree | 26ce51b45ae535a6fc47fb04cad8da42ec408a2f /spec/lib | |
parent | 62c78157be8fe8888787162293f13945a5fa5d3e (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/api/helpers/rate_limiter_spec.rb | 73 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/samplers/action_cable_sampler_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/samplers/ruby_sampler_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab_edition_spec.rb | 160 | ||||
-rw-r--r-- | spec/lib/gitlab_spec.rb | 131 | ||||
-rw-r--r-- | spec/lib/sidebars/projects/panel_spec.rb | 3 |
6 files changed, 250 insertions, 121 deletions
diff --git a/spec/lib/api/helpers/rate_limiter_spec.rb b/spec/lib/api/helpers/rate_limiter_spec.rb new file mode 100644 index 00000000000..2fed1cf3604 --- /dev/null +++ b/spec/lib/api/helpers/rate_limiter_spec.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe API::Helpers::RateLimiter do + let(:key) { :some_key } + let(:scope) { [:some, :scope] } + let(:request) { instance_double('Rack::Request') } + let(:user) { build_stubbed(:user) } + + let(:api_class) do + Class.new do + include API::Helpers::RateLimiter + + attr_reader :request, :current_user + + def initialize(request, current_user) + @request = request + @current_user = current_user + end + + def render_api_error!(**args) + end + end + end + + subject { api_class.new(request, user) } + + before do + allow(::Gitlab::ApplicationRateLimiter).to receive(:throttled?) + allow(::Gitlab::ApplicationRateLimiter).to receive(:log_request) + end + + describe '#check_rate_limit!' do + it 'calls ApplicationRateLimiter#throttled? with the right arguments' do + expect(::Gitlab::ApplicationRateLimiter).to receive(:throttled?).with(key, scope: scope).and_return(false) + expect(subject).not_to receive(:render_api_error!) + + subject.check_rate_limit!(key, scope: scope) + end + + it 'renders api error and logs request if throttled' do + expect(::Gitlab::ApplicationRateLimiter).to receive(:throttled?).with(key, scope: scope).and_return(true) + expect(::Gitlab::ApplicationRateLimiter).to receive(:log_request).with(request, "#{key}_request_limit".to_sym, user) + expect(subject).to receive(:render_api_error!).with({ error: _('This endpoint has been requested too many times. Try again later.') }, 429) + + subject.check_rate_limit!(key, scope: scope) + end + + context 'when the bypass header is set' do + before do + allow(Gitlab::Throttle).to receive(:bypass_header).and_return('SOME_HEADER') + end + + it 'skips rate limit if set to "1"' do + allow(request).to receive(:get_header).with(Gitlab::Throttle.bypass_header).and_return('1') + + expect(::Gitlab::ApplicationRateLimiter).not_to receive(:throttled?) + expect(subject).not_to receive(:render_api_error!) + + subject.check_rate_limit!(key, scope: scope) + end + + it 'does not skip rate limit if set to something else than "1"' do + allow(request).to receive(:get_header).with(Gitlab::Throttle.bypass_header).and_return('0') + + expect(::Gitlab::ApplicationRateLimiter).to receive(:throttled?) + + subject.check_rate_limit!(key, scope: scope) + end + end + end +end diff --git a/spec/lib/gitlab/metrics/samplers/action_cable_sampler_spec.rb b/spec/lib/gitlab/metrics/samplers/action_cable_sampler_spec.rb index d834b796179..e1e4877cd50 100644 --- a/spec/lib/gitlab/metrics/samplers/action_cable_sampler_spec.rb +++ b/spec/lib/gitlab/metrics/samplers/action_cable_sampler_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' RSpec.describe Gitlab::Metrics::Samplers::ActionCableSampler do let(:action_cable) { instance_double(ActionCable::Server::Base) } - subject { described_class.new(action_cable: action_cable) } + subject { described_class.new(action_cable: action_cable, logger: double) } it_behaves_like 'metrics sampler', 'ACTION_CABLE_SAMPLER' diff --git a/spec/lib/gitlab/metrics/samplers/ruby_sampler_spec.rb b/spec/lib/gitlab/metrics/samplers/ruby_sampler_spec.rb index 6f1e0480197..a4877208bcf 100644 --- a/spec/lib/gitlab/metrics/samplers/ruby_sampler_spec.rb +++ b/spec/lib/gitlab/metrics/samplers/ruby_sampler_spec.rb @@ -84,7 +84,7 @@ RSpec.describe Gitlab::Metrics::Samplers::RubySampler do end describe '#sample_gc' do - let!(:sampler) { described_class.new(5) } + let!(:sampler) { described_class.new } let(:gc_reports) { [{ GC_TIME: 0.1 }, { GC_TIME: 0.2 }, { GC_TIME: 0.3 }] } diff --git a/spec/lib/gitlab_edition_spec.rb b/spec/lib/gitlab_edition_spec.rb new file mode 100644 index 00000000000..2f1316819ec --- /dev/null +++ b/spec/lib/gitlab_edition_spec.rb @@ -0,0 +1,160 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe GitlabEdition do + before do + # Make sure the ENV is clean + stub_env('FOSS_ONLY', nil) + stub_env('EE_ONLY', nil) + + described_class.instance_variable_set(:@is_ee, nil) + described_class.instance_variable_set(:@is_jh, nil) + end + + after do + described_class.instance_variable_set(:@is_ee, nil) + described_class.instance_variable_set(:@is_jh, nil) + end + + describe '.root' do + it 'returns the root path of the app' do + expect(described_class.root).to eq(Pathname.new(File.expand_path('../..', __dir__))) + end + end + + describe 'extensions' do + context 'when .jh? is true' do + before do + allow(described_class).to receive(:jh?).and_return(true) + end + + it 'returns %w[ee jh]' do + expect(described_class.extensions).to match_array(%w[ee jh]) + end + end + + context 'when .ee? is true' do + before do + allow(described_class).to receive(:jh?).and_return(false) + allow(described_class).to receive(:ee?).and_return(true) + end + + it 'returns %w[ee]' do + expect(described_class.extensions).to match_array(%w[ee]) + end + end + + context 'when neither .jh? and .ee? are true' do + before do + allow(described_class).to receive(:jh?).and_return(false) + allow(described_class).to receive(:ee?).and_return(false) + end + + it 'returns the exyensions according to the current edition' do + expect(described_class.extensions).to be_empty + end + end + end + + describe '.ee? and .jh?' do + def stub_path(*paths, **arguments) + root = Pathname.new('dummy') + pathname = double(:path, **arguments) + + allow(described_class) + .to receive(:root) + .and_return(root) + + allow(root).to receive(:join) + + paths.each do |path| + allow(root) + .to receive(:join) + .with(path) + .and_return(pathname) + end + end + + describe '.ee?' do + context 'for EE' do + before do + stub_path('ee/app/models/license.rb', exist?: true) + end + + context 'when using FOSS_ONLY=1' do + before do + stub_env('FOSS_ONLY', '1') + end + + it 'returns not to be EE' do + expect(described_class).not_to be_ee + end + end + + context 'when using FOSS_ONLY=0' do + before do + stub_env('FOSS_ONLY', '0') + end + + it 'returns to be EE' do + expect(described_class).to be_ee + end + end + + context 'when using default FOSS_ONLY' do + it 'returns to be EE' do + expect(described_class).to be_ee + end + end + end + + context 'for CE' do + before do + stub_path('ee/app/models/license.rb', exist?: false) + end + + it 'returns not to be EE' do + expect(described_class).not_to be_ee + end + end + end + + describe '.jh?' do + context 'for JH' do + before do + stub_path( + 'ee/app/models/license.rb', + 'jh', + exist?: true) + end + + context 'when using default FOSS_ONLY and EE_ONLY' do + it 'returns to be JH' do + expect(described_class).to be_jh + end + end + + context 'when using FOSS_ONLY=1' do + before do + stub_env('FOSS_ONLY', '1') + end + + it 'returns not to be JH' do + expect(described_class).not_to be_jh + end + end + + context 'when using EE_ONLY=1' do + before do + stub_env('EE_ONLY', '1') + end + + it 'returns not to be JH' do + expect(described_class).not_to be_jh + end + end + end + end + end +end diff --git a/spec/lib/gitlab_spec.rb b/spec/lib/gitlab_spec.rb index 869eaf26772..49ba4debe31 100644 --- a/spec/lib/gitlab_spec.rb +++ b/spec/lib/gitlab_spec.rb @@ -3,9 +3,19 @@ require 'spec_helper' RSpec.describe Gitlab do - describe '.root' do - it 'returns the root path of the app' do - expect(described_class.root).to eq(Pathname.new(File.expand_path('../..', __dir__))) + %w[root extensions ee? jh?].each do |method_name| + it "delegates #{method_name} to GitlabEdition" do + expect(GitlabEdition).to receive(method_name) + + described_class.public_send(method_name) + end + end + + %w[ee jh].each do |method_name| + it "delegates #{method_name} to GitlabEdition" do + expect(GitlabEdition).to receive(method_name) + + described_class.public_send(method_name) {} end end @@ -248,121 +258,6 @@ RSpec.describe Gitlab do end end - describe 'ee? and jh?' do - before do - # Make sure the ENV is clean - stub_env('FOSS_ONLY', nil) - stub_env('EE_ONLY', nil) - - described_class.instance_variable_set(:@is_ee, nil) - described_class.instance_variable_set(:@is_jh, nil) - end - - after do - described_class.instance_variable_set(:@is_ee, nil) - described_class.instance_variable_set(:@is_jh, nil) - end - - def stub_path(*paths, **arguments) - root = Pathname.new('dummy') - pathname = double(:path, **arguments) - - allow(described_class) - .to receive(:root) - .and_return(root) - - allow(root).to receive(:join) - - paths.each do |path| - allow(root) - .to receive(:join) - .with(path) - .and_return(pathname) - end - end - - describe '.ee?' do - context 'for EE' do - before do - stub_path('ee/app/models/license.rb', exist?: true) - end - - context 'when using FOSS_ONLY=1' do - before do - stub_env('FOSS_ONLY', '1') - end - - it 'returns not to be EE' do - expect(described_class).not_to be_ee - end - end - - context 'when using FOSS_ONLY=0' do - before do - stub_env('FOSS_ONLY', '0') - end - - it 'returns to be EE' do - expect(described_class).to be_ee - end - end - - context 'when using default FOSS_ONLY' do - it 'returns to be EE' do - expect(described_class).to be_ee - end - end - end - - context 'for CE' do - before do - stub_path('ee/app/models/license.rb', exist?: false) - end - - it 'returns not to be EE' do - expect(described_class).not_to be_ee - end - end - end - - describe '.jh?' do - context 'for JH' do - before do - stub_path( - 'ee/app/models/license.rb', - 'jh', - exist?: true) - end - - context 'when using default FOSS_ONLY and EE_ONLY' do - it 'returns to be JH' do - expect(described_class).to be_jh - end - end - - context 'when using FOSS_ONLY=1' do - before do - stub_env('FOSS_ONLY', '1') - end - - it 'returns not to be JH' do - expect(described_class).not_to be_jh - end - end - - context 'when using EE_ONLY=1' do - before do - stub_env('EE_ONLY', '1') - end - - it 'returns not to be JH' do - expect(described_class).not_to be_jh - end - end - end - end - end - describe '.http_proxy_env?' do it 'returns true when lower case https' do stub_env('https_proxy', 'https://my.proxy') diff --git a/spec/lib/sidebars/projects/panel_spec.rb b/spec/lib/sidebars/projects/panel_spec.rb index 2e79ced7039..7e69a2dfe52 100644 --- a/spec/lib/sidebars/projects/panel_spec.rb +++ b/spec/lib/sidebars/projects/panel_spec.rb @@ -3,7 +3,8 @@ require 'spec_helper' RSpec.describe Sidebars::Projects::Panel do - let(:project) { build(:project) } + let_it_be(:project) { create(:project) } + let(:context) { Sidebars::Projects::Context.new(current_user: nil, container: project) } subject { described_class.new(context) } |