summaryrefslogtreecommitdiffstats
path: root/spec/unit/indirector/file_server.rb
blob: ba951737a0c7e6a4224ddfdf34dff8e6844f5a11 (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
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env ruby
#
#  Created by Luke Kanies on 2007-10-19.
#  Copyright (c) 2007. All rights reserved.

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

require 'puppet/indirector/file_server'
require 'puppet/file_serving/configuration'

describe Puppet::Indirector::FileServer do

    before :each do
        Puppet::Indirector::Terminus.stubs(:register_terminus_class)
        @model = mock 'model'
        @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
        Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)

        @file_server_class = Class.new(Puppet::Indirector::FileServer) do
            def self.to_s
                "Testing::Mytype"
            end
        end

        @file_server = @file_server_class.new

        @uri = "puppetmounts://host/my/local/file"
        @configuration = mock 'configuration'
        Puppet::FileServing::Configuration.stubs(:create).returns(@configuration)

        @request = Puppet::Indirector::Request.new(:myind, :mymethod, @uri)
    end

    describe Puppet::Indirector::FileServer, " when finding files" do

        it "should use the path portion of the URI as the file name" do
            @configuration.expects(:file_path).with("/my/local/file", :node => nil)
            @file_server.find(@request)
        end

        it "should use the FileServing configuration to convert the file name to a fully qualified path" do
            @configuration.expects(:file_path).with("/my/local/file", :node => nil)
            @file_server.find(@request)
        end

        it "should pass the node name to the FileServing configuration if one is provided" do
            @configuration.expects(:file_path).with("/my/local/file", :node => "testing")
            @request.node = "testing"
            @file_server.find(@request)
        end

        it "should return nil if no fully qualified path is found" do
            @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns(nil)
            @file_server.find(@request).should be_nil
        end

        it "should return an instance of the model created with the full path if a file is found" do
            @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/some/file")
            @model.expects(:new).returns(:myinstance)
            @file_server.find(@request).should == :myinstance
        end
    end

    describe Puppet::Indirector::FileServer, " when returning instances" do
        before :each do
            @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/some/file")
            @instance = mock 'instance'
        end

        it "should create the instance with the key used to find the instance" do
            @model.expects(:new).with { |key, *options| key == @uri }
            @file_server.find(@request)
        end

        it "should create the instance with the path at which the instance was found" do
            @model.expects(:new).with { |key, options| options[:path] == "/some/file" }
            @file_server.find(@request)
        end

        it "should set the provided :links setting on to the instance if one is provided" do
            @model.expects(:new).returns(@instance)
            @instance.expects(:links=).with(:mytest)
            @request.options[:links] = :mytest
            @file_server.find(@request)
        end

        it "should not set a :links value if no :links parameter is provided" do
            @model.expects(:new).returns(@instance)
            @file_server.find(@request)
        end
    end

    describe Puppet::Indirector::FileServer, " when checking authorization" do

        it "should have an authorization hook" do
            @file_server.should respond_to(:authorized?)
        end

        it "should deny the :destroy method" do
            @request.method = :destroy
            @file_server.authorized?(@request).should be_false
        end

        it "should deny the :save method" do
            @request.method = :save
            @file_server.authorized?(@request).should be_false
        end
        
        describe "and finding file information" do
            before do
                @request.key =  "puppetmounts://host/my/file"
                @request.method = :find 
            end

            it "should use the file server configuration to determine authorization" do
                @configuration.expects(:authorized?)
                @file_server.authorized?(@request)
            end

            it "should pass the file path from the URI to the file server configuration" do
                @configuration.expects(:authorized?).with { |uri, *args| uri == "/my/file" }
                @file_server.authorized?(@request)
            end

            it "should pass the node name to the file server configuration" do
                @configuration.expects(:authorized?).with { |key, options| options[:node] == "mynode" }
                @request.node = "mynode"
                @file_server.authorized?(@request)
            end

            it "should pass the IP address to the file server configuration" do
                @configuration.expects(:authorized?).with { |key, options| options[:ipaddress] == "myip" }
                @request.ip = "myip"
                @file_server.authorized?(@request)
            end

            it "should return false if the file server configuration denies authorization" do
                @configuration.expects(:authorized?).returns(false)
                @file_server.authorized?(@request)
            end

            it "should return true if the file server configuration approves authorization" do
                @configuration.expects(:authorized?).returns(true)
                @file_server.authorized?(@request)
            end
        end
    end

    describe Puppet::Indirector::FileServer, " when searching for files" do

        it "should use the path portion of the URI as the file name" do
            @configuration.expects(:file_path).with("/my/local/file", :node => nil)
            @file_server.search(@request)
        end

        it "should use the FileServing configuration to convert the file name to a fully qualified path" do
            @configuration.expects(:file_path).with("/my/local/file", :node => nil)
            @file_server.search(@request)
        end

        it "should pass the node name to the FileServing configuration if one is provided" do
            @configuration.expects(:file_path).with("/my/local/file", :node => "testing")
            @request.node = "testing"
            @file_server.search(@request)
        end

        it "should return nil if no fully qualified path is found" do
            @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns(nil)
            @file_server.search(@request).should be_nil
        end

        it "should use :path2instances from the terminus_helper to return instances if a module is found and the file exists" do
            @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/my/file")
            @file_server.expects(:path2instances)
            @file_server.search(@request)
        end

        it "should pass the request on to :path2instances" do
            @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/my/file")
            @file_server.expects(:path2instances).with(@request, "/my/file")
            @file_server.search(@request)
        end
    end
end