summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/query_limiting_spec.rb
blob: 4f70c65adca67bfe2c4cb1de711e925d553c59d9 (plain)
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
78
79
80
81
82
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::QueryLimiting do
  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
      stub_rails_env('development')
      stub_rails_env('development')

      expect(described_class.enable?).to eq(true)
    end

    it 'returns false on GitLab.com' do
      stub_rails_env('production')
      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)
      stub_rails_env('production')

      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

      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
    end
  end
end