Temp disable latitude tests due to discovery bug
[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 =begin
394   describe 'with the latitude API' do
395     before do
396       CLIENT.authorization = nil
397       @latitude = CLIENT.discovered_api('latitude')
398     end
399
400     it 'should correctly determine the discovery URI' do
401       CLIENT.discovery_uri('latitude').should ===
402         'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
403     end
404
405     it 'should find APIs that are in the discovery document' do
406       CLIENT.discovered_api('latitude').name.should == 'latitude'
407       CLIENT.discovered_api('latitude').version.should == 'v1'
408     end
409
410     it 'should return a batch path' do
411       CLIENT.discovered_api('latitude').batch_path.should_not be_nil
412     end
413
414     it 'should find methods that are in the discovery document' do
415       CLIENT.discovered_method(
416         'latitude.currentLocation.get', 'latitude'
417       ).name.should == 'get'
418     end
419
420     it 'should define the origin API in discovered methods' do
421       CLIENT.discovered_method(
422         'latitude.currentLocation.get', 'latitude'
423       ).api.name.should == 'latitude'
424     end
425
426     it 'should not find methods that are not in the discovery document' do
427       CLIENT.discovered_method('latitude.bogus', 'latitude').should == nil
428     end
429
430     it 'should generate requests against the correct URIs' do
431       request = CLIENT.generate_request(
432         :api_method => 'latitude.currentLocation.get',
433         :authenticated => false
434       )
435       request.to_env(Faraday.default_connection)[:url].to_s.should ===
436         'https://www.googleapis.com/latitude/v1/currentLocation'
437     end
438
439     it 'should generate requests against the correct URIs' do
440       request = CLIENT.generate_request(
441         :api_method => @latitude.current_location.get,
442         :authenticated => false
443       )
444       request.to_env(Faraday.default_connection)[:url].to_s.should ===
445         'https://www.googleapis.com/latitude/v1/currentLocation'
446     end
447
448     it 'should not be able to execute requests without authorization' do
449       result = CLIENT.execute(
450         :api_method => 'latitude.currentLocation.get',
451         :authenticated => false
452       )
453       result.response.status.should == 401
454     end
455   end
456 =end
457
458   describe 'with the moderator API' do
459     before do
460       CLIENT.authorization = nil
461       @moderator = CLIENT.discovered_api('moderator')
462     end
463
464     it 'should correctly determine the discovery URI' do
465       CLIENT.discovery_uri('moderator').should ===
466         'https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest'
467     end
468
469     it 'should find APIs that are in the discovery document' do
470       CLIENT.discovered_api('moderator').name.should == 'moderator'
471       CLIENT.discovered_api('moderator').version.should == 'v1'
472     end
473
474     it 'should find methods that are in the discovery document' do
475       CLIENT.discovered_method(
476         'moderator.profiles.get', 'moderator'
477       ).name.should == 'get'
478     end
479
480     it 'should define the origin API in discovered methods' do
481       CLIENT.discovered_method(
482         'moderator.profiles.get', 'moderator'
483       ).api.name.should == 'moderator'
484     end
485
486     it 'should not find methods that are not in the discovery document' do
487       CLIENT.discovered_method('moderator.bogus', 'moderator').should == nil
488     end
489
490     it 'should return a batch path' do
491       CLIENT.discovered_api('moderator').batch_path.should_not be_nil
492     end
493
494     it 'should generate requests against the correct URIs' do
495       request = CLIENT.generate_request(
496         :api_method => 'moderator.profiles.get',
497         :authenticated => false
498       )
499       request.to_env(Faraday.default_connection)[:url].to_s.should ===
500         'https://www.googleapis.com/moderator/v1/profiles/@me'
501     end
502
503     it 'should generate requests against the correct URIs' do
504       request = CLIENT.generate_request(
505         :api_method => @moderator.profiles.get,
506         :authenticated => false
507       )
508       request.to_env(Faraday.default_connection)[:url].to_s.should ===
509         'https://www.googleapis.com/moderator/v1/profiles/@me'
510     end
511
512     it 'should not be able to execute requests without authorization' do
513       result = CLIENT.execute(
514         'moderator.profiles.get',
515         {},
516         '',
517         [],
518         {:authenticated => false}
519       )
520       result.response.status.should == 401
521     end
522   end
523
524   describe 'with the adsense API' do
525     before do
526       CLIENT.authorization = nil
527       @adsense = CLIENT.discovered_api('adsense', 'v1')
528     end
529
530     it 'should correctly determine the discovery URI' do
531       CLIENT.discovery_uri('adsense').should ===
532         'https://www.googleapis.com/discovery/v1/apis/adsense/v1/rest'
533     end
534
535     it 'should find APIs that are in the discovery document' do
536       CLIENT.discovered_api('adsense').name.should == 'adsense'
537       CLIENT.discovered_api('adsense').version.should == 'v1'
538     end
539
540     it 'should return a batch path' do
541       CLIENT.discovered_api('adsense').batch_path.should_not be_nil
542     end
543
544     it 'should find methods that are in the discovery document' do
545       CLIENT.discovered_method(
546         'adsense.reports.generate', 'adsense'
547       ).name.should == 'generate'
548     end
549
550     it 'should not find methods that are not in the discovery document' do
551       CLIENT.discovered_method('adsense.bogus', 'adsense').should == nil
552     end
553
554     it 'should generate requests against the correct URIs' do
555       request = CLIENT.generate_request(
556         :api_method => 'adsense.adclients.list',
557         :authenticated => false
558       )
559       request.to_env(Faraday.default_connection)[:url].to_s.should ===
560         'https://www.googleapis.com/adsense/v1/adclients'
561     end
562
563     it 'should generate requests against the correct URIs' do
564       request = CLIENT.generate_request(
565         :api_method => @adsense.adclients.list,
566         :authenticated => false
567       )
568       request.to_env(Faraday.default_connection)[:url].to_s.should ===
569         'https://www.googleapis.com/adsense/v1/adclients'
570     end
571
572     it 'should not be able to execute requests without authorization' do
573       result = CLIENT.execute(
574         :api_method => 'adsense.adclients.list',
575         :authenticated => false
576       )
577       result.response.status.should == 401
578     end
579
580     it 'should fail when validating missing required parameters' do
581       (lambda do
582         CLIENT.generate_request(
583           :api_method => @adsense.reports.generate,
584           :authenticated => false
585         )
586       end).should raise_error(ArgumentError)
587     end
588
589     it 'should succeed when validating parameters in a correct call' do
590       (lambda do
591         CLIENT.generate_request(
592           :api_method => @adsense.reports.generate,
593           :parameters => {
594             'startDate' => '2000-01-01',
595             'endDate' => '2010-01-01',
596             'dimension' => 'DATE',
597             'metric' => 'PAGE_VIEWS'
598           },
599           :authenticated => false
600         )
601       end).should_not raise_error
602     end
603
604     it 'should fail when validating parameters with invalid values' do
605       (lambda do
606         CLIENT.generate_request(
607           :api_method => @adsense.reports.generate,
608           :parameters => {
609             'startDate' => '2000-01-01',
610             'endDate' => '2010-01-01',
611             'dimension' => 'BAD_CHARACTERS=-&*(£&',
612             'metric' => 'PAGE_VIEWS'
613           },
614           :authenticated => false
615         )
616       end).should raise_error(ArgumentError)
617     end
618
619     it 'should succeed when validating repeated parameters in a correct call' do
620       (lambda do
621         CLIENT.generate_request(
622           :api_method => @adsense.reports.generate,
623           :parameters => {
624             'startDate' => '2000-01-01',
625             'endDate' => '2010-01-01',
626             'dimension' => ['DATE', 'PRODUCT_CODE'],
627             'metric' => ['PAGE_VIEWS', 'CLICKS']
628           },
629           :authenticated => false
630         )
631       end).should_not raise_error
632     end
633
634     it 'should fail when validating incorrect repeated parameters' do
635       (lambda do
636         CLIENT.generate_request(
637           :api_method => @adsense.reports.generate,
638           :parameters => {
639             'startDate' => '2000-01-01',
640             'endDate' => '2010-01-01',
641             'dimension' => ['DATE', 'BAD_CHARACTERS=-&*(£&'],
642             'metric' => ['PAGE_VIEWS', 'CLICKS']
643           },
644           :authenticated => false
645         )
646       end).should raise_error(ArgumentError)
647     end
648   end
649
650   describe 'with the Drive API' do
651     before do
652       CLIENT.authorization = nil
653       @drive = CLIENT.discovered_api('drive', 'v1')
654     end
655
656     it 'should include media upload info methods' do
657       @drive.files.insert.media_upload.should_not == nil
658     end
659
660     it 'should include accepted media types' do
661       @drive.files.insert.media_upload.accepted_types.should_not be_empty
662     end
663
664     it 'should have an upload path' do
665       @drive.files.insert.media_upload.uri_template.should_not == nil
666     end
667
668     it 'should have a max file size' do
669       @drive.files.insert.media_upload.max_size.should_not == nil
670     end
671   end
672 end