Commit ced6a706 authored by Andrew Fontaine's avatar Andrew Fontaine

Refactor Edit Feature Flags Data Set

Instead of adding several methods to the feature flags helper that then
need to be overridden in the EE version, we can create one method that
returns the entire hash of values and then append to that map with the
EE only properties by overriding that single method.
parent 46026b23
......@@ -95,7 +95,7 @@ export default {
return this.formStrategies.filter((s) => !s.shouldBeDestroyed);
},
showRelatedIssues() {
return this.featureFlagIssuesEndpoint.length > 0;
return Boolean(this.featureFlagIssuesEndpoint);
},
},
methods: {
......
......@@ -11,8 +11,15 @@ module FeatureFlagsHelper
project.feature_flags_client_token
end
def feature_flag_issues_links_endpoint(_project, _feature_flag, _user)
''
def edit_feature_flag_data
{
endpoint: project_feature_flag_path(@project, @feature_flag),
project_id: @project.id,
feature_flags_path: project_feature_flags_path(@project),
environments_endpoint: search_project_environments_path(@project, format: :json),
strategy_type_docs_page_path: help_page_path('operations/feature_flags', anchor: 'feature-flag-strategies'),
environments_scope_docs_path: help_page_path('ci/environments/index.md', anchor: 'scope-environments-with-specs')
}
end
end
......
......@@ -4,10 +4,4 @@
- breadcrumb_title @feature_flag.name
- page_title s_('FeatureFlags|Edit Feature Flag')
#js-edit-feature-flag{ data: { endpoint: project_feature_flag_path(@project, @feature_flag),
project_id: @project.id,
feature_flags_path: project_feature_flags_path(@project),
environments_endpoint: search_project_environments_path(@project, format: :json),
strategy_type_docs_page_path: help_page_path('operations/feature_flags', anchor: 'feature-flag-strategies'),
environments_scope_docs_path: help_page_path('ci/environments/index.md', anchor: 'scope-environments-with-specs'),
feature_flag_issues_endpoint: feature_flag_issues_links_endpoint(@project, @feature_flag, current_user) } }
#js-edit-feature-flag{ data: edit_feature_flag_data }
......@@ -4,7 +4,13 @@ module EE
module FeatureFlagsHelper
extend ::Gitlab::Utils::Override
override :feature_flag_issues_links_endpoint
override :edit_feature_flag_data
def edit_feature_flag_data
super.merge(feature_flag_issues_endpoint: feature_flag_issues_links_endpoint(@project, @feature_flag, current_user))
end
private
def feature_flag_issues_links_endpoint(project, feature_flag, user)
return '' unless can?(user, :admin_feature_flags_issue_links, project)
......
......@@ -3,23 +3,37 @@
require 'spec_helper'
RSpec.describe EE::FeatureFlagsHelper do
include Devise::Test::ControllerHelpers
let_it_be(:project) { create(:project) }
let_it_be(:feature_flag) { create(:operations_feature_flag, project: project) }
let_it_be(:user) { create(:user) }
describe '#feature_flag_issues_links_endpoint' do
subject { helper.feature_flag_issues_links_endpoint(project, feature_flag, user) }
before do
allow(helper).to receive(:can?).with(user, :admin_feature_flags_issue_links, project).and_return(admin_feature_flags_issue_links?)
allow(helper).to receive(:current_user).and_return(user)
self.instance_variable_set(:@project, project)
self.instance_variable_set(:@feature_flag, feature_flag)
end
describe "#edit_feature_flags_data" do
subject { helper.edit_feature_flag_data }
it 'returns an empty string when the user is not allowed' do
allow(helper).to receive(:can?).with(user, :admin_feature_flags_issue_links, project).and_return(false)
context 'with permissions' do
let(:admin_feature_flags_issue_links?) { true }
is_expected.to be_empty
it 'adds the issue links path' do
is_expected.to include(feature_flag_issues_endpoint: "/#{project.full_path}/-/feature_flags/#{feature_flag.iid}/issues")
end
end
it 'returns the issue endpoint when the user is allowed' do
allow(helper).to receive(:can?).with(user, :admin_feature_flags_issue_links, project).and_return(true)
context 'without permissions' do
let(:admin_feature_flags_issue_links?) { false }
is_expected.to eq("/#{project.full_path}/-/feature_flags/#{feature_flag.iid}/issues")
it 'adds a blank issue links path' do
is_expected.to include(feature_flag_issues_endpoint: '')
end
end
end
end
......@@ -3,10 +3,20 @@
require 'spec_helper'
RSpec.describe FeatureFlagsHelper do
include Devise::Test::ControllerHelpers
let_it_be(:project) { create(:project) }
let_it_be(:feature_flag) { create(:operations_feature_flag, project: project) }
let_it_be(:user) { create(:user) }
before do
allow(helper).to receive(:can?).and_return(true)
allow(helper).to receive(:current_user).and_return(user)
self.instance_variable_set(:@project, project)
self.instance_variable_set(:@feature_flag, feature_flag)
end
describe '#unleash_api_url' do
subject { helper.unleash_api_url(project) }
......@@ -18,4 +28,17 @@ RSpec.describe FeatureFlagsHelper do
it { is_expected.not_to be_empty }
end
describe '#edit_feature_flag_data' do
subject { helper.edit_feature_flag_data }
it 'contains all the data needed to edit feature flags' do
is_expected.to include(endpoint: "/#{project.full_path}/-/feature_flags/#{feature_flag.iid}",
project_id: project.id,
feature_flags_path: "/#{project.full_path}/-/feature_flags",
environments_endpoint: "/#{project.full_path}/-/environments/search.json",
strategy_type_docs_page_path: "/help/operations/feature_flags#feature-flag-strategies",
environments_scope_docs_path: "/help/ci/environments/index.md#scope-environments-with-specs")
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