Commit 25bde28d authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Fixed tests. removed delegate to id

parent bb164ebf
...@@ -14,7 +14,7 @@ class Key < ActiveRecord::Base ...@@ -14,7 +14,7 @@ class Key < ActiveRecord::Base
before_save :set_identifier before_save :set_identifier
after_save :update_repository after_save :update_repository
after_destroy :repository_delete_key after_destroy :repository_delete_key
delegate :id, :name, :email, :to => :user, :prefix => true delegate :name, :email, :to => :user, :prefix => true
def set_identifier def set_identifier
if is_deploy_key if is_deploy_key
......
...@@ -117,7 +117,7 @@ class Project < ActiveRecord::Base ...@@ -117,7 +117,7 @@ class Project < ActiveRecord::Base
before: oldrev, before: oldrev,
after: newrev, after: newrev,
ref: ref, ref: ref,
user_id: key.user_id, user_id: key.user.id,
user_name: key.user_name, user_name: key.user_name,
repository: { repository: {
name: name, name: name,
......
...@@ -16,7 +16,7 @@ class UsersProject < ActiveRecord::Base ...@@ -16,7 +16,7 @@ class UsersProject < ActiveRecord::Base
validates_presence_of :user_id validates_presence_of :user_id
validates_presence_of :project_id validates_presence_of :project_id
delegate :id, :name, :email, :to => :user, :prefix => true delegate :name, :email, :to => :user, :prefix => true
def self.bulk_import(project, user_ids, project_access, repo_access) def self.bulk_import(project, user_ids, project_access, repo_access)
UsersProject.transaction do UsersProject.transaction do
......
...@@ -5,6 +5,9 @@ class PostReceive ...@@ -5,6 +5,9 @@ class PostReceive
project = Project.find_by_path(reponame) project = Project.find_by_path(reponame)
return false if project.nil? return false if project.nil?
# Ignore push from non-gitlab users
return false unless Key.find_by_identifier(author_key_id)
project.observe_push(oldrev, newrev, ref, author_key_id) project.observe_push(oldrev, newrev, ref, author_key_id)
project.execute_web_hooks(oldrev, newrev, ref, author_key_id) project.execute_web_hooks(oldrev, newrev, ref, author_key_id)
end end
......
...@@ -2,17 +2,21 @@ require 'spec_helper' ...@@ -2,17 +2,21 @@ require 'spec_helper'
describe Project, "Hooks" do describe Project, "Hooks" do
let(:project) { Factory :project } let(:project) { Factory :project }
before do
@key = Factory :key, :user => project.owner
@key_id = @key.identifier
end
describe "Post Receive Event" do describe "Post Receive Event" do
it "should create push event" do it "should create push event" do
oldrev, newrev, ref = '00000000000000000000000000000000', 'newrev', 'refs/heads/master' oldrev, newrev, ref = '00000000000000000000000000000000', 'newrev', 'refs/heads/master'
project.observe_push(oldrev, newrev, ref) project.observe_push(oldrev, newrev, ref, @key_id)
event = Event.last event = Event.last
event.should_not be_nil event.should_not be_nil
event.project.should == project event.project.should == project
event.action.should == Event::Pushed event.action.should == Event::Pushed
event.data == project.web_hook_data(oldrev, newrev, ref) event.data == project.web_hook_data(oldrev, newrev, ref, @key_id)
end end
end end
...@@ -20,7 +24,7 @@ describe Project, "Hooks" do ...@@ -20,7 +24,7 @@ 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') project.execute_web_hooks('oldrev', 'newrev', 'ref', @key_id)
}.should_not raise_error }.should_not raise_error
end end
end end
...@@ -36,7 +40,7 @@ describe Project, "Hooks" do ...@@ -36,7 +40,7 @@ describe Project, "Hooks" do
@webhook.should_receive(:execute).once @webhook.should_receive(:execute).once
@webhook_2.should_receive(:execute).once @webhook_2.should_receive(:execute).once
project.execute_web_hooks('oldrev', 'newrev', 'refs/heads/master') project.execute_web_hooks('oldrev', 'newrev', 'refs/heads/master', @key_id)
end end
end end
...@@ -48,12 +52,12 @@ describe Project, "Hooks" do ...@@ -48,12 +52,12 @@ describe Project, "Hooks" do
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) @webhook.should_not_receive(:execute)
project.execute_web_hooks('00000000000000000000000000000000', 'newrev', 'refs/heads/master') project.execute_web_hooks('00000000000000000000000000000000', 'newrev', 'refs/heads/master', @key_id)
end end
it "when pushing tags" do it "when pushing tags" do
@webhook.should_not_receive(:execute) @webhook.should_not_receive(:execute)
project.execute_web_hooks('oldrev', 'newrev', 'refs/tags/v1.0.0') project.execute_web_hooks('oldrev', 'newrev', 'refs/tags/v1.0.0', @key_id)
end end
end end
...@@ -69,7 +73,7 @@ describe Project, "Hooks" do ...@@ -69,7 +73,7 @@ describe Project, "Hooks" do
# Fill nil/empty attributes # Fill nil/empty attributes
project.description = "This is a description" project.description = "This is a description"
@data = project.web_hook_data(@oldrev, @newrev, @ref) @data = project.web_hook_data(@oldrev, @newrev, @ref, @key_id)
end end
subject { @data } subject { @data }
...@@ -77,6 +81,8 @@ describe Project, "Hooks" do ...@@ -77,6 +81,8 @@ describe Project, "Hooks" do
it { should include(before: @oldrev) } it { should include(before: @oldrev) }
it { should include(after: @newrev) } it { should include(after: @newrev) }
it { should include(ref: @ref) } it { should include(ref: @ref) }
it { should include(user_id: project.owner.id) }
it { should include(user_name: project.owner.name) }
context "with repository data" do context "with repository data" do
subject { @data[:repository] } subject { @data[:repository] }
......
...@@ -10,17 +10,22 @@ describe PostReceive do ...@@ -10,17 +10,22 @@ describe PostReceive do
context "web hooks" do context "web hooks" do
let(:project) { Factory :project } let(:project) { Factory :project }
before do
@key = Factory :key, :user => project.owner
@key_id = @key.identifier
end
it "it retrieves the correct project" do it "it retrieves the correct project" do
Project.should_receive(:find_by_path).with(project.path) Project.should_receive(:find_by_path).with(project.path)
PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master') Key.should_receive(:find_by_identifier).with(project.path)
PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master', @key_id)
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.should_receive(:execute_web_hooks).with('sha-old', 'sha-new', 'refs/heads/master', @key_id)
PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master') PostReceive.perform(project.path, 'sha-old', 'sha-new', 'refs/heads/master', @key_id)
end end
end end
end 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