|
14 | 14 | it { gitlab_keys.instance_variable_get(:@key_id).should == 'key-741' }
|
15 | 15 | end
|
16 | 16 |
|
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 |
23 | 17 |
|
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') } |
26 | 20 |
|
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) } |
32 | 30 |
|
33 | 31 | it "should log an add-key event" do
|
34 | 32 | $logger.should_receive(:info).with('Adding key key-741 => "ssh-rsa AAAAB3NzaDAxx2E"')
|
35 |
| - gitlab_keys.stub(:open) |
36 | 33 | gitlab_keys.send :add_key
|
37 | 34 | end
|
| 35 | + |
| 36 | + it "should return true" do |
| 37 | + gitlab_keys.send(:add_key).should be_true |
| 38 | + end |
38 | 39 | end
|
| 40 | + end |
39 | 41 |
|
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') } |
42 | 44 |
|
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 |
51 | 51 | 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) } |
52 | 58 |
|
53 | 59 | it "should log an rm-key event" do
|
54 | 60 | $logger.should_receive(:info).with('Removing key key-741')
|
55 | 61 | gitlab_keys.send :rm_key
|
56 | 62 | 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 |
57 | 76 | end
|
58 | 77 | end
|
59 | 78 |
|
@@ -95,6 +114,12 @@ def argv(*args)
|
95 | 114 | end
|
96 | 115 | end
|
97 | 116 |
|
| 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 | + |
98 | 123 | def tmp_authorized_keys_path
|
99 | 124 | File.join(ROOT_PATH, 'tmp', 'authorized_keys')
|
100 | 125 | end
|
|
0 commit comments