koala 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ shared_examples_for "Koala GraphAPI" do
2
2
  # all Graph API instances should pass these tests, regardless of configuration
3
3
 
4
4
  # API
5
- it "should never use the rest api server" do
5
+ it "never uses the rest api server" do
6
6
  Koala.should_receive(:make_request).with(
7
7
  anything,
8
8
  anything,
@@ -15,13 +15,13 @@ shared_examples_for "Koala GraphAPI" do
15
15
 
16
16
  # GRAPH CALL
17
17
  describe "graph_call" do
18
- it "should pass all arguments to the api method" do
18
+ it "passes all arguments to the api method" do
19
19
  args = [KoalaTest.user1, {}, "get", {:a => :b}]
20
20
  @api.should_receive(:api).with(*args)
21
21
  @api.graph_call(*args)
22
22
  end
23
23
 
24
- it "should throw an APIError if the result hash has an error key" do
24
+ it "throws an APIError if the result hash has an error key" do
25
25
  Koala.stub(:make_request).and_return(Koala::Response.new(500, {"error" => "An error occurred!"}, {}))
26
26
  lambda { @api.graph_call(KoalaTest.user1, {}) }.should raise_exception(Koala::Facebook::APIError)
27
27
  end
@@ -49,7 +49,7 @@ shared_examples_for "Koala GraphAPI" do
49
49
  end
50
50
 
51
51
  # SEARCH
52
- it "should be able to search" do
52
+ it "can search" do
53
53
  result = @api.search("facebook")
54
54
  result.length.should be_an(Integer)
55
55
  end
@@ -58,109 +58,116 @@ shared_examples_for "Koala GraphAPI" do
58
58
  # access public info
59
59
 
60
60
  # get_object
61
- it "should get public data about a user" do
61
+ it "gets public data about a user" do
62
62
  result = @api.get_object(KoalaTest.user1)
63
63
  # the results should have an ID and a name, among other things
64
64
  (result["id"] && result["name"]).should_not be_nil
65
65
  end
66
66
 
67
- it "should get public data about a Page" do
67
+ it "gets public data about a Page" do
68
68
  result = @api.get_object(KoalaTest.page)
69
69
  # the results should have an ID and a name, among other things
70
70
  (result["id"] && result["name"]).should
71
71
  end
72
72
 
73
- it "should return [] from get_objects if passed an empty array" do
73
+ it "returns [] from get_objects if passed an empty array" do
74
74
  results = @api.get_objects([])
75
75
  results.should == []
76
76
  end
77
77
 
78
- it "should be able to get multiple objects" do
78
+ it "gets multiple objects" do
79
79
  results = @api.get_objects([KoalaTest.page, KoalaTest.user1])
80
80
  results.should have(2).items
81
81
  end
82
82
 
83
- it "should be able to get multiple objects if they're a string" do
83
+ it "gets multiple objects if they're a string" do
84
84
  results = @api.get_objects("contextoptional,#{KoalaTest.user1}")
85
85
  results.should have(2).items
86
86
  end
87
87
 
88
- it "should be able to access a user's picture" do
88
+ it "can access a user's picture" do
89
89
  @api.get_picture("chris.baclig").should =~ /http[s]*\:\/\//
90
90
  end
91
91
 
92
- it "should be able to access a user's picture, given a picture type" do
92
+ it "can access a user's picture, given a picture type" do
93
93
  @api.get_picture(KoalaTest.user2, {:type => 'large'}).should =~ /^http[s]*\:\/\//
94
94
  end
95
95
 
96
- it "should be able to access connections from public Pages" do
96
+ it "can access connections from public Pages" do
97
97
  result = @api.get_connections(KoalaTest.page, "photos")
98
98
  result.should be_a(Array)
99
99
  end
100
100
 
101
- it "should be able to access comments for a URL" do
101
+ it "can access comments for a URL" do
102
102
  result = @api.get_comments_for_urls(["http://developers.facebook.com/blog/post/472"])
103
103
  (result["http://developers.facebook.com/blog/post/472"]).should
104
104
  end
105
105
 
106
- it "should be able to access comments for 2 URLs" do
106
+ it "can access comments for 2 URLs" do
107
107
  result = @api.get_comments_for_urls(["http://developers.facebook.com/blog/post/490", "http://developers.facebook.com/blog/post/472"])
108
108
  (result["http://developers.facebook.com/blog/post/490"] && result["http://developers.facebook.com/blog/post/472"]).should
109
109
  end
110
110
 
111
111
  # SEARCH
112
- it "should be able to search" do
112
+ it "can search" do
113
113
  result = @api.search("facebook")
114
114
  result.length.should be_an(Integer)
115
115
  end
116
116
 
117
117
  # PAGING THROUGH COLLECTIONS
118
118
  # see also graph_collection_tests
119
- it "should make a request for a page when provided a specific set of page params" do
119
+ it "makes a request for a page when provided a specific set of page params" do
120
120
  query = [1, 2]
121
121
  @api.should_receive(:graph_call).with(*query)
122
122
  @api.get_page(query)
123
123
  end
124
+
125
+ # Beta tier
126
+ it "can use the beta tier" do
127
+ result = @api.get_object(KoalaTest.user1, {}, :beta => true)
128
+ # the results should have an ID and a name, among other things
129
+ (result["id"] && result["name"]).should_not be_nil
130
+ end
124
131
  end
125
132
 
126
133
 
127
134
  shared_examples_for "Koala GraphAPI with an access token" do
128
- it "should get private data about a user" do
135
+ it "gets private data about a user" do
129
136
  result = @api.get_object(KoalaTest.user1)
130
137
  # updated_time should be a pretty fixed test case
131
138
  result["updated_time"].should_not be_nil
132
139
  end
133
140
 
134
- it "should get data about 'me'" do
141
+ it "gets data about 'me'" do
135
142
  result = @api.get_object("me")
136
143
  result["updated_time"].should
137
144
  end
138
145
 
139
- it "should be able to get multiple objects" do
146
+ it "gets multiple objects" do
140
147
  result = @api.get_objects([KoalaTest.page, KoalaTest.user1])
141
148
  result.length.should == 2
142
149
  end
143
- it "should be able to access connections from users" do
150
+ it "can access connections from users" do
144
151
  result = @api.get_connections(KoalaTest.user2, "friends")
145
152
  result.length.should > 0
146
153
  end
147
154
 
148
155
  # PUT
149
- it "should be able to write an object to the graph" do
156
+ it "can write an object to the graph" do
150
157
  result = @api.put_wall_post("Hello, world, from the test suite!")
151
158
  @temporary_object_id = result["id"]
152
159
  @temporary_object_id.should_not be_nil
153
160
  end
154
161
 
155
162
  # DELETE
156
- it "should be able to delete posts" do
163
+ it "can delete posts" do
157
164
  result = @api.put_wall_post("Hello, world, from the test suite delete method!")
158
165
  object_id_to_delete = result["id"]
159
166
  delete_result = @api.delete_object(object_id_to_delete)
160
167
  delete_result.should == true
161
168
  end
162
169
 
163
- it "should be able to delete likes" do
170
+ it "can delete likes" do
164
171
  result = @api.put_wall_post("Hello, world, from the test suite delete method!")
165
172
  @temporary_object_id = result["id"]
166
173
  @api.put_like(@temporary_object_id)
@@ -169,7 +176,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
169
176
  end
170
177
 
171
178
  # additional put tests
172
- it "should be able to verify messages posted to a wall" do
179
+ it "can verify messages posted to a wall" do
173
180
  message = "the cats are asleep"
174
181
  put_result = @api.put_wall_post(message)
175
182
  @temporary_object_id = put_result["id"]
@@ -179,14 +186,33 @@ shared_examples_for "Koala GraphAPI with an access token" do
179
186
  get_result["message"].should == message
180
187
  end
181
188
 
182
- it "should be able to post a message with an attachment to a feed" do
189
+ it "can post a message with an attachment to a feed" do
183
190
  result = @api.put_wall_post("Hello, world, from the test suite again!", {:name => "OAuth Playground", :link => "http://oauth.twoalex.com/"})
184
191
  @temporary_object_id = result["id"]
185
192
  @temporary_object_id.should_not be_nil
186
193
  end
194
+
195
+ it "can post a message whose attachment has a properties dictionary" do
196
+ url = KoalaTest.oauth_test_data["callback_url"]
197
+ options = {
198
+ "picture" => "#{KoalaTest.oauth_test_data["callback_url"]}/images/logo.png",
199
+ "name" => "It's a big question",
200
+ "type" => "link",
201
+ "link" => KoalaTest.oauth_test_data["callback_url"],
202
+ "properties" => [
203
+ {"name" => "Link1'", "text" => "Left", "href" => url},
204
+ {"name" => "other", "text" => "Straight ahead"}
205
+ ]
206
+ }
207
+
208
+ result = @api.put_wall_post("body", options)
209
+ @temporary_object_id = result["id"]
210
+ @temporary_object_id.should_not be_nil
211
+ end
212
+
187
213
 
188
214
  describe ".put_picture" do
189
- it "should be able to post photos to the user's wall with an open file object" do
215
+ it "can post photos to the user's wall with an open file object" do
190
216
  content_type = "image/jpg"
191
217
  file = File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "beach.jpg"))
192
218
 
@@ -195,7 +221,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
195
221
  @temporary_object_id.should_not be_nil
196
222
  end
197
223
 
198
- it "should be able to post photos to the user's wall without an open file object" do
224
+ it "can post photos to the user's wall without an open file object" do
199
225
  content_type = "image/jpg",
200
226
  file_path = File.join(File.dirname(__FILE__), "..", "fixtures", "beach.jpg")
201
227
 
@@ -204,7 +230,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
204
230
  @temporary_object_id.should_not be_nil
205
231
  end
206
232
 
207
- it "should be able to verify a photo posted to a user's wall" do
233
+ it "can verify a photo posted to a user's wall" do
208
234
  content_type = "image/jpg",
209
235
  file_path = File.join(File.dirname(__FILE__), "..", "fixtures", "beach.jpg")
210
236
 
@@ -224,13 +250,13 @@ shared_examples_for "Koala GraphAPI with an access token" do
224
250
  @url = "http://img.slate.com/images/redesign2008/slate_logo.gif"
225
251
  end
226
252
 
227
- it "should be able to post photo to the user's wall using a URL" do
253
+ it "can post photo to the user's wall using a URL" do
228
254
  result = @api.put_picture(@url)
229
255
  @temporary_object_id = result["id"]
230
256
  @temporary_object_id.should_not be_nil
231
257
  end
232
258
 
233
- it "should be able to post photo to the user's wall using a URL and an additional param" do
259
+ it "can post photo to the user's wall using a URL and an additional param" do
234
260
  result = @api.put_picture(@url, :message => "my message")
235
261
  @temporary_object_id = result["id"]
236
262
  @temporary_object_id.should_not be_nil
@@ -244,7 +270,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
244
270
  @content_type = "video/mpeg4"
245
271
  end
246
272
 
247
- it "should set options[:video] to true" do
273
+ it "sets options[:video] to true" do
248
274
  source = stub("UploadIO")
249
275
  Koala::UploadableIO.stub(:new).and_return(source)
250
276
  source.stub(:requires_base_http_service).and_return(false)
@@ -252,7 +278,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
252
278
  @api.put_video("foo")
253
279
  end
254
280
 
255
- it "should be able to post videos to the user's wall with an open file object" do
281
+ it "can post videos to the user's wall with an open file object" do
256
282
  file = File.open(@cat_movie)
257
283
 
258
284
  result = @api.put_video(file, @content_type)
@@ -261,7 +287,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
261
287
  end
262
288
 
263
289
 
264
- it "should be able to post videos to the user's wall without an open file object" do
290
+ it "can post videos to the user's wall without an open file object" do
265
291
  result = @api.put_video(@cat_movie, @content_type)
266
292
  @temporary_object_id = result["id"]
267
293
  @temporary_object_id.should_not be_nil
@@ -272,7 +298,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
272
298
  # hence we can't do the same verify test we do for photos
273
299
  end
274
300
 
275
- it "should be able to verify a message with an attachment posted to a feed" do
301
+ it "can verify a message with an attachment posted to a feed" do
276
302
  attachment = {"name" => "OAuth Playground", "link" => "http://oauth.twoalex.com/"}
277
303
  result = @api.put_wall_post("Hello, world, from the test suite again!", attachment)
278
304
  @temporary_object_id = result["id"]
@@ -283,7 +309,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
283
309
  it_matches.should == true
284
310
  end
285
311
 
286
- it "should be able to comment on an object" do
312
+ it "can comment on an object" do
287
313
  result = @api.put_wall_post("Hello, world, from the test suite, testing comments!")
288
314
  @temporary_object_id = result["id"]
289
315
 
@@ -292,8 +318,8 @@ shared_examples_for "Koala GraphAPI with an access token" do
292
318
  comment_result.should_not be_nil
293
319
  end
294
320
 
295
- it "should be able to verify a comment posted about an object" do
296
- message_text = "Hello, world, from the test suite, testing comments!"
321
+ it "can verify a comment posted about an object" do
322
+ message_text = "Hello, world, from the test suite, testing comments again!"
297
323
  result = @api.put_wall_post(message_text)
298
324
  @temporary_object_id = result["id"]
299
325
 
@@ -306,8 +332,8 @@ shared_examples_for "Koala GraphAPI with an access token" do
306
332
  get_result["message"].should == comment_text
307
333
  end
308
334
 
309
- it "should be able to like an object" do
310
- result = @api.put_wall_post("Hello, world, from the test suite, testing comments!")
335
+ it "can like an object" do
336
+ result = @api.put_wall_post("Hello, world, from the test suite, testing liking!")
311
337
  @temporary_object_id = result["id"]
312
338
  like_result = @api.put_like(@temporary_object_id)
313
339
  like_result.should be_true
@@ -342,7 +368,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
342
368
  :put_video => ["x.mp4", "video/mpeg4", {}, "me"],
343
369
  :get_objects => [["x"], {}]
344
370
  }.each_pair do |method_name, params|
345
- it "should pass http options through for #{method_name}" do
371
+ it "passes http options through for #{method_name}" do
346
372
  options = {:a => 2}
347
373
  # graph call should ultimately receive options as the fourth argument
348
374
  @api.should_receive(:graph_call).with(anything, anything, anything, options)
@@ -356,7 +382,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
356
382
  end
357
383
 
358
384
  # also test get_picture, which merges a parameter into options
359
- it "should pass http options through for get_picture" do
385
+ it "passes http options through for get_picture" do
360
386
  options = {:a => 2}
361
387
  # graph call should ultimately receive options as the fourth argument
362
388
  @api.should_receive(:graph_call).with(anything, anything, anything, hash_including(options)).and_return({})
@@ -370,34 +396,34 @@ end
370
396
  shared_examples_for "Koala GraphAPI with GraphCollection" do
371
397
  describe "when getting a collection" do
372
398
  # GraphCollection methods
373
- it "should get a GraphCollection when getting connections" do
399
+ it "gets a GraphCollection when getting connections" do
374
400
  @result = @api.get_connections(KoalaTest.page, "photos")
375
401
  @result.should be_a(Koala::Facebook::GraphCollection)
376
402
  end
377
403
 
378
- it "should return nil if the get_collections call fails with nil" do
404
+ it "returns nil if the get_collections call fails with nil" do
379
405
  # this happens sometimes
380
406
  @api.should_receive(:graph_call).and_return(nil)
381
407
  @api.get_connections(KoalaTest.page, "photos").should be_nil
382
408
  end
383
409
 
384
- it "should get a GraphCollection when searching" do
410
+ it "gets a GraphCollection when searching" do
385
411
  result = @api.search("facebook")
386
412
  result.should be_a(Koala::Facebook::GraphCollection)
387
413
  end
388
414
 
389
- it "should return nil if the search call fails with nil" do
415
+ it "returns nil if the search call fails with nil" do
390
416
  # this happens sometimes
391
417
  @api.should_receive(:graph_call).and_return(nil)
392
418
  @api.search("facebook").should be_nil
393
419
  end
394
420
 
395
- it "should get a GraphCollection when paging through results" do
421
+ it "gets a GraphCollection when paging through results" do
396
422
  @results = @api.get_page(["search", {"q"=>"facebook", "limit"=>"25", "until"=> KoalaTest.search_time}])
397
423
  @results.should be_a(Koala::Facebook::GraphCollection)
398
424
  end
399
425
 
400
- it "should return nil if the page call fails with nil" do
426
+ it "returns nil if the page call fails with nil" do
401
427
  # this happens sometimes
402
428
  @api.should_receive(:graph_call).and_return(nil)
403
429
  @api.get_page(["search", {"q"=>"facebook", "limit"=>"25", "until"=> KoalaTest.search_time}]).should be_nil
@@ -408,48 +434,48 @@ end
408
434
 
409
435
  shared_examples_for "Koala GraphAPI without an access token" do
410
436
 
411
- it "should not get private data about a user" do
437
+ it "can't get private data about a user" do
412
438
  result = @api.get_object("koppel")
413
439
  # updated_time should be a pretty fixed test case
414
440
  result["updated_time"].should be_nil
415
441
  end
416
442
 
417
- it "should not be able to get data about 'me'" do
443
+ it "can't get data about 'me'" do
418
444
  lambda { @api.get_object("me") }.should raise_error(Koala::Facebook::APIError)
419
445
  end
420
446
 
421
- it "shouldn't be able to access connections from users" do
447
+ it "can't access connections from users" do
422
448
  lambda { @api.get_connections("lukeshepard", "friends") }.should raise_error(Koala::Facebook::APIError)
423
449
  end
424
450
 
425
- it "should not be able to put an object" do
451
+ it "can't put an object" do
426
452
  lambda { @result = @api.put_object("lukeshepard", "feed", :message => "Hello, world") }.should raise_error(Koala::Facebook::APIError)
427
453
  end
428
454
 
429
455
  # these are not strictly necessary as the other put methods resolve to put_object, but are here for completeness
430
- it "should not be able to post to a feed" do
456
+ it "can't post to a feed" do
431
457
  (lambda do
432
458
  attachment = {:name => "OAuth Playground", :link => "http://oauth.twoalex.com/"}
433
459
  @result = @api.put_wall_post("Hello, world", attachment, "contextoptional")
434
460
  end).should raise_error(Koala::Facebook::APIError)
435
461
  end
436
462
 
437
- it "should not be able to comment on an object" do
463
+ it "can't comment on an object" do
438
464
  # random public post on the ContextOptional wall
439
465
  lambda { @result = @api.put_comment("7204941866_119776748033392", "The hackathon was great!") }.should raise_error(Koala::Facebook::APIError)
440
466
  end
441
467
 
442
- it "should not be able to like an object" do
468
+ it "can't like an object" do
443
469
  lambda { @api.put_like("7204941866_119776748033392") }.should raise_error(Koala::Facebook::APIError)
444
470
  end
445
471
 
446
472
  # DELETE
447
- it "should not be able to delete posts" do
473
+ it "can't delete posts" do
448
474
  # test post on the Ruby SDK Test application
449
475
  lambda { @result = @api.delete_object("115349521819193_113815981982767") }.should raise_error(Koala::Facebook::APIError)
450
476
  end
451
477
 
452
- it "should not be able to delete a like" do
478
+ it "can't delete a like" do
453
479
  lambda { @api.delete_like("7204941866_119776748033392") }.should raise_error(Koala::Facebook::APIError)
454
480
  end
455
481
  end
@@ -57,16 +57,16 @@ module KoalaTest
57
57
  end
58
58
 
59
59
  config.after :each do
60
- # clean up any objects posted to Facebook
61
- if @temporary_object_id && !KoalaTest.mock_interface?
62
- api = @api || (@test_users ? @test_users.graph_api : nil)
63
- raise "Unable to locate API when passed temporary object to delete!" unless api
60
+ # if we're working with a real user, clean up any objects posted to Facebook
61
+ # no need to do so for test users, since they get deleted at the end
62
+ if @temporary_object_id && KoalaTest.real_user?
63
+ raise "Unable to locate API when passed temporary object to delete!" unless @api
64
64
 
65
65
  # wait 10ms to allow Facebook to propagate data so we can delete it
66
66
  sleep(0.01)
67
67
 
68
68
  # clean up any objects we've posted
69
- result = (api.delete_object(@temporary_object_id) rescue false)
69
+ result = (@api.delete_object(@temporary_object_id) rescue false)
70
70
  # if we errored out or Facebook returned false, track that
71
71
  puts "Unable to delete #{@temporary_object_id}: #{result} (probably a photo or video, which can't be deleted through the API)" unless result
72
72
  end
@@ -94,19 +94,16 @@ module KoalaTest
94
94
  end
95
95
 
96
96
  def self.setup_test_users
97
- # note: we don't have to delete the two test users explicitly, since the test user specs do that for us
98
- # technically, this is a point of brittleness and would break if the tests were run out of order
99
- # however, for now we can live with it since it would slow tests way too much to constantly recreate our test users
100
97
  print "Setting up test users..."
101
98
  @test_user_api = Koala::Facebook::TestUsers.new(:app_id => self.app_id, :secret => self.secret)
102
99
 
103
100
  RSpec.configure do |config|
104
- config.before :all do
101
+ config.before :suite do
105
102
  # before each test module, create two test users with specific names and befriend them
106
103
  KoalaTest.create_test_users
107
104
  end
108
105
 
109
- config.after :all do
106
+ config.after :suite do
110
107
  # after each test module, delete the test users to avoid cluttering up the application
111
108
  KoalaTest.destroy_test_users
112
109
  end
@@ -116,10 +113,15 @@ module KoalaTest
116
113
  end
117
114
 
118
115
  def self.create_test_users
119
- @live_testing_user = @test_user_api.create(true, KoalaTest.testing_permissions, :name => KoalaTest.user1_name)
120
- @live_testing_friend = @test_user_api.create(true, KoalaTest.testing_permissions, :name => KoalaTest.user2_name)
121
- @test_user_api.befriend(@live_testing_user, @live_testing_friend)
122
- self.oauth_token = @live_testing_user["access_token"]
116
+ begin
117
+ @live_testing_user = @test_user_api.create(true, KoalaTest.testing_permissions, :name => KoalaTest.user1_name)
118
+ @live_testing_friend = @test_user_api.create(true, KoalaTest.testing_permissions, :name => KoalaTest.user2_name)
119
+ @test_user_api.befriend(@live_testing_user, @live_testing_friend)
120
+ self.oauth_token = @live_testing_user["access_token"]
121
+ rescue Exception => e
122
+ Kernel.warn("Problem creating test users! #{e.message}")
123
+ raise
124
+ end
123
125
  end
124
126
 
125
127
  def self.destroy_test_users
@@ -144,7 +146,7 @@ module KoalaTest
144
146
 
145
147
  # Info about the testing environment
146
148
  def self.real_user?
147
- !(mock_interface? || @test_user)
149
+ !(mock_interface? || @test_user_api)
148
150
  end
149
151
 
150
152
  def self.test_user?