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