cure-google-api-client 0.8.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +178 -0
  3. data/Gemfile +9 -0
  4. data/LICENSE +202 -0
  5. data/README.md +218 -0
  6. data/Rakefile +41 -0
  7. data/google-api-client.gemspec +43 -0
  8. data/lib/cacerts.pem +2183 -0
  9. data/lib/compat/multi_json.rb +19 -0
  10. data/lib/google/api_client.rb +750 -0
  11. data/lib/google/api_client/auth/compute_service_account.rb +28 -0
  12. data/lib/google/api_client/auth/file_storage.rb +59 -0
  13. data/lib/google/api_client/auth/installed_app.rb +126 -0
  14. data/lib/google/api_client/auth/jwt_asserter.rb +126 -0
  15. data/lib/google/api_client/auth/key_utils.rb +93 -0
  16. data/lib/google/api_client/auth/pkcs12.rb +41 -0
  17. data/lib/google/api_client/auth/storage.rb +102 -0
  18. data/lib/google/api_client/auth/storages/file_store.rb +58 -0
  19. data/lib/google/api_client/auth/storages/redis_store.rb +54 -0
  20. data/lib/google/api_client/batch.rb +326 -0
  21. data/lib/google/api_client/charset.rb +33 -0
  22. data/lib/google/api_client/client_secrets.rb +179 -0
  23. data/lib/google/api_client/discovery.rb +19 -0
  24. data/lib/google/api_client/discovery/api.rb +310 -0
  25. data/lib/google/api_client/discovery/media.rb +77 -0
  26. data/lib/google/api_client/discovery/method.rb +363 -0
  27. data/lib/google/api_client/discovery/resource.rb +156 -0
  28. data/lib/google/api_client/discovery/schema.rb +117 -0
  29. data/lib/google/api_client/environment.rb +42 -0
  30. data/lib/google/api_client/errors.rb +65 -0
  31. data/lib/google/api_client/gzip.rb +28 -0
  32. data/lib/google/api_client/logging.rb +32 -0
  33. data/lib/google/api_client/media.rb +259 -0
  34. data/lib/google/api_client/railtie.rb +18 -0
  35. data/lib/google/api_client/reference.rb +27 -0
  36. data/lib/google/api_client/request.rb +350 -0
  37. data/lib/google/api_client/result.rb +255 -0
  38. data/lib/google/api_client/service.rb +233 -0
  39. data/lib/google/api_client/service/batch.rb +110 -0
  40. data/lib/google/api_client/service/request.rb +144 -0
  41. data/lib/google/api_client/service/resource.rb +40 -0
  42. data/lib/google/api_client/service/result.rb +162 -0
  43. data/lib/google/api_client/service/simple_file_store.rb +151 -0
  44. data/lib/google/api_client/service/stub_generator.rb +61 -0
  45. data/lib/google/api_client/service_account.rb +21 -0
  46. data/lib/google/api_client/version.rb +26 -0
  47. data/spec/google/api_client/auth/storage_spec.rb +122 -0
  48. data/spec/google/api_client/auth/storages/file_store_spec.rb +40 -0
  49. data/spec/google/api_client/auth/storages/redis_store_spec.rb +70 -0
  50. data/spec/google/api_client/batch_spec.rb +248 -0
  51. data/spec/google/api_client/client_secrets_spec.rb +53 -0
  52. data/spec/google/api_client/discovery_spec.rb +708 -0
  53. data/spec/google/api_client/gzip_spec.rb +98 -0
  54. data/spec/google/api_client/media_spec.rb +178 -0
  55. data/spec/google/api_client/request_spec.rb +29 -0
  56. data/spec/google/api_client/result_spec.rb +207 -0
  57. data/spec/google/api_client/service_account_spec.rb +169 -0
  58. data/spec/google/api_client/service_spec.rb +618 -0
  59. data/spec/google/api_client/simple_file_store_spec.rb +133 -0
  60. data/spec/google/api_client_spec.rb +352 -0
  61. data/spec/spec_helper.rb +66 -0
  62. metadata +339 -0
@@ -0,0 +1,133 @@
1
+ # encoding:utf-8
2
+
3
+ # Copyright 2013 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
+ require 'spec_helper'
18
+
19
+ require 'google/api_client/service/simple_file_store'
20
+
21
+ RSpec.describe Google::APIClient::Service::SimpleFileStore do
22
+
23
+ FILE_NAME = 'test.cache'
24
+
25
+ describe 'with no cache file' do
26
+ before(:each) do
27
+ File.delete(FILE_NAME) if File.exists?(FILE_NAME)
28
+ @cache = Google::APIClient::Service::SimpleFileStore.new(FILE_NAME)
29
+ end
30
+
31
+ it 'should return nil when asked if a key exists' do
32
+ expect(@cache.exist?('invalid')).to be_nil
33
+ expect(File.exists?(FILE_NAME)).to be_falsey
34
+ end
35
+
36
+ it 'should return nil when asked to read a key' do
37
+ expect(@cache.read('invalid')).to be_nil
38
+ expect(File.exists?(FILE_NAME)).to be_falsey
39
+ end
40
+
41
+ it 'should return nil when asked to fetch a key' do
42
+ expect(@cache.fetch('invalid')).to be_nil
43
+ expect(File.exists?(FILE_NAME)).to be_falsey
44
+ end
45
+
46
+ it 'should create a cache file when asked to fetch a key with a default' do
47
+ expect(@cache.fetch('new_key') do
48
+ 'value'
49
+ end).to eq('value')
50
+ expect(File.exists?(FILE_NAME)).to be_truthy
51
+ end
52
+
53
+ it 'should create a cache file when asked to write a key' do
54
+ @cache.write('new_key', 'value')
55
+ expect(File.exists?(FILE_NAME)).to be_truthy
56
+ end
57
+
58
+ it 'should return nil when asked to delete a key' do
59
+ expect(@cache.delete('invalid')).to be_nil
60
+ expect(File.exists?(FILE_NAME)).to be_falsey
61
+ end
62
+ end
63
+
64
+ describe 'with an existing cache' do
65
+ before(:each) do
66
+ File.delete(FILE_NAME) if File.exists?(FILE_NAME)
67
+ @cache = Google::APIClient::Service::SimpleFileStore.new(FILE_NAME)
68
+ @cache.write('existing_key', 'existing_value')
69
+ end
70
+
71
+ it 'should return true when asked if an existing key exists' do
72
+ expect(@cache.exist?('existing_key')).to be_truthy
73
+ end
74
+
75
+ it 'should return false when asked if a nonexistent key exists' do
76
+ expect(@cache.exist?('invalid')).to be_falsey
77
+ end
78
+
79
+ it 'should return the value for an existing key when asked to read it' do
80
+ expect(@cache.read('existing_key')).to eq('existing_value')
81
+ end
82
+
83
+ it 'should return nil for a nonexistent key when asked to read it' do
84
+ expect(@cache.read('invalid')).to be_nil
85
+ end
86
+
87
+ it 'should return the value for an existing key when asked to read it' do
88
+ expect(@cache.read('existing_key')).to eq('existing_value')
89
+ end
90
+
91
+ it 'should return nil for a nonexistent key when asked to fetch it' do
92
+ expect(@cache.fetch('invalid')).to be_nil
93
+ end
94
+
95
+ it 'should return and save the default value for a nonexistent key when asked to fetch it with a default' do
96
+ expect(@cache.fetch('new_key') do
97
+ 'value'
98
+ end).to eq('value')
99
+ expect(@cache.read('new_key')).to eq('value')
100
+ end
101
+
102
+ it 'should remove an existing value and return true when asked to delete it' do
103
+ expect(@cache.delete('existing_key')).to be_truthy
104
+ expect(@cache.read('existing_key')).to be_nil
105
+ end
106
+
107
+ it 'should return false when asked to delete a nonexistent key' do
108
+ expect(@cache.delete('invalid')).to be_falsey
109
+ end
110
+
111
+ it 'should convert keys to strings when storing them' do
112
+ @cache.write(:symbol_key, 'value')
113
+ expect(@cache.read('symbol_key')).to eq('value')
114
+ end
115
+
116
+ it 'should convert keys to strings when reading them' do
117
+ expect(@cache.read(:existing_key)).to eq('existing_value')
118
+ end
119
+
120
+ it 'should convert keys to strings when fetching them' do
121
+ expect(@cache.fetch(:existing_key)).to eq('existing_value')
122
+ end
123
+
124
+ it 'should convert keys to strings when deleting them' do
125
+ expect(@cache.delete(:existing_key)).to be_truthy
126
+ expect(@cache.read('existing_key')).to be_nil
127
+ end
128
+ end
129
+
130
+ after(:all) do
131
+ File.delete(FILE_NAME) if File.exists?(FILE_NAME)
132
+ end
133
+ end
@@ -0,0 +1,352 @@
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