diff options
author | Peter Leitzen <pleitzen@gitlab.com> | 2019-01-07 18:12:40 +0100 |
---|---|---|
committer | Peter Leitzen <pleitzen@gitlab.com> | 2019-01-07 19:09:53 +0100 |
commit | 4c7466b217a8a5d3ece8760020d7c2382a2499f6 (patch) | |
tree | e40398017d7087b7952668e71e89a9f4b55ad62a | |
parent | 191c20d71447dca1a516b27c01a152a43cf8be69 (diff) | |
download | gitlab-ce-55177-sentry-configuration.tar.gz |
Implement error tracking configuration55177-sentry-configuration
Re-use operations controller which already handles tracing settings.
-rw-r--r-- | app/controllers/projects/settings/operations_controller.rb | 9 | ||||
-rw-r--r-- | app/helpers/projects_helper.rb | 2 | ||||
-rw-r--r-- | app/models/project.rb | 2 | ||||
-rw-r--r-- | app/services/projects/operations/update_service.rb | 2 | ||||
-rw-r--r-- | spec/controllers/projects/settings/operations_controller_spec.rb | 169 | ||||
-rw-r--r-- | spec/services/projects/operations/update_service_spec.rb | 61 |
6 files changed, 231 insertions, 14 deletions
diff --git a/app/controllers/projects/settings/operations_controller.rb b/app/controllers/projects/settings/operations_controller.rb index ae8ac61ad46..521ec2acebb 100644 --- a/app/controllers/projects/settings/operations_controller.rb +++ b/app/controllers/projects/settings/operations_controller.rb @@ -6,6 +6,8 @@ module Projects before_action :check_license before_action :authorize_update_environment! + helper_method :error_tracking_setting + def show end @@ -22,13 +24,18 @@ module Projects private + def error_tracking_setting + @error_tracking_setting ||= project.error_tracking_setting || + project.build_error_tracking_setting + end + def update_params params.require(:project).permit(permitted_project_params) end # overridden in EE def permitted_project_params - {} + { error_tracking_setting_attributes: [:enabled, :api_url, :token] } end def check_license diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index e23e6ec49e4..e67c327f7f8 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -285,7 +285,7 @@ module ProjectsHelper # overridden in EE def settings_operations_available? - false + Feature.enabled?(:error_tracking, @project) && can?(current_user, :read_environment, @project) end private diff --git a/app/models/project.rb b/app/models/project.rb index 9d0348d848a..d57098407d0 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -296,6 +296,8 @@ class Project < ActiveRecord::Base allow_destroy: true, 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 :members, to: :team, prefix: true delegate :add_user, :add_users, to: :team diff --git a/app/services/projects/operations/update_service.rb b/app/services/projects/operations/update_service.rb index 7ff0599ee95..abd6d8de750 100644 --- a/app/services/projects/operations/update_service.rb +++ b/app/services/projects/operations/update_service.rb @@ -12,7 +12,7 @@ module Projects private def project_update_params - {} + params.slice(:error_tracking_setting_attributes) end end end diff --git a/spec/controllers/projects/settings/operations_controller_spec.rb b/spec/controllers/projects/settings/operations_controller_spec.rb index fbb26de76d1..6dba70824a7 100644 --- a/spec/controllers/projects/settings/operations_controller_spec.rb +++ b/spec/controllers/projects/settings/operations_controller_spec.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +# require 'spec_helper' @@ -11,25 +12,171 @@ describe Projects::Settings::OperationsController do project.add_maintainer(user) end - describe 'GET #show' do - it 'returns 404' do - get :show, params: project_params(project) + context 'error tracking' do + describe 'GET #show' do + it 'renders show template' do + get :show, params: project_params(project) - expect(response).to have_gitlab_http_status(:not_found) + 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) + + 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 + 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 + 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) + + 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 + patch :update, params: project_params(project) + + expect(response).to redirect_to(new_user_session_path) + end + end end - end - describe 'PATCH #update' do - it 'returns 404' do - patch :update, params: project_params(project) + private - expect(response).to have_gitlab_http_status(:not_found) + 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 - def project_params(project) - { namespace_id: project.namespace, project_id: project } + def project_params(project, params = {}) + { + namespace_id: project.namespace, + project_id: project, + project: params + } end end diff --git a/spec/services/projects/operations/update_service_spec.rb b/spec/services/projects/operations/update_service_spec.rb index 332c1600cde..731be907453 100644 --- a/spec/services/projects/operations/update_service_spec.rb +++ b/spec/services/projects/operations/update_service_spec.rb @@ -11,6 +11,67 @@ describe Projects::Operations::UpdateService do subject { described_class.new(project, user, params) } 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 let(:params) { { name: '' } } |