2014-09-02 18:07:02 +05:30
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: web_hooks
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# url :string(255)
|
|
|
|
# project_id :integer
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# type :string(255) default("ProjectHook")
|
|
|
|
# service_id :integer
|
|
|
|
# push_events :boolean default(TRUE), not null
|
|
|
|
# issues_events :boolean default(FALSE), not null
|
|
|
|
# merge_requests_events :boolean default(FALSE), not null
|
|
|
|
# tag_push_events :boolean default(FALSE)
|
2015-09-11 14:41:01 +05:30
|
|
|
# note_events :boolean default(FALSE), not null
|
2014-09-02 18:07:02 +05:30
|
|
|
#
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
describe WebHook, models: true do
|
2014-09-02 18:07:02 +05:30
|
|
|
describe "Validations" do
|
2015-04-26 12:48:37 +05:30
|
|
|
it { is_expected.to validate_presence_of(:url) }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
describe 'url' do
|
2015-04-26 12:48:37 +05:30
|
|
|
it { is_expected.to allow_value("http://example.com").for(:url) }
|
2016-06-02 11:05:42 +05:30
|
|
|
it { is_expected.to allow_value("https://example.com").for(:url) }
|
|
|
|
it { is_expected.to allow_value(" https://example.com ").for(:url) }
|
2015-04-26 12:48:37 +05:30
|
|
|
it { is_expected.to allow_value("http://test.com/api").for(:url) }
|
|
|
|
it { is_expected.to allow_value("http://test.com/api?key=abc").for(:url) }
|
|
|
|
it { is_expected.to allow_value("http://test.com/api?key=abc&type=def").for(:url) }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
it { is_expected.not_to allow_value("example.com").for(:url) }
|
|
|
|
it { is_expected.not_to allow_value("ftp://example.com").for(:url) }
|
|
|
|
it { is_expected.not_to allow_value("herp-and-derp").for(:url) }
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
it 'strips :url before saving it' do
|
|
|
|
hook = create(:project_hook, url: ' https://example.com ')
|
|
|
|
|
|
|
|
expect(hook.url).to eq('https://example.com')
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "execute" do
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:project_hook) { create(:project_hook) }
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
before(:each) do
|
2016-06-02 11:05:42 +05:30
|
|
|
project.hooks << [project_hook]
|
2015-09-11 14:41:01 +05:30
|
|
|
@data = { before: 'oldrev', after: 'newrev', ref: 'ref' }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
WebMock.stub_request(:post, project_hook.url)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when token is defined' do
|
|
|
|
let(:project_hook) { create(:project_hook, :token) }
|
|
|
|
|
|
|
|
it 'POSTs to the webhook URL' do
|
|
|
|
project_hook.execute(@data, 'push_hooks')
|
|
|
|
expect(WebMock).to have_requested(:post, project_hook.url).with(
|
|
|
|
headers: { 'Content-Type' => 'application/json',
|
|
|
|
'X-Gitlab-Event' => 'Push Hook',
|
|
|
|
'X-Gitlab-Token' => project_hook.token }
|
|
|
|
).once
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
it "POSTs to the webhook URL" do
|
|
|
|
project_hook.execute(@data, 'push_hooks')
|
|
|
|
expect(WebMock).to have_requested(:post, project_hook.url).with(
|
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'Push Hook' }
|
2015-09-11 14:41:01 +05:30
|
|
|
).once
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "POSTs the data as JSON" do
|
2016-06-02 11:05:42 +05:30
|
|
|
project_hook.execute(@data, 'push_hooks')
|
|
|
|
expect(WebMock).to have_requested(:post, project_hook.url).with(
|
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'Push Hook' }
|
2015-09-11 14:41:01 +05:30
|
|
|
).once
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "catches exceptions" do
|
2015-04-26 12:48:37 +05:30
|
|
|
expect(WebHook).to receive(:post).and_raise("Some HTTP Post error")
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
expect { project_hook.execute(@data, 'push_hooks') }.to raise_error(RuntimeError)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
|
|
|
it "handles SSL exceptions" do
|
|
|
|
expect(WebHook).to receive(:post).and_raise(OpenSSL::SSL::SSLError.new('SSL error'))
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
expect(project_hook.execute(@data, 'push_hooks')).to eq([false, 'SSL error'])
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
it "handles 200 status code" do
|
2016-06-02 11:05:42 +05:30
|
|
|
WebMock.stub_request(:post, project_hook.url).to_return(status: 200, body: "Success")
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
expect(project_hook.execute(@data, 'push_hooks')).to eq([200, 'Success'])
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "handles 2xx status codes" do
|
2016-06-02 11:05:42 +05:30
|
|
|
WebMock.stub_request(:post, project_hook.url).to_return(status: 201, body: "Success")
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
expect(project_hook.execute(@data, 'push_hooks')).to eq([201, 'Success'])
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|