koala 0.9.0 → 1.0.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 (66) hide show
  1. data/.gitignore +3 -0
  2. data/CHANGELOG +47 -7
  3. data/Gemfile +3 -0
  4. data/LICENSE +1 -1
  5. data/Manifest +10 -15
  6. data/Rakefile +13 -13
  7. data/koala.gemspec +36 -16
  8. data/lib/koala/graph_api.rb +188 -123
  9. data/lib/koala/http_services.rb +93 -18
  10. data/lib/koala/rest_api.rb +73 -6
  11. data/lib/koala/test_users.rb +85 -0
  12. data/lib/koala/uploadable_io.rb +115 -0
  13. data/lib/koala.rb +114 -116
  14. data/readme.md +32 -18
  15. data/spec/cases/api_base_spec.rb +101 -0
  16. data/spec/cases/graph_and_rest_api_spec.rb +31 -0
  17. data/spec/cases/graph_api_spec.rb +25 -0
  18. data/spec/cases/http_services/http_service_spec.rb +54 -0
  19. data/spec/cases/http_services/net_http_service_spec.rb +350 -0
  20. data/spec/cases/http_services/typhoeus_service_spec.rb +144 -0
  21. data/spec/cases/oauth_spec.rb +409 -0
  22. data/spec/cases/realtime_updates_spec.rb +184 -0
  23. data/spec/cases/rest_api_spec.rb +25 -0
  24. data/spec/cases/test_users_spec.rb +221 -0
  25. data/spec/cases/uploadable_io_spec.rb +151 -0
  26. data/spec/fixtures/beach.jpg +0 -0
  27. data/spec/{facebook_data.yml → fixtures/facebook_data.yml} +18 -14
  28. data/spec/{mock_facebook_responses.yml → fixtures/mock_facebook_responses.yml} +314 -241
  29. data/spec/spec_helper.rb +18 -0
  30. data/spec/support/graph_api_shared_examples.rb +424 -0
  31. data/spec/support/live_testing_data_helper.rb +40 -0
  32. data/spec/{mock_http_service.rb → support/mock_http_service.rb} +94 -80
  33. data/spec/support/rest_api_shared_examples.rb +161 -0
  34. data/spec/support/setup_mocks_or_live.rb +52 -0
  35. data/spec/support/uploadable_io_shared_examples.rb +76 -0
  36. metadata +140 -55
  37. data/examples/oauth_playground/Capfile +0 -2
  38. data/examples/oauth_playground/LICENSE +0 -22
  39. data/examples/oauth_playground/Rakefile +0 -4
  40. data/examples/oauth_playground/config/deploy.rb +0 -39
  41. data/examples/oauth_playground/config/facebook.yml +0 -13
  42. data/examples/oauth_playground/config.ru +0 -27
  43. data/examples/oauth_playground/lib/load_facebook.rb +0 -3
  44. data/examples/oauth_playground/lib/oauth_playground.rb +0 -187
  45. data/examples/oauth_playground/readme.md +0 -8
  46. data/examples/oauth_playground/spec/oauth_playground_spec.rb +0 -35
  47. data/examples/oauth_playground/spec/spec_helper.rb +0 -36
  48. data/examples/oauth_playground/tmp/restart.txt +0 -0
  49. data/examples/oauth_playground/views/index.erb +0 -206
  50. data/examples/oauth_playground/views/layout.erb +0 -39
  51. data/init.rb +0 -2
  52. data/spec/koala/api_base_tests.rb +0 -95
  53. data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +0 -10
  54. data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +0 -11
  55. data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +0 -114
  56. data/spec/koala/graph_api/graph_api_with_access_token_tests.rb +0 -150
  57. data/spec/koala/graph_api/graph_collection_tests.rb +0 -104
  58. data/spec/koala/live_testing_data_helper.rb +0 -15
  59. data/spec/koala/net_http_service_tests.rb +0 -181
  60. data/spec/koala/oauth/oauth_tests.rb +0 -440
  61. data/spec/koala/realtime_updates/realtime_updates_tests.rb +0 -187
  62. data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +0 -94
  63. data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +0 -36
  64. data/spec/koala_spec.rb +0 -18
  65. data/spec/koala_spec_helper.rb +0 -31
  66. data/spec/koala_spec_without_mocks.rb +0 -19
@@ -1,440 +0,0 @@
1
- # stub the Time class to always return a time for which the valid cookie is still valid
2
- class Time
3
- def self.now
4
- self
5
- end
6
-
7
- def self.to_i
8
- 1273363199
9
- end
10
- end
11
-
12
- class FacebookOAuthTests < Test::Unit::TestCase
13
- describe "Koala OAuth tests" do
14
- before :each do
15
- # make the relevant test data easily accessible
16
- @oauth_data = $testing_data["oauth_test_data"]
17
- @app_id = @oauth_data["app_id"]
18
- @secret = @oauth_data["secret"]
19
- @code = @oauth_data["code"]
20
- @callback_url = @oauth_data["callback_url"]
21
- @raw_token_string = @oauth_data["raw_token_string"]
22
- @raw_offline_access_token_string = @oauth_data["raw_offline_access_token_string"]
23
-
24
- # per Facebook's example:
25
- # http://developers.facebook.com/docs/authentication/canvas
26
- # this allows us to use Facebook's example data while running the other live data
27
- @request_secret = @oauth_data["request_secret"] || @secret
28
- @signed_request = @oauth_data["signed_request"]
29
- @signed_request_result = @oauth_data["signed_request_result"]
30
-
31
- # this should expanded to cover all variables
32
- raise Exception, "Must supply app data to run FacebookOAuthTests!" unless @app_id && @secret && @callback_url &&
33
- @code && @raw_token_string &&
34
- @raw_offline_access_token_string
35
-
36
- @oauth = Koala::Facebook::OAuth.new(@app_id, @secret, @callback_url)
37
- end
38
-
39
- # initialization
40
- it "should properly initialize" do
41
- @oauth.should
42
- end
43
-
44
- it "should properly set attributes" do
45
- (@oauth.app_id == @app_id &&
46
- @oauth.app_secret == @secret &&
47
- @oauth.oauth_callback_url == @callback_url).should be_true
48
- end
49
-
50
- it "should properly initialize without a callback_url" do
51
- @oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
52
- end
53
-
54
- it "should properly set attributes without a callback URL" do
55
- @oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
56
- (@oauth.app_id == @app_id &&
57
- @oauth.app_secret == @secret &&
58
- @oauth.oauth_callback_url == nil).should be_true
59
- end
60
-
61
- describe "for cookie parsing" do
62
- describe "get_user_info_from_cookies" do
63
- it "should properly parse valid cookies" do
64
- result = @oauth.get_user_info_from_cookies(@oauth_data["valid_cookies"])
65
- result.should be_a(Hash)
66
- end
67
-
68
- it "should return all the cookie components from valid cookie string" do
69
- cookie_data = @oauth_data["valid_cookies"]
70
- parsing_results = @oauth.get_user_info_from_cookies(cookie_data)
71
- number_of_components = cookie_data["fbs_#{@app_id.to_s}"].scan(/\=/).length
72
- parsing_results.length.should == number_of_components
73
- end
74
-
75
- it "should properly parse valid offline access cookies (e.g. no expiration)" do
76
- result = @oauth.get_user_info_from_cookies(@oauth_data["offline_access_cookies"])
77
- result["uid"].should
78
- end
79
-
80
- it "should return all the cookie components from offline access cookies" do
81
- cookie_data = @oauth_data["offline_access_cookies"]
82
- parsing_results = @oauth.get_user_info_from_cookies(cookie_data)
83
- number_of_components = cookie_data["fbs_#{@app_id.to_s}"].scan(/\=/).length
84
- parsing_results.length.should == number_of_components
85
- end
86
-
87
- it "shouldn't parse expired cookies" do
88
- result = @oauth.get_user_info_from_cookies(@oauth_data["expired_cookies"])
89
- result.should be_nil
90
- end
91
-
92
- it "shouldn't parse invalid cookies" do
93
- # make an invalid string by replacing some values
94
- bad_cookie_hash = @oauth_data["valid_cookies"].inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
95
- result = @oauth.get_user_info_from_cookies(bad_cookie_hash)
96
- result.should be_nil
97
- end
98
- end
99
-
100
- describe "get_user_from_cookies" do
101
- it "should use get_user_info_from_cookies to parse the cookies" do
102
- data = @oauth_data["valid_cookies"]
103
- @oauth.should_receive(:get_user_info_from_cookies).with(data).and_return({})
104
- @oauth.get_user_from_cookies(data)
105
- end
106
-
107
- it "should use return a string if the cookies are valid" do
108
- result = @oauth.get_user_from_cookies(@oauth_data["valid_cookies"])
109
- result.should be_a(String)
110
- end
111
-
112
- it "should return nil if the cookies are invalid" do
113
- # make an invalid string by replacing some values
114
- bad_cookie_hash = @oauth_data["valid_cookies"].inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
115
- result = @oauth.get_user_from_cookies(bad_cookie_hash)
116
- result.should be_nil
117
- end
118
- end
119
- end
120
-
121
- # OAuth URLs
122
-
123
- describe "for URL generation" do
124
-
125
- describe "for OAuth codes" do
126
- # url_for_oauth_code
127
- it "should generate a properly formatted OAuth code URL with the default values" do
128
- url = @oauth.url_for_oauth_code
129
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}"
130
- end
131
-
132
- it "should generate a properly formatted OAuth code URL when a callback is given" do
133
- callback = "foo.com"
134
- url = @oauth.url_for_oauth_code(:callback => callback)
135
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{callback}"
136
- end
137
-
138
- it "should generate a properly formatted OAuth code URL when permissions are requested as a string" do
139
- permissions = "publish_stream,read_stream"
140
- url = @oauth.url_for_oauth_code(:permissions => permissions)
141
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}&scope=#{permissions}"
142
- end
143
-
144
- it "should generate a properly formatted OAuth code URL when permissions are requested as a string" do
145
- permissions = ["publish_stream", "read_stream"]
146
- url = @oauth.url_for_oauth_code(:permissions => permissions)
147
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}&scope=#{permissions.join(",")}"
148
- end
149
-
150
- it "should generate a properly formatted OAuth code URL when both permissions and callback are provided" do
151
- permissions = "publish_stream,read_stream"
152
- callback = "foo.com"
153
- url = @oauth.url_for_oauth_code(:callback => callback, :permissions => permissions)
154
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{callback}&scope=#{permissions}"
155
- end
156
-
157
- it "should raise an exception if no callback is given in initialization or the call" do
158
- oauth2 = Koala::Facebook::OAuth.new(@app_id, @secret)
159
- lambda { oauth2.url_for_oauth_code }.should raise_error(ArgumentError)
160
- end
161
- end
162
-
163
- describe "for access token URLs" do
164
-
165
- # url_for_access_token
166
- it "should generate a properly formatted OAuth token URL when provided a code" do
167
- url = @oauth.url_for_access_token(@code)
168
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{@callback_url}&client_secret=#{@secret}&code=#{@code}"
169
- end
170
-
171
- it "should generate a properly formatted OAuth token URL when provided a callback" do
172
- callback = "foo.com"
173
- url = @oauth.url_for_access_token(@code, :callback => callback)
174
- url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{callback}&client_secret=#{@secret}&code=#{@code}"
175
- end
176
- end
177
- end
178
-
179
- describe "for fetching access tokens" do
180
- describe "get_access_token_info" do
181
- it "should properly get and parse an access token token results into a hash" do
182
- result = @oauth.get_access_token_info(@code)
183
- result.should be_a(Hash)
184
- end
185
-
186
- it "should properly include the access token results" do
187
- result = @oauth.get_access_token_info(@code)
188
- result["access_token"].should
189
- end
190
-
191
- it "should raise an error when get_access_token is called with a bad code" do
192
- lambda { @oauth.get_access_token_info("foo") }.should raise_error(Koala::Facebook::APIError)
193
- end
194
- end
195
-
196
- describe "get_access_token" do
197
- it "should use get_access_token_info to get and parse an access token token results" do
198
- result = @oauth.get_access_token(@code)
199
- result.should be_a(String)
200
- end
201
-
202
- it "should return the access token as a string" do
203
- result = @oauth.get_access_token(@code)
204
- original = @oauth.get_access_token_info(@code)
205
- result.should == original["access_token"]
206
- end
207
-
208
- it "should raise an error when get_access_token is called with a bad code" do
209
- lambda { @oauth.get_access_token("foo") }.should raise_error(Koala::Facebook::APIError)
210
- end
211
- end
212
-
213
- describe "get_app_access_token_info" do
214
- it "should properly get and parse an app's access token as a hash" do
215
- result = @oauth.get_app_access_token_info
216
- result.should be_a(Hash)
217
- end
218
-
219
- it "should include the access token" do
220
- result = @oauth.get_app_access_token_info
221
- result["access_token"].should
222
- end
223
- end
224
-
225
- describe "get_app_acess_token" do
226
- it "should use get_access_token_info to get and parse an access token token results" do
227
- result = @oauth.get_app_access_token
228
- result.should be_a(String)
229
- end
230
-
231
- it "should return the access token as a string" do
232
- result = @oauth.get_app_access_token
233
- original = @oauth.get_app_access_token_info
234
- result.should == original["access_token"]
235
- end
236
- end
237
-
238
- describe "protected methods" do
239
-
240
- # protected methods
241
- # since these are pretty fundamental and pretty testable, we want to test them
242
-
243
- # parse_access_token
244
- it "should properly parse access token results" do
245
- result = @oauth.send(:parse_access_token, @raw_token_string)
246
- has_both_parts = result["access_token"] && result["expires"]
247
- has_both_parts.should
248
- end
249
-
250
- it "should properly parse offline access token results" do
251
- result = @oauth.send(:parse_access_token, @raw_offline_access_token_string)
252
- has_both_parts = result["access_token"] && !result["expires"]
253
- has_both_parts.should
254
- end
255
-
256
- # fetch_token_string
257
- # somewhat duplicative with the tests for get_access_token and get_app_access_token
258
- # but no harm in thoroughness
259
- it "should fetch a proper token string from Facebook when given a code" do
260
- result = @oauth.send(:fetch_token_string, :code => @code, :redirect_uri => @callback_url)
261
- result.should =~ /^access_token/
262
- end
263
-
264
- it "should fetch a proper token string from Facebook when asked for the app token" do
265
- result = @oauth.send(:fetch_token_string, {:type => 'client_cred'}, true)
266
- result.should =~ /^access_token/
267
- end
268
- end
269
- end
270
-
271
- describe "for exchanging session keys" do
272
- describe "with get_token_info_from_session_keys" do
273
- it "should get an array of session keys from Facebook when passed a single key" do
274
- result = @oauth.get_tokens_from_session_keys([@oauth_data["session_key"]])
275
- result.should be_an(Array)
276
- result.length.should == 1
277
- end
278
-
279
- it "should get an array of session keys from Facebook when passed multiple keys" do
280
- result = @oauth.get_tokens_from_session_keys(@oauth_data["multiple_session_keys"])
281
- result.should be_an(Array)
282
- result.length.should == 2
283
- end
284
-
285
- it "should return the original hashes" do
286
- result = @oauth.get_token_info_from_session_keys(@oauth_data["multiple_session_keys"])
287
- result[0].should be_a(Hash)
288
- end
289
-
290
- it "should properly handle invalid session keys" do
291
- result = @oauth.get_token_info_from_session_keys(["foo", "bar"])
292
- #it should return nil for each of the invalid ones
293
- result.each {|r| r.should be_nil}
294
- end
295
-
296
- it "should properly handle a mix of valid and invalid session keys" do
297
- result = @oauth.get_token_info_from_session_keys(["foo"].concat(@oauth_data["multiple_session_keys"]))
298
- # it should return nil for each of the invalid ones
299
- result.each_with_index {|r, index| index > 0 ? r.should(be_a(Hash)) : r.should(be_nil)}
300
- end
301
- end
302
-
303
- describe "with get_tokens_from_session_keys" do
304
- it "should call get_token_info_from_session_keys" do
305
- args = @oauth_data["multiple_session_keys"]
306
- @oauth.should_receive(:get_token_info_from_session_keys).with(args).and_return([])
307
- @oauth.get_tokens_from_session_keys(args)
308
- end
309
-
310
- it "should return an array of strings" do
311
- args = @oauth_data["multiple_session_keys"]
312
- result = @oauth.get_tokens_from_session_keys(args)
313
- result.each {|r| r.should be_a(String) }
314
- end
315
-
316
- it "should properly handle invalid session keys" do
317
- result = @oauth.get_tokens_from_session_keys(["foo", "bar"])
318
- # it should return nil for each of the invalid ones
319
- result.each {|r| r.should be_nil}
320
- end
321
-
322
- it "should properly handle a mix of valid and invalid session keys" do
323
- result = @oauth.get_tokens_from_session_keys(["foo"].concat(@oauth_data["multiple_session_keys"]))
324
- # it should return nil for each of the invalid ones
325
- result.each_with_index {|r, index| index > 0 ? r.should(be_a(String)) : r.should(be_nil)}
326
- end
327
- end
328
-
329
- describe "get_token_from_session_key" do
330
- it "should call get_tokens_from_session_keys when the get_token_from_session_key is called" do
331
- key = @oauth_data["session_key"]
332
- @oauth.should_receive(:get_tokens_from_session_keys).with([key]).and_return([])
333
- @oauth.get_token_from_session_key(key)
334
- end
335
-
336
- it "should get back the access token string from get_token_from_session_key" do
337
- result = @oauth.get_token_from_session_key(@oauth_data["session_key"])
338
- result.should be_a(String)
339
- end
340
-
341
- it "should be the first value in the array" do
342
- result = @oauth.get_token_from_session_key(@oauth_data["session_key"])
343
- array = @oauth.get_tokens_from_session_keys([@oauth_data["session_key"]])
344
- result.should == array[0]
345
- end
346
-
347
- it "should properly handle an invalid session key" do
348
- result = @oauth.get_token_from_session_key("foo")
349
- result.should be_nil
350
- end
351
- end
352
- end
353
-
354
- describe "for parsing signed requests" do
355
- before :each do
356
- # you can test against live data by updating the YML, or you can use the default data
357
- # which tests against Facebook's example on http://developers.facebook.com/docs/authentication/canvas
358
- @oauth = Koala::Facebook::OAuth.new(@app_id, @request_secret || @app_secret)
359
- end
360
-
361
- it "should break the request into the encoded signature and the payload" do
362
- @signed_request.should_receive(:split).with(".").and_return(["", ""])
363
- @oauth.parse_signed_request(@signed_request)
364
- end
365
-
366
- it "should base64 URL decode the signed request" do
367
- sig = ""
368
- @signed_request.should_receive(:split).with(".").and_return([sig, "1"])
369
- @oauth.should_receive(:base64_url_decode).with(sig).and_return("4")
370
- @oauth.parse_signed_request(@signed_request)
371
- end
372
-
373
- it "should base64 URL decode the signed request" do
374
- sig = @signed_request.split(".")[0]
375
- @oauth.should_receive(:base64_url_decode).with(sig).and_return(nil)
376
- @oauth.parse_signed_request(@signed_request)
377
- end
378
-
379
- it "should get the sha64 encoded payload using proper arguments from OpenSSL::HMAC" do
380
- payload = ""
381
- @signed_request.should_receive(:split).with(".").and_return(["1", payload])
382
- OpenSSL::HMAC.should_receive(:digest).with("sha256", @request_secret, payload)
383
- @oauth.parse_signed_request(@signed_request)
384
- end
385
-
386
- it "should compare the encoded payload with the signature" do
387
- sig = "2"
388
- @oauth.should_receive(:base64_url_decode).and_return(sig)
389
- encoded_payload = "1"
390
- OpenSSL::HMAC.should_receive(:digest).with(anything, anything, anything).and_return(encoded_payload)
391
- encoded_payload.should_receive(:==).with(sig)
392
- @oauth.parse_signed_request(@signed_request)
393
- end
394
-
395
- describe "if the encoded payload matches the signature" do
396
- before :each do
397
- # set it up so the sig will match the encoded payload
398
- raw_sig = ""
399
- @sig = "2"
400
- @payload = "1"
401
- @signed_request.should_receive(:split).and_return([raw_sig, @payload])
402
- @oauth.should_receive(:base64_url_decode).with(raw_sig).and_return(@sig)
403
- OpenSSL::HMAC.should_receive(:digest).with(anything, anything, anything).and_return(@sig.dup)
404
- end
405
-
406
- it "should base64_url_decode the payload" do
407
- @oauth.should_receive(:base64_url_decode).with(@payload).ordered.and_return("{}")
408
- @oauth.parse_signed_request(@signed_request)
409
- end
410
-
411
- it "should JSON decode the payload" do
412
- result = "{}"
413
- @oauth.should_receive(:base64_url_decode).with(@payload).and_return(result)
414
- JSON.should_receive(:parse).with(result)
415
- @oauth.parse_signed_request(@signed_request)
416
- end
417
- end
418
-
419
- describe "if the encoded payload does not match the signature" do
420
- before :each do
421
- sig = ""
422
- @signed_request.should_receive(:split).and_return([sig, ""])
423
- OpenSSL::HMAC.should_receive(:digest).with(anything, anything, anything).and_return("hi")
424
- end
425
-
426
- it "should return nil" do
427
- @oauth.parse_signed_request(@signed_request).should be_nil
428
- end
429
- end
430
-
431
- describe "run against data" do
432
- it "should work" do
433
- @oauth.parse_signed_request(@signed_request).should == @signed_request_result
434
- end
435
- end
436
- end
437
-
438
- end # describe
439
-
440
- 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