Skip to content

Commit a1f2c95

Browse files
committed
Merge branch 'gitlab_keys_return_values' of /home/git/repositories/gitlab/gitlab-shell
2 parents 098e5a6 + d7e1deb commit a1f2c95

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

lib/gitlab_keys.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def add_key
3131
$logger.info "Adding key #{@key_id} => #{@key.inspect}"
3232
auth_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell #{@key_id}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty #{@key}"
3333
open(auth_file, 'a') { |file| file.puts(auth_line) }
34+
true
3435
end
3536

3637
def rm_key
@@ -44,9 +45,11 @@ def rm_key
4445
temp.close
4546
FileUtils.cp(temp.path, auth_file)
4647
end
48+
true
4749
end
4850

4951
def clear
5052
open(auth_file, 'w') { |file| file.puts '# Managed by gitlab-shell' }
53+
true
5154
end
5255
end

spec/gitlab_keys_spec.rb

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,65 @@
1414
it { gitlab_keys.instance_variable_get(:@key_id).should == 'key-741' }
1515
end
1616

17-
context "file writing tests" do
18-
before do
19-
FileUtils.mkdir_p(File.dirname(tmp_authorized_keys_path))
20-
open(tmp_authorized_keys_path, 'w') { |file| file.puts('existing content') }
21-
gitlab_keys.stub(auth_file: tmp_authorized_keys_path)
22-
end
2317

24-
describe :add_key do
25-
let(:gitlab_keys) { build_gitlab_keys('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
18+
describe :add_key do
19+
let(:gitlab_keys) { build_gitlab_keys('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
2620

27-
it "adds a line at the end of the file" do
28-
gitlab_keys.send :add_key
29-
auth_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-741\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaDAxx2E"
30-
File.read(tmp_authorized_keys_path).should == "existing content\n#{auth_line}\n"
31-
end
21+
it "adds a line at the end of the file" do
22+
create_authorized_keys_fixture
23+
gitlab_keys.send :add_key
24+
auth_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-741\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaDAxx2E"
25+
File.read(tmp_authorized_keys_path).should == "existing content\n#{auth_line}\n"
26+
end
27+
28+
context "without file writing" do
29+
before { gitlab_keys.stub(:open) }
3230

3331
it "should log an add-key event" do
3432
$logger.should_receive(:info).with('Adding key key-741 => "ssh-rsa AAAAB3NzaDAxx2E"')
35-
gitlab_keys.stub(:open)
3633
gitlab_keys.send :add_key
3734
end
35+
36+
it "should return true" do
37+
gitlab_keys.send(:add_key).should be_true
38+
end
3839
end
40+
end
3941

40-
describe :rm_key do
41-
let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
42+
describe :rm_key do
43+
let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
4244

43-
it "removes the right line" do
44-
other_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-742\",options ssh-rsa AAAAB3NzaDAxx2E"
45-
open(tmp_authorized_keys_path, 'a') do |auth_file|
46-
auth_file.puts "command=\"#{ROOT_PATH}/bin/gitlab-shell key-741\",options ssh-rsa AAAAB3NzaDAxx2E"
47-
auth_file.puts other_line
48-
end
49-
gitlab_keys.send :rm_key
50-
File.read(tmp_authorized_keys_path).should == "existing content\n#{other_line}\n"
45+
it "removes the right line" do
46+
create_authorized_keys_fixture
47+
other_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-742\",options ssh-rsa AAAAB3NzaDAxx2E"
48+
open(tmp_authorized_keys_path, 'a') do |auth_file|
49+
auth_file.puts "command=\"#{ROOT_PATH}/bin/gitlab-shell key-741\",options ssh-rsa AAAAB3NzaDAxx2E"
50+
auth_file.puts other_line
5151
end
52+
gitlab_keys.send :rm_key
53+
File.read(tmp_authorized_keys_path).should == "existing content\n#{other_line}\n"
54+
end
55+
56+
context "without file writing" do
57+
before { Tempfile.stub(:open) }
5258

5359
it "should log an rm-key event" do
5460
$logger.should_receive(:info).with('Removing key key-741')
5561
gitlab_keys.send :rm_key
5662
end
63+
64+
it "should return true" do
65+
gitlab_keys.send(:rm_key).should be_true
66+
end
67+
end
68+
end
69+
70+
describe :clear do
71+
let(:gitlab_keys) { build_gitlab_keys('clear') }
72+
73+
it "should return true" do
74+
gitlab_keys.stub(:open)
75+
gitlab_keys.send(:clear).should be_true
5776
end
5877
end
5978

@@ -95,6 +114,12 @@ def argv(*args)
95114
end
96115
end
97116

117+
def create_authorized_keys_fixture
118+
FileUtils.mkdir_p(File.dirname(tmp_authorized_keys_path))
119+
open(tmp_authorized_keys_path, 'w') { |file| file.puts('existing content') }
120+
gitlab_keys.stub(auth_file: tmp_authorized_keys_path)
121+
end
122+
98123
def tmp_authorized_keys_path
99124
File.join(ROOT_PATH, 'tmp', 'authorized_keys')
100125
end

0 commit comments

Comments
 (0)