summaryrefslogtreecommitdiff
path: root/spec/gitlab_shell_spec.rb
blob: 51e02dd196dead5e229b43c694e36bd1ce5828a4 (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
require_relative 'spec_helper'
require_relative '../lib/gitlab_shell'

describe GitlabShell do
  subject do
    ARGV[0] = 'key-56'
    GitlabShell.new.tap do |shell|
      shell.stub(process_cmd: true)
      shell.stub(api: api)
    end
  end
  let(:api) do
    double(GitlabNet).tap do |api|
      api.stub(discover: 'John Doe')
      api.stub(allowed?: true)
    end
  end

  describe :initialize do
    before { ssh_cmd 'git-receive-pack' }

    its(:key_id) { should == 'key-56' }
    its(:repos_path) { should == "/home/git/repositories" }
  end

  describe :parse_cmd do
    context 'w/o namespace' do
      before do
        ssh_cmd 'git-upload-pack gitlab-ci.git'
        subject.send :parse_cmd
      end

      its(:repo_name) { should == 'gitlab-ci.git' }
      its(:git_cmd) { should == 'git-upload-pack' }
    end

    context 'namespace' do
      before do
        ssh_cmd 'git-upload-pack dmitriy.zaporozhets/gitlab-ci.git'
        subject.send :parse_cmd
      end

      its(:repo_name) { should == 'dmitriy.zaporozhets/gitlab-ci.git' }
      its(:git_cmd) { should == 'git-upload-pack' }
    end
  end

  describe :exec do
    context 'git-upload-pack' do
      before { ssh_cmd 'git-upload-pack gitlab-ci.git' }
      after { subject.exec }

      it "should process the command" do
        subject.should_receive(:process_cmd).with()
      end
    end

    context 'git-receive-pack' do
      before { ssh_cmd 'git-receive-pack gitlab-ci.git' }
      after { subject.exec }

      it "should process the command" do
        subject.should_receive(:process_cmd).with()
      end
    end

    context 'arbitrary command' do
      before { ssh_cmd 'arbitrary command' }
      after { subject.exec }

      it "should not process the command" do
        subject.should_not_receive(:process_cmd)
      end
    end

    context 'no command' do
      before { ssh_cmd nil }
      after { subject.exec }

      it "should call api.discover" do
        api.should_receive(:discover).with('key-56')
      end
    end
  end

  describe :validate_access do
    before { ssh_cmd 'git-upload-pack gitlab-ci.git' }
    after { subject.exec }

    it "should call api.allowed?" do
      api.should_receive(:allowed?).
        with('git-upload-pack', 'gitlab-ci.git', 'key-56', '_any')
    end
  end

  def ssh_cmd(cmd)
    ENV['SSH_ORIGINAL_COMMAND'] = cmd
  end

end