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,122 @@
1
+ require 'spec_helper'
2
+
3
+ require 'google/api_client'
4
+ require 'google/api_client/version'
5
+
6
+ describe Google::APIClient::Storage do
7
+ let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') }
8
+ let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..')) }
9
+ let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) }
10
+
11
+ let(:store) { double }
12
+ let(:client_stub) { double }
13
+ subject { Google::APIClient::Storage.new(store) }
14
+
15
+ describe 'authorize' do
16
+ it 'should authorize' do
17
+ expect(subject).to respond_to(:authorization)
18
+ expect(subject.store).to be == store
19
+ end
20
+ end
21
+
22
+ describe 'authorize' do
23
+ describe 'with credentials' do
24
+
25
+ it 'should initialize a new OAuth Client' do
26
+ expect(subject).to receive(:load_credentials).and_return({:first => 'a dummy'})
27
+ expect(client_stub).to receive(:issued_at=)
28
+ expect(client_stub).to receive(:expired?).and_return(false)
29
+ expect(Signet::OAuth2::Client).to receive(:new).and_return(client_stub)
30
+ expect(subject).not_to receive(:refresh_authorization)
31
+ subject.authorize
32
+ end
33
+
34
+ it 'should refresh authorization' do
35
+ expect(subject).to receive(:load_credentials).and_return({:first => 'a dummy'})
36
+ expect(client_stub).to receive(:issued_at=)
37
+ expect(client_stub).to receive(:expired?).and_return(true)
38
+ expect(Signet::OAuth2::Client).to receive(:new).and_return(client_stub)
39
+ expect(subject).to receive(:refresh_authorization)
40
+ auth = subject.authorize
41
+ expect(auth).to be == subject.authorization
42
+ expect(auth).not_to be_nil
43
+ end
44
+ end
45
+
46
+ describe 'without credentials' do
47
+
48
+ it 'should return nil' do
49
+ expect(subject.authorization).to be_nil
50
+ expect(subject).to receive(:load_credentials).and_return({})
51
+ expect(subject.authorize).to be_nil
52
+ expect(subject.authorization).to be_nil
53
+ end
54
+ end
55
+ end
56
+
57
+ describe 'write_credentials' do
58
+ it 'should call store to write credentials' do
59
+ authorization_stub = double
60
+ expect(authorization_stub).to receive(:refresh_token).and_return(true)
61
+ expect(subject).to receive(:credentials_hash)
62
+ expect(subject.store).to receive(:write_credentials)
63
+ subject.write_credentials(authorization_stub)
64
+ expect(subject.authorization).to be == authorization_stub
65
+ end
66
+
67
+ it 'should not call store to write credentials' do
68
+ expect(subject).not_to receive(:credentials_hash)
69
+ expect(subject.store).not_to receive(:write_credentials)
70
+ expect {
71
+ subject.write_credentials()
72
+ }.not_to raise_error
73
+ end
74
+ it 'should not call store to write credentials' do
75
+ expect(subject).not_to receive(:credentials_hash)
76
+ expect(subject.store).not_to receive(:write_credentials)
77
+ expect {
78
+ subject.write_credentials('something')
79
+ }.not_to raise_error
80
+ end
81
+
82
+ end
83
+
84
+ describe 'refresh_authorization' do
85
+ it 'should call refresh and write credentials' do
86
+ expect(subject).to receive(:write_credentials)
87
+ authorization_stub = double
88
+ expect(subject).to receive(:authorization).and_return(authorization_stub)
89
+ expect(authorization_stub).to receive(:refresh!).and_return(true)
90
+ subject.refresh_authorization
91
+ end
92
+ end
93
+
94
+ describe 'load_credentials' do
95
+ it 'should call store to load credentials' do
96
+ expect(subject.store).to receive(:load_credentials)
97
+ subject.send(:load_credentials)
98
+ end
99
+ end
100
+
101
+ describe 'credentials_hash' do
102
+ it 'should return an hash' do
103
+ authorization_stub = double
104
+ expect(authorization_stub).to receive(:access_token)
105
+ expect(authorization_stub).to receive(:client_id)
106
+ expect(authorization_stub).to receive(:client_secret)
107
+ expect(authorization_stub).to receive(:expires_in)
108
+ expect(authorization_stub).to receive(:refresh_token)
109
+ expect(authorization_stub).to receive(:issued_at).and_return('100')
110
+ allow(subject).to receive(:authorization).and_return(authorization_stub)
111
+ credentials = subject.send(:credentials_hash)
112
+ expect(credentials).to include(:access_token)
113
+ expect(credentials).to include(:authorization_uri)
114
+ expect(credentials).to include(:client_id)
115
+ expect(credentials).to include(:client_secret)
116
+ expect(credentials).to include(:expires_in)
117
+ expect(credentials).to include(:refresh_token)
118
+ expect(credentials).to include(:token_credential_uri)
119
+ expect(credentials).to include(:issued_at)
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ require 'google/api_client'
4
+ require 'google/api_client/version'
5
+
6
+ describe Google::APIClient::FileStore do
7
+ let(:root_path) { File.expand_path(File.join(__FILE__, '..','..','..', '..','..')) }
8
+ let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) }
9
+
10
+ let(:credentials_hash) {{
11
+ "access_token"=>"my_access_token",
12
+ "authorization_uri"=>"https://accounts.google.com/o/oauth2/auth",
13
+ "client_id"=>"123456_test_client_id@.apps.googleusercontent.com",
14
+ "client_secret"=>"123456_client_secret",
15
+ "expires_in"=>3600,
16
+ "refresh_token"=>"my_refresh_token",
17
+ "token_credential_uri"=>"https://accounts.google.com/o/oauth2/token",
18
+ "issued_at"=>1384440275
19
+ }}
20
+
21
+ subject{Google::APIClient::FileStore.new('a file path')}
22
+
23
+ it 'should have a path' do
24
+ expect(subject.path).to be == 'a file path'
25
+ subject.path = 'an other file path'
26
+ expect(subject.path).to be == 'an other file path'
27
+ end
28
+
29
+ it 'should load credentials' do
30
+ subject.path = json_file
31
+ credentials = subject.load_credentials
32
+ expect(credentials).to include('access_token', 'authorization_uri', 'refresh_token')
33
+ end
34
+
35
+ it 'should write credentials' do
36
+ io_stub = StringIO.new
37
+ expect(subject).to receive(:open).and_return(io_stub)
38
+ subject.write_credentials(credentials_hash)
39
+ end
40
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ require 'google/api_client'
4
+ require 'google/api_client/version'
5
+
6
+
7
+ describe Google::APIClient::RedisStore do
8
+ let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..', '..', '..')) }
9
+ let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) }
10
+ let(:redis) {double}
11
+
12
+ let(:credentials_hash) { {
13
+ "access_token" => "my_access_token",
14
+ "authorization_uri" => "https://accounts.google.com/o/oauth2/auth",
15
+ "client_id" => "123456_test_client_id@.apps.googleusercontent.com",
16
+ "client_secret" => "123456_client_secret",
17
+ "expires_in" => 3600,
18
+ "refresh_token" => "my_refresh_token",
19
+ "token_credential_uri" => "https://accounts.google.com/o/oauth2/token",
20
+ "issued_at" => 1384440275
21
+ } }
22
+
23
+ subject { Google::APIClient::RedisStore.new('a redis instance') }
24
+
25
+ it 'should have a redis instance' do
26
+ expect(subject.redis).to be == 'a redis instance'
27
+ subject.redis = 'an other redis instance'
28
+ expect(subject.redis).to be == 'an other redis instance'
29
+ end
30
+
31
+ describe 'load_credentials' do
32
+
33
+ it 'should load credentials' do
34
+ subject.redis= redis
35
+ expect(redis).to receive(:get).and_return(credentials_hash.to_json)
36
+ expect(subject.load_credentials).to be == credentials_hash
37
+ end
38
+
39
+ it 'should return nil' do
40
+ subject.redis= redis
41
+ expect(redis).to receive(:get).and_return(nil)
42
+ expect(subject.load_credentials).to be_nil
43
+ end
44
+ end
45
+
46
+ describe 'redis_credentials_key' do
47
+ context 'without given key' do
48
+ it 'should return default key' do
49
+ expect(subject.redis_credentials_key).to be == "google_api_credentials"
50
+ end
51
+ end
52
+ context 'with given key' do
53
+ let(:redis_store) { Google::APIClient::RedisStore.new('a redis instance', 'another_google_api_credentials') }
54
+ it 'should use given key' do
55
+ expect(redis_store.redis_credentials_key).to be == "another_google_api_credentials"
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ describe 'write credentials' do
62
+
63
+ it 'should write credentials' do
64
+ subject.redis= redis
65
+ expect(redis).to receive(:set).and_return('ok')
66
+ expect(subject.write_credentials(credentials_hash)).to be_truthy
67
+ end
68
+ end
69
+
70
+ end
@@ -0,0 +1,248 @@
1
+ # Copyright 2012 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
+ require 'google/api_client'
17
+
18
+ RSpec.describe Google::APIClient::BatchRequest do
19
+ CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
20
+
21
+ after do
22
+ # Reset client to not-quite-pristine state
23
+ CLIENT.key = nil
24
+ CLIENT.user_ip = nil
25
+ end
26
+
27
+ it 'should raise an error if making an empty batch request' do
28
+ batch = Google::APIClient::BatchRequest.new
29
+
30
+ expect(lambda do
31
+ CLIENT.execute(batch)
32
+ end).to raise_error(Google::APIClient::BatchError)
33
+ end
34
+
35
+ it 'should allow query parameters in batch requests' do
36
+ batch = Google::APIClient::BatchRequest.new
37
+ batch.add(:uri => 'https://example.com', :parameters => {
38
+ 'a' => '12345'
39
+ })
40
+ method, uri, headers, body = batch.to_http_request
41
+ expect(body.read).to include("/?a=12345")
42
+ end
43
+
44
+ describe 'with the discovery API' do
45
+ before do
46
+ CLIENT.authorization = nil
47
+ @discovery = CLIENT.discovered_api('discovery', 'v1')
48
+ end
49
+
50
+ describe 'with two valid requests' do
51
+ before do
52
+ @call1 = {
53
+ :api_method => @discovery.apis.get_rest,
54
+ :parameters => {
55
+ 'api' => 'plus',
56
+ 'version' => 'v1'
57
+ }
58
+ }
59
+
60
+ @call2 = {
61
+ :api_method => @discovery.apis.get_rest,
62
+ :parameters => {
63
+ 'api' => 'discovery',
64
+ 'version' => 'v1'
65
+ }
66
+ }
67
+ end
68
+
69
+ it 'should execute both when using a global callback' do
70
+ block_called = 0
71
+ ids = ['first_call', 'second_call']
72
+ expected_ids = ids.clone
73
+ batch = Google::APIClient::BatchRequest.new do |result|
74
+ block_called += 1
75
+ expect(result.status).to eq(200)
76
+ expect(expected_ids).to include(result.response.call_id)
77
+ expected_ids.delete(result.response.call_id)
78
+ end
79
+
80
+ batch.add(@call1, ids[0])
81
+ batch.add(@call2, ids[1])
82
+
83
+ CLIENT.execute(batch)
84
+ expect(block_called).to eq(2)
85
+ end
86
+
87
+ it 'should execute both when using individual callbacks' do
88
+ batch = Google::APIClient::BatchRequest.new
89
+
90
+ call1_returned, call2_returned = false, false
91
+ batch.add(@call1) do |result|
92
+ call1_returned = true
93
+ expect(result.status).to eq(200)
94
+ end
95
+ batch.add(@call2) do |result|
96
+ call2_returned = true
97
+ expect(result.status).to eq(200)
98
+ end
99
+
100
+ CLIENT.execute(batch)
101
+ expect(call1_returned).to be_truthy
102
+ expect(call2_returned).to be_truthy
103
+ end
104
+
105
+ it 'should raise an error if using the same call ID more than once' do
106
+ batch = Google::APIClient::BatchRequest.new
107
+
108
+ expect(lambda do
109
+ batch.add(@call1, 'my_id')
110
+ batch.add(@call2, 'my_id')
111
+ end).to raise_error(Google::APIClient::BatchError)
112
+ end
113
+ end
114
+
115
+ describe 'with a valid request and an invalid one' do
116
+ before do
117
+ @call1 = {
118
+ :api_method => @discovery.apis.get_rest,
119
+ :parameters => {
120
+ 'api' => 'plus',
121
+ 'version' => 'v1'
122
+ }
123
+ }
124
+
125
+ @call2 = {
126
+ :api_method => @discovery.apis.get_rest,
127
+ :parameters => {
128
+ 'api' => 0,
129
+ 'version' => 1
130
+ }
131
+ }
132
+ end
133
+
134
+ it 'should execute both when using a global callback' do
135
+ block_called = 0
136
+ ids = ['first_call', 'second_call']
137
+ expected_ids = ids.clone
138
+ batch = Google::APIClient::BatchRequest.new do |result|
139
+ block_called += 1
140
+ expect(expected_ids).to include(result.response.call_id)
141
+ expected_ids.delete(result.response.call_id)
142
+ if result.response.call_id == ids[0]
143
+ expect(result.status).to eq(200)
144
+ else
145
+ expect(result.status).to be >= 400
146
+ expect(result.status).to be < 500
147
+ end
148
+ end
149
+
150
+ batch.add(@call1, ids[0])
151
+ batch.add(@call2, ids[1])
152
+
153
+ CLIENT.execute(batch)
154
+ expect(block_called).to eq(2)
155
+ end
156
+
157
+ it 'should execute both when using individual callbacks' do
158
+ batch = Google::APIClient::BatchRequest.new
159
+
160
+ call1_returned, call2_returned = false, false
161
+ batch.add(@call1) do |result|
162
+ call1_returned = true
163
+ expect(result.status).to eq(200)
164
+ end
165
+ batch.add(@call2) do |result|
166
+ call2_returned = true
167
+ expect(result.status).to be >= 400
168
+ expect(result.status).to be < 500
169
+ end
170
+
171
+ CLIENT.execute(batch)
172
+ expect(call1_returned).to be_truthy
173
+ expect(call2_returned).to be_truthy
174
+ end
175
+ end
176
+ end
177
+
178
+ describe 'with the calendar API' do
179
+ before do
180
+ CLIENT.authorization = nil
181
+ @calendar = CLIENT.discovered_api('calendar', 'v3')
182
+ end
183
+
184
+ describe 'with two valid requests' do
185
+ before do
186
+ event1 = {
187
+ 'summary' => 'Appointment 1',
188
+ 'location' => 'Somewhere',
189
+ 'start' => {
190
+ 'dateTime' => '2011-01-01T10:00:00.000-07:00'
191
+ },
192
+ 'end' => {
193
+ 'dateTime' => '2011-01-01T10:25:00.000-07:00'
194
+ },
195
+ 'attendees' => [
196
+ {
197
+ 'email' => 'myemail@mydomain.tld'
198
+ }
199
+ ]
200
+ }
201
+
202
+ event2 = {
203
+ 'summary' => 'Appointment 2',
204
+ 'location' => 'Somewhere as well',
205
+ 'start' => {
206
+ 'dateTime' => '2011-01-02T10:00:00.000-07:00'
207
+ },
208
+ 'end' => {
209
+ 'dateTime' => '2011-01-02T10:25:00.000-07:00'
210
+ },
211
+ 'attendees' => [
212
+ {
213
+ 'email' => 'myemail@mydomain.tld'
214
+ }
215
+ ]
216
+ }
217
+
218
+ @call1 = {
219
+ :api_method => @calendar.events.insert,
220
+ :parameters => {'calendarId' => 'myemail@mydomain.tld'},
221
+ :body => MultiJson.dump(event1),
222
+ :headers => {'Content-Type' => 'application/json'}
223
+ }
224
+
225
+ @call2 = {
226
+ :api_method => @calendar.events.insert,
227
+ :parameters => {'calendarId' => 'myemail@mydomain.tld'},
228
+ :body => MultiJson.dump(event2),
229
+ :headers => {'Content-Type' => 'application/json'}
230
+ }
231
+ end
232
+
233
+ it 'should convert to a correct HTTP request' do
234
+ batch = Google::APIClient::BatchRequest.new { |result| }
235
+ batch.add(@call1, '1').add(@call2, '2')
236
+ request = batch.to_env(CLIENT.connection)
237
+ boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
238
+ expect(request[:method].to_s.downcase).to eq('post')
239
+ expect(request[:url].to_s).to eq('https://www.googleapis.com/batch')
240
+ expect(request[:request_headers]['Content-Type']).to eq("multipart/mixed;boundary=#{boundary}")
241
+ body = request[:body].read
242
+ expect(body).to include(@call1[:body])
243
+ expect(body).to include(@call2[:body])
244
+ end
245
+ end
246
+
247
+ end
248
+ end