3
require File.dirname(__FILE__) + '/../../spec_helper'
5
require 'puppet/ssl/certificate'
7
describe Puppet::SSL::Certificate do
9
@class = Puppet::SSL::Certificate
13
@class.instance_variable_set("@ca_location", nil)
16
it "should be extended with the Indirector module" do
17
@class.metaclass.should be_include(Puppet::Indirector)
20
it "should indirect certificate" do
21
@class.indirection.name.should == :certificate
24
it "should only support the text format" do
25
@class.supported_formats.should == [:s]
28
describe "when converting from a string" do
29
it "should create a certificate instance with its name set to the certificate subject and its content set to the extracted certificate" do
30
cert = stub 'certificate', :subject => "/CN=Foo.madstop.com"
31
OpenSSL::X509::Certificate.expects(:new).with("my certificate").returns(cert)
33
mycert = stub 'sslcert'
34
mycert.expects(:content=).with(cert)
36
@class.expects(:new).with("foo.madstop.com").returns mycert
38
@class.from_s("my certificate")
41
it "should create multiple certificate instances when asked" do
43
@class.expects(:from_s).with("cert1").returns cert1
45
@class.expects(:from_s).with("cert2").returns cert2
47
@class.from_multiple_s("cert1\n---\ncert2").should == [cert1, cert2]
51
describe "when converting to a string" do
53
@certificate = @class.new("myname")
56
it "should return an empty string when it has no certificate" do
57
@certificate.to_s.should == ""
60
it "should convert the certificate to pem format" do
61
certificate = mock 'certificate', :to_pem => "pem"
62
@certificate.content = certificate
63
@certificate.to_s.should == "pem"
66
it "should be able to convert multiple instances to a string" do
67
cert2 = @class.new("foo")
68
@certificate.expects(:to_s).returns "cert1"
69
cert2.expects(:to_s).returns "cert2"
71
@class.to_multiple_s([@certificate, cert2]).should == "cert1\n---\ncert2"
76
describe "when managing instances" do
78
@certificate = @class.new("myname")
81
it "should have a name attribute" do
82
@certificate.name.should == "myname"
85
it "should convert its name to a string and downcase it" do
86
@class.new(:MyName).name.should == "myname"
89
it "should have a content attribute" do
90
@certificate.should respond_to(:content)
93
it "should return a nil expiration if there is no actual certificate" do
94
@certificate.stubs(:content).returns nil
96
@certificate.expiration.should be_nil
99
it "should use the expiration of the certificate as its expiration date" do
101
@certificate.stubs(:content).returns cert
103
cert.expects(:not_after).returns "sometime"
105
@certificate.expiration.should == "sometime"
108
it "should be able to read certificates from disk" do
110
File.expects(:read).with(path).returns("my certificate")
111
certificate = mock 'certificate'
112
OpenSSL::X509::Certificate.expects(:new).with("my certificate").returns(certificate)
113
@certificate.read(path).should equal(certificate)
114
@certificate.content.should equal(certificate)
117
it "should have a :to_text method that it delegates to the actual key" do
118
real_certificate = mock 'certificate'
119
real_certificate.expects(:to_text).returns "certificatetext"
120
@certificate.content = real_certificate
121
@certificate.to_text.should == "certificatetext"