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)
|
2012-10-09 04:14:17 -04:00
|
|
|
#
|
|
|
|
|
2011-12-14 11:38:52 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2012-07-15 10:36:06 -04:00
|
|
|
describe ProjectHook do
|
2011-12-14 11:38:52 -05:00
|
|
|
describe "Associations" do
|
|
|
|
it { should belong_to :project }
|
|
|
|
end
|
|
|
|
|
2012-09-26 14:17:17 -04:00
|
|
|
describe "Mass assignment" do
|
|
|
|
it { should_not allow_mass_assignment_of(:project_id) }
|
|
|
|
end
|
|
|
|
|
2011-12-14 11:38:52 -05:00
|
|
|
describe "Validations" do
|
|
|
|
it { should validate_presence_of(:url) }
|
|
|
|
|
|
|
|
context "url format" do
|
|
|
|
it { should allow_value("http://example.com").for(:url) }
|
|
|
|
it { should allow_value("https://excample.com").for(:url) }
|
|
|
|
it { should allow_value("http://test.com/api").for(:url) }
|
|
|
|
it { should allow_value("http://test.com/api?key=abc").for(:url) }
|
|
|
|
it { should allow_value("http://test.com/api?key=abc&type=def").for(:url) }
|
|
|
|
|
|
|
|
it { should_not allow_value("example.com").for(:url) }
|
|
|
|
it { should_not allow_value("ftp://example.com").for(:url) }
|
|
|
|
it { should_not allow_value("herp-and-derp").for(:url) }
|
|
|
|
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]
|
2011-12-14 11:38:52 -05:00
|
|
|
@data = { before: 'oldrev', after: 'newrev', ref: 'ref'}
|
|
|
|
|
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
|
2012-07-15 10:36:06 -04:00
|
|
|
@project_hook.execute(@data)
|
|
|
|
WebMock.should have_requested(:post, @project_hook.url).once
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "POSTs the data as JSON" do
|
|
|
|
json = @data.to_json
|
|
|
|
|
2012-07-15 10:36:06 -04:00
|
|
|
@project_hook.execute(@data)
|
|
|
|
WebMock.should have_requested(:post, @project_hook.url).with(body: json).once
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "catches exceptions" do
|
|
|
|
WebHook.should_receive(:post).and_raise("Some HTTP Post error")
|
|
|
|
|
|
|
|
lambda {
|
2012-07-15 10:36:06 -04:00
|
|
|
@project_hook.execute(@data)
|
|
|
|
}.should raise_error
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|