debian-mirror-gitlab/spec/policies/base_policy_spec.rb

92 lines
2.4 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2020-05-24 23:13:21 +05:30
describe BasePolicy do
2019-07-07 11:18:12 +05:30
include ExternalAuthorizationServiceHelpers
2019-12-26 22:10:19 +05:30
include AdminModeHelper
2019-07-07 11:18:12 +05:30
2017-08-17 22:00:37 +05:30
describe '.class_for' do
it 'detects policy class based on the subject ancestors' do
2017-09-10 17:25:29 +05:30
expect(DeclarativePolicy.class_for(GenericCommitStatus.new)).to eq(CommitStatusPolicy)
2017-08-17 22:00:37 +05:30
end
it 'detects policy class for a presented subject' do
presentee = Ci::BuildPresenter.new(Ci::Build.new)
2017-09-10 17:25:29 +05:30
expect(DeclarativePolicy.class_for(presentee)).to eq(Ci::BuildPolicy)
2017-08-17 22:00:37 +05:30
end
it 'uses GlobalPolicy when :global is given' do
2017-09-10 17:25:29 +05:30
expect(DeclarativePolicy.class_for(:global)).to eq(GlobalPolicy)
2017-08-17 22:00:37 +05:30
end
end
2019-07-07 11:18:12 +05:30
describe 'read cross project' do
2020-03-13 15:44:24 +05:30
let(:current_user) { build_stubbed(:user) }
let(:user) { build_stubbed(:user) }
2019-07-07 11:18:12 +05:30
subject { described_class.new(current_user, [user]) }
it { is_expected.to be_allowed(:read_cross_project) }
2020-04-22 19:07:51 +05:30
context 'for anonymous' do
let(:current_user) { nil }
it { is_expected.to be_allowed(:read_cross_project) }
end
2019-07-07 11:18:12 +05:30
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) }
2019-12-26 22:10:19 +05:30
context 'for admins' do
2020-03-13 15:44:24 +05:30
let(:current_user) { build_stubbed(:admin) }
2019-12-26 22:10:19 +05:30
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
2020-04-22 19:07:51 +05:30
context 'for anonymous' do
let(:current_user) { nil }
it { is_expected.not_to be_allowed(:read_cross_project) }
end
2019-12-26 22:10:19 +05:30
end
end
describe 'full private access' do
2020-03-13 15:44:24 +05:30
let(:current_user) { build_stubbed(:user) }
2019-12-26 22:10:19 +05:30
subject { described_class.new(current_user, nil) }
it { is_expected.not_to be_allowed(:read_all_resources) }
context 'for admins' do
2020-03-13 15:44:24 +05:30
let(:current_user) { build_stubbed(:admin) }
2019-12-26 22:10:19 +05:30
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)
2019-07-07 11:18:12 +05:30
end
end
end
2017-08-17 22:00:37 +05:30
end