Commit b39f2900 authored by Markus Koller's avatar Markus Koller

Merge branch 'skr-networks' into 'master'

Catch network errors when testing integration

See merge request gitlab-org/gitlab!56457
parents 82f32b6f f8249880
......@@ -71,7 +71,7 @@ class Projects::ServicesController < Projects::ApplicationController
end
result[:data].presence || {}
rescue Gitlab::HTTP::BlockedUrlError => e
rescue *Gitlab::HTTP::HTTP_ERRORS => e
{ error: true, message: s_('Integrations|Connection failed. Please check your settings.'), service_response: e.message, test_failed: true }
end
......
---
title: Catch network errors
merge_request: 56457
author: Shubham Kumar (@imskr)
type: fixed
......@@ -4,6 +4,7 @@ require 'spec_helper'
RSpec.describe Projects::ServicesController do
include JiraServiceHelper
include AfterNextHelpers
let(:project) { create(:project, :repository) }
let(:user) { create(:user) }
......@@ -13,7 +14,6 @@ RSpec.describe Projects::ServicesController do
before do
sign_in(user)
project.add_maintainer(user)
allow(Gitlab::UrlBlocker).to receive(:validate!).and_return([URI.parse('http://example.com'), nil])
end
describe '#test' do
......@@ -114,7 +114,7 @@ RSpec.describe Projects::ServicesController do
end
context 'failure' do
it 'returns success status code and the error message' do
it 'returns an error response when the integration test fails' do
stub_request(:get, 'http://example.com/rest/api/2/serverInfo')
.to_return(status: 404)
......@@ -128,6 +128,36 @@ RSpec.describe Projects::ServicesController do
'test_failed' => true
)
end
context 'with the Slack integration' do
let_it_be(:service) { build(:slack_service) }
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
expect_next(SlackService).to receive(:test).and_raise(Errno::ECONNREFUSED)
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
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