summaryrefslogtreecommitdiffstats
path: root/spec/unit/network/authconfig.rb
blob: 9d5f6154d0a8ed9c52d04cbeddbc026ec9663cfa (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'puppet/network/authconfig'

describe Puppet::Network::AuthConfig do
    before do
        @rights = stubs 'rights'
        Puppet::Network::Rights.stubs(:new).returns(@rights)
        @rights.stubs(:each).returns([])

        FileTest.stubs(:exists?).returns(true)
        File.stubs(:stat).returns(stub 'stat', :ctime => :now)
        Time.stubs(:now).returns :now

        @authconfig = Puppet::Network::AuthConfig.new("dummy", false)
    end

    describe "when initializing" do
        before :each do
            Puppet::Network::AuthConfig.any_instance.stubs(:read)
        end

        it "should use the authconfig default pathname if none provided" do
            Puppet.expects(:[]).with(:authconfig).returns("dummy")

            Puppet::Network::AuthConfig.new
        end

        it "should raise an error if no file is defined in fine" do
            Puppet.stubs(:[]).with(:authconfig).returns(nil)

            lambda { Puppet::Network::AuthConfig.new }.should raise_error(Puppet::DevError)
        end

        it "should read and parse the file if parsenow is true" do
            Puppet::Network::AuthConfig.any_instance.expects(:read)

            Puppet::Network::AuthConfig.new("dummy", true)
        end

    end

    describe "when checking authorization" do
        before :each do
            @authconfig.stubs(:read)
            @call = stub 'call', :intern => "name"
            @handler = stub 'handler', :intern => "handler"
            @method = stub_everything 'method'
            @request = stub 'request', :call => @call, :handler => @handler, :method => @method, :name => "me", :ip => "1.2.3.4"
        end

        it "should attempt to read the authconfig file" do
            @rights.stubs(:include?)

            @authconfig.expects(:read)

            @authconfig.allowed?(@request)
        end

        it "should use a name right if it exists" do
            right = stub 'right'

            @rights.stubs(:include?).with("name").returns(true)
            @rights.stubs(:[]).with("name").returns(right)

            right.expects(:allowed?).with("me", "1.2.3.4")

            @authconfig.allowed?(@request)
        end

        it "should use a namespace right otherwise" do
            right = stub 'right'

            @rights.stubs(:include?).with("name").returns(false)
            @rights.stubs(:include?).with("handler").returns(true)
            @rights.stubs(:[]).with("handler").returns(right)

            right.expects(:allowed?).with("me", "1.2.3.4")

            @authconfig.allowed?(@request)
        end

        it "should return whatever the found rights returns" do
            right = stub 'right'

            @rights.stubs(:include?).with("name").returns(true)
            @rights.stubs(:[]).with("name").returns(right)

            right.stubs(:allowed?).with("me", "1.2.3.4").returns(:returned)

            @authconfig.allowed?(@request).should == :returned
        end

    end

    describe "when parsing authconfig file" do
        before :each do
            @fd = stub 'fd'
            File.stubs(:open).yields(@fd)
            @rights.stubs(:include?).returns(false)
            @rights.stubs(:[])
        end

        it "should skip comments" do
            @fd.stubs(:each).yields('  # comment')

            @rights.expects(:newright).never

            @authconfig.read
        end

        it "should skip blank lines" do
            @fd.stubs(:each).yields('  ')

            @rights.expects(:newright).never

            @authconfig.read
        end

        it "should throw an error if read rights already exist" do
            @fd.stubs(:each).yields('[puppetca]')

            @rights.stubs(:include?).with("puppetca").returns(true)

            lambda { @authconfig.read }.should raise_error
        end

        it "should create a new right for found namespaces" do
            @fd.stubs(:each).yields('[puppetca]')

            @rights.expects(:newright).with("puppetca")

            @authconfig.read
        end

        it "should create a new right for each found namespace line" do
            @fd.stubs(:each).multiple_yields('[puppetca]', '[fileserver]')

            @rights.expects(:newright).with("puppetca")
            @rights.expects(:newright).with("fileserver")

            @authconfig.read
        end

        it "should create an allow ACE on each subsequent allow" do
            acl = stub 'acl', :info

            @fd.stubs(:each).multiple_yields('[puppetca]', 'allow 127.0.0.1')
            @rights.stubs(:newright).with("puppetca")
            @rights.stubs(:[]).returns(acl)

            acl.expects(:allow).with('127.0.0.1')

            @authconfig.read
        end

        it "should create a deny ACE on each subsequent allow" do
            acl = stub 'acl', :info

            @fd.stubs(:each).multiple_yields('[puppetca]', 'deny 127.0.0.1')
            @rights.stubs(:newright).with("puppetca")
            @rights.stubs(:[]).returns(acl)

            acl.expects(:deny).with('127.0.0.1')

            @authconfig.read
        end

    end

end