Commit f25b5b7f authored by Michael Kozono's avatar Michael Kozono

Fix “Share lock” help text

parent edf8dd44
...@@ -69,12 +69,18 @@ module GroupsHelper ...@@ -69,12 +69,18 @@ module GroupsHelper
{ group_name: group.name } { group_name: group.name }
end end
def share_with_group_lock_help_text def share_with_group_lock_help_text(group)
return default_help unless @group.has_parent? return default_help unless group.parent&.share_with_group_lock?
return default_help unless @group.parent.share_with_group_lock?
return parent_locked_and_has_been_overridden unless @group.share_with_group_lock? if group.share_with_group_lock?
return parent_locked_but_you_can_override if @group.has_owner?(current_user) if can?(current_user, :change_share_with_group_lock, group.parent)
return parent_locked_so_ask_the_owner ancestor_locked_but_you_can_override(group)
else
ancestor_locked_so_ask_the_owner(group)
end
else
ancestor_locked_and_has_been_overridden(group)
end
end end
private private
...@@ -93,23 +99,30 @@ module GroupsHelper ...@@ -93,23 +99,30 @@ module GroupsHelper
end end
end end
def parent_group_link def ancestor_group_link(group)
link_to @group.parent.name, group_path(@group.parent) ancestor = oldest_consecutively_locked_ancestor(group)
link_to ancestor.name, group_path(ancestor)
end
def oldest_consecutively_locked_ancestor(group)
group.ancestors.find do |group|
!group.has_parent? || !group.parent.share_with_group_lock?
end
end end
def default_help def default_help
s_("GroupSettings|This setting will be applied to all subgroups unless overridden by a group owner.") s_("GroupSettings|This setting will be applied to all subgroups unless overridden by a group owner.")
end end
def parent_locked_but_you_can_override def ancestor_locked_but_you_can_override(group)
s_("GroupSettings|This setting is applied on %{parent_group}. You can override the setting or remove the share lock from the parent group.") % { parent_group: parent_group_link } s_("GroupSettings|This setting is applied on %{ancestor_group}. You can override the setting or remove the share lock from %{ancestor_group}.") % { ancestor_group: ancestor_group_link(group) }
end end
def parent_locked_so_ask_the_owner def ancestor_locked_so_ask_the_owner(group)
s_("GroupSettings|This setting is applied on %{parent_group}. To share this group with another group, ask the owner to override the setting or remove the share lock from the parent group.") % { parent_group: parent_group_link } s_("GroupSettings|This setting is applied on %{ancestor_group}. To share projects in this group with another group, ask the owner to override the setting or remove the share lock from %{ancestor_group}.") % { ancestor_group: ancestor_group_link(group) }
end end
def parent_locked_and_has_been_overridden def ancestor_locked_and_has_been_overridden(group)
s_("GroupSettings|This setting is applied on %{parent_group} and has been overridden on this subgroup.") % { parent_group: parent_group_link } s_("GroupSettings|This setting is applied on %{ancestor_group} and has been overridden on this subgroup.") % { ancestor_group: ancestor_group_link(group) }
end end
end end
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
- group_link = link_to @group.name, group_path(@group) - group_link = link_to @group.name, group_path(@group)
= s_("GroupSettings|Prevent sharing a project within %{group} with other groups").html_safe % { group: group_link } = s_("GroupSettings|Prevent sharing a project within %{group} with other groups").html_safe % { group: group_link }
%br %br
%span.descr= share_with_group_lock_help_text %span.descr= share_with_group_lock_help_text(@group)
= render 'group_admin_settings', f: f = render 'group_admin_settings', f: f
......
...@@ -95,4 +95,87 @@ describe GroupsHelper do ...@@ -95,4 +95,87 @@ describe GroupsHelper do
.to match(/<li style="text-indent: 16px;"><a.*>#{deep_nested_group.name}.*<\/li>.*<a.*>#{very_deep_nested_group.name}<\/a>/m) .to match(/<li style="text-indent: 16px;"><a.*>#{deep_nested_group.name}.*<\/li>.*<a.*>#{very_deep_nested_group.name}<\/a>/m)
end end
end end
# rubocop:disable Layout/SpaceBeforeComma
describe '#share_with_group_lock_help_text', :nested_groups do
let!(:root_group) { create(:group) }
let!(:subgroup) { create(:group, parent: root_group) }
let!(:sub_subgroup) { create(:group, parent: subgroup) }
let(:root_owner) { create(:user) }
let(:sub_owner) { create(:user) }
let(:sub_sub_owner) { create(:user) }
let(:possible_help_texts) do
{
default_help: "This setting will be applied to all subgroups unless overridden by a group owner",
ancestor_locked_but_you_can_override: /This setting is applied on [\w\s<>="\/]+\. You can override the setting or remove the share lock from [\w\s<>="\/]+/,
ancestor_locked_so_ask_the_owner: /This setting is applied on [\w\s<>="\/]+\. To share projects in this group with another group, ask the owner to override the setting or remove the share lock from [\w\s<>="\/]+/,
ancestor_locked_and_has_been_overridden: /This setting is applied on [\w\s<>="\/]+ and has been overridden on this subgroup/
}
end
let(:possible_linked_ancestors) do
{
root_group: root_group,
subgroup: subgroup
}
end
let(:users) do
{
root_owner: root_owner,
sub_owner: sub_owner,
sub_sub_owner: sub_sub_owner
}
end
subject { helper.share_with_group_lock_help_text(sub_subgroup) }
where(:root_share_locked, :subgroup_share_locked, :sub_subgroup_share_locked, :current_user, :help_text, :linked_ancestor) do
[
[false , false , false , :root_owner , :default_help , nil],
[false , false , false , :sub_owner , :default_help , nil],
[false , false , false , :sub_sub_owner , :default_help , nil],
[false , false , true , :root_owner , :default_help , nil],
[false , false , true , :sub_owner , :default_help , nil],
[false , false , true , :sub_sub_owner , :default_help , nil],
[false , true , false , :root_owner , :ancestor_locked_and_has_been_overridden , :subgroup],
[false , true , false , :sub_owner , :ancestor_locked_and_has_been_overridden , :subgroup],
[false , true , false , :sub_sub_owner , :ancestor_locked_and_has_been_overridden , :subgroup],
[false , true , true , :root_owner , :ancestor_locked_but_you_can_override , :subgroup],
[false , true , true , :sub_owner , :ancestor_locked_but_you_can_override , :subgroup],
[false , true , true , :sub_sub_owner , :ancestor_locked_so_ask_the_owner , :subgroup],
[true , false , false , :root_owner , :default_help , nil],
[true , false , false , :sub_owner , :default_help , nil],
[true , false , false , :sub_sub_owner , :default_help , nil],
[true , false , true , :root_owner , :default_help , nil],
[true , false , true , :sub_owner , :default_help , nil],
[true , false , true , :sub_sub_owner , :default_help , nil],
[true , true , false , :root_owner , :ancestor_locked_and_has_been_overridden , :root_group],
[true , true , false , :sub_owner , :ancestor_locked_and_has_been_overridden , :root_group],
[true , true , false , :sub_sub_owner , :ancestor_locked_and_has_been_overridden , :root_group],
[true , true , true , :root_owner , :ancestor_locked_but_you_can_override , :root_group],
[true , true , true , :sub_owner , :ancestor_locked_so_ask_the_owner , :root_group],
[true , true , true , :sub_sub_owner , :ancestor_locked_so_ask_the_owner , :root_group]
]
end
with_them do
before do
root_group.add_owner(root_owner)
subgroup.add_owner(sub_owner)
sub_subgroup.add_owner(sub_sub_owner)
root_group.update_column(:share_with_group_lock, true) if root_share_locked
subgroup.update_column(:share_with_group_lock, true) if subgroup_share_locked
sub_subgroup.update_column(:share_with_group_lock, true) if sub_subgroup_share_locked
allow(helper).to receive(:current_user).and_return(users[current_user])
allow(helper).to receive(:can?)
.with(users[current_user], :change_share_with_group_lock, subgroup)
.and_return(Ability.allowed?(users[current_user], :change_share_with_group_lock, subgroup))
end
it 'has the correct help text with correct ancestor links' do
expect(subject).to match(possible_help_texts[help_text])
expect(subject).to match(possible_linked_ancestors[linked_ancestor].name) unless help_text == :default_help
end
end
end
end end
...@@ -6,8 +6,6 @@ describe 'groups/edit.html.haml' do ...@@ -6,8 +6,6 @@ describe 'groups/edit.html.haml' do
describe 'Share lock option' do describe 'Share lock option' do
let(:root_owner) { create(:user) } let(:root_owner) { create(:user) }
let(:root_group) { create(:group) } let(:root_group) { create(:group) }
let(:expected_label) { default_label }
let(:expected_help) { default_help }
before do before do
root_group.add_owner(root_owner) root_group.add_owner(root_owner)
...@@ -16,14 +14,16 @@ describe 'groups/edit.html.haml' do ...@@ -16,14 +14,16 @@ describe 'groups/edit.html.haml' do
shared_examples_for 'share lock option' do |checkbox_options| shared_examples_for 'share lock option' do |checkbox_options|
it 'should have the correct label, help text, and checkbox options' do it 'should have the correct label, help text, and checkbox options' do
assign(:group, test_group) assign(:group, test_group)
allow(view).to receive(:can?).and_return(true) allow(view).to receive(:can?).with(test_user, :admin_group, test_group).and_return(true)
allow(view).to receive(:can_change_group_visibility_level?).and_return(false) allow(view).to receive(:can_change_group_visibility_level?).and_return(false)
allow(view).to receive(:current_user).and_return(test_user) allow(view).to receive(:current_user).and_return(test_user)
expect(view).to receive(:can_change_share_with_group_lock?).and_return(!checkbox_options[:disabled])
expect(view).to receive(:share_with_group_lock_help_text).and_return('help text here')
render render
expect(rendered).to have_content(expected_label) expect(rendered).to have_content("Prevent sharing a project within #{test_group.name} with other groups")
expect(rendered).to have_css('.descr', text: expected_help) expect(rendered).to have_css('.descr', text: 'help text here')
expect(rendered).to have_field('group_share_with_group_lock', checkbox_options) expect(rendered).to have_field('group_share_with_group_lock', checkbox_options)
end end
end end
...@@ -82,14 +82,12 @@ describe 'groups/edit.html.haml' do ...@@ -82,14 +82,12 @@ describe 'groups/edit.html.haml' do
context 'when the subgroup has "Share lock" disabled (parent overridden)' do context 'when the subgroup has "Share lock" disabled (parent overridden)' do
context 'as the root_owner' do context 'as the root_owner' do
let(:test_user) { root_owner } let(:test_user) { root_owner }
let(:expected_help) { parent_locked_and_has_been_overridden }
it_behaves_like 'share lock option', { disabled: false, checked: false } it_behaves_like 'share lock option', { disabled: false, checked: false }
end end
context 'as the sub_owner' do context 'as the sub_owner' do
let(:test_user) { sub_owner } let(:test_user) { sub_owner }
let(:expected_help) { parent_locked_and_has_been_overridden }
it_behaves_like 'share lock option', { disabled: false, checked: false } it_behaves_like 'share lock option', { disabled: false, checked: false }
end end
...@@ -102,39 +100,17 @@ describe 'groups/edit.html.haml' do ...@@ -102,39 +100,17 @@ describe 'groups/edit.html.haml' do
context 'as the root_owner' do context 'as the root_owner' do
let(:test_user) { root_owner } let(:test_user) { root_owner }
let(:expected_help) { parent_locked_but_you_can_override }
it_behaves_like 'share lock option', { disabled: false, checked: true } it_behaves_like 'share lock option', { disabled: false, checked: true }
end end
context 'as the sub_owner' do context 'as the sub_owner' do
let(:test_user) { sub_owner } let(:test_user) { sub_owner }
let(:expected_help) { parent_locked_so_ask_the_owner }
it_behaves_like 'share lock option', { disabled: true, checked: true } it_behaves_like 'share lock option', { disabled: true, checked: true }
end end
end end
end end
end end
def default_label
"Prevent sharing a project within #{test_group.name} with other groups"
end
def default_help
"This setting will be applied to all subgroups unless overridden by a group owner."
end
def parent_locked_but_you_can_override
"This setting is applied on #{test_group.parent.name}. You can override the setting or remove the share lock from the parent group."
end
def parent_locked_so_ask_the_owner
"This setting is applied on #{test_group.parent.name}. To share this group with another group, ask the owner to override the setting or remove the share lock from the parent group."
end
def parent_locked_and_has_been_overridden
"This setting is applied on #{test_group.parent.name} and has been overridden on this subgroup."
end
end end
end end
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