Commit 6710c874 authored by Peter Leitzen's avatar Peter Leitzen

Implement error tracking configuration

Re-use operations controller which already handles tracing settings.
parent 191c20d7
...@@ -6,6 +6,8 @@ module Projects ...@@ -6,6 +6,8 @@ module Projects
before_action :check_license before_action :check_license
before_action :authorize_update_environment! before_action :authorize_update_environment!
helper_method :error_tracking_setting
def show def show
end end
...@@ -22,13 +24,18 @@ module Projects ...@@ -22,13 +24,18 @@ module Projects
private private
def error_tracking_setting
@error_tracking_setting ||= project.error_tracking_setting ||
project.build_error_tracking_setting
end
def update_params def update_params
params.require(:project).permit(permitted_project_params) params.require(:project).permit(permitted_project_params)
end end
# overridden in EE # overridden in EE
def permitted_project_params def permitted_project_params
{} { error_tracking_setting_attributes: [:enabled, :api_url, :token] }
end end
def check_license def check_license
......
...@@ -285,7 +285,7 @@ module ProjectsHelper ...@@ -285,7 +285,7 @@ module ProjectsHelper
# overridden in EE # overridden in EE
def settings_operations_available? def settings_operations_available?
false Feature.enabled?(:error_tracking, @project) && can?(current_user, :read_environment, @project)
end end
private private
......
...@@ -296,6 +296,8 @@ class Project < ActiveRecord::Base ...@@ -296,6 +296,8 @@ class Project < ActiveRecord::Base
allow_destroy: true, allow_destroy: true,
reject_if: ->(attrs) { attrs[:id].blank? && attrs[:url].blank? } reject_if: ->(attrs) { attrs[:id].blank? && attrs[:url].blank? }
accepts_nested_attributes_for :error_tracking_setting, update_only: true
delegate :name, to: :owner, allow_nil: true, prefix: true delegate :name, to: :owner, allow_nil: true, prefix: true
delegate :members, to: :team, prefix: true delegate :members, to: :team, prefix: true
delegate :add_user, :add_users, to: :team delegate :add_user, :add_users, to: :team
......
...@@ -12,7 +12,7 @@ module Projects ...@@ -12,7 +12,7 @@ module Projects
private private
def project_update_params def project_update_params
{} params.slice(:error_tracking_setting_attributes)
end end
end end
end end
......
...@@ -11,25 +11,171 @@ describe Projects::Settings::OperationsController do ...@@ -11,25 +11,171 @@ describe Projects::Settings::OperationsController do
project.add_maintainer(user) project.add_maintainer(user)
end end
context 'error tracking' do
describe 'GET #show' do describe 'GET #show' do
it 'returns 404' do it 'renders show template' do
get :show, params: project_params(project)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:show)
end
context 'with existing setting' do
let!(:error_tracking_setting) do
create(:project_error_tracking_setting, project: project)
end
it 'loads existing setting' do
get :show, params: project_params(project)
expect(controller.helpers.error_tracking_setting)
.to eq(error_tracking_setting)
end
end
context 'without an existing setting' do
it 'builds a new setting' do
get :show, params: project_params(project)
expect(controller.helpers.error_tracking_setting).to be_new_record
end
end
context 'with feature flag disabled' do
before do
stub_feature_flags(error_tracking: false)
end
it 'renders 404' do
get :show, params: project_params(project) get :show, params: project_params(project)
expect(response).to have_gitlab_http_status(:not_found) expect(response).to have_gitlab_http_status(:not_found)
end end
end end
context 'with insufficient permissions' do
before do
project.add_reporter(user)
end
it 'renders 404' do
get :show, params: project_params(project)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'as an anonymous user' do
before do
sign_out(user)
end
it 'redirects to signup page' do
get :show, params: project_params(project)
expect(response).to redirect_to(new_user_session_path)
end
end
end
describe 'PATCH #update' do describe 'PATCH #update' do
it 'returns 404' do let(:operations_update_service) { spy(:operations_update_service) }
let(:operations_url) { project_settings_operations_url(project) }
let(:error_tracking_params) do
{
error_tracking_setting_attributes: {
enabled: '1',
api_url: 'http://url',
token: 'token'
}
}
end
let(:error_tracking_permitted) do
ActionController::Parameters.new(error_tracking_params).permit!
end
context 'when update succeeds' do
before do
stub_operations_update_service_returning(status: :success)
end
it 'shows a notice' do
patch :update, params: project_params(project, error_tracking_params)
expect(response).to redirect_to(operations_url)
expect(flash[:notice]).to eq _('Your changes have been saved')
end
end
context 'when update fails' do
before do
stub_operations_update_service_returning(status: :error)
end
it 'renders show page' do
patch :update, params: project_params(project, error_tracking_params)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:show)
end
end
context 'with feature flag disabled' do
before do
stub_feature_flags(error_tracking: false)
end
it 'renders 404' do
patch :update, params: project_params(project)
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'with insufficient permissions' do
before do
project.add_reporter(user)
end
it 'renders 404' do
patch :update, params: project_params(project) patch :update, params: project_params(project)
expect(response).to have_gitlab_http_status(:not_found) expect(response).to have_gitlab_http_status(:not_found)
end end
end end
context 'as an anonymous user' do
before do
sign_out(user)
end
it 'redirects to signup page' do
patch :update, params: project_params(project)
expect(response).to redirect_to(new_user_session_path)
end
end
end
private
def stub_operations_update_service_returning(return_value = {})
expect(::Projects::Operations::UpdateService)
.to receive(:new).with(project, user, error_tracking_permitted)
.and_return(operations_update_service)
expect(operations_update_service).to receive(:execute)
.and_return(return_value)
end
end
private private
def project_params(project) def project_params(project, params = {})
{ namespace_id: project.namespace, project_id: project } {
namespace_id: project.namespace,
project_id: project,
project: params
}
end end
end end
...@@ -11,6 +11,67 @@ describe Projects::Operations::UpdateService do ...@@ -11,6 +11,67 @@ describe Projects::Operations::UpdateService do
subject { described_class.new(project, user, params) } subject { described_class.new(project, user, params) }
describe '#execute' do describe '#execute' do
context 'error tracking' do
context 'with existing error tracking setting' do
let(:params) do
{
error_tracking_setting_attributes: {
enabled: false,
api_url: 'http://url',
token: 'token'
}
}
end
before do
create(:project_error_tracking_setting, project: project)
end
it 'updates the settings' do
expect(result[:status]).to eq(:success)
project.reload
expect(project.error_tracking_setting).not_to be_enabled
expect(project.error_tracking_setting.api_url).to eq('http://url')
expect(project.error_tracking_setting.token).to eq('token')
end
end
context 'without an existing error tracking setting' do
let(:params) do
{
error_tracking_setting_attributes: {
enabled: true,
api_url: 'http://url',
token: 'token'
}
}
end
it 'creates a setting' do
expect(result[:status]).to eq(:success)
expect(project.error_tracking_setting).to be_enabled
expect(project.error_tracking_setting.api_url).to eq('http://url')
expect(project.error_tracking_setting.token).to eq('token')
end
end
context 'with invalid parameters' do
let(:params) { {} }
let!(:error_tracking_setting) do
create(:project_error_tracking_setting, project: project)
end
it 'does nothing' do
expect(result[:status]).to eq(:success)
expect(project.reload.error_tracking_setting)
.to eq(error_tracking_setting)
end
end
end
context 'with inappropriate params' do context 'with inappropriate params' do
let(:params) { { name: '' } } let(:params) { { name: '' } }
......
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