Added web hooks functionality
This commit includes: * Projects can have zero or more WebHooks. * The PostReceive job will ask a project to execute any web hooks defined for that project. * WebHook has a URL, we post Github-compatible JSON to that URL. * Failure to execute a WebHook will be silently ignored.
This commit is contained in:
parent
56fc53e8d8
commit
edab46e9fa
14 changed files with 295 additions and 4 deletions
54
spec/models/web_hook_spec.rb
Normal file
54
spec/models/web_hook_spec.rb
Normal file
|
@ -0,0 +1,54 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe WebHook do
|
||||
describe "Associations" do
|
||||
it { should belong_to :project }
|
||||
end
|
||||
|
||||
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
|
||||
@webhook = Factory :web_hook
|
||||
@project = Factory :project
|
||||
@project.web_hooks << [@webhook]
|
||||
@data = { before: 'oldrev', after: 'newrev', ref: 'ref'}
|
||||
|
||||
WebMock.stub_request(:post, @webhook.url)
|
||||
end
|
||||
|
||||
it "POSTs to the web hook URL" do
|
||||
@webhook.execute(@data)
|
||||
WebMock.should have_requested(:post, @webhook.url).once
|
||||
end
|
||||
|
||||
it "POSTs the data as JSON" do
|
||||
json = @data.to_json
|
||||
|
||||
@webhook.execute(@data)
|
||||
WebMock.should have_requested(:post, @webhook.url).with(body: json).once
|
||||
end
|
||||
|
||||
it "catches exceptions" do
|
||||
WebHook.should_receive(:post).and_raise("Some HTTP Post error")
|
||||
|
||||
lambda {
|
||||
@webhook.execute(@data)
|
||||
}.should_not raise_error
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue