Commit 62a5969e authored by Shinya Maeda's avatar Shinya Maeda

Support Ajax endpoints for FeatureFlagsController

- Support show, create, update, destroy endpoints
- Add tests

Add changelog

Refactor

fix

Use rails status
parent 59e0801e
......@@ -4,6 +4,7 @@ class Projects::FeatureFlagsController < Projects::ApplicationController
respond_to :html
before_action :authorize_read_feature_flag!
before_action :authorize_create_feature_flag!, only: [:new, :create]
before_action :authorize_update_feature_flag!, only: [:edit, :update]
before_action :authorize_destroy_feature_flag!, only: [:destroy]
......@@ -30,13 +31,29 @@ class Projects::FeatureFlagsController < Projects::ApplicationController
@feature_flag = project.operations_feature_flags.new
end
def show
respond_to do |format|
format.json do
Gitlab::PollingInterval.set_header(response, interval: 10_000)
render_success_json
end
end
end
def create
@feature_flag = project.operations_feature_flags.create(create_params)
if @feature_flag.persisted?
redirect_to project_feature_flags_path(@project), status: 302, notice: 'Feature flag was successfully created.'
respond_to do |format|
format.html { redirect_to_index(notice: 'Feature flag was successfully created.') }
format.json { render_success_json }
end
else
render :new
respond_to do |format|
format.html { render :new }
format.json { render_error_json }
end
end
end
......@@ -45,17 +62,29 @@ class Projects::FeatureFlagsController < Projects::ApplicationController
def update
if feature_flag.update(update_params)
redirect_to project_feature_flags_path(@project), status: 302, notice: 'Feature flag was successfully updated.'
respond_to do |format|
format.html { redirect_to_index(notice: 'Feature flag was successfully updated.') }
format.json { render_success_json }
end
else
render :edit
respond_to do |format|
format.html { render :edit }
format.json { render_error_json }
end
end
end
def destroy
if feature_flag.destroy
redirect_to project_feature_flags_path(@project), status: 302, notice: 'Feature flag was successfully removed.'
respond_to do |format|
format.html { redirect_to_index(notice: 'Feature flag was successfully removed.') }
format.json { render_success_json }
end
else
redirect_to project_feature_flags_path(@project), status: 302, alert: 'Feature flag was not removed.'
respond_to do |format|
format.html { redirect_to_index(alert: 'Feature flag was not removed.') }
format.json { render_error_json }
end
end
end
......@@ -75,6 +104,12 @@ class Projects::FeatureFlagsController < Projects::ApplicationController
.permit(:name, :description, :active)
end
def feature_flag_json
FeatureFlagSerializer
.new(project: @project, current_user: @current_user)
.represent(feature_flag)
end
def feature_flags_json
FeatureFlagSerializer
.new(project: @project, current_user: @current_user)
......@@ -87,4 +122,17 @@ class Projects::FeatureFlagsController < Projects::ApplicationController
.new(project: @project, current_user: @current_user)
.represent(@project)
end
def redirect_to_index(**args)
redirect_to project_feature_flags_path(@project), status: :found, **args
end
def render_success_json
render json: feature_flag_json, status: :ok
end
def render_error_json
render json: { message: feature_flag.errors.full_messages },
status: :bad_request
end
end
---
title: Support Ajax endpoints for FeatureFlagsController
merge_request: 9127
author:
type: other
......@@ -3,12 +3,15 @@ require 'spec_helper'
describe Projects::FeatureFlagsController do
include Gitlab::Routing
set(:user) { create(:user) }
set(:project) { create(:project) }
let(:user) { developer }
let(:developer) { create(:user) }
let(:reporter) { create(:user) }
let(:feature_enabled) { true }
before do
project.add_developer(user)
project.add_developer(developer)
project.add_reporter(reporter)
sign_in(user)
stub_licensed_features(feature_flags: feature_enabled)
......@@ -151,6 +154,63 @@ describe Projects::FeatureFlagsController do
end
end
describe 'GET #show.json' do
subject { get(:show, params: params, format: :json) }
let!(:feature_flag) do
create(:operations_feature_flag, project: project)
end
let(:params) do
{
namespace_id: project.namespace,
project_id: project,
id: feature_flag.id
}
end
it 'returns all feature flags as json response' do
subject
expect(json_response['name']).to eq(feature_flag.name)
expect(json_response['active']).to eq(feature_flag.active)
end
it 'matches json schema' do
subject
expect(response).to match_response_schema('feature_flag', dir: 'ee')
end
context 'when feature flag is not found' do
let!(:feature_flag) { }
let(:params) do
{
namespace_id: project.namespace,
project_id: project,
id: 1
}
end
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(404)
end
end
context 'when user is reporter' do
let(:user) { reporter }
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(404)
end
end
end
describe 'POST create' do
render_views
......@@ -184,6 +244,68 @@ describe Projects::FeatureFlagsController do
end
end
describe 'POST create.json' do
subject { post(:create, params: params, format: :json) }
let(:params) do
{
namespace_id: project.namespace,
project_id: project,
operations_feature_flag: {
name: 'my_feature_flag',
active: true
}
}
end
it 'returns 200' do
subject
expect(response).to have_gitlab_http_status(200)
end
it 'creates a new feature flag' do
subject
expect(json_response['name']).to eq('my_feature_flag')
expect(json_response['active']).to be_truthy
end
it 'matches json schema' do
subject
expect(response).to match_response_schema('feature_flag', dir: 'ee')
end
context 'when the same named feature flag has already existed' do
before do
create(:operations_feature_flag, name: 'my_feature_flag', project: project)
end
it 'returns 400' do
subject
expect(response).to have_gitlab_http_status(400)
end
it 'returns an error message' do
subject
expect(json_response['message']).to include('Name has already been taken')
end
end
context 'when user is reporter' do
let(:user) { reporter }
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(404)
end
end
end
describe 'PUT update' do
let!(:feature_flag) { create(:operations_feature_flag, project: project, name: 'my_feature_flag') }
......@@ -222,6 +344,114 @@ describe Projects::FeatureFlagsController do
end
end
describe 'DELETE destroy.json' do
subject { delete(:destroy, params: params, format: :json) }
let!(:feature_flag) { create(:operations_feature_flag, project: project) }
let(:params) do
{
namespace_id: project.namespace,
project_id: project,
id: feature_flag.id
}
end
it 'returns 200' do
subject
expect(response).to have_gitlab_http_status(200)
end
it 'deletes one feature flag' do
expect { subject }.to change { Operations::FeatureFlag.count }.by(-1)
end
it 'matches json schema' do
subject
expect(response).to match_response_schema('feature_flag', dir: 'ee')
end
context 'when user is reporter' do
let(:user) { reporter }
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(404)
end
end
end
describe 'PUT update json' do
subject { put(:update, params: params, format: :json) }
let!(:feature_flag) do
create(:operations_feature_flag,
name: 'ci_live_trace',
active: true,
project: project)
end
let(:params) do
{
namespace_id: project.namespace,
project_id: project,
id: feature_flag.id,
operations_feature_flag: {
name: 'ci_new_live_trace'
}
}
end
it 'returns 200' do
subject
expect(response).to have_gitlab_http_status(200)
end
it 'updates the name of the feature flag name' do
subject
expect(json_response['name']).to eq('ci_new_live_trace')
end
it 'matches json schema' do
subject
expect(response).to match_response_schema('feature_flag', dir: 'ee')
end
context 'when updates active' do
let(:params) do
{
namespace_id: project.namespace,
project_id: project,
id: feature_flag.id,
operations_feature_flag: {
active: false
}
}
end
it 'updates active from true to false' do
expect { subject }
.to change { feature_flag.reload.active }.from(true).to(false)
end
end
context 'when user is reporter' do
let(:user) { reporter }
it 'returns 404' do
subject
expect(response).to have_gitlab_http_status(404)
end
end
end
private
def view_params
......
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