Commit 4d66019e authored by Jarka Košanová's avatar Jarka Košanová

Redirect to the referrer when updating a service

- if a referrer is not empty
parent 1b3d3066
......@@ -2,6 +2,7 @@
class Projects::ServicesController < Projects::ApplicationController
include ServiceParams
include InternalRedirect
# Authorize
before_action :authorize_admin_project!
......@@ -26,8 +27,8 @@ class Projects::ServicesController < Projects::ApplicationController
respond_to do |format|
format.html do
if saved
redirect_to project_settings_integrations_path(@project),
notice: success_message
target_url = safe_redirect_path(params[:redirect_to]).presence || project_settings_integrations_path(@project)
redirect_to target_url, notice: success_message
else
render 'edit'
end
......
......@@ -13,6 +13,7 @@
= form_for(@service, as: :service, url: scoped_integration_path(@service), method: :put, html: { class: 'gl-show-field-errors integration-settings-form js-integration-settings-form', data: { 'can-test' => @service.can_test?, 'test-url' => test_project_service_path(@project, @service) } }) do |form|
= render 'shared/service_settings', form: form, service: @service
.footer-block.row-content-block
%input{ id: 'services_redirect_to', type: 'hidden', name: 'redirect_to', value: request.referrer }
= service_save_button
&nbsp;
= link_to _('Cancel'), project_settings_integrations_path(@project), class: 'btn btn-cancel'
......
......@@ -134,24 +134,50 @@ describe Projects::ServicesController do
describe 'PUT #update' do
describe 'as HTML' do
let(:service_params) { { active: true } }
let(:params) { project_params(service: service_params) }
let(:message) { 'Jira activated.' }
let(:redirect_url) { project_settings_integrations_path(project) }
before do
put :update, params: project_params(service: service_params)
put :update, params: params
end
shared_examples 'service update' do
it 'redirects to the correct url with a flash message' do
expect(response).to redirect_to(redirect_url)
expect(flash[:notice]).to eq(message)
end
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.'
let(:params) { project_params(service: service_params, redirect_to: redirect) }
context 'when redirect_to param is present' do
let(:redirect) { '/redirect_here' }
let(:redirect_url) { redirect }
it_behaves_like 'service update'
end
context 'when redirect_to is an external domain' do
let(:redirect) { 'http://examle.com' }
it_behaves_like 'service update'
end
context 'when redirect_to param is an empty string' do
let(:redirect) { '' }
it_behaves_like 'service update'
end
end
context 'when param `active` is set to false' do
let(:service_params) { { active: false } }
let(:message) { 'Jira settings saved, but not activated.' }
it 'does not activate the service but saves the settings' do
expect(flash[:notice]).to eq 'Jira settings saved, but not activated.'
end
it_behaves_like 'service update'
end
end
......
......@@ -15,7 +15,8 @@ describe 'projects/services/_form' do
allow(view).to receive_messages(current_user: user,
can?: true,
current_application_settings: Gitlab::CurrentSettings.current_application_settings)
current_application_settings: Gitlab::CurrentSettings.current_application_settings,
request: double(referrer: '/services'))
end
context 'commit_events and merge_request_events' do
......@@ -30,6 +31,7 @@ describe 'projects/services/_form' do
expect(rendered).to have_content('Event will be triggered when a commit is created/updated')
expect(rendered).to have_content('Event will be triggered when a merge request is created/updated/merged')
expect(rendered).to have_css("input[name='redirect_to'][value='/services']", count: 1, visible: false)
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