diff options
author | Douwe Maan <douwe@gitlab.com> | 2017-06-28 17:54:15 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2017-06-28 17:54:15 +0000 |
commit | 4539a066ecd2c5a16cab66f9e85f8f0ab747fc5f (patch) | |
tree | 299888d74240d7a714267c14f448dd2187af6392 /spec/gitlab_keys_spec.rb | |
parent | 7e41e378ab71b326f28d62195faeca5c54aafdb8 (diff) | |
parent | fb4260ce3340bde750f0426c1e539d06b0445fe2 (diff) | |
download | gitlab-shell-4539a066ecd2c5a16cab66f9e85f8f0ab747fc5f.tar.gz |
Merge branch 'add-list-key-ids' into 'master'v5.1.0
Add list-key-ids command
See merge request !140
Diffstat (limited to 'spec/gitlab_keys_spec.rb')
-rw-r--r-- | spec/gitlab_keys_spec.rb | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/spec/gitlab_keys_spec.rb b/spec/gitlab_keys_spec.rb index c1c3854..b9f2f87 100644 --- a/spec/gitlab_keys_spec.rb +++ b/spec/gitlab_keys_spec.rb @@ -80,6 +80,23 @@ describe GitlabKeys do end end + describe :list_key_ids do + let(:gitlab_keys) { build_gitlab_keys('list-key-ids') } + before do + create_authorized_keys_fixture( + existing_content: + "key-1\tssh-dsa AAA\nkey-2\tssh-rsa BBB\nkey-3\tssh-rsa CCC\nkey-9000\tssh-rsa DDD\n" + ) + end + + it 'outputs the key IDs, separated by newlines' do + output = capture_stdout do + gitlab_keys.send(:list_key_ids) + end + output.should match "1\n2\n3\n9000" + end + end + describe :batch_add_keys do let(:gitlab_keys) { build_gitlab_keys('batch-add-keys') } let(:fake_stdin) { StringIO.new("key-12\tssh-dsa ASDFASGADG\nkey-123\tssh-rsa GFDGDFSGSDFG\n", 'r') } @@ -160,6 +177,23 @@ describe GitlabKeys do gitlab_keys.send(:rm_key).should be_true end end + + context 'without key content' do + let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741') } + + it "removes the right line by key ID" do + create_authorized_keys_fixture + other_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-742\",options ssh-rsa AAAAB3NzaDAxx2E" + delete_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-741\",options ssh-rsa AAAAB3NzaDAxx2E" + open(tmp_authorized_keys_path, 'a') do |auth_file| + auth_file.puts delete_line + auth_file.puts other_line + end + gitlab_keys.send :rm_key + erased_line = delete_line.gsub(/./, '#') + File.read(tmp_authorized_keys_path).should == "existing content\n#{erased_line}\n#{other_line}\n" + end + end end describe :clear do @@ -288,9 +322,9 @@ describe GitlabKeys do end end - def create_authorized_keys_fixture + def create_authorized_keys_fixture(existing_content: 'existing content') FileUtils.mkdir_p(File.dirname(tmp_authorized_keys_path)) - open(tmp_authorized_keys_path, 'w') { |file| file.puts('existing content') } + open(tmp_authorized_keys_path, 'w') { |file| file.puts(existing_content) } gitlab_keys.stub(auth_file: tmp_authorized_keys_path) end @@ -301,4 +335,13 @@ describe GitlabKeys do def tmp_lock_file_path tmp_authorized_keys_path + '.lock' end + + def capture_stdout(&blk) + old = $stdout + $stdout = fake = StringIO.new + blk.call + fake.string + ensure + $stdout = old + end end |