Commit 113c7af4 authored by rpereira2's avatar rpereira2

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.
parent 98f898d3
...@@ -11,7 +11,6 @@ describe Projects::Settings::OperationsController do ...@@ -11,7 +11,6 @@ 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 'renders show template' do it 'renders show template' do
get :show, params: project_params(project) get :show, params: project_params(project)
...@@ -20,34 +19,39 @@ describe Projects::Settings::OperationsController do ...@@ -20,34 +19,39 @@ describe Projects::Settings::OperationsController do
expect(response).to render_template(:show) expect(response).to render_template(:show)
end end
context 'with existing setting' do context 'with insufficient permissions' do
let!(:error_tracking_setting) do before do
create(:project_error_tracking_setting, project: project) project.add_reporter(user)
end end
it 'loads existing setting' do it 'renders 404' do
get :show, params: project_params(project) get :show, params: project_params(project)
expect(controller.helpers.error_tracking_setting) expect(response).to have_gitlab_http_status(:not_found)
.to eq(error_tracking_setting)
end end
end end
context 'without an existing setting' do context 'as an anonymous user' do
it 'builds a new setting' do before do
sign_out(user)
end
it 'redirects to signup page' do
get :show, params: project_params(project) get :show, params: project_params(project)
expect(controller.helpers.error_tracking_setting).to be_new_record expect(response).to redirect_to(new_user_session_path)
end
end end
end end
describe 'PATCH #update' do
context 'with insufficient permissions' do context 'with insufficient permissions' do
before do before do
project.add_reporter(user) project.add_reporter(user)
end end
it 'renders 404' do it 'renders 404' do
get :show, 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
...@@ -59,35 +63,19 @@ describe Projects::Settings::OperationsController do ...@@ -59,35 +63,19 @@ describe Projects::Settings::OperationsController do
end end
it 'redirects to signup page' do it 'redirects to signup page' do
get :show, params: project_params(project) patch :update, params: project_params(project)
expect(response).to redirect_to(new_user_session_path) expect(response).to redirect_to(new_user_session_path)
end end
end end
end end
describe 'PATCH #update' do shared_context 'PATCH #update' do
let(:operations_update_service) { spy(:operations_update_service) } let(:operations_update_service) { instance_double(::Projects::Operations::UpdateService) }
let(:operations_url) { project_settings_operations_url(project) } let(:operations_url) { project_settings_operations_url(project) }
let(:error_tracking_params) do let(:permitted_params) do
{ ActionController::Parameters.new(params).permit!
error_tracking_setting_attributes: {
enabled: '1',
api_host: 'http://url',
token: 'token',
project: {
slug: 'sentry-project',
name: 'Sentry Project',
organization_slug: 'sentry-org',
organization_name: 'Sentry Org'
}
}
}
end
let(:error_tracking_permitted) do
ActionController::Parameters.new(error_tracking_params).permit!
end end
context 'format json' do context 'format json' do
...@@ -98,9 +86,12 @@ describe Projects::Settings::OperationsController do ...@@ -98,9 +86,12 @@ describe Projects::Settings::OperationsController do
it 'returns success status' do it 'returns success status' do
patch :update, patch :update,
params: project_params(project, error_tracking_params), params: project_params(project, params),
format: :json 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(response).to have_gitlab_http_status(:ok)
expect(json_response).to eq('status' => 'success') expect(json_response).to eq('status' => 'success')
expect(flash[:notice]).to eq('Your changes have been saved') expect(flash[:notice]).to eq('Your changes have been saved')
...@@ -117,48 +108,61 @@ describe Projects::Settings::OperationsController do ...@@ -117,48 +108,61 @@ describe Projects::Settings::OperationsController do
it 'returns error' do it 'returns error' do
patch :update, patch :update,
params: project_params(project, error_tracking_params), params: project_params(project, params),
format: :json 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(:bad_request) expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).not_to be_nil expect(json_response['message']).not_to be_nil
end end
end end
end end
end
context 'with insufficient permissions' do context 'error tracking' do
before do describe 'GET #show' do
project.add_reporter(user) context 'with existing setting' do
let!(:error_tracking_setting) do
create(:project_error_tracking_setting, project: project)
end end
it 'renders 404' do it 'loads existing setting' do
patch :update, params: project_params(project) get :show, params: project_params(project)
expect(response).to have_gitlab_http_status(:not_found) expect(controller.helpers.error_tracking_setting)
.to eq(error_tracking_setting)
end end
end end
context 'as an anonymous user' do context 'without an existing setting' do
before do it 'builds a new setting' do
sign_out(user) get :show, params: project_params(project)
end
it 'redirects to signup page' do
patch :update, 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 end
end end
private describe 'PATCH #update' do
let(:params) do
{
error_tracking_setting_attributes: {
enabled: '1',
api_host: 'http://url',
token: 'token',
project: {
slug: 'sentry-project',
name: 'Sentry Project',
organization_slug: 'sentry-org',
organization_name: 'Sentry Org'
}
}
}
end
def stub_operations_update_service_returning(return_value = {}) it_behaves_like 'PATCH #update'
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
end end
...@@ -171,4 +175,12 @@ describe Projects::Settings::OperationsController do ...@@ -171,4 +175,12 @@ describe Projects::Settings::OperationsController do
project: params project: params
} }
end 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 end
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