2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-15 10:21:04 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::QueryLimiting do
|
2018-01-15 10:21:04 -05:00
|
|
|
describe '.enable?' do
|
|
|
|
it 'returns true in a test environment' do
|
|
|
|
expect(described_class.enable?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true in a development environment' do
|
2019-09-03 21:57:25 -04:00
|
|
|
stub_rails_env('development')
|
|
|
|
stub_rails_env('development')
|
2018-01-15 10:21:04 -05:00
|
|
|
|
|
|
|
expect(described_class.enable?).to eq(true)
|
|
|
|
end
|
|
|
|
|
2018-02-12 14:15:26 -05:00
|
|
|
it 'returns false on GitLab.com' do
|
2019-09-03 21:57:25 -04:00
|
|
|
stub_rails_env('production')
|
2018-01-15 10:21:04 -05:00
|
|
|
allow(Gitlab).to receive(:com?).and_return(true)
|
|
|
|
|
2018-02-12 14:15:26 -05:00
|
|
|
expect(described_class.enable?).to eq(false)
|
2018-01-15 10:21:04 -05:00
|
|
|
end
|
|
|
|
|
2018-02-12 14:15:26 -05:00
|
|
|
it 'returns false in a non GitLab.com' do
|
|
|
|
allow(Gitlab).to receive(:com?).and_return(false)
|
2019-09-03 21:57:25 -04:00
|
|
|
stub_rails_env('production')
|
2018-01-15 10:21:04 -05:00
|
|
|
|
|
|
|
expect(described_class.enable?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.whitelist' do
|
|
|
|
it 'raises ArgumentError when an invalid issue URL is given' do
|
|
|
|
expect { described_class.whitelist('foo') }
|
|
|
|
.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without a transaction' do
|
|
|
|
it 'does nothing' do
|
|
|
|
expect { described_class.whitelist('https://example.com') }
|
|
|
|
.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a transaction' do
|
|
|
|
let(:transaction) { Gitlab::QueryLimiting::Transaction.new }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::QueryLimiting::Transaction)
|
|
|
|
.to receive(:current)
|
|
|
|
.and_return(transaction)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not increment the number of SQL queries executed in the block' do
|
|
|
|
before = transaction.count
|
|
|
|
|
|
|
|
described_class.whitelist('https://example.com')
|
|
|
|
|
|
|
|
2.times do
|
|
|
|
User.count
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(transaction.count).to eq(before)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|