Welcome to mirror list, hosted at ThFree Co, Russian Federation.

prevent_cross_joins_spec.rb « database « support_specs « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: efeabd15b5893c576992d55a6d2e9ea24a882a75 (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
83
84
85
86
87
88
89
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Database::PreventCrossJoins do
  context 'when running in a default scope' 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 annotation is used' do
        it 'does not raise exception' do
          expect { main_and_ci_allowed_via_annotate }.not_to raise_error
        end
      end

      context 'when allow_cross_joins_across_databases is used' do
        it 'does not raise exception' do
          expect { main_and_ci_query_allowlisted }.not_to raise_error
        end
      end

      context 'when allow_cross_joins_across_databases is used' do
        it 'does not raise exception' do
          expect { main_and_ci_query_allowlist_nested }.not_to raise_error
        end
      end

      context 'when there is a parser error' do
        it 'does not raise parse PGQuery::ParseError' do
          # Since this is in an invalid query it still raises from ActiveRecord
          # but this tests that we rescue the PGQuery::ParseError which would
          # have otherwise raised first
          expect { ApplicationRecord.connection.execute('SELECT SELECT FROM SELECT') }.to raise_error(ActiveRecord::StatementInvalid)
        end
      end
    end
  end

  private

  def main_and_ci_query_allowlisted
    Gitlab::Database.allow_cross_joins_across_databases(url: 'http://issue-url') do
      main_and_ci_query
    end
  end

  def main_and_ci_query_allowlist_nested
    Gitlab::Database.allow_cross_joins_across_databases(url: 'http://issue-url') do
      main_and_ci_query_allowlisted

      main_and_ci_query
    end
  end

  def main_and_ci_allowed_via_annotate
    main_and_ci_query do |relation|
      relation.allow_cross_joins_across_databases(url: 'http://issue-url')
    end
  end

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

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

  def main_and_ci_query
    relation = Ci::Build.joins(:project)
    relation = yield(relation) if block_given?
    relation.last
  end
end