Commit 70e3bffd95eb5736dd108e0836abaa85a2f1c742

Authored by Dmitriy Zaporozhets
1 parent 39e37677

Fixed: post-receive, project remove, tests

app/observers/project_observer.rb
@@ -15,11 +15,10 @@ class ProjectObserver < ActiveRecord::Observer @@ -15,11 +15,10 @@ class ProjectObserver < ActiveRecord::Observer
15 def after_destroy(project) 15 def after_destroy(project)
16 GitoliteWorker.perform_async( 16 GitoliteWorker.perform_async(
17 :remove_repository, 17 :remove_repository,
18 - self.path_with_namespace 18 + project.path_with_namespace
19 ) 19 )
20 20
21 project.satellite.destroy 21 project.satellite.destroy
22 - project.destroy_repository  
23 22
24 log_info("Project \"#{project.name}\" was removed") 23 log_info("Project \"#{project.name}\" was removed")
25 end 24 end
app/workers/post_receive.rb
@@ -27,8 +27,9 @@ class PostReceive @@ -27,8 +27,9 @@ class PostReceive
27 User.find_by_email(email) if email 27 User.find_by_email(email) if email
28 elsif /^[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}$/.match(identifier) 28 elsif /^[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}$/.match(identifier)
29 User.find_by_email(identifier) 29 User.find_by_email(identifier)
30 - else  
31 - User.find_by_username(identifier.strip) 30 + elsif identifier =~ /key/
  31 + key_id = identifier.gsub("key-", "")
  32 + Key.find_by_id(key_id).try(:user)
32 end 33 end
33 34
34 unless user 35 unless user
lib/api/internal.rb
1 module Gitlab 1 module Gitlab
2 - # Access API 2 + # Internal access API
3 class Internal < Grape::API 3 class Internal < Grape::API
  4 + namespace 'internal' do
  5 + #
  6 + # Check if ssh key has access to project code
  7 + #
  8 + get "/allowed" do
  9 + key = Key.find(params[:key_id])
  10 + user = key.user
4 11
5 - get "/allowed" do  
6 - user = User.find_by_username(params[:username])  
7 - project = Project.find_with_namespace(params[:project])  
8 - action = case params[:action]  
9 - when 'git-upload-pack'  
10 - then :download_code  
11 - when 'git-receive-pack'  
12 - then  
13 - if project.protected_branch?(params[:ref])  
14 - :push_code_to_protected_branches  
15 - else  
16 - :push_code 12 + project = Project.find_with_namespace(params[:project])
  13 + action = case params[:action]
  14 + when 'git-upload-pack'
  15 + then :download_code
  16 + when 'git-receive-pack'
  17 + then
  18 + if project.protected_branch?(params[:ref])
  19 + :push_code_to_protected_branches
  20 + else
  21 + :push_code
  22 + end
17 end 23 end
18 - end  
19 24
20 - user.can?(action, project) 25 + user.can?(action, project)
  26 + end
  27 +
  28 + #
  29 + # Discover user by ssh key
  30 + #
  31 + get "/discover" do
  32 + key = Key.find(params[:key_id])
  33 + present key.user, with: Entities::User
  34 + end
21 end 35 end
22 end 36 end
23 end 37 end
spec/lib/gitolite_spec.rb
@@ -1,18 +0,0 @@ @@ -1,18 +0,0 @@
1 -require 'spec_helper'  
2 -  
3 -describe Gitlab::Gitolite do  
4 - let(:project) { double('Project', id: 7, path: 'diaspora') }  
5 - let(:gitolite) { Gitlab::Gitolite.new }  
6 -  
7 - before do  
8 - Project.stub(find: project)  
9 - end  
10 -  
11 - it { should respond_to :set_key }  
12 - it { should respond_to :remove_key }  
13 -  
14 - it { should respond_to :add_repository }  
15 - it { should respond_to :remove_repository }  
16 -  
17 - it { gitolite.url_to_repo('diaspora').should == Gitlab.config.gitolite.ssh_path_prefix + "diaspora.git" }  
18 -end  
spec/lib/shell_spec.rb 0 → 100644
@@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Gitlab::Shell do
  4 + let(:project) { double('Project', id: 7, path: 'diaspora') }
  5 + let(:gitolite) { Gitlab::Shell.new }
  6 +
  7 + before do
  8 + Project.stub(find: project)
  9 + end
  10 +
  11 + it { should respond_to :add_key }
  12 + it { should respond_to :remove_key }
  13 + it { should respond_to :add_repository }
  14 + it { should respond_to :remove_repository }
  15 +
  16 + it { gitolite.url_to_repo('diaspora').should == Gitlab.config.gitolite.ssh_path_prefix + "diaspora.git" }
  17 +end
spec/models/project_spec.rb
@@ -77,8 +77,6 @@ describe Project do @@ -77,8 +77,6 @@ describe Project do
77 it { should respond_to(:url_to_repo) } 77 it { should respond_to(:url_to_repo) }
78 it { should respond_to(:repo_exists?) } 78 it { should respond_to(:repo_exists?) }
79 it { should respond_to(:satellite) } 79 it { should respond_to(:satellite) }
80 - it { should respond_to(:update_repository) }  
81 - it { should respond_to(:destroy_repository) }  
82 it { should respond_to(:observe_push) } 80 it { should respond_to(:observe_push) }
83 it { should respond_to(:update_merge_requests) } 81 it { should respond_to(:update_merge_requests) }
84 it { should respond_to(:execute_hooks) } 82 it { should respond_to(:execute_hooks) }
spec/models/protected_branch_spec.rb
@@ -24,19 +24,4 @@ describe ProtectedBranch do @@ -24,19 +24,4 @@ describe ProtectedBranch do
24 it { should validate_presence_of(:project) } 24 it { should validate_presence_of(:project) }
25 it { should validate_presence_of(:name) } 25 it { should validate_presence_of(:name) }
26 end 26 end
27 -  
28 - describe 'Callbacks' do  
29 - let(:branch) { build(:protected_branch) }  
30 -  
31 - it 'call update_repository after save' do  
32 - branch.should_receive(:update_repository)  
33 - branch.save  
34 - end  
35 -  
36 - it 'call update_repository after destroy' do  
37 - branch.save  
38 - branch.should_receive(:update_repository)  
39 - branch.destroy  
40 - end  
41 - end  
42 end 27 end
spec/observers/key_observer_spec.rb
@@ -3,7 +3,7 @@ require &#39;spec_helper&#39; @@ -3,7 +3,7 @@ require &#39;spec_helper&#39;
3 describe KeyObserver do 3 describe KeyObserver do
4 before do 4 before do
5 @key = double('Key', 5 @key = double('Key',
6 - identifier: 'admin_654654', 6 + shell_id: 'key-32',
7 key: '== a vaild ssh key', 7 key: '== a vaild ssh key',
8 projects: [], 8 projects: [],
9 is_deploy_key: false 9 is_deploy_key: false
@@ -14,14 +14,14 @@ describe KeyObserver do @@ -14,14 +14,14 @@ describe KeyObserver do
14 14
15 context :after_save do 15 context :after_save do
16 it do 16 it do
17 - GitoliteWorker.should_receive(:perform_async).with(:set_key, @key.identifier, @key.key, @key.projects.map(&:id)) 17 + GitoliteWorker.should_receive(:perform_async).with(:add_key, @key.shell_id, @key.key)
18 @observer.after_save(@key) 18 @observer.after_save(@key)
19 end 19 end
20 end 20 end
21 21
22 context :after_destroy do 22 context :after_destroy do
23 it do 23 it do
24 - GitoliteWorker.should_receive(:perform_async).with(:remove_key, @key.identifier, @key.projects.map(&:id)) 24 + GitoliteWorker.should_receive(:perform_async).with(:remove_key, @key.shell_id, @key.key)
25 @observer.after_destroy(@key) 25 @observer.after_destroy(@key)
26 end 26 end
27 end 27 end
spec/support/stubbed_repository.rb
@@ -48,11 +48,11 @@ module Gitlab @@ -48,11 +48,11 @@ module Gitlab
48 true 48 true
49 end 49 end
50 50
51 - def add_key name, key 51 + def add_key id, key
52 true 52 true
53 end 53 end
54 54
55 - def remove_key key 55 + def remove_key id, key
56 true 56 true
57 end 57 end
58 end 58 end
spec/workers/post_receive_spec.rb
@@ -11,7 +11,7 @@ describe PostReceive do @@ -11,7 +11,7 @@ describe PostReceive do
11 context "web hook" do 11 context "web hook" do
12 let(:project) { create(:project) } 12 let(:project) { create(:project) }
13 let(:key) { create(:key, user: project.owner) } 13 let(:key) { create(:key, user: project.owner) }
14 - let(:key_id) { key.identifier } 14 + let(:key_id) { key.shell_id }
15 15
16 it "fetches the correct project" do 16 it "fetches the correct project" do
17 Project.should_receive(:find_with_namespace).with(project.path_with_namespace).and_return(project) 17 Project.should_receive(:find_with_namespace).with(project.path_with_namespace).and_return(project)
@@ -19,7 +19,7 @@ describe PostReceive do @@ -19,7 +19,7 @@ describe PostReceive do
19 end 19 end
20 20
21 it "does not run if the author is not in the project" do 21 it "does not run if the author is not in the project" do
22 - Key.stub(find_by_identifier: nil) 22 + Key.stub(find_by_id: nil)
23 23
24 project.should_not_receive(:observe_push) 24 project.should_not_receive(:observe_push)
25 project.should_not_receive(:execute_hooks) 25 project.should_not_receive(:execute_hooks)