summaryrefslogtreecommitdiffstats
path: root/test/unit/lib/ssh_client_test.rb
blob: 89ab7a93fedc3bde50c1113a95a81a5ced01b470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# encoding: utf-8
#--
#   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++


require File.dirname(__FILE__) + '/../../test_helper'

class SSHClientTest < ActiveSupport::TestCase

  def setup
    @strainer = Gitorious::SSH::Strainer.new("git-upload-pack 'foo/bar.git'").parse!
    @real_path = "abc/123/defg.git"
    @full_real_path = File.join(GitoriousConfig["repository_base_path"], @real_path)
    @ok_stub = stub("ok response mock",
      :body => "real_path:#{@real_path}\nforce_pushing_denied:false")
    @not_ok_stub = stub("ok response mock", :body => "nil")
  end
  
  should "parse the project name from the passed in Strainer" do
    client = Gitorious::SSH::Client.new(@strainer, "johan")
    assert_equal "foo", client.project_name
  end

  should "parse the repository name from the passed in Strainer" do
    client = Gitorious::SSH::Client.new(@strainer, "johan")
    assert_equal "bar", client.repository_name
  end
    
  context "namespacing" do
    setup do
      @team_strainer = Gitorious::SSH::Strainer.new("git-upload-pack '+foo/bar/baz.git'").parse!
      @user_strainer = Gitorious::SSH::Strainer.new("git-upload-pack '~foo/bar/baz.git'").parse!
    end
    
    should "parse the project name from a team namespaced repo" do
      client = Gitorious::SSH::Client.new(@team_strainer, "johan")
      assert_equal "+foo", client.project_name
      assert_equal "bar/baz", client.repository_name
    end
    
    should "parse the project name from a user namespaced repo" do
      client = Gitorious::SSH::Client.new(@user_strainer, "johan")
      assert_equal "~foo", client.project_name
      assert_equal "bar/baz", client.repository_name
    end
  end
  
  should "sets the username that was passed into it" do
    client = Gitorious::SSH::Client.new(@strainer, "johan")
    assert_equal "johan", client.user_name
  end
  
  def make_request(path)
    request = ActionController::TestRequest.new
    request.request_uri = path
    request
  end
  
  
  should 'ask for the real path' do
    client = Gitorious::SSH::Client.new(@strainer, "johan")
    exp_path = "/foo/bar/writable_by?username=johan"
    assert_equal exp_path, client.writable_by_query_path

    request = make_request(client.writable_by_query_path)
    assert_equal RepositoriesController, ActionController::Routing::Routes.recognize(request)
    assert_equal "writable_by", request.symbolized_path_parameters[:action]
    assert_equal "repositories", request.symbolized_path_parameters[:controller]
    assert_equal "foo", request.symbolized_path_parameters[:project_id]
    assert_equal "bar", request.symbolized_path_parameters[:id]
  end
  
  should 'return the correct authentication URL' do
    client = Gitorious::SSH::Client.new(@strainer, "johan")
    assert_equal "http://#{GitoriousConfig['gitorious_client_host']}:#{GitoriousConfig['gitorious_client_port']}/foo/bar/writable_by?username=johan", client.writable_by_query_url
  end

  context "configuration parsing" do
    should "parse the basic configuration format" do
      client = Gitorious::SSH::Client.new(@strainer, "johan")
      connection_stub = stub_everything("connection stub")
      connection_stub.expects(:get).with("/foo/bar/config").returns(@ok_stub)
      client.expects(:connection).once.returns(connection_stub)
      assert_equal @real_path, client.configuration["real_path"]
      assert_equal "false", client.configuration["force_pushing_denied"]
    end

    should "know if force_pushing is allowed" do
      client = Gitorious::SSH::Client.new(@strainer, "johan")
      connection_stub = stub_everything("connection stub")
      connection_stub.expects(:get).with("/foo/bar/config").returns(@ok_stub)
      client.expects(:connection).once.returns(connection_stub)
      assert !client.force_pushing_denied?
    end

    should "ask gets the real path from the query url" do
      client = Gitorious::SSH::Client.new(@strainer, "johan")
      connection_stub = stub_everything("connection_stub")
      connection_stub.expects(:get) \
        .with("/foo/bar/config") \
        .returns(@ok_stub)
      client.expects(:connection).once.returns(connection_stub)
      File.expects(:exist?).with(@full_real_path).returns(true)
      assert_equal @full_real_path, client.real_path
    end
    
    should 'raise if the pre-receive hook is not executable' do
      client = Gitorious::SSH::Client.new(@strainer, "johan")
      client.stubs(:real_path).returns("/tmp/foo.git")
      File.expects(:"exist?").with("/tmp/foo.git/hooks/pre-receive").returns(true)
      File.expects(:"symlink?").with("/tmp/foo.git/hooks/pre-receive").returns(false)
      File.expects(:"executable?").with("/tmp/foo.git/hooks/pre-receive").returns(false)
      assert !client.pre_receive_hook_exists?
    end
    
    should "raises if the real path doesn't exist" do
      client = Gitorious::SSH::Client.new(@strainer, "johan")
      connection_stub = stub_everything("connection_stub")
      connection_stub.expects(:get) \
        .with("/foo/bar/config") \
        .returns(@ok_stub)
      client.expects(:connection).once.returns(connection_stub)
      File.expects(:exist?).with(@full_real_path).returns(false)
      assert_raises(Gitorious::SSH::AccessDeniedError) do
        client.real_path
      end
    end
    
    should "raises if the real path isn't returned" do
      client = Gitorious::SSH::Client.new(@strainer, "johan")
      connection_stub = stub_everything("connection_stub")
      connection_stub.expects(:get) \
        .with("/foo/bar/config") \
        .returns(@not_ok_stub)
      client.expects(:connection).once.returns(connection_stub)
      assert_raises(Gitorious::SSH::AccessDeniedError) do
        client.real_path
      end
    end

  end  
    
  should "returns the command we can safely execute with git-shell" do
    client = Gitorious::SSH::Client.new(@strainer, "johan")
    connection_stub = stub_everything("connection_stub")
    connection_stub.expects(:get) \
      .with("/foo/bar/config") \
      .returns(@ok_stub)
    client.expects(:connection).once.returns(connection_stub)
    File.expects(:exist?).with(@full_real_path).returns(true)
    assert_equal "git-upload-pack '#{@full_real_path}'", client.to_git_shell_argument
  end
  
end