System hooks: fix broken tests

This commit is contained in:
Valeriy Sizov 2012-07-15 17:36:06 +03:00
parent f5908cef19
commit 655418bed2
8 changed files with 39 additions and 38 deletions

View file

@ -27,7 +27,7 @@ module GitPush
true true
end end
def execute_web_hooks(oldrev, newrev, ref, user) def execute_hooks(oldrev, newrev, ref, user)
ref_parts = ref.split('/') ref_parts = ref.split('/')
# Return if this is not a push to a branch (e.g. new commits) # Return if this is not a push to a branch (e.g. new commits)
@ -35,7 +35,7 @@ module GitPush
data = post_receive_data(oldrev, newrev, ref, user) data = post_receive_data(oldrev, newrev, ref, user)
hooks.each { |web_hook| web_hook.execute(data) } hooks.each { |hook| hook.execute(data) }
end end
def post_receive_data(oldrev, newrev, ref, user) def post_receive_data(oldrev, newrev, ref, user)
@ -97,7 +97,7 @@ module GitPush
self.update_merge_requests(oldrev, newrev, ref, user) self.update_merge_requests(oldrev, newrev, ref, user)
# Execute web hooks # Execute web hooks
self.execute_web_hooks(oldrev, newrev, ref, user) self.execute_hooks(oldrev, newrev, ref, user)
# Create satellite # Create satellite
self.satellite.create unless self.satellite.exists? self.satellite.create unless self.satellite.exists?

View file

@ -60,7 +60,7 @@ Factory.add(:key, Key) do |obj|
obj.key = File.read(File.join(Rails.root, "db", "pkey.example")) obj.key = File.read(File.join(Rails.root, "db", "pkey.example"))
end end
Factory.add(:web_hook, WebHook) do |obj| Factory.add(:project_hook, ProjectHook) do |obj|
obj.url = Faker::Internet.uri("http") obj.url = Faker::Internet.uri("http")
end end

View file

@ -21,44 +21,44 @@ describe Project, "Hooks" do
end end
end end
describe "Web hooks" do describe "Project hooks" do
context "with no web hooks" do context "with no web hooks" do
it "raises no errors" do it "raises no errors" do
lambda { lambda {
project.execute_web_hooks('oldrev', 'newrev', 'ref', @user) project.execute_hooks('oldrev', 'newrev', 'ref', @user)
}.should_not raise_error }.should_not raise_error
end end
end end
context "with web hooks" do context "with web hooks" do
before do before do
@webhook = Factory(:web_hook) @project_hook = Factory(:project_hook)
@webhook_2 = Factory(:web_hook) @project_hook_2 = Factory(:project_hook)
project.web_hooks << [@webhook, @webhook_2] project.hooks << [@project_hook, @project_hook_2]
end end
it "executes multiple web hook" do it "executes multiple web hook" do
@webhook.should_receive(:execute).once @project_hook.should_receive(:execute).once
@webhook_2.should_receive(:execute).once @project_hook_2.should_receive(:execute).once
project.execute_web_hooks('oldrev', 'newrev', 'refs/heads/master', @user) project.execute_hooks('oldrev', 'newrev', 'refs/heads/master', @user)
end end
end end
context "does not execute web hooks" do context "does not execute web hooks" do
before do before do
@webhook = Factory(:web_hook) @project_hook = Factory(:project_hook)
project.web_hooks << [@webhook] project.hooks << [@project_hook]
end end
it "when pushing a branch for the first time" do it "when pushing a branch for the first time" do
@webhook.should_not_receive(:execute) @project_hook.should_not_receive(:execute)
project.execute_web_hooks('00000000000000000000000000000000', 'newrev', 'refs/heads/master', @user) project.execute_hooks('00000000000000000000000000000000', 'newrev', 'refs/heads/master', @user)
end end
it "when pushing tags" do it "when pushing tags" do
@webhook.should_not_receive(:execute) @project_hook.should_not_receive(:execute)
project.execute_web_hooks('oldrev', 'newrev', 'refs/tags/v1.0.0', @user) project.execute_hooks('oldrev', 'newrev', 'refs/tags/v1.0.0', @user)
end end
end end

View file

@ -11,7 +11,7 @@ describe Project do
it { should have_many(:issues).dependent(:destroy) } it { should have_many(:issues).dependent(:destroy) }
it { should have_many(:notes).dependent(:destroy) } it { should have_many(:notes).dependent(:destroy) }
it { should have_many(:snippets).dependent(:destroy) } it { should have_many(:snippets).dependent(:destroy) }
it { should have_many(:web_hooks).dependent(:destroy) } it { should have_many(:hooks).dependent(:destroy) }
it { should have_many(:deploy_keys).dependent(:destroy) } it { should have_many(:deploy_keys).dependent(:destroy) }
end end

View file

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe WebHook do describe ProjectHook do
describe "Associations" do describe "Associations" do
it { should belong_to :project } it { should belong_to :project }
end end
@ -23,32 +23,32 @@ describe WebHook do
describe "execute" do describe "execute" do
before(:each) do before(:each) do
@webhook = Factory :web_hook @project_hook = Factory :project_hook
@project = Factory :project @project = Factory :project
@project.web_hooks << [@webhook] @project.hooks << [@project_hook]
@data = { before: 'oldrev', after: 'newrev', ref: 'ref'} @data = { before: 'oldrev', after: 'newrev', ref: 'ref'}
WebMock.stub_request(:post, @webhook.url) WebMock.stub_request(:post, @project_hook.url)
end end
it "POSTs to the web hook URL" do it "POSTs to the web hook URL" do
@webhook.execute(@data) @project_hook.execute(@data)
WebMock.should have_requested(:post, @webhook.url).once WebMock.should have_requested(:post, @project_hook.url).once
end end
it "POSTs the data as JSON" do it "POSTs the data as JSON" do
json = @data.to_json json = @data.to_json
@webhook.execute(@data) @project_hook.execute(@data)
WebMock.should have_requested(:post, @webhook.url).with(body: json).once WebMock.should have_requested(:post, @project_hook.url).with(body: json).once
end end
it "catches exceptions" do it "catches exceptions" do
WebHook.should_receive(:post).and_raise("Some HTTP Post error") WebHook.should_receive(:post).and_raise("Some HTTP Post error")
lambda { lambda {
@webhook.execute(@data) @project_hook.execute(@data)
}.should_not raise_error }.should raise_error
end end
end end
end end

View file

@ -13,9 +13,9 @@ describe "Admin::Projects" do
it { admin_users_path.should be_denied_for :visitor } it { admin_users_path.should be_denied_for :visitor }
end end
describe "GET /admin/emails" do describe "GET /admin/hooks" do
it { admin_emails_path.should be_allowed_for :admin } it { admin_hooks_path.should be_allowed_for :admin }
it { admin_emails_path.should be_denied_for :user } it { admin_hooks_path.should be_denied_for :user }
it { admin_emails_path.should be_denied_for :visitor } it { admin_hooks_path.should be_denied_for :visitor }
end end
end end

View file

@ -9,7 +9,7 @@ describe "Hooks" do
describe "GET index" do describe "GET index" do
it "should be available" do it "should be available" do
@hook = Factory :web_hook, :project => @project @hook = Factory :project_hook, :project => @project
visit project_hooks_path(@project) visit project_hooks_path(@project)
page.should have_content "Hooks" page.should have_content "Hooks"
page.should have_content @hook.url page.should have_content @hook.url
@ -21,7 +21,7 @@ describe "Hooks" do
@url = Faker::Internet.uri("http") @url = Faker::Internet.uri("http")
visit project_hooks_path(@project) visit project_hooks_path(@project)
fill_in "hook_url", :with => @url fill_in "hook_url", :with => @url
expect { click_button "Add Web Hook" }.to change(WebHook, :count).by(1) expect { click_button "Add Web Hook" }.to change(ProjectHook, :count).by(1)
end end
it "should open new team member popup" do it "should open new team member popup" do
@ -32,7 +32,8 @@ describe "Hooks" do
describe "Test" do describe "Test" do
before do before do
@hook = Factory :web_hook, :project => @project @hook = Factory :project_hook, :project => @project
stub_request(:post, @hook.url)
visit project_hooks_path(@project) visit project_hooks_path(@project)
click_link "Test Hook" click_link "Test Hook"
end end

View file

@ -22,14 +22,14 @@ describe PostReceive do
Key.stub(find_by_identifier: nil) Key.stub(find_by_identifier: nil)
project.should_not_receive(:observe_push) project.should_not_receive(:observe_push)
project.should_not_receive(:execute_web_hooks) project.should_not_receive(:execute_hooks)
PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master', key_id).should be_false PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master', key_id).should be_false
end end
it "asks the project to execute web hooks" do it "asks the project to execute web hooks" do
Project.stub(find_by_path: project) Project.stub(find_by_path: project)
project.should_receive(:execute_web_hooks).with('sha-old', 'sha-new', 'refs/heads/master', project.owner) project.should_receive(:execute_hooks).with('sha-old', 'sha-new', 'refs/heads/master', project.owner)
PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master', key_id) PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master', key_id)
end end