summaryrefslogtreecommitdiff
path: root/spec/gitlab_keys_spec.rb
blob: 1c557aaf5bc68344725cf9590a7c5955a448bc4e (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
require_relative 'spec_helper'
require_relative '../lib/gitlab_keys'

describe GitlabKeys do
  describe :initialize do
    before do
      argv('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E')
      @gl_keys = GitlabKeys.new
    end

    it { @gl_keys.key.should == 'ssh-rsa AAAAB3NzaDAxx2E' }
    it { @gl_keys.instance_variable_get(:@command).should == 'add-key' }
    it { @gl_keys.instance_variable_get(:@key_id).should == 'key-741' }
  end

  describe :add_key do
    before do
      argv('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E')
      @gl_keys = GitlabKeys.new
    end

    it "should receive valid cmd" do
      valid_cmd = "echo 'command=\"#{ROOT_PATH}/bin/gitlab-shell key-741\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaDAxx2E' >> /home/git/.ssh/authorized_keys"
      @gl_keys.should_receive(:system).with(valid_cmd)
      @gl_keys.send :add_key
    end
  end

  describe :rm_key do
    before do
      argv('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E')
      @gl_keys = GitlabKeys.new
    end

    it "should receive valid cmd" do
      valid_cmd = "sed -i '/key-741/d' /home/git/.ssh/authorized_keys"
      @gl_keys.should_receive(:system).with(valid_cmd)
      @gl_keys.send :rm_key
    end
  end

  def argv(*args)
    args.each_with_index do |arg, i|
      ARGV[i] = arg
    end
  end
end