Commit 674e5e5b authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch '57171-operations-controller-spec-refactor' into 'master'

Refactor operations controller spec

Closes #57171

See merge request gitlab-org/gitlab-ce!27558
parents 696698d3 e7e9929e
...@@ -11,34 +11,63 @@ describe Projects::Settings::OperationsController do ...@@ -11,34 +11,63 @@ describe Projects::Settings::OperationsController do
project.add_maintainer(user) project.add_maintainer(user)
end end
context 'error tracking' do shared_examples 'PATCHable' do
describe 'GET #show' do let(:operations_update_service) { instance_double(::Projects::Operations::UpdateService) }
it 'renders show template' do let(:operations_url) { project_settings_operations_url(project) }
get :show, params: project_params(project)
let(:permitted_params) do
ActionController::Parameters.new(params).permit!
end
context 'format json' do
context 'when update succeeds' do
it 'returns success status' do
stub_operations_update_service_returning(status: :success)
patch :update,
params: project_params(project, params),
format: :json
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(response).to render_template(:show) expect(json_response).to eq('status' => 'success')
expect(flash[:notice]).to eq('Your changes have been saved')
end
end end
context 'with existing setting' do context 'when update fails' do
let!(:error_tracking_setting) do it 'returns error' do
create(:project_error_tracking_setting, project: project) stub_operations_update_service_returning(
status: :error,
message: 'error message'
)
patch :update,
params: project_params(project, params),
format: :json
expect(response).to have_gitlab_http_status(:bad_request)
expect(json_response['message']).to eq('error message')
end
end
end end
it 'loads existing setting' do private
get :show, params: project_params(project)
expect(controller.helpers.error_tracking_setting) def stub_operations_update_service_returning(return_value = {})
.to eq(error_tracking_setting) expect(::Projects::Operations::UpdateService)
.to receive(:new).with(project, user, permitted_params)
.and_return(operations_update_service)
expect(operations_update_service).to receive(:execute)
.and_return(return_value)
end end
end end
context 'without an existing setting' do describe 'GET #show' do
it 'builds a new setting' do it 'renders show template' 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 have_gitlab_http_status(:ok)
end expect(response).to render_template(:show)
end end
context 'with insufficient permissions' do context 'with insufficient permissions' do
...@@ -67,98 +96,73 @@ describe Projects::Settings::OperationsController do ...@@ -67,98 +96,73 @@ describe Projects::Settings::OperationsController do
end end
describe 'PATCH #update' do describe 'PATCH #update' do
let(:operations_update_service) { spy(:operations_update_service) } context 'with insufficient permissions' do
let(:operations_url) { project_settings_operations_url(project) }
let(:error_tracking_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
let(:error_tracking_permitted) do
ActionController::Parameters.new(error_tracking_params).permit!
end
context 'format json' do
context 'when update succeeds' do
before do before do
stub_operations_update_service_returning(status: :success) project.add_reporter(user)
end end
it 'returns success status' do it 'renders 404' do
patch :update, patch :update, params: project_params(project)
params: project_params(project, error_tracking_params),
format: :json
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:not_found)
expect(json_response).to eq('status' => 'success')
expect(flash[:notice]).to eq('Your changes have been saved')
end end
end end
context 'when update fails' do context 'as an anonymous user' do
before do before do
stub_operations_update_service_returning( sign_out(user)
status: :error,
message: 'error message'
)
end end
it 'returns error' do it 'redirects to signup page' do
patch :update, patch :update, params: project_params(project)
params: project_params(project, error_tracking_params),
format: :json
expect(response).to have_gitlab_http_status(:bad_request) expect(response).to redirect_to(new_user_session_path)
expect(json_response['message']).not_to be_nil
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)
end .to eq(error_tracking_setting)
end end
context 'as an anonymous user' do
before do
sign_out(user)
end end
it 'redirects to signup page' do context 'without an existing setting' do
patch :update, params: project_params(project) 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 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 'PATCHable'
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
......
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