2019-09-30 08:06:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-26 01:01:52 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe VisibilityLevelHelper do
|
2018-08-31 13:16:34 -04:00
|
|
|
include ProjectForksHelper
|
|
|
|
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { build(:project) }
|
2016-03-01 10:22:29 -05:00
|
|
|
let(:group) { build(:group) }
|
2015-12-07 07:37:01 -05:00
|
|
|
let(:personal_snippet) { build(:personal_snippet) }
|
|
|
|
let(:project_snippet) { build(:project_snippet) }
|
2015-04-26 01:01:52 -04:00
|
|
|
|
2018-07-23 05:39:03 -04:00
|
|
|
describe 'visibility_icon_description' do
|
|
|
|
context 'used with a Project' do
|
|
|
|
it 'delegates projects to #project_visibility_icon_description' do
|
|
|
|
expect(visibility_icon_description(project))
|
|
|
|
.to match /project/i
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'used with a ProjectPresenter' do
|
|
|
|
it 'delegates projects to #project_visibility_icon_description' do
|
|
|
|
expect(visibility_icon_description(project.present))
|
|
|
|
.to match /project/i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'used with a Group' do
|
|
|
|
it 'delegates groups to #group_visibility_icon_description' do
|
|
|
|
expect(visibility_icon_description(group))
|
|
|
|
.to match /group/i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-05 07:08:55 -05:00
|
|
|
describe 'visibility_level_label' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:level_value, :level_name) do
|
|
|
|
Gitlab::VisibilityLevel::PRIVATE | 'Private'
|
|
|
|
Gitlab::VisibilityLevel::INTERNAL | 'Internal'
|
|
|
|
Gitlab::VisibilityLevel::PUBLIC | 'Public'
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it 'returns the name of the visibility level' do
|
|
|
|
expect(visibility_level_label(level_value)).to eq(level_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-26 01:01:52 -04:00
|
|
|
describe 'visibility_level_description' do
|
2015-12-07 07:37:01 -05:00
|
|
|
context 'used with a Project' do
|
2020-12-21 04:10:07 -05:00
|
|
|
let(:descriptions) do
|
|
|
|
[
|
|
|
|
visibility_level_description(Gitlab::VisibilityLevel::PRIVATE, project),
|
|
|
|
visibility_level_description(Gitlab::VisibilityLevel::INTERNAL, project),
|
|
|
|
visibility_level_description(Gitlab::VisibilityLevel::PUBLIC, project)
|
|
|
|
]
|
2015-04-26 01:01:52 -04:00
|
|
|
end
|
|
|
|
|
2020-12-21 04:10:07 -05:00
|
|
|
it 'returns different project related descriptions depending on visibility level' do
|
|
|
|
expect(descriptions.uniq.size).to eq(descriptions.size)
|
|
|
|
expect(descriptions).to all match /project/i
|
2016-03-01 10:22:29 -05:00
|
|
|
end
|
|
|
|
end
|
2015-04-26 01:01:52 -04:00
|
|
|
|
2020-12-21 04:10:07 -05:00
|
|
|
context 'used with a Group' do
|
|
|
|
let(:descriptions) do
|
|
|
|
[
|
|
|
|
visibility_level_description(Gitlab::VisibilityLevel::PRIVATE, group),
|
|
|
|
visibility_level_description(Gitlab::VisibilityLevel::INTERNAL, group),
|
|
|
|
visibility_level_description(Gitlab::VisibilityLevel::PUBLIC, group)
|
|
|
|
]
|
|
|
|
end
|
2015-04-26 01:01:52 -04:00
|
|
|
|
2020-12-21 04:10:07 -05:00
|
|
|
it 'returns different group related descriptions depending on visibility level' do
|
|
|
|
expect(descriptions.uniq.size).to eq(descriptions.size)
|
|
|
|
expect(descriptions).to all match /group/i
|
|
|
|
end
|
2015-04-26 01:01:52 -04:00
|
|
|
end
|
2015-12-07 07:37:01 -05:00
|
|
|
end
|
2015-04-26 01:01:52 -04:00
|
|
|
|
2017-08-24 18:24:29 -04:00
|
|
|
describe "disallowed_visibility_level?" do
|
2015-07-06 08:38:43 -04:00
|
|
|
describe "forks" do
|
2018-08-31 13:16:34 -04:00
|
|
|
let(:project) { create(:project, :internal) }
|
|
|
|
let(:forked_project) { fork_project(project) }
|
2015-07-06 08:38:43 -04:00
|
|
|
|
2017-08-24 18:24:29 -04:00
|
|
|
it "disallows levels" do
|
2018-08-31 13:16:34 -04:00
|
|
|
expect(disallowed_visibility_level?(forked_project, Gitlab::VisibilityLevel::PUBLIC)).to be_truthy
|
|
|
|
expect(disallowed_visibility_level?(forked_project, Gitlab::VisibilityLevel::INTERNAL)).to be_falsey
|
|
|
|
expect(disallowed_visibility_level?(forked_project, Gitlab::VisibilityLevel::PRIVATE)).to be_falsey
|
2015-07-06 08:38:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "non-forked project" do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :internal) }
|
2015-07-06 08:38:43 -04:00
|
|
|
|
2017-08-24 18:24:29 -04:00
|
|
|
it "disallows levels" do
|
|
|
|
expect(disallowed_visibility_level?(project, Gitlab::VisibilityLevel::PUBLIC)).to be_falsey
|
|
|
|
expect(disallowed_visibility_level?(project, Gitlab::VisibilityLevel::INTERNAL)).to be_falsey
|
|
|
|
expect(disallowed_visibility_level?(project, Gitlab::VisibilityLevel::PRIVATE)).to be_falsey
|
2015-07-06 08:38:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-24 18:55:43 -04:00
|
|
|
describe "group" do
|
|
|
|
let(:group) { create(:group, :internal) }
|
|
|
|
|
|
|
|
it "disallows levels" do
|
|
|
|
expect(disallowed_visibility_level?(group, Gitlab::VisibilityLevel::PUBLIC)).to be_falsey
|
|
|
|
expect(disallowed_visibility_level?(group, Gitlab::VisibilityLevel::INTERNAL)).to be_falsey
|
|
|
|
expect(disallowed_visibility_level?(group, Gitlab::VisibilityLevel::PRIVATE)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "sub-group" do
|
|
|
|
let(:group) { create(:group, :private) }
|
|
|
|
let(:subgroup) { create(:group, :private, parent: group) }
|
|
|
|
|
|
|
|
it "disallows levels" do
|
|
|
|
expect(disallowed_visibility_level?(subgroup, Gitlab::VisibilityLevel::PUBLIC)).to be_truthy
|
|
|
|
expect(disallowed_visibility_level?(subgroup, Gitlab::VisibilityLevel::INTERNAL)).to be_truthy
|
|
|
|
expect(disallowed_visibility_level?(subgroup, Gitlab::VisibilityLevel::PRIVATE)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "snippet" do
|
2016-03-08 16:40:45 -05:00
|
|
|
let(:snippet) { create(:snippet, :internal) }
|
2015-07-06 08:38:43 -04:00
|
|
|
|
2017-08-24 18:24:29 -04:00
|
|
|
it "disallows levels" do
|
|
|
|
expect(disallowed_visibility_level?(snippet, Gitlab::VisibilityLevel::PUBLIC)).to be_falsey
|
|
|
|
expect(disallowed_visibility_level?(snippet, Gitlab::VisibilityLevel::INTERNAL)).to be_falsey
|
|
|
|
expect(disallowed_visibility_level?(snippet, Gitlab::VisibilityLevel::PRIVATE)).to be_falsey
|
2015-07-06 08:38:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-25 16:40:21 -04:00
|
|
|
|
2019-05-26 01:42:42 -04:00
|
|
|
describe "selected_visibility_level" do
|
|
|
|
let(:group) { create(:group, :public) }
|
|
|
|
let!(:project) { create(:project, :internal, group: group) }
|
|
|
|
let!(:forked_project) { fork_project(project) }
|
|
|
|
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2020-05-17 20:08:13 -04:00
|
|
|
public_vis = Gitlab::VisibilityLevel::PUBLIC
|
|
|
|
internal_vis = Gitlab::VisibilityLevel::INTERNAL
|
|
|
|
private_vis = Gitlab::VisibilityLevel::PRIVATE
|
2019-05-26 01:42:42 -04:00
|
|
|
|
|
|
|
# This is a subset of all the permutations
|
|
|
|
where(:requested_level, :max_allowed, :global_default_level, :restricted_levels, :expected) do
|
2020-05-17 20:08:13 -04:00
|
|
|
public_vis | public_vis | public_vis | [] | public_vis
|
|
|
|
public_vis | public_vis | public_vis | [public_vis] | internal_vis
|
|
|
|
internal_vis | public_vis | public_vis | [] | internal_vis
|
|
|
|
internal_vis | private_vis | private_vis | [] | private_vis
|
|
|
|
private_vis | public_vis | public_vis | [] | private_vis
|
|
|
|
public_vis | private_vis | internal_vis | [] | private_vis
|
|
|
|
public_vis | internal_vis | public_vis | [] | internal_vis
|
|
|
|
public_vis | private_vis | public_vis | [] | private_vis
|
|
|
|
public_vis | internal_vis | internal_vis | [] | internal_vis
|
|
|
|
public_vis | public_vis | internal_vis | [] | public_vis
|
2019-05-26 01:42:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_application_setting(restricted_visibility_levels: restricted_levels,
|
|
|
|
default_project_visibility: global_default_level)
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
it "provides correct visibility level for forked project" do
|
2020-07-27 08:09:50 -04:00
|
|
|
project.update!(visibility_level: max_allowed)
|
2019-05-26 01:42:42 -04:00
|
|
|
|
|
|
|
expect(selected_visibility_level(forked_project, requested_level)).to eq(expected)
|
|
|
|
end
|
|
|
|
|
2020-07-27 08:09:50 -04:00
|
|
|
it "provides correct visibility level for project in group" do
|
|
|
|
project.update!(visibility_level: max_allowed)
|
|
|
|
project.group.update!(visibility_level: max_allowed)
|
2019-05-26 01:42:42 -04:00
|
|
|
|
|
|
|
expect(selected_visibility_level(project, requested_level)).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-06-28 17:38:26 -04:00
|
|
|
|
2020-05-26 17:07:45 -04:00
|
|
|
shared_examples_for 'available visibility level' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
subject { helper.available_visibility_levels(form_model) }
|
|
|
|
|
|
|
|
public_vis = Gitlab::VisibilityLevel::PUBLIC
|
|
|
|
internal_vis = Gitlab::VisibilityLevel::INTERNAL
|
|
|
|
private_vis = Gitlab::VisibilityLevel::PRIVATE
|
|
|
|
|
|
|
|
where(:restricted_visibility_levels, :expected) do
|
|
|
|
[] | [private_vis, internal_vis, public_vis]
|
|
|
|
[private_vis] | [internal_vis, public_vis]
|
|
|
|
[private_vis, internal_vis] | [public_vis]
|
|
|
|
[private_vis, public_vis] | [internal_vis]
|
|
|
|
[internal_vis] | [private_vis, public_vis]
|
|
|
|
[internal_vis, private_vis] | [public_vis]
|
|
|
|
[internal_vis, public_vis] | [private_vis]
|
|
|
|
[public_vis] | [private_vis, internal_vis]
|
|
|
|
[public_vis, private_vis] | [internal_vis]
|
|
|
|
[public_vis, internal_vis] | [private_vis]
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:current_user) { user }
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
stub_application_setting(restricted_visibility_levels: restricted_visibility_levels)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(expected) }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'excludes disallowed visibility levels' do
|
|
|
|
stub_application_setting(restricted_visibility_levels: [])
|
|
|
|
allow(helper).to receive(:disallowed_visibility_level?).with(form_model, private_vis) { true }
|
|
|
|
allow(helper).to receive(:disallowed_visibility_level?).with(form_model, internal_vis) { false }
|
|
|
|
allow(helper).to receive(:disallowed_visibility_level?).with(form_model, public_vis) { false }
|
|
|
|
|
|
|
|
expect(subject).to eq([internal_vis, public_vis])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#available_visibility_levels' do
|
|
|
|
it_behaves_like 'available visibility level' do
|
|
|
|
let(:form_model) { project_snippet }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'available visibility level' do
|
|
|
|
let(:form_model) { personal_snippet }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'available visibility level' do
|
|
|
|
let(:form_model) { project }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'available visibility level' do
|
|
|
|
let(:form_model) { group }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#snippets_selected_visibility_level' do
|
|
|
|
let(:available_levels) { [Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::INTERNAL] }
|
|
|
|
|
|
|
|
it 'returns the selected visibility level' do
|
|
|
|
expect(helper.snippets_selected_visibility_level(available_levels, Gitlab::VisibilityLevel::PUBLIC))
|
|
|
|
.to eq(Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "fallbacks using the lowest available visibility level when selected level isn't available" do
|
|
|
|
expect(helper.snippets_selected_visibility_level(available_levels, Gitlab::VisibilityLevel::PRIVATE))
|
|
|
|
.to eq(Gitlab::VisibilityLevel::INTERNAL)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-28 17:38:26 -04:00
|
|
|
describe 'multiple_visibility_levels_restricted?' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
subject { helper.multiple_visibility_levels_restricted? }
|
|
|
|
|
|
|
|
where(:restricted_visibility_levels, :expected) do
|
|
|
|
[Gitlab::VisibilityLevel::PUBLIC] | false
|
|
|
|
[Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::INTERNAL] | true
|
|
|
|
[Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PRIVATE] | true
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:current_user) { user }
|
|
|
|
allow(Gitlab::CurrentSettings.current_application_settings).to receive(:restricted_visibility_levels) { restricted_visibility_levels }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(expected) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'all_visibility_levels_restricted?' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
subject { helper.all_visibility_levels_restricted? }
|
|
|
|
|
|
|
|
where(:restricted_visibility_levels, :expected) do
|
|
|
|
[Gitlab::VisibilityLevel::PUBLIC] | false
|
|
|
|
[Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::INTERNAL] | false
|
|
|
|
Gitlab::VisibilityLevel.values | true
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:current_user) { user }
|
|
|
|
allow(Gitlab::CurrentSettings.current_application_settings).to receive(:restricted_visibility_levels) { restricted_visibility_levels }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(expected) }
|
|
|
|
end
|
|
|
|
end
|
2020-12-14 10:09:40 -05:00
|
|
|
|
|
|
|
describe '#visibility_level_options' do
|
|
|
|
let(:user) { build(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(helper).to receive(:current_user).and_return(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the desired mapping' do
|
|
|
|
expected_options = [
|
|
|
|
{
|
|
|
|
level: 0,
|
|
|
|
label: 'Private',
|
|
|
|
description: 'The group and its projects can only be viewed by members.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
level: 10,
|
|
|
|
label: 'Internal',
|
|
|
|
description: 'The group and any internal projects can be viewed by any logged in user except external users.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
level: 20,
|
|
|
|
label: 'Public',
|
|
|
|
description: 'The group and any public projects can be viewed without any authentication.'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
expect(helper.visibility_level_options(group)).to eq expected_options
|
|
|
|
end
|
|
|
|
end
|
2015-04-26 01:01:52 -04:00
|
|
|
end
|