summaryrefslogtreecommitdiff
path: root/spec/gitlab_keys_spec.rb
diff options
context:
space:
mode:
authorJacob Vosmaer <contact@jacobvosmaer.nl>2013-11-20 12:33:55 +0100
committerJacob Vosmaer <contact@jacobvosmaer.nl>2013-11-20 12:34:14 +0100
commitc67395e924496016a2ccec6eb0e4c42612f6d545 (patch)
treeac487e995d9f553c48f8f9d700df028b9f32a45c /spec/gitlab_keys_spec.rb
parent9e00fb4078ca8a168687f876c1f6a98198a68155 (diff)
downloadgitlab-shell-c67395e924496016a2ccec6eb0e4c42612f6d545.tar.gz
Refactor file writing tests
Diffstat (limited to 'spec/gitlab_keys_spec.rb')
-rw-r--r--spec/gitlab_keys_spec.rb67
1 files changed, 34 insertions, 33 deletions
diff --git a/spec/gitlab_keys_spec.rb b/spec/gitlab_keys_spec.rb
index 89438fc..a0bf678 100644
--- a/spec/gitlab_keys_spec.rb
+++ b/spec/gitlab_keys_spec.rb
@@ -14,46 +14,41 @@ describe GitlabKeys do
it { gitlab_keys.instance_variable_get(:@key_id).should == 'key-741' }
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
- let(:gitlab_keys) { build_gitlab_keys('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
+ describe :add_key do
+ let(:gitlab_keys) { build_gitlab_keys('add-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 "adds a line at the end of the file" do
+ create_authorized_keys_fixture
+ 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 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
+ 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 :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
+ describe :rm_key do
+ let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
- it "should log an rm-key event" do
- $logger.should_receive(:info).with('Removing key key-741')
- gitlab_keys.send :rm_key
+ it "removes the right line" do
+ create_authorized_keys_fixture
+ 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
@@ -95,6 +90,12 @@ describe GitlabKeys do
end
end
+ def create_authorized_keys_fixture
+ 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
+
def tmp_authorized_keys_path
File.join(ROOT_PATH, 'tmp', 'authorized_keys')
end