koala 1.0.0.beta → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. data/.autotest +12 -0
  2. data/.gitignore +5 -0
  3. data/.travis.yml +8 -0
  4. data/CHANGELOG +42 -4
  5. data/Gemfile +7 -0
  6. data/LICENSE +1 -1
  7. data/Manifest +5 -2
  8. data/Rakefile +13 -14
  9. data/autotest/discover.rb +1 -0
  10. data/koala.gemspec +35 -20
  11. data/lib/koala/batch_operation.rb +74 -0
  12. data/lib/koala/graph_api.rb +196 -143
  13. data/lib/koala/graph_batch_api.rb +87 -0
  14. data/lib/koala/graph_collection.rb +54 -0
  15. data/lib/koala/http_services/net_http_service.rb +92 -0
  16. data/lib/koala/http_services/typhoeus_service.rb +37 -0
  17. data/lib/koala/http_services.rb +15 -124
  18. data/lib/koala/oauth.rb +181 -0
  19. data/lib/koala/realtime_updates.rb +5 -14
  20. data/lib/koala/rest_api.rb +13 -8
  21. data/lib/koala/test_users.rb +21 -8
  22. data/lib/koala/uploadable_io.rb +175 -0
  23. data/lib/koala.rb +48 -240
  24. data/readme.md +60 -28
  25. data/spec/cases/api_base_spec.rb +101 -0
  26. data/spec/cases/graph_and_rest_api_spec.rb +31 -0
  27. data/spec/cases/graph_api_batch_spec.rb +609 -0
  28. data/spec/cases/graph_api_spec.rb +25 -0
  29. data/spec/cases/http_services/http_service_spec.rb +129 -0
  30. data/spec/cases/http_services/net_http_service_spec.rb +532 -0
  31. data/spec/cases/http_services/typhoeus_service_spec.rb +152 -0
  32. data/spec/cases/koala_spec.rb +55 -0
  33. data/spec/cases/oauth_spec.rb +409 -0
  34. data/spec/cases/realtime_updates_spec.rb +184 -0
  35. data/spec/cases/rest_api_spec.rb +25 -0
  36. data/spec/{koala/test_users/test_users_tests.rb → cases/test_users_spec.rb} +47 -34
  37. data/spec/cases/uploadable_io_spec.rb +193 -0
  38. data/spec/fixtures/cat.m4v +0 -0
  39. data/spec/{facebook_data.yml → fixtures/facebook_data.yml} +12 -14
  40. data/spec/{mock_facebook_responses.yml → fixtures/mock_facebook_responses.yml} +408 -306
  41. data/spec/spec_helper.rb +19 -0
  42. data/spec/support/graph_api_shared_examples.rb +495 -0
  43. data/spec/support/json_testing_fix.rb +18 -0
  44. data/spec/{koala → support}/live_testing_data_helper.rb +39 -42
  45. data/spec/support/mock_http_service.rb +96 -0
  46. data/spec/support/rest_api_shared_examples.rb +285 -0
  47. data/spec/support/setup_mocks_or_live.rb +51 -0
  48. data/spec/support/uploadable_io_shared_examples.rb +76 -0
  49. metadata +110 -64
  50. data/init.rb +0 -2
  51. data/spec/koala/api_base_tests.rb +0 -102
  52. data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +0 -14
  53. data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +0 -16
  54. data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +0 -63
  55. data/spec/koala/graph_api/graph_api_tests.rb +0 -86
  56. data/spec/koala/graph_api/graph_api_with_access_token_tests.rb +0 -154
  57. data/spec/koala/graph_api/graph_collection_tests.rb +0 -104
  58. data/spec/koala/net_http_service_tests.rb +0 -430
  59. data/spec/koala/oauth/oauth_tests.rb +0 -409
  60. data/spec/koala/realtime_updates/realtime_updates_tests.rb +0 -187
  61. data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +0 -25
  62. data/spec/koala/rest_api/rest_api_tests.rb +0 -118
  63. data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +0 -38
  64. data/spec/koala/typhoeus_service_tests.rb +0 -156
  65. data/spec/koala_spec.rb +0 -18
  66. data/spec/koala_spec_helper.rb +0 -70
  67. data/spec/koala_spec_without_mocks.rb +0 -19
  68. data/spec/mock_http_service.rb +0 -96
  69. /data/spec/{koala/assets → fixtures}/beach.jpg +0 -0
@@ -1,409 +0,0 @@
1
- class FacebookOAuthTests < Test::Unit::TestCase
2
- describe "Koala OAuth tests" do
3
- before :each do
4
- # make the relevant test data easily accessible
5
- @oauth_data = $testing_data["oauth_test_data"]
6
- @app_id = @oauth_data["app_id"]
7
- @secret = @oauth_data["secret"]
8
- @code = @oauth_data["code"]
9
- @callback_url = @oauth_data["callback_url"]
10
- @raw_token_string = @oauth_data["raw_token_string"]
11
- @raw_offline_access_token_string = @oauth_data["raw_offline_access_token_string"]
12
-
13
- # per Facebook's example:
14
- # http://developers.facebook.com/docs/authentication/canvas
15
- # this allows us to use Facebook's example data while running the other live data
16
- @request_secret = @oauth_data["request_secret"] || @secret
17
- @signed_request = @oauth_data["signed_request"]
18
- @signed_request_result = @oauth_data["signed_request_result"]
19
- # for signed requests (http://developers.facebook.com/docs/authentication/canvas/encryption_proposal)
20
- @signed_params_secret = @oauth_data["signed_params_secret"] || @secret
21
- @signed_params = @oauth_data["signed_params"]
22
- @signed_params_result = @oauth_data["signed_params_result"]
23
-
24
- # this should expanded to cover all variables
25
- raise Exception, "Must supply app data to run FacebookOAuthTests!" unless @app_id && @secret && @callback_url &&
26
- @code && @raw_token_string &&
27
- @raw_offline_access_token_string
28
-
29
- @oauth = Koala::Facebook::OAuth.new(@app_id, @secret, @callback_url)
30
-
31
- @time = Time.now
32
- Time.stub!(:now).and_return(@time)
33
- @time.stub!(:to_i).and_return(1273363199)
34
- end
35
-
36
- # initialization
37
- it "should properly initialize" do
38
- @oauth.should
39
- end
40
-
41
- it "should properly set attributes" do
42
- (@oauth.app_id == @app_id &&
43
- @oauth.app_secret == @secret &&
44
- @oauth.oauth_callback_url == @callback_url).should be_true
45
- end
46
-
47
- it "should properly initialize without a callback_url" do
48
- @oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
49
- end
50
-
51
- it "should properly set attributes without a callback URL" do
52
- @oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
53
- (@oauth.app_id == @app_id &&
54
- @oauth.app_secret == @secret &&
55
- @oauth.oauth_callback_url == nil).should be_true
56
- end
57
-
58
- describe "for cookie parsing" do
59
- describe "get_user_info_from_cookies" do
60
- it "should properly parse valid cookies" do
61
- result = @oauth.get_user_info_from_cookies(@oauth_data["valid_cookies"])
62
- result.should be_a(Hash)
63
- end
64
-
65
- it "should return all the cookie components from valid cookie string" do
66
- cookie_data = @oauth_data["valid_cookies"]
67
- parsing_results = @oauth.get_user_info_from_cookies(cookie_data)
68
- number_of_components = cookie_data["fbs_#{@app_id.to_s}"].scan(/\=/).length
69
- parsing_results.length.should == number_of_components
70
- end
71
-
72
- it "should properly parse valid offline access cookies (e.g. no expiration)" do
73
- result = @oauth.get_user_info_from_cookies(@oauth_data["offline_access_cookies"])
74
- result["uid"].should
75
- end
76
-
77
- it "should return all the cookie components from offline access cookies" do
78
- cookie_data = @oauth_data["offline_access_cookies"]
79
- parsing_results = @oauth.get_user_info_from_cookies(cookie_data)
80
- number_of_components = cookie_data["fbs_#{@app_id.to_s}"].scan(/\=/).length
81
- parsing_results.length.should == number_of_components
82
- end
83
-
84
- it "shouldn't parse expired cookies" do
85
- result = @oauth.get_user_info_from_cookies(@oauth_data["expired_cookies"])
86
- result.should be_nil
87
- end
88
-
89
- it "shouldn't parse invalid cookies" do
90
- # make an invalid string by replacing some values
91
- bad_cookie_hash = @oauth_data["valid_cookies"].inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
92
- result = @oauth.get_user_info_from_cookies(bad_cookie_hash)
93
- result.should be_nil
94
- end
95
- end
96
-
97
- describe "get_user_from_cookies" do
98
- it "should use get_user_info_from_cookies to parse the cookies" do
99
- data = @oauth_data["valid_cookies"]
100
- @oauth.should_receive(:get_user_info_from_cookies).with(data).and_return({})
101
- @oauth.get_user_from_cookies(data)
102
- end
103
-
104
- it "should use return a string if the cookies are valid" do
105
- result = @oauth.get_user_from_cookies(@oauth_data["valid_cookies"])
106
- result.should be_a(String)
107
- end
108
-
109
- it "should return nil if the cookies are invalid" do
110
- # make an invalid string by replacing some values
111
- bad_cookie_hash = @oauth_data["valid_cookies"].inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
112
- result = @oauth.get_user_from_cookies(bad_cookie_hash)
113
- result.should be_nil
114
- end
115
- end
116
- end
117
-
118
- # OAuth URLs
119
-
120
- describe "for URL generation" do
121
-
122
- describe "for OAuth codes" do
123
- # url_for_oauth_code
124
- it "should generate a properly formatted OAuth code URL with the default values" do
125
- url = @oauth.url_for_oauth_code
126
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}"
127
- end
128
-
129
- it "should generate a properly formatted OAuth code URL when a callback is given" do
130
- callback = "foo.com"
131
- url = @oauth.url_for_oauth_code(:callback => callback)
132
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{callback}"
133
- end
134
-
135
- it "should generate a properly formatted OAuth code URL when permissions are requested as a string" do
136
- permissions = "publish_stream,read_stream"
137
- url = @oauth.url_for_oauth_code(:permissions => permissions)
138
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}&scope=#{permissions}"
139
- end
140
-
141
- it "should generate a properly formatted OAuth code URL when permissions are requested as a string" do
142
- permissions = ["publish_stream", "read_stream"]
143
- url = @oauth.url_for_oauth_code(:permissions => permissions)
144
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}&scope=#{permissions.join(",")}"
145
- end
146
-
147
- it "should generate a properly formatted OAuth code URL when both permissions and callback are provided" do
148
- permissions = "publish_stream,read_stream"
149
- callback = "foo.com"
150
- url = @oauth.url_for_oauth_code(:callback => callback, :permissions => permissions)
151
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{callback}&scope=#{permissions}"
152
- end
153
-
154
- it "should raise an exception if no callback is given in initialization or the call" do
155
- oauth2 = Koala::Facebook::OAuth.new(@app_id, @secret)
156
- lambda { oauth2.url_for_oauth_code }.should raise_error(ArgumentError)
157
- end
158
- end
159
-
160
- describe "for access token URLs" do
161
-
162
- # url_for_access_token
163
- it "should generate a properly formatted OAuth token URL when provided a code" do
164
- url = @oauth.url_for_access_token(@code)
165
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{@callback_url}&client_secret=#{@secret}&code=#{@code}"
166
- end
167
-
168
- it "should generate a properly formatted OAuth token URL when provided a callback" do
169
- callback = "foo.com"
170
- url = @oauth.url_for_access_token(@code, :callback => callback)
171
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{callback}&client_secret=#{@secret}&code=#{@code}"
172
- end
173
- end
174
- end
175
-
176
- describe "for fetching access tokens" do
177
- describe "get_access_token_info" do
178
- it "should properly get and parse an access token token results into a hash" do
179
- result = @oauth.get_access_token_info(@code)
180
- result.should be_a(Hash)
181
- end
182
-
183
- it "should properly include the access token results" do
184
- result = @oauth.get_access_token_info(@code)
185
- result["access_token"].should
186
- end
187
-
188
- it "should raise an error when get_access_token is called with a bad code" do
189
- lambda { @oauth.get_access_token_info("foo") }.should raise_error(Koala::Facebook::APIError)
190
- end
191
- end
192
-
193
- describe "get_access_token" do
194
- it "should use get_access_token_info to get and parse an access token token results" do
195
- result = @oauth.get_access_token(@code)
196
- result.should be_a(String)
197
- end
198
-
199
- it "should return the access token as a string" do
200
- result = @oauth.get_access_token(@code)
201
- original = @oauth.get_access_token_info(@code)
202
- result.should == original["access_token"]
203
- end
204
-
205
- it "should raise an error when get_access_token is called with a bad code" do
206
- lambda { @oauth.get_access_token("foo") }.should raise_error(Koala::Facebook::APIError)
207
- end
208
- end
209
-
210
- describe "get_app_access_token_info" do
211
- it "should properly get and parse an app's access token as a hash" do
212
- result = @oauth.get_app_access_token_info
213
- result.should be_a(Hash)
214
- end
215
-
216
- it "should include the access token" do
217
- result = @oauth.get_app_access_token_info
218
- result["access_token"].should
219
- end
220
- end
221
-
222
- describe "get_app_acess_token" do
223
- it "should use get_access_token_info to get and parse an access token token results" do
224
- result = @oauth.get_app_access_token
225
- result.should be_a(String)
226
- end
227
-
228
- it "should return the access token as a string" do
229
- result = @oauth.get_app_access_token
230
- original = @oauth.get_app_access_token_info
231
- result.should == original["access_token"]
232
- end
233
- end
234
-
235
- describe "protected methods" do
236
-
237
- # protected methods
238
- # since these are pretty fundamental and pretty testable, we want to test them
239
-
240
- # parse_access_token
241
- it "should properly parse access token results" do
242
- result = @oauth.send(:parse_access_token, @raw_token_string)
243
- has_both_parts = result["access_token"] && result["expires"]
244
- has_both_parts.should
245
- end
246
-
247
- it "should properly parse offline access token results" do
248
- result = @oauth.send(:parse_access_token, @raw_offline_access_token_string)
249
- has_both_parts = result["access_token"] && !result["expires"]
250
- has_both_parts.should
251
- end
252
-
253
- # fetch_token_string
254
- # somewhat duplicative with the tests for get_access_token and get_app_access_token
255
- # but no harm in thoroughness
256
- it "should fetch a proper token string from Facebook when given a code" do
257
- result = @oauth.send(:fetch_token_string, :code => @code, :redirect_uri => @callback_url)
258
- result.should =~ /^access_token/
259
- end
260
-
261
- it "should fetch a proper token string from Facebook when asked for the app token" do
262
- result = @oauth.send(:fetch_token_string, {:type => 'client_cred'}, true)
263
- result.should =~ /^access_token/
264
- end
265
- end
266
- end
267
-
268
- describe "for exchanging session keys" do
269
- describe "with get_token_info_from_session_keys" do
270
- it "should get an array of session keys from Facebook when passed a single key" do
271
- result = @oauth.get_tokens_from_session_keys([@oauth_data["session_key"]])
272
- result.should be_an(Array)
273
- result.length.should == 1
274
- end
275
-
276
- it "should get an array of session keys from Facebook when passed multiple keys" do
277
- result = @oauth.get_tokens_from_session_keys(@oauth_data["multiple_session_keys"])
278
- result.should be_an(Array)
279
- result.length.should == 2
280
- end
281
-
282
- it "should return the original hashes" do
283
- result = @oauth.get_token_info_from_session_keys(@oauth_data["multiple_session_keys"])
284
- result[0].should be_a(Hash)
285
- end
286
-
287
- it "should properly handle invalid session keys" do
288
- result = @oauth.get_token_info_from_session_keys(["foo", "bar"])
289
- #it should return nil for each of the invalid ones
290
- result.each {|r| r.should be_nil}
291
- end
292
-
293
- it "should properly handle a mix of valid and invalid session keys" do
294
- result = @oauth.get_token_info_from_session_keys(["foo"].concat(@oauth_data["multiple_session_keys"]))
295
- # it should return nil for each of the invalid ones
296
- result.each_with_index {|r, index| index > 0 ? r.should(be_a(Hash)) : r.should(be_nil)}
297
- end
298
-
299
- it "should throw an APIError if Facebook returns an empty body (as happens for instance when the API breaks)" do
300
- @oauth.should_receive(:fetch_token_string).and_return("")
301
- lambda { @oauth.get_token_info_from_session_keys(@oauth_data["multiple_session_keys"]) }.should raise_error(Koala::Facebook::APIError)
302
- end
303
- end
304
-
305
- describe "with get_tokens_from_session_keys" do
306
- it "should call get_token_info_from_session_keys" do
307
- args = @oauth_data["multiple_session_keys"]
308
- @oauth.should_receive(:get_token_info_from_session_keys).with(args).and_return([])
309
- @oauth.get_tokens_from_session_keys(args)
310
- end
311
-
312
- it "should return an array of strings" do
313
- args = @oauth_data["multiple_session_keys"]
314
- result = @oauth.get_tokens_from_session_keys(args)
315
- result.each {|r| r.should be_a(String) }
316
- end
317
-
318
- it "should properly handle invalid session keys" do
319
- result = @oauth.get_tokens_from_session_keys(["foo", "bar"])
320
- # it should return nil for each of the invalid ones
321
- result.each {|r| r.should be_nil}
322
- end
323
-
324
- it "should properly handle a mix of valid and invalid session keys" do
325
- result = @oauth.get_tokens_from_session_keys(["foo"].concat(@oauth_data["multiple_session_keys"]))
326
- # it should return nil for each of the invalid ones
327
- result.each_with_index {|r, index| index > 0 ? r.should(be_a(String)) : r.should(be_nil)}
328
- end
329
- end
330
-
331
- describe "get_token_from_session_key" do
332
- it "should call get_tokens_from_session_keys when the get_token_from_session_key is called" do
333
- key = @oauth_data["session_key"]
334
- @oauth.should_receive(:get_tokens_from_session_keys).with([key]).and_return([])
335
- @oauth.get_token_from_session_key(key)
336
- end
337
-
338
- it "should get back the access token string from get_token_from_session_key" do
339
- result = @oauth.get_token_from_session_key(@oauth_data["session_key"])
340
- result.should be_a(String)
341
- end
342
-
343
- it "should be the first value in the array" do
344
- result = @oauth.get_token_from_session_key(@oauth_data["session_key"])
345
- array = @oauth.get_tokens_from_session_keys([@oauth_data["session_key"]])
346
- result.should == array[0]
347
- end
348
-
349
- it "should properly handle an invalid session key" do
350
- result = @oauth.get_token_from_session_key("foo")
351
- result.should be_nil
352
- end
353
- end
354
- end
355
-
356
- describe "for parsing signed requests" do
357
- before :each do
358
- # you can test against live data by updating the YML, or you can use the default data
359
- # which tests against Facebook's example on http://developers.facebook.com/docs/authentication/canvas
360
- @oauth = Koala::Facebook::OAuth.new(@app_id, @request_secret || @app_secret)
361
- end
362
-
363
- # the signed request code comes directly from Facebook
364
- # so we only need to test at a high level that it works
365
- # signed params refers to http://developers.facebook.com/docs/authentication/canvas
366
- # signed request refers to http://developers.facebook.com/docs/authentication/canvas/encryption_proposal
367
- it "should throw an error if the algorithm is unsupported" do
368
- JSON.stub!(:parse).and_return("algorithm" => "my fun algorithm")
369
- lambda { @oauth.parse_signed_request(@signed_request) }.should raise_error
370
- end
371
-
372
- it "should throw an error if the signature is invalid" do
373
- OpenSSL::HMAC.stub!(:hexdigest).and_return("i'm an invalid signature")
374
- lambda { @oauth.parse_signed_request(@signed_request) }.should raise_error
375
- end
376
-
377
- describe "for signed params" do
378
- it "should work" do
379
- @oauth.parse_signed_request(@signed_request).should == @signed_request_result
380
- end
381
- end
382
-
383
- describe "for signed requests" do
384
- it "should work" do
385
- @oauth = Koala::Facebook::OAuth.new(@app_id, @signed_params_secret || @app_secret)
386
- @oauth.parse_signed_request(@signed_params).should == @signed_params_result
387
- end
388
-
389
- it "should throw an error if the params are too old" do
390
- @time.stub!(:to_i).and_return(1287601988 + 4000)
391
- @oauth = Koala::Facebook::OAuth.new(@app_id, @signed_params_secret || @app_secret)
392
-
393
- lambda { @oauth.parse_signed_request(@signed_params) }.should raise_error
394
- end
395
-
396
- it "should let you specify the max age for a request" do
397
- @time.stub!(:to_i).and_return(1287601988 + 4000)
398
- @oauth = Koala::Facebook::OAuth.new(@app_id, @signed_params_secret || @app_secret)
399
-
400
- lambda { @oauth.parse_signed_request(@signed_params, 4001) }.should_not raise_error
401
- end
402
-
403
- end
404
-
405
- end
406
-
407
- end # describe
408
-
409
- end #class
@@ -1,187 +0,0 @@
1
- class FacebookRealtimeUpdatesTests < Test::Unit::TestCase
2
- include Koala
3
-
4
- describe "Koala RealtimeUpdates" do
5
- before :all do
6
- # get oauth data
7
- @oauth_data = $testing_data["oauth_test_data"]
8
- @app_id = @oauth_data["app_id"]
9
- @secret = @oauth_data["secret"]
10
- @callback_url = @oauth_data["callback_url"]
11
- @app_access_token = @oauth_data["app_access_token"]
12
-
13
- # check OAuth data
14
- unless @app_id && @secret && @callback_url && @app_access_token
15
- raise Exception, "Must supply OAuth app id, secret, app_access_token, and callback to run live subscription tests!"
16
- end
17
-
18
- # get subscription data
19
- @subscription_data = $testing_data["subscription_test_data"]
20
- @verify_token = @subscription_data["verify_token"]
21
- @challenge_data = @subscription_data["challenge_data"]
22
- @subscription_path = @subscription_data["subscription_path"]
23
-
24
- # check subscription data
25
- unless @verify_token && @challenge_data && @subscription_path
26
- raise Exception, "Must supply verify_token and equivalent challenge_data to run live subscription tests!"
27
- end
28
- end
29
-
30
- describe "when initializing" do
31
- # basic initialization
32
- it "should initialize properly with an app_id and an app_access_token" do
33
- updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
34
- updates.should be_a(Facebook::RealtimeUpdates)
35
- end
36
-
37
- # attributes
38
- it "should allow read access to app_id, app_access_token, and secret" do
39
- updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
40
- # this should not throw errors
41
- updates.app_id && updates.app_access_token && updates.secret
42
- end
43
-
44
- it "should not allow write access to app_id" do
45
- updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
46
- # this should not throw errors
47
- lambda { updates.app_id = 2 }.should raise_error(NoMethodError)
48
- end
49
-
50
- it "should not allow write access to app_access_token" do
51
- updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
52
- # this should not throw errors
53
- lambda { updates.app_access_token = 2 }.should raise_error(NoMethodError)
54
- end
55
-
56
- it "should not allow write access to secret" do
57
- updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
58
- # this should not throw errors
59
- lambda { updates.secret = 2 }.should raise_error(NoMethodError)
60
- end
61
-
62
- # init with secret / fetching the token
63
- it "should initialize properly with an app_id and a secret" do
64
- updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
65
- updates.should be_a(Facebook::RealtimeUpdates)
66
- end
67
-
68
- it "should fetch an app_token from Facebook when provided an app_id and a secret" do
69
- updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
70
- updates.app_access_token.should_not be_nil
71
- end
72
-
73
- it "should use the OAuth class to fetch a token when provided an app_id and a secret" do
74
- oauth = Facebook::OAuth.new(@app_id, @secret)
75
- token = oauth.get_app_access_token
76
- oauth.should_receive(:get_app_access_token).and_return(token)
77
- Facebook::OAuth.should_receive(:new).with(@app_id, @secret).and_return(oauth)
78
- updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
79
- end
80
- end
81
-
82
- describe "when used" do
83
- before :each do
84
- @updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
85
- end
86
-
87
- it "should send a subscription request to a valid server" do
88
- result = @updates.subscribe("user", "name", @subscription_path, @verify_token)
89
- result.should be_true
90
- end
91
-
92
- it "should send a subscription request to a valid server" do
93
- result = @updates.subscribe("user", "name", @subscription_path, @verify_token)
94
- result.should be_true
95
- end
96
-
97
- it "should send a subscription request to an invalid path on a valid server" do
98
- lambda { result = @updates.subscribe("user", "name", @subscription_path + "foo", @verify_token) }.should raise_exception(Koala::Facebook::APIError)
99
- end
100
-
101
- it "should fail to send a subscription request to an invalid server" do
102
- lambda { @updates.subscribe("user", "name", "foo", @verify_token) }.should raise_exception(Koala::Facebook::APIError)
103
- end
104
-
105
- it "should unsubscribe a valid individual object successfully" do
106
- @updates.unsubscribe("user").should be_true
107
- end
108
-
109
- it "should unsubscribe all subscriptions successfully" do
110
- @updates.unsubscribe.should be_true
111
- end
112
-
113
- it "should fail when an invalid object is provided to unsubscribe" do
114
- lambda { @updates.unsubscribe("kittens") }.should raise_error(Koala::Facebook::APIError)
115
- end
116
-
117
- it "should is subscriptions properly" do
118
- @updates.list_subscriptions.should be_a(Array)
119
- end
120
- end # describe "when used"
121
-
122
- describe "when meeting challenge" do
123
- it "should return false if hub.mode isn't subscribe" do
124
- params = {'hub.mode' => 'not subscribe'}
125
- Facebook::RealtimeUpdates.meet_challenge(params).should be_false
126
- end
127
-
128
- it "should return false if not given a verify_token or block" do
129
- params = {'hub.mode' => 'subscribe'}
130
- Facebook::RealtimeUpdates.meet_challenge(params).should be_false
131
- end
132
-
133
- describe "and mode is 'subscribe'" do
134
- before(:each) do
135
- @params = {'hub.mode' => 'subscribe'}
136
- end
137
-
138
- describe "and a token is given" do
139
- before(:each) do
140
- @token = 'token'
141
- @params['hub.verify_token'] = @token
142
- end
143
-
144
- it "should return false if the given verify token doesn't match" do
145
- Facebook::RealtimeUpdates.meet_challenge(@params, @token + '1').should be_false
146
- end
147
-
148
- it "should return the challenge if the given verify token matches" do
149
- @params['hub.challenge'] = 'challenge val'
150
- Facebook::RealtimeUpdates.meet_challenge(@params, @token).should == @params['hub.challenge']
151
- end
152
- end
153
-
154
- describe "and a block is given" do
155
- it "should give the block the token as a parameter" do
156
- Facebook::RealtimeUpdates.meet_challenge(@params)do |token|
157
- token.should == @token
158
- end
159
- end
160
-
161
- it "should return false if the given block return false" do
162
- Facebook::RealtimeUpdates.meet_challenge(@params)do |token|
163
- false
164
- end.should be_false
165
- end
166
-
167
- it "should return false if the given block returns nil" do
168
- Facebook::RealtimeUpdates.meet_challenge(@params)do |token|
169
- nil
170
- end.should be_false
171
- end
172
-
173
- it "should return the challenge if the given block returns true" do
174
- @params['hub.challenge'] = 'challenge val'
175
- Facebook::RealtimeUpdates.meet_challenge(@params) do |token|
176
- true
177
- end.should be_true
178
- end
179
- end
180
-
181
- end # describe "and mode is subscribe"
182
-
183
- end # describe "when meeting challenge"
184
-
185
- end # describe
186
-
187
- end #class
@@ -1,25 +0,0 @@
1
- shared_examples_for "Koala RestAPI without an access token" do
2
- # FQL_QUERY
3
- describe "when making a FQL request" do
4
- it "should be able to access public information via FQL" do
5
- @result = @api.fql_query("select first_name from user where uid = 216743")
6
- @result.size.should == 1
7
- @result.first["first_name"].should == "Chris"
8
- end
9
-
10
- it "should not be able to access protected information via FQL" do
11
- lambda { @api.fql_query("select read_stream from permissions where uid = 216743") }.should raise_error(Koala::Facebook::APIError)
12
- end
13
- end
14
- end
15
-
16
- class FacebookRestAPINoAccessTokenTest < Test::Unit::TestCase
17
- describe "Koala RestAPI without an access token" do
18
- before :each do
19
- @api = Koala::Facebook::RestAPI.new
20
- end
21
-
22
- it_should_behave_like "Koala RestAPI"
23
- it_should_behave_like "Koala RestAPI without an access token"
24
- end
25
- end