global_policy_spec.rb 1.48 KB
Newer Older
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
# frozen_string_literal: true

require 'spec_helper'

describe GlobalPolicy do
  include ExternalAuthorizationServiceHelpers

  let(:current_user) { create(:user) }
  let(:user) { create(:user) }

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

  describe 'reading operations dashboard' do
    before do
      stub_licensed_features(operations_dashboard: true)
    end

    it { is_expected.to be_allowed(:read_operations_dashboard) }

    context 'when unlicensed' do
      before do
        stub_licensed_features(operations_dashboard: false)
      end

      it { is_expected.not_to be_allowed(:read_operations_dashboard) }
    end
  end
28 29 30 31 32 33

  it { is_expected.to be_disallowed(:read_licenses) }
  it { is_expected.to be_disallowed(:destroy_licenses) }

  it { expect(described_class.new(create(:admin), [user])).to be_allowed(:read_licenses) }
  it { expect(described_class.new(create(:admin), [user])).to be_allowed(:destroy_licenses) }
34

35 36
  shared_examples 'analytics policy' do |action|
    context 'anonymous user' do
37
      let(:current_user) { nil }
38

39 40 41
      it 'is not allowed' do
        is_expected.not_to be_allowed(action)
      end
42 43
    end

44 45 46 47
    context 'authenticated user' do
      it 'is allowed' do
        is_expected.to be_allowed(action)
      end
48 49
    end
  end
50 51 52 53 54 55 56 57

  describe 'view_code_analytics' do
    include_examples 'analytics policy', :view_code_analytics
  end

  describe 'view_productivity_analytics' do
    include_examples 'analytics policy', :view_productivity_analytics
  end
58
end