Commit 655418be authored by Valeriy Sizov's avatar Valeriy Sizov

System hooks: fix broken tests

parent f5908cef
......@@ -27,7 +27,7 @@ module GitPush
true
end
def execute_web_hooks(oldrev, newrev, ref, user)
def execute_hooks(oldrev, newrev, ref, user)
ref_parts = ref.split('/')
# 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)
hooks.each { |web_hook| web_hook.execute(data) }
hooks.each { |hook| hook.execute(data) }
end
def post_receive_data(oldrev, newrev, ref, user)
......@@ -97,7 +97,7 @@ module GitPush
self.update_merge_requests(oldrev, newrev, ref, user)
# Execute web hooks
self.execute_web_hooks(oldrev, newrev, ref, user)
self.execute_hooks(oldrev, newrev, ref, user)
# Create satellite
self.satellite.create unless self.satellite.exists?
......
......@@ -60,7 +60,7 @@ Factory.add(:key, Key) do |obj|
obj.key = File.read(File.join(Rails.root, "db", "pkey.example"))
end
Factory.add(:web_hook, WebHook) do |obj|
Factory.add(:project_hook, ProjectHook) do |obj|
obj.url = Faker::Internet.uri("http")
end
......
......@@ -21,44 +21,44 @@ describe Project, "Hooks" do
end
end
describe "Web hooks" do
describe "Project hooks" do
context "with no web hooks" do
it "raises no errors" do
lambda {
project.execute_web_hooks('oldrev', 'newrev', 'ref', @user)
project.execute_hooks('oldrev', 'newrev', 'ref', @user)
}.should_not raise_error
end
end
context "with web hooks" do
before do
@webhook = Factory(:web_hook)
@webhook_2 = Factory(:web_hook)
project.web_hooks << [@webhook, @webhook_2]
@project_hook = Factory(:project_hook)
@project_hook_2 = Factory(:project_hook)
project.hooks << [@project_hook, @project_hook_2]
end
it "executes multiple web hook" do
@webhook.should_receive(:execute).once
@webhook_2.should_receive(:execute).once
@project_hook.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
context "does not execute web hooks" do
before do
@webhook = Factory(:web_hook)
project.web_hooks << [@webhook]
@project_hook = Factory(:project_hook)
project.hooks << [@project_hook]
end
it "when pushing a branch for the first time" do
@webhook.should_not_receive(:execute)
project.execute_web_hooks('00000000000000000000000000000000', 'newrev', 'refs/heads/master', @user)
@project_hook.should_not_receive(:execute)
project.execute_hooks('00000000000000000000000000000000', 'newrev', 'refs/heads/master', @user)
end
it "when pushing tags" do
@webhook.should_not_receive(:execute)
project.execute_web_hooks('oldrev', 'newrev', 'refs/tags/v1.0.0', @user)
@project_hook.should_not_receive(:execute)
project.execute_hooks('oldrev', 'newrev', 'refs/tags/v1.0.0', @user)
end
end
......
......@@ -11,7 +11,7 @@ describe Project do
it { should have_many(:issues).dependent(:destroy) }
it { should have_many(:notes).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) }
end
......
require 'spec_helper'
describe WebHook do
describe ProjectHook do
describe "Associations" do
it { should belong_to :project }
end
......@@ -23,32 +23,32 @@ describe WebHook do
describe "execute" do
before(:each) do
@webhook = Factory :web_hook
@project_hook = Factory :project_hook
@project = Factory :project
@project.web_hooks << [@webhook]
@project.hooks << [@project_hook]
@data = { before: 'oldrev', after: 'newrev', ref: 'ref'}
WebMock.stub_request(:post, @webhook.url)
WebMock.stub_request(:post, @project_hook.url)
end
it "POSTs to the web hook URL" do
@webhook.execute(@data)
WebMock.should have_requested(:post, @webhook.url).once
@project_hook.execute(@data)
WebMock.should have_requested(:post, @project_hook.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
@project_hook.execute(@data)
WebMock.should have_requested(:post, @project_hook.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
@project_hook.execute(@data)
}.should raise_error
end
end
end
......
......@@ -13,9 +13,9 @@ describe "Admin::Projects" do
it { admin_users_path.should be_denied_for :visitor }
end
describe "GET /admin/emails" do
it { admin_emails_path.should be_allowed_for :admin }
it { admin_emails_path.should be_denied_for :user }
it { admin_emails_path.should be_denied_for :visitor }
describe "GET /admin/hooks" do
it { admin_hooks_path.should be_allowed_for :admin }
it { admin_hooks_path.should be_denied_for :user }
it { admin_hooks_path.should be_denied_for :visitor }
end
end
......@@ -9,7 +9,7 @@ describe "Hooks" do
describe "GET index" do
it "should be available" do
@hook = Factory :web_hook, :project => @project
@hook = Factory :project_hook, :project => @project
visit project_hooks_path(@project)
page.should have_content "Hooks"
page.should have_content @hook.url
......@@ -21,7 +21,7 @@ describe "Hooks" do
@url = Faker::Internet.uri("http")
visit project_hooks_path(@project)
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
it "should open new team member popup" do
......@@ -32,7 +32,8 @@ describe "Hooks" do
describe "Test" 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)
click_link "Test Hook"
end
......
......@@ -22,14 +22,14 @@ describe PostReceive do
Key.stub(find_by_identifier: nil)
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
end
it "asks the project to execute web hooks" do
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)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment