Merge branch 'master' of github.com:google/google-api-ruby-client
[arvados.git] / spec / google / api_client / discovery_spec.rb
1 # encoding:utf-8
2
3 # Copyright 2010 Google Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17
18 require 'spec_helper'
19
20 gem 'faraday', '~> 0.8.1'
21 require 'faraday'
22 require 'faraday/utils'
23 require 'multi_json'
24 require 'compat/multi_json'
25
26 gem 'signet', '~> 0.4.0'
27 require 'signet/oauth_1/client'
28
29 require 'google/api_client'
30 require 'google/api_client/version'
31
32 describe Google::APIClient do
33   CLIENT ||= Google::APIClient.new
34
35   after do
36     # Reset client to not-quite-pristine state
37     CLIENT.key = nil
38     CLIENT.user_ip = nil
39   end
40
41   it 'should raise a type error for bogus authorization' do
42     (lambda do
43       Google::APIClient.new(:authorization => 42)
44     end).should raise_error(TypeError)
45   end
46
47   it 'should not be able to retrieve the discovery document for a bogus API' do
48     (lambda do
49       CLIENT.discovery_document('bogus')
50     end).should raise_error(Google::APIClient::TransmissionError)
51     (lambda do
52       CLIENT.discovered_api('bogus')
53     end).should raise_error(Google::APIClient::TransmissionError)
54   end
55
56   it 'should raise an error for bogus services' do
57     (lambda do
58       CLIENT.discovered_api(42)
59     end).should raise_error(TypeError)
60   end
61
62   it 'should raise an error for bogus services' do
63     (lambda do
64       CLIENT.preferred_version(42)
65     end).should raise_error(TypeError)
66   end
67
68   it 'should raise an error for bogus methods' do
69     (lambda do
70       CLIENT.generate_request(42)
71     end).should raise_error(TypeError)
72   end
73
74   it 'should not return a preferred version for bogus service names' do
75     CLIENT.preferred_version('bogus').should == nil
76   end
77
78   describe 'with the prediction API' do
79     before do
80       CLIENT.authorization = nil
81       # The prediction API no longer exposes a v1, so we have to be
82       # careful about looking up the wrong API version.
83       @prediction = CLIENT.discovered_api('prediction', 'v1.2')
84     end
85
86     it 'should correctly determine the discovery URI' do
87       CLIENT.discovery_uri('prediction').should ===
88         'https://www.googleapis.com/discovery/v1/apis/prediction/v1/rest'
89     end
90
91     it 'should correctly determine the discovery URI if :user_ip is set' do
92       CLIENT.user_ip = '127.0.0.1'
93       request = CLIENT.generate_request(
94         :http_method => 'GET',
95         :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
96         :authenticated => false
97       )
98       request.to_env(Faraday.default_connection)[:url].to_s.should === (
99         'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
100         '?userIp=127.0.0.1'
101       )
102     end
103
104     it 'should correctly determine the discovery URI if :key is set' do
105       CLIENT.key = 'qwerty'
106       request = CLIENT.generate_request(
107         :http_method => 'GET',
108         :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
109         :authenticated => false
110       )
111       request.to_env(Faraday.default_connection)[:url].to_s.should === (
112         'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
113         '?key=qwerty'
114       )
115     end
116
117     it 'should correctly determine the discovery URI if both are set' do
118       CLIENT.key = 'qwerty'
119       CLIENT.user_ip = '127.0.0.1'
120       request = CLIENT.generate_request(
121         :http_method => 'GET',
122         :uri => CLIENT.discovery_uri('prediction', 'v1.2'),
123         :authenticated => false
124       )
125       Addressable::URI.parse(
126         request.to_env(Faraday.default_connection)[:url]
127       ).query_values.should == {
128         'key' => 'qwerty',
129         'userIp' => '127.0.0.1'
130       }
131     end
132
133     it 'should correctly generate API objects' do
134       CLIENT.discovered_api('prediction', 'v1.2').name.should == 'prediction'
135       CLIENT.discovered_api('prediction', 'v1.2').version.should == 'v1.2'
136       CLIENT.discovered_api(:prediction, 'v1.2').name.should == 'prediction'
137       CLIENT.discovered_api(:prediction, 'v1.2').version.should == 'v1.2'
138     end
139
140     it 'should discover methods' do
141       CLIENT.discovered_method(
142         'prediction.training.insert', 'prediction', 'v1.2'
143       ).name.should == 'insert'
144       CLIENT.discovered_method(
145         :'prediction.training.insert', :prediction, 'v1.2'
146       ).name.should == 'insert'
147       CLIENT.discovered_method(
148         'prediction.training.delete', 'prediction', 'v1.2'
149       ).name.should == 'delete'
150     end
151
152     it 'should define the origin API in discovered methods' do
153       CLIENT.discovered_method(
154         'prediction.training.insert', 'prediction', 'v1.2'
155       ).api.name.should == 'prediction'
156     end
157
158     it 'should not find methods that are not in the discovery document' do
159       CLIENT.discovered_method(
160         'prediction.bogus', 'prediction', 'v1.2'
161       ).should == nil
162     end
163
164     it 'should raise an error for bogus methods' do
165       (lambda do
166         CLIENT.discovered_method(42, 'prediction', 'v1.2')
167       end).should raise_error(TypeError)
168     end
169
170     it 'should raise an error for bogus methods' do
171       (lambda do
172         CLIENT.generate_request(CLIENT.discovered_api('prediction', 'v1.2'))
173       end).should raise_error(TypeError)
174     end
175
176     it 'should correctly determine the preferred version' do
177       CLIENT.preferred_version('prediction').version.should_not == 'v1'
178       CLIENT.preferred_version(:prediction).version.should_not == 'v1'
179     end
180
181     it 'should return a batch path' do
182       CLIENT.discovered_api('prediction', 'v1.2').batch_path.should_not be_nil
183     end
184
185     it 'should generate valid requests' do
186       request = CLIENT.generate_request(
187         :api_method => @prediction.training.insert,
188         :parameters => {'data' => '12345'}
189       )
190       request.method.should == :post
191       request.to_env(Faraday.default_connection)[:url].to_s.should ===
192         'https://www.googleapis.com/prediction/v1.2/training?data=12345'
193       request.headers.should be_empty
194       request.body.should == ''
195     end
196
197     it 'should generate valid requests when repeated parameters are passed' do
198       request = CLIENT.generate_request(
199         :api_method => @prediction.training.insert,
200         :parameters => [['data', '1'], ['data','2']]
201       )
202       request.method.should == :post
203       request.to_env(Faraday.default_connection)[:url].to_s.should ===
204         'https://www.googleapis.com/prediction/v1.2/training?data=1&data=2'
205     end
206
207     it 'should generate requests against the correct URIs' do
208       request = CLIENT.generate_request(
209         :api_method => @prediction.training.insert,
210         :parameters => {'data' => '12345'}
211       )
212       request.to_env(Faraday.default_connection)[:url].to_s.should ===
213         'https://www.googleapis.com/prediction/v1.2/training?data=12345'
214     end
215
216     it 'should generate requests against the correct URIs' do
217       request = CLIENT.generate_request(
218         :api_method => @prediction.training.insert,
219         :parameters => {'data' => '12345'}
220       )
221       request.to_env(Faraday.default_connection)[:url].to_s.should ===
222         'https://www.googleapis.com/prediction/v1.2/training?data=12345'
223     end
224
225     it 'should allow modification to the base URIs for testing purposes' do
226       # Using a new client instance here to avoid caching rebased discovery doc
227       prediction_rebase =
228         Google::APIClient.new.discovered_api('prediction', 'v1.2')
229       prediction_rebase.method_base =
230         'https://testing-domain.example.com/prediction/v1.2/'
231       request = CLIENT.generate_request(
232         :api_method => prediction_rebase.training.insert,
233         :parameters => {'data' => '123'}
234       )
235       request.to_env(Faraday.default_connection)[:url].to_s.should === (
236         'https://testing-domain.example.com/' +
237         'prediction/v1.2/training?data=123'
238       )
239     end
240
241     it 'should generate OAuth 1 requests' do
242       CLIENT.authorization = :oauth_1
243       CLIENT.authorization.token_credential_key = '12345'
244       CLIENT.authorization.token_credential_secret = '12345'
245       request = CLIENT.generate_request(
246         :api_method => @prediction.training.insert,
247         :parameters => {'data' => '12345'}
248       )
249       request.headers.should have_key('Authorization')
250       request.headers['Authorization'].should =~ /^OAuth/
251     end
252
253     it 'should generate OAuth 2 requests' do
254       CLIENT.authorization = :oauth_2
255       CLIENT.authorization.access_token = '12345'
256       request = CLIENT.generate_request(
257         :api_method => @prediction.training.insert,
258         :parameters => {'data' => '12345'}
259       )
260       request.headers.should have_key('Authorization')
261       request.headers['Authorization'].should =~ /^Bearer/
262     end
263
264     it 'should not be able to execute improperly authorized requests' do
265       CLIENT.authorization = :oauth_1
266       CLIENT.authorization.token_credential_key = '12345'
267       CLIENT.authorization.token_credential_secret = '12345'
268       result = CLIENT.execute(
269         @prediction.training.insert,
270         {'data' => '12345'}
271       )
272       result.response.status.should == 401
273     end
274
275     it 'should not be able to execute improperly authorized requests' do
276       CLIENT.authorization = :oauth_2
277       CLIENT.authorization.access_token = '12345'
278       result = CLIENT.execute(
279         @prediction.training.insert,
280         {'data' => '12345'}
281       )
282       result.response.status.should == 401
283     end
284
285     it 'should not be able to execute improperly authorized requests' do
286       (lambda do
287         CLIENT.authorization = :oauth_1
288         CLIENT.authorization.token_credential_key = '12345'
289         CLIENT.authorization.token_credential_secret = '12345'
290         result = CLIENT.execute!(
291           @prediction.training.insert,
292           {'data' => '12345'}
293         )
294       end).should raise_error(Google::APIClient::ClientError)
295     end
296
297     it 'should not be able to execute improperly authorized requests' do
298       (lambda do
299         CLIENT.authorization = :oauth_2
300         CLIENT.authorization.access_token = '12345'
301         result = CLIENT.execute!(
302           @prediction.training.insert,
303           {'data' => '12345'}
304         )
305       end).should raise_error(Google::APIClient::ClientError)
306     end
307
308     it 'should correctly handle unnamed parameters' do
309       CLIENT.authorization = :oauth_2
310       CLIENT.authorization.access_token = '12345'
311       result = CLIENT.execute(
312         @prediction.training.insert,
313         {},
314         MultiJson.dump({"id" => "bucket/object"}),
315         {'Content-Type' => 'application/json'}
316       )
317       result.request.headers['Content-Type'].should == 'application/json'
318     end
319   end
320
321   describe 'with the plus API' do
322     before do
323       CLIENT.authorization = nil
324       @plus = CLIENT.discovered_api('plus')
325     end
326
327     it 'should correctly determine the discovery URI' do
328       CLIENT.discovery_uri('plus').should ===
329         'https://www.googleapis.com/discovery/v1/apis/plus/v1/rest'
330     end
331
332     it 'should find APIs that are in the discovery document' do
333       CLIENT.discovered_api('plus').name.should == 'plus'
334       CLIENT.discovered_api('plus').version.should == 'v1'
335       CLIENT.discovered_api(:plus).name.should == 'plus'
336       CLIENT.discovered_api(:plus).version.should == 'v1'
337     end
338
339     it 'should find methods that are in the discovery document' do
340       # TODO(bobaman) Fix this when the RPC names are correct
341       CLIENT.discovered_method(
342         'plus.activities.list', 'plus'
343       ).name.should == 'list'
344     end
345
346     it 'should define the origin API in discovered methods' do
347       CLIENT.discovered_method(
348         'plus.activities.list', 'plus'
349       ).api.name.should == 'plus'
350     end
351
352     it 'should not find methods that are not in the discovery document' do
353       CLIENT.discovered_method('plus.bogus', 'plus').should == nil
354     end
355
356     it 'should generate requests against the correct URIs' do
357       request = CLIENT.generate_request(
358         :api_method => @plus.activities.list,
359         :parameters => {
360           'userId' => '107807692475771887386', 'collection' => 'public'
361         },
362         :authenticated => false
363       )
364       request.to_env(Faraday.default_connection)[:url].to_s.should === (
365         'https://www.googleapis.com/plus/v1/' +
366         'people/107807692475771887386/activities/public'
367       )
368     end
369
370     it 'should correctly validate parameters' do
371       (lambda do
372         CLIENT.generate_request(
373           :api_method => @plus.activities.list,
374           :parameters => {'alt' => 'json'},
375           :authenticated => false
376         )
377       end).should raise_error(ArgumentError)
378     end
379
380     it 'should correctly validate parameters' do
381       (lambda do
382         CLIENT.generate_request(
383           :api_method => @plus.activities.list,
384           :parameters => {
385             'userId' => '107807692475771887386', 'collection' => 'bogus'
386           },
387           :authenticated => false
388         )
389       end).should raise_error(ArgumentError)
390     end
391   end
392
393   describe 'with the latitude API' do
394     before do
395       CLIENT.authorization = nil
396       @latitude = CLIENT.discovered_api('latitude')
397     end
398
399     it 'should correctly determine the discovery URI' do
400       CLIENT.discovery_uri('latitude').should ===
401         'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
402     end
403
404     it 'should find APIs that are in the discovery document' do
405       CLIENT.discovered_api('latitude').name.should == 'latitude'
406       CLIENT.discovered_api('latitude').version.should == 'v1'
407     end
408
409     it 'should return a batch path' do
410       CLIENT.discovered_api('latitude').batch_path.should_not be_nil
411     end
412
413     it 'should find methods that are in the discovery document' do
414       CLIENT.discovered_method(
415         'latitude.currentLocation.get', 'latitude'
416       ).name.should == 'get'
417     end
418
419     it 'should define the origin API in discovered methods' do
420       CLIENT.discovered_method(
421         'latitude.currentLocation.get', 'latitude'
422       ).api.name.should == 'latitude'
423     end
424
425     it 'should not find methods that are not in the discovery document' do
426       CLIENT.discovered_method('latitude.bogus', 'latitude').should == nil
427     end
428
429     it 'should generate requests against the correct URIs' do
430       request = CLIENT.generate_request(
431         :api_method => 'latitude.currentLocation.get',
432         :authenticated => false
433       )
434       request.to_env(Faraday.default_connection)[:url].to_s.should ===
435         'https://www.googleapis.com/latitude/v1/currentLocation'
436     end
437
438     it 'should generate requests against the correct URIs' do
439       request = CLIENT.generate_request(
440         :api_method => @latitude.current_location.get,
441         :authenticated => false
442       )
443       request.to_env(Faraday.default_connection)[:url].to_s.should ===
444         'https://www.googleapis.com/latitude/v1/currentLocation'
445     end
446
447     it 'should not be able to execute requests without authorization' do
448       result = CLIENT.execute(
449         :api_method => 'latitude.currentLocation.get',
450         :authenticated => false
451       )
452       result.response.status.should == 401
453     end
454   end
455
456   describe 'with the moderator API' do
457     before do
458       CLIENT.authorization = nil
459       @moderator = CLIENT.discovered_api('moderator')
460     end
461
462     it 'should correctly determine the discovery URI' do
463       CLIENT.discovery_uri('moderator').should ===
464         'https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest'
465     end
466
467     it 'should find APIs that are in the discovery document' do
468       CLIENT.discovered_api('moderator').name.should == 'moderator'
469       CLIENT.discovered_api('moderator').version.should == 'v1'
470     end
471
472     it 'should find methods that are in the discovery document' do
473       CLIENT.discovered_method(
474         'moderator.profiles.get', 'moderator'
475       ).name.should == 'get'
476     end
477
478     it 'should define the origin API in discovered methods' do
479       CLIENT.discovered_method(
480         'moderator.profiles.get', 'moderator'
481       ).api.name.should == 'moderator'
482     end
483
484     it 'should not find methods that are not in the discovery document' do
485       CLIENT.discovered_method('moderator.bogus', 'moderator').should == nil
486     end
487
488     it 'should return a batch path' do
489       CLIENT.discovered_api('moderator').batch_path.should_not be_nil
490     end
491
492     it 'should generate requests against the correct URIs' do
493       request = CLIENT.generate_request(
494         :api_method => 'moderator.profiles.get',
495         :authenticated => false
496       )
497       request.to_env(Faraday.default_connection)[:url].to_s.should ===
498         'https://www.googleapis.com/moderator/v1/profiles/@me'
499     end
500
501     it 'should generate requests against the correct URIs' do
502       request = CLIENT.generate_request(
503         :api_method => @moderator.profiles.get,
504         :authenticated => false
505       )
506       request.to_env(Faraday.default_connection)[:url].to_s.should ===
507         'https://www.googleapis.com/moderator/v1/profiles/@me'
508     end
509
510     it 'should not be able to execute requests without authorization' do
511       result = CLIENT.execute(
512         'moderator.profiles.get',
513         {},
514         '',
515         [],
516         {:authenticated => false}
517       )
518       result.response.status.should == 401
519     end
520   end
521
522   describe 'with the adsense API' do
523     before do
524       CLIENT.authorization = nil
525       @adsense = CLIENT.discovered_api('adsense', 'v1')
526     end
527
528     it 'should correctly determine the discovery URI' do
529       CLIENT.discovery_uri('adsense').should ===
530         'https://www.googleapis.com/discovery/v1/apis/adsense/v1/rest'
531     end
532
533     it 'should find APIs that are in the discovery document' do
534       CLIENT.discovered_api('adsense').name.should == 'adsense'
535       CLIENT.discovered_api('adsense').version.should == 'v1'
536     end
537
538     it 'should return a batch path' do
539       CLIENT.discovered_api('adsense').batch_path.should_not be_nil
540     end
541
542     it 'should find methods that are in the discovery document' do
543       CLIENT.discovered_method(
544         'adsense.reports.generate', 'adsense'
545       ).name.should == 'generate'
546     end
547
548     it 'should not find methods that are not in the discovery document' do
549       CLIENT.discovered_method('adsense.bogus', 'adsense').should == nil
550     end
551
552     it 'should generate requests against the correct URIs' do
553       request = CLIENT.generate_request(
554         :api_method => 'adsense.adclients.list',
555         :authenticated => false
556       )
557       request.to_env(Faraday.default_connection)[:url].to_s.should ===
558         'https://www.googleapis.com/adsense/v1/adclients'
559     end
560
561     it 'should generate requests against the correct URIs' do
562       request = CLIENT.generate_request(
563         :api_method => @adsense.adclients.list,
564         :authenticated => false
565       )
566       request.to_env(Faraday.default_connection)[:url].to_s.should ===
567         'https://www.googleapis.com/adsense/v1/adclients'
568     end
569
570     it 'should not be able to execute requests without authorization' do
571       result = CLIENT.execute(
572         :api_method => 'adsense.adclients.list',
573         :authenticated => false
574       )
575       result.response.status.should == 401
576     end
577
578     it 'should fail when validating missing required parameters' do
579       (lambda do
580         CLIENT.generate_request(
581           :api_method => @adsense.reports.generate,
582           :authenticated => false
583         )
584       end).should raise_error(ArgumentError)
585     end
586
587     it 'should succeed when validating parameters in a correct call' do
588       (lambda do
589         CLIENT.generate_request(
590           :api_method => @adsense.reports.generate,
591           :parameters => {
592             'startDate' => '2000-01-01',
593             'endDate' => '2010-01-01',
594             'dimension' => 'DATE',
595             'metric' => 'PAGE_VIEWS'
596           },
597           :authenticated => false
598         )
599       end).should_not raise_error
600     end
601
602     it 'should fail when validating parameters with invalid values' do
603       (lambda do
604         CLIENT.generate_request(
605           :api_method => @adsense.reports.generate,
606           :parameters => {
607             'startDate' => '2000-01-01',
608             'endDate' => '2010-01-01',
609             'dimension' => 'BAD_CHARACTERS=-&*(£&',
610             'metric' => 'PAGE_VIEWS'
611           },
612           :authenticated => false
613         )
614       end).should raise_error(ArgumentError)
615     end
616
617     it 'should succeed when validating repeated parameters in a correct call' do
618       (lambda do
619         CLIENT.generate_request(
620           :api_method => @adsense.reports.generate,
621           :parameters => {
622             'startDate' => '2000-01-01',
623             'endDate' => '2010-01-01',
624             'dimension' => ['DATE', 'PRODUCT_CODE'],
625             'metric' => ['PAGE_VIEWS', 'CLICKS']
626           },
627           :authenticated => false
628         )
629       end).should_not raise_error
630     end
631
632     it 'should fail when validating incorrect repeated parameters' do
633       (lambda do
634         CLIENT.generate_request(
635           :api_method => @adsense.reports.generate,
636           :parameters => {
637             'startDate' => '2000-01-01',
638             'endDate' => '2010-01-01',
639             'dimension' => ['DATE', 'BAD_CHARACTERS=-&*(£&'],
640             'metric' => ['PAGE_VIEWS', 'CLICKS']
641           },
642           :authenticated => false
643         )
644       end).should raise_error(ArgumentError)
645     end
646   end
647
648   describe 'with the Drive API' do
649     before do
650       CLIENT.authorization = nil
651       @drive = CLIENT.discovered_api('drive', 'v1')
652     end
653
654     it 'should include media upload info methods' do
655       @drive.files.insert.media_upload.should_not == nil
656     end
657
658     it 'should include accepted media types' do
659       @drive.files.insert.media_upload.accepted_types.should_not be_empty
660     end
661
662     it 'should have an upload path' do
663       @drive.files.insert.media_upload.uri_template.should_not == nil
664     end
665
666     it 'should have a max file size' do
667       @drive.files.insert.media_upload.max_size.should_not == nil
668     end
669   end
670 end