summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-03-10 10:37:57 -0700
committerLuke Kanies <luke@madstop.com>2008-04-15 21:34:04 -0500
commitec5bdf3b2d089d53f1f8fd986df83768564e79ac (patch)
treef2246b87471efe1f4b2689afb2a4abc1d2319843 /spec
parentbb87464f75cf3ea2e17bb660e7d1880bc36e141f (diff)
The basics for the certificate and certificate request
indirection terminii are done. I need to move most of the test code to a shared behaviour now.
Diffstat (limited to 'spec')
-rwxr-xr-xspec/unit/indirector/certificate/file.rb103
-rwxr-xr-xspec/unit/ssl/certificate.rb25
-rwxr-xr-xspec/unit/ssl/certificate_request.rb33
-rwxr-xr-xspec/unit/ssl/key.rb19
4 files changed, 177 insertions, 3 deletions
diff --git a/spec/unit/indirector/certificate/file.rb b/spec/unit/indirector/certificate/file.rb
new file mode 100755
index 000000000..159a87606
--- /dev/null
+++ b/spec/unit/indirector/certificate/file.rb
@@ -0,0 +1,103 @@
+#!/usr/bin/env ruby
+#
+# Created by Luke Kanies on 2008-3-7.
+# Copyright (c) 2007. All rights reserved.
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+require 'puppet/indirector/certificate/file'
+
+describe Puppet::SSL::Certificate::File do
+ before do
+ @file = Puppet::SSL::Certificate::File.new
+ @cert = Puppet::SSL::Certificate.new("myname")
+ Puppet.settings.stubs(:value).with(@file.class.directory_setting).returns "/test/dir"
+ @path = "/test/dir/myname.pem"
+ end
+
+ it "should have documentation" do
+ Puppet::SSL::Certificate::File.doc.should be_instance_of(String)
+ end
+
+ describe "when choosing the location for certificates" do
+ it "should set them in the :certdir, with the certificate name plus '.pem'" do
+ @file.path(@cert.name).should == @path
+ end
+ end
+
+ describe "when finding certificates on disk" do
+ describe "and no certificate is present" do
+ before do
+ FileTest.expects(:exist?).with(@path).returns false
+ end
+
+ it "should return nil" do
+ @file.find(@cert.name).should be_nil
+ end
+ end
+
+ describe "and a certificate is present" do
+ before do
+ FileTest.expects(:exist?).with(@path).returns true
+ end
+
+ it "should return an instance of the model, which it should use to read the certificate" do
+ cert = mock 'cert'
+ Puppet::SSL::Certificate.expects(:new).with("myname").returns cert
+ cert.expects(:read).with(@path)
+ @file.find("myname").should equal(cert)
+ end
+ end
+ end
+
+ describe "when saving certificates to disk" do
+ it "should fail if the directory is absent" do
+ FileTest.expects(:directory?).with(File.dirname(@path)).returns false
+ lambda { @file.save(@cert) }.should raise_error(Puppet::Error)
+ end
+
+ it "should fail if the directory is not writeable" do
+ FileTest.stubs(:directory?).returns true
+ FileTest.expects(:writable?).with(File.dirname(@path)).returns false
+ lambda { @file.save(@cert) }.should raise_error(Puppet::Error)
+ end
+
+ it "should save to the path the output of converting the certificate to a string" do
+ FileTest.stubs(:directory?).returns true
+ FileTest.stubs(:writable?).returns true
+
+ fh = mock 'filehandle'
+ File.expects(:open).with(@path, "w").yields(fh)
+
+ @cert.expects(:to_s).returns "mycert"
+
+ fh.expects(:print).with("mycert")
+
+ @file.save(@cert)
+
+ end
+ end
+
+ describe "when destroying certificates" do
+ describe "that do not exist" do
+ before do
+ FileTest.expects(:exist?).with(@path).returns false
+ end
+
+ it "should fail" do
+ lambda { @file.destroy(@cert) }.should raise_error(Puppet::Error)
+ end
+ end
+
+ describe "that exist" do
+ before do
+ FileTest.expects(:exist?).with(@path).returns true
+ end
+
+ it "should unlink the certificate file" do
+ File.expects(:unlink).with(@path)
+ @file.destroy(@cert)
+ end
+ end
+ end
+end
diff --git a/spec/unit/ssl/certificate.rb b/spec/unit/ssl/certificate.rb
index d3ae1b949..69f4e1fa1 100755
--- a/spec/unit/ssl/certificate.rb
+++ b/spec/unit/ssl/certificate.rb
@@ -19,15 +19,34 @@ describe Puppet::SSL::Certificate do
describe "when managing instances" do
before do
- @cert = @class.new("myname")
+ @certificate = @class.new("myname")
end
it "should have a name attribute" do
- @cert.name.should == "myname"
+ @certificate.name.should == "myname"
end
it "should have a content attribute" do
- @cert.should respond_to(:content)
+ @certificate.should respond_to(:content)
+ end
+
+ it "should be able to read certificates from disk" do
+ path = "/my/path"
+ File.expects(:read).with(path).returns("my certificate")
+ certificate = mock 'certificate'
+ OpenSSL::X509::Certificate.expects(:new).with("my certificate").returns(certificate)
+ @certificate.read(path).should equal(certificate)
+ @certificate.content.should equal(certificate)
+ end
+
+ it "should return an empty string when converted to a string with no certificate" do
+ @certificate.to_s.should == ""
+ end
+
+ it "should convert the certificate to pem format when converted to a string" do
+ certificate = mock 'certificate', :to_pem => "pem"
+ @certificate.content = certificate
+ @certificate.to_s.should == "pem"
end
end
diff --git a/spec/unit/ssl/certificate_request.rb b/spec/unit/ssl/certificate_request.rb
index 4bcec5567..48755a614 100755
--- a/spec/unit/ssl/certificate_request.rb
+++ b/spec/unit/ssl/certificate_request.rb
@@ -22,6 +22,39 @@ describe Puppet::SSL::CertificateRequest do
@class.new("myname").name.should == "myname"
end
+ describe "when managing instances" do
+ before do
+ @request = @class.new("myname")
+ end
+
+ it "should have a name attribute" do
+ @request.name.should == "myname"
+ end
+
+ it "should have a content attribute" do
+ @request.should respond_to(:content)
+ end
+
+ it "should be able to read requests from disk" do
+ path = "/my/path"
+ File.expects(:read).with(path).returns("my request")
+ request = mock 'request'
+ OpenSSL::X509::Request.expects(:new).with("my request").returns(request)
+ @request.read(path).should equal(request)
+ @request.content.should equal(request)
+ end
+
+ it "should return an empty string when converted to a string with no request" do
+ @request.to_s.should == ""
+ end
+
+ it "should convert the request to pem format when converted to a string" do
+ request = mock 'request', :to_pem => "pem"
+ @request.content = request
+ @request.to_s.should == "pem"
+ end
+ end
+
describe "when generating" do
before do
@instance = @class.new("myname")
diff --git a/spec/unit/ssl/key.rb b/spec/unit/ssl/key.rb
index f8a74dcf1..e580bbc55 100755
--- a/spec/unit/ssl/key.rb
+++ b/spec/unit/ssl/key.rb
@@ -28,6 +28,25 @@ describe Puppet::SSL::Key do
it "should have a content attribute" do
@key.should respond_to(:content)
end
+
+ it "should be able to read keys from disk" do
+ path = "/my/path"
+ File.expects(:read).with(path).returns("my key")
+ key = mock 'key'
+ OpenSSL::PKey::RSA.expects(:new).with("my key").returns(key)
+ @key.read(path).should equal(key)
+ @key.content.should equal(key)
+ end
+
+ it "should return an empty string when converted to a string with no key" do
+ @key.to_s.should == ""
+ end
+
+ it "should convert the key to pem format when converted to a string" do
+ key = mock 'key', :to_pem => "pem"
+ @key.content = key
+ @key.to_s.should == "pem"
+ end
end
describe "when generating the private key" do