debian-mirror-gitlab/spec/models/project_feature_spec.rb

191 lines
6.1 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2016-09-29 09:46:39 +05:30
require 'spec_helper'
describe ProjectFeature do
2020-03-13 15:44:24 +05:30
using RSpec::Parameterized::TableSyntax
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2016-09-29 09:46:39 +05:30
let(:user) { create(:user) }
2019-12-04 20:38:33 +05:30
describe 'PRIVATE_FEATURES_MIN_ACCESS_LEVEL_FOR_PRIVATE_PROJECT' do
it 'has higher level than that of PRIVATE_FEATURES_MIN_ACCESS_LEVEL' do
described_class::PRIVATE_FEATURES_MIN_ACCESS_LEVEL_FOR_PRIVATE_PROJECT.each do |feature, level|
if generic_level = described_class::PRIVATE_FEATURES_MIN_ACCESS_LEVEL[feature]
expect(level).to be >= generic_level
end
end
end
end
2016-11-03 12:29:30 +05:30
context 'repository related features' do
before do
2018-11-18 11:00:15 +05:30
project.project_feature.update(
2016-11-03 12:29:30 +05:30
merge_requests_access_level: ProjectFeature::DISABLED,
builds_access_level: ProjectFeature::DISABLED,
repository_access_level: ProjectFeature::PRIVATE
)
end
it "does not allow repository related features have higher level" do
features = %w(builds merge_requests)
project_feature = project.project_feature
features.each do |feature|
field = "#{feature}_access_level".to_sym
project_feature.update_attribute(field, ProjectFeature::ENABLED)
2020-05-24 23:13:21 +05:30
expect(project_feature.valid?).to be_falsy, "#{field} failed"
2016-11-03 12:29:30 +05:30
end
end
end
2018-12-05 23:21:45 +05:30
context 'public features' do
2020-04-22 19:07:51 +05:30
features = %w(issues wiki builds merge_requests snippets repository metrics_dashboard)
2018-12-05 23:21:45 +05:30
2020-03-13 15:44:24 +05:30
features.each do |feature|
it "does not allow public access level for #{feature}" do
project_feature = project.project_feature
2018-12-05 23:21:45 +05:30
field = "#{feature}_access_level".to_sym
project_feature.update_attribute(field, ProjectFeature::PUBLIC)
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
expect(project_feature.valid?).to be_falsy, "#{field} failed"
2018-12-05 23:21:45 +05:30
end
end
end
2019-09-30 21:07:59 +05:30
describe 'default pages access level' do
2020-03-13 15:44:24 +05:30
subject { project_feature.pages_access_level }
2019-09-30 21:07:59 +05:30
2020-03-13 15:44:24 +05:30
let(:project_feature) do
2019-09-30 21:07:59 +05:30
# project factory overrides all values in project_feature after creation
project.project_feature.destroy!
project.build_project_feature.save!
2020-03-13 15:44:24 +05:30
project.project_feature
2019-09-30 21:07:59 +05:30
end
context 'when new project is private' do
let(:project) { create(:project, :private) }
it { is_expected.to eq(ProjectFeature::PRIVATE) }
end
context 'when new project is internal' do
let(:project) { create(:project, :internal) }
it { is_expected.to eq(ProjectFeature::PRIVATE) }
end
context 'when new project is public' do
let(:project) { create(:project, :public) }
it { is_expected.to eq(ProjectFeature::ENABLED) }
2020-03-13 15:44:24 +05:30
context 'when access control is forced on the admin level' do
before do
allow(::Gitlab::Pages).to receive(:access_control_is_forced?).and_return(true)
end
it { is_expected.to eq(ProjectFeature::PRIVATE) }
end
2019-09-30 21:07:59 +05:30
end
end
2019-10-03 12:08:05 +05:30
2019-12-04 20:38:33 +05:30
describe '#public_pages?' do
2020-05-24 23:13:21 +05:30
it 'returns true if Pages access control is not enabled' do
2019-12-04 20:38:33 +05:30
stub_config(pages: { access_control: false })
2020-03-13 15:44:24 +05:30
project_feature = described_class.new(pages_access_level: described_class::PRIVATE)
2019-12-04 20:38:33 +05:30
expect(project_feature.public_pages?).to eq(true)
end
2020-03-13 15:44:24 +05:30
context 'when Pages access control is enabled' do
2019-12-04 20:38:33 +05:30
before do
stub_config(pages: { access_control: true })
end
2020-03-13 15:44:24 +05:30
where(:project_visibility, :pages_access_level, :result) do
:private | ProjectFeature::PUBLIC | true
:internal | ProjectFeature::PUBLIC | true
:internal | ProjectFeature::ENABLED | false
:public | ProjectFeature::ENABLED | true
:private | ProjectFeature::PRIVATE | false
:public | ProjectFeature::PRIVATE | false
2019-12-04 20:38:33 +05:30
end
2020-03-13 15:44:24 +05:30
with_them do
let(:project_feature) do
project = build(:project, project_visibility)
project_feature = project.project_feature
project_feature.update!(pages_access_level: pages_access_level)
project_feature
end
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
it 'properly handles project and Pages visibility settings' do
expect(project_feature.public_pages?).to eq(result)
end
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
it 'returns false if access_control is forced on the admin level' do
stub_application_setting(force_pages_access_control: true)
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
expect(project_feature.public_pages?).to eq(false)
end
2019-12-04 20:38:33 +05:30
end
end
2020-03-13 15:44:24 +05:30
end
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
describe '#private_pages?' do
subject(:project_feature) { described_class.new }
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
it 'returns false if public_pages? is true' do
expect(project_feature).to receive(:public_pages?).and_return(true)
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
expect(project_feature.private_pages?).to eq(false)
end
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
it 'returns true if public_pages? is false' do
expect(project_feature).to receive(:public_pages?).and_return(false)
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
expect(project_feature.private_pages?).to eq(true)
2019-12-04 20:38:33 +05:30
end
end
2019-10-03 12:08:05 +05:30
describe '.required_minimum_access_level' do
it 'handles reporter level' do
expect(described_class.required_minimum_access_level(:merge_requests)).to eq(Gitlab::Access::REPORTER)
end
it 'handles guest level' do
expect(described_class.required_minimum_access_level(:issues)).to eq(Gitlab::Access::GUEST)
end
it 'accepts ActiveModel' do
expect(described_class.required_minimum_access_level(MergeRequest)).to eq(Gitlab::Access::REPORTER)
end
it 'accepts string' do
expect(described_class.required_minimum_access_level('merge_requests')).to eq(Gitlab::Access::REPORTER)
end
2019-12-04 20:38:33 +05:30
it 'handles repository' do
expect(described_class.required_minimum_access_level(:repository)).to eq(Gitlab::Access::GUEST)
end
2019-10-03 12:08:05 +05:30
it 'raises error if feature is invalid' do
expect do
described_class.required_minimum_access_level(:foos)
2020-05-24 23:13:21 +05:30
end.to raise_error(ArgumentError)
2019-10-03 12:08:05 +05:30
end
end
2019-12-04 20:38:33 +05:30
describe '.required_minimum_access_level_for_private_project' do
it 'returns higher permission for repository' do
expect(described_class.required_minimum_access_level_for_private_project(:repository)).to eq(Gitlab::Access::REPORTER)
end
it 'returns normal permission for issues' do
expect(described_class.required_minimum_access_level_for_private_project(:issues)).to eq(Gitlab::Access::GUEST)
end
end
2016-09-29 09:46:39 +05:30
end