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