Commit 41203f40 authored by GitLab Bot's avatar GitLab Bot

Automatic merge of gitlab-org/gitlab-ce master

parents b60a2789 56342d88
...@@ -18,10 +18,23 @@ class Projects::ServicesController < Projects::ApplicationController ...@@ -18,10 +18,23 @@ class Projects::ServicesController < Projects::ApplicationController
def update def update
@service.attributes = service_params[:service] @service.attributes = service_params[:service]
if @service.save(context: :manual_change) saved = @service.save(context: :manual_change)
redirect_to(project_settings_integrations_path(@project), notice: success_message)
else respond_to do |format|
render 'edit' format.html do
if saved
redirect_to project_settings_integrations_path(@project),
notice: success_message
else
render 'edit'
end
end
format.json do
status = saved ? :ok : :unprocessable_entity
render json: serialize_as_json, status: status
end
end end
end end
...@@ -67,4 +80,10 @@ class Projects::ServicesController < Projects::ApplicationController ...@@ -67,4 +80,10 @@ class Projects::ServicesController < Projects::ApplicationController
def ensure_service_enabled def ensure_service_enabled
render_404 unless service render_404 unless service
end end
def serialize_as_json
@service
.as_json(only: @service.json_fields)
.merge(errors: @service.errors.as_json)
end
end end
...@@ -107,6 +107,13 @@ class Service < ApplicationRecord ...@@ -107,6 +107,13 @@ class Service < ApplicationRecord
[] []
end end
# Expose a list of fields in the JSON endpoint.
#
# This list is used in `Service#as_json(only: json_fields)`.
def json_fields
%w(active)
end
def test_data(project, user) def test_data(project, user)
Gitlab::DataBuilder::Push.build_sample(project, user) Gitlab::DataBuilder::Push.build_sample(project, user)
end end
......
---
title: Expose update project service endpoint JSON
merge_request: 32759
author:
type: added
...@@ -19,9 +19,9 @@ describe Projects::ServicesController do ...@@ -19,9 +19,9 @@ describe Projects::ServicesController do
it 'renders 404' do it 'renders 404' do
allow_any_instance_of(Service).to receive(:can_test?).and_return(false) allow_any_instance_of(Service).to receive(:can_test?).and_return(false)
put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param } put :test, params: project_params
expect(response).to have_gitlab_http_status(404) expect(response).to have_gitlab_http_status(:not_found)
end end
end end
...@@ -29,11 +29,11 @@ describe Projects::ServicesController do ...@@ -29,11 +29,11 @@ describe Projects::ServicesController do
let(:service_params) { { active: 'true', url: '' } } let(:service_params) { { active: 'true', url: '' } }
it 'returns error messages in JSON response' do it 'returns error messages in JSON response' do
put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params } put :test, params: project_params(service: service_params)
expect(json_response['message']).to eq "Validations failed." expect(json_response['message']).to eq 'Validations failed.'
expect(json_response['service_response']).to include "Url can't be blank" expect(json_response['service_response']).to include "Url can't be blank"
expect(response).to have_gitlab_http_status(200) expect(response).to be_successful
end end
end end
...@@ -47,9 +47,9 @@ describe Projects::ServicesController do ...@@ -47,9 +47,9 @@ describe Projects::ServicesController do
it 'returns success' do it 'returns success' do
allow_any_instance_of(MicrosoftTeams::Notifier).to receive(:ping).and_return(true) allow_any_instance_of(MicrosoftTeams::Notifier).to receive(:ping).and_return(true)
put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param } put :test, params: project_params
expect(response.status).to eq(200) expect(response).to be_successful
end end
end end
...@@ -57,11 +57,11 @@ describe Projects::ServicesController do ...@@ -57,11 +57,11 @@ describe Projects::ServicesController do
stub_request(:get, 'http://example.com/rest/api/2/serverInfo') stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
.to_return(status: 200, body: '{}') .to_return(status: 200, body: '{}')
expect(Gitlab::HTTP).to receive(:get).with("/rest/api/2/serverInfo", any_args).and_call_original expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original
put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params } put :test, params: project_params(service: service_params)
expect(response.status).to eq(200) expect(response).to be_successful
end end
end end
...@@ -69,14 +69,23 @@ describe Projects::ServicesController do ...@@ -69,14 +69,23 @@ describe Projects::ServicesController do
stub_request(:get, 'http://example.com/rest/api/2/serverInfo') stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
.to_return(status: 200, body: '{}') .to_return(status: 200, body: '{}')
expect(Gitlab::HTTP).to receive(:get).with("/rest/api/2/serverInfo", any_args).and_call_original expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original
put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params } put :test, params: project_params(service: service_params)
expect(response.status).to eq(200) expect(response).to be_successful
end end
context 'when service is configured for the first time' do context 'when service is configured for the first time' do
let(:service_params) do
{
'active' => '1',
'push_events' => '1',
'token' => 'token',
'project_url' => 'http://test.com'
}
end
before do before do
allow_any_instance_of(ServiceHook).to receive(:execute).and_return(true) allow_any_instance_of(ServiceHook).to receive(:execute).and_return(true)
end end
...@@ -84,7 +93,7 @@ describe Projects::ServicesController do ...@@ -84,7 +93,7 @@ describe Projects::ServicesController do
it 'persist the object' do it 'persist the object' do
do_put do_put
expect(response).to have_gitlab_http_status(200) expect(response).to be_successful
expect(json_response).to be_empty expect(json_response).to be_empty
expect(BuildkiteService.first).to be_present expect(BuildkiteService.first).to be_present
end end
...@@ -92,18 +101,14 @@ describe Projects::ServicesController do ...@@ -92,18 +101,14 @@ describe Projects::ServicesController do
it 'creates the ServiceHook object' do it 'creates the ServiceHook object' do
do_put do_put
expect(response).to have_gitlab_http_status(200) expect(response).to be_successful
expect(json_response).to be_empty expect(json_response).to be_empty
expect(BuildkiteService.first.service_hook).to be_present expect(BuildkiteService.first.service_hook).to be_present
end end
def do_put def do_put
put :test, params: { put :test, params: project_params(id: 'buildkite',
namespace_id: project.namespace, service: service_params)
project_id: project,
id: 'buildkite',
service: { 'active' => '1', 'push_events' => '1', token: 'token', 'project_url' => 'http://test.com' }
}
end end
end end
end end
...@@ -113,9 +118,9 @@ describe Projects::ServicesController do ...@@ -113,9 +118,9 @@ describe Projects::ServicesController do
stub_request(:get, 'http://example.com/rest/api/2/serverInfo') stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
.to_return(status: 404) .to_return(status: 404)
put :test, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: service_params } put :test, params: project_params(service: service_params)
expect(response).to have_gitlab_http_status(200) expect(response).to be_successful
expect(json_response).to eq( expect(json_response).to eq(
'error' => true, 'error' => true,
'message' => 'Test failed.', 'message' => 'Test failed.',
...@@ -127,39 +132,70 @@ describe Projects::ServicesController do ...@@ -127,39 +132,70 @@ describe Projects::ServicesController do
end end
describe 'PUT #update' do describe 'PUT #update' do
context 'when param `active` is set to true' do describe 'as HTML' do
it 'activates the service and redirects to integrations paths' do let(:service_params) { { active: true } }
put :update,
params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: true } }
expect(response).to redirect_to(project_settings_integrations_path(project)) before do
expect(flash[:notice]).to eq 'Jira activated.' put :update, params: project_params(service: service_params)
end
context 'when param `active` is set to true' do
it 'activates the service and redirects to integrations paths' do
expect(response).to redirect_to(project_settings_integrations_path(project))
expect(flash[:notice]).to eq 'Jira activated.'
end
end
context 'when param `active` is set to false' do
let(:service_params) { { active: false } }
it 'does not activate the service but saves the settings' do
expect(flash[:notice]).to eq 'Jira settings saved, but not activated.'
end
end end
end
context 'when param `active` is set to false' do context 'when activating Jira service from a template' do
it 'does not activate the service but saves the settings' do let(:service) do
put :update, create(:jira_service, project: project, template: true)
params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: false } } end
expect(flash[:notice]).to eq 'Jira settings saved, but not activated.' it 'activate Jira service from template' do
expect(flash[:notice]).to eq 'Jira activated.'
end
end end
end end
context 'when activating Jira service from a template' do describe 'as JSON' do
let(:template_service) { create(:jira_service, project: project, template: true) } before do
put :update, params: project_params(service: service_params, format: :json)
end
context 'when update succeeds' do
let(:service_params) { { url: 'http://example.com' } }
it 'returns JSON response with no errors' do
expect(response).to be_successful
expect(json_response).to include('active' => true, 'errors' => {})
end
end
it 'activate Jira service from template' do context 'when update fails' do
put :update, params: { namespace_id: project.namespace, project_id: project, id: service.to_param, service: { active: true } } let(:service_params) { { url: '' } }
expect(flash[:notice]).to eq 'Jira activated.' it 'returns JSON response with errors' do
expect(response).to have_gitlab_http_status(:unprocessable_entity)
expect(json_response).to include(
'active' => true,
'errors' => { 'url' => ['must be a valid URL', %{can't be blank}] }
)
end
end end
end end
end end
describe "GET #edit" do describe 'GET #edit' do
before do before do
get :edit, params: { namespace_id: project.namespace, project_id: project, id: 'jira' } get :edit, params: project_params(id: 'jira')
end end
context 'with approved services' do context 'with approved services' do
...@@ -168,4 +204,14 @@ describe Projects::ServicesController do ...@@ -168,4 +204,14 @@ describe Projects::ServicesController do
end end
end end
end end
private
def project_params(opts = {})
opts.reverse_merge(
namespace_id: project.namespace,
project_id: project,
id: service.to_param
)
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