summaryrefslogtreecommitdiff
path: root/spec/unit/forge/repository_spec.rb
blob: 4ac77ecb873827e21de2a71c4596ad77f1d1d811 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# encoding: utf-8
require 'spec_helper'
require 'net/http'
require 'puppet/forge/repository'
require 'puppet/forge/cache'
require 'puppet/forge/errors'

describe Puppet::Forge::Repository do
  let(:agent) { "Test/1.0" }
  let(:repository) { Puppet::Forge::Repository.new('http://fake.com', agent) }
  let(:ssl_repository) { Puppet::Forge::Repository.new('https://fake.com', agent) }

  it "retrieve accesses the cache" do
    path = '/module/foo.tar.gz'
    repository.cache.expects(:retrieve)

    repository.retrieve(path)
  end

  it "retrieve merges forge URI and path specified" do
    host = 'http://fake.com/test'
    path = '/module/foo.tar.gz'
    uri  = [ host, path ].join('')

    repository = Puppet::Forge::Repository.new(host, agent)
    repository.cache.expects(:retrieve).with(uri)

    repository.retrieve(path)
  end

  describe "making a request" do
    before :each do
      proxy_settings_of("proxy", 1234)
    end

    it "returns the result object from the request" do
      result = "#{Object.new}"

      performs_an_http_request result do |http|
        http.expects(:request).with(responds_with(:path, "the_path"))
      end

      repository.make_http_request("the_path").should == result
    end

    it 'returns the result object from a request with ssl' do
      result = "#{Object.new}"
      performs_an_https_request result do |http|
        http.expects(:request).with(responds_with(:path, "the_path"))
      end

      ssl_repository.make_http_request("the_path").should == result
    end

    it 'return a valid exception when there is an SSL verification problem' do
      performs_an_https_request "#{Object.new}" do |http|
        http.expects(:request).with(responds_with(:path, "the_path")).raises OpenSSL::SSL::SSLError.new("certificate verify failed")
      end

      expect { ssl_repository.make_http_request("the_path") }.to raise_error Puppet::Forge::Errors::SSLVerifyError, 'Unable to verify the SSL certificate at https://fake.com'
    end

    it 'return a valid exception when there is a communication problem' do
      performs_an_http_request "#{Object.new}" do |http|
        http.expects(:request).with(responds_with(:path, "the_path")).raises SocketError
      end

      expect { repository.make_http_request("the_path") }.
        to raise_error Puppet::Forge::Errors::CommunicationError,
        'Unable to connect to the server at http://fake.com. Detail: SocketError.'
    end

    it "sets the user agent for the request" do
      path = 'the_path'

      request = repository.get_request_object(path)

      request['User-Agent'].should =~ /\b#{agent}\b/
      request['User-Agent'].should =~ /\bPuppet\b/
      request['User-Agent'].should =~ /\bRuby\b/
    end

    it "Does not set Authorization header by default" do
      Puppet.features.stubs(:pe_license?).returns(false)
      Puppet[:forge_authorization] = nil
      request = repository.get_request_object("the_path")
      request['Authorization'].should == nil
    end

    it "Sets Authorization header from config" do
      token = 'bearer some token'
      Puppet[:forge_authorization] = token
      request = repository.get_request_object("the_path")
      request['Authorization'].should == token
    end

    it "escapes the received URI" do
      unescaped_uri = "héllo world !! ç à"
      performs_an_http_request do |http|
        http.expects(:request).with(responds_with(:path, URI.escape(unescaped_uri)))
      end

      repository.make_http_request(unescaped_uri)
    end

    def performs_an_http_request(result = nil, &block)
      http = mock("http client")
      yield http

      proxy_class = mock("http proxy class")
      proxy = mock("http proxy")
      proxy_class.expects(:new).with("fake.com", 80).returns(proxy)
      proxy.expects(:start).yields(http).returns(result)
      Net::HTTP.expects(:Proxy).with("proxy", 1234, nil, nil).returns(proxy_class)
    end

    def performs_an_https_request(result = nil, &block)
      http = mock("http client")
      yield http

      proxy_class = mock("http proxy class")
      proxy = mock("http proxy")
      proxy_class.expects(:new).with("fake.com", 443).returns(proxy)
      proxy.expects(:start).yields(http).returns(result)
      proxy.expects(:use_ssl=).with(true)
      proxy.expects(:cert_store=)
      proxy.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
      Net::HTTP.expects(:Proxy).with("proxy", 1234, nil, nil).returns(proxy_class)
    end
  end

  describe "making a request against an authentiated proxy" do
    before :each do
      authenticated_proxy_settings_of("proxy", 1234, 'user1', 'password')
    end

    it "returns the result object from the request" do
      result = "#{Object.new}"

      performs_an_authenticated_http_request result do |http|
        http.expects(:request).with(responds_with(:path, "the_path"))
      end

      repository.make_http_request("the_path").should == result
    end

    it 'returns the result object from a request with ssl' do
      result = "#{Object.new}"
      performs_an_authenticated_https_request result do |http|
        http.expects(:request).with(responds_with(:path, "the_path"))
      end

      ssl_repository.make_http_request("the_path").should == result
    end

    it 'return a valid exception when there is an SSL verification problem' do
      performs_an_authenticated_https_request "#{Object.new}" do |http|
        http.expects(:request).with(responds_with(:path, "the_path")).raises OpenSSL::SSL::SSLError.new("certificate verify failed")
      end

      expect { ssl_repository.make_http_request("the_path") }.to raise_error Puppet::Forge::Errors::SSLVerifyError, 'Unable to verify the SSL certificate at https://fake.com'
    end

    it 'return a valid exception when there is a communication problem' do
      performs_an_authenticated_http_request "#{Object.new}" do |http|
        http.expects(:request).with(responds_with(:path, "the_path")).raises SocketError
      end

      expect { repository.make_http_request("the_path") }.
        to raise_error Puppet::Forge::Errors::CommunicationError,
        'Unable to connect to the server at http://fake.com. Detail: SocketError.'
    end

    it "sets the user agent for the request" do
      path = 'the_path'

      request = repository.get_request_object(path)

      request['User-Agent'].should =~ /\b#{agent}\b/
      request['User-Agent'].should =~ /\bPuppet\b/
      request['User-Agent'].should =~ /\bRuby\b/
    end

    it "escapes the received URI" do
      unescaped_uri = "héllo world !! ç à"
      performs_an_authenticated_http_request do |http|
        http.expects(:request).with(responds_with(:path, URI.escape(unescaped_uri)))
      end

      repository.make_http_request(unescaped_uri)
    end

    def performs_an_authenticated_http_request(result = nil, &block)
      http = mock("http client")
      yield http

      proxy_class = mock("http proxy class")
      proxy = mock("http proxy")
      proxy_class.expects(:new).with("fake.com", 80).returns(proxy)
      proxy.expects(:start).yields(http).returns(result)
      Net::HTTP.expects(:Proxy).with("proxy", 1234, "user1", "password").returns(proxy_class)
    end

    def performs_an_authenticated_https_request(result = nil, &block)
      http = mock("http client")
      yield http

      proxy_class = mock("http proxy class")
      proxy = mock("http proxy")
      proxy_class.expects(:new).with("fake.com", 443).returns(proxy)
      proxy.expects(:start).yields(http).returns(result)
      proxy.expects(:use_ssl=).with(true)
      proxy.expects(:cert_store=)
      proxy.expects(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
      Net::HTTP.expects(:Proxy).with("proxy", 1234, "user1", "password").returns(proxy_class)
    end
  end

  def proxy_settings_of(host, port)
    Puppet[:http_proxy_host] = host
    Puppet[:http_proxy_port] = port
  end

  def authenticated_proxy_settings_of(host, port, user, password)
    Puppet[:http_proxy_host] = host
    Puppet[:http_proxy_port] = port
    Puppet[:http_proxy_user] = user
    Puppet[:http_proxy_password] = password
  end
end