summaryrefslogtreecommitdiff
path: root/spec/support_specs/database/prevent_cross_joins_spec.rb
blob: dd4ed9c40b83f2fdd0f01db9cb32b9860e60648d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Database::PreventCrossJoins do
  context 'when running in :prevent_cross_joins scope', :prevent_cross_joins do
    context 'when only non-CI tables are used' do
      it 'does not raise exception' do
        expect { main_only_query }.not_to raise_error
      end
    end

    context 'when only CI tables are used' do
      it 'does not raise exception' do
        expect { ci_only_query }.not_to raise_error
      end
    end

    context 'when CI and non-CI tables are used' do
      it 'raises exception' do
        expect { main_and_ci_query }.to raise_error(
          described_class::CrossJoinAcrossUnsupportedTablesError)
      end

      context 'when allow_cross_joins_across_databases is used' do
        it 'does not raise exception' do
          Gitlab::Database.allow_cross_joins_across_databases(url: 'http://issue-url')

          expect { main_and_ci_query }.not_to raise_error
        end
      end
    end
  end

  context 'when running in a default scope' do
    context 'when CI and non-CI tables are used' do
      it 'does not raise exception' do
        expect { main_and_ci_query }.not_to raise_error
      end
    end
  end

  private

  def main_only_query
    Issue.joins(:project).last
  end

  def ci_only_query
    Ci::Build.joins(:pipeline).last
  end

  def main_and_ci_query
    Ci::Build.joins(:project).last
  end
end