Commit 6303b598 authored by Jason Goodman's avatar Jason Goodman Committed by Shinya Maeda

Refactor Feature Flags Controller Put Request Specs

parent 2dbcb567
......@@ -903,6 +903,17 @@ RSpec.describe Projects::FeatureFlagsController do
end
describe 'PUT update.json' do
def put_request(feature_flag, feature_flag_params)
params = {
namespace_id: project.namespace,
project_id: project,
iid: feature_flag.iid,
operations_feature_flag: feature_flag_params
}
put(:update, params: params, format: :json, as: :json)
end
subject { put(:update, params: params, format: :json) }
let!(:feature_flag) do
......@@ -963,13 +974,7 @@ RSpec.describe Projects::FeatureFlagsController do
feature_flag = create(:operations_feature_flag, project: project, name: 'my_flag', active: false)
create(:operations_feature_flag_scope, feature_flag: feature_flag, environment_scope: 'production', active: true)
params = {
namespace_id: project.namespace,
project_id: project,
iid: feature_flag.iid,
operations_feature_flag: { active: true }
}
put(:update, params: params, format: :json)
put_request(feature_flag, { active: true })
expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('feature_flag', dir: 'ee')
......@@ -1122,27 +1127,13 @@ RSpec.describe Projects::FeatureFlagsController do
end
describe "updating the strategy" do
def request_params(scope, strategies)
{
namespace_id: project.namespace,
project_id: project,
iid: feature_flag.iid,
operations_feature_flag: {
scopes_attributes: [
{
id: scope.id,
strategies: strategies
}
]
}
}
end
it 'creates a default strategy' do
scope = create_scope(feature_flag, 'production', true, [])
params = request_params(scope, [{ name: 'default', parameters: {} }])
put(:update, params: params, format: :json, as: :json)
put_request(feature_flag, scopes_attributes: [{
id: scope.id,
strategies: [{ name: 'default', parameters: {} }]
}])
expect(response).to have_gitlab_http_status(:ok)
scope_json = json_response['scopes'].select do |s|
......@@ -1156,10 +1147,12 @@ RSpec.describe Projects::FeatureFlagsController do
it 'creates a gradualRolloutUserId strategy' do
scope = create_scope(feature_flag, 'production', true, [])
params = request_params(scope, [{ name: 'gradualRolloutUserId',
parameters: { groupId: 'default', percentage: "70" } }])
put(:update, params: params, format: :json)
put_request(feature_flag, scopes_attributes: [{
id: scope.id,
strategies: [{ name: 'gradualRolloutUserId',
parameters: { groupId: 'default', percentage: "70" } }]
}])
expect(response).to have_gitlab_http_status(:ok)
scope_json = json_response['scopes'].select do |s|
......@@ -1176,9 +1169,11 @@ RSpec.describe Projects::FeatureFlagsController do
it 'creates a userWithId strategy' do
scope = create_scope(feature_flag, 'production', true, [{ name: 'default', parameters: {} }])
params = request_params(scope, [{ name: 'userWithId', parameters: { userIds: 'sam,fred' } }])
put(:update, params: params, format: :json, as: :json)
put_request(feature_flag, scopes_attributes: [{
id: scope.id,
strategies: [{ name: 'userWithId', parameters: { userIds: 'sam,fred' } }]
}])
expect(response).to have_gitlab_http_status(:ok)
scope_json = json_response['scopes'].select do |s|
......@@ -1192,10 +1187,12 @@ RSpec.describe Projects::FeatureFlagsController do
it 'updates an existing strategy' do
scope = create_scope(feature_flag, 'production', true, [{ name: 'default', parameters: {} }])
params = request_params(scope, [{ name: 'gradualRolloutUserId',
parameters: { groupId: 'default', percentage: "50" } }])
put(:update, params: params, format: :json)
put_request(feature_flag, scopes_attributes: [{
id: scope.id,
strategies: [{ name: 'gradualRolloutUserId',
parameters: { groupId: 'default', percentage: "50" } }]
}])
expect(response).to have_gitlab_http_status(:ok)
scope_json = json_response['scopes'].select do |s|
......@@ -1212,9 +1209,11 @@ RSpec.describe Projects::FeatureFlagsController do
it 'clears an existing strategy' do
scope = create_scope(feature_flag, 'production', true, [{ name: 'default', parameters: {} }])
params = request_params(scope, [])
put(:update, params: params, format: :json, as: :json)
put_request(feature_flag, scopes_attributes: [{
id: scope.id,
strategies: []
}])
expect(response).to have_gitlab_http_status(:ok)
scope_json = json_response['scopes'].select do |s|
......@@ -1225,12 +1224,14 @@ RSpec.describe Projects::FeatureFlagsController do
it 'accepts multiple strategies' do
scope = create_scope(feature_flag, 'production', true, [{ name: 'default', parameters: {} }])
params = request_params(scope, [
put_request(feature_flag, scopes_attributes: [{
id: scope.id,
strategies: [
{ name: 'gradualRolloutUserId', parameters: { groupId: 'mygroup', percentage: '55' } },
{ name: 'userWithId', parameters: { userIds: 'joe' } }
])
put(:update, params: params, format: :json, as: :json)
]
}])
expect(response).to have_gitlab_http_status(:ok)
scope_json = json_response['scopes'].select do |s|
......@@ -1249,16 +1250,8 @@ RSpec.describe Projects::FeatureFlagsController do
it 'does not modify strategies when there is no strategies key in the params' do
scope = create_scope(feature_flag, 'production', true, [{ name: 'default', parameters: {} }])
params = {
namespace_id: project.namespace,
project_id: project,
iid: feature_flag.iid,
operations_feature_flag: {
scopes_attributes: [{ id: scope.id }]
}
}
put(:update, params: params, format: :json)
put_request(feature_flag, scopes_attributes: [{ id: scope.id }])
expect(response).to have_gitlab_http_status(:ok)
scope_json = json_response['scopes'].select do |s|
......@@ -1273,16 +1266,8 @@ RSpec.describe Projects::FeatureFlagsController do
it 'leaves an existing strategy when there are no strategies in the params' do
scope = create_scope(feature_flag, 'production', true, [{ name: 'gradualRolloutUserId',
parameters: { groupId: 'default', percentage: '10' } }])
params = {
namespace_id: project.namespace,
project_id: project,
iid: feature_flag.iid,
operations_feature_flag: {
scopes_attributes: [{ id: scope.id }]
}
}
put(:update, params: params, format: :json, as: :json)
put_request(feature_flag, scopes_attributes: [{ id: scope.id }])
expect(response).to have_gitlab_http_status(:ok)
scope_json = json_response['scopes'].select do |s|
......@@ -1296,9 +1281,11 @@ RSpec.describe Projects::FeatureFlagsController do
it 'does not accept extra parameters in the strategy params' do
scope = create_scope(feature_flag, 'production', true, [{ name: 'default', parameters: {} }])
params = request_params(scope, [{ name: 'userWithId', parameters: { userIds: 'joe', groupId: 'default' } }])
put(:update, params: params, format: :json, as: :json)
put_request(feature_flag, scopes_attributes: [{
id: scope.id,
strategies: [{ name: 'userWithId', parameters: { userIds: 'joe', groupId: 'default' } }]
}])
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to eq(["Scopes strategies parameters are invalid"])
......@@ -1315,22 +1302,13 @@ RSpec.describe Projects::FeatureFlagsController do
end
it 'creates a new strategy and scope' do
params = {
namespace_id: project.namespace,
project_id: project,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
put_request(new_version_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)
......@@ -1345,19 +1323,10 @@ RSpec.describe Projects::FeatureFlagsController do
end
it 'creates a gradualRolloutUserId strategy' do
params = {
namespace_id: project.namespace,
project_id: project,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
put_request(new_version_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)
......@@ -1372,20 +1341,12 @@ RSpec.describe Projects::FeatureFlagsController do
it 'creates a gitlabUserList strategy' do
user_list = create(:operations_feature_flag_user_list, project: project, name: 'My List', user_xids: 'user1,user2')
params = {
namespace_id: project.namespace,
project_id: project,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
put_request(new_version_flag, strategies_attributes: [{
name: 'gitlabUserList',
parameters: {},
user_list_id: user_list.id
}]
}
}
put(:update, params: params, format: :json)
}])
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies']).to match([a_hash_including({
......@@ -1406,19 +1367,11 @@ RSpec.describe Projects::FeatureFlagsController do
user_list = create(:operations_feature_flag_user_list, project: project, name: 'My List', user_xids: 'user1,user2')
strategy = create(:operations_strategy, feature_flag: new_version_flag, name: 'gitlabUserList', parameters: {}, user_list: user_list)
other_user_list = create(:operations_feature_flag_user_list, project: project, name: 'Other List', user_xids: 'user3')
params = {
namespace_id: project.namespace,
project_id: project,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
put_request(new_version_flag, strategies_attributes: [{
id: strategy.id,
user_list_id: other_user_list.id
}]
}
}
put(:update, params: params, format: :json)
}])
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies']).to eq([{
......@@ -1438,23 +1391,15 @@ RSpec.describe Projects::FeatureFlagsController do
it 'automatically dissociates the user list when switching the type of an existing gitlabUserList strategy' do
user_list = create(:operations_feature_flag_user_list, project: project, name: 'My List', user_xids: 'user1,user2')
strategy = create(:operations_strategy, feature_flag: new_version_flag, name: 'gitlabUserList', parameters: {}, user_list: user_list)
params = {
namespace_id: project.namespace,
project_id: project,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
put_request(new_version_flag, strategies_attributes: [{
id: strategy.id,
name: 'gradualRolloutUserId',
parameters: {
groupId: 'default',
percentage: '25'
}
}]
}
}
put(:update, params: params, format: :json)
}])
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['strategies']).to eq([{
......@@ -1471,19 +1416,11 @@ RSpec.describe Projects::FeatureFlagsController do
it 'does not delete a user list when deleting a gitlabUserList strategy' do
user_list = create(:operations_feature_flag_user_list, project: project, name: 'My List', user_xids: 'user1,user2')
strategy = create(:operations_strategy, feature_flag: new_version_flag, name: 'gitlabUserList', parameters: {}, user_list: user_list)
params = {
namespace_id: project.namespace,
project_id: project,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
put_request(new_version_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([])
......@@ -1493,40 +1430,23 @@ RSpec.describe Projects::FeatureFlagsController do
end
it 'returns not found when trying to create a gitlabUserList strategy with an invalid user list id' do
params = {
namespace_id: project.namespace,
project_id: project,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
put_request(new_version_flag, strategies_attributes: [{
name: 'gitlabUserList',
parameters: {},
user_list_id: 1
}]
}
}
put(:update, params: params, format: :json)
}])
expect(response).to have_gitlab_http_status(:not_found)
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,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
put_request(new_version_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([{
......@@ -1540,22 +1460,14 @@ RSpec.describe Projects::FeatureFlagsController do
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,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
put_request(new_version_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([{
......@@ -1566,19 +1478,11 @@ RSpec.describe Projects::FeatureFlagsController do
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,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
put_request(new_version_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([])
......@@ -1587,22 +1491,14 @@ RSpec.describe Projects::FeatureFlagsController do
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,
iid: new_version_flag.iid,
operations_feature_flag: {
strategies_attributes: [{
put_request(new_version_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([])
......@@ -1610,16 +1506,8 @@ RSpec.describe Projects::FeatureFlagsController do
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,
iid: new_version_flag.iid,
operations_feature_flag: {
name: 'some-other-name'
}
}
put(:update, params: params, format: :json)
put_request(new_version_flag, { name: 'some-other-name' })
expect(response).to have_gitlab_http_status(:not_found)
expect(new_version_flag.reload.name).to eq('new-feature')
......
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