summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/key/file.rb
blob: add80b26c6874b7ebe0fdf361a4600dc7fbde23a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/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/key/file'

describe Puppet::SSL::Key::File do
    it "should have documentation" do
        Puppet::SSL::Key::File.doc.should be_instance_of(String)
    end

    it "should use the :privatekeydir as the collection directory" do
        Puppet.settings.expects(:value).with(:privatekeydir).returns "/key/dir"
        Puppet::SSL::Key::File.collection_directory.should == "/key/dir"
    end

    describe "when managing private keys" do
        before do
            @private = "/private/key/dir"
            @public = "/public/key/dir"
            Puppet.settings.stubs(:value).with(:privatekeydir).returns @private
            Puppet.settings.stubs(:value).with(:publickeydir).returns @public
            Puppet.settings.stubs(:use)

            @searcher = Puppet::SSL::Key::File.new

            @privatekey = File.join(@private, "myname" + ".pem")
            @publickey = File.join(@public, "myname" + ".pem")

            @public_key = stub 'public_key'
            @real_key = stub 'sslkey', :public_key => @public_key

            @key = stub 'key', :name => "myname", :content => @real_key
        end

        it "should save the public key when saving the private key" do
            FileTest.stubs(:directory?).returns true
            FileTest.stubs(:writable?).returns true

            File.stubs(:open).with(@privatekey, "w")

            fh = mock 'filehandle'

            File.expects(:open).with(@publickey, "w").yields fh
            @public_key.expects(:to_pem).returns "my pem"

            fh.expects(:print).with "my pem"

            @searcher.save(@key)
        end

        it "should destroy the public key when destroying the private key" do
            File.stubs(:unlink).with(@privatekey)
            FileTest.stubs(:exist?).with(@privatekey).returns true
            FileTest.expects(:exist?).with(@publickey).returns true
            File.expects(:unlink).with(@publickey)

            @searcher.destroy(@key)
        end

        it "should not fail if the public key does not exist when deleting the private key" do
            File.stubs(:unlink).with(@privatekey)

            FileTest.stubs(:exist?).with(@privatekey).returns true
            FileTest.expects(:exist?).with(@publickey).returns false
            File.expects(:unlink).with(@publickey).never

            @searcher.destroy(@key)
        end
    end
end