Commit 16a49237 authored by Jason Goodman's avatar Jason Goodman Committed by Shinya Maeda

Support new version feature flags PUT and DELETE requests in controller

Hide behind a feature flag
parent f1dd55e8
......@@ -117,7 +117,9 @@ class Projects::FeatureFlagsController < Projects::ApplicationController
params.require(:operations_feature_flag)
.permit(:name, :description, :active,
scopes_attributes: [:id, :environment_scope, :active, :_destroy,
strategies: [:name, parameters: [:groupId, :percentage, :userIds]]])
strategies: [:name, parameters: [:groupId, :percentage, :userIds]]],
strategies_attributes: [:id, :name, :_destroy, parameters: [:groupId, :percentage, :userIds],
scopes_attributes: [:id, :environment_scope, :_destroy]])
end
def feature_flag_json(feature_flag)
......
......@@ -757,6 +757,20 @@ describe Projects::FeatureFlagsController do
end
end
context 'when the feature flag does not exist' do
let(:params) do
{
namespace_id: project.namespace,
project_id: project,
id: 0
}
end
it 'returns not found' do
is_expected.to have_gitlab_http_status(:not_found)
end
end
context 'when there is an additional scope' do
let!(:scope) { create_scope(feature_flag, 'production', false) }
......@@ -764,6 +778,30 @@ describe Projects::FeatureFlagsController do
expect { subject }.to change { Operations::FeatureFlagScope.count }.by(-2)
end
end
context 'with a version 2 flag' do
let!(:new_version_flag) { create(:operations_feature_flag, :new_version_flag, project: project) }
let(:params) do
{
namespace_id: project.namespace,
project_id: project,
id: new_version_flag.id
}
end
it 'deletes the flag' do
expect { subject }.to change { Operations::FeatureFlag.count }.by(-1)
end
context 'when new version flags are disabled' do
it 'returns a 404' do
stub_feature_flags(feature_flags_new_version: false)
expect { subject }.not_to change { Operations::FeatureFlag.count }
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
end
describe 'PUT update.json' do
......@@ -1168,6 +1206,186 @@ describe Projects::FeatureFlagsController do
expect(json_response['message']).to eq(["Scopes strategies parameters are invalid"])
end
end
context 'with a version 2 feature flag' do
let!(:new_version_flag) do
create(:operations_feature_flag,
:new_version_flag,
name: 'new-feature',
active: true,
project: project)
end
it 'creates a new strategy and scope' do
params = {
namespace_id: project.namespace,
project_id: project,
id: new_version_flag.id,
operations_feature_flag: {
strategies_attributes: [{
name: 'userWithId',
parameters: { userIds: 'user1' },
scopes_attributes: [{
environment_scope: 'production'
}]
}]
}
}
put(:update, params: params, format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies'].count).to eq(1)
strategy_json = json_response['strategies'].first
expect(strategy_json['name']).to eq('userWithId')
expect(strategy_json['parameters']).to eq({
'userIds' => 'user1'
})
expect(strategy_json['scopes'].count).to eq(1)
scope_json = strategy_json['scopes'].first
expect(scope_json['environment_scope']).to eq('production')
end
it 'creates a gradualRolloutUserId strategy' do
params = {
namespace_id: project.namespace,
project_id: project,
id: new_version_flag.id,
operations_feature_flag: {
strategies_attributes: [{
name: 'gradualRolloutUserId',
parameters: { groupId: 'default', percentage: '30' }
}]
}
}
put(:update, params: params, format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies'].count).to eq(1)
strategy_json = json_response['strategies'].first
expect(strategy_json['name']).to eq('gradualRolloutUserId')
expect(strategy_json['parameters']).to eq({
'groupId' => 'default',
'percentage' => '30'
})
expect(strategy_json['scopes']).to eq([])
end
it 'updates an existing strategy' do
strategy = create(:operations_strategy, feature_flag: new_version_flag, name: 'default', parameters: {})
params = {
namespace_id: project.namespace,
project_id: project,
id: new_version_flag.id,
operations_feature_flag: {
strategies_attributes: [{
id: strategy.id,
name: 'userWithId',
parameters: { userIds: 'user2,user3' }
}]
}
}
put(:update, params: params, format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies']).to eq([{
'id' => strategy.id,
'name' => 'userWithId',
'parameters' => { 'userIds' => 'user2,user3' },
'scopes' => []
}])
end
it 'updates an existing scope' do
strategy = create(:operations_strategy, feature_flag: new_version_flag, name: 'default', parameters: {})
scope = create(:operations_scope, strategy: strategy, environment_scope: 'staging')
params = {
namespace_id: project.namespace,
project_id: project,
id: new_version_flag.id,
operations_feature_flag: {
strategies_attributes: [{
id: strategy.id,
scopes_attributes: [{
id: scope.id,
environment_scope: 'sandbox'
}]
}]
}
}
put(:update, params: params, format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies'].first['scopes']).to eq([{
'id' => scope.id,
'environment_scope' => 'sandbox'
}])
end
it 'deletes an existing strategy' do
strategy = create(:operations_strategy, feature_flag: new_version_flag, name: 'default', parameters: {})
params = {
namespace_id: project.namespace,
project_id: project,
id: new_version_flag.id,
operations_feature_flag: {
strategies_attributes: [{
id: strategy.id,
_destroy: true
}]
}
}
put(:update, params: params, format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies']).to eq([])
end
it 'deletes an existing scope' do
strategy = create(:operations_strategy, feature_flag: new_version_flag, name: 'default', parameters: {})
scope = create(:operations_scope, strategy: strategy, environment_scope: 'staging')
params = {
namespace_id: project.namespace,
project_id: project,
id: new_version_flag.id,
operations_feature_flag: {
strategies_attributes: [{
id: strategy.id,
scopes_attributes: [{
id: scope.id,
_destroy: true
}]
}]
}
}
put(:update, params: params, format: :json)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies'].first['scopes']).to eq([])
end
it 'does not update the flag if version 2 flags are disabled' do
stub_feature_flags(feature_flags_new_version: false)
params = {
namespace_id: project.namespace,
project_id: project,
id: new_version_flag.id,
operations_feature_flag: {
name: 'some-other-name'
}
}
put(:update, params: params, format: :json)
expect(response).to have_gitlab_http_status(:not_found)
expect(new_version_flag.reload.name).to eq('new-feature')
end
end
end
private
......
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