summaryrefslogtreecommitdiff
path: root/spec/policies/base_policy_spec.rb
blob: 67f7452528a60a499e03cb8c003aafc27bdd794b (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
90
91
# frozen_string_literal: true

require 'spec_helper'

describe BasePolicy do
  include ExternalAuthorizationServiceHelpers
  include AdminModeHelper

  describe '.class_for' do
    it 'detects policy class based on the subject ancestors' do
      expect(DeclarativePolicy.class_for(GenericCommitStatus.new)).to eq(CommitStatusPolicy)
    end

    it 'detects policy class for a presented subject' do
      presentee = Ci::BuildPresenter.new(Ci::Build.new)

      expect(DeclarativePolicy.class_for(presentee)).to eq(Ci::BuildPolicy)
    end

    it 'uses GlobalPolicy when :global is given' do
      expect(DeclarativePolicy.class_for(:global)).to eq(GlobalPolicy)
    end
  end

  describe 'read cross project' do
    let(:current_user) { build_stubbed(:user) }
    let(:user) { build_stubbed(:user) }

    subject { described_class.new(current_user, [user]) }

    it { is_expected.to be_allowed(:read_cross_project) }

    context 'for anonymous' do
      let(:current_user) { nil }

      it { is_expected.to be_allowed(:read_cross_project) }
    end

    context 'when an external authorization service is enabled' do
      before do
        enable_external_authorization_service_check
      end

      it { is_expected.not_to be_allowed(:read_cross_project) }

      context 'for admins' do
        let(:current_user) { build_stubbed(:admin) }

        subject { described_class.new(current_user, nil) }

        it 'allowed when in admin mode' do
          enable_admin_mode!(current_user)

          is_expected.to be_allowed(:read_cross_project)
        end

        it 'prevented when not in admin mode' do
          is_expected.not_to be_allowed(:read_cross_project)
        end
      end

      context 'for anonymous' do
        let(:current_user) { nil }

        it { is_expected.not_to be_allowed(:read_cross_project) }
      end
    end
  end

  describe 'full private access' do
    let(:current_user) { build_stubbed(:user) }

    subject { described_class.new(current_user, nil) }

    it { is_expected.not_to be_allowed(:read_all_resources) }

    context 'for admins' do
      let(:current_user) { build_stubbed(:admin) }

      it 'allowed when in admin mode' do
        enable_admin_mode!(current_user)

        is_expected.to be_allowed(:read_all_resources)
      end

      it 'prevented when not in admin mode' do
        is_expected.not_to be_allowed(:read_all_resources)
      end
    end
  end
end