2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe WebHookService do
|
2019-04-21 06:03:26 -04:00
|
|
|
include StubRequests
|
|
|
|
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2017-04-27 06:08:57 -04:00
|
|
|
let(:project_hook) { create(:project_hook) }
|
|
|
|
let(:headers) do
|
|
|
|
{
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'X-Gitlab-Event' => 'Push Hook'
|
|
|
|
}
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
let(:data) do
|
|
|
|
{ before: 'oldrev', after: 'newrev', ref: 'ref' }
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2017-09-07 07:29:12 -04:00
|
|
|
let(:service_instance) { described_class.new(project_hook, data, :push_hooks) }
|
2017-04-27 06:08:57 -04:00
|
|
|
|
2018-03-13 18:38:25 -04:00
|
|
|
describe '#initialize' do
|
2019-07-26 09:03:06 -04:00
|
|
|
before do
|
|
|
|
stub_application_setting(setting_name => setting)
|
|
|
|
end
|
|
|
|
|
2019-07-30 06:53:23 -04:00
|
|
|
shared_examples_for 'respects outbound network setting' do
|
|
|
|
context 'when local requests are allowed' do
|
2019-07-26 09:03:06 -04:00
|
|
|
let(:setting) { true }
|
2019-07-26 06:21:52 -04:00
|
|
|
|
2019-07-26 09:03:06 -04:00
|
|
|
it { expect(hook.request_options[:allow_local_requests]).to be_truthy }
|
2019-07-26 06:21:52 -04:00
|
|
|
end
|
|
|
|
|
2019-07-30 06:53:23 -04:00
|
|
|
context 'when local requests are not allowed' do
|
2019-07-26 09:03:06 -04:00
|
|
|
let(:setting) { false }
|
2019-07-26 06:21:52 -04:00
|
|
|
|
2019-07-26 09:03:06 -04:00
|
|
|
it { expect(hook.request_options[:allow_local_requests]).to be_falsey }
|
2019-07-26 06:21:52 -04:00
|
|
|
end
|
2019-07-26 09:03:06 -04:00
|
|
|
end
|
2019-07-26 06:21:52 -04:00
|
|
|
|
2019-07-26 09:03:06 -04:00
|
|
|
context 'when SystemHook' do
|
|
|
|
let(:setting_name) { :allow_local_requests_from_system_hooks }
|
|
|
|
let(:hook) { described_class.new(build(:system_hook), data, :system_hook) }
|
2019-07-26 06:21:52 -04:00
|
|
|
|
2019-07-30 06:53:23 -04:00
|
|
|
include_examples 'respects outbound network setting'
|
2019-07-26 09:03:06 -04:00
|
|
|
end
|
2019-07-26 06:21:52 -04:00
|
|
|
|
2019-07-26 09:03:06 -04:00
|
|
|
context 'when ProjectHook' do
|
|
|
|
let(:setting_name) { :allow_local_requests_from_web_hooks_and_services }
|
|
|
|
let(:hook) { described_class.new(build(:project_hook), data, :project_hook) }
|
2018-03-13 18:38:25 -04:00
|
|
|
|
2019-07-30 06:53:23 -04:00
|
|
|
include_examples 'respects outbound network setting'
|
2018-03-13 18:38:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
describe '#execute' do
|
2017-08-10 18:31:42 -04:00
|
|
|
before do
|
2017-04-27 06:08:57 -04:00
|
|
|
project.hooks << [project_hook]
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when token is defined' do
|
|
|
|
let(:project_hook) { create(:project_hook, :token) }
|
|
|
|
|
|
|
|
it 'POSTs to the webhook URL' do
|
2019-08-27 05:04:27 -04:00
|
|
|
stub_full_request(project_hook.url, method: :post)
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
service_instance.execute
|
2019-08-27 05:04:27 -04:00
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, stubbed_hostname(project_hook.url)).with(
|
2017-04-27 06:08:57 -04:00
|
|
|
headers: headers.merge({ 'X-Gitlab-Token' => project_hook.token })
|
|
|
|
).once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'POSTs the data as JSON' do
|
2019-08-27 05:04:27 -04:00
|
|
|
stub_full_request(project_hook.url, method: :post)
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
service_instance.execute
|
2019-08-27 05:04:27 -04:00
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, stubbed_hostname(project_hook.url)).with(
|
2017-04-27 06:08:57 -04:00
|
|
|
headers: headers
|
|
|
|
).once
|
|
|
|
end
|
|
|
|
|
2018-06-25 10:44:29 -04:00
|
|
|
context 'when auth credentials are present' do
|
2019-01-16 07:09:29 -05:00
|
|
|
let(:url) {'https://example.org'}
|
2018-06-25 10:44:29 -04:00
|
|
|
let(:project_hook) { create(:project_hook, url: 'https://demo:demo@example.org/') }
|
|
|
|
|
|
|
|
it 'uses the credentials' do
|
2019-04-21 06:03:26 -04:00
|
|
|
stub_full_request(url, method: :post)
|
2018-06-25 10:44:29 -04:00
|
|
|
|
|
|
|
service_instance.execute
|
|
|
|
|
2019-04-21 06:03:26 -04:00
|
|
|
expect(WebMock).to have_requested(:post, stubbed_hostname(url)).with(
|
2018-06-25 10:44:29 -04:00
|
|
|
headers: headers.merge('Authorization' => 'Basic ZGVtbzpkZW1v')
|
|
|
|
).once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when auth credentials are partial present' do
|
2019-01-16 07:09:29 -05:00
|
|
|
let(:url) {'https://example.org'}
|
2018-06-25 10:44:29 -04:00
|
|
|
let(:project_hook) { create(:project_hook, url: 'https://demo@example.org/') }
|
|
|
|
|
|
|
|
it 'uses the credentials anyways' do
|
2019-04-21 06:03:26 -04:00
|
|
|
stub_full_request(url, method: :post)
|
2018-06-25 10:44:29 -04:00
|
|
|
|
|
|
|
service_instance.execute
|
|
|
|
|
2019-04-21 06:03:26 -04:00
|
|
|
expect(WebMock).to have_requested(:post, stubbed_hostname(url)).with(
|
2018-06-25 10:44:29 -04:00
|
|
|
headers: headers.merge('Authorization' => 'Basic ZGVtbzo=')
|
|
|
|
).once
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
it 'catches exceptions' do
|
2019-08-27 05:04:27 -04:00
|
|
|
stub_full_request(project_hook.url, method: :post).to_raise(StandardError.new('Some error'))
|
2017-04-27 06:08:57 -04:00
|
|
|
|
|
|
|
expect { service_instance.execute }.to raise_error(StandardError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles exceptions' do
|
2018-10-19 01:55:06 -04:00
|
|
|
exceptions = [SocketError, OpenSSL::SSL::SSLError, Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Net::OpenTimeout, Net::ReadTimeout, Gitlab::HTTP::BlockedUrlError, Gitlab::HTTP::RedirectionTooDeep]
|
2017-04-27 06:08:57 -04:00
|
|
|
exceptions.each do |exception_class|
|
|
|
|
exception = exception_class.new('Exception message')
|
|
|
|
|
2019-08-27 05:04:27 -04:00
|
|
|
stub_full_request(project_hook.url, method: :post).to_raise(exception)
|
2019-02-16 17:36:04 -05:00
|
|
|
expect(service_instance.execute).to eq({ status: :error, message: exception.to_s })
|
2017-04-27 06:08:57 -04:00
|
|
|
expect { service_instance.execute }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles 200 status code' do
|
2019-08-27 05:04:27 -04:00
|
|
|
stub_full_request(project_hook.url, method: :post).to_return(status: 200, body: 'Success')
|
2017-04-27 06:08:57 -04:00
|
|
|
|
2017-07-20 11:12:06 -04:00
|
|
|
expect(service_instance.execute).to include({ status: :success, http_status: 200, message: 'Success' })
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles 2xx status codes' do
|
2019-08-27 05:04:27 -04:00
|
|
|
stub_full_request(project_hook.url, method: :post).to_return(status: 201, body: 'Success')
|
2017-04-27 06:08:57 -04:00
|
|
|
|
2017-07-20 11:12:06 -04:00
|
|
|
expect(service_instance.execute).to include({ status: :success, http_status: 201, message: 'Success' })
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'execution logging' do
|
|
|
|
let(:hook_log) { project_hook.web_hook_logs.last }
|
|
|
|
|
|
|
|
context 'with success' do
|
|
|
|
before do
|
2019-08-27 05:04:27 -04:00
|
|
|
stub_full_request(project_hook.url, method: :post).to_return(status: 200, body: 'Success')
|
2017-04-27 06:08:57 -04:00
|
|
|
service_instance.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'log successful execution' do
|
|
|
|
expect(hook_log.trigger).to eq('push_hooks')
|
|
|
|
expect(hook_log.url).to eq(project_hook.url)
|
|
|
|
expect(hook_log.request_headers).to eq(headers)
|
|
|
|
expect(hook_log.response_body).to eq('Success')
|
|
|
|
expect(hook_log.response_status).to eq('200')
|
|
|
|
expect(hook_log.execution_duration).to be > 0
|
|
|
|
expect(hook_log.internal_error_message).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with exception' do
|
|
|
|
before do
|
2019-08-27 05:04:27 -04:00
|
|
|
stub_full_request(project_hook.url, method: :post).to_raise(SocketError.new('Some HTTP Post error'))
|
2017-04-27 06:08:57 -04:00
|
|
|
service_instance.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'log failed execution' do
|
|
|
|
expect(hook_log.trigger).to eq('push_hooks')
|
|
|
|
expect(hook_log.url).to eq(project_hook.url)
|
|
|
|
expect(hook_log.request_headers).to eq(headers)
|
|
|
|
expect(hook_log.response_body).to eq('')
|
|
|
|
expect(hook_log.response_status).to eq('internal error')
|
|
|
|
expect(hook_log.execution_duration).to be > 0
|
|
|
|
expect(hook_log.internal_error_message).to eq('Some HTTP Post error')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-01 10:30:29 -04:00
|
|
|
context 'with unsafe response body' do
|
|
|
|
before do
|
2019-08-27 05:04:27 -04:00
|
|
|
stub_full_request(project_hook.url, method: :post).to_return(status: 200, body: "\xBB")
|
2017-08-01 10:30:29 -04:00
|
|
|
service_instance.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'log successful execution' do
|
|
|
|
expect(hook_log.trigger).to eq('push_hooks')
|
|
|
|
expect(hook_log.url).to eq(project_hook.url)
|
|
|
|
expect(hook_log.request_headers).to eq(headers)
|
|
|
|
expect(hook_log.response_body).to eq('')
|
|
|
|
expect(hook_log.response_status).to eq('200')
|
|
|
|
expect(hook_log.execution_duration).to be > 0
|
|
|
|
expect(hook_log.internal_error_message).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#async_execute' do
|
|
|
|
let(:system_hook) { create(:system_hook) }
|
2017-08-15 13:44:37 -04:00
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
it 'enqueue WebHookWorker' do
|
2017-11-29 10:30:17 -05:00
|
|
|
expect(WebHookWorker).to receive(:perform_async).with(project_hook.id, data, 'push_hooks')
|
2017-04-27 06:08:57 -04:00
|
|
|
|
2017-07-25 13:09:00 -04:00
|
|
|
described_class.new(project_hook, data, 'push_hooks').async_execute
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|