2012-10-09 04:14:17 -04:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: web_hooks
|
|
|
|
#
|
2013-12-03 12:11:10 -05:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# url :string(255)
|
|
|
|
# project_id :integer
|
2014-04-09 08:05:03 -04:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2013-12-03 12:11:10 -05:00
|
|
|
# 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
|
2014-04-09 08:05:03 -04:00
|
|
|
# tag_push_events :boolean default(FALSE)
|
2015-05-16 02:33:31 -04:00
|
|
|
# note_events :boolean default(FALSE), not null
|
2012-10-09 04:14:17 -04:00
|
|
|
#
|
|
|
|
|
2011-12-14 11:38:52 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 04:50:51 -05:00
|
|
|
describe ProjectHook, models: true do
|
2011-12-14 11:38:52 -05:00
|
|
|
describe "Associations" do
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to belong_to :project }
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
|
|
|
|
2012-09-26 14:17:17 -04:00
|
|
|
describe "Mass assignment" do
|
|
|
|
end
|
|
|
|
|
2011-12-14 11:38:52 -05:00
|
|
|
describe "Validations" do
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to validate_presence_of(:url) }
|
2011-12-14 11:38:52 -05:00
|
|
|
|
|
|
|
context "url format" do
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to allow_value("http://example.com").for(:url) }
|
|
|
|
it { is_expected.to allow_value("https://excample.com").for(:url) }
|
|
|
|
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) }
|
2011-12-14 11:38:52 -05:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
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) }
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "execute" do
|
|
|
|
before(:each) do
|
2012-11-05 22:31:55 -05:00
|
|
|
@project_hook = create(:project_hook)
|
|
|
|
@project = create(:project)
|
2012-07-15 10:36:06 -04:00
|
|
|
@project.hooks << [@project_hook]
|
2015-06-22 14:41:00 -04:00
|
|
|
@data = { before: 'oldrev', after: 'newrev', ref: 'ref' }
|
2011-12-14 11:38:52 -05:00
|
|
|
|
2012-07-15 10:36:06 -04:00
|
|
|
WebMock.stub_request(:post, @project_hook.url)
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "POSTs to the web hook URL" do
|
2015-01-23 19:10:43 -05:00
|
|
|
@project_hook.execute(@data, 'push_hooks')
|
|
|
|
expect(WebMock).to have_requested(:post, @project_hook.url).with(
|
2015-06-22 14:41:00 -04:00
|
|
|
headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'Push Hook' }
|
2015-01-23 19:10:43 -05:00
|
|
|
).once
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "POSTs the data as JSON" do
|
2015-01-23 19:10:43 -05:00
|
|
|
@project_hook.execute(@data, 'push_hooks')
|
|
|
|
expect(WebMock).to have_requested(:post, @project_hook.url).with(
|
2015-06-22 14:41:00 -04:00
|
|
|
headers: { 'Content-Type'=>'application/json', 'X-Gitlab-Event'=>'Push Hook' }
|
2015-01-23 19:10:43 -05:00
|
|
|
).once
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "catches exceptions" do
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(WebHook).to receive(:post).and_raise("Some HTTP Post error")
|
2011-12-14 11:38:52 -05:00
|
|
|
|
2015-06-23 04:44:03 -04:00
|
|
|
expect { @project_hook.execute(@data, 'push_hooks') }.to raise_error(RuntimeError)
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
2015-12-01 19:15:01 -05:00
|
|
|
|
|
|
|
it "handles SSL exceptions" do
|
|
|
|
expect(WebHook).to receive(:post).and_raise(OpenSSL::SSL::SSLError.new('SSL error'))
|
|
|
|
|
|
|
|
expect(@project_hook.execute(@data, 'push_hooks')).to eq([false, 'SSL error'])
|
|
|
|
end
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
|
|
|
end
|