305d2c9e3c3926e6fd68fc9856ed971fd051aa58
[arvados.git] / spec / google / api_client / discovery_spec.rb
1 # Copyright 2010 Google Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 require 'spec_helper'
16
17 require 'signet/oauth_1/client'
18 require 'httpadapter/adapters/net_http'
19
20 require 'google/api_client'
21 require 'google/api_client/version'
22 require 'google/api_client/parsers/json_parser'
23
24 describe Google::APIClient, 'unconfigured' do
25   before do
26     @client = Google::APIClient.new
27   end
28
29   it 'should not be able to determine the discovery URI' do
30     (lambda do
31       @client.discovery_uri
32     end).should raise_error(ArgumentError)
33   end
34 end
35
36 describe Google::APIClient, 'configured for a bogus API' do
37   before do
38     @client = Google::APIClient.new(:service => 'bogus')
39   end
40
41   it 'should not be able to retrieve the discovery document' do
42     (lambda do
43       @client.discovery_document
44     end).should raise_error(Google::APIClient::TransmissionError)
45   end
46 end
47
48 describe Google::APIClient, 'configured for bogus authorization' do
49   it 'should raise a type error' do
50     (lambda do
51       Google::APIClient.new(:service => 'prediction', :authorization => 42)
52     end).should raise_error(TypeError)
53   end
54 end
55
56 describe Google::APIClient, 'configured for the prediction API' do
57   before do
58     @client = Google::APIClient.new(:service => 'prediction')
59   end
60
61   it 'should correctly determine the discovery URI' do
62     @client.discovery_uri.should ===
63       'http://www.googleapis.com/discovery/0.1/describe?api=prediction'
64   end
65
66   it 'should have multiple versions available' do
67     @client.discovered_services.size.should > 1
68   end
69
70   it 'should find APIs that are in the discovery document' do
71     @client.discovered_service('prediction').name.should == 'prediction'
72     @client.discovered_service('prediction').version.should == 'v1'
73     @client.discovered_service(:prediction).name.should == 'prediction'
74     @client.discovered_service(:prediction).version.should == 'v1'
75   end
76
77   it 'should find API versions that are in the discovery document' do
78     @client.discovered_service('prediction', 'v1.1').version.should == 'v1.1'
79   end
80
81   it 'should not find APIs that are not in the discovery document' do
82     @client.discovered_service('bogus').should == nil
83   end
84
85   it 'should raise an error for bogus services' do
86     (lambda do
87       @client.discovered_service(42)
88     end).should raise_error(TypeError)
89   end
90
91   it 'should find methods that are in the discovery document' do
92     @client.discovered_method('prediction.training.insert').name.should ==
93       'insert'
94     @client.discovered_method(:'prediction.training.insert').name.should ==
95       'insert'
96   end
97
98   it 'should find methods for versions that are in the discovery document' do
99     @client.discovered_method(
100       'prediction.training.delete', 'v1.1'
101     ).should_not == nil
102   end
103
104   it 'should not find methods that are not in the discovery document' do
105     @client.discovered_method('prediction.training.delete', 'v1').should == nil
106     @client.discovered_method('prediction.bogus').should == nil
107   end
108
109   it 'should raise an error for bogus methods' do
110     (lambda do
111       @client.discovered_method(42)
112     end).should raise_error(TypeError)
113   end
114
115   it 'should correctly determine the latest version' do
116     @client.latest_service_version('prediction').version.should_not == 'v1'
117     @client.latest_service_version(:prediction).version.should_not == 'v1'
118   end
119
120   it 'should raise an error for bogus services' do
121     (lambda do
122       @client.latest_service_version(42)
123     end).should raise_error(TypeError)
124   end
125
126   it 'should correctly determine the latest version' do
127     # Sanity check the algorithm
128     @client.discovered_services.clear
129     @client.discovered_services <<
130       Google::APIClient::Service.new('magic', 'v1', {})
131     @client.discovered_services <<
132       Google::APIClient::Service.new('magic', 'v1.1', {})
133     @client.discovered_services <<
134       Google::APIClient::Service.new('magic', 'v1.10', {})
135     @client.discovered_services <<
136       Google::APIClient::Service.new('magic', 'v10.1', {})
137     @client.discovered_services <<
138       Google::APIClient::Service.new('magic', 'v2.1', {})
139     @client.discovered_services <<
140       Google::APIClient::Service.new('magic', 'v10.0', {})
141     @client.latest_service_version('magic').version.should == 'v10.1'
142   end
143
144   it 'should correctly determine the latest version' do
145     # Sanity check the algorithm
146     @client.discovered_services.clear
147     @client.discovered_services <<
148       Google::APIClient::Service.new('one', 'v3', {})
149     @client.discovered_services <<
150       Google::APIClient::Service.new('two', 'v1', {})
151     @client.discovered_services <<
152       Google::APIClient::Service.new('two', 'v1.1-r1c3', {})
153     @client.discovered_services <<
154       Google::APIClient::Service.new('two', 'v2', {})
155     @client.discovered_services <<
156       Google::APIClient::Service.new('two', 'v2beta1', {})
157     @client.latest_service_version('two').version.should == 'v2'
158   end
159
160   it 'should return nil for bogus service names' do
161     # Sanity check the algorithm
162     @client.latest_service_version('bogus').should == nil
163   end
164
165   it 'should generate valid requests' do
166     request = @client.generate_request(
167       'prediction.training.insert',
168       {'query' => '12345'}
169     )
170     method, uri, headers, body = request
171     method.should == 'POST'
172     uri.should ==
173       'https://www.googleapis.com/prediction/v1/training?query=12345'
174     Hash[headers].should == {}
175     body.should respond_to(:each)
176   end
177
178   it 'should generate requests against the correct URIs' do
179     request = @client.generate_request(
180       :'prediction.training.insert',
181       {'query' => '12345'}
182     )
183     method, uri, headers, body = request
184     uri.should ==
185       'https://www.googleapis.com/prediction/v1/training?query=12345'
186   end
187
188   it 'should generate requests against the correct URIs' do
189     prediction = @client.discovered_service('prediction', 'v1')
190     request = @client.generate_request(
191       prediction.training.insert,
192       {'query' => '12345'}
193     )
194     method, uri, headers, body = request
195     uri.should ==
196       'https://www.googleapis.com/prediction/v1/training?query=12345'
197   end
198
199   it 'should generate signed requests' do
200     @client.authorization = :oauth_1
201     @client.authorization.token_credential_key = '12345'
202     @client.authorization.token_credential_secret = '12345'
203     request = @client.generate_request(
204       'prediction.training.insert',
205       {'query' => '12345'}
206     )
207     method, uri, headers, body = request
208     headers = Hash[headers]
209     headers.keys.should include('Authorization')
210     headers['Authorization'].should =~ /^OAuth/
211   end
212
213   it 'should not be able to execute improperly authorized requests' do
214     @client.authorization = :oauth_1
215     @client.authorization.token_credential_key = '12345'
216     @client.authorization.token_credential_secret = '12345'
217     response = @client.execute(
218       'prediction.training.insert',
219       {'query' => '12345'}
220     )
221     status, headers, body = response
222     status.should == 401
223   end
224
225   it 'should raise an error for bogus methods' do
226     (lambda do
227       @client.generate_request(42)
228     end).should raise_error(TypeError)
229   end
230
231   it 'should raise an error for bogus methods' do
232     (lambda do
233       @client.generate_request(@client.discovered_service('prediction'))
234     end).should raise_error(TypeError)
235   end
236 end
237
238 describe Google::APIClient, 'configured for the buzz API' do
239   before do
240     @client = Google::APIClient.new(:service => 'buzz')
241   end
242
243   it 'should correctly determine the discovery URI' do
244     @client.discovery_uri.should ===
245       'http://www.googleapis.com/discovery/0.1/describe?api=buzz'
246   end
247
248   it 'should find APIs that are in the discovery document' do
249     @client.discovered_service('buzz').name.should == 'buzz'
250     @client.discovered_service('buzz').version.should == 'v1'
251   end
252
253   it 'should not find APIs that are not in the discovery document' do
254     @client.discovered_service('bogus').should == nil
255   end
256
257   it 'should find methods that are in the discovery document' do
258     # TODO(bobaman) Fix this when the RPC names are correct
259     @client.discovered_method('chili.activities.list').name.should == 'list'
260   end
261
262   it 'should not find methods that are not in the discovery document' do
263     @client.discovered_method('buzz.bogus').should == nil
264   end
265
266   it 'should generate requests against the correct URIs' do
267     # TODO(bobaman) Fix this when the RPC names are correct
268     request = @client.generate_request(
269       'chili.activities.list',
270       {'userId' => 'hikingfan', 'scope' => '@public'},
271       '',
272       [],
273       {:signed => false}
274     )
275     method, uri, headers, body = request
276     uri.should ==
277       'https://www.googleapis.com/buzz/v1/activities/hikingfan/@public'
278   end
279
280   it 'should correctly validate parameters' do
281     # TODO(bobaman) Fix this when the RPC names are correct
282     (lambda do
283       @client.generate_request(
284         'chili.activities.list',
285         {'alt' => 'json'},
286         '',
287         [],
288         {:signed => false}
289       )
290     end).should raise_error(ArgumentError)
291   end
292
293   it 'should correctly validate parameters' do
294     # TODO(bobaman) Fix this when the RPC names are correct
295     (lambda do
296       @client.generate_request(
297         'chili.activities.list',
298         {'userId' => 'hikingfan', 'scope' => '@bogus'},
299         '',
300         [],
301         {:signed => false}
302       )
303     end).should raise_error(ArgumentError)
304   end
305
306   it 'should be able to execute requests without authorization' do
307     # TODO(bobaman) Fix this when the RPC names are correct
308     response = @client.execute(
309       'chili.activities.list',
310       {'alt' => 'json', 'userId' => 'hikingfan', 'scope' => '@public'},
311       '',
312       [],
313       {:signed => false}
314     )
315     status, headers, body = response
316     status.should == 200
317   end
318 end
319
320 describe Google::APIClient, 'configured for the latitude API' do
321   before do
322     @client = Google::APIClient.new(:service => 'latitude')
323   end
324
325   it 'should correctly determine the discovery URI' do
326     @client.discovery_uri.should ===
327       'http://www.googleapis.com/discovery/0.1/describe?api=latitude'
328   end
329
330   it 'should find APIs that are in the discovery document' do
331     @client.discovered_service('latitude').name.should == 'latitude'
332     @client.discovered_service('latitude').version.should == 'v1'
333   end
334
335   it 'should not find APIs that are not in the discovery document' do
336     @client.discovered_service('bogus').should == nil
337   end
338
339   it 'should find methods that are in the discovery document' do
340     @client.discovered_method('latitude.currentLocation.get').name.should ==
341       'get'
342   end
343
344   it 'should not find methods that are not in the discovery document' do
345     @client.discovered_method('latitude.bogus').should == nil
346   end
347
348   it 'should generate requests against the correct URIs' do
349     request = @client.generate_request(
350       'latitude.currentLocation.get',
351       {},
352       '',
353       [],
354       {:signed => false}
355     )
356     method, uri, headers, body = request
357     uri.should ==
358       'https://www.googleapis.com/latitude/v1/currentLocation'
359   end
360
361   it 'should not be able to execute requests without authorization' do
362     response = @client.execute(
363       'latitude.currentLocation.get',
364       {},
365       '',
366       [],
367       {:signed => false}
368     )
369     status, headers, body = response
370     status.should == 401
371   end
372 end
373
374 describe Google::APIClient, 'configured for the moderator API' do
375   before do
376     @client = Google::APIClient.new(:service => 'moderator')
377   end
378
379   it 'should correctly determine the discovery URI' do
380     @client.discovery_uri.should ===
381       'http://www.googleapis.com/discovery/0.1/describe?api=moderator'
382   end
383
384   it 'should find APIs that are in the discovery document' do
385     @client.discovered_service('moderator').name.should == 'moderator'
386     @client.discovered_service('moderator').version.should == 'v1'
387   end
388
389   it 'should not find APIs that are not in the discovery document' do
390     @client.discovered_service('bogus').should == nil
391   end
392
393   it 'should find methods that are in the discovery document' do
394     @client.discovered_method('moderator.profiles.get').name.should ==
395       'get'
396   end
397
398   it 'should not find methods that are not in the discovery document' do
399     @client.discovered_method('moderator.bogus').should == nil
400   end
401
402   it 'should generate requests against the correct URIs' do
403     request = @client.generate_request(
404       'moderator.profiles.get',
405       {},
406       '',
407       [],
408       {:signed => false}
409     )
410     method, uri, headers, body = request
411     uri.should ==
412       'https://www.googleapis.com/moderator/v1/profiles/@me'
413   end
414
415   it 'should not be able to execute requests without authorization' do
416     response = @client.execute(
417       'moderator.profiles.get',
418       {},
419       '',
420       [],
421       {:signed => false}
422     )
423     status, headers, body = response
424     status.should == 401
425   end
426 end