2017-05-31 17:06:01 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe API::Features do
|
2018-04-03 07:35:26 -04:00
|
|
|
set(:user) { create(:user) }
|
|
|
|
set(:admin) { create(:admin) }
|
2017-05-31 17:06:01 -04:00
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
before do
|
|
|
|
Flipper.unregister_groups
|
|
|
|
Flipper.register(:perf_team) do |actor|
|
|
|
|
actor.respond_to?(:admin) && actor.admin?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-31 17:06:01 -04:00
|
|
|
describe 'GET /features' do
|
|
|
|
let(:expected_features) do
|
|
|
|
[
|
|
|
|
{
|
|
|
|
'name' => 'feature_1',
|
|
|
|
'state' => 'on',
|
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => true }]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name' => 'feature_2',
|
|
|
|
'state' => 'off',
|
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => false }]
|
2017-06-21 10:49:51 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'name' => 'feature_3',
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'groups', 'value' => ['perf_team'] }
|
|
|
|
]
|
2017-05-31 17:06:01 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
Feature.get('feature_1').enable
|
|
|
|
Feature.get('feature_2').disable
|
2017-06-21 10:49:51 -04:00
|
|
|
Feature.get('feature_3').enable Feature.group(:perf_team)
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 401 for anonymous users' do
|
|
|
|
get api('/features')
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 403 for users' do
|
|
|
|
get api('/features', user)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(403)
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the feature list for admins' do
|
|
|
|
get api('/features', admin)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-05-31 17:06:01 -04:00
|
|
|
expect(json_response).to match_array(expected_features)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /feature' do
|
|
|
|
let(:feature_name) { 'my_feature' }
|
|
|
|
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(403)
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-06-21 10:49:51 -04:00
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'on',
|
|
|
|
'gates' => [{ 'key' => 'boolean', '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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-06-21 10:49:51 -04:00
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'groups', 'value' => ['perf_team'] }
|
|
|
|
])
|
|
|
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-06-21 10:49:51 -04:00
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'actors', 'value' => ["User:#{user.id}"] }
|
|
|
|
])
|
|
|
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-07-05 15:55:27 -04:00
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'groups', 'value' => ['perf_team'] },
|
|
|
|
{ 'key' => 'actors', 'value' => ["User:#{user.id}"] }
|
|
|
|
])
|
|
|
|
end
|
2017-06-21 10:49:51 -04:00
|
|
|
end
|
|
|
|
|
2019-01-07 05:07:14 -05:00
|
|
|
context 'when enabling for a project by path' do
|
|
|
|
context 'when the project exists' do
|
|
|
|
let!(:project) { create(:project) }
|
|
|
|
|
|
|
|
it 'sets the feature gate' do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true', project: project.full_path }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(201)
|
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'actors', 'value' => ["Project:#{project.id}"] }
|
|
|
|
])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the project does not exist' do
|
|
|
|
it 'sets no new values' do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true', project: 'mep/to/the/mep/mep' }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(201)
|
|
|
|
expect(json_response).to eq(
|
|
|
|
"name" => "my_feature",
|
|
|
|
"state" => "off",
|
|
|
|
"gates" => [
|
|
|
|
{ "key" => "boolean", "value" => false }
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
it 'sets the feature gate' do
|
|
|
|
group = create(:group)
|
|
|
|
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true', group: group.full_path }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(201)
|
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'actors', 'value' => ["Group:#{group.id}"] }
|
|
|
|
])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the group does not exist' do
|
|
|
|
it 'sets no new values and keeps the feature disabled' do
|
|
|
|
post api("/features/#{feature_name}", admin), params: { value: 'true', group: 'not/a/group' }
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(201)
|
|
|
|
expect(json_response).to eq(
|
|
|
|
"name" => "my_feature",
|
|
|
|
"state" => "off",
|
|
|
|
"gates" => [
|
|
|
|
{ "key" => "boolean", "value" => false }
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-21 10:49:51 -04:00
|
|
|
it 'creates a feature with the given percentage 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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-06-21 10:49:51 -04:00
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'percentage_of_time', 'value' => 50 }
|
|
|
|
])
|
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the feature exists' do
|
|
|
|
let(:feature) { Feature.get(feature_name) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
feature.disable # This also persists the feature on the DB
|
|
|
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-06-21 10:49:51 -04:00
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'on',
|
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => true }])
|
|
|
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-06-21 10:49:51 -04:00
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'groups', 'value' => ['perf_team'] }
|
|
|
|
])
|
|
|
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-06-21 10:49:51 -04:00
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'actors', 'value' => ["User:#{user.id}"] }
|
|
|
|
])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when feature is enabled and value=false is passed' do
|
|
|
|
it 'disables the feature' do
|
|
|
|
feature.enable
|
|
|
|
expect(feature).to be_enabled
|
|
|
|
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-06-21 10:49:51 -04:00
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'off',
|
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => false }])
|
|
|
|
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
|
2017-06-21 10:49:51 -04:00
|
|
|
feature.enable(Feature.group(:perf_team))
|
|
|
|
expect(Feature.get(feature_name).enabled?(admin)).to be_truthy
|
|
|
|
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-06-21 10:49:51 -04:00
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'off',
|
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => false }])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'disables the feature for the given user when passed user=username' do
|
|
|
|
feature.enable(user)
|
|
|
|
expect(Feature.get(feature_name).enabled?(user)).to be_truthy
|
|
|
|
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-06-21 10:49:51 -04:00
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'off',
|
|
|
|
'gates' => [{ 'key' => 'boolean', 'value' => false }])
|
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a pre-existing percentage value' do
|
|
|
|
before do
|
|
|
|
feature.enable_percentage_of_time(50)
|
|
|
|
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
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2017-06-21 10:49:51 -04:00
|
|
|
expect(json_response).to eq(
|
|
|
|
'name' => 'my_feature',
|
|
|
|
'state' => 'conditional',
|
|
|
|
'gates' => [
|
|
|
|
{ 'key' => 'boolean', 'value' => false },
|
|
|
|
{ 'key' => 'percentage_of_time', 'value' => 30 }
|
|
|
|
])
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
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}")
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(401)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a 403 for users' do
|
|
|
|
delete api("/features/#{feature_name}", user)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(403)
|
|
|
|
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)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(204)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the gate value was set' do
|
|
|
|
before do
|
|
|
|
Feature.get(feature_name).enable
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes an enabled feature' do
|
|
|
|
delete api("/features/#{feature_name}", admin)
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(204)
|
|
|
|
expect(Feature.get(feature_name)).not_to be_enabled
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-31 17:06:01 -04:00
|
|
|
end
|