Commit ab008254 authored by Jacob Vosmaer's avatar Jacob Vosmaer

More file writing tests for GitlabKeys

parent de09438a
...@@ -14,15 +14,20 @@ describe GitlabKeys do ...@@ -14,15 +14,20 @@ describe GitlabKeys do
it { gitlab_keys.instance_variable_get(:@key_id).should == 'key-741' } it { gitlab_keys.instance_variable_get(:@key_id).should == 'key-741' }
end end
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
describe :add_key do describe :add_key do
let(:gitlab_keys) { build_gitlab_keys('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') } let(:gitlab_keys) { build_gitlab_keys('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
let(:file) { mock(:file) }
it "should receive valid cmd" do it "adds a line at the end of the file" 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 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 end
it "should log an add-key event" do it "should log an add-key event" do
...@@ -35,14 +40,14 @@ describe GitlabKeys do ...@@ -35,14 +40,14 @@ describe GitlabKeys do
describe :rm_key do describe :rm_key do
let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') } let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
it "should receive valid cmd" do it "removes the right line" do
FileUtils.mkdir_p(File.dirname(tmp_authorized_keys_path)) other_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-742\",options ssh-rsa AAAAB3NzaDAxx2E"
open(tmp_authorized_keys_path, 'w') do |auth_file| open(tmp_authorized_keys_path, 'a') do |auth_file|
auth_file.puts ['first key', '/bin/gitlab-shell key-741"', 'third key'] auth_file.puts "command=\"#{ROOT_PATH}/bin/gitlab-shell key-741\",options ssh-rsa AAAAB3NzaDAxx2E"
auth_file.puts other_line
end end
gitlab_keys.stub(auth_file: tmp_authorized_keys_path)
gitlab_keys.send :rm_key gitlab_keys.send :rm_key
File.read(tmp_authorized_keys_path).should == "first key\nthird key\n" File.read(tmp_authorized_keys_path).should == "existing content\n#{other_line}\n"
end end
it "should log an rm-key event" do it "should log an rm-key event" do
...@@ -50,6 +55,7 @@ describe GitlabKeys do ...@@ -50,6 +55,7 @@ describe GitlabKeys do
gitlab_keys.send :rm_key gitlab_keys.send :rm_key
end end
end end
end
describe :exec do describe :exec do
it 'add-key arg should execute add_key method' do it 'add-key arg should execute add_key method' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment