This repository has been archived by the owner on Aug 29, 2018. It is now read-only.
/
rest_client_spec.rb
672 lines (627 loc) · 27.6 KB
/
rest_client_spec.rb
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
require 'base64'
require 'spec_helper'
require 'stringio'
require 'rest_spec_helper'
require 'rhc/rest'
module RHC
module Rest
describe Client do
after{ ENV['http_proxy'] = nil }
after{ ENV['HTTP_PROXY'] = nil }
it 'should set the proxy protocol if it is missing' do
ENV['http_proxy'] = 'foo.bar.com:8081'
expect{ RHC::Rest::Client.new.send(:httpclient_for, {}) }.to raise_error(ArgumentError)
end
it 'should not alter the proxy protocol if it is present' do
ENV['http_proxy'] = 'http://foo.bar.com:8081'
RHC::Rest::Client.new.send(:httpclient_for, {}).proxy.to_s.should == URI.parse(ENV['http_proxy']).to_s
end
it 'should not affect the proxy protocol if nil' do
ENV['http_proxy'] = nil
RHC::Rest::Client.new.send(:httpclient_for, {}).proxy.should be_nil
ENV['http_proxy'].should be_nil
end
let(:endpoint){ mock_href }
let(:username){ nil }
let(:password){ nil }
let(:use_debug){ false }
let(:client) do
respond_to?(:spec_versions) ?
RHC::Rest::Client.new(endpoint, username, password, use_debug, spec_versions) :
RHC::Rest::Client.new(endpoint, username, password, use_debug)
end
let(:client_links) { mock_response_links(mock_client_links) }
let(:domain_0_links) { mock_response_links(mock_domain_links('mock_domain_0')) }
let(:domain_1_links) { mock_response_links(mock_domain_links('mock_domain_1')) }
let(:app_0_links) { mock_response_links(mock_app_links('mock_domain_0', 'mock_app')) }
let(:user_links) { mock_response_links(mock_user_links) }
let(:key_links) { mock_response_links(mock_key_links) }
let(:api_links) { client_links }
context "#new" do
before do
stub_api_request(:get, '').
to_return({ :body => { :data => client_links, :supported_api_versions => [1.0, 1.1] }.to_json,
:status => 200
})
stub_api_request(:get, 'api_error').
to_raise(HTTPClient::BadResponseError.new('API Error'))
stub_api_request(:get, 'other_error').
to_raise(StandardError.new('Other Error'))
end
it "returns a client object from the required arguments" do
credentials = Base64.strict_encode64(mock_user + ":" + mock_pass)
client.api.send(:links).should == client_links
end
context "against an endpoint that won't connect" do
let(:endpoint){ mock_href('api_error') }
it "raises an error message" do
expect{ client.api }.to raise_error
end
end
context "against an endpoint that has a generic error" do
let(:endpoint){ mock_href('other_error') }
it "raises a generic error for any other error condition" do
expect{ client.api }.to raise_error(RHC::Rest::ConnectionException, "An unexpected error occured: Other Error")
end
end
end
describe "#new" do
context "when server supports API versions [1.0, 1.1]" do
before :each do
stub_api_request(:get, '').
to_return({ :body => { :data => client_links, :supported_api_versions => [1.0, 1.1] }.to_json,
:status => 200
})
stub_api_request(:get, 'api_error').
to_raise(HTTPClient::BadResponseError.new('API Error'))
stub_api_request(:get, 'other_error').
to_raise(StandardError.new('Other Error'))
end
context "when client is instantiated with [1.0, 1.1] as the preferred API versions" do
let(:spec_versions){ [1.0, 1.1] }
it "settles on 1.1 as the API version" do
client.api.api_version_negotiated.should == 1.1
end
end
context "when client is instantiated with [1.1, 1.0] as the preferred API versions" do
let(:spec_versions){ [1.1, 1.0] }
it "settles on 1.0 as the API version" do
client.api.api_version_negotiated.should == 1.0
end
end
context "when client is instantiated with [1.2, 1.3] as the preferred API versions" do
let(:spec_versions){ [1.2, 1.3] }
it "fails to negotiate an agreeable API version" do
client.api.api_version_negotiated.should be_nil
end
end
context "when client is instantiated with [1.1, 1.0, 1.3] as the preferred API versions" do
let(:spec_versions){ [1.1, 1.0, 1.3] }
it "settles on 1.0 as the API version" do
client.api.api_version_negotiated.should == 1.0
end
end
end
end
context "with an instantiated client " do
before do
stub_api_request(:get, '').
to_return({ :body => {
:data => api_links,
:supported_api_versions => [1.0, 1.1]
}.to_json,
:status => 200
})
end
context "#add_domain" do
before do
stub_api_request(:any, api_links['ADD_DOMAIN']['relative']).
to_return({ :body => {
:type => 'domain',
:supported_api_versions => [1.0, 1.1],
:data => {
:id => 'mock_domain',
:links => mock_response_links(mock_domain_links('mock_domain')),
}
}.to_json,
:status => 200
})
end
it "returns a domain object" do
domain = client.add_domain('mock_domain')
domain.class.should == RHC::Rest::Domain
domain.id.should == 'mock_domain'
domain.send(:links).should ==
mock_response_links(mock_domain_links('mock_domain'))
end
end
context "#domains" do
before(:each) do
stub_api_request(:any, api_links['LIST_DOMAINS']['relative']).
to_return({ :body => {
:type => 'domains',
:data =>
[{ :id => 'mock_domain_0',
:links => mock_response_links(mock_domain_links('mock_domain_0')),
},
{ :id => 'mock_domain_1',
:links => mock_response_links(mock_domain_links('mock_domain_1')),
}]
}.to_json,
:status => 200
}).
to_return({ :body => {
:type => 'domains',
:data => []
}.to_json,
:status => 200
})
end
it "returns a list of existing domains" do
domains = client.domains
domains.length.should equal(2)
(0..1).each do |idx|
domains[idx].class.should == RHC::Rest::Domain
domains[idx].id.should == "mock_domain_#{idx}"
domains[idx].send(:links).should ==
mock_response_links(mock_domain_links("mock_domain_#{idx}"))
end
end
it "returns an empty list when no domains exist" do
# Disregard the first response; this is for the previous expectiation.
domains = client.domains
client.instance_variable_set(:@domains, nil)
domains = client.domains
domains.length.should equal(0)
end
end
context "#find_domain" do
context "when server does not support SHOW_DOMAIN" do
before do
stub_api_request(:any, api_links['LIST_DOMAINS']['relative']).
to_return({ :body => {
:type => 'domains',
:data =>
[{ :id => 'mock_domain_0',
:links => mock_response_links(mock_domain_links('mock_domain_0')),
},
{ :id => 'mock_domain_1',
:links => mock_response_links(mock_domain_links('mock_domain_1')),
}]
}.to_json,
:status => 200
})
end
it "returns a domain object for matching domain IDs" do
match = nil
expect { match = client.find_domain('mock_domain_0') }.to_not raise_error
match.id.should == 'mock_domain_0'
match.class.should == RHC::Rest::Domain
end
it "returns a domain object for matching case-insensitive domain IDs" do
match = nil
expect { match = client.find_domain('MOCK_DOMAIN_0') }.to_not raise_error
match.id.should == 'mock_domain_0'
match.class.should == RHC::Rest::Domain
end
it "raise an error when no matching domain IDs can be found" do
expect { client.find_domain('mock_domain_2') }.to raise_error(RHC::Rest::DomainNotFoundException)
end
end
context "when server supports SHOW_DOMAIN" do
let(:api_links){ client_links.merge!(mock_response_links([['SHOW_DOMAIN', 'domains/:name', 'get']])) }
before do
stub_api_request(:any, api_links['SHOW_DOMAIN']['relative'].gsub(/:name/, 'mock_domain_0')).
to_return({ :body => {
:type => 'domain',
:data =>
{ :id => 'mock_domain_0',
:links => mock_response_links(mock_domain_links('mock_domain_0')),
}
}.to_json,
:status => 200
})
stub_api_request(:any, api_links['SHOW_DOMAIN']['relative'].gsub(/:name/, 'mock_domain_%^&')).
to_return({ :body => {
:type => 'domain',
:data =>
{ :id => 'mock_domain_%^&',
:links => mock_response_links(mock_domain_links('mock_domain_0')),
}
}.to_json,
:status => 200
})
stub_api_request(:any, api_links['SHOW_DOMAIN']['relative'].gsub(/:name/, 'mock_domain_2')).
to_return({ :body => {:messages => [{:exit_code => 127}]}.to_json,
:status => 404
})
end
it "returns a domain object for matching domain IDs" do
match = nil
expect { match = client.find_domain('mock_domain_0') }.to_not raise_error
match.id.should == 'mock_domain_0'
match.class.should == RHC::Rest::Domain
end
it "encodes special characters" do
match = nil
expect { match = client.find_domain('mock_domain_%^&') }.to_not raise_error
match.id.should == 'mock_domain_%^&'
match.class.should == RHC::Rest::Domain
end
it "raise an error when no matching domain IDs can be found" do
expect { client.find_domain('mock_domain_2') }.to raise_error(RHC::Rest::DomainNotFoundException)
end
end
end
context "when server supports LIST_DOMAINS_BY_OWNER" do
let(:api_links){ client_links.merge!(mock_response_links([['LIST_DOMAINS_BY_OWNER', 'domains', 'get']])) }
before do
stub_api_request(:any, "#{api_links['LIST_DOMAINS_BY_OWNER']['relative']}?owner=@self").
to_return({ :body => {
:type => 'domains',
:data => [{
:id => 'mock_domain_0',
:links => mock_response_links(mock_domain_links('mock_domain_0')),
}]
}.to_json,
:status => 200
})
end
it "returns owned domains when called" do
match = nil
expect { match = client.owned_domains }.to_not raise_error
match.length.should == 1
match.first.id.should == 'mock_domain_0'
match.first.class.should == RHC::Rest::Domain
end
end
context "find_application" do
let(:mock_domain){ 'mock_domain_0' }
let(:mock_app){ 'mock_app' }
let(:missing){ 'no_match' }
before(:each) do
stub_one_application(mock_domain, mock_app)
stub_one_application(mock_domain, missing, {
:type => nil,
:data => nil,
:messages => [{
:exit_code => 101,
:field => nil,
:severity => 'error',
:text => "Application #{missing} not found"
}],
:status => 'not_found'
}, 404)
stub_one_application(missing, mock_app, {
:type => nil,
:data => nil,
:messages => [{
:exit_code => 127,
:field => nil,
:severity => 'error',
:text => "Domain #{missing} not found"
}],
:status => 'not_found'
}, 404)
end
it "returns application object for nested application IDs" do
match = client.find_application(mock_domain, mock_app)
match.class.should == RHC::Rest::Application
match.name.should == mock_app
match.domain_id.should == mock_domain
match.send(:links).should ==
mock_response_links(mock_app_links(mock_domain, mock_app))
end
it "Raises an exception when no matching applications can be found" do
expect { client.find_application(mock_domain, missing) }.to raise_error(RHC::Rest::ApplicationNotFoundException)
end
it "Raises an exception when no matching domain can be found" do
expect { client.find_application(missing, mock_app) }.to raise_error(RHC::Rest::DomainNotFoundException)
end
end
describe RHC::Rest::Cartridge do
subject do
RHC::Rest::Cartridge.new({
:name => 'foo',
:links => mock_response_links([
['GET', 'broker/rest/cartridge', 'get']
])}, client)
end
context "when several messages are present" do
before do
stub_api_request(:get, 'broker/rest/cartridge', true).
with(:query => {:include => 'status_messages'}).
to_return(:body => {
:type => 'cartridge',
:data => {
:status_messages => [{:message => 'Test'}]
}
}.to_json)
end
its(:status){ should == [{'message' => 'Test'}] }
end
end
context "#cartridges" do
before(:each) do
stub_api_request(:any, api_links['LIST_CARTRIDGES']['relative']).
to_return({ :body => {
:type => 'cartridges',
:data =>
[{ :name => 'mock_cart_0',
:type => 'mock_cart_0_type',
:links => mock_response_links(mock_cart_links('mock_cart_0')),
},
{ :name => 'mock_cart_1',
:type => 'mock_cart_1_type',
:links => mock_response_links(mock_cart_links('mock_cart_1')),
}]
}.to_json,
:status => 200
}).
to_return({ :body => {
:type => 'cartridges',
:data => []
}.to_json,
:status => 200
})
end
it "returns a list of existing cartridges" do
carts = client.cartridges
carts.length.should equal(2)
(0..1).each do |idx|
carts[idx].class.should == RHC::Rest::Cartridge
carts[idx].name.should == "mock_cart_#{idx}"
carts[idx].type.should == "mock_cart_#{idx}_type"
carts[idx].send(:links).should ==
mock_response_links(mock_cart_links("mock_cart_#{idx}"))
end
end
it "caches cartridges on the client" do
# Disregard the first response; this is for the previous expectiation.
old = client.cartridges.length
client.cartridges.length.should equal(old)
client.instance_variable_set(:@cartridges, nil)
client.cartridges.length.should equal(0)
end
end
context "#find_cartridges" do
before(:each) do
stub_api_request(:any, api_links['LIST_CARTRIDGES']['relative']).
to_return({ :body => {
:type => 'cartridges',
:data =>
[{ :name => 'mock_cart_0',
:type => 'mock_cart_0_type',
:links => mock_response_links(mock_cart_links('mock_cart_0')),
},
{ :name => 'mock_cart_1',
:type => 'mock_cart_1_type',
:links => mock_response_links(mock_cart_links('mock_cart_1')),
},
{ :name => 'mock_nomatch_cart_0',
:type => 'mock_nomatch_cart_0_type',
:links => mock_response_links(mock_cart_links('mock_nomatch_cart_0')),
}
]
}.to_json,
:status => 200
})
end
it "returns a list of cartridge objects for matching cartridges" do
matches = client.find_cartridges('mock_cart_0')
matches.length.should equal(1)
matches[0].class.should == RHC::Rest::Cartridge
matches[0].name.should == 'mock_cart_0'
matches[0].type.should == 'mock_cart_0_type'
matches[0].send(:links).should ==
mock_response_links(mock_cart_links('mock_cart_0'))
end
it "returns an empty list when no matching cartridges can be found" do
matches = client.find_cartridges('no_match')
matches.length.should equal(0)
end
it "returns multiple cartridge matches" do
matches = client.find_cartridges :regex => "mock_cart_[0-9]"
matches.length.should equal(2)
end
end
context "#user" do
before(:each) do
stub_api_request(:any, api_links['GET_USER']['relative']).
to_return({ :body => {
:type => 'user',
:data =>
{ :login => mock_user,
:links => mock_response_links(mock_user_links)
}
}.to_json,
:status => 200
})
end
it "returns the user object associated with this client connection" do
user = client.user
user.class.should == RHC::Rest::User
user.login.should == mock_user
user.send(:links).should == mock_response_links(mock_user_links)
end
end
context "#find_key" do
before(:each) do
stub_api_request(:any, api_links['GET_USER']['relative']).
to_return({ :body => {
:type => 'user',
:data =>
{ :login => mock_user,
:links => mock_response_links(mock_user_links)
}
}.to_json,
:status => 200
})
stub_api_request(:any, user_links['LIST_KEYS']['relative']).
to_return({ :body => {
:type => 'keys',
:data =>
[{ :name => 'mock_key_0',
:type => 'mock_key_0_type',
:content => '123456789:0',
:links => mock_response_links(mock_key_links('mock_key_0'))
},
{ :name => 'mock_key_1',
:type => 'mock_key_1_type',
:content => '123456789:1',
:links => mock_response_links(mock_key_links('mock_key_1'))
}]
}.to_json,
:status => 200
})
end
it "returns a list of key objects for matching keys" do
key = nil
expect { key = client.find_key('mock_key_0') }.to_not raise_error
key.class.should == RHC::Rest::Key
key.name.should == 'mock_key_0'
key.type.should == 'mock_key_0_type'
key.content.should == '123456789:0'
key.send(:links).should ==
mock_response_links(mock_key_links('mock_key_0'))
end
it "raise an error when no matching keys can be found" do
expect { client.find_key('no_match') }.to raise_error(RHC::KeyNotFoundException)
end
end
context "#delete_key" do
before(:each) do
stub_api_request(:any, api_links['GET_USER']['relative']).
to_return({ :body => {
:type => 'user',
:data =>
{ :login => mock_user,
:links => mock_response_links(mock_user_links)
}
}.to_json,
:status => 200
})
stub_api_request(:any, user_links['LIST_KEYS']['relative']).
to_return({ :body => {
:type => 'keys',
:data =>
[{ :name => 'mock_key_0',
:type => 'mock_key_0_type',
:content => '123456789:0',
:links => mock_response_links(mock_key_links('mock_key_0'))
},
{ :name => 'mock_key_1',
:type => 'mock_key_1_type',
:content => '123456789:1',
:links => mock_response_links(mock_key_links('mock_key_1'))
}]
}.to_json,
:status => 200
})
stub_api_request(:post, key_links['DELETE']['relative']).
to_return({ :body => {}.to_json,
:status => 200
})
end
it "should delete keys" do
expect { client.delete_key('mock_key_0') }.to be_true
end
it 'raises an error if nonexistent key is requested' do
expect { client.find_key('no_match') }.to raise_error(RHC::KeyNotFoundException)
end
end
end
context "when server supports API versions 1.0 and 1.1" do
before :each do
stub_api_request(:get, '').
to_return({ :body => {
:data => api_links,
:supported_api_versions => [1.0, 1.1]
}.to_json,
:status => 200
})
end
context "when client supports API version 1.1" do
let(:spec_versions){ [1.1] }
describe "#api_version_negotiated" do
it "returns 1.1" do
client.api.api_version_negotiated.to_s.should == '1.1'
end
end
end
context "when client supports only API version 1.2" do
let(:spec_versions){ [1.2] }
describe "#api_version_negotiated" do
it 'returns nil' do
client.api.api_version_negotiated.should be_nil
end
end
end
context "when client supports only API version 0.9" do
describe "#new" do
let(:spec_versions){ [0.9] }
it "warns user that it is outdated" do
capture do
client.api
@output.rewind
@output.read.should =~ /client version may be outdated/
end
end
end
end
end
describe "#supports_sessions?" do
before{ subject.should_receive(:api).at_least(2).times.and_return(double) }
context "with ADD_AUTHORIZATION link" do
before{ subject.api.should_receive(:supports?).with('ADD_AUTHORIZATION').and_return(true) }
its(:supports_sessions?){ should be_true }
end
context "without ADD_AUTHORIZATION link" do
before{ subject.api.should_receive(:supports?).with('ADD_AUTHORIZATION').and_return(false) }
its(:supports_sessions?){ should be_false }
end
end
describe "#authorizations" do
before do
stub_api_request(:get, '').to_return({:body => {
:data => mock_response_links(mock_api_with_authorizations),
:supported_api_versions => [1.0, 1.1]
}.to_json,
:status => 200
})
stub_authorizations
end
it{ client.authorizations.first.token.should == 'a_token_value' }
it{ client.authorizations.first.note.should == 'an_authorization' }
it{ client.authorizations.first.expires_in_seconds.should == 60 }
end
end
end
end
module RHC
module Rest
describe HTTPClient do
end
describe WWWAuth::DeferredCredential do
subject{ described_class.new(nil, nil) }
its(:user){ should be_nil }
its(:passwd){ should be_nil }
context "with a username and password" do
subject{ described_class.new(username, password) }
let(:username){ 'a_user' }
let(:password){ 'a_password' }
its(:user){ should == username }
its(:passwd){ should == password }
its(:to_str){ should == ["#{username}:#{password}"].pack('m').tr("\n", '') }
end
context "with a deferred username and password" do
subject{ described_class.new(username, password) }
let(:username){ lambda{ 'a_user' } }
let(:password){ lambda{ 'a_password' } }
its(:user){ should == username.call }
its(:passwd){ should == password.call }
its(:to_str){ should == ["#{username.call}:#{password.call}"].pack('m').tr("\n", '') }
end
end
end
end