summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrpereira2 <rpereira@gitlab.com>2019-04-22 23:57:29 +0530
committerrpereira2 <rpereira@gitlab.com>2019-04-22 23:58:27 +0530
commit113c7af4c295df678af41daf1ed76b58304a393c (patch)
tree7f95d366d16f6f4466bc392e327540eb91355365
parent98f898d3cdcc79daad91c538551760295c0123e4 (diff)
downloadgitlab-ce-113c7af4c295df678af41daf1ed76b58304a393c.tar.gz
Refactor operations controller spec
- Move specs into a shared_context so that they can be reused. - Move common specs out of a more specific context.
-rw-r--r--spec/controllers/projects/settings/operations_controller_spec.rb222
1 files changed, 117 insertions, 105 deletions
diff --git a/spec/controllers/projects/settings/operations_controller_spec.rb b/spec/controllers/projects/settings/operations_controller_spec.rb
index 02a392f23c2..7ec5ebf3ce9 100644
--- a/spec/controllers/projects/settings/operations_controller_spec.rb
+++ b/spec/controllers/projects/settings/operations_controller_spec.rb
@@ -11,66 +11,142 @@ describe Projects::Settings::OperationsController do
project.add_maintainer(user)
end
- context 'error tracking' do
- describe 'GET #show' do
- it 'renders show template' do
+ describe 'GET #show' 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 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(:ok)
- expect(response).to render_template(:show)
+ expect(response).to have_gitlab_http_status(:not_found)
end
+ end
- context 'with existing setting' do
- let!(:error_tracking_setting) do
- create(:project_error_tracking_setting, project: project)
- end
+ context 'as an anonymous user' do
+ before do
+ sign_out(user)
+ end
- it 'loads existing setting' do
- get :show, params: project_params(project)
+ it 'redirects to signup page' do
+ get :show, params: project_params(project)
- expect(controller.helpers.error_tracking_setting)
- .to eq(error_tracking_setting)
- end
+ expect(response).to redirect_to(new_user_session_path)
end
+ end
+ end
- context 'without an existing setting' do
- it 'builds a new setting' do
- get :show, params: project_params(project)
+ describe 'PATCH #update' do
+ context 'with insufficient permissions' do
+ before do
+ project.add_reporter(user)
+ end
- expect(controller.helpers.error_tracking_setting).to be_new_record
+ 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
+
+ shared_context 'PATCH #update' do
+ let(:operations_update_service) { instance_double(::Projects::Operations::UpdateService) }
+ let(:operations_url) { project_settings_operations_url(project) }
+
+ let(:permitted_params) do
+ ActionController::Parameters.new(params).permit!
+ end
+
+ context 'format json' do
+ context 'when update succeeds' do
+ before do
+ stub_operations_update_service_returning(status: :success)
+ end
+
+ it 'returns success status' do
+ patch :update,
+ params: project_params(project, params),
+ format: :json
+
+ expect(::Projects::Operations::UpdateService)
+ .to have_received(:new).with(project, user, permitted_params)
+ expect(operations_update_service).to have_received(:execute)
+ expect(response).to have_gitlab_http_status(:ok)
+ expect(json_response).to eq('status' => 'success')
+ expect(flash[:notice]).to eq('Your changes have been saved')
end
end
- context 'with insufficient permissions' do
+ context 'when update fails' do
before do
- project.add_reporter(user)
+ stub_operations_update_service_returning(
+ status: :error,
+ message: 'error message'
+ )
end
- it 'renders 404' do
- get :show, params: project_params(project)
+ it 'returns error' do
+ patch :update,
+ params: project_params(project, params),
+ format: :json
- expect(response).to have_gitlab_http_status(:not_found)
+ expect(::Projects::Operations::UpdateService)
+ .to have_received(:new).with(project, user, permitted_params)
+ expect(operations_update_service).to have_received(:execute)
+ expect(response).to have_gitlab_http_status(:bad_request)
+ expect(json_response['message']).not_to be_nil
end
end
+ end
+ end
- context 'as an anonymous user' do
- before do
- sign_out(user)
+ context 'error tracking' do
+ describe 'GET #show' do
+ 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
- it 'redirects to signup page' do
+ context 'without an existing setting' do
+ it 'builds a new setting' do
get :show, params: project_params(project)
- expect(response).to redirect_to(new_user_session_path)
+ expect(controller.helpers.error_tracking_setting).to be_new_record
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
+ let(:params) do
{
error_tracking_setting_attributes: {
enabled: '1',
@@ -86,79 +162,7 @@ describe Projects::Settings::OperationsController do
}
end
- let(:error_tracking_permitted) do
- ActionController::Parameters.new(error_tracking_params).permit!
- end
-
- context 'format json' do
- context 'when update succeeds' do
- before do
- stub_operations_update_service_returning(status: :success)
- end
-
- it 'returns success status' do
- patch :update,
- params: project_params(project, error_tracking_params),
- format: :json
-
- expect(response).to have_gitlab_http_status(:ok)
- expect(json_response).to eq('status' => 'success')
- 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,
- message: 'error message'
- )
- end
-
- it 'returns error' do
- patch :update,
- params: project_params(project, error_tracking_params),
- format: :json
-
- expect(response).to have_gitlab_http_status(:bad_request)
- expect(json_response['message']).not_to be_nil
- end
- 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
-
- 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)
+ it_behaves_like 'PATCH #update'
end
end
@@ -171,4 +175,12 @@ describe Projects::Settings::OperationsController do
project: params
}
end
+
+ def stub_operations_update_service_returning(return_value = {})
+ allow(::Projects::Operations::UpdateService)
+ .to receive(:new).with(project, user, permitted_params)
+ .and_return(operations_update_service)
+ allow(operations_update_service).to receive(:execute)
+ .and_return(return_value)
+ end
end