gitlab-org--gitlab-foss/spec/models/ci/web_hook_spec.rb

63 lines
1.8 KiB
Ruby
Raw Normal View History

2015-08-26 01:42:46 +00:00
# == Schema Information
#
# Table name: web_hooks
#
# id :integer not null, primary key
# url :string(255) not null
# project_id :integer not null
# created_at :datetime
# updated_at :datetime
#
require 'spec_helper'
2015-09-09 12:17:16 +00:00
describe Ci::WebHook do
2015-08-26 01:42:46 +00:00
describe "Associations" do
2015-09-10 13:52:52 +00:00
it { is_expected.to belong_to :project }
2015-08-26 01:42:46 +00:00
end
describe "Validations" do
2015-09-10 13:52:52 +00:00
it { is_expected.to validate_presence_of(:url) }
2015-08-26 01:42:46 +00:00
context "url format" do
2015-09-10 13:52:52 +00: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) }
2015-08-26 01:42:46 +00:00
2015-09-10 13:52:52 +00: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) }
2015-08-26 01:42:46 +00:00
end
end
describe "execute" do
before(:each) do
2015-09-10 13:47:15 +00:00
@web_hook = FactoryGirl.create(:ci_web_hook)
2015-08-26 01:42:46 +00:00
@project = @web_hook.project
2015-09-15 12:51:03 +00:00
@data = { before: 'oldrev', after: 'newrev', ref: 'ref' }
2015-08-26 01:42:46 +00:00
WebMock.stub_request(:post, @web_hook.url)
end
it "POSTs to the web hook URL" do
@web_hook.execute(@data)
2015-09-10 13:52:52 +00:00
expect(WebMock).to have_requested(:post, @web_hook.url).once
2015-08-26 01:42:46 +00:00
end
it "POSTs the data as JSON" do
json = @data.to_json
@web_hook.execute(@data)
2015-09-10 13:52:52 +00:00
expect(WebMock).to have_requested(:post, @web_hook.url).with(body: json).once
2015-08-26 01:42:46 +00:00
end
it "catches exceptions" do
2015-09-10 13:52:52 +00:00
expect(WebHook).to receive(:post).and_raise("Some HTTP Post error")
2015-08-26 01:42:46 +00:00
2015-09-15 12:51:03 +00:00
expect{ @web_hook.execute(@data) }.to raise_error
2015-08-26 01:42:46 +00:00
end
end
end