debian-mirror-gitlab/spec/lib/gitlab/query_limiting_spec.rb

87 lines
2.2 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'spec_helper'
2021-04-29 21:17:54 +05:30
RSpec.describe Gitlab::QueryLimiting, :request_store do
describe '.enabled_for_env?' do
2018-03-17 18:26:18 +05:30
it 'returns true in a test environment' do
2021-04-29 21:17:54 +05:30
expect(described_class.enabled_for_env?).to eq(true)
2018-03-17 18:26:18 +05:30
end
it 'returns true in a development environment' do
2019-12-04 20:38:33 +05:30
stub_rails_env('development')
stub_rails_env('development')
2018-03-17 18:26:18 +05:30
2021-04-29 21:17:54 +05:30
expect(described_class.enabled_for_env?).to eq(true)
2018-03-17 18:26:18 +05:30
end
it 'returns false on GitLab.com' do
2019-12-04 20:38:33 +05:30
stub_rails_env('production')
2018-03-17 18:26:18 +05:30
allow(Gitlab).to receive(:com?).and_return(true)
2021-04-29 21:17:54 +05:30
expect(described_class.enabled_for_env?).to eq(false)
2018-03-17 18:26:18 +05:30
end
it 'returns false in a non GitLab.com' do
allow(Gitlab).to receive(:com?).and_return(false)
2019-12-04 20:38:33 +05:30
stub_rails_env('production')
2018-03-17 18:26:18 +05:30
2021-04-29 21:17:54 +05:30
expect(described_class.enabled_for_env?).to eq(false)
2018-03-17 18:26:18 +05:30
end
end
2021-04-29 21:17:54 +05:30
shared_context 'disable and enable' do |result|
let(:transaction) { Gitlab::QueryLimiting::Transaction.new }
let(:code) do
proc do
2.times { User.count }
end
2018-03-17 18:26:18 +05:30
end
2021-04-29 21:17:54 +05:30
before do
allow(Gitlab::QueryLimiting::Transaction)
.to receive(:current)
.and_return(transaction)
2018-03-17 18:26:18 +05:30
end
2021-04-29 21:17:54 +05:30
end
2018-03-17 18:26:18 +05:30
2021-04-29 21:17:54 +05:30
describe '.disable!' do
include_context 'disable and enable'
2018-03-17 18:26:18 +05:30
2021-04-29 21:17:54 +05:30
it 'raises an ArgumentError when an invalid issue URL is given' do
expect { described_class.disable!('foo') }
.to raise_error(ArgumentError)
end
2018-03-17 18:26:18 +05:30
2021-04-29 21:17:54 +05:30
it 'stops the number of SQL queries from being incremented' do
described_class.disable!('https://example.com')
2018-03-17 18:26:18 +05:30
2021-04-29 21:17:54 +05:30
expect { code.call }.not_to change { transaction.count }
end
end
2018-03-17 18:26:18 +05:30
2021-04-29 21:17:54 +05:30
describe '.enable!' do
include_context 'disable and enable'
2018-03-17 18:26:18 +05:30
2021-04-29 21:17:54 +05:30
it 'allows the number of SQL queries to be incremented' do
described_class.enable!
2021-04-17 20:07:23 +05:30
2021-04-29 21:17:54 +05:30
expect { code.call }.to change { transaction.count }.by(2)
end
end
2021-04-17 20:07:23 +05:30
2021-04-29 21:17:54 +05:30
describe '#enabled?' do
it 'returns true when enabled' do
Gitlab::SafeRequestStore[:query_limiting_disabled] = nil
2021-04-17 20:07:23 +05:30
2021-04-29 21:17:54 +05:30
expect(described_class).to be_enabled
end
2021-04-17 20:07:23 +05:30
2021-04-29 21:17:54 +05:30
it 'returns false when disabled' do
Gitlab::SafeRequestStore[:query_limiting_disabled] = true
2021-04-17 20:07:23 +05:30
2021-04-29 21:17:54 +05:30
expect(described_class).not_to be_enabled
2018-03-17 18:26:18 +05:30
end
end
end