21700: Install Bundler system-wide in Rails postinst
[arvados.git] / sdk / ruby-google-api-client / spec / google / api_client_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 'faraday'
18 require 'signet/oauth_1/client'
19 require 'google/api_client'
20
21 shared_examples_for 'configurable user agent' do
22   include ConnectionHelpers
23
24   it 'should allow the user agent to be modified' do
25     client.user_agent = 'Custom User Agent/1.2.3'
26     expect(client.user_agent).to eq('Custom User Agent/1.2.3')
27   end
28
29   it 'should allow the user agent to be set to nil' do
30     client.user_agent = nil
31     expect(client.user_agent).to eq(nil)
32   end
33
34   it 'should not allow the user agent to be used with bogus values' do
35     expect(lambda do
36       client.user_agent = 42
37       client.execute(:uri=>'https://www.google.com/')
38     end).to raise_error(TypeError)
39   end
40
41   it 'should transmit a User-Agent header when sending requests' do
42     client.user_agent = 'Custom User Agent/1.2.3'
43
44     conn = stub_connection do |stub|
45       stub.get('/') do |env|
46         headers = env[:request_headers]
47         expect(headers).to have_key('User-Agent')
48         expect(headers['User-Agent']).to eq(client.user_agent)
49         [200, {}, ['']]
50       end
51     end
52     client.execute(:uri=>'https://www.google.com/', :connection => conn)
53     conn.verify
54   end
55 end
56
57 RSpec.describe Google::APIClient do
58   include ConnectionHelpers
59
60   let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') }
61
62   it "should pass the faraday options provided on initialization to FaraDay configuration block" do
63     client = Google::APIClient.new(faraday_option: {timeout: 999})
64     expect(client.connection.options.timeout).to be == 999
65   end
66
67   it 'should make its version number available' do
68     expect(Google::APIClient::VERSION::STRING).to be_instance_of(String)
69   end
70
71   it 'should default to OAuth 2' do
72     expect(Signet::OAuth2::Client).to be === client.authorization
73   end
74
75   describe 'configure for no authentication' do
76     before do
77       client.authorization = nil
78     end
79     it_should_behave_like 'configurable user agent'
80   end
81
82   describe 'configured for OAuth 1' do
83     before do
84       client.authorization = :oauth_1
85       client.authorization.token_credential_key = 'abc'
86       client.authorization.token_credential_secret = '123'
87     end
88
89     it 'should use the default OAuth1 client configuration' do
90       expect(client.authorization.temporary_credential_uri.to_s).to eq(
91         'https://www.google.com/accounts/OAuthGetRequestToken'
92       )
93       expect(client.authorization.authorization_uri.to_s).to include(
94         'https://www.google.com/accounts/OAuthAuthorizeToken'
95       )
96       expect(client.authorization.token_credential_uri.to_s).to eq(
97         'https://www.google.com/accounts/OAuthGetAccessToken'
98       )
99       expect(client.authorization.client_credential_key).to eq('anonymous')
100       expect(client.authorization.client_credential_secret).to eq('anonymous')
101     end
102
103     it_should_behave_like 'configurable user agent'
104   end
105
106   describe 'configured for OAuth 2' do
107     before do
108       client.authorization = :oauth_2
109       client.authorization.access_token = '12345'
110     end
111
112     # TODO
113     it_should_behave_like 'configurable user agent'
114   end
115
116   describe 'when executing requests' do
117     before do
118       @prediction = client.discovered_api('prediction', 'v1.2')
119       client.authorization = :oauth_2
120       @connection = stub_connection do |stub|
121         stub.post('/prediction/v1.2/training?data=12345') do |env|
122           expect(env[:request_headers]['Authorization']).to eq('Bearer 12345')
123           [200, {}, '{}']
124         end
125       end
126     end
127
128     after do
129       @connection.verify
130     end
131
132     it 'should use default authorization' do
133       client.authorization.access_token = "12345"
134       client.execute(
135         :api_method => @prediction.training.insert,
136         :parameters => {'data' => '12345'},
137         :connection => @connection
138       )
139     end
140
141     it 'should use request scoped authorization when provided' do
142       client.authorization.access_token = "abcdef"
143       new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
144       client.execute(
145         :api_method => @prediction.training.insert,
146         :parameters => {'data' => '12345'},
147         :authorization => new_auth,
148         :connection => @connection
149       )
150     end
151
152     it 'should accept options with batch/request style execute' do
153       client.authorization.access_token = "abcdef"
154       new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
155       request = client.generate_request(
156         :api_method => @prediction.training.insert,
157         :parameters => {'data' => '12345'}
158       )
159       client.execute(
160         request,
161         :authorization => new_auth,
162         :connection => @connection
163       )
164     end
165
166
167     it 'should accept options in array style execute' do
168        client.authorization.access_token = "abcdef"
169        new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
170        client.execute(
171          @prediction.training.insert, {'data' => '12345'}, '', {},
172          { :authorization => new_auth, :connection => @connection }
173        )
174      end
175   end
176
177   describe 'when retries enabled' do
178     before do
179       client.retries = 2
180     end
181
182     after do
183       @connection.verify
184     end
185
186     it 'should follow redirects' do
187       client.authorization = nil
188       @connection = stub_connection do |stub|
189         stub.get('/foo') do |env|
190           [302, {'location' => 'https://www.google.com/bar'}, '{}']
191         end
192         stub.get('/bar') do |env|
193           [200, {}, '{}']
194         end
195       end
196
197       client.execute(  
198         :uri => 'https://www.google.com/foo',
199         :connection => @connection
200       )
201     end
202
203     it 'should refresh tokens on 401 errors' do
204       client.authorization.access_token = '12345'
205       expect(client.authorization).to receive(:fetch_access_token!)
206
207       @connection = stub_connection do |stub|
208         stub.get('/foo') do |env|
209           [401, {}, '{}']
210         end
211         stub.get('/foo') do |env|
212           [200, {}, '{}']
213         end
214       end
215
216       client.execute(  
217         :uri => 'https://www.google.com/foo',
218         :connection => @connection
219       )
220     end
221
222
223     it 'should not attempt multiple token refreshes' do
224       client.authorization.access_token = '12345'
225       expect(client.authorization).to receive(:fetch_access_token!).once
226
227       @connection = stub_connection do |stub|
228         stub.get('/foo') do |env|
229           [401, {}, '{}']
230         end
231       end
232
233       client.execute(  
234         :uri => 'https://www.google.com/foo',
235         :connection => @connection
236       )
237     end
238
239     it 'should not retry on client errors' do
240       count = 0
241       @connection = stub_connection do |stub|
242         stub.get('/foo') do |env|
243           expect(count).to eq(0)
244           count += 1
245           [403, {}, '{}']
246         end
247       end
248
249       client.execute(  
250         :uri => 'https://www.google.com/foo',
251         :connection => @connection,
252         :authenticated => false
253       )
254     end
255
256     it 'should retry on 500 errors' do
257       client.authorization = nil
258
259       @connection = stub_connection do |stub|
260         stub.get('/foo') do |env|
261           [500, {}, '{}']
262         end
263         stub.get('/foo') do |env|
264           [200, {}, '{}']
265         end
266       end
267
268       expect(client.execute(  
269         :uri => 'https://www.google.com/foo',
270         :connection => @connection
271       ).status).to eq(200)
272
273     end
274
275     it 'should fail after max retries' do
276       client.authorization = nil
277       count = 0
278       @connection = stub_connection do |stub|
279         stub.get('/foo') do |env|
280           count += 1
281           [500, {}, '{}']
282         end
283       end
284
285       expect(client.execute(  
286         :uri => 'https://www.google.com/foo',
287         :connection => @connection
288       ).status).to eq(500)
289       expect(count).to eq(3)
290     end
291
292   end
293
294   describe 'when retries disabled and expired_auth_retry on (default)' do
295     before do
296       client.retries = 0
297     end
298
299     after do
300       @connection.verify
301     end
302
303     it 'should refresh tokens on 401 errors' do
304       client.authorization.access_token = '12345'
305       expect(client.authorization).to receive(:fetch_access_token!)
306
307       @connection = stub_connection do |stub|
308         stub.get('/foo') do |env|
309           [401, {}, '{}']
310         end
311         stub.get('/foo') do |env|
312           [200, {}, '{}']
313         end
314       end
315
316       client.execute(
317         :uri => 'https://www.gogole.com/foo',
318         :connection => @connection
319       )
320     end
321
322   end
323
324   describe 'when retries disabled and expired_auth_retry off' do
325     before do
326       client.retries = 0
327       client.expired_auth_retry = false
328     end
329
330     it 'should not refresh tokens on 401 errors' do
331       client.authorization.access_token = '12345'
332       expect(client.authorization).not_to receive(:fetch_access_token!)
333
334       @connection = stub_connection do |stub|
335         stub.get('/foo') do |env|
336           [401, {}, '{}']
337         end
338         stub.get('/foo') do |env|
339           [200, {}, '{}']
340         end
341       end
342
343       resp = client.execute(
344         :uri => 'https://www.gogole.com/foo',
345         :connection => @connection
346       )
347
348       expect(resp.response.status).to be == 401
349     end
350
351   end
352 end