2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::QueryLimiting do
|
2018-03-17 18:26:18 +05:30
|
|
|
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-12-04 20:38:33 +05:30
|
|
|
stub_rails_env('development')
|
|
|
|
stub_rails_env('development')
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
expect(described_class.enable?).to eq(true)
|
|
|
|
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)
|
|
|
|
|
|
|
|
expect(described_class.enable?).to eq(false)
|
|
|
|
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
|
|
|
|
|
|
|
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
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
it 'whitelists when enabled' do
|
|
|
|
described_class.whitelist('https://example.com')
|
|
|
|
|
|
|
|
expect(transaction.whitelisted).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not whitelist when disabled' do
|
|
|
|
allow(described_class).to receive(:enable?).and_return(false)
|
|
|
|
|
|
|
|
described_class.whitelist('https://example.com')
|
|
|
|
|
|
|
|
expect(transaction.whitelisted).to eq(false)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|