koala 2.4.0 → 3.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/test.yml +32 -0
  3. data/Gemfile +5 -3
  4. data/ISSUE_TEMPLATE +25 -0
  5. data/PULL_REQUEST_TEMPLATE +11 -0
  6. data/changelog.md +161 -4
  7. data/code_of_conduct.md +64 -12
  8. data/koala.gemspec +5 -1
  9. data/lib/koala/api/batch_operation.rb +3 -6
  10. data/lib/koala/api/{graph_api.rb → graph_api_methods.rb} +29 -104
  11. data/lib/koala/api/graph_batch_api.rb +112 -65
  12. data/lib/koala/api/graph_collection.rb +19 -12
  13. data/lib/koala/api/graph_error_checker.rb +4 -3
  14. data/lib/koala/api.rb +65 -26
  15. data/lib/koala/configuration.rb +56 -0
  16. data/lib/koala/errors.rb +22 -2
  17. data/lib/koala/http_service/request.rb +133 -0
  18. data/lib/koala/http_service/response.rb +6 -4
  19. data/lib/koala/http_service/uploadable_io.rb +0 -5
  20. data/lib/koala/http_service.rb +29 -76
  21. data/lib/koala/oauth.rb +8 -8
  22. data/lib/koala/realtime_updates.rb +26 -21
  23. data/lib/koala/test_users.rb +9 -8
  24. data/lib/koala/version.rb +1 -1
  25. data/lib/koala.rb +7 -9
  26. data/readme.md +83 -109
  27. data/spec/cases/api_spec.rb +176 -69
  28. data/spec/cases/configuration_spec.rb +11 -0
  29. data/spec/cases/error_spec.rb +16 -3
  30. data/spec/cases/graph_api_batch_spec.rb +75 -44
  31. data/spec/cases/graph_api_spec.rb +15 -29
  32. data/spec/cases/graph_collection_spec.rb +47 -34
  33. data/spec/cases/graph_error_checker_spec.rb +31 -2
  34. data/spec/cases/http_service/request_spec.rb +250 -0
  35. data/spec/cases/http_service/response_spec.rb +24 -0
  36. data/spec/cases/http_service_spec.rb +126 -286
  37. data/spec/cases/koala_spec.rb +7 -5
  38. data/spec/cases/oauth_spec.rb +41 -2
  39. data/spec/cases/realtime_updates_spec.rb +51 -13
  40. data/spec/cases/test_users_spec.rb +56 -2
  41. data/spec/cases/uploadable_io_spec.rb +31 -31
  42. data/spec/fixtures/cat.m4v +0 -0
  43. data/spec/fixtures/facebook_data.yml +4 -6
  44. data/spec/fixtures/mock_facebook_responses.yml +41 -78
  45. data/spec/fixtures/vcr_cassettes/app_test_accounts.yml +97 -0
  46. data/spec/integration/graph_collection_spec.rb +8 -5
  47. data/spec/spec_helper.rb +2 -2
  48. data/spec/support/graph_api_shared_examples.rb +152 -337
  49. data/spec/support/koala_test.rb +11 -13
  50. data/spec/support/mock_http_service.rb +11 -14
  51. data/spec/support/uploadable_io_shared_examples.rb +4 -4
  52. metadata +47 -48
  53. data/.autotest +0 -12
  54. data/.travis.yml +0 -17
  55. data/Guardfile +0 -6
  56. data/autotest/discover.rb +0 -1
  57. data/lib/koala/api/rest_api.rb +0 -135
  58. data/lib/koala/http_service/multipart_request.rb +0 -41
  59. data/spec/cases/multipart_request_spec.rb +0 -65
  60. data/spec/support/rest_api_shared_examples.rb +0 -168
@@ -16,32 +16,31 @@ describe Koala::HTTPService do
16
16
  end
17
17
 
18
18
  describe "DEFAULT_MIDDLEWARE" do
19
- before :each do
20
- @builder = double("Faraday connection builder")
21
- allow(@builder).to receive(:request)
22
- allow(@builder).to receive(:adapter)
23
- allow(@builder).to receive(:use)
24
- end
19
+ class FakeBuilder
20
+ attr_reader :requests, :uses, :adapters
25
21
 
26
- it "is defined" do
27
- expect(Koala::HTTPService.const_defined?("DEFAULT_MIDDLEWARE")).to be_truthy
28
- end
22
+ def use(arg)
23
+ @uses ||= []
24
+ @uses << arg
25
+ end
29
26
 
30
- it "adds multipart" do
31
- expect(@builder).to receive(:use).with(Koala::HTTPService::MultipartRequest)
32
- Koala::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
33
- end
27
+ def request(arg)
28
+ @requests ||= []
29
+ @requests << arg
30
+ end
34
31
 
35
- it "adds url_encoded" do
36
- expect(@builder).to receive(:request).with(:url_encoded)
37
- Koala::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
32
+ def adapter(arg)
33
+ @adapters ||= []
34
+ @adapters << arg
35
+ end
38
36
  end
39
37
 
40
- it "uses the default adapter" do
41
- adapter = :testing_now
42
- allow(Faraday).to receive(:default_adapter).and_return(adapter)
43
- expect(@builder).to receive(:adapter).with(adapter)
44
- Koala::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
38
+ let(:builder) { FakeBuilder.new }
39
+
40
+ it "adds the right default middleware" do
41
+ Koala::HTTPService::DEFAULT_MIDDLEWARE.call(builder)
42
+ expect(builder.requests).to eq([:multipart, :url_encoded])
43
+ expect(builder.adapters).to eq([Faraday.default_adapter])
45
44
  end
46
45
  end
47
46
 
@@ -52,10 +51,6 @@ describe Koala::HTTPService do
52
51
  expect(defaults[:graph_server]).to eq("graph.facebook.com")
53
52
  end
54
53
 
55
- it "defines the rest server" do
56
- expect(defaults[:rest_server]).to eq("api.facebook.com")
57
- end
58
-
59
54
  it "defines the dialog host" do
60
55
  expect(defaults[:dialog_host]).to eq("www.facebook.com")
61
56
  end
@@ -73,86 +68,21 @@ describe Koala::HTTPService do
73
68
  end
74
69
  end
75
70
 
76
- describe "server" do
77
- describe "with no options" do
78
- it "returns the REST server if options[:rest_api]" do
79
- expect(Koala::HTTPService.server(:rest_api => true)).to eq(
80
- "http://#{Koala.config.rest_server}"
81
- )
82
- end
83
-
84
- it "returns the graph server if !options[:rest_api]" do
85
- expect(Koala::HTTPService.server(:rest_api => false)).to eq(
86
- "http://#{Koala.config.graph_server}"
87
- )
88
- expect(Koala::HTTPService.server({})).to eq(
89
- "http://#{Koala.config.graph_server}"
90
- )
91
- end
92
-
93
- context "with use_ssl" do
94
- it "includes https" do
95
- expect(Koala::HTTPService.server(use_ssl: true)).to eq(
96
- "https://#{Koala.config.graph_server}"
97
- )
98
- end
99
- end
100
- end
101
-
102
- describe "with options[:beta]" do
103
- before :each do
104
- @options = {:beta => true}
105
- end
106
-
107
- it "returns the beta REST server if options[:rest_api]" do
108
- server = Koala::HTTPService.server(@options.merge(:rest_api => true))
109
- expect(server).to match(Regexp.new(Koala.config.rest_server.gsub(/\.facebook/, ".beta.facebook")))
110
- end
111
-
112
- it "returns the beta rest server if !options[:rest_api]" do
113
- server = Koala::HTTPService.server(@options)
114
- expect(server).to match(Regexp.new(Koala.config.graph_server.gsub(/\.facebook/, ".beta.facebook")))
115
- end
116
- end
117
-
118
- describe "with options[:video]" do
119
- before :each do
120
- @options = {:video => true}
121
- end
122
-
123
- it "returns the REST video server if options[:rest_api]" do
124
- server = Koala::HTTPService.server(@options.merge(:rest_api => true))
125
- expect(server).to match(Regexp.new(Koala.config.rest_server.gsub(/\.facebook/, "-video.facebook")))
126
- end
127
-
128
- it "returns the graph video server if !options[:rest_api]" do
129
- server = Koala::HTTPService.server(@options)
130
- expect(server).to match(Regexp.new(Koala.config.graph_server.gsub(/\.facebook/, "-video.facebook")))
131
- end
132
- end
133
- end
134
-
135
71
  describe ".encode_params" do
136
72
  it "returns an empty string if param_hash evaluates to false" do
137
73
  expect(Koala::HTTPService.encode_params(nil)).to eq('')
138
74
  end
139
75
 
140
76
  it "converts values to JSON if the value is not a String" do
141
- val = 'json_value'
142
- not_a_string = 'not_a_string'
143
- allow(not_a_string).to receive(:is_a?).and_return(false)
144
- expect(MultiJson).to receive(:dump).with(not_a_string).and_return(val)
145
-
146
- string = "hi"
77
+ not_a_string = {not_a_string: 2}
147
78
 
148
79
  args = {
149
- not_a_string => not_a_string,
150
- string => string
80
+ :arg => not_a_string,
151
81
  }
152
82
 
153
83
  result = Koala::HTTPService.encode_params(args)
154
84
  expect(result.split('&').find do |key_and_val|
155
- key_and_val.match("#{not_a_string}=#{val}")
85
+ key_and_val.match("arg=#{CGI.escape not_a_string.to_json}")
156
86
  end).to be_truthy
157
87
  end
158
88
 
@@ -185,244 +115,154 @@ describe Koala::HTTPService do
185
115
  end
186
116
 
187
117
  describe ".make_request" do
188
- before :each do
189
- # Setup stubs for make_request to execute without exceptions
190
- @mock_body = double('Typhoeus response body')
191
- @mock_headers_hash = double({:value => "headers hash"})
192
- @mock_http_response = double("Faraday Response", :status => 200, :headers => @mock_headers_hash, :body => @mock_body)
193
-
194
- @mock_connection = double("Faraday connection")
195
- allow(@mock_connection).to receive(:get).and_return(@mock_http_response)
196
- allow(@mock_connection).to receive(:post).and_return(@mock_http_response)
197
- allow(Faraday).to receive(:new).and_return(@mock_connection)
198
- end
199
-
200
- describe "creating the Faraday connection" do
201
- it "creates a Faraday connection using the server" do
202
- server = "foo"
203
- allow(Koala::HTTPService).to receive(:server).and_return(server)
204
- expect(Faraday).to receive(:new).with(server, anything).and_return(@mock_connection)
205
- Koala::HTTPService.make_request("anything", {}, "anything")
206
- end
207
-
208
- it "merges Koala::HTTPService.http_options into the request params" do
209
- http_options = {:proxy => "http://user:password@example.org/", :request => { :timeout => 3 }}
210
- Koala::HTTPService.http_options = http_options
211
- expect(Faraday).to receive(:new).with(anything, hash_including(http_options)).and_return(@mock_connection)
212
- Koala::HTTPService.make_request("anything", {}, "get")
213
- end
118
+ let(:mock_body) { "a body" }
119
+ let(:mock_headers_hash) { double(value: "headers hash") }
120
+ let(:mock_http_response) { double("Faraday Response", status: 200, headers: mock_headers_hash, body: mock_body) }
214
121
 
215
- it "does not merge invalid Faraday options from Koala::HTTPService.http_options into the request params" do
216
- http_options = {:invalid => "fake param"}
217
- Koala::HTTPService.http_options = http_options
218
- expect(Faraday).to receive(:new).with(anything, hash_not_including(http_options)).and_return(@mock_connection)
219
- Koala::HTTPService.make_request("anything", {}, "get")
220
- end
122
+ let(:verb) { "get" }
123
+ let(:options) { {} }
124
+ let(:args) { {"an" => :arg } }
125
+ let(:request) { Koala::HTTPService::Request.new(path: "/foo", verb: verb, args: args, options: options) }
221
126
 
222
- it "merges any provided options into the request params" do
223
- options = {:proxy => "http://user:password@example.org/", :request => { :timeout => 3 }}
224
- expect(Faraday).to receive(:new).with(anything, hash_including(options)).and_return(@mock_connection)
225
- Koala::HTTPService.make_request("anything", {}, "get", options)
127
+ shared_examples_for :making_a_request do
128
+ before :each do
129
+ allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(mock_http_response)
130
+ allow_any_instance_of(Faraday::Connection).to receive(:post).and_return(mock_http_response)
226
131
  end
227
132
 
228
- it "overrides Koala::HTTPService.http_options with any provided options for the request params" do
229
- options = {:proxy => "http://user:password@proxy.org/", :request => { :timeout => 10 }}
230
- http_options = {:proxy => "http://user:password@example.org/", :request => { :timeout => 3 }}
231
- allow(Koala::HTTPService).to receive(:http_options).and_return(http_options)
133
+ it "makes a Faraday request appropriately" do
134
+ expect_any_instance_of(Faraday::Connection).to receive(verb) do |instance, path, post_params|
135
+ expect(path).to eq(request.path)
136
+ expect(post_params).to eq(request.post_args)
137
+ expect(instance.params).to eq(request.options[:params])
138
+ expect(instance.url_prefix).to eq(URI.parse(request.server))
232
139
 
233
- expect(Faraday).to receive(:new).with(anything, hash_including(http_options.merge(options))).and_return(@mock_connection)
234
- Koala::HTTPService.make_request("anything", {}, "get", options)
235
- end
140
+ mock_http_response
141
+ end
236
142
 
237
- it "forces use_ssl to true if an access token is present" do
238
- options = {:use_ssl => false}
239
- allow(Koala::HTTPService).to receive(:http_options).and_return(:use_ssl => false)
240
- expect(Faraday).to receive(:new).with(anything, hash_including(:ssl => {:verify => true})).and_return(@mock_connection)
241
- Koala::HTTPService.make_request("anything", {"access_token" => "foo"}, "get", options)
143
+ Koala::HTTPService.make_request(request)
242
144
  end
243
145
 
244
- it "defaults verify to true if use_ssl is true" do
245
- expect(Faraday).to receive(:new).with(anything, hash_including(:ssl => {:verify => true})).and_return(@mock_connection)
246
- Koala::HTTPService.make_request("anything", {"access_token" => "foo"}, "get")
247
- end
146
+ it "returns the right response" do
147
+ response = Koala::HTTPService.make_request(request)
148
+ expect(response.status).to eq(mock_http_response.status)
149
+ expect(response.headers).to eq(mock_http_response.headers)
150
+ expect(response.body).to eq(mock_http_response.body)
248
151
 
249
- it "allows you to set other verify modes if you really want" do
250
- options = {:ssl => {:verify => :foo}}
251
- expect(Faraday).to receive(:new).with(anything, hash_including(options)).and_return(@mock_connection)
252
- Koala::HTTPService.make_request("anything", {"access_token" => "foo"}, "get", options)
253
152
  end
254
153
 
255
- it "calls server with a json object when provided a format option for post requests" do
256
- # Unstub the now somewhat regrettable stubbing above
257
- allow(Faraday).to receive(:new).and_call_original
154
+ it "logs verb, url and params to debug" do
155
+ log_message = "#{verb.upcase}: #{request.path} params: #{request.raw_args.inspect}"
156
+ expect(Koala::Utils.logger).to receive(:debug).with("STARTED => #{log_message}")
157
+ expect(Koala::Utils.logger).to receive(:debug).with("FINISHED => #{log_message}")
258
158
 
259
- mock_request_klass = Class.new do
260
- attr_accessor :path, :body, :headers, :status
261
- def initialize
262
- @headers = {}
263
- end
264
- end
159
+ Koala::HTTPService.make_request(request)
160
+ end
161
+ end
265
162
 
266
- mock_request = mock_request_klass.new
267
- allow_any_instance_of(Faraday::Connection).to receive(:post).and_yield(mock_request)
163
+ context "for gets" do
164
+ it_should_behave_like :making_a_request
165
+ end
268
166
 
269
- path = "California"
270
- args = {:a => 2, :c => "3"}
167
+ # we don't need to test delete and put since those are translated into posts
168
+ context "for posts" do
169
+ let(:verb) { "post" }
271
170
 
272
- Koala::HTTPService.make_request(path, args, "post", format: :json)
171
+ it_should_behave_like :making_a_request
172
+ end
273
173
 
274
- expect(mock_request.path).to eq(path)
275
- expect(mock_request.headers).to eq("Content-Type" => "application/json")
276
- expect(mock_request.body).to eq(args.to_json)
277
- end
174
+ context "for JSON requests" do
175
+ let(:verb) { "post" }
176
+ let(:options) { {format: :json} }
278
177
 
279
- it "calls server with the composite options" do
280
- options = {:a => 2, :c => "3"}
281
- http_options = {:a => :a}
282
- allow(Koala::HTTPService).to receive(:http_options).and_return(http_options)
283
- expect(Koala::HTTPService).to receive(:server).with(hash_including(http_options.merge(options))).and_return("foo")
284
- Koala::HTTPService.make_request("anything", {}, "get", options)
285
- end
178
+ it "makes a Faraday request appropriately" do
179
+ expect_any_instance_of(Faraday::Connection).to receive(verb) do |instance, path, &block|
180
+ faraday_request = Faraday::Request.new
181
+ faraday_request.headers = {}
182
+ block.call(faraday_request)
183
+ expect(faraday_request.path).to eq(request.path)
184
+ expect(faraday_request.body).to eq(request.post_args.to_json)
185
+ expect(faraday_request.headers).to include("Content-Type" => "application/json")
286
186
 
287
- it "uses the default builder block if HTTPService.faraday_middleware block is not defined" do
288
- block = Proc.new {}
289
- stub_const("Koala::HTTPService::DEFAULT_MIDDLEWARE", block)
290
- allow(Koala::HTTPService).to receive(:faraday_middleware).and_return(nil)
291
- expect(Faraday).to receive(:new).with(anything, anything, &block).and_return(@mock_connection)
292
- Koala::HTTPService.make_request("anything", {}, "get")
293
- end
187
+ mock_http_response
188
+ end
294
189
 
295
- it "uses the defined HTTPService.faraday_middleware block if defined" do
296
- block = Proc.new { }
297
- expect(Koala::HTTPService).to receive(:faraday_middleware).and_return(block)
298
- expect(Faraday).to receive(:new).with(anything, anything, &block).and_return(@mock_connection)
299
- Koala::HTTPService.make_request("anything", {}, "get")
190
+ Koala::HTTPService.make_request(request)
300
191
  end
301
192
  end
302
193
 
194
+ it "uses the default builder block if HTTPService.faraday_middleware block is not defined" do
195
+ block = Proc.new { |builder|
196
+ builder.request :multipart
197
+ builder.request :url_encoded
198
+ }
199
+ stub_const("Koala::HTTPService::DEFAULT_MIDDLEWARE", block)
200
+ allow(Koala::HTTPService).to receive(:faraday_middleware).and_return(nil)
303
201
 
304
- context "with API versions" do
305
- it "adds a version if specified by Koala.config" do
306
- expect(Koala.config).to receive(:api_version).and_return("v11")
307
- expect(@mock_connection).to receive(:get).with("/v11/anything", anything)
308
- Koala::HTTPService.make_request("anything", {}, "get")
202
+ expect_any_instance_of(Faraday::Connection).to receive(:get) do |instance|
203
+ expect(instance.builder.handlers).to eq([
204
+ Faraday::Multipart::Middleware,
205
+ Faraday::Request::UrlEncoded,
206
+ ])
207
+ mock_http_response
309
208
  end
310
209
 
311
- it "prefers a version set in http_options" do
312
- allow(Koala.config).to receive(:api_version).and_return("v11")
313
- allow(Koala::HTTPService).to receive(:http_options).and_return({ api_version: 'v12' })
314
- expect(@mock_connection).to receive(:get).with("/v12/anything", anything)
315
- Koala::HTTPService.make_request("anything", {}, "get")
316
- end
210
+ Koala::HTTPService.make_request(request)
211
+ end
317
212
 
318
- it "doesn't add double slashes to the path" do
319
- allow(Koala::HTTPService).to receive(:http_options).and_return({ api_version: 'v12' })
320
- expect(@mock_connection).to receive(:get).with("/v12/anything", anything)
321
- Koala::HTTPService.make_request("/anything", {}, "get")
322
- end
213
+ it "uses the defined HTTPService.faraday_middleware block if defined" do
214
+ block = Proc.new { |builder|
215
+ builder.request :multipart
216
+ builder.request :url_encoded
217
+ }
218
+ expect(Koala::HTTPService).to receive(:faraday_middleware).and_return(block)
323
219
 
324
- it "doesn't add a version if the path already contains one" do
325
- expect(Koala.config).to receive(:api_version).and_return("v11")
326
- expect(@mock_connection).to receive(:get).with("/v12/anything", anything)
327
- Koala::HTTPService.make_request("/v12/anything", {}, "get")
220
+ expect_any_instance_of(Faraday::Connection).to receive(:get) do |instance|
221
+ expect(instance.builder.handlers).to eq([
222
+ Faraday::Multipart::Middleware,
223
+ Faraday::Request::UrlEncoded,
224
+ ])
225
+ mock_http_response
328
226
  end
329
- end
330
227
 
331
- it "makes a POST request if the verb isn't get" do
332
- expect(@mock_connection).to receive(:post).and_return(@mock_http_response)
333
- Koala::HTTPService.make_request("anything", {}, "anything")
228
+ Koala::HTTPService.make_request(request)
334
229
  end
335
230
 
336
- it "includes the verb in the body if the verb isn't get" do
337
- verb = "eat"
338
- expect(@mock_connection).to receive(:post).with(anything, hash_including("method" => verb)).and_return(@mock_http_response)
339
- Koala::HTTPService.make_request("anything", {}, verb)
340
- end
341
-
342
- it "makes a GET request if the verb is get" do
343
- expect(@mock_connection).to receive(:get).and_return(@mock_http_response)
344
- Koala::HTTPService.make_request("anything", {}, "get")
345
- end
231
+ context 'log_tokens configuration' do
232
+ let(:args) { { "an" => :arg, "access_token" => "myvisbleaccesstoken" } }
346
233
 
347
- describe "for GETs" do
348
- it "submits the arguments in the body" do
349
- # technically this is done for all requests, but you don't send GET requests with files
350
- args = {"a" => :b, "c" => 3}
351
- expect(Faraday).to receive(:new).with(anything, hash_including(:params => args)).and_return(@mock_connection)
352
- Koala::HTTPService.make_request("anything", args, "get")
234
+ before(:each) do
235
+ allow_any_instance_of(Faraday::Connection).to receive(:get) { double(status: '200', body: 'ok', headers: {}) }
353
236
  end
354
237
 
355
- it "submits nothing to the body" do
356
- # technically this is done for all requests, but you don't send GET requests with files
357
- args = {"a" => :b, "c" => 3}
358
- expect(@mock_connection).to receive(:get).with(anything, {}).and_return(@mock_http_response)
359
- Koala::HTTPService.make_request("anything", args, "get")
360
- end
238
+ it 'logs tokens' do
239
+ allow(Koala.config).to receive(:mask_tokens) { false }
361
240
 
362
- it "logs verb, url and params to debug" do
363
- args = {"a" => :b, "c" => 3}
364
- log_message_stem = "GET: anything params: "
365
- expect(Koala::Utils.logger).to receive(:debug) do |log_message|
366
- # unordered hashes are a bane
367
- # Ruby in 1.8 modes tends to return different hash orderings,
368
- # which makes checking the content of the stringified hash hard
369
- # it's enough just to ensure that there's hash content in the string, I think
370
- expect(log_message).to include(log_message_stem)
371
- expect(log_message.match(/\{.*\}/)).not_to be_nil
372
- end
241
+ expect(Koala::Utils).to receive(:debug).with('STARTED => GET: /foo params: {"an"=>:arg, "access_token"=>"myvisbleaccesstoken"}')
242
+ expect(Koala::Utils).to receive(:debug).with('FINISHED => GET: /foo params: {"an"=>:arg, "access_token"=>"myvisbleaccesstoken"}')
373
243
 
374
- Koala::HTTPService.make_request("anything", args, "get")
244
+ Koala::HTTPService.make_request(request)
375
245
  end
376
- end
377
246
 
378
- describe "for POSTs" do
379
- it "submits the arguments in the body" do
380
- # technically this is done for all requests, but you don't send GET requests with files
381
- args = {"a" => :b, "c" => 3}
382
- expect(@mock_connection).to receive(:post).with(anything, hash_including(args)).and_return(@mock_http_response)
383
- Koala::HTTPService.make_request("anything", args, "post")
384
- end
247
+ it 'doesnt log tokens' do
248
+ allow(Koala.config).to receive(:mask_tokens) { true }
385
249
 
386
- it "turns any UploadableIOs to UploadIOs" do
387
- # technically this is done for all requests, but you don't send GET requests with files
388
- upload_io = double("UploadIO")
389
- u = Koala::UploadableIO.new("/path/to/stuff", "img/jpg")
390
- allow(u).to receive(:to_upload_io).and_return(upload_io)
391
- expect(@mock_connection).to receive(:post).with(anything, hash_including("source" => upload_io)).and_return(@mock_http_response)
392
- Koala::HTTPService.make_request("anything", {:source => u}, "post")
393
- end
250
+ expect(Koala::Utils).to receive(:debug).with('STARTED => GET: /foo params: {"an"=>:arg, "access_token"=>"myvisbleac*****token"}')
251
+ expect(Koala::Utils).to receive(:debug).with('FINISHED => GET: /foo params: {"an"=>:arg, "access_token"=>"myvisbleac*****token"}')
394
252
 
395
- it "logs verb, url and params to debug" do
396
- args = {"a" => :b, "c" => 3}
397
- log_message_stem = "POST: anything params: "
398
- expect(Koala::Utils.logger).to receive(:debug) do |log_message|
399
- # unordered hashes are a bane
400
- # Ruby in 1.8 modes tends to return different hash orderings,
401
- # which makes checking the content of the stringified hash hard
402
- # it's enough just to ensure that there's hash content in the string, I think
403
- expect(log_message).to include(log_message_stem)
404
- expect(log_message.match(/\{.*\}/)).not_to be_nil
405
- end
406
- Koala::HTTPService.make_request("anything", args, "post")
253
+ Koala::HTTPService.make_request(request)
407
254
  end
408
- end
409
- end
410
255
 
411
- describe ".path_contains_api_version?" do
412
- it "works when the path is prefixed by a slash" do
413
- expect(Koala::HTTPService.path_contains_api_version?('/v2.1/anything')).to be true
414
- end
256
+ it 'hides the token for the debug_token api endpoint' do
257
+ request = Koala::HTTPService::Request.new(path: "/debug_token", verb: verb, args: { input_token: 'myvisibleaccesstoken', 'access_token' => 'myvisibleaccesstoken' }, options: options)
415
258
 
416
- it "works when the path is not prefixed by a slash" do
417
- expect(Koala::HTTPService.path_contains_api_version?('v2.1/anything')).to be true
418
- end
259
+ allow(Koala.config).to receive(:mask_tokens) { true }
419
260
 
420
- it "works with versions without a ." do
421
- expect(Koala::HTTPService.path_contains_api_version?('v21/anything')).to be true
422
- end
261
+ expect(Koala::Utils).to receive(:debug).with('STARTED => GET: /debug_token params: {"input_token"=>"myvisiblea*****token", "access_token"=>"myvisiblea*****token"}')
262
+ expect(Koala::Utils).to receive(:debug).with('FINISHED => GET: /debug_token params: {"input_token"=>"myvisiblea*****token", "access_token"=>"myvisiblea*****token"}')
423
263
 
424
- it "returns nil for paths without a version" do
425
- expect(Koala::HTTPService.path_contains_api_version?('/anything')).to be false
264
+ Koala::HTTPService.make_request(request)
265
+ end
426
266
  end
427
267
  end
428
268
  end
@@ -19,16 +19,19 @@ describe Koala do
19
19
  verb = "get"
20
20
  options = {:c => :d}
21
21
 
22
- expect(Koala.http_service).to receive(:make_request).with(path, args, verb, options)
22
+ expect(Koala.http_service).to receive(:make_request) do |request|
23
+ expect(request.raw_path).to eq(path)
24
+ expect(request.raw_args).to eq(args)
25
+ expect(request.raw_verb).to eq(verb)
26
+ expect(request.raw_options).to eq(options)
27
+ end
23
28
  Koala.make_request(path, args, verb, options)
24
29
  end
25
30
  end
26
31
 
27
32
  describe ".configure" do
28
33
  it "yields a configurable object" do
29
- expect {
30
- Koala.configure {|c| c.foo = "bar"}
31
- }.not_to raise_exception
34
+ Koala.configure {|c| expect(c).to be_a(Koala::Configuration)}
32
35
  end
33
36
 
34
37
  it "caches the config (singleton)" do
@@ -51,5 +54,4 @@ describe Koala do
51
54
  expect(Koala.config.graph_server).to eq("some-new.graph_server.com")
52
55
  end
53
56
  end
54
-
55
57
  end
@@ -53,6 +53,45 @@ describe "Koala::Facebook::OAuth" do
53
53
  @oauth.app_secret == @secret &&
54
54
  @oauth.oauth_callback_url == nil).to be_truthy
55
55
  end
56
+
57
+ context "with global defaults" do
58
+ let(:app_id) { :app_id }
59
+ let(:app_secret) { :app_secret }
60
+ let(:oauth_callback_url) { :oauth_callback_url }
61
+
62
+ before :each do
63
+ Koala.configure do |config|
64
+ config.app_id = app_id
65
+ config.app_secret = app_secret
66
+ config.oauth_callback_url = oauth_callback_url
67
+ end
68
+ end
69
+
70
+ it "defaults to the configured data if not otherwise provided" do
71
+ oauth = Koala::Facebook::OAuth.new
72
+ expect(oauth.app_id).to eq(app_id)
73
+ expect(oauth.app_secret).to eq(app_secret)
74
+ expect(oauth.oauth_callback_url).to eq(oauth_callback_url)
75
+ end
76
+
77
+ it "lets you override app_id" do
78
+ other_value = :another_id
79
+ oauth = Koala::Facebook::OAuth.new(other_value)
80
+ expect(oauth.app_id).to eq(other_value)
81
+ end
82
+
83
+ it "lets you override secret" do
84
+ other_value = :another_secret
85
+ oauth = Koala::Facebook::OAuth.new(nil, other_value)
86
+ expect(oauth.app_secret).to eq(other_value)
87
+ end
88
+
89
+ it "lets you override app_id" do
90
+ other_value = :another_token
91
+ oauth = Koala::Facebook::OAuth.new(nil, nil, other_value)
92
+ expect(oauth.oauth_callback_url).to eq(other_value)
93
+ end
94
+ end
56
95
  end
57
96
 
58
97
  describe "for cookie parsing" do
@@ -401,7 +440,7 @@ describe "Koala::Facebook::OAuth" do
401
440
  allow(Koala).to receive(:make_request).and_return(
402
441
  Koala::HTTPService::Response.new(
403
442
  200,
404
- MultiJson.dump(result),
443
+ JSON.dump(result),
405
444
  {}
406
445
  )
407
446
  )
@@ -584,7 +623,7 @@ describe "Koala::Facebook::OAuth" do
584
623
  # the signed request code is ported directly from Facebook
585
624
  # so we only need to test at a high level that it works
586
625
  it "throws an error if the algorithm is unsupported" do
587
- allow(MultiJson).to receive(:load).and_return("algorithm" => "my fun algorithm")
626
+ allow(JSON).to receive(:parse).and_return("algorithm" => "my fun algorithm")
588
627
  expect { @oauth.parse_signed_request(@signed_params) }.to raise_error(Koala::Facebook::OAuthSignatureError)
589
628
  end
590
629