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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/CHANGELOG.md +5 -0
  4. data/README.md +79 -144
  5. data/Rakefile +0 -11
  6. data/lib/vra/catalog.rb +39 -8
  7. data/lib/vra/catalog_base.rb +62 -0
  8. data/lib/vra/catalog_item.rb +28 -74
  9. data/lib/vra/catalog_source.rb +116 -0
  10. data/lib/vra/catalog_type.rb +56 -0
  11. data/lib/vra/client.rb +62 -54
  12. data/lib/vra/deployment.rb +155 -0
  13. data/lib/vra/deployment_request.rb +117 -0
  14. data/lib/vra/{resources.rb → deployments.rb} +26 -17
  15. data/lib/vra/exceptions.rb +1 -1
  16. data/lib/vra/http.rb +11 -6
  17. data/lib/vra/request.rb +28 -36
  18. data/lib/vra/request_parameters.rb +12 -12
  19. data/lib/vra/resource.rb +32 -203
  20. data/lib/vra/version.rb +2 -2
  21. data/lib/vra.rb +15 -12
  22. data/spec/catalog_item_spec.rb +64 -222
  23. data/spec/catalog_source_spec.rb +178 -0
  24. data/spec/catalog_spec.rb +112 -72
  25. data/spec/catalog_type_spec.rb +114 -0
  26. data/spec/client_spec.rb +271 -226
  27. data/spec/deployment_request_spec.rb +192 -0
  28. data/spec/deployment_spec.rb +227 -0
  29. data/spec/deployments_spec.rb +80 -0
  30. data/spec/fixtures/resource/sample_catalog_item.json +18 -0
  31. data/spec/fixtures/resource/sample_catalog_item_2.json +18 -0
  32. data/spec/fixtures/resource/sample_catalog_source.json +20 -0
  33. data/spec/fixtures/resource/sample_catalog_type.json +49 -0
  34. data/spec/fixtures/resource/sample_dep_actions.json +58 -0
  35. data/spec/fixtures/resource/sample_dep_request.json +19 -0
  36. data/spec/fixtures/resource/sample_dep_resource.json +112 -0
  37. data/spec/fixtures/resource/sample_deployment.json +26 -0
  38. data/spec/fixtures/resource/sample_entitlements.json +25 -0
  39. data/spec/http_spec.rb +63 -61
  40. data/spec/request_spec.rb +62 -68
  41. data/spec/resource_spec.rb +71 -390
  42. data/spec/spec_helper.rb +10 -4
  43. data/vmware-vra.gemspec +0 -1
  44. metadata +40 -30
  45. data/.travis.yml +0 -14
  46. data/lib/vra/catalog_request.rb +0 -137
  47. data/lib/vra/requests.rb +0 -41
  48. data/spec/catalog_request_spec.rb +0 -268
  49. data/spec/requests_spec.rb +0 -60
  50. 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) 2015 Chef Software, Inc.
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 "spec_helper"
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
- Vra::Client.new(username: "user@corp.local",
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
- Vra::Client.new(username: "user@corp.local",
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 "#initialize" do
39
- it "calls validate_client_options!" do
40
- client = Vra::Client.allocate
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, username: "user@corp.local",
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 "#bearer_token_request_body" do
50
- it "gets the correct password from the PasswordMasker object" do
51
- expect(client.bearer_token_request_body["password"]).to eq("password")
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 "#request_headers" do
56
- context "when bearer token exists" do
57
- it "has an Authorization header" do
58
- client.bearer_token = "12345"
59
- expect(client.request_headers.key?("Authorization")).to be true
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 "when bearer token does not exist" do
64
- it "has an Authorization header" do
65
- expect(client.request_headers.key?("Authorization")).to be false
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 "#authorize!" do
71
- context "when a token is not authorized or no token exists" do
72
- it "generates a token successfully" do
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(:generate_bearer_token)
75
+ expect(client).to receive(:generate_access_token)
75
76
 
76
77
  client.authorize!
77
78
  end
78
79
 
79
- it "raises an exception if token generation fails" do
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(:generate_bearer_token).and_raise(Vra::Exception::Unauthorized)
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 "raises an exception if a generated token is unauthorized" do
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(:generate_bearer_token)
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 "when a token is authorized" do
95
- it "does not generate a new token" do
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(:generate_bearer_token)
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 "#authorized?" do
105
- context "when token does not exist" do
106
- it "returns false" do
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 "when token exists" do
112
+ context 'when token exists' do
112
113
  before(:each) do
113
- client.bearer_token = "12345"
114
+ client.access_token = '12345'
115
+ client.refresh_token = '54321'
114
116
  end
115
117
 
116
- it "returns true if the token validates successfully" do
117
- response = double("response", success_no_content?: true, code: 204)
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 "returns false if the token validates unsuccessfully" do
124
- response = double("response", success_no_content?: false, code: 500)
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 "#generate_bearer_token" do
133
- payload = {
134
- "username" => "user@corp.local",
135
- "password" => "password",
136
- "tenant" => "tenant",
137
- }.to_json
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 "posts to the tokens API endpoint" do
140
- response = double("response", code: 200, body: '{"id":"12345"}', success_ok?: true)
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("/identity/api/tokens"),
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.generate_bearer_token
161
+ client.generate_access_token
150
162
  end
151
163
 
152
- context "when token is generated successfully" do
153
- it "sets the token" do
154
- response = double("response", code: 200, body: '{"id":"12345"}', success_ok?: true)
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.generate_bearer_token
169
+ client.generate_access_token
158
170
 
159
- expect(client.bearer_token).to eq "12345"
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 "when token is not generated successfully" do
164
- it "raises an exception" do
165
- response = double("response", code: 500, body: "error string", success_ok?: false)
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.generate_bearer_token }.to raise_error(Vra::Exception::Unauthorized)
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 "#full_url" do
174
- it "returns a properly formatted url" do
175
- expect(client.full_url("/mypath")).to eq "https://vra.corp.local/mypath"
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 "#http_head" do
180
- context "when skip_auth is nil" do
181
- it "authorizes before proceeding" do
182
- response = double("response")
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("/test")
199
+ client.http_head('/test')
187
200
  end
188
201
  end
189
202
 
190
- context "when skip_auth is not nil" do
191
- it "does not authorize before proceeding" do
192
- response = double("response")
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("/test", :skip_auth)
209
+ client.http_head('/test', :skip_auth)
197
210
  end
198
211
  end
199
212
 
200
- it "calls Vra::Http.execute" do
201
- response = double("response")
202
- path = "/test"
203
- full_url = "https://vra.corp.local/test"
204
- headers = { "Accept" => "application/json", "Content-Type" => "application/json" }
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
- expect(Vra::Http).to receive(:execute).with(method: :head,
209
- url: full_url,
210
- headers: headers,
211
- verify_ssl: verify_ssl)
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 "calls Vra::Http.execute with verify_ssl false" do
218
- response = double("response")
219
- path = "/test"
220
- full_url = "https://vra.corp.local/test"
221
- headers = { "Accept" => "application/json", "Content-Type" => "application/json" }
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).with(method: :head,
226
- url: full_url,
227
- headers: headers,
228
- verify_ssl: verify_ssl)
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 "raises an HTTPNotFound on a 404 error" do
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("message", 404, "Not Found"))
253
+ .and_raise(Vra::Http::Error.new('message', 404, 'Not Found'))
238
254
 
239
- expect { client.http_head("/404") }.to raise_error(Vra::Exception::HTTPNotFound)
255
+ expect { client.http_head('/404') }.to raise_error(Vra::Exception::HTTPNotFound)
240
256
  end
241
257
  end
242
258
 
243
- describe "#http_get" do
244
- context "when skip_auth is nil" do
245
- it "authorizes before proceeding" do
246
- response = double("response")
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("/test")
266
+ client.http_get('/test')
251
267
  end
252
268
  end
253
269
 
254
- context "when skip_auth is not nil" do
255
- it "does not authorize before proceeding" do
256
- response = double("response")
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("/test", :skip_auth)
276
+ client.http_get('/test', :skip_auth)
261
277
  end
262
278
  end
263
279
 
264
- it "calls Vra::Http.execute" do
265
- response = double("response")
266
- path = "/test"
267
- full_url = "https://vra.corp.local/test"
268
- headers = { "Accept" => "application/json", "Content-Type" => "application/json" }
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).with(method: :get,
273
- url: full_url,
274
- headers: headers,
275
- verify_ssl: verify_ssl)
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 "raises an HTTPNotFound on a 404 error" do
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("message", 404, "Not Found"))
301
+ .and_raise(Vra::Http::Error.new('message', 404, 'Not Found'))
285
302
 
286
- expect { client.http_get("/404") }.to raise_error(Vra::Exception::HTTPNotFound)
303
+ expect { client.http_get('/404') }.to raise_error(Vra::Exception::HTTPNotFound)
287
304
  end
288
305
  end
289
306
 
290
- describe "#http_get_paginated_array!" do
291
- it "allows a limit override" do
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("/test?limit=10&page=1")
295
- .and_return("content" => [], "metadata" => { "totalPages" => 1 })
311
+ .with('/test?$top=10&$skip=0')
312
+ .and_return('content' => [], 'totalPages' => 1)
296
313
 
297
- client.http_get_paginated_array!("/test")
314
+ client.http_get_paginated_array!('/test')
298
315
  end
299
316
 
300
- it "only calls get_parsed once when total pages is 0 (no items)" do
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("/test?limit=20&page=1")
304
- .and_return("content" => [], "metadata" => { "totalPages" => 0 })
320
+ .with('/test?$top=20&$skip=0')
321
+ .and_return('content' => [], 'totalPages' => 0)
305
322
 
306
- client.http_get_paginated_array!("/test")
323
+ client.http_get_paginated_array!('/test')
307
324
  end
308
325
 
309
- it "only calls get_parsed once when total pages is 1" do
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("/test?limit=20&page=1")
313
- .and_return("content" => [], "metadata" => { "totalPages" => 1 })
329
+ .with('/test?$top=20&$skip=0')
330
+ .and_return('content' => [], 'totalPages' => 1)
314
331
 
315
- client.http_get_paginated_array!("/test")
332
+ client.http_get_paginated_array!('/test')
316
333
  end
317
334
 
318
- it "calls get_parsed 3 times if there are 3 pages of response" do
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("/test?limit=20&page=1")
321
- .and_return("content" => [], "metadata" => { "totalPages" => 3 })
337
+ .with('/test?$top=20&$skip=0')
338
+ .and_return('content' => [], 'totalPages' => 3)
322
339
  expect(client).to receive(:get_parsed)
323
- .with("/test?limit=20&page=2")
324
- .and_return("content" => [], "metadata" => { "totalPages" => 3 })
340
+ .with('/test?$top=20&$skip=20')
341
+ .and_return('content' => [], 'totalPages' => 3)
325
342
  expect(client).to receive(:get_parsed)
326
- .with("/test?limit=20&page=3")
327
- .and_return("content" => [], "metadata" => { "totalPages" => 3 })
343
+ .with('/test?$top=20&$skip=40')
344
+ .and_return('content' => [], 'totalPages' => 3)
328
345
 
329
- client.http_get_paginated_array!("/test")
346
+ client.http_get_paginated_array!('/test')
330
347
  end
331
348
 
332
- it "raises an exception if duplicate items are returned by the API" do
349
+ it 'raises an exception if duplicate items are returned by the API' do
333
350
  allow(client).to receive(:get_parsed)
334
- .with("/test?limit=20&page=1")
335
- .and_return("content" => [ 1, 2, 3, 1 ], "metadata" => { "totalPages" => 1 })
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!("/test") }.to raise_error(Vra::Exception::DuplicateItemsDetected)
354
+ expect { client.http_get_paginated_array!('/test') }.to raise_error(Vra::Exception::DuplicateItemsDetected)
338
355
  end
339
356
  end
340
357
 
341
- describe "#http_post" do
342
- context "when skip_auth is nil" do
343
- it "authorizes before proceeding" do
344
- response = double("response")
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("/test", "some payload")
365
+ client.http_post('/test', 'some payload')
349
366
  end
350
367
  end
351
368
 
352
- context "when skip_auth is not nil" do
353
- it "does not authorize before proceeding" do
354
- response = double("response")
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("/test", "some payload", :skip_auth)
375
+ client.http_post('/test', 'some payload', :skip_auth)
359
376
  end
360
377
  end
361
378
 
362
- it "calls Vra::Http.execute" do
363
- response = double("response")
364
- path = "/test"
365
- full_url = "https://vra.corp.local/test"
366
- headers = { "Accept" => "application/json", "Content-Type" => "application/json" }
367
- payload = "some 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).with(method: :post,
372
- url: full_url,
373
- headers: headers,
374
- payload: payload,
375
- verify_ssl: verify_ssl)
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 "when not verifying ssl" do
399
+ context 'when not verifying ssl' do
382
400
  let(:unverified_client) do
383
- Vra::Client.new(username: "user@corp.local",
384
- password: "password",
385
- tenant: "tenant",
386
- base_url: "https://vra.corp.local",
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 "configures Net::HTTP with VERIFY_NONE" do
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("response", final?: true, success?: true)
415
+ double('response', final?: true, success?: true)
398
416
  end
399
417
 
400
- unverified_client.http_post("/path", "payload")
418
+ unverified_client.http_post('/path', 'payload')
401
419
 
402
- %i{head get}.each do |method|
403
- unverified_client.http_fetch(method, "/test", true)
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 "calls raise_http_exception upon error" do
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("/404", "test payload")
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 "#http_post!" do
418
- it "returns the response body" do
419
- response = double("response", body: "body text")
420
- allow(client).to receive(:http_post).with("/test", "test payload").and_return(response)
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
- expect(client.http_post!("/test", "test payload")).to eq "body text"
449
+ client.http_delete('/test')
423
450
  end
424
451
  end
425
452
 
426
- describe "#raise_http_exception" do
427
- context "when a 404 is received" do
453
+ describe '#raise_http_exception' do
454
+ context 'when a 404 is received' do
428
455
  let(:exception) do
429
- double("RestClient::ResourceNotFound",
456
+ double('RestClient::ResourceNotFound',
430
457
  http_code: 404,
431
- message: "Not Found",
432
- response: "404 Not Found")
458
+ message: 'Not Found',
459
+ response: '404 Not Found')
433
460
  end
434
461
 
435
- it "raises a Vra::Exception::HTTPNotFound exception" do
436
- expect { client.raise_http_exception(exception, "/test") }.to raise_error(Vra::Exception::HTTPNotFound)
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 "when an unspecified http error is received" do
467
+ context 'when an unspecified http error is received' do
441
468
  let(:exception) do
442
- double("RestClient::BadRequest",
469
+ double('RestClient::BadRequest',
443
470
  http_code: 400,
444
- message: "Bad Request",
445
- response: "400 Bad Request")
471
+ message: 'Bad Request',
472
+ response: '400 Bad Request')
446
473
  end
447
474
 
448
- it "raises a Vra::Exception::HTTPError exception" do
449
- expect { client.raise_http_exception(exception, "/test") }.to raise_error(Vra::Exception::HTTPError)
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 "#validate_client_options!" do
455
- context "when all required options are supplied" do
456
- it "does not raise an exception" do
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 "when username is missing" do
488
+ context 'when username is missing' do
462
489
  let(:client) do
463
- Vra::Client.new(password: "password",
464
- tenant: "tenant",
465
- base_url: "https://vra.corp.local")
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 "raises an exception" do
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 "when password is missing" do
502
+ context 'when password is missing' do
474
503
  let(:client) do
475
- Vra::Client.new(username: "username",
476
- tenant: "tenant",
477
- base_url: "https://vra.corp.local")
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 "raises an exception" do
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 "when tenant is missing" do
516
+ context 'when tenant is missing' do
486
517
  let(:client) do
487
- Vra::Client.new(username: "username",
488
- password: "password",
489
- base_url: "https://vra.corp.local")
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 "raises an exception" do
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 "when base URL is missing" do
530
+ context 'when base URL is missing' do
498
531
  let(:client) do
499
- Vra::Client.new(username: "username",
500
- password: "password",
501
- tenant: "tenant")
532
+ described_class.new(
533
+ username: 'username',
534
+ password: 'password',
535
+ tenant: 'tenant'
536
+ )
502
537
  end
503
538
 
504
- it "raises an exception" do
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 "when base URL is not a valid HTTP URL" do
544
+ context 'when base URL is not a valid HTTP URL' do
510
545
  let(:client) do
511
- Vra::Client.new(username: "username",
512
- password: "password",
513
- tenant: "tenant",
514
- base_url: "something-that-is-not-a-HTTP-URI")
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 "raises an exception" do
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