diff --git a/lib/flipper/api/v1/actions/features.rb b/lib/flipper/api/v1/actions/features.rb index 278039183..0805b703b 100644 --- a/lib/flipper/api/v1/actions/features.rb +++ b/lib/flipper/api/v1/actions/features.rb @@ -19,6 +19,19 @@ def get features: features }) end + + def post + feature_name = params.fetch('name') do + json_response({ + errors: [{ + message: 'Missing post parameter: name', + }] + }, 422) + end + + flipper.adapter.add(flipper[feature_name]) + json_response({}, 200) + end end end end diff --git a/spec/flipper/api/v1/actions/features_spec.rb b/spec/flipper/api/v1/actions/features_spec.rb index 055982630..21cfc1292 100644 --- a/spec/flipper/api/v1/actions/features_spec.rb +++ b/spec/flipper/api/v1/actions/features_spec.rb @@ -25,26 +25,26 @@ "name"=> "boolean", "value" => true}, { - "key" =>"groups", - "name" => "group", - "value" =>[], - }, - { - "key" => "actors", - "name"=>"actor", - "value"=>["10"], - }, - { - "key" => "percentage_of_actors", - "name" => "percentage_of_actors", - "value" => 0, - }, - { - "key"=> "percentage_of_time", - "name"=> "percentage_of_time", - "value"=> 0, - }, - ], + "key" =>"groups", + "name" => "group", + "value" =>[], + }, + { + "key" => "actors", + "name"=>"actor", + "value"=>["10"], + }, + { + "key" => "percentage_of_actors", + "name" => "percentage_of_actors", + "value" => 0, + }, + { + "key"=> "percentage_of_time", + "name"=> "percentage_of_time", + "value"=> 0, + }, + ], }, ] } @@ -67,4 +67,40 @@ end end end + + describe 'post' do + context 'succesful request' do + before do + post 'api/v1/features', { name: 'my_feature' } + end + + it 'responds 200 on success' do + expect(last_response.status).to eq(200) + expect(json_response).to eq({}) + end + + it 'adds feature' do + expect(flipper.features.map(&:key)).to include('my_feature') + end + + it 'does not enable feature' do + expect(flipper['my_feature'].enabled?).to be_falsy + end + end + + context 'bad request' do + before do + post 'api/v1/features' + end + + it 'returns correct status code' do + expect(last_response.status).to eq(422) + end + + it 'returns formatted error' do + errors = json_response['errors'] + expect(errors.first['message']).to eq('Missing post parameter: name') + end + end + end end