summaryrefslogtreecommitdiff
path: root/spec/models/ci/job_token/scope_spec.rb
blob: 4b95adf8476e0f10fa1d56c54f7beba55cdc997b (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::JobToken::Scope do
  let_it_be(:project) { create(:project, ci_job_token_scope_enabled: true).tap(&:save!) }

  let(:scope) { described_class.new(project) }

  describe '#all_projects' do
    subject(:all_projects) { scope.all_projects }

    context 'when no projects are added to the scope' do
      it 'returns the project defining the scope' do
        expect(all_projects).to contain_exactly(project)
      end
    end

    context 'when other projects are added to the scope' do
      let_it_be(:scoped_project) { create(:project) }
      let_it_be(:unscoped_project) { create(:project) }

      let!(:link_in_scope) { create(:ci_job_token_project_scope_link, source_project: project, target_project: scoped_project) }
      let!(:link_out_of_scope) { create(:ci_job_token_project_scope_link, target_project: unscoped_project) }

      it 'returns all projects that can be accessed from a given scope' do
        expect(subject).to contain_exactly(project, scoped_project)
      end
    end
  end

  describe '#includes?' do
    subject { scope.includes?(target_project) }

    context 'when param is the project defining the scope' do
      let(:target_project) { project }

      it { is_expected.to be_truthy }
    end

    context 'when param is a project in scope' do
      let(:target_link) { create(:ci_job_token_project_scope_link, source_project: project) }
      let(:target_project) { target_link.target_project }

      it { is_expected.to be_truthy }
    end

    context 'when param is a project in another scope' do
      let(:scope_link) { create(:ci_job_token_project_scope_link) }
      let(:target_project) { scope_link.target_project }

      it { is_expected.to be_falsey }

      context 'when project scope setting is disabled' do
        before do
          project.ci_job_token_scope_enabled = false
        end

        it 'considers any project to be part of the scope' do
          expect(subject).to be_truthy
        end
      end
    end
  end
end