vmware-vra 2.7.2 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -1
- data/CHANGELOG.md +5 -0
- data/README.md +79 -144
- data/Rakefile +0 -11
- data/lib/vra/catalog.rb +39 -8
- data/lib/vra/catalog_base.rb +62 -0
- data/lib/vra/catalog_item.rb +28 -74
- data/lib/vra/catalog_source.rb +116 -0
- data/lib/vra/catalog_type.rb +56 -0
- data/lib/vra/client.rb +62 -54
- data/lib/vra/deployment.rb +155 -0
- data/lib/vra/deployment_request.rb +117 -0
- data/lib/vra/{resources.rb → deployments.rb} +26 -17
- data/lib/vra/exceptions.rb +1 -1
- data/lib/vra/http.rb +11 -6
- data/lib/vra/request.rb +28 -36
- data/lib/vra/request_parameters.rb +12 -12
- data/lib/vra/resource.rb +32 -203
- data/lib/vra/version.rb +2 -2
- data/lib/vra.rb +15 -12
- data/spec/catalog_item_spec.rb +64 -222
- data/spec/catalog_source_spec.rb +178 -0
- data/spec/catalog_spec.rb +112 -72
- data/spec/catalog_type_spec.rb +114 -0
- data/spec/client_spec.rb +271 -226
- data/spec/deployment_request_spec.rb +192 -0
- data/spec/deployment_spec.rb +227 -0
- data/spec/deployments_spec.rb +80 -0
- data/spec/fixtures/resource/sample_catalog_item.json +18 -0
- data/spec/fixtures/resource/sample_catalog_item_2.json +18 -0
- data/spec/fixtures/resource/sample_catalog_source.json +20 -0
- data/spec/fixtures/resource/sample_catalog_type.json +49 -0
- data/spec/fixtures/resource/sample_dep_actions.json +58 -0
- data/spec/fixtures/resource/sample_dep_request.json +19 -0
- data/spec/fixtures/resource/sample_dep_resource.json +112 -0
- data/spec/fixtures/resource/sample_deployment.json +26 -0
- data/spec/fixtures/resource/sample_entitlements.json +25 -0
- data/spec/http_spec.rb +63 -61
- data/spec/request_spec.rb +62 -68
- data/spec/resource_spec.rb +71 -390
- data/spec/spec_helper.rb +10 -4
- data/vmware-vra.gemspec +0 -1
- metadata +40 -30
- data/.travis.yml +0 -14
- data/lib/vra/catalog_request.rb +0 -137
- data/lib/vra/requests.rb +0 -41
- data/spec/catalog_request_spec.rb +0 -268
- data/spec/requests_spec.rb +0 -60
- data/spec/resources_spec.rb +0 -71
data/spec/client_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
#
|
3
3
|
# Author:: Chef Partner Engineering (<partnereng@chef.io>)
|
4
|
-
# Copyright:: Copyright (c)
|
4
|
+
# Copyright:: Copyright (c) 2022 Chef Software, Inc.
|
5
5
|
# License:: Apache License, Version 2.0
|
6
6
|
#
|
7
7
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -17,111 +17,113 @@
|
|
17
17
|
# limitations under the License.
|
18
18
|
#
|
19
19
|
|
20
|
-
require
|
20
|
+
require 'spec_helper'
|
21
|
+
require 'pry'
|
22
|
+
# require 'pry-byebug'
|
21
23
|
|
22
24
|
describe Vra::Client do
|
25
|
+
let(:client_params) do
|
26
|
+
{
|
27
|
+
username: 'user@corp.local',
|
28
|
+
password: 'password',
|
29
|
+
tenant: 'tenant',
|
30
|
+
base_url: 'https://vra.corp.local'
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
23
34
|
let(:client) do
|
24
|
-
|
25
|
-
password: "password",
|
26
|
-
tenant: "tenant",
|
27
|
-
base_url: "https://vra.corp.local")
|
35
|
+
described_class.new(client_params)
|
28
36
|
end
|
29
37
|
|
30
38
|
let(:client_without_ssl) do
|
31
|
-
|
32
|
-
password: "password",
|
33
|
-
tenant: "tenant",
|
34
|
-
base_url: "https://vra.corp.local",
|
35
|
-
verify_ssl: false)
|
39
|
+
described_class.new(client_params.merge(verify_ssl: false))
|
36
40
|
end
|
37
41
|
|
38
|
-
describe
|
39
|
-
it
|
40
|
-
client =
|
42
|
+
describe '#initialize' do
|
43
|
+
it 'calls validate_client_options!' do
|
44
|
+
client = described_class.allocate
|
41
45
|
expect(client).to receive(:validate_client_options!)
|
42
|
-
client.send(:initialize,
|
43
|
-
password: "password",
|
44
|
-
tenant: "tenant",
|
45
|
-
base_url: "https://vra.corp.local")
|
46
|
+
client.send(:initialize, client_params)
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
49
|
-
describe
|
50
|
-
it
|
51
|
-
expect(client.
|
50
|
+
describe '#token_params' do
|
51
|
+
it 'gets the correct password from the PasswordMasker object' do
|
52
|
+
expect(client.token_params[:password]).to eq('password')
|
52
53
|
end
|
53
54
|
end
|
54
55
|
|
55
|
-
describe
|
56
|
-
context
|
57
|
-
it
|
58
|
-
client.
|
59
|
-
expect(client.request_headers.key?(
|
56
|
+
describe '#request_headers' do
|
57
|
+
context 'when bearer token exists' do
|
58
|
+
it 'has an Authorization header' do
|
59
|
+
client.access_token = '12345'
|
60
|
+
expect(client.request_headers.key?('csp-auth-token')).to be true
|
60
61
|
end
|
61
62
|
end
|
62
63
|
|
63
|
-
context
|
64
|
-
it
|
65
|
-
expect(client.request_headers.key?(
|
64
|
+
context 'when access token does not exist' do
|
65
|
+
it 'does not have an Authorization header' do
|
66
|
+
expect(client.request_headers.key?('csp-auth-token')).to be false
|
66
67
|
end
|
67
68
|
end
|
68
69
|
end
|
69
70
|
|
70
|
-
describe
|
71
|
-
context
|
72
|
-
it
|
71
|
+
describe '#authorize!' do
|
72
|
+
context 'when a token is not authorized or no token exists' do
|
73
|
+
it 'generates a token successfully' do
|
73
74
|
allow(client).to receive(:authorized?).twice.and_return(false, true)
|
74
|
-
expect(client).to receive(:
|
75
|
+
expect(client).to receive(:generate_access_token)
|
75
76
|
|
76
77
|
client.authorize!
|
77
78
|
end
|
78
79
|
|
79
|
-
it
|
80
|
+
it 'raises an exception if token generation fails' do
|
80
81
|
allow(client).to receive(:authorized?).and_return(false)
|
81
|
-
allow(client).to receive(:
|
82
|
+
allow(client).to receive(:generate_access_token).and_raise(Vra::Exception::Unauthorized)
|
82
83
|
|
83
84
|
expect { client.authorize! }.to raise_error(Vra::Exception::Unauthorized)
|
84
85
|
end
|
85
86
|
|
86
|
-
it
|
87
|
+
it 'raises an exception if a generated token is unauthorized' do
|
87
88
|
allow(client).to receive(:authorized).twice.and_return(false, false)
|
88
|
-
allow(client).to receive(:
|
89
|
+
allow(client).to receive(:generate_access_token)
|
89
90
|
|
90
91
|
expect { client.authorize! }.to raise_error(Vra::Exception::Unauthorized)
|
91
92
|
end
|
92
93
|
end
|
93
94
|
|
94
|
-
context
|
95
|
-
it
|
95
|
+
context 'when a token is authorized' do
|
96
|
+
it 'does not generate a new token' do
|
96
97
|
allow(client).to receive(:authorized?).and_return(true)
|
97
|
-
expect(client).to_not receive(:
|
98
|
+
expect(client).to_not receive(:generate_access_token)
|
98
99
|
|
99
100
|
client.authorize!
|
100
101
|
end
|
101
102
|
end
|
102
103
|
end
|
103
104
|
|
104
|
-
describe
|
105
|
-
context
|
106
|
-
it
|
105
|
+
describe '#authorized?' do
|
106
|
+
context 'when token does not exist' do
|
107
|
+
it 'returns false' do
|
107
108
|
expect(client.authorized?).to be false
|
108
109
|
end
|
109
110
|
end
|
110
111
|
|
111
|
-
context
|
112
|
+
context 'when token exists' do
|
112
113
|
before(:each) do
|
113
|
-
client.
|
114
|
+
client.access_token = '12345'
|
115
|
+
client.refresh_token = '54321'
|
114
116
|
end
|
115
117
|
|
116
|
-
it
|
117
|
-
response = double(
|
118
|
+
it 'returns true if the token validates successfully' do
|
119
|
+
response = double('response', success_no_content?: true, code: 204, success?: true)
|
118
120
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
119
121
|
|
120
122
|
expect(client.authorized?).to be true
|
121
123
|
end
|
122
124
|
|
123
|
-
it
|
124
|
-
response = double(
|
125
|
+
it 'returns false if the token validates unsuccessfully' do
|
126
|
+
response = double('response', success_no_content?: false, code: 401, success?: false)
|
125
127
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
126
128
|
|
127
129
|
expect(client.authorized?).to be false
|
@@ -129,261 +131,277 @@ describe Vra::Client do
|
|
129
131
|
end
|
130
132
|
end
|
131
133
|
|
132
|
-
describe
|
133
|
-
payload
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
134
|
+
describe '#generate_bearer_token' do
|
135
|
+
let(:payload) do
|
136
|
+
{
|
137
|
+
username: 'user@corp.local',
|
138
|
+
password: 'password',
|
139
|
+
tenant: 'tenant'
|
140
|
+
}.to_json
|
141
|
+
end
|
142
|
+
|
143
|
+
let(:success_response) do
|
144
|
+
{
|
145
|
+
access_token: '123456',
|
146
|
+
refresh_token: '654321',
|
147
|
+
id: '123456'
|
148
|
+
}.to_json
|
149
|
+
end
|
138
150
|
|
139
|
-
it
|
140
|
-
response = double(
|
151
|
+
it 'posts to the tokens API endpoint' do
|
152
|
+
response = double('response', code: 200, body: success_response, success_ok?: true)
|
141
153
|
expect(Vra::Http).to receive(:execute)
|
142
154
|
.with(method: :post,
|
143
|
-
url: client.full_url(
|
155
|
+
url: client.full_url(described_class::ACCESS_TOKEN_URL),
|
144
156
|
payload: payload,
|
145
157
|
headers: anything,
|
146
158
|
verify_ssl: true)
|
147
159
|
.and_return(response)
|
148
160
|
|
149
|
-
client.
|
161
|
+
client.generate_access_token
|
150
162
|
end
|
151
163
|
|
152
|
-
context
|
153
|
-
it
|
154
|
-
response = double(
|
164
|
+
context 'when token is generated successfully' do
|
165
|
+
it 'sets the token' do
|
166
|
+
response = double('response', code: 200, body: success_response, success_ok?: true)
|
155
167
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
156
168
|
|
157
|
-
client.
|
169
|
+
client.generate_access_token
|
158
170
|
|
159
|
-
expect(client.
|
171
|
+
expect(client.access_token).to eq '123456'
|
172
|
+
expect(client.refresh_token).to eq '654321'
|
160
173
|
end
|
161
174
|
end
|
162
175
|
|
163
|
-
context
|
164
|
-
it
|
165
|
-
response = double(
|
176
|
+
context 'when token is not generated successfully' do
|
177
|
+
it 'raises an exception' do
|
178
|
+
response = double('response', code: 400, body: 'error string', success_ok?: false)
|
166
179
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
167
180
|
|
168
|
-
expect { client.
|
181
|
+
expect { client.generate_access_token }.to raise_error(Vra::Exception::Unauthorized)
|
169
182
|
end
|
170
183
|
end
|
171
184
|
end
|
172
185
|
|
173
|
-
describe
|
174
|
-
it
|
175
|
-
expect(client.full_url(
|
186
|
+
describe '#full_url' do
|
187
|
+
it 'returns a properly formatted url' do
|
188
|
+
expect(client.full_url('/url_path')).to eq 'https://vra.corp.local/url_path'
|
176
189
|
end
|
177
190
|
end
|
178
191
|
|
179
|
-
describe
|
180
|
-
context
|
181
|
-
it
|
182
|
-
response = double(
|
192
|
+
describe '#http_head' do
|
193
|
+
context 'when skip_auth is nil' do
|
194
|
+
it 'authorizes before proceeding' do
|
195
|
+
response = double('response')
|
183
196
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
184
197
|
expect(client).to receive(:authorize!)
|
185
198
|
|
186
|
-
client.http_head(
|
199
|
+
client.http_head('/test')
|
187
200
|
end
|
188
201
|
end
|
189
202
|
|
190
|
-
context
|
191
|
-
it
|
192
|
-
response = double(
|
203
|
+
context 'when skip_auth is not nil' do
|
204
|
+
it 'does not authorize before proceeding' do
|
205
|
+
response = double('response')
|
193
206
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
194
207
|
expect(client).to_not receive(:authorize!)
|
195
208
|
|
196
|
-
client.http_head(
|
209
|
+
client.http_head('/test', :skip_auth)
|
197
210
|
end
|
198
211
|
end
|
199
212
|
|
200
|
-
it
|
201
|
-
response = double(
|
202
|
-
path =
|
203
|
-
full_url =
|
204
|
-
headers = {
|
213
|
+
it 'calls Vra::Http.execute' do
|
214
|
+
response = double('response')
|
215
|
+
path = '/test'
|
216
|
+
full_url = 'https://vra.corp.local/test'
|
217
|
+
headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
|
205
218
|
verify_ssl = true
|
206
219
|
|
207
220
|
allow(client).to receive(:authorize!)
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
221
|
+
|
222
|
+
expect(Vra::Http).to receive(:execute)
|
223
|
+
.with(method: :head,
|
224
|
+
url: full_url,
|
225
|
+
headers: headers,
|
226
|
+
verify_ssl: verify_ssl)
|
212
227
|
.and_return(response)
|
213
228
|
|
214
229
|
client.http_head(path)
|
215
230
|
end
|
216
231
|
|
217
|
-
it
|
218
|
-
response = double(
|
219
|
-
path =
|
220
|
-
full_url =
|
221
|
-
headers = {
|
232
|
+
it 'calls Vra::Http.execute with verify_ssl false' do
|
233
|
+
response = double('response')
|
234
|
+
path = '/test'
|
235
|
+
full_url = 'https://vra.corp.local/test'
|
236
|
+
headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
|
222
237
|
verify_ssl = false
|
223
238
|
|
224
239
|
allow(client_without_ssl).to receive(:authorize!)
|
225
|
-
expect(Vra::Http).to receive(:execute)
|
226
|
-
|
227
|
-
|
228
|
-
|
240
|
+
expect(Vra::Http).to receive(:execute)
|
241
|
+
.with(method: :head,
|
242
|
+
url: full_url,
|
243
|
+
headers: headers,
|
244
|
+
verify_ssl: verify_ssl)
|
229
245
|
.and_return(response)
|
230
246
|
|
231
247
|
client_without_ssl.http_head(path)
|
232
248
|
end
|
233
249
|
|
234
|
-
it
|
250
|
+
it 'raises an HTTPNotFound on a 404 error' do
|
235
251
|
allow(client).to receive(:authorize!)
|
236
252
|
allow(Vra::Http).to receive(:execute)
|
237
|
-
.and_raise(Vra::Http::Error.new(
|
253
|
+
.and_raise(Vra::Http::Error.new('message', 404, 'Not Found'))
|
238
254
|
|
239
|
-
expect { client.http_head(
|
255
|
+
expect { client.http_head('/404') }.to raise_error(Vra::Exception::HTTPNotFound)
|
240
256
|
end
|
241
257
|
end
|
242
258
|
|
243
|
-
describe
|
244
|
-
context
|
245
|
-
it
|
246
|
-
response = double(
|
259
|
+
describe '#http_get' do
|
260
|
+
context 'when skip_auth is nil' do
|
261
|
+
it 'authorizes before proceeding' do
|
262
|
+
response = double('response')
|
247
263
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
248
264
|
expect(client).to receive(:authorize!)
|
249
265
|
|
250
|
-
client.http_get(
|
266
|
+
client.http_get('/test')
|
251
267
|
end
|
252
268
|
end
|
253
269
|
|
254
|
-
context
|
255
|
-
it
|
256
|
-
response = double(
|
270
|
+
context 'when skip_auth is not nil' do
|
271
|
+
it 'does not authorize before proceeding' do
|
272
|
+
response = double('response')
|
257
273
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
258
274
|
expect(client).to_not receive(:authorize!)
|
259
275
|
|
260
|
-
client.http_get(
|
276
|
+
client.http_get('/test', :skip_auth)
|
261
277
|
end
|
262
278
|
end
|
263
279
|
|
264
|
-
it
|
265
|
-
response = double(
|
266
|
-
path =
|
267
|
-
full_url =
|
268
|
-
headers = {
|
280
|
+
it 'calls Vra::Http.execute' do
|
281
|
+
response = double('response')
|
282
|
+
path = '/test'
|
283
|
+
full_url = 'https://vra.corp.local/test'
|
284
|
+
headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
|
269
285
|
verify_ssl = true
|
270
286
|
|
271
287
|
allow(client).to receive(:authorize!)
|
272
|
-
expect(Vra::Http).to receive(:execute)
|
273
|
-
|
274
|
-
|
275
|
-
|
288
|
+
expect(Vra::Http).to receive(:execute)
|
289
|
+
.with(method: :get,
|
290
|
+
url: full_url,
|
291
|
+
headers: headers,
|
292
|
+
verify_ssl: verify_ssl)
|
276
293
|
.and_return(response)
|
277
294
|
|
278
295
|
client.http_get(path)
|
279
296
|
end
|
280
297
|
|
281
|
-
it
|
298
|
+
it 'raises an HTTPNotFound on a 404 error' do
|
282
299
|
allow(client).to receive(:authorize!)
|
283
300
|
allow(Vra::Http).to receive(:execute)
|
284
|
-
.and_raise(Vra::Http::Error.new(
|
301
|
+
.and_raise(Vra::Http::Error.new('message', 404, 'Not Found'))
|
285
302
|
|
286
|
-
expect { client.http_get(
|
303
|
+
expect { client.http_get('/404') }.to raise_error(Vra::Exception::HTTPNotFound)
|
287
304
|
end
|
288
305
|
end
|
289
306
|
|
290
|
-
describe
|
291
|
-
it
|
307
|
+
describe '#http_get_paginated_array!' do
|
308
|
+
it 'allows a limit override' do
|
292
309
|
client.page_size = 10
|
293
310
|
expect(client).to receive(:get_parsed)
|
294
|
-
.with(
|
295
|
-
.and_return(
|
311
|
+
.with('/test?$top=10&$skip=0')
|
312
|
+
.and_return('content' => [], 'totalPages' => 1)
|
296
313
|
|
297
|
-
client.http_get_paginated_array!(
|
314
|
+
client.http_get_paginated_array!('/test')
|
298
315
|
end
|
299
316
|
|
300
|
-
it
|
317
|
+
it 'only calls get_parsed once when total pages is 0 (no items)' do
|
301
318
|
expect(client).to receive(:get_parsed)
|
302
319
|
.once
|
303
|
-
.with(
|
304
|
-
.and_return(
|
320
|
+
.with('/test?$top=20&$skip=0')
|
321
|
+
.and_return('content' => [], 'totalPages' => 0)
|
305
322
|
|
306
|
-
client.http_get_paginated_array!(
|
323
|
+
client.http_get_paginated_array!('/test')
|
307
324
|
end
|
308
325
|
|
309
|
-
it
|
326
|
+
it 'only calls get_parsed once when total pages is 1' do
|
310
327
|
expect(client).to receive(:get_parsed)
|
311
328
|
.once
|
312
|
-
.with(
|
313
|
-
.and_return(
|
329
|
+
.with('/test?$top=20&$skip=0')
|
330
|
+
.and_return('content' => [], 'totalPages' => 1)
|
314
331
|
|
315
|
-
client.http_get_paginated_array!(
|
332
|
+
client.http_get_paginated_array!('/test')
|
316
333
|
end
|
317
334
|
|
318
|
-
it
|
335
|
+
it 'calls get_parsed 3 times if there are 3 pages of response' do
|
319
336
|
expect(client).to receive(:get_parsed)
|
320
|
-
.with(
|
321
|
-
.and_return(
|
337
|
+
.with('/test?$top=20&$skip=0')
|
338
|
+
.and_return('content' => [], 'totalPages' => 3)
|
322
339
|
expect(client).to receive(:get_parsed)
|
323
|
-
.with(
|
324
|
-
.and_return(
|
340
|
+
.with('/test?$top=20&$skip=20')
|
341
|
+
.and_return('content' => [], 'totalPages' => 3)
|
325
342
|
expect(client).to receive(:get_parsed)
|
326
|
-
.with(
|
327
|
-
.and_return(
|
343
|
+
.with('/test?$top=20&$skip=40')
|
344
|
+
.and_return('content' => [], 'totalPages' => 3)
|
328
345
|
|
329
|
-
client.http_get_paginated_array!(
|
346
|
+
client.http_get_paginated_array!('/test')
|
330
347
|
end
|
331
348
|
|
332
|
-
it
|
349
|
+
it 'raises an exception if duplicate items are returned by the API' do
|
333
350
|
allow(client).to receive(:get_parsed)
|
334
|
-
.with(
|
335
|
-
.and_return(
|
351
|
+
.with('/test?$top=20&$skip=0')
|
352
|
+
.and_return('content' => [1, 2, 3, 1], 'totalPages' => 1)
|
336
353
|
|
337
|
-
expect { client.http_get_paginated_array!(
|
354
|
+
expect { client.http_get_paginated_array!('/test') }.to raise_error(Vra::Exception::DuplicateItemsDetected)
|
338
355
|
end
|
339
356
|
end
|
340
357
|
|
341
|
-
describe
|
342
|
-
context
|
343
|
-
it
|
344
|
-
response = double(
|
358
|
+
describe '#http_post' do
|
359
|
+
context 'when skip_auth is nil' do
|
360
|
+
it 'authorizes before proceeding' do
|
361
|
+
response = double('response')
|
345
362
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
346
363
|
expect(client).to receive(:authorize!)
|
347
364
|
|
348
|
-
client.http_post(
|
365
|
+
client.http_post('/test', 'some payload')
|
349
366
|
end
|
350
367
|
end
|
351
368
|
|
352
|
-
context
|
353
|
-
it
|
354
|
-
response = double(
|
369
|
+
context 'when skip_auth is not nil' do
|
370
|
+
it 'does not authorize before proceeding' do
|
371
|
+
response = double('response')
|
355
372
|
allow(Vra::Http).to receive(:execute).and_return(response)
|
356
373
|
expect(client).to_not receive(:authorize!)
|
357
374
|
|
358
|
-
client.http_post(
|
375
|
+
client.http_post('/test', 'some payload', :skip_auth)
|
359
376
|
end
|
360
377
|
end
|
361
378
|
|
362
|
-
it
|
363
|
-
response = double(
|
364
|
-
path =
|
365
|
-
full_url =
|
366
|
-
headers = {
|
367
|
-
payload =
|
379
|
+
it 'calls Vra::Http.execute' do
|
380
|
+
response = double('response')
|
381
|
+
path = '/test'
|
382
|
+
full_url = 'https://vra.corp.local/test'
|
383
|
+
headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
|
384
|
+
payload = 'some payload'
|
368
385
|
verify_ssl = true
|
369
386
|
|
370
387
|
allow(client).to receive(:authorize!)
|
371
|
-
expect(Vra::Http).to receive(:execute)
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
388
|
+
expect(Vra::Http).to receive(:execute)
|
389
|
+
.with(method: :post,
|
390
|
+
url: full_url,
|
391
|
+
headers: headers,
|
392
|
+
payload: payload,
|
393
|
+
verify_ssl: verify_ssl)
|
376
394
|
.and_return(response)
|
377
395
|
|
378
396
|
client.http_post(path, payload)
|
379
397
|
end
|
380
398
|
|
381
|
-
context
|
399
|
+
context 'when not verifying ssl' do
|
382
400
|
let(:unverified_client) do
|
383
|
-
Vra::Client.new(username:
|
384
|
-
password:
|
385
|
-
tenant:
|
386
|
-
base_url:
|
401
|
+
Vra::Client.new(username: 'user@corp.local',
|
402
|
+
password: 'password',
|
403
|
+
tenant: 'tenant',
|
404
|
+
base_url: 'https://vra.corp.local',
|
387
405
|
verify_ssl: false)
|
388
406
|
end
|
389
407
|
|
@@ -391,132 +409,159 @@ describe Vra::Client do
|
|
391
409
|
allow(unverified_client).to receive(:authorized?).and_return(true)
|
392
410
|
end
|
393
411
|
|
394
|
-
it
|
412
|
+
it 'configures Net::HTTP with VERIFY_NONE' do
|
395
413
|
allow(Net::HTTP).to receive(:start).and_wrap_original do |_http, *args|
|
396
414
|
expect(args.last).to include(verify_mode: OpenSSL::SSL::VERIFY_NONE)
|
397
|
-
double(
|
415
|
+
double('response', final?: true, success?: true)
|
398
416
|
end
|
399
417
|
|
400
|
-
unverified_client.http_post(
|
418
|
+
unverified_client.http_post('/path', 'payload')
|
401
419
|
|
402
|
-
%i
|
403
|
-
unverified_client.http_fetch(method,
|
420
|
+
%i[head get].each do |method|
|
421
|
+
unverified_client.http_fetch(method, '/test', true)
|
404
422
|
end
|
405
423
|
end
|
406
424
|
end
|
407
425
|
|
408
|
-
it
|
426
|
+
it 'calls raise_http_exception upon error' do
|
409
427
|
allow(client).to receive(:authorize!)
|
410
428
|
allow(Vra::Http).to receive(:execute).and_raise(StandardError)
|
411
429
|
expect(client).to receive(:raise_http_exception)
|
412
430
|
|
413
|
-
client.http_post(
|
431
|
+
client.http_post('/404', 'test payload')
|
432
|
+
end
|
433
|
+
end
|
434
|
+
|
435
|
+
describe '#http_post!' do
|
436
|
+
it 'returns the response body' do
|
437
|
+
response = double('response', body: 'body text')
|
438
|
+
allow(client).to receive(:http_post).with('/test', 'test payload').and_return(response)
|
439
|
+
|
440
|
+
expect(client.http_post!('/test', 'test payload')).to eq 'body text'
|
414
441
|
end
|
415
442
|
end
|
416
443
|
|
417
|
-
describe
|
418
|
-
it
|
419
|
-
|
420
|
-
|
444
|
+
describe '#http_delete' do
|
445
|
+
it 'should perform the delete method' do
|
446
|
+
allow(client).to receive(:authorized?).and_return(true)
|
447
|
+
expect(Vra::Http).to receive(:execute)
|
421
448
|
|
422
|
-
|
449
|
+
client.http_delete('/test')
|
423
450
|
end
|
424
451
|
end
|
425
452
|
|
426
|
-
describe
|
427
|
-
context
|
453
|
+
describe '#raise_http_exception' do
|
454
|
+
context 'when a 404 is received' do
|
428
455
|
let(:exception) do
|
429
|
-
double(
|
456
|
+
double('RestClient::ResourceNotFound',
|
430
457
|
http_code: 404,
|
431
|
-
message:
|
432
|
-
response:
|
458
|
+
message: 'Not Found',
|
459
|
+
response: '404 Not Found')
|
433
460
|
end
|
434
461
|
|
435
|
-
it
|
436
|
-
expect { client.raise_http_exception(exception,
|
462
|
+
it 'raises a Vra::Exception::HTTPNotFound exception' do
|
463
|
+
expect { client.raise_http_exception(exception, '/test') }.to raise_error(Vra::Exception::HTTPNotFound)
|
437
464
|
end
|
438
465
|
end
|
439
466
|
|
440
|
-
context
|
467
|
+
context 'when an unspecified http error is received' do
|
441
468
|
let(:exception) do
|
442
|
-
double(
|
469
|
+
double('RestClient::BadRequest',
|
443
470
|
http_code: 400,
|
444
|
-
message:
|
445
|
-
response:
|
471
|
+
message: 'Bad Request',
|
472
|
+
response: '400 Bad Request')
|
446
473
|
end
|
447
474
|
|
448
|
-
it
|
449
|
-
expect { client.raise_http_exception(exception,
|
475
|
+
it 'raises a Vra::Exception::HTTPError exception' do
|
476
|
+
expect { client.raise_http_exception(exception, '/test') }.to raise_error(Vra::Exception::HTTPError)
|
450
477
|
end
|
451
478
|
end
|
452
479
|
end
|
453
480
|
|
454
|
-
describe
|
455
|
-
context
|
456
|
-
it
|
481
|
+
describe '#validate_client_options!' do
|
482
|
+
context 'when all required options are supplied' do
|
483
|
+
it 'does not raise an exception' do
|
457
484
|
expect { client.validate_client_options! }.not_to raise_error
|
458
485
|
end
|
459
486
|
end
|
460
487
|
|
461
|
-
context
|
488
|
+
context 'when username is missing' do
|
462
489
|
let(:client) do
|
463
|
-
|
464
|
-
|
465
|
-
|
490
|
+
described_class.new(
|
491
|
+
password: 'password',
|
492
|
+
tenant: 'tenant',
|
493
|
+
base_url: 'https://vra.corp.local'
|
494
|
+
)
|
466
495
|
end
|
467
496
|
|
468
|
-
it
|
497
|
+
it 'raises an exception' do
|
469
498
|
expect { client.validate_client_options! }.to raise_error(ArgumentError)
|
470
499
|
end
|
471
500
|
end
|
472
501
|
|
473
|
-
context
|
502
|
+
context 'when password is missing' do
|
474
503
|
let(:client) do
|
475
|
-
|
476
|
-
|
477
|
-
|
504
|
+
described_class.new(
|
505
|
+
username: 'username',
|
506
|
+
tenant: 'tenant',
|
507
|
+
base_url: 'https://vra.corp.local'
|
508
|
+
)
|
478
509
|
end
|
479
510
|
|
480
|
-
it
|
511
|
+
it 'raises an exception' do
|
481
512
|
expect { client.validate_client_options! }.to raise_error(ArgumentError)
|
482
513
|
end
|
483
514
|
end
|
484
515
|
|
485
|
-
context
|
516
|
+
context 'when tenant is missing' do
|
486
517
|
let(:client) do
|
487
|
-
|
488
|
-
|
489
|
-
|
518
|
+
described_class.new(
|
519
|
+
username: 'username',
|
520
|
+
password: 'password',
|
521
|
+
base_url: 'https://vra.corp.local'
|
522
|
+
)
|
490
523
|
end
|
491
524
|
|
492
|
-
it
|
525
|
+
it 'raises an exception' do
|
493
526
|
expect { client.validate_client_options! }.to raise_error(ArgumentError)
|
494
527
|
end
|
495
528
|
end
|
496
529
|
|
497
|
-
context
|
530
|
+
context 'when base URL is missing' do
|
498
531
|
let(:client) do
|
499
|
-
|
500
|
-
|
501
|
-
|
532
|
+
described_class.new(
|
533
|
+
username: 'username',
|
534
|
+
password: 'password',
|
535
|
+
tenant: 'tenant'
|
536
|
+
)
|
502
537
|
end
|
503
538
|
|
504
|
-
it
|
539
|
+
it 'raises an exception' do
|
505
540
|
expect { client.validate_client_options! }.to raise_error(ArgumentError)
|
506
541
|
end
|
507
542
|
end
|
508
543
|
|
509
|
-
context
|
544
|
+
context 'when base URL is not a valid HTTP URL' do
|
510
545
|
let(:client) do
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
546
|
+
described_class.new(
|
547
|
+
username: 'username',
|
548
|
+
password: 'password',
|
549
|
+
tenant: 'tenant',
|
550
|
+
base_url: 'something-that-is-not-a-HTTP-URI'
|
551
|
+
)
|
515
552
|
end
|
516
553
|
|
517
|
-
it
|
554
|
+
it 'raises an exception' do
|
518
555
|
expect { client.validate_client_options! }.to raise_error(ArgumentError)
|
519
556
|
end
|
557
|
+
|
558
|
+
it 'should raise an exception when the URI::InvalidURIError is raised' do
|
559
|
+
allow(URI).to receive(:parse).and_raise(URI::InvalidURIError)
|
560
|
+
|
561
|
+
expect { client.validate_client_options! }
|
562
|
+
.to raise_error(ArgumentError)
|
563
|
+
.with_message('Base URL something-that-is-not-a-HTTP-URI is not a valid URI.')
|
564
|
+
end
|
520
565
|
end
|
521
566
|
end
|
522
567
|
end
|