~nvalcarcel/ubuntu/lucid/puppet/fix-546677

« back to all changes in this revision

Viewing changes to spec/unit/ssl/certificate.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require File.dirname(__FILE__) + '/../../spec_helper'
 
4
 
 
5
require 'puppet/ssl/certificate'
 
6
 
 
7
describe Puppet::SSL::Certificate do
 
8
    before do
 
9
        @class = Puppet::SSL::Certificate
 
10
    end
 
11
 
 
12
    after do
 
13
        @class.instance_variable_set("@ca_location", nil)
 
14
    end
 
15
 
 
16
    it "should be extended with the Indirector module" do
 
17
        @class.metaclass.should be_include(Puppet::Indirector)
 
18
    end
 
19
 
 
20
    it "should indirect certificate" do
 
21
        @class.indirection.name.should == :certificate
 
22
    end
 
23
 
 
24
    it "should only support the text format" do
 
25
        @class.supported_formats.should == [:s]
 
26
    end
 
27
 
 
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)
 
32
 
 
33
            mycert = stub 'sslcert'
 
34
            mycert.expects(:content=).with(cert)
 
35
 
 
36
            @class.expects(:new).with("foo.madstop.com").returns mycert
 
37
 
 
38
            @class.from_s("my certificate")
 
39
        end
 
40
 
 
41
        it "should create multiple certificate instances when asked" do
 
42
            cert1 = stub 'cert1'
 
43
            @class.expects(:from_s).with("cert1").returns cert1
 
44
            cert2 = stub 'cert2'
 
45
            @class.expects(:from_s).with("cert2").returns cert2
 
46
 
 
47
            @class.from_multiple_s("cert1\n---\ncert2").should == [cert1, cert2]
 
48
        end
 
49
    end
 
50
 
 
51
    describe "when converting to a string" do
 
52
        before do
 
53
            @certificate = @class.new("myname")
 
54
        end
 
55
 
 
56
        it "should return an empty string when it has no certificate" do
 
57
            @certificate.to_s.should == ""
 
58
        end
 
59
 
 
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"
 
64
        end
 
65
 
 
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"
 
70
 
 
71
            @class.to_multiple_s([@certificate, cert2]).should == "cert1\n---\ncert2"
 
72
 
 
73
        end
 
74
    end
 
75
 
 
76
    describe "when managing instances" do
 
77
        before do
 
78
            @certificate = @class.new("myname")
 
79
        end
 
80
 
 
81
        it "should have a name attribute" do
 
82
            @certificate.name.should == "myname"
 
83
        end
 
84
 
 
85
        it "should convert its name to a string and downcase it" do
 
86
            @class.new(:MyName).name.should == "myname"
 
87
        end
 
88
 
 
89
        it "should have a content attribute" do
 
90
            @certificate.should respond_to(:content)
 
91
        end
 
92
 
 
93
        it "should return a nil expiration if there is no actual certificate" do
 
94
            @certificate.stubs(:content).returns nil
 
95
 
 
96
            @certificate.expiration.should be_nil
 
97
        end
 
98
 
 
99
        it "should use the expiration of the certificate as its expiration date" do
 
100
            cert = stub 'cert'
 
101
            @certificate.stubs(:content).returns cert
 
102
 
 
103
            cert.expects(:not_after).returns "sometime"
 
104
 
 
105
            @certificate.expiration.should == "sometime"
 
106
        end
 
107
 
 
108
        it "should be able to read certificates from disk" do
 
109
            path = "/my/path"
 
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)
 
115
        end
 
116
 
 
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"
 
122
        end
 
123
    end
 
124
end