koala 1.11.1 → 2.0.0rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,35 +12,6 @@ describe Koala do
12
12
  end
13
13
  end
14
14
 
15
- context "for deprecated services" do
16
- before :each do
17
- @service = Koala.http_service
18
- end
19
-
20
- after :each do
21
- Koala.http_service = @service
22
- end
23
-
24
- it "invokes deprecated_interface if present" do
25
- mock_service = double("http service")
26
- expect(mock_service).to receive(:deprecated_interface)
27
- Koala.http_service = mock_service
28
- end
29
-
30
- it "does not set the service if it's deprecated" do
31
- mock_service = double("http service")
32
- allow(mock_service).to receive(:deprecated_interface)
33
- Koala.http_service = mock_service
34
- expect(Koala.http_service).to eq(@service)
35
- end
36
-
37
- it "sets the service if it's not deprecated" do
38
- mock_service = double("http service")
39
- Koala.http_service = mock_service
40
- expect(Koala.http_service).to eq(mock_service)
41
- end
42
- end
43
-
44
15
  describe "make_request" do
45
16
  it "passes all its arguments to the http_service" do
46
17
  path = "foo"
@@ -17,8 +17,8 @@ describe "Koala::Facebook::OAuth" do
17
17
 
18
18
  # this should expanded to cover all variables
19
19
  raise Exception, "Must supply app data to run FacebookOAuthTests!" unless @app_id && @secret && @callback_url &&
20
- @raw_token_string &&
21
- @raw_offline_access_token_string
20
+ @raw_token_string &&
21
+ @raw_offline_access_token_string
22
22
 
23
23
  # we can just test against the same key twice
24
24
  @multiple_session_keys = [KoalaTest.session_key, KoalaTest.session_key] if KoalaTest.session_key
@@ -39,8 +39,8 @@ describe "Koala::Facebook::OAuth" do
39
39
 
40
40
  it "properly sets attributes" do
41
41
  expect(@oauth.app_id == @app_id &&
42
- @oauth.app_secret == @secret &&
43
- @oauth.oauth_callback_url == @callback_url).to be_truthy
42
+ @oauth.app_secret == @secret &&
43
+ @oauth.oauth_callback_url == @callback_url).to be_truthy
44
44
  end
45
45
 
46
46
  it "properly initializes without a callback_url" do
@@ -50,8 +50,8 @@ describe "Koala::Facebook::OAuth" do
50
50
  it "properly sets attributes without a callback URL" do
51
51
  @oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
52
52
  expect(@oauth.app_id == @app_id &&
53
- @oauth.app_secret == @secret &&
54
- @oauth.oauth_callback_url == nil).to be_truthy
53
+ @oauth.app_secret == @secret &&
54
+ @oauth.oauth_callback_url == nil).to be_truthy
55
55
  end
56
56
  end
57
57
 
@@ -120,14 +120,14 @@ describe "Koala::Facebook::OAuth" do
120
120
 
121
121
  it "returns nil if the call to FB returns an expired code error" do
122
122
  allow(@oauth).to receive(:get_access_token_info).and_raise(Koala::Facebook::OAuthTokenRequestError.new(400,
123
- '{ "error": { "type": "OAuthException", "message": "Code was invalid or expired. Session has expired at unix time 1324044000. The current unix time is 1324300957." } }'
124
- ))
123
+ '{ "error": { "type": "OAuthException", "message": "Code was invalid or expired. Session has expired at unix time 1324044000. The current unix time is 1324300957." } }'
124
+ ))
125
125
  expect(@oauth.get_user_info_from_cookies(@cookie)).to be_nil
126
126
  end
127
127
 
128
128
  it "raises the error if the call to FB returns a different error" do
129
129
  allow(@oauth).to receive(:get_access_token_info).and_raise(Koala::Facebook::OAuthTokenRequestError.new(400,
130
- '{ "error": { "type": "OtherError", "message": "A Facebook Error" } }'))
130
+ '{ "error": { "type": "OtherError", "message": "A Facebook Error" } }'))
131
131
  expect { @oauth.get_user_info_from_cookies(@cookie) }.to raise_exception(Koala::Facebook::OAuthTokenRequestError)
132
132
  end
133
133
  end
@@ -180,596 +180,429 @@ describe "Koala::Facebook::OAuth" do
180
180
  end
181
181
  end
182
182
 
183
- describe "get_user_from_cookies" do
184
- describe "for signed cookies" do
185
- before :each do
186
- # we don't actually want to make requests to Facebook to redeem the code
187
- @cookie = KoalaTest.oauth_test_data["valid_signed_cookies"]
188
- allow(@oauth).to receive(:get_access_token_info).and_return("access_token" => "my token")
183
+ describe "for URL generation" do
184
+ describe "#url_for_oauth_code" do
185
+ it "generates a properly formatted OAuth code URL with the default values" do
186
+ url = @oauth.url_for_oauth_code
187
+ expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&redirect_uri=#{CGI.escape @callback_url}")
189
188
  end
190
189
 
191
- it "does not uses get_user_info_from_cookies to parse the cookies" do
192
- expect(@oauth).not_to receive(:get_user_info_from_cookies).with(@cookie)
193
- @oauth.get_user_from_cookies(@cookie)
190
+ it "includes the api version if specified" do
191
+ version = Koala.config.api_version = "v.2.2.2.2"
192
+ url = @oauth.url_for_oauth_code
193
+ expect(url).to match_url("https://#{Koala.config.dialog_host}/#{version}/dialog/oauth?client_id=#{@app_id}&redirect_uri=#{CGI.escape @callback_url}")
194
194
  end
195
195
 
196
- it "uses return the facebook user id string if the cookies are valid" do
197
- result = @oauth.get_user_from_cookies(@cookie)
198
- expect(result).to eq("2905623") # the user who generated the original test cookie
196
+ it "generates a properly formatted OAuth code URL when a callback is given" do
197
+ callback = "foo.com"
198
+ url = @oauth.url_for_oauth_code(:callback => callback)
199
+ expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&redirect_uri=#{callback}")
199
200
  end
200
201
 
201
- it "returns nil if the cookies are invalid" do
202
- # make an invalid string by replacing some values
203
- bad_cookie_hash = @cookie.inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
204
- result = @oauth.get_user_from_cookies(bad_cookie_hash)
205
- expect(result).to be_nil
202
+ it "generates a properly formatted OAuth code URL when permissions are requested as a string" do
203
+ permissions = "publish_stream,read_stream"
204
+ url = @oauth.url_for_oauth_code(:permissions => permissions)
205
+ expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&scope=#{CGI.escape permissions}&redirect_uri=#{CGI.escape @callback_url}")
206
206
  end
207
207
 
208
- it "is deprecated" do
209
- expect(Koala::Utils).to receive(:deprecate)
210
- @oauth.get_user_from_cookies({})
208
+ it "generates a properly formatted OAuth code URL when permissions are requested as a string" do
209
+ permissions = ["publish_stream", "read_stream"]
210
+ url = @oauth.url_for_oauth_code(:permissions => permissions)
211
+ expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&scope=#{CGI.escape permissions.join(",")}&redirect_uri=#{CGI.escape @callback_url}")
211
212
  end
212
- end
213
213
 
214
- describe "for unsigned cookies" do
215
- before :each do
216
- # we don't actually want to make requests to Facebook to redeem the code
217
- @cookie = KoalaTest.oauth_test_data["valid_cookies"]
214
+ it "generates a properly formatted OAuth code URL when both permissions and callback are provided" do
215
+ permissions = "publish_stream,read_stream"
216
+ callback = "foo.com"
217
+ url = @oauth.url_for_oauth_code(:callback => callback, :permissions => permissions)
218
+ expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&scope=#{CGI.escape permissions}&redirect_uri=#{CGI.escape callback}")
218
219
  end
219
220
 
220
- it "uses get_user_info_from_cookies to parse the cookies" do
221
- expect(@oauth).to receive(:get_user_info_from_cookies).with(@cookie).and_return({})
222
- @oauth.get_user_from_cookies(@cookie)
221
+ it "generates a properly formatted OAuth code URL when a display is given as a string" do
222
+ url = @oauth.url_for_oauth_code(:display => "page")
223
+ expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&display=page&redirect_uri=#{CGI.escape @callback_url}")
223
224
  end
224
225
 
225
- it "uses return a string if the cookies are valid" do
226
- result = @oauth.get_user_from_cookies(@cookie)
227
- expect(result).to eq("2905623") # the user who generated the original test cookie
226
+ it "raises an exception if no callback is given in initialization or the call" do
227
+ oauth2 = Koala::Facebook::OAuth.new(@app_id, @secret)
228
+ expect { oauth2.url_for_oauth_code }.to raise_error(ArgumentError)
228
229
  end
229
230
 
230
- it "returns nil if the cookies are invalid" do
231
- # make an invalid string by replacing some values
232
- bad_cookie_hash = @cookie.inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
233
- result = @oauth.get_user_from_cookies(bad_cookie_hash)
234
- expect(result).to be_nil
231
+ it "includes any additional options as URL parameters, appropriately escaped" do
232
+ params = {
233
+ :url => "http://foo.bar?c=2",
234
+ :email => "cdc@b.com"
235
+ }
236
+ url = @oauth.url_for_oauth_code(params)
237
+ params.each_pair do |key, value|
238
+ expect(url).to match(/[\&\?]#{key}=#{CGI.escape value}/)
239
+ end
235
240
  end
236
241
  end
237
- end
238
- end
239
-
240
- describe "for URL generation" do
241
- describe "#url_for_oauth_code" do
242
- it "generates a properly formatted OAuth code URL with the default values" do
243
- url = @oauth.url_for_oauth_code
244
- expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&redirect_uri=#{CGI.escape @callback_url}")
245
- end
246
-
247
- it "includes the api version if specified" do
248
- version = Koala.config.api_version = "v.2.2.2.2"
249
- url = @oauth.url_for_oauth_code
250
- expect(url).to match_url("https://#{Koala.config.dialog_host}/#{version}/dialog/oauth?client_id=#{@app_id}&redirect_uri=#{CGI.escape @callback_url}")
251
- end
252
-
253
- it "generates a properly formatted OAuth code URL when a callback is given" do
254
- callback = "foo.com"
255
- url = @oauth.url_for_oauth_code(:callback => callback)
256
- expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&redirect_uri=#{callback}")
257
- end
258
-
259
- it "generates a properly formatted OAuth code URL when permissions are requested as a string" do
260
- permissions = "publish_stream,read_stream"
261
- url = @oauth.url_for_oauth_code(:permissions => permissions)
262
- expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&scope=#{CGI.escape permissions}&redirect_uri=#{CGI.escape @callback_url}")
263
- end
264
-
265
- it "generates a properly formatted OAuth code URL when permissions are requested as a string" do
266
- permissions = ["publish_stream", "read_stream"]
267
- url = @oauth.url_for_oauth_code(:permissions => permissions)
268
- expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&scope=#{CGI.escape permissions.join(",")}&redirect_uri=#{CGI.escape @callback_url}")
269
- end
270
242
 
271
- it "generates a properly formatted OAuth code URL when both permissions and callback are provided" do
272
- permissions = "publish_stream,read_stream"
273
- callback = "foo.com"
274
- url = @oauth.url_for_oauth_code(:callback => callback, :permissions => permissions)
275
- expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&scope=#{CGI.escape permissions}&redirect_uri=#{CGI.escape callback}")
276
- end
277
-
278
- it "generates a properly formatted OAuth code URL when a display is given as a string" do
279
- url = @oauth.url_for_oauth_code(:display => "page")
280
- expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&display=page&redirect_uri=#{CGI.escape @callback_url}")
281
- end
282
-
283
- it "raises an exception if no callback is given in initialization or the call" do
284
- oauth2 = Koala::Facebook::OAuth.new(@app_id, @secret)
285
- expect { oauth2.url_for_oauth_code }.to raise_error(ArgumentError)
286
- end
287
-
288
- it "includes any additional options as URL parameters, appropriately escaped" do
289
- params = {
290
- :url => "http://foo.bar?c=2",
291
- :email => "cdc@b.com"
292
- }
293
- url = @oauth.url_for_oauth_code(params)
294
- params.each_pair do |key, value|
295
- expect(url).to match(/[\&\?]#{key}=#{CGI.escape value}/)
243
+ describe "#url_for_access_token" do
244
+ before :each do
245
+ # since we're just composing a URL here, we don't need to have a real code
246
+ @code ||= "test_code"
296
247
  end
297
- end
298
- end
299
-
300
- describe "#url_for_access_token" do
301
- before :each do
302
- # since we're just composing a URL here, we don't need to have a real code
303
- @code ||= "test_code"
304
- end
305
248
 
306
- it "generates a properly formatted OAuth token URL when provided a code" do
307
- url = @oauth.url_for_access_token(@code)
308
- expect(url).to match_url("https://#{Koala.config.graph_server}/oauth/access_token?client_id=#{@app_id}&code=#{@code}&client_secret=#{@secret}&redirect_uri=#{CGI.escape @callback_url}")
309
- end
310
-
311
- it "includes the api version if specified" do
312
- version = Koala.config.api_version = "v.2.2.2.2"
313
- url = @oauth.url_for_access_token(@code)
314
- expect(url).to match_url("https://#{Koala.config.graph_server}/#{version}/oauth/access_token?client_id=#{@app_id}&code=#{@code}&client_secret=#{@secret}&redirect_uri=#{CGI.escape @callback_url}")
315
- end
316
-
317
- it "generates a properly formatted OAuth token URL when provided a callback" do
318
- callback = "foo.com"
319
- url = @oauth.url_for_access_token(@code, :callback => callback)
320
- expect(url).to match_url("https://#{Koala.config.graph_server}/oauth/access_token?client_id=#{@app_id}&code=#{@code}&client_secret=#{@secret}&redirect_uri=#{CGI.escape callback}")
321
- end
322
-
323
- it "includes any additional options as URL parameters, appropriately escaped" do
324
- params = {
325
- :url => "http://foo.bar?c=2",
326
- :email => "cdc@b.com"
327
- }
328
- url = @oauth.url_for_access_token(@code, params)
329
- params.each_pair do |key, value|
330
- expect(url).to match(/[\&\?]#{key}=#{CGI.escape value}/)
249
+ it "generates a properly formatted OAuth token URL when provided a code" do
250
+ url = @oauth.url_for_access_token(@code)
251
+ expect(url).to match_url("https://#{Koala.config.graph_server}/oauth/access_token?client_id=#{@app_id}&code=#{@code}&client_secret=#{@secret}&redirect_uri=#{CGI.escape @callback_url}")
331
252
  end
332
- end
333
- end
334
253
 
335
- describe "#url_for_dialog" do
336
- it "builds the base properly" do
337
- dialog_type = "my_dialog_type"
338
- expect(@oauth.url_for_dialog(dialog_type)).to match(/^https:\/\/#{Koala.config.dialog_host}\/dialog\/#{dialog_type}/)
339
- end
340
-
341
- it "includes the api version if specified" do
342
- version = Koala.config.api_version = "v.2.2.2.2"
343
- dialog_type = "my_dialog_type"
344
- expect(@oauth.url_for_dialog(dialog_type)).to match("https:\/\/#{Koala.config.dialog_host}\/#{version}\/dialog\/#{dialog_type}")
345
- end
346
-
347
- it "adds the app_id/client_id to the url" do
348
- automatic_params = {:app_id => @app_id, :client_id => @client_id}
349
- url = @oauth.url_for_dialog("foo", automatic_params)
350
- automatic_params.each_pair do |key, value|
351
- # we're slightly simplifying how encode_params works, but for strings/ints, it's okay
352
- expect(url).to match(/[\&\?]#{key}=#{CGI.escape value.to_s}/)
254
+ it "includes the api version if specified" do
255
+ version = Koala.config.api_version = "v.2.2.2.2"
256
+ url = @oauth.url_for_access_token(@code)
257
+ expect(url).to match_url("https://#{Koala.config.graph_server}/#{version}/oauth/access_token?client_id=#{@app_id}&code=#{@code}&client_secret=#{@secret}&redirect_uri=#{CGI.escape @callback_url}")
353
258
  end
354
- end
355
259
 
356
- it "includes any additional options as URL parameters, appropriately escaped" do
357
- params = {
358
- :url => "http://foo.bar?c=2",
359
- :email => "cdc@b.com"
360
- }
361
- url = @oauth.url_for_dialog("friends", params)
362
- params.each_pair do |key, value|
363
- # we're slightly simplifying how encode_params works, but strings/ints, it's okay
364
- expect(url).to match(/[\&\?]#{key}=#{CGI.escape value.to_s}/)
260
+ it "generates a properly formatted OAuth token URL when provided a callback" do
261
+ callback = "foo.com"
262
+ url = @oauth.url_for_access_token(@code, :callback => callback)
263
+ expect(url).to match_url("https://#{Koala.config.graph_server}/oauth/access_token?client_id=#{@app_id}&code=#{@code}&client_secret=#{@secret}&redirect_uri=#{CGI.escape callback}")
365
264
  end
366
- end
367
265
 
368
- describe "real examples from FB documentation" do
369
- # see http://developers.facebook.com/docs/reference/dialogs/
370
- # slightly brittle (e.g. if parameter order changes), but still useful
371
- it "can generate a send dialog" do
372
- url = @oauth.url_for_dialog("send", :name => "People Argue Just to Win", :link => "http://www.nytimes.com/2011/06/15/arts/people-argue-just-to-win-scholars-assert.html")
373
- expect(url).to match_url("https://www.facebook.com/dialog/send?app_id=#{@app_id}&client_id=#{@app_id}&link=http%3A%2F%2Fwww.nytimes.com%2F2011%2F06%2F15%2Farts%2Fpeople-argue-just-to-win-scholars-assert.html&name=People+Argue+Just+to+Win&redirect_uri=#{CGI.escape @callback_url}")
374
- end
375
-
376
- it "can generate a feed dialog" do
377
- url = @oauth.url_for_dialog("feed", :name => "People Argue Just to Win", :link => "http://www.nytimes.com/2011/06/15/arts/people-argue-just-to-win-scholars-assert.html")
378
- expect(url).to match_url("https://www.facebook.com/dialog/feed?app_id=#{@app_id}&client_id=#{@app_id}&link=http%3A%2F%2Fwww.nytimes.com%2F2011%2F06%2F15%2Farts%2Fpeople-argue-just-to-win-scholars-assert.html&name=People+Argue+Just+to+Win&redirect_uri=#{CGI.escape @callback_url}")
266
+ it "includes any additional options as URL parameters, appropriately escaped" do
267
+ params = {
268
+ :url => "http://foo.bar?c=2",
269
+ :email => "cdc@b.com"
270
+ }
271
+ url = @oauth.url_for_access_token(@code, params)
272
+ params.each_pair do |key, value|
273
+ expect(url).to match(/[\&\?]#{key}=#{CGI.escape value}/)
274
+ end
379
275
  end
276
+ end
380
277
 
381
- it "can generate a oauth dialog" do
382
- url = @oauth.url_for_dialog("oauth", :scope => "email", :response_type => "token")
383
- expect(url).to match_url("https://www.facebook.com/dialog/oauth?app_id=#{@app_id}&client_id=#{@app_id}&redirect_uri=#{CGI.escape @callback_url}&response_type=token&scope=email")
278
+ describe "#url_for_dialog" do
279
+ it "builds the base properly" do
280
+ dialog_type = "my_dialog_type"
281
+ expect(@oauth.url_for_dialog(dialog_type)).to match(/^https:\/\/#{Koala.config.dialog_host}\/dialog\/#{dialog_type}/)
384
282
  end
385
283
 
386
- it "can generate a pay dialog" do
387
- url = @oauth.url_for_dialog("pay", :order_id => "foo", :credits_purchase => false)
388
- expect(url).to match_url("https://www.facebook.com/dialog/pay?app_id=#{@app_id}&client_id=#{@app_id}&order_id=foo&credits_purchase=false&redirect_uri=#{CGI.escape @callback_url}")
284
+ it "includes the api version if specified" do
285
+ version = Koala.config.api_version = "v.2.2.2.2"
286
+ dialog_type = "my_dialog_type"
287
+ expect(@oauth.url_for_dialog(dialog_type)).to match("https:\/\/#{Koala.config.dialog_host}\/#{version}\/dialog\/#{dialog_type}")
389
288
  end
390
- end
391
- end
392
- end
393
289
 
394
- describe 'for generating a client code' do
395
- describe '#generate_client_code' do
396
- if KoalaTest.mock_interface? || KoalaTest.oauth_token
397
- it 'makes a request using the correct endpoint' do
398
- expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(200, '{"code": "fake_client_code"}', {}))
399
- @oauth.generate_client_code(KoalaTest.oauth_token)
290
+ it "adds the app_id/client_id to the url" do
291
+ automatic_params = {:app_id => @app_id, :client_id => @client_id}
292
+ url = @oauth.url_for_dialog("foo", automatic_params)
293
+ automatic_params.each_pair do |key, value|
294
+ # we're slightly simplifying how encode_params works, but for strings/ints, it's okay
295
+ expect(url).to match(/[\&\?]#{key}=#{CGI.escape value.to_s}/)
296
+ end
400
297
  end
401
298
 
402
- it 'gets a valid client code returned' do
403
- expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(200, '{"code": "fake_client_code"}', {}))
404
- result = @oauth.generate_client_code(KoalaTest.oauth_token)
405
- expect(result).to be_a(String)
406
- expect(result).to eq('fake_client_code')
299
+ it "includes any additional options as URL parameters, appropriately escaped" do
300
+ params = {
301
+ :url => "http://foo.bar?c=2",
302
+ :email => "cdc@b.com"
303
+ }
304
+ url = @oauth.url_for_dialog("friends", params)
305
+ params.each_pair do |key, value|
306
+ # we're slightly simplifying how encode_params works, but strings/ints, it's okay
307
+ expect(url).to match(/[\&\?]#{key}=#{CGI.escape value.to_s}/)
308
+ end
407
309
  end
408
310
 
409
- it 'raises a BadFacebookResponse error when empty response body is returned' do
410
- expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(200, '', {}))
411
- expect { @oauth.generate_client_code(KoalaTest.oauth_token) }.to raise_error(Koala::Facebook::BadFacebookResponse)
412
- end
311
+ describe "real examples from FB documentation" do
312
+ # see http://developers.facebook.com/docs/reference/dialogs/
313
+ # slightly brittle (e.g. if parameter order changes), but still useful
314
+ it "can generate a send dialog" do
315
+ url = @oauth.url_for_dialog("send", :name => "People Argue Just to Win", :link => "http://www.nytimes.com/2011/06/15/arts/people-argue-just-to-win-scholars-assert.html")
316
+ expect(url).to match_url("https://www.facebook.com/dialog/send?app_id=#{@app_id}&client_id=#{@app_id}&link=http%3A%2F%2Fwww.nytimes.com%2F2011%2F06%2F15%2Farts%2Fpeople-argue-just-to-win-scholars-assert.html&name=People+Argue+Just+to+Win&redirect_uri=#{CGI.escape @callback_url}")
317
+ end
413
318
 
414
- it 'raises an OAuthTokenRequestError when empty response body is returned' do
415
- expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(400, '', {}))
416
- expect { @oauth.generate_client_code(KoalaTest.oauth_token) }.to raise_error(Koala::Facebook::OAuthTokenRequestError)
417
- end
319
+ it "can generate a feed dialog" do
320
+ url = @oauth.url_for_dialog("feed", :name => "People Argue Just to Win", :link => "http://www.nytimes.com/2011/06/15/arts/people-argue-just-to-win-scholars-assert.html")
321
+ expect(url).to match_url("https://www.facebook.com/dialog/feed?app_id=#{@app_id}&client_id=#{@app_id}&link=http%3A%2F%2Fwww.nytimes.com%2F2011%2F06%2F15%2Farts%2Fpeople-argue-just-to-win-scholars-assert.html&name=People+Argue+Just+to+Win&redirect_uri=#{CGI.escape @callback_url}")
322
+ end
418
323
 
419
- it 'raises a ServerError when empty response body is returned' do
420
- expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(500, '', {}))
421
- expect { @oauth.generate_client_code(KoalaTest.oauth_token) }.to raise_error(Koala::Facebook::ServerError)
422
- end
324
+ it "can generate a oauth dialog" do
325
+ url = @oauth.url_for_dialog("oauth", :scope => "email", :response_type => "token")
326
+ expect(url).to match_url("https://www.facebook.com/dialog/oauth?app_id=#{@app_id}&client_id=#{@app_id}&redirect_uri=#{CGI.escape @callback_url}&response_type=token&scope=email")
327
+ end
423
328
 
424
- it 'raises a KoalaError when empty response body is returned' do
425
- expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(200, '{"client_code":"should_not_be_returned"}', {}))
426
- expect { @oauth.generate_client_code(KoalaTest.oauth_token) }.to raise_error(Koala::KoalaError)
329
+ it "can generate a pay dialog" do
330
+ url = @oauth.url_for_dialog("pay", :order_id => "foo", :credits_purchase => false)
331
+ expect(url).to match_url("https://www.facebook.com/dialog/pay?app_id=#{@app_id}&client_id=#{@app_id}&order_id=foo&credits_purchase=false&redirect_uri=#{CGI.escape @callback_url}")
332
+ end
427
333
  end
428
- else
429
- pending "Some OAuth token exchange tests will not be run since the access token field in facebook_data.yml is blank."
430
334
  end
431
335
  end
432
- end
433
-
434
- describe "for fetching access tokens" do
435
- describe "#get_access_token_info" do
436
- it "uses options[:redirect_uri] if provided" do
437
- uri = "foo"
438
- expect(Koala).to receive(:make_request).with(anything, hash_including(:redirect_uri => uri), anything, anything).and_return(Koala::HTTPService::Response.new(200, "", {}))
439
- @oauth.get_access_token_info(@code, :redirect_uri => uri)
440
- end
441
-
442
- it "uses the redirect_uri used to create the @oauth if no :redirect_uri option is provided" do
443
- expect(Koala).to receive(:make_request).with(anything, hash_including(:redirect_uri => @callback_url), anything, anything).and_return(Koala::HTTPService::Response.new(200, "", {}))
444
- @oauth.get_access_token_info(@code)
445
- end
446
336
 
447
- it "makes a GET request" do
448
- expect(Koala).to receive(:make_request).with(anything, anything, "get", anything).and_return(Koala::HTTPService::Response.new(200, "", {}))
449
- @oauth.get_access_token_info(@code)
450
- end
337
+ describe 'for generating a client code' do
338
+ describe '#generate_client_code' do
339
+ if KoalaTest.mock_interface? || KoalaTest.oauth_token
340
+ it 'makes a request using the correct endpoint' do
341
+ expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(200, '{"code": "fake_client_code"}', {}))
342
+ @oauth.generate_client_code(KoalaTest.oauth_token)
343
+ end
451
344
 
452
- it "properly decodes JSON results" do
453
- result = {
454
- "access_token" => "foo",
455
- "expires_in" => "baz",
456
- "machine_id" => "bar"
457
- }
458
- allow(Koala).to receive(:make_request).and_return(
459
- Koala::HTTPService::Response.new(
460
- 200,
461
- MultiJson.dump(result),
462
- {}
463
- )
464
- )
465
- expect(@oauth.get_access_token_info(@code)).to eq(result)
466
- end
345
+ it 'gets a valid client code returned' do
346
+ expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(200, '{"code": "fake_client_code"}', {}))
347
+ result = @oauth.generate_client_code(KoalaTest.oauth_token)
348
+ expect(result).to be_a(String)
349
+ expect(result).to eq('fake_client_code')
350
+ end
467
351
 
468
- it "falls back to URL-style parsing " do
469
- result = "access_token=foo&expires_in=baz&machine_id=bar"
470
- allow(Koala).to receive(:make_request).and_return(
471
- Koala::HTTPService::Response.new(200, result, {})
472
- )
473
- expect(@oauth.get_access_token_info(@code)).to eq({
474
- "access_token" => "foo",
475
- "expires_in" => "baz",
476
- "machine_id" => "bar"
477
- })
478
- end
352
+ it 'raises a BadFacebookResponse error when empty response body is returned' do
353
+ expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(200, '', {}))
354
+ expect { @oauth.generate_client_code(KoalaTest.oauth_token) }.to raise_error(Koala::Facebook::BadFacebookResponse)
355
+ end
479
356
 
480
- if KoalaTest.code
481
- it "properly gets and parses an access token token results into a hash" do
482
- result = @oauth.get_access_token_info(@code)
483
- expect(result).to be_a(Hash)
484
- end
357
+ it 'raises an OAuthTokenRequestError when empty response body is returned' do
358
+ expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(400, '', {}))
359
+ expect { @oauth.generate_client_code(KoalaTest.oauth_token) }.to raise_error(Koala::Facebook::OAuthTokenRequestError)
360
+ end
485
361
 
486
- it "properly includes the access token results" do
487
- result = @oauth.get_access_token_info(@code)
488
- expect(result["access_token"]).to be_truthy
489
- end
362
+ it 'raises a ServerError when empty response body is returned' do
363
+ expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(500, '', {}))
364
+ expect { @oauth.generate_client_code(KoalaTest.oauth_token) }.to raise_error(Koala::Facebook::ServerError)
365
+ end
490
366
 
491
- it "raises an error when get_access_token is called with a bad code" do
492
- expect { @oauth.get_access_token_info("foo") }.to raise_error(Koala::Facebook::OAuthTokenRequestError)
367
+ it 'raises a KoalaError when empty response body is returned' do
368
+ expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(200, '{"client_code":"should_not_be_returned"}', {}))
369
+ expect { @oauth.generate_client_code(KoalaTest.oauth_token) }.to raise_error(Koala::KoalaError)
370
+ end
371
+ else
372
+ pending "Some OAuth token exchange tests will not be run since the access token field in facebook_data.yml is blank."
493
373
  end
494
374
  end
495
375
  end
496
376
 
497
- describe "#get_access_token" do
498
- # TODO refactor these to be proper tests with stubs and tests against real data
499
- it "passes on any options provided to make_request" do
500
- options = {:a => 2}
501
- expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
502
- @oauth.get_access_token(@code, options)
503
- end
504
-
505
- if KoalaTest.code
506
- it "uses get_access_token_info to get and parse an access token token results" do
507
- result = @oauth.get_access_token(@code)
508
- expect(result).to be_a(String)
377
+ describe "for fetching access tokens" do
378
+ describe "#get_access_token_info" do
379
+ it "uses options[:redirect_uri] if provided" do
380
+ uri = "foo"
381
+ expect(Koala).to receive(:make_request).with(anything, hash_including(:redirect_uri => uri), anything, anything).and_return(Koala::HTTPService::Response.new(200, "", {}))
382
+ @oauth.get_access_token_info(@code, :redirect_uri => uri)
509
383
  end
510
384
 
511
- it "returns the access token as a string" do
512
- result = @oauth.get_access_token(@code)
513
- original = @oauth.get_access_token_info(@code)
514
- expect(result).to eq(original["access_token"])
385
+ it "uses the redirect_uri used to create the @oauth if no :redirect_uri option is provided" do
386
+ expect(Koala).to receive(:make_request).with(anything, hash_including(:redirect_uri => @callback_url), anything, anything).and_return(Koala::HTTPService::Response.new(200, "", {}))
387
+ @oauth.get_access_token_info(@code)
515
388
  end
516
389
 
517
- it "raises an error when get_access_token is called with a bad code" do
518
- expect { @oauth.get_access_token("foo") }.to raise_error(Koala::Facebook::OAuthTokenRequestError)
390
+ it "makes a GET request" do
391
+ expect(Koala).to receive(:make_request).with(anything, anything, "get", anything).and_return(Koala::HTTPService::Response.new(200, "", {}))
392
+ @oauth.get_access_token_info(@code)
519
393
  end
520
- end
521
- end
522
-
523
- unless KoalaTest.code
524
- it "Some OAuth code tests will not be run since the code field in facebook_data.yml is blank."
525
- end
526
-
527
- describe "get_app_access_token_info" do
528
- it "properly gets and parses an app's access token as a hash" do
529
- result = @oauth.get_app_access_token_info
530
- expect(result).to be_a(Hash)
531
- end
532
394
 
533
- it "includes the access token" do
534
- result = @oauth.get_app_access_token_info
535
- expect(result["access_token"]).to be_truthy
536
- end
537
-
538
- it "passes on any options provided to make_request" do
539
- options = {:a => 2}
540
- expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
541
- @oauth.get_app_access_token_info(options)
542
- end
543
- end
544
-
545
- describe "get_app_access_token" do
546
- it "uses get_access_token_info to get and parse an access token token results" do
547
- result = @oauth.get_app_access_token
548
- expect(result).to be_a(String)
549
- end
550
-
551
- it "returns the access token as a string" do
552
- result = @oauth.get_app_access_token
553
- original = @oauth.get_app_access_token_info
554
- expect(result).to eq(original["access_token"])
555
- end
556
-
557
- it "passes on any options provided to make_request" do
558
- options = {:a => 2}
559
- expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
560
- @oauth.get_app_access_token(options)
561
- end
562
- end
563
-
564
- describe "exchange_access_token_info" do
565
- if KoalaTest.mock_interface? || KoalaTest.oauth_token
566
- it "properly gets and parses an app's access token as a hash" do
567
- result = @oauth.exchange_access_token_info(KoalaTest.oauth_token)
568
- expect(result).to be_a(Hash)
395
+ it "properly decodes JSON results" do
396
+ result = {
397
+ "access_token" => "foo",
398
+ "expires_in" => "baz",
399
+ "machine_id" => "bar"
400
+ }
401
+ allow(Koala).to receive(:make_request).and_return(
402
+ Koala::HTTPService::Response.new(
403
+ 200,
404
+ MultiJson.dump(result),
405
+ {}
406
+ )
407
+ )
408
+ expect(@oauth.get_access_token_info(@code)).to eq(result)
569
409
  end
570
410
 
571
- it "includes the access token" do
572
- result = @oauth.exchange_access_token_info(KoalaTest.oauth_token)
573
- expect(result["access_token"]).not_to be_nil
411
+ it "falls back to URL-style parsing " do
412
+ result = "access_token=foo&expires_in=baz&machine_id=bar"
413
+ allow(Koala).to receive(:make_request).and_return(
414
+ Koala::HTTPService::Response.new(200, result, {})
415
+ )
416
+ expect(@oauth.get_access_token_info(@code)).to eq({
417
+ "access_token" => "foo",
418
+ "expires_in" => "baz",
419
+ "machine_id" => "bar"
420
+ })
574
421
  end
575
- else
576
- pending "Some OAuth token exchange tests will not be run since the access token field in facebook_data.yml is blank."
577
- end
578
-
579
- it "passes on any options provided to make_request" do
580
- options = {:a => 2}
581
- expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
582
- @oauth.exchange_access_token_info(KoalaTest.oauth_token, options)
583
- end
584
422
 
585
- it "raises an error when exchange_access_token_info is called with a bad code" do
586
- expect { @oauth.exchange_access_token_info("foo") }.to raise_error(Koala::Facebook::OAuthTokenRequestError)
587
- end
588
- end
423
+ if KoalaTest.code
424
+ it "properly gets and parses an access token token results into a hash" do
425
+ result = @oauth.get_access_token_info(@code)
426
+ expect(result).to be_a(Hash)
427
+ end
589
428
 
590
- describe "exchange_access_token" do
591
- it "uses get_access_token_info to get and parse an access token token results" do
592
- hash = {"access_token" => Time.now.to_i * rand}
593
- allow(@oauth).to receive(:exchange_access_token_info).and_return(hash)
594
- expect(@oauth.exchange_access_token(KoalaTest.oauth_token)).to eq(hash["access_token"])
595
- end
429
+ it "properly includes the access token results" do
430
+ result = @oauth.get_access_token_info(@code)
431
+ expect(result["access_token"]).to be_truthy
432
+ end
596
433
 
597
- it "passes on any options provided to make_request" do
598
- options = {:a => 2}
599
- expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
600
- @oauth.exchange_access_token(KoalaTest.oauth_token, options)
434
+ it "raises an error when get_access_token is called with a bad code" do
435
+ expect { @oauth.get_access_token_info("foo") }.to raise_error(Koala::Facebook::OAuthTokenRequestError)
436
+ end
437
+ end
601
438
  end
602
- end
603
439
 
604
- describe "protected methods" do
605
- # protected methods
606
- # since these are pretty fundamental and pretty testable, we want to test them
440
+ describe "#get_access_token" do
441
+ # TODO refactor these to be proper tests with stubs and tests against real data
442
+ it "passes on any options provided to make_request" do
443
+ options = {:a => 2}
444
+ expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
445
+ @oauth.get_access_token(@code, options)
446
+ end
607
447
 
608
- # parse_access_token
609
- it "properly parses access token results" do
610
- result = @oauth.send(:parse_access_token, @raw_token_string)
611
- has_both_parts = result["access_token"] && result["expires"]
612
- expect(has_both_parts).to be_truthy
613
- end
448
+ if KoalaTest.code
449
+ it "uses get_access_token_info to get and parse an access token token results" do
450
+ result = @oauth.get_access_token(@code)
451
+ expect(result).to be_a(String)
452
+ end
614
453
 
615
- it "properly parses offline access token results" do
616
- result = @oauth.send(:parse_access_token, @raw_offline_access_token_string)
617
- has_both_parts = result["access_token"] && !result["expires"]
618
- expect(has_both_parts).to be true
619
- end
454
+ it "returns the access token as a string" do
455
+ result = @oauth.get_access_token(@code)
456
+ original = @oauth.get_access_token_info(@code)
457
+ expect(result).to eq(original["access_token"])
458
+ end
620
459
 
621
- # fetch_token_string
622
- # somewhat duplicative with the tests for get_access_token and get_app_access_token
623
- # but no harm in thoroughness
624
- if KoalaTest.code
625
- it "fetches a proper token string from Facebook when given a code" do
626
- result = @oauth.send(:fetch_token_string, :code => @code, :redirect_uri => @callback_url)
627
- expect(result).to match(/^access_token/)
460
+ it "raises an error when get_access_token is called with a bad code" do
461
+ expect { @oauth.get_access_token("foo") }.to raise_error(Koala::Facebook::OAuthTokenRequestError)
462
+ end
628
463
  end
629
- else
630
- it "fetch_token_string code test will not be run since the code field in facebook_data.yml is blank."
631
464
  end
632
465
 
633
- it "fetches a proper token string from Facebook when asked for the app token" do
634
- result = @oauth.send(:fetch_token_string, {:grant_type => 'client_credentials'}, true)
635
- expect(result).to match(/^access_token/)
466
+ unless KoalaTest.code
467
+ it "Some OAuth code tests will not be run since the code field in facebook_data.yml is blank."
636
468
  end
637
- end
638
- end
639
-
640
- describe "for exchanging session keys" do
641
- if KoalaTest.session_key
642
- describe "with get_token_info_from_session_keys" do
643
- it "gets an array of session keys from Facebook when passed a single key" do
644
- result = @oauth.get_tokens_from_session_keys([KoalaTest.session_key])
645
- expect(result).to be_an(Array)
646
- expect(result.length).to eq(1)
647
- end
648
469
 
649
- it "gets an array of session keys from Facebook when passed multiple keys" do
650
- result = @oauth.get_tokens_from_session_keys(@multiple_session_keys)
651
- expect(result).to be_an(Array)
652
- expect(result.length).to eq(2)
470
+ describe "get_app_access_token_info" do
471
+ it "properly gets and parses an app's access token as a hash" do
472
+ result = @oauth.get_app_access_token_info
473
+ expect(result).to be_a(Hash)
653
474
  end
654
475
 
655
- it "returns the original hashes" do
656
- result = @oauth.get_token_info_from_session_keys(@multiple_session_keys)
657
- expect(result[0]).to be_a(Hash)
476
+ it "includes the access token" do
477
+ result = @oauth.get_app_access_token_info
478
+ expect(result["access_token"]).to be_truthy
658
479
  end
659
480
 
660
- it "properly handles invalid session keys" do
661
- result = @oauth.get_token_info_from_session_keys(["foo", "bar"])
662
- #it should return nil for each of the invalid ones
663
- result.each {|r| expect(r).to be_nil}
481
+ it "passes on any options provided to make_request" do
482
+ options = {:a => 2}
483
+ expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
484
+ @oauth.get_app_access_token_info(options)
664
485
  end
486
+ end
665
487
 
666
- it "properly handles a mix of valid and invalid session keys" do
667
- result = @oauth.get_token_info_from_session_keys(["foo"].concat(@multiple_session_keys))
668
- # it should return nil for each of the invalid ones
669
- result.each_with_index {|r, index| index > 0 ? expect(r).to(be_a(Hash)) : expect(r).to(be_nil)}
488
+ describe "get_app_access_token" do
489
+ it "uses get_access_token_info to get and parse an access token token results" do
490
+ result = @oauth.get_app_access_token
491
+ expect(result).to be_a(String)
670
492
  end
671
493
 
672
- it "throws a BadFacebookResponse if Facebook returns an empty body (as happens for instance when the API breaks)" do
673
- expect(@oauth).to receive(:fetch_token_string).and_return("")
674
- expect { @oauth.get_token_info_from_session_keys(@multiple_session_keys) }.to raise_error(Koala::Facebook::BadFacebookResponse)
494
+ it "returns the access token as a string" do
495
+ result = @oauth.get_app_access_token
496
+ original = @oauth.get_app_access_token_info
497
+ expect(result).to eq(original["access_token"])
675
498
  end
676
499
 
677
500
  it "passes on any options provided to make_request" do
678
501
  options = {:a => 2}
679
- expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "[{}]", {}))
680
- @oauth.get_token_info_from_session_keys([], options)
502
+ expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
503
+ @oauth.get_app_access_token(options)
681
504
  end
682
505
  end
683
506
 
684
- describe "with get_tokens_from_session_keys" do
685
- it "calls get_token_info_from_session_keys" do
686
- args = @multiple_session_keys
687
- expect(@oauth).to receive(:get_token_info_from_session_keys).with(args, anything).and_return([])
688
- @oauth.get_tokens_from_session_keys(args)
507
+ describe "exchange_access_token_info" do
508
+ if KoalaTest.mock_interface? || KoalaTest.oauth_token
509
+ it "properly gets and parses an app's access token as a hash" do
510
+ result = @oauth.exchange_access_token_info(KoalaTest.oauth_token)
511
+ expect(result).to be_a(Hash)
512
+ end
513
+
514
+ it "includes the access token" do
515
+ result = @oauth.exchange_access_token_info(KoalaTest.oauth_token)
516
+ expect(result["access_token"]).not_to be_nil
517
+ end
518
+ else
519
+ pending "Some OAuth token exchange tests will not be run since the access token field in facebook_data.yml is blank."
689
520
  end
690
521
 
691
- it "returns an array of strings" do
692
- args = @multiple_session_keys
693
- result = @oauth.get_tokens_from_session_keys(args)
694
- result.each {|r| expect(r).to be_a(String) }
522
+ it "passes on any options provided to make_request" do
523
+ options = {:a => 2}
524
+ expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
525
+ @oauth.exchange_access_token_info(KoalaTest.oauth_token, options)
695
526
  end
696
527
 
697
- it "properly handles invalid session keys" do
698
- result = @oauth.get_tokens_from_session_keys(["foo", "bar"])
699
- # it should return nil for each of the invalid ones
700
- result.each {|r| expect(r).to be_nil}
528
+ it "raises an error when exchange_access_token_info is called with a bad code" do
529
+ expect { @oauth.exchange_access_token_info("foo") }.to raise_error(Koala::Facebook::OAuthTokenRequestError)
701
530
  end
531
+ end
702
532
 
703
- it "properly handles a mix of valid and invalid session keys" do
704
- result = @oauth.get_tokens_from_session_keys(["foo"].concat(@multiple_session_keys))
705
- # it should return nil for each of the invalid ones
706
- result.each_with_index {|r, index| index > 0 ? expect(r).to(be_a(String)) : expect(r).to(be_nil)}
533
+ describe "exchange_access_token" do
534
+ it "uses get_access_token_info to get and parse an access token token results" do
535
+ hash = {"access_token" => Time.now.to_i * rand}
536
+ allow(@oauth).to receive(:exchange_access_token_info).and_return(hash)
537
+ expect(@oauth.exchange_access_token(KoalaTest.oauth_token)).to eq(hash["access_token"])
707
538
  end
708
539
 
709
540
  it "passes on any options provided to make_request" do
710
541
  options = {:a => 2}
711
- expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "[{}]", {}))
712
- @oauth.get_tokens_from_session_keys([], options)
542
+ expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
543
+ @oauth.exchange_access_token(KoalaTest.oauth_token, options)
713
544
  end
714
545
  end
715
546
 
716
- describe "get_token_from_session_key" do
717
- it "calls get_tokens_from_session_keys when the get_token_from_session_key is called" do
718
- key = KoalaTest.session_key
719
- expect(@oauth).to receive(:get_tokens_from_session_keys).with([key], anything).and_return([])
720
- @oauth.get_token_from_session_key(key)
721
- end
547
+ describe "protected methods" do
548
+ # protected methods
549
+ # since these are pretty fundamental and pretty testable, we want to test them
722
550
 
723
- it "gets back the access token string from get_token_from_session_key" do
724
- result = @oauth.get_token_from_session_key(KoalaTest.session_key)
725
- expect(result).to be_a(String)
551
+ # parse_access_token
552
+ it "properly parses access token results" do
553
+ result = @oauth.send(:parse_access_token, @raw_token_string)
554
+ has_both_parts = result["access_token"] && result["expires"]
555
+ expect(has_both_parts).to be_truthy
726
556
  end
727
557
 
728
- it "returns the first value in the array" do
729
- result = @oauth.get_token_from_session_key(KoalaTest.session_key)
730
- array = @oauth.get_tokens_from_session_keys([KoalaTest.session_key])
731
- expect(result).to eq(array[0])
558
+ it "properly parses offline access token results" do
559
+ result = @oauth.send(:parse_access_token, @raw_offline_access_token_string)
560
+ has_both_parts = result["access_token"] && !result["expires"]
561
+ expect(has_both_parts).to be true
732
562
  end
733
563
 
734
- it "properly handles an invalid session key" do
735
- result = @oauth.get_token_from_session_key("foo")
736
- expect(result).to be_nil
564
+ # fetch_token_string
565
+ # somewhat duplicative with the tests for get_access_token and get_app_access_token
566
+ # but no harm in thoroughness
567
+ if KoalaTest.code
568
+ it "fetches a proper token string from Facebook when given a code" do
569
+ result = @oauth.send(:fetch_token_string, :code => @code, :redirect_uri => @callback_url)
570
+ expect(result).to match(/^access_token/)
571
+ end
572
+ else
573
+ it "fetch_token_string code test will not be run since the code field in facebook_data.yml is blank."
737
574
  end
738
575
 
739
- it "passes on any options provided to make_request" do
740
- options = {:a => 2}
741
- expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "[{}]", {}))
742
- @oauth.get_token_from_session_key("", options)
576
+ it "fetches a proper token string from Facebook when asked for the app token" do
577
+ result = @oauth.send(:fetch_token_string, {:grant_type => 'client_credentials'}, true)
578
+ expect(result).to match(/^access_token/)
743
579
  end
744
580
  end
745
- else
746
- it "Session key exchange tests will not be run since the session key in facebook_data.yml is blank."
747
581
  end
748
- end
749
582
 
750
- describe "for parsing signed requests" do
751
- # the signed request code is ported directly from Facebook
752
- # so we only need to test at a high level that it works
753
- it "throws an error if the algorithm is unsupported" do
754
- allow(MultiJson).to receive(:load).and_return("algorithm" => "my fun algorithm")
755
- expect { @oauth.parse_signed_request(@signed_request) }.to raise_error
756
- end
583
+ describe "for parsing signed requests" do
584
+ # the signed request code is ported directly from Facebook
585
+ # so we only need to test at a high level that it works
586
+ it "throws an error if the algorithm is unsupported" do
587
+ allow(MultiJson).to receive(:load).and_return("algorithm" => "my fun algorithm")
588
+ expect { @oauth.parse_signed_request(@signed_request) }.to raise_error
589
+ end
757
590
 
758
- it "throws an error if the signature is invalid" do
759
- allow(OpenSSL::HMAC).to receive(:hexdigest).and_return("i'm an invalid signature")
760
- expect { @oauth.parse_signed_request(@signed_request) }.to raise_error
761
- end
591
+ it "throws an error if the signature is invalid" do
592
+ allow(OpenSSL::HMAC).to receive(:hexdigest).and_return("i'm an invalid signature")
593
+ expect { @oauth.parse_signed_request(@signed_request) }.to raise_error
594
+ end
762
595
 
763
- it "throws an error if the signature string is empty" do
764
- # this occasionally happens due to Facebook error
765
- expect { @oauth.parse_signed_request("") }.to raise_error
766
- expect { @oauth.parse_signed_request("abc-def") }.to raise_error
767
- end
596
+ it "throws an error if the signature string is empty" do
597
+ # this occasionally happens due to Facebook error
598
+ expect { @oauth.parse_signed_request("") }.to raise_error
599
+ expect { @oauth.parse_signed_request("abc-def") }.to raise_error
600
+ end
768
601
 
769
- it "properly parses requests" do
770
- @oauth = Koala::Facebook::OAuth.new(@app_id, @secret || @app_secret)
771
- expect(@oauth.parse_signed_request(@signed_params)).to eq(@signed_params_result)
602
+ it "properly parses requests" do
603
+ @oauth = Koala::Facebook::OAuth.new(@app_id, @secret || @app_secret)
604
+ expect(@oauth.parse_signed_request(@signed_params)).to eq(@signed_params_result)
605
+ end
772
606
  end
773
607
  end
774
-
775
608
  end # describe