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