2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-20 17:05:53 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Gitlab::VisibilityLevel do
|
2017-03-20 17:05:53 -04:00
|
|
|
describe '.level_value' do
|
|
|
|
it 'converts "public" to integer value' do
|
|
|
|
expect(described_class.level_value('public')).to eq(Gitlab::VisibilityLevel::PUBLIC)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'converts string integer to integer value' do
|
|
|
|
expect(described_class.level_value('20')).to eq(20)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults to PRIVATE when string value is not valid' do
|
|
|
|
expect(described_class.level_value('invalid')).to eq(Gitlab::VisibilityLevel::PRIVATE)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults to PRIVATE when integer value is not valid' do
|
|
|
|
expect(described_class.level_value(100)).to eq(Gitlab::VisibilityLevel::PRIVATE)
|
|
|
|
end
|
|
|
|
end
|
2017-06-13 10:44:55 -04:00
|
|
|
|
|
|
|
describe '.levels_for_user' do
|
|
|
|
it 'returns all levels for an admin' do
|
2017-06-22 02:35:49 -04:00
|
|
|
user = build(:user, :admin)
|
2017-06-13 10:44:55 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(described_class.levels_for_user(user))
|
|
|
|
.to eq([Gitlab::VisibilityLevel::PRIVATE,
|
|
|
|
Gitlab::VisibilityLevel::INTERNAL,
|
|
|
|
Gitlab::VisibilityLevel::PUBLIC])
|
2017-06-13 10:44:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns INTERNAL and PUBLIC for internal users' do
|
2017-06-22 02:35:49 -04:00
|
|
|
user = build(:user)
|
2017-06-13 10:44:55 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(described_class.levels_for_user(user))
|
|
|
|
.to eq([Gitlab::VisibilityLevel::INTERNAL,
|
|
|
|
Gitlab::VisibilityLevel::PUBLIC])
|
2017-06-13 10:44:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns PUBLIC for external users' do
|
2017-06-22 02:35:49 -04:00
|
|
|
user = build(:user, :external)
|
2017-06-13 10:44:55 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(described_class.levels_for_user(user))
|
|
|
|
.to eq([Gitlab::VisibilityLevel::PUBLIC])
|
2017-06-13 10:44:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns PUBLIC when no user is given' do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(described_class.levels_for_user)
|
|
|
|
.to eq([Gitlab::VisibilityLevel::PUBLIC])
|
2017-06-13 10:44:55 -04:00
|
|
|
end
|
|
|
|
end
|
2017-12-20 10:19:54 -05:00
|
|
|
|
|
|
|
describe '.allowed_levels' do
|
|
|
|
it 'only includes the levels that arent restricted' do
|
|
|
|
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::INTERNAL])
|
|
|
|
|
|
|
|
expect(described_class.allowed_levels)
|
|
|
|
.to contain_exactly(described_class::PRIVATE, described_class::PUBLIC)
|
|
|
|
end
|
2018-02-02 05:23:05 -05:00
|
|
|
|
|
|
|
it 'returns all levels when no visibility level was set' do
|
|
|
|
allow(described_class)
|
|
|
|
.to receive_message_chain('current_application_settings.restricted_visibility_levels')
|
|
|
|
.and_return(nil)
|
|
|
|
|
|
|
|
expect(described_class.allowed_levels)
|
|
|
|
.to contain_exactly(described_class::PRIVATE, described_class::INTERNAL, described_class::PUBLIC)
|
|
|
|
end
|
2017-12-20 10:19:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.closest_allowed_level' do
|
|
|
|
it 'picks INTERNAL instead of PUBLIC if public is restricted' do
|
|
|
|
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
|
|
|
|
|
|
|
|
expect(described_class.closest_allowed_level(described_class::PUBLIC))
|
|
|
|
.to eq(described_class::INTERNAL)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'picks PRIVATE if nothing is available' do
|
|
|
|
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC,
|
|
|
|
Gitlab::VisibilityLevel::INTERNAL,
|
|
|
|
Gitlab::VisibilityLevel::PRIVATE])
|
|
|
|
|
|
|
|
expect(described_class.closest_allowed_level(described_class::PUBLIC))
|
|
|
|
.to eq(described_class::PRIVATE)
|
|
|
|
end
|
|
|
|
end
|
2019-04-12 01:00:50 -04:00
|
|
|
|
|
|
|
describe '.valid_level?' do
|
|
|
|
it 'returns true when visibility is valid' do
|
|
|
|
expect(described_class.valid_level?(described_class::PRIVATE)).to be_truthy
|
|
|
|
expect(described_class.valid_level?(described_class::INTERNAL)).to be_truthy
|
|
|
|
expect(described_class.valid_level?(described_class::PUBLIC)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2019-11-28 10:06:57 -05:00
|
|
|
|
2020-04-27 14:09:41 -04:00
|
|
|
describe '.restricted_level?, .non_restricted_level?, and .public_level_restricted?' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:visibility_levels, :expected_status) do
|
|
|
|
nil | false
|
|
|
|
[Gitlab::VisibilityLevel::PRIVATE] | false
|
|
|
|
[Gitlab::VisibilityLevel::PRIVATE, Gitlab::VisibilityLevel::INTERNAL] | false
|
|
|
|
[Gitlab::VisibilityLevel::PUBLIC] | true
|
|
|
|
[Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::INTERNAL] | true
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
before do
|
|
|
|
stub_application_setting(restricted_visibility_levels: visibility_levels)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the expected status' do
|
|
|
|
expect(described_class.restricted_level?(Gitlab::VisibilityLevel::PUBLIC)).to eq(expected_status)
|
|
|
|
expect(described_class.non_restricted_level?(Gitlab::VisibilityLevel::PUBLIC)).to eq(!expected_status)
|
|
|
|
expect(described_class.public_visibility_restricted?).to eq(expected_status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-28 10:06:57 -05:00
|
|
|
describe '#visibility_level_decreased?' do
|
|
|
|
let(:project) { create(:project, :internal) }
|
|
|
|
|
|
|
|
context 'when visibility level decreases' do
|
|
|
|
before do
|
|
|
|
project.update!(visibility_level: described_class::PRIVATE)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(project.visibility_level_decreased?).to be(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when visibility level does not decrease' do
|
|
|
|
before do
|
|
|
|
project.update!(visibility_level: described_class::PUBLIC)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(project.visibility_level_decreased?).to be(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-20 17:05:53 -04:00
|
|
|
end
|