| 1 |
require File.dirname(__FILE__) + '/../spec_helper' |
| 2 |
require "fileutils" |
| 3 |
|
| 4 |
describe SshKeyFile do |
| 5 |
|
| 6 |
before(:each) do |
| 7 |
FileUtils.cp(File.join(fixture_path, "authorized_keys"), fixture_key_path) |
| 8 |
@keyfile = SshKeyFile.new(fixture_key_path) |
| 9 |
@keydata = ssh_keys(:johan).to_key |
| 10 |
end |
| 11 |
|
| 12 |
after(:each) do |
| 13 |
FileUtils.rm(fixture_key_path) |
| 14 |
end |
| 15 |
|
| 16 |
it "initializes with the path to the key file" do |
| 17 |
keyfile = SshKeyFile.new("foo/bar") |
| 18 |
keyfile.path.should == "foo/bar" |
| 19 |
end |
| 20 |
|
| 21 |
it "defaults to $HOME/.ssh/authorized_keys" do |
| 22 |
keyfile = SshKeyFile.new |
| 23 |
keyfile.path.should == File.join(File.expand_path("~"), ".ssh", "authorized_keys") |
| 24 |
end |
| 25 |
|
| 26 |
it "reads all the data in the file" do |
| 27 |
@keyfile.contents.should == File.read(fixture_key_path) |
| 28 |
end |
| 29 |
|
| 30 |
it "adds a key to the authorized_keys file" do |
| 31 |
@keyfile.add_key(@keydata) |
| 32 |
@keyfile.contents.should include(@keydata) |
| 33 |
end |
| 34 |
|
| 35 |
it "deletes a key from the file" do |
| 36 |
@keyfile.add_key(@keydata) |
| 37 |
@keyfile.delete_key(@keydata) |
| 38 |
@keyfile.contents.should_not include(@keydata) |
| 39 |
@keyfile.contents.should == File.read(File.join(fixture_path, "authorized_keys")) |
| 40 |
end |
| 41 |
|
| 42 |
it "doesn't rewrite the file unless the key to delete is in there" do |
| 43 |
File.should_not_receive(:open) |
| 44 |
@keyfile.delete_key(@keydata) |
| 45 |
end |
| 46 |
|
| 47 |
protected |
| 48 |
def fixture_key_path |
| 49 |
File.join(fixture_path, "authorized_keys_fixture") |
| 50 |
end |
| 51 |
|
| 52 |
end |