Commit f285328c authored by Jacopo's avatar Jacopo

Fallback to lowest visibility level in snippet visibility radio

In snippets form when the default visibility level is not
available fallback to the lowest available visibility level.
parent f9bc8d20
......@@ -167,6 +167,17 @@ module VisibilityLevelHelper
[requested_level, max_allowed_visibility_level(form_model)].min
end
def available_visibility_levels(form_model)
Gitlab::VisibilityLevel.values.reject do |level|
disallowed_visibility_level?(form_model, level) ||
restricted_visibility_levels.include?(level)
end
end
def snippets_selected_visibility_level(visibility_levels, selected)
visibility_levels.find { |level| level == selected } || visibility_levels.min
end
def multiple_visibility_levels_restricted?
restricted_visibility_levels.many? # rubocop: disable CodeReuse/ActiveRecord
end
......
- Gitlab::VisibilityLevel.values.each do |level|
- disallowed = disallowed_visibility_level?(form_model, level)
- restricted = restricted_visibility_levels.include?(level)
- next if disallowed || restricted
- available_visibility_levels = available_visibility_levels(form_model)
- selected_level = snippets_selected_visibility_level(available_visibility_levels, selected_level)
- available_visibility_levels.each do |level|
.form-check
= form.radio_button model_method, level, checked: (selected_level == level), class: 'form-check-input', data: { track_label: "blank_project", track_event: "activate_form_input", track_property: "#{model_method}_#{level}", track_value: "", qa_selector: "#{visibility_level_label(level).downcase}_radio" }
= form.label "#{model_method}_#{level}", class: 'form-check-label' do
......
---
title: Fallback to lowest visibility level in snippet visibility radio
merge_request: 31847
author: Jacopo Beschi @jacopo-beschi
type: fixed
......@@ -7,7 +7,6 @@ shared_examples_for 'snippet editor' do
stub_feature_flags(snippets_vue: false)
stub_feature_flags(snippets_edit_vue: false)
sign_in(user)
visit new_snippet_path
end
def description_field
......@@ -28,6 +27,8 @@ shared_examples_for 'snippet editor' do
end
it 'Authenticated user creates a snippet' do
visit new_snippet_path
fill_form
click_button('Create snippet')
......@@ -42,6 +43,8 @@ shared_examples_for 'snippet editor' do
end
it 'previews a snippet with file' do
visit new_snippet_path
# Click placeholder first to expand full description field
description_field.click
fill_in 'personal_snippet_description', with: 'My Snippet'
......@@ -62,6 +65,8 @@ shared_examples_for 'snippet editor' do
end
it 'uploads a file when dragging into textarea' do
visit new_snippet_path
fill_form
dropzone_file Rails.root.join('spec', 'fixtures', 'banana_sample.gif')
......@@ -86,6 +91,8 @@ shared_examples_for 'snippet editor' do
allow(instance).to receive(:create_commit).and_raise(StandardError, error)
end
visit new_snippet_path
fill_form
click_button('Create snippet')
......@@ -107,6 +114,8 @@ shared_examples_for 'snippet editor' do
end
it 'validation fails for the first time' do
visit new_snippet_path
fill_in 'personal_snippet_title', with: 'My Snippet Title'
click_button('Create snippet')
......@@ -132,6 +141,8 @@ shared_examples_for 'snippet editor' do
end
it 'Authenticated user creates a snippet with + in filename' do
visit new_snippet_path
fill_in 'personal_snippet_title', with: 'My Snippet Title'
page.within('.file-editor') do
find(:xpath, "//input[@id='personal_snippet_file_name']").set 'snippet+file+name'
......@@ -146,6 +157,27 @@ shared_examples_for 'snippet editor' do
expect(page).to have_content('snippet+file+name')
expect(page).to have_content('Hello World!')
end
context 'when snippets default visibility level is restricted' do
before do
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PRIVATE],
default_snippet_visibility: Gitlab::VisibilityLevel::PRIVATE)
end
it 'creates a snippet using the lowest available visibility level as default' do
visit new_snippet_path
fill_form
click_button('Create snippet')
wait_for_requests
visit snippets_path
click_link('Internal')
expect(page).to have_content('My Snippet Title')
end
end
end
describe 'User creates snippet', :js do
......
......@@ -184,6 +184,84 @@ describe VisibilityLevelHelper do
end
end
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
describe 'multiple_visibility_levels_restricted?' do
using RSpec::Parameterized::TableSyntax
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment