2019-10-28 20:06:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe API::Features, stub_feature_flags: false do
|
2020-02-17 10:09:01 -05:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:admin) { create(:admin) }
|
2017-05-31 17:06:01 -04:00
|
|
|
|
2020-11-18 22:08:59 -05:00
|
|
|
# Find any `development` feature flag name
|
|
|
|
let(:known_feature_flag) do
|
|
|
|
Feature::Definition.definitions
|
|
|
|
.values.find(&:development?)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:known_feature_flag_definition_hash) do
|
|
|
|
a_hash_including(
|
|
|
|
'type' => 'development'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
before do
|
2020-05-29 14:08:26 -04:00
|
|
|
Feature.reset
|
2017-06-21 10:49:51 -04:00
|
|
|
Flipper.unregister_groups
|
|
|
|
Flipper.register(:perf_team) do |actor|
|
|
|
|
actor.respond_to?(:admin) && actor.admin?
|
|
|
|
end
|
2020-09-21 08:09:34 -04:00
|
|
|
|
|
|
|
skip_feature_flags_yaml_validation
|
2022-05-04 17:08:35 -04:00
|
|
|
skip_default_enabled_yaml_check
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
describe 'GET /features' do
|
|
|
|
let(:expected_features) do
|
|
|
|
[
|
|
|
|
{
|
|
|
|
'name' => 'feature_1',
|
|
|
|
'state' => 'on',
|
2020-11-18 22:08:59 -05:00
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => true }],
|
|
|
|
'definition' => nil
|
2017-05-31 17:06:01 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'name' => 'feature_2',
|
|
|
|
'state' => 'off',
|
2020-11-18 22:08:59 -05:00
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => false }],
|
|
|
|
'definition' => nil
|
2017-06-21 10:49:51 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'name' => 'feature_3',
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'groups', 'value' => ['perf_team'] }
|
2020-11-18 22:08:59 -05:00
|
|
|
],
|
|
|
|
'definition' => nil
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name' => known_feature_flag.name,
|
|
|
|
'state' => 'on',
|
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => true }],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
2017-05-31 17:06:01 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2020-06-02 08:08:33 -04:00
|
|
|
Feature.enable('feature_1')
|
|
|
|
Feature.disable('feature_2')
|
|
|
|
Feature.enable('feature_3', Feature.group(:perf_team))
|
2020-11-18 22:08:59 -05:00
|
|
|
Feature.enable(known_feature_flag.name)
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 401 for anonymous users' do
|
|
|
|
get api('/features')
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 403 for users' do
|
|
|
|
get api('/features', user)
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the feature list for admins' do
|
|
|
|
get api('/features', admin)
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2017-05-31 17:06:01 -04:00
|
|
|
expect(json_response).to match_array(expected_features)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /feature' do
|
2020-11-18 22:08:59 -05:00
|
|
|
let(:feature_name) { known_feature_flag.name }
|
2017-05-31 17:06:01 -04:00
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
context 'when the feature does not exist' do
|
|
|
|
it 'returns a 401 for anonymous users' do
|
|
|
|
post api("/features/#{feature_name}")
|
2017-05-31 17:06:01 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
it 'returns a 403 for users' do
|
|
|
|
post api("/features/#{feature_name}", user)
|
2017-05-31 17:06:01 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
context 'when passed value=true' do
|
|
|
|
it 'creates an enabled feature' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true' }
|
2017-05-31 17:06:01 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2017-06-21 10:49:51 -04:00
|
|
|
'state' => 'on',
|
2020-11-18 22:08:59 -05:00
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => true }],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
|
2020-12-16 10:10:18 -05:00
|
|
|
it 'logs the event' do
|
|
|
|
expect(Feature.logger).to receive(:info).once
|
|
|
|
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true' }
|
|
|
|
end
|
|
|
|
|
2017-06-28 13:29:56 -04:00
|
|
|
it 'creates an enabled feature for the given Flipper group when passed feature_group=perf_team' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true', feature_group: 'perf_team' }
|
2017-06-21 10:49:51 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2017-06-21 10:49:51 -04:00
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'groups', 'value' => ['perf_team'] }
|
2020-11-18 22:08:59 -05:00
|
|
|
],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates an enabled feature for the given user when passed user=username' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true', user: user.username }
|
2017-05-31 17:06:01 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2017-06-21 10:49:51 -04:00
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'actors', 'value' => ["User:#{user.id}"] }
|
2020-11-18 22:08:59 -05:00
|
|
|
],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
2017-07-05 15:55:27 -04:00
|
|
|
|
|
|
|
it 'creates an enabled feature for the given user and feature group when passed user=username and feature_group=perf_team' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true', user: user.username, feature_group: 'perf_team' }
|
2017-07-05 15:55:27 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response['name']).to eq(feature_name)
|
2019-10-22 14:06:35 -04:00
|
|
|
expect(json_response['state']).to eq('conditional')
|
|
|
|
expect(json_response['gates']).to contain_exactly(
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'groups', 'value' => ['perf_team'] },
|
|
|
|
{ 'key' => 'actors', 'value' => ["User:#{user.id}"] }
|
|
|
|
)
|
2017-07-05 15:55:27 -04:00
|
|
|
end
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
|
2022-05-19 05:09:08 -04:00
|
|
|
shared_examples 'does not enable the flag' do |actor_type|
|
|
|
|
let(:actor_path) { raise NotImplementedError }
|
|
|
|
let(:expected_inexistent_path) { actor_path }
|
|
|
|
|
2022-02-17 07:12:30 -05:00
|
|
|
it 'returns the current state of the flag without changes' do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true', actor_type => actor_path }
|
|
|
|
|
2022-05-19 05:09:08 -04:00
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(json_response['message']).to eq("400 Bad request - #{expected_inexistent_path} is not found!")
|
2022-02-17 07:12:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'enables the flag for the actor' do |actor_type|
|
|
|
|
it 'sets the feature gate' do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true', actor_type => actor.full_path }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'actors', 'value' => ["#{actor.class}:#{actor.id}"] }
|
|
|
|
],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-19 05:09:08 -04:00
|
|
|
shared_examples 'creates an enabled feature for the specified entries' do
|
|
|
|
it do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true', **gate_params }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
|
|
|
expect(json_response['name']).to eq(feature_name)
|
|
|
|
expect(json_response['gates']).to contain_exactly(
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'actors', 'value' => array_including(expected_gate_params) }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-07 05:07:14 -05:00
|
|
|
context 'when enabling for a project by path' do
|
|
|
|
context 'when the project exists' do
|
2022-02-17 07:12:30 -05:00
|
|
|
it_behaves_like 'enables the flag for the actor', :project do
|
|
|
|
let(:actor) { create(:project) }
|
2019-01-07 05:07:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the project does not exist' do
|
2022-05-19 05:09:08 -04:00
|
|
|
it_behaves_like 'does not enable the flag', :project do
|
|
|
|
let(:actor_path) { 'mep/to/the/mep/mep' }
|
|
|
|
end
|
2019-01-07 05:07:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-07 15:27:03 -05:00
|
|
|
context 'when enabling for a group by path' do
|
|
|
|
context 'when the group exists' do
|
2022-02-17 07:12:30 -05:00
|
|
|
it_behaves_like 'enables the flag for the actor', :group do
|
|
|
|
let(:actor) { create(:group) }
|
2019-02-07 15:27:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the group does not exist' do
|
2022-05-19 05:09:08 -04:00
|
|
|
it_behaves_like 'does not enable the flag', :group do
|
|
|
|
let(:actor_path) { 'not/a/group' }
|
|
|
|
end
|
2022-02-17 07:12:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when enabling for a namespace by path' do
|
|
|
|
context 'when the user namespace exists' do
|
|
|
|
it_behaves_like 'enables the flag for the actor', :namespace do
|
|
|
|
let(:actor) { create(:namespace) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the group namespace exists' do
|
|
|
|
it_behaves_like 'enables the flag for the actor', :namespace do
|
|
|
|
let(:actor) { create(:group) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user namespace does not exist' do
|
2022-05-19 05:09:08 -04:00
|
|
|
it_behaves_like 'does not enable the flag', :namespace do
|
|
|
|
let(:actor_path) { 'not/a/group' }
|
|
|
|
end
|
2022-02-17 07:12:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a project namespace exists' do
|
|
|
|
let(:project_namespace) { create(:project_namespace) }
|
|
|
|
|
|
|
|
it_behaves_like 'does not enable the flag', :namespace do
|
|
|
|
let(:actor_path) { project_namespace.full_path }
|
2019-02-07 15:27:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-19 05:09:08 -04:00
|
|
|
context 'with multiple users' do
|
|
|
|
let_it_be(:users) { create_list(:user, 3) }
|
|
|
|
|
|
|
|
it_behaves_like 'creates an enabled feature for the specified entries' do
|
|
|
|
let(:gate_params) { { user: users.map(&:username).join(',') } }
|
|
|
|
let(:expected_gate_params) { users.map(&:flipper_id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when empty value exists between comma' do
|
|
|
|
it_behaves_like 'creates an enabled feature for the specified entries' do
|
|
|
|
let(:gate_params) { { user: "#{users.first.username},,,," } }
|
|
|
|
let(:expected_gate_params) { users.first.flipper_id }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when one of the users does not exist' do
|
|
|
|
it_behaves_like 'does not enable the flag', :user do
|
|
|
|
let(:actor_path) { "#{users.first.username},inexistent-entry" }
|
|
|
|
let(:expected_inexistent_path) { "inexistent-entry" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple projects' do
|
|
|
|
let_it_be(:projects) { create_list(:project, 3) }
|
|
|
|
|
|
|
|
it_behaves_like 'creates an enabled feature for the specified entries' do
|
|
|
|
let(:gate_params) { { project: projects.map(&:full_path).join(',') } }
|
|
|
|
let(:expected_gate_params) { projects.map(&:flipper_id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when empty value exists between comma' do
|
|
|
|
it_behaves_like 'creates an enabled feature for the specified entries' do
|
|
|
|
let(:gate_params) { { project: "#{projects.first.full_path},,,," } }
|
|
|
|
let(:expected_gate_params) { projects.first.flipper_id }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when one of the projects does not exist' do
|
|
|
|
it_behaves_like 'does not enable the flag', :project do
|
|
|
|
let(:actor_path) { "#{projects.first.full_path},inexistent-entry" }
|
|
|
|
let(:expected_inexistent_path) { "inexistent-entry" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple groups' do
|
|
|
|
let_it_be(:groups) { create_list(:group, 3) }
|
|
|
|
|
|
|
|
it_behaves_like 'creates an enabled feature for the specified entries' do
|
|
|
|
let(:gate_params) { { group: groups.map(&:full_path).join(',') } }
|
|
|
|
let(:expected_gate_params) { groups.map(&:flipper_id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when empty value exists between comma' do
|
|
|
|
it_behaves_like 'creates an enabled feature for the specified entries' do
|
|
|
|
let(:gate_params) { { group: "#{groups.first.full_path},,,," } }
|
|
|
|
let(:expected_gate_params) { groups.first.flipper_id }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when one of the groups does not exist' do
|
|
|
|
it_behaves_like 'does not enable the flag', :group do
|
|
|
|
let(:actor_path) { "#{groups.first.full_path},inexistent-entry" }
|
|
|
|
let(:expected_inexistent_path) { "inexistent-entry" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with multiple namespaces' do
|
|
|
|
let_it_be(:namespaces) { create_list(:namespace, 3) }
|
|
|
|
|
|
|
|
it_behaves_like 'creates an enabled feature for the specified entries' do
|
|
|
|
let(:gate_params) { { namespace: namespaces.map(&:full_path).join(',') } }
|
|
|
|
let(:expected_gate_params) { namespaces.map(&:flipper_id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when empty value exists between comma' do
|
|
|
|
it_behaves_like 'creates an enabled feature for the specified entries' do
|
|
|
|
let(:gate_params) { { namespace: "#{namespaces.first.full_path},,,," } }
|
|
|
|
let(:expected_gate_params) { namespaces.first.flipper_id }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when one of the namespaces does not exist' do
|
|
|
|
it_behaves_like 'does not enable the flag', :namespace do
|
|
|
|
let(:actor_path) { "#{namespaces.first.full_path},inexistent-entry" }
|
|
|
|
let(:expected_inexistent_path) { "inexistent-entry" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-04 14:10:20 -04:00
|
|
|
it 'creates a feature with the given percentage of time if passed an integer' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/features/#{feature_name}", admin), params: { value: '50' }
|
2017-06-21 10:49:51 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2017-06-21 10:49:51 -04:00
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'percentage_of_time', 'value' => 50 }
|
2020-11-18 22:08:59 -05:00
|
|
|
],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
2020-05-04 14:10:20 -04:00
|
|
|
|
2021-11-08 22:42:22 -05:00
|
|
|
it 'creates a feature with the given percentage of time if passed a float' do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: '0.01' }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'percentage_of_time', 'value' => 0.01 }
|
|
|
|
],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-05-04 14:10:20 -04:00
|
|
|
it 'creates a feature with the given percentage of actors if passed an integer' do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: '50', key: 'percentage_of_actors' }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2020-05-04 14:10:20 -04:00
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'percentage_of_actors', 'value' => 50 }
|
2020-11-18 22:08:59 -05:00
|
|
|
],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2020-05-04 14:10:20 -04:00
|
|
|
end
|
2021-11-08 22:42:22 -05:00
|
|
|
|
|
|
|
it 'creates a feature with the given percentage of actors if passed a float' do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: '0.01', key: 'percentage_of_actors' }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'percentage_of_actors', 'value' => 0.01 }
|
|
|
|
],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
|
|
|
end
|
2022-05-09 14:08:30 -04:00
|
|
|
|
|
|
|
describe 'mutually exclusive parameters' do
|
|
|
|
shared_examples 'fails to set the feature flag' do
|
|
|
|
it 'returns an error' do
|
|
|
|
expect(response).to have_gitlab_http_status(:bad_request)
|
|
|
|
expect(json_response['error']).to match(/key, \w+ are mutually exclusive/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when key and feature_group are provided' do
|
|
|
|
before do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: '0.01', key: 'percentage_of_actors', feature_group: 'some-value' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'fails to set the feature flag'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when key and user are provided' do
|
|
|
|
before do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: '0.01', key: 'percentage_of_actors', user: 'some-user' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'fails to set the feature flag'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when key and group are provided' do
|
|
|
|
before do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: '0.01', key: 'percentage_of_actors', group: 'somepath' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'fails to set the feature flag'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when key and namespace are provided' do
|
|
|
|
before do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: '0.01', key: 'percentage_of_actors', namespace: 'somepath' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'fails to set the feature flag'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when key and project are provided' do
|
|
|
|
before do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: '0.01', key: 'percentage_of_actors', project: 'somepath' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'fails to set the feature flag'
|
|
|
|
end
|
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the feature exists' do
|
|
|
|
before do
|
2020-06-02 08:08:33 -04:00
|
|
|
Feature.disable(feature_name) # This also persists the feature on the DB
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
context 'when passed value=true' do
|
|
|
|
it 'enables the feature' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true' }
|
2017-05-31 17:06:01 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2017-06-21 10:49:51 -04:00
|
|
|
'state' => 'on',
|
2020-11-18 22:08:59 -05:00
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => true }],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
|
2017-06-28 13:29:56 -04:00
|
|
|
it 'enables the feature for the given Flipper group when passed feature_group=perf_team' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true', feature_group: 'perf_team' }
|
2017-06-21 10:49:51 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2017-06-21 10:49:51 -04:00
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'groups', 'value' => ['perf_team'] }
|
2020-11-18 22:08:59 -05:00
|
|
|
],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'enables the feature for the given user when passed user=username' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true', user: user.username }
|
2017-06-21 10:49:51 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2017-06-21 10:49:51 -04:00
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'actors', 'value' => ["User:#{user.id}"] }
|
2020-11-18 22:08:59 -05:00
|
|
|
],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when feature is enabled and value=false is passed' do
|
|
|
|
it 'disables the feature' do
|
2020-06-02 08:08:33 -04:00
|
|
|
Feature.enable(feature_name)
|
|
|
|
expect(Feature.enabled?(feature_name)).to eq(true)
|
2017-06-21 10:49:51 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'false' }
|
2017-06-21 10:49:51 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2017-06-21 10:49:51 -04:00
|
|
|
'state' => 'off',
|
2020-11-18 22:08:59 -05:00
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => false }],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
|
2017-06-28 13:29:56 -04:00
|
|
|
it 'disables the feature for the given Flipper group when passed feature_group=perf_team' do
|
2020-06-02 08:08:33 -04:00
|
|
|
Feature.enable(feature_name, Feature.group(:perf_team))
|
|
|
|
expect(Feature.enabled?(feature_name, admin)).to be_truthy
|
2017-06-21 10:49:51 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'false', feature_group: 'perf_team' }
|
2017-06-21 10:49:51 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2017-06-21 10:49:51 -04:00
|
|
|
'state' => 'off',
|
2020-11-18 22:08:59 -05:00
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => false }],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'disables the feature for the given user when passed user=username' do
|
2020-06-02 08:08:33 -04:00
|
|
|
Feature.enable(feature_name, user)
|
|
|
|
expect(Feature.enabled?(feature_name, user)).to be_truthy
|
2017-06-21 10:49:51 -04:00
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'false', user: user.username }
|
2017-06-21 10:49:51 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2017-06-21 10:49:51 -04:00
|
|
|
'state' => 'off',
|
2020-11-18 22:08:59 -05:00
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => false }],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
2020-05-04 14:10:20 -04:00
|
|
|
context 'with a pre-existing percentage of time value' do
|
2017-05-31 17:06:01 -04:00
|
|
|
before do
|
2020-06-02 08:08:33 -04:00
|
|
|
Feature.enable_percentage_of_time(feature_name, 50)
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the percentage of time if passed an integer' do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/features/#{feature_name}", admin), params: { value: '30' }
|
2017-05-31 17:06:01 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2017-06-21 10:49:51 -04:00
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'percentage_of_time', 'value' => 30 }
|
2020-11-18 22:08:59 -05:00
|
|
|
],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
end
|
2020-05-04 14:10:20 -04:00
|
|
|
|
|
|
|
context 'with a pre-existing percentage of actors value' do
|
|
|
|
before do
|
2020-06-02 08:08:33 -04:00
|
|
|
Feature.enable_percentage_of_actors(feature_name, 42)
|
2020-05-04 14:10:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the percentage of actors if passed an integer' do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: '74', key: 'percentage_of_actors' }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:created)
|
2020-11-18 22:08:59 -05:00
|
|
|
expect(json_response).to match(
|
|
|
|
'name' => feature_name,
|
2020-05-04 14:10:20 -04:00
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'percentage_of_actors', 'value' => 74 }
|
2020-11-18 22:08:59 -05:00
|
|
|
],
|
|
|
|
'definition' => known_feature_flag_definition_hash
|
|
|
|
)
|
2020-05-04 14:10:20 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
end
|
2018-04-03 07:35:26 -04:00
|
|
|
|
|
|
|
describe 'DELETE /feature/:name' do
|
|
|
|
let(:feature_name) { 'my_feature' }
|
|
|
|
|
|
|
|
context 'when the user has no access' do
|
|
|
|
it 'returns a 401 for anonymous users' do
|
|
|
|
delete api("/features/#{feature_name}")
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:unauthorized)
|
2018-04-03 07:35:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 403 for users' do
|
|
|
|
delete api("/features/#{feature_name}", user)
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:forbidden)
|
2018-04-03 07:35:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user has access' do
|
|
|
|
it 'returns 204 when the value is not set' do
|
|
|
|
delete api("/features/#{feature_name}", admin)
|
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2018-04-03 07:35:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the gate value was set' do
|
|
|
|
before do
|
2020-06-02 08:08:33 -04:00
|
|
|
Feature.enable(feature_name)
|
2018-04-03 07:35:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes an enabled feature' do
|
2020-06-02 08:08:33 -04:00
|
|
|
expect do
|
|
|
|
delete api("/features/#{feature_name}", admin)
|
|
|
|
Feature.reset
|
|
|
|
end.to change { Feature.persisted_name?(feature_name) }
|
|
|
|
.and change { Feature.enabled?(feature_name) }
|
2018-04-03 07:35:26 -04:00
|
|
|
|
2020-02-21 07:09:07 -05:00
|
|
|
expect(response).to have_gitlab_http_status(:no_content)
|
2018-04-03 07:35:26 -04:00
|
|
|
end
|
2020-12-16 10:10:18 -05:00
|
|
|
|
|
|
|
it 'logs the event' do
|
|
|
|
expect(Feature.logger).to receive(:info).once
|
|
|
|
|
|
|
|
delete api("/features/#{feature_name}", admin)
|
|
|
|
end
|
2018-04-03 07:35:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|