summaryrefslogtreecommitdiff
path: root/spec/gitlab_keys_spec.rb
diff options
context:
space:
mode:
authorJacob Vosmaer <contact@jacobvosmaer.nl>2013-11-05 11:50:59 +0100
committerJacob Vosmaer <contact@jacobvosmaer.nl>2013-11-05 13:35:02 +0100
commitab008254dc74d928d5e361955cb2fa9ab3005f79 (patch)
tree6c50b19f07ed6f244c2b903d457e4f1dc022ece4 /spec/gitlab_keys_spec.rb
parentde09438aba040b00e7e579d4154353f861e20aa1 (diff)
downloadgitlab-shell-ab008254dc74d928d5e361955cb2fa9ab3005f79.tar.gz
More file writing tests for GitlabKeys
Diffstat (limited to 'spec/gitlab_keys_spec.rb')
-rw-r--r--spec/gitlab_keys_spec.rb60
1 files changed, 33 insertions, 27 deletions
diff --git a/spec/gitlab_keys_spec.rb b/spec/gitlab_keys_spec.rb
index 3688a96..89438fc 100644
--- a/spec/gitlab_keys_spec.rb
+++ b/spec/gitlab_keys_spec.rb
@@ -14,40 +14,46 @@ describe GitlabKeys do
it { gitlab_keys.instance_variable_get(:@key_id).should == 'key-741' }
end
- describe :add_key do
- let(:gitlab_keys) { build_gitlab_keys('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
- let(:file) { mock(:file) }
-
- it "should receive valid cmd" do
- auth_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-741\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaDAxx2E"
- gitlab_keys.should_receive(:open).with(GitlabConfig.new.auth_file, 'a').and_yield(file)
- file.should_receive(:puts).with(auth_line)
- gitlab_keys.send :add_key
+ context "file writing tests" do
+ before do
+ FileUtils.mkdir_p(File.dirname(tmp_authorized_keys_path))
+ open(tmp_authorized_keys_path, 'w') { |file| file.puts('existing content') }
+ gitlab_keys.stub(auth_file: tmp_authorized_keys_path)
end
- it "should log an add-key event" do
- $logger.should_receive(:info).with('Adding key key-741 => "ssh-rsa AAAAB3NzaDAxx2E"')
- gitlab_keys.stub(:open)
- gitlab_keys.send :add_key
- end
- end
+ describe :add_key do
+ let(:gitlab_keys) { build_gitlab_keys('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
- describe :rm_key do
- let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
+ it "adds a line at the end of the file" do
+ gitlab_keys.send :add_key
+ auth_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-741\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaDAxx2E"
+ File.read(tmp_authorized_keys_path).should == "existing content\n#{auth_line}\n"
+ end
- it "should receive valid cmd" do
- FileUtils.mkdir_p(File.dirname(tmp_authorized_keys_path))
- open(tmp_authorized_keys_path, 'w') do |auth_file|
- auth_file.puts ['first key', '/bin/gitlab-shell key-741"', 'third key']
+ it "should log an add-key event" do
+ $logger.should_receive(:info).with('Adding key key-741 => "ssh-rsa AAAAB3NzaDAxx2E"')
+ gitlab_keys.stub(:open)
+ gitlab_keys.send :add_key
end
- gitlab_keys.stub(auth_file: tmp_authorized_keys_path)
- gitlab_keys.send :rm_key
- File.read(tmp_authorized_keys_path).should == "first key\nthird key\n"
end
- it "should log an rm-key event" do
- $logger.should_receive(:info).with('Removing key key-741')
- gitlab_keys.send :rm_key
+ describe :rm_key do
+ let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
+
+ it "removes the right line" do
+ other_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-742\",options ssh-rsa AAAAB3NzaDAxx2E"
+ open(tmp_authorized_keys_path, 'a') do |auth_file|
+ auth_file.puts "command=\"#{ROOT_PATH}/bin/gitlab-shell key-741\",options ssh-rsa AAAAB3NzaDAxx2E"
+ auth_file.puts other_line
+ end
+ gitlab_keys.send :rm_key
+ File.read(tmp_authorized_keys_path).should == "existing content\n#{other_line}\n"
+ end
+
+ it "should log an rm-key event" do
+ $logger.should_receive(:info).with('Removing key key-741')
+ gitlab_keys.send :rm_key
+ end
end
end