debian-mirror-gitlab/spec/controllers/projects/services_controller_spec.rb

431 lines
14 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Projects::ServicesController do
2020-11-24 15:15:51 +05:30
include JiraServiceHelper
2021-04-29 21:17:54 +05:30
include AfterNextHelpers
2020-11-24 15:15:51 +05:30
2021-09-30 23:02:18 +05:30
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
let_it_be(:jira_integration) { create(:jira_integration, project: project) }
let(:integration) { jira_integration }
let(:integration_params) { { username: 'username', password: 'password', url: 'http://example.com' } }
2015-09-11 14:41:01 +05:30
before do
sign_in(user)
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2015-09-11 14:41:01 +05:30
end
2021-12-11 22:18:48 +05:30
it_behaves_like Integrations::Actions do
2021-11-11 11:23:49 +05:30
let(:integration_attributes) { { project: project } }
let(:routing_params) do
{
namespace_id: project.namespace,
project_id: project,
id: integration.to_param
}
end
end
2017-09-10 17:25:29 +05:30
describe '#test' do
2021-09-30 23:02:18 +05:30
context 'when the integration is not testable' do
2017-09-10 17:25:29 +05:30
it 'renders 404' do
2021-09-30 23:02:18 +05:30
allow_any_instance_of(Integration).to receive(:testable?).and_return(false)
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
put :test, params: project_params
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2018-03-27 19:54:05 +05:30
context 'when validations fail' do
2021-09-30 23:02:18 +05:30
let(:integration_params) { { active: 'true', url: '' } }
2018-03-27 19:54:05 +05:30
it 'returns error messages in JSON response' do
2021-09-30 23:02:18 +05:30
put :test, params: project_params(service: integration_params)
2018-03-27 19:54:05 +05:30
2019-12-04 20:38:33 +05:30
expect(json_response['message']).to eq 'Validations failed.'
2019-02-15 15:39:39 +05:30
expect(json_response['service_response']).to include "Url can't be blank"
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2018-03-27 19:54:05 +05:30
end
end
2021-09-30 23:02:18 +05:30
context 'when successful' do
2017-09-10 17:25:29 +05:30
context 'with empty project' do
2021-09-30 23:02:18 +05:30
let_it_be(:project) { create(:project) }
context 'with chat notification integration' do
let_it_be(:teams_integration) { project.create_microsoft_teams_integration(webhook: 'http://webhook.com') }
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
let(:integration) { teams_integration }
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
it 'returns success' do
2021-09-30 23:02:18 +05:30
allow_next(::MicrosoftTeams::Notifier).to receive(:ping).and_return(true)
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
put :test, params: project_params
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
it 'returns success' do
2021-09-30 23:02:18 +05:30
stub_jira_integration_test
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original
2019-09-04 21:01:54 +05:30
2021-09-30 23:02:18 +05:30
put :test, params: project_params(service: integration_params)
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
it 'returns success' do
2021-09-30 23:02:18 +05:30
stub_jira_integration_test
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
expect(Gitlab::HTTP).to receive(:get).with('/rest/api/2/serverInfo', any_args).and_call_original
2019-09-04 21:01:54 +05:30
2021-09-30 23:02:18 +05:30
put :test, params: project_params(service: integration_params)
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2015-10-24 18:46:33 +05:30
end
2017-09-10 17:25:29 +05:30
context 'when service is configured for the first time' do
2021-09-30 23:02:18 +05:30
let(:integration_params) do
2019-12-04 20:38:33 +05:30
{
'active' => '1',
'push_events' => '1',
'token' => 'token',
2020-10-24 23:57:45 +05:30
'project_url' => 'https://buildkite.com/organization/pipeline'
2019-12-04 20:38:33 +05:30
}
end
2017-09-10 17:25:29 +05:30
before do
allow_any_instance_of(ServiceHook).to receive(:execute).and_return(true)
end
it 'persist the object' do
do_put
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2019-02-15 15:39:39 +05:30
expect(json_response).to be_empty
2021-09-04 01:27:46 +05:30
expect(Integrations::Buildkite.first).to be_present
2017-09-10 17:25:29 +05:30
end
it 'creates the ServiceHook object' do
do_put
2017-08-17 22:00:37 +05:30
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2019-02-15 15:39:39 +05:30
expect(json_response).to be_empty
2021-09-04 01:27:46 +05:30
expect(Integrations::Buildkite.first.service_hook).to be_present
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def do_put
2019-12-04 20:38:33 +05:30
put :test, params: project_params(id: 'buildkite',
2021-09-30 23:02:18 +05:30
service: integration_params)
2015-10-24 18:46:33 +05:30
end
2015-09-11 14:41:01 +05:30
end
end
2015-10-24 18:46:33 +05:30
2021-09-30 23:02:18 +05:30
context 'when unsuccessful' do
2021-04-29 21:17:54 +05:30
it 'returns an error response when the integration test fails' do
2019-02-15 15:39:39 +05:30
stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
.to_return(status: 404)
2015-10-24 18:46:33 +05:30
2021-09-30 23:02:18 +05:30
put :test, params: project_params(service: integration_params)
2017-09-10 17:25:29 +05:30
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2019-02-15 15:39:39 +05:30
expect(json_response).to eq(
'error' => true,
2020-11-24 15:15:51 +05:30
'message' => 'Connection failed. Please check your settings.',
2019-02-15 15:39:39 +05:30
'service_response' => '',
'test_failed' => true
)
2021-04-29 21:17:54 +05:30
end
context 'with the Slack integration' do
2021-09-30 23:02:18 +05:30
let_it_be(:integration) { build(:integrations_slack) }
2021-04-29 21:17:54 +05:30
it 'returns an error response when the URL is blocked' do
put :test, params: project_params(service: { webhook: 'http://127.0.0.1' })
expect(response).to be_successful
expect(json_response).to eq(
'error' => true,
'message' => 'Connection failed. Please check your settings.',
'service_response' => "URL 'http://127.0.0.1' is blocked: Requests to localhost are not allowed",
'test_failed' => true
)
end
it 'returns an error response when a network exception is raised' do
2021-09-04 01:27:46 +05:30
expect_next(Integrations::Slack).to receive(:test).and_raise(Errno::ECONNREFUSED)
2021-04-29 21:17:54 +05:30
put :test, params: project_params
expect(response).to be_successful
expect(json_response).to eq(
'error' => true,
'message' => 'Connection failed. Please check your settings.',
'service_response' => 'Connection refused',
'test_failed' => true
)
end
2017-09-10 17:25:29 +05:30
end
2015-10-24 18:46:33 +05:30
end
end
2016-09-29 09:46:39 +05:30
describe 'PUT #update' do
2019-12-04 20:38:33 +05:30
describe 'as HTML' do
2021-09-30 23:02:18 +05:30
let(:integration_params) { { active: true } }
let(:params) { project_params(service: integration_params) }
2020-06-23 00:09:42 +05:30
2020-11-24 15:15:51 +05:30
let(:message) { 'Jira settings saved and active.' }
2022-03-02 08:16:31 +05:30
let(:redirect_url) { edit_project_integration_path(project, integration) }
2017-09-10 17:25:29 +05:30
2019-12-04 20:38:33 +05:30
before do
2021-10-27 15:23:28 +05:30
stub_jira_integration_test
2020-06-23 00:09:42 +05:30
put :update, params: params
end
2021-09-30 23:02:18 +05:30
shared_examples 'integration update' do
2020-06-23 00:09:42 +05:30
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
2019-12-04 20:38:33 +05:30
end
context 'when param `active` is set to true' do
2021-09-30 23:02:18 +05:30
let(:params) { project_params(service: integration_params, redirect_to: redirect) }
2020-06-23 00:09:42 +05:30
context 'when redirect_to param is present' do
let(:redirect) { '/redirect_here' }
let(:redirect_url) { redirect }
2021-09-30 23:02:18 +05:30
it_behaves_like 'integration update'
2020-06-23 00:09:42 +05:30
end
context 'when redirect_to is an external domain' do
let(:redirect) { 'http://examle.com' }
2021-09-30 23:02:18 +05:30
it_behaves_like 'integration update'
2020-06-23 00:09:42 +05:30
end
context 'when redirect_to param is an empty string' do
let(:redirect) { '' }
2021-09-30 23:02:18 +05:30
it_behaves_like 'integration update'
2019-12-04 20:38:33 +05:30
end
2017-09-10 17:25:29 +05:30
end
2016-09-29 09:46:39 +05:30
2019-12-04 20:38:33 +05:30
context 'when param `active` is set to false' do
2021-09-30 23:02:18 +05:30
let(:integration_params) { { active: false } }
let(:message) { 'Jira settings saved, but not active.' }
2016-09-29 09:46:39 +05:30
2021-09-30 23:02:18 +05:30
it_behaves_like 'integration update'
2019-12-04 20:38:33 +05:30
end
2020-07-28 23:09:34 +05:30
2021-09-30 23:02:18 +05:30
context 'when param `inherit_from_id` is set to empty string' do
let(:integration_params) { { inherit_from_id: '' } }
2020-07-28 23:09:34 +05:30
it 'sets inherit_from_id to nil' do
2021-09-30 23:02:18 +05:30
expect(integration.reload.inherit_from_id).to eq(nil)
2020-07-28 23:09:34 +05:30
end
end
2021-10-27 15:23:28 +05:30
context 'when param `inherit_from_id` is set to an instance integration' do
let(:instance_integration) { create(:jira_integration, :instance, url: 'http://instance.com', password: 'instance') }
let(:integration_params) { { inherit_from_id: instance_integration.id, url: 'http://custom.com', password: 'custom' } }
it 'ignores submitted params and inherits instance settings' do
expect(integration.reload).to have_attributes(
inherit_from_id: instance_integration.id,
url: instance_integration.url,
password: instance_integration.password
)
end
end
context 'when param `inherit_from_id` is set to a group integration' do
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, group: group) }
let_it_be(:jira_integration) { create(:jira_integration, project: project) }
2020-07-28 23:09:34 +05:30
2021-12-11 22:18:48 +05:30
let(:group_integration) { create(:jira_integration, :group, group: group, url: 'http://group.com', password: 'group') }
2021-10-27 15:23:28 +05:30
let(:integration_params) { { inherit_from_id: group_integration.id, url: 'http://custom.com', password: 'custom' } }
it 'ignores submitted params and inherits group settings' do
expect(integration.reload).to have_attributes(
inherit_from_id: group_integration.id,
url: group_integration.url,
password: group_integration.password
)
end
end
context 'when param `inherit_from_id` is set to an unrelated group' do
let_it_be(:group) { create(:group) }
2021-12-11 22:18:48 +05:30
let(:group_integration) { create(:jira_integration, :group, group: group, url: 'http://group.com', password: 'group') }
2021-10-27 15:23:28 +05:30
let(:integration_params) { { inherit_from_id: group_integration.id, url: 'http://custom.com', password: 'custom' } }
it 'ignores the param and saves the submitted settings' do
expect(integration.reload).to have_attributes(
inherit_from_id: nil,
url: 'http://custom.com',
password: 'custom'
)
2020-07-28 23:09:34 +05:30
end
end
2016-09-29 09:46:39 +05:30
end
2018-03-17 18:26:18 +05:30
2019-12-04 20:38:33 +05:30
describe 'as JSON' do
before do
2021-09-30 23:02:18 +05:30
stub_jira_integration_test
put :update, params: project_params(service: integration_params, format: :json)
2019-12-04 20:38:33 +05:30
end
context 'when update succeeds' do
2021-10-27 15:23:28 +05:30
let(:integration_params) { { url: 'http://example.com', password: 'password' } }
2019-12-04 20:38:33 +05:30
2021-10-27 15:23:28 +05:30
it 'returns success response' do
2019-12-04 20:38:33 +05:30
expect(response).to be_successful
2021-10-27 15:23:28 +05:30
expect(json_response).to include(
'active' => true,
'errors' => {}
)
end
end
context 'when update fails with missing password' do
let(:integration_params) { { url: 'http://example.com' } }
it 'returns JSON response errors' do
expect(response).not_to be_successful
expect(json_response).to include(
'active' => true,
'errors' => {
'password' => ["can't be blank"]
}
)
2019-12-04 20:38:33 +05:30
end
end
2019-03-02 22:35:43 +05:30
2021-10-27 15:23:28 +05:30
context 'when update fails with invalid URL' do
let(:integration_params) { { url: '', password: 'password' } }
2019-03-02 22:35:43 +05:30
2019-12-04 20:38:33 +05:30
it 'returns JSON response with errors' do
expect(response).to have_gitlab_http_status(:unprocessable_entity)
expect(json_response).to include(
'active' => true,
2021-10-27 15:23:28 +05:30
'errors' => { 'url' => ['must be a valid URL', "can't be blank"] }
2019-12-04 20:38:33 +05:30
)
end
2019-03-02 22:35:43 +05:30
end
end
2020-03-13 15:44:24 +05:30
2021-09-30 23:02:18 +05:30
context 'with Prometheus integration' do
let_it_be(:prometheus_integration) { create(:prometheus_integration, project: project) }
2020-03-13 15:44:24 +05:30
2021-09-30 23:02:18 +05:30
let(:integration) { prometheus_integration }
let(:integration_params) { { manual_configuration: '1', api_url: 'http://example.com' } }
context 'when feature flag :settings_operations_prometheus_service is enabled' do
2020-03-13 15:44:24 +05:30
before do
stub_feature_flags(settings_operations_prometheus_service: true)
end
it 'redirects user back to edit page with alert' do
2021-09-30 23:02:18 +05:30
put :update, params: project_params.merge(service: integration_params)
2020-03-13 15:44:24 +05:30
2022-03-02 08:16:31 +05:30
expect(response).to redirect_to(edit_project_integration_path(project, integration))
2021-09-30 23:02:18 +05:30
expected_alert = [
"You can now manage your Prometheus settings on the",
%(<a href="#{project_settings_operations_path(project)}">Operations</a> page.),
"Fields on this page have been deprecated."
].join(' ')
2020-03-13 15:44:24 +05:30
2021-06-08 01:23:25 +05:30
expect(controller).to set_flash.now[:alert].to(expected_alert)
2020-03-13 15:44:24 +05:30
end
2021-09-30 23:02:18 +05:30
it 'does not modify integration' do
expect { put :update, params: project_params.merge(service: integration_params) }
.not_to change { project.prometheus_integration.reload.attributes }
2020-03-13 15:44:24 +05:30
end
end
2021-09-30 23:02:18 +05:30
context 'when feature flag :settings_operations_prometheus_service is disabled' do
2020-03-13 15:44:24 +05:30
before do
stub_feature_flags(settings_operations_prometheus_service: false)
end
2021-09-30 23:02:18 +05:30
it 'modifies integration' do
expect { put :update, params: project_params.merge(service: integration_params) }
.to change { project.prometheus_integration.reload.attributes }
2020-03-13 15:44:24 +05:30
end
end
end
2018-03-17 18:26:18 +05:30
end
2019-12-04 20:38:33 +05:30
describe 'GET #edit' do
2021-09-30 23:02:18 +05:30
context 'with Jira service' do
let(:integration_param) { 'jira' }
2020-03-13 15:44:24 +05:30
before do
2021-09-30 23:02:18 +05:30
get :edit, params: project_params(id: integration_param)
2020-03-13 15:44:24 +05:30
end
context 'with approved services' do
it 'renders edit page' do
expect(response).to be_successful
end
end
2018-03-17 18:26:18 +05:30
end
2021-09-30 23:02:18 +05:30
context 'with Prometheus service' do
let(:integration_param) { 'prometheus' }
2020-03-13 15:44:24 +05:30
2021-09-30 23:02:18 +05:30
context 'when feature flag :settings_operations_prometheus_service is enabled' do
2020-03-13 15:44:24 +05:30
before do
stub_feature_flags(settings_operations_prometheus_service: true)
2021-09-30 23:02:18 +05:30
get :edit, params: project_params(id: integration_param)
2020-03-13 15:44:24 +05:30
end
it 'renders deprecation warning notice' do
2021-09-30 23:02:18 +05:30
expected_alert = [
"You can now manage your Prometheus settings on the",
%(<a href="#{project_settings_operations_path(project)}">Operations</a> page.),
"Fields on this page have been deprecated."
].join(' ')
2021-06-08 01:23:25 +05:30
expect(controller).to set_flash.now[:alert].to(expected_alert)
2020-03-13 15:44:24 +05:30
end
end
2021-09-30 23:02:18 +05:30
context 'when feature flag :settings_operations_prometheus_service is disabled' do
2020-03-13 15:44:24 +05:30
before do
stub_feature_flags(settings_operations_prometheus_service: false)
2021-09-30 23:02:18 +05:30
get :edit, params: project_params(id: integration_param)
2020-03-13 15:44:24 +05:30
end
it 'does not render deprecation warning notice' do
2021-06-08 01:23:25 +05:30
expect(controller).not_to set_flash.now[:alert]
2020-03-13 15:44:24 +05:30
end
2018-03-17 18:26:18 +05:30
end
end
2016-09-29 09:46:39 +05:30
end
2019-12-04 20:38:33 +05:30
private
def project_params(opts = {})
opts.reverse_merge(
namespace_id: project.namespace,
project_id: project,
2021-09-30 23:02:18 +05:30
id: integration.to_param
2019-12-04 20:38:33 +05:30
)
end
2015-09-11 14:41:01 +05:30
end