koala 1.2.0beta1 → 1.2.1

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.
@@ -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,22 +15,41 @@ 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
-
21
20
  @api.should_receive(:api).with(*args)
22
-
23
21
  @api.graph_call(*args)
24
22
  end
25
23
 
26
- 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
27
25
  Koala.stub(:make_request).and_return(Koala::Response.new(500, {"error" => "An error occurred!"}, {}))
28
26
  lambda { @api.graph_call(KoalaTest.user1, {}) }.should raise_exception(Koala::Facebook::APIError)
29
27
  end
28
+
29
+ it "passes the results through GraphCollection.evaluate" do
30
+ result = {}
31
+ @api.stub(:api).and_return(result)
32
+ Koala::Facebook::GraphCollection.should_receive(:evaluate).with(result, @api)
33
+ @api.graph_call("/me")
34
+ end
35
+
36
+ it "returns the results of GraphCollection.evaluate" do
37
+ expected = {}
38
+ @api.stub(:api).and_return([])
39
+ Koala::Facebook::GraphCollection.should_receive(:evaluate).and_return(expected)
40
+ @api.graph_call("/me").should == expected
41
+ end
42
+
43
+ it "returns the post_processing block's results if one is supplied" do
44
+ other_result = [:a, 2, :three]
45
+ block = Proc.new {|r| other_result}
46
+ @api.stub(:api).and_return({})
47
+ @api.graph_call("/me", {}, "get", {}, &block).should == other_result
48
+ end
30
49
  end
31
50
 
32
51
  # SEARCH
33
- it "should be able to search" do
52
+ it "can search" do
34
53
  result = @api.search("facebook")
35
54
  result.length.should be_an(Integer)
36
55
  end
@@ -39,109 +58,116 @@ shared_examples_for "Koala GraphAPI" do
39
58
  # access public info
40
59
 
41
60
  # get_object
42
- it "should get public data about a user" do
61
+ it "gets public data about a user" do
43
62
  result = @api.get_object(KoalaTest.user1)
44
63
  # the results should have an ID and a name, among other things
45
64
  (result["id"] && result["name"]).should_not be_nil
46
65
  end
47
66
 
48
- it "should get public data about a Page" do
67
+ it "gets public data about a Page" do
49
68
  result = @api.get_object(KoalaTest.page)
50
69
  # the results should have an ID and a name, among other things
51
70
  (result["id"] && result["name"]).should
52
71
  end
53
72
 
54
- it "should return [] from get_objects if passed an empty array" do
73
+ it "returns [] from get_objects if passed an empty array" do
55
74
  results = @api.get_objects([])
56
75
  results.should == []
57
76
  end
58
77
 
59
- it "should be able to get multiple objects" do
78
+ it "gets multiple objects" do
60
79
  results = @api.get_objects([KoalaTest.page, KoalaTest.user1])
61
80
  results.should have(2).items
62
81
  end
63
82
 
64
- 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
65
84
  results = @api.get_objects("contextoptional,#{KoalaTest.user1}")
66
85
  results.should have(2).items
67
86
  end
68
87
 
69
- it "should be able to access a user's picture" do
88
+ it "can access a user's picture" do
70
89
  @api.get_picture("chris.baclig").should =~ /http[s]*\:\/\//
71
90
  end
72
91
 
73
- 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
74
93
  @api.get_picture(KoalaTest.user2, {:type => 'large'}).should =~ /^http[s]*\:\/\//
75
94
  end
76
95
 
77
- it "should be able to access connections from public Pages" do
96
+ it "can access connections from public Pages" do
78
97
  result = @api.get_connections(KoalaTest.page, "photos")
79
98
  result.should be_a(Array)
80
99
  end
81
100
 
82
- it "should be able to access comments for a URL" do
101
+ it "can access comments for a URL" do
83
102
  result = @api.get_comments_for_urls(["http://developers.facebook.com/blog/post/472"])
84
103
  (result["http://developers.facebook.com/blog/post/472"]).should
85
104
  end
86
105
 
87
- it "should be able to access comments for 2 URLs" do
106
+ it "can access comments for 2 URLs" do
88
107
  result = @api.get_comments_for_urls(["http://developers.facebook.com/blog/post/490", "http://developers.facebook.com/blog/post/472"])
89
108
  (result["http://developers.facebook.com/blog/post/490"] && result["http://developers.facebook.com/blog/post/472"]).should
90
109
  end
91
110
 
92
111
  # SEARCH
93
- it "should be able to search" do
112
+ it "can search" do
94
113
  result = @api.search("facebook")
95
114
  result.length.should be_an(Integer)
96
115
  end
97
116
 
98
117
  # PAGING THROUGH COLLECTIONS
99
118
  # see also graph_collection_tests
100
- 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
101
120
  query = [1, 2]
102
121
  @api.should_receive(:graph_call).with(*query)
103
122
  @api.get_page(query)
104
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
105
131
  end
106
132
 
107
133
 
108
134
  shared_examples_for "Koala GraphAPI with an access token" do
109
- it "should get private data about a user" do
135
+ it "gets private data about a user" do
110
136
  result = @api.get_object(KoalaTest.user1)
111
137
  # updated_time should be a pretty fixed test case
112
138
  result["updated_time"].should_not be_nil
113
139
  end
114
140
 
115
- it "should get data about 'me'" do
141
+ it "gets data about 'me'" do
116
142
  result = @api.get_object("me")
117
143
  result["updated_time"].should
118
144
  end
119
145
 
120
- it "should be able to get multiple objects" do
146
+ it "gets multiple objects" do
121
147
  result = @api.get_objects([KoalaTest.page, KoalaTest.user1])
122
148
  result.length.should == 2
123
149
  end
124
- it "should be able to access connections from users" do
150
+ it "can access connections from users" do
125
151
  result = @api.get_connections(KoalaTest.user2, "friends")
126
152
  result.length.should > 0
127
153
  end
128
154
 
129
155
  # PUT
130
- it "should be able to write an object to the graph" do
156
+ it "can write an object to the graph" do
131
157
  result = @api.put_wall_post("Hello, world, from the test suite!")
132
158
  @temporary_object_id = result["id"]
133
159
  @temporary_object_id.should_not be_nil
134
160
  end
135
161
 
136
162
  # DELETE
137
- it "should be able to delete posts" do
163
+ it "can delete posts" do
138
164
  result = @api.put_wall_post("Hello, world, from the test suite delete method!")
139
165
  object_id_to_delete = result["id"]
140
166
  delete_result = @api.delete_object(object_id_to_delete)
141
167
  delete_result.should == true
142
168
  end
143
169
 
144
- it "should be able to delete likes" do
170
+ it "can delete likes" do
145
171
  result = @api.put_wall_post("Hello, world, from the test suite delete method!")
146
172
  @temporary_object_id = result["id"]
147
173
  @api.put_like(@temporary_object_id)
@@ -150,7 +176,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
150
176
  end
151
177
 
152
178
  # additional put tests
153
- it "should be able to verify messages posted to a wall" do
179
+ it "can verify messages posted to a wall" do
154
180
  message = "the cats are asleep"
155
181
  put_result = @api.put_wall_post(message)
156
182
  @temporary_object_id = put_result["id"]
@@ -160,14 +186,33 @@ shared_examples_for "Koala GraphAPI with an access token" do
160
186
  get_result["message"].should == message
161
187
  end
162
188
 
163
- 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
164
190
  result = @api.put_wall_post("Hello, world, from the test suite again!", {:name => "OAuth Playground", :link => "http://oauth.twoalex.com/"})
165
191
  @temporary_object_id = result["id"]
166
192
  @temporary_object_id.should_not be_nil
167
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
+
168
213
 
169
214
  describe ".put_picture" do
170
- 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
171
216
  content_type = "image/jpg"
172
217
  file = File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "beach.jpg"))
173
218
 
@@ -176,7 +221,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
176
221
  @temporary_object_id.should_not be_nil
177
222
  end
178
223
 
179
- 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
180
225
  content_type = "image/jpg",
181
226
  file_path = File.join(File.dirname(__FILE__), "..", "fixtures", "beach.jpg")
182
227
 
@@ -185,7 +230,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
185
230
  @temporary_object_id.should_not be_nil
186
231
  end
187
232
 
188
- 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
189
234
  content_type = "image/jpg",
190
235
  file_path = File.join(File.dirname(__FILE__), "..", "fixtures", "beach.jpg")
191
236
 
@@ -205,13 +250,13 @@ shared_examples_for "Koala GraphAPI with an access token" do
205
250
  @url = "http://img.slate.com/images/redesign2008/slate_logo.gif"
206
251
  end
207
252
 
208
- 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
209
254
  result = @api.put_picture(@url)
210
255
  @temporary_object_id = result["id"]
211
256
  @temporary_object_id.should_not be_nil
212
257
  end
213
258
 
214
- 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
215
260
  result = @api.put_picture(@url, :message => "my message")
216
261
  @temporary_object_id = result["id"]
217
262
  @temporary_object_id.should_not be_nil
@@ -225,7 +270,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
225
270
  @content_type = "video/mpeg4"
226
271
  end
227
272
 
228
- it "should set options[:video] to true" do
273
+ it "sets options[:video] to true" do
229
274
  source = stub("UploadIO")
230
275
  Koala::UploadableIO.stub(:new).and_return(source)
231
276
  source.stub(:requires_base_http_service).and_return(false)
@@ -233,7 +278,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
233
278
  @api.put_video("foo")
234
279
  end
235
280
 
236
- 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
237
282
  file = File.open(@cat_movie)
238
283
 
239
284
  result = @api.put_video(file, @content_type)
@@ -242,7 +287,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
242
287
  end
243
288
 
244
289
 
245
- 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
246
291
  result = @api.put_video(@cat_movie, @content_type)
247
292
  @temporary_object_id = result["id"]
248
293
  @temporary_object_id.should_not be_nil
@@ -253,7 +298,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
253
298
  # hence we can't do the same verify test we do for photos
254
299
  end
255
300
 
256
- 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
257
302
  attachment = {"name" => "OAuth Playground", "link" => "http://oauth.twoalex.com/"}
258
303
  result = @api.put_wall_post("Hello, world, from the test suite again!", attachment)
259
304
  @temporary_object_id = result["id"]
@@ -264,7 +309,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
264
309
  it_matches.should == true
265
310
  end
266
311
 
267
- it "should be able to comment on an object" do
312
+ it "can comment on an object" do
268
313
  result = @api.put_wall_post("Hello, world, from the test suite, testing comments!")
269
314
  @temporary_object_id = result["id"]
270
315
 
@@ -273,8 +318,8 @@ shared_examples_for "Koala GraphAPI with an access token" do
273
318
  comment_result.should_not be_nil
274
319
  end
275
320
 
276
- it "should be able to verify a comment posted about an object" do
277
- 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!"
278
323
  result = @api.put_wall_post(message_text)
279
324
  @temporary_object_id = result["id"]
280
325
 
@@ -287,8 +332,8 @@ shared_examples_for "Koala GraphAPI with an access token" do
287
332
  get_result["message"].should == comment_text
288
333
  end
289
334
 
290
- it "should be able to like an object" do
291
- 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!")
292
337
  @temporary_object_id = result["id"]
293
338
  like_result = @api.put_like(@temporary_object_id)
294
339
  like_result.should be_true
@@ -323,7 +368,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
323
368
  :put_video => ["x.mp4", "video/mpeg4", {}, "me"],
324
369
  :get_objects => [["x"], {}]
325
370
  }.each_pair do |method_name, params|
326
- it "should pass http options through for #{method_name}" do
371
+ it "passes http options through for #{method_name}" do
327
372
  options = {:a => 2}
328
373
  # graph call should ultimately receive options as the fourth argument
329
374
  @api.should_receive(:graph_call).with(anything, anything, anything, options)
@@ -337,7 +382,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
337
382
  end
338
383
 
339
384
  # also test get_picture, which merges a parameter into options
340
- it "should pass http options through for get_picture" do
385
+ it "passes http options through for get_picture" do
341
386
  options = {:a => 2}
342
387
  # graph call should ultimately receive options as the fourth argument
343
388
  @api.should_receive(:graph_call).with(anything, anything, anything, hash_including(options)).and_return({})
@@ -349,105 +394,39 @@ end
349
394
 
350
395
  # GraphCollection
351
396
  shared_examples_for "Koala GraphAPI with GraphCollection" do
352
-
353
- it "should create an array-like object" do
354
- call = @api.graph_call("contextoptional/photos")
355
- Koala::Facebook::GraphCollection.new(call, @api).should be_an(Array)
356
- end
357
-
358
397
  describe "when getting a collection" do
359
398
  # GraphCollection methods
360
- it "should get a GraphCollection when getting connections" do
399
+ it "gets a GraphCollection when getting connections" do
361
400
  @result = @api.get_connections(KoalaTest.page, "photos")
362
401
  @result.should be_a(Koala::Facebook::GraphCollection)
363
402
  end
364
403
 
365
- 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
366
405
  # this happens sometimes
367
406
  @api.should_receive(:graph_call).and_return(nil)
368
407
  @api.get_connections(KoalaTest.page, "photos").should be_nil
369
408
  end
370
409
 
371
- it "should get a GraphCollection when searching" do
410
+ it "gets a GraphCollection when searching" do
372
411
  result = @api.search("facebook")
373
412
  result.should be_a(Koala::Facebook::GraphCollection)
374
413
  end
375
414
 
376
- it "should return nil if the search call fails with nil" do
415
+ it "returns nil if the search call fails with nil" do
377
416
  # this happens sometimes
378
417
  @api.should_receive(:graph_call).and_return(nil)
379
418
  @api.search("facebook").should be_nil
380
419
  end
381
420
 
382
- it "should get a GraphCollection when paging through results" do
383
- @results = @api.get_page(["search", {"q"=>"facebook", "limit"=>"25", "until"=>"2010-09-23T21:17:33+0000"}])
421
+ it "gets a GraphCollection when paging through results" do
422
+ @results = @api.get_page(["search", {"q"=>"facebook", "limit"=>"25", "until"=> KoalaTest.search_time}])
384
423
  @results.should be_a(Koala::Facebook::GraphCollection)
385
424
  end
386
425
 
387
- it "should return nil if the page call fails with nil" do
426
+ it "returns nil if the page call fails with nil" do
388
427
  # this happens sometimes
389
428
  @api.should_receive(:graph_call).and_return(nil)
390
- @api.get_page(["search", {"q"=>"facebook", "limit"=>"25", "until"=>"2010-09-23T21:17:33+0000"}]).should be_nil
391
- end
392
-
393
- # GraphCollection attributes
394
- describe "the GraphCollection" do
395
- before(:each) do
396
- @result = @api.get_connections(KoalaTest.page, "photos")
397
- end
398
-
399
- it "should have a read-only paging attribute" do
400
- @result.methods.map(&:to_sym).should include(:paging)
401
- @result.methods.map(&:to_sym).should_not include(:paging=)
402
- end
403
-
404
- describe "when getting a whole page" do
405
- before(:each) do
406
- @second_page = stub("page of Fb graph results")
407
- @base = stub("base")
408
- @args = stub("args")
409
- @page_of_results = stub("page of results")
410
- end
411
-
412
- it "should return the previous page of results" do
413
- @result.should_receive(:previous_page_params).and_return([@base, @args])
414
- @api.should_receive(:graph_call).with(@base, @args).and_yield(@second_page)
415
- Koala::Facebook::GraphCollection.should_receive(:new).with(@second_page, @api).and_return(@page_of_results)
416
-
417
- @result.previous_page#.should == @page_of_results
418
- end
419
-
420
- it "should return the next page of results" do
421
- @result.should_receive(:next_page_params).and_return([@base, @args])
422
- @api.should_receive(:graph_call).with(@base, @args).and_yield(@second_page)
423
- Koala::Facebook::GraphCollection.should_receive(:new).with(@second_page, @api).and_return(@page_of_results)
424
-
425
- @result.next_page#.should == @page_of_results
426
- end
427
-
428
- it "should return nil it there are no other pages" do
429
- %w{next previous}.each do |this|
430
- @result.should_receive("#{this}_page_params".to_sym).and_return(nil)
431
- @result.send("#{this}_page").should == nil
432
- end
433
- end
434
- end
435
-
436
- describe "when parsing page paramters" do
437
- before(:each) do
438
- @graph_collection = Koala::Facebook::GraphCollection.new({"data" => []}, Koala::Facebook::API.new)
439
- end
440
-
441
- it "should return the base as the first array entry" do
442
- base = "url_path"
443
- @graph_collection.parse_page_url("anything.com/#{base}?anything").first.should == base
444
- end
445
-
446
- it "should return the arguments as a hash as the last array entry" do
447
- args_hash = {"one" => "val_one", "two" => "val_two"}
448
- @graph_collection.parse_page_url("anything.com/anything?#{args_hash.map {|k,v| "#{k}=#{v}" }.join("&")}").last.should == args_hash
449
- end
450
- end
429
+ @api.get_page(["search", {"q"=>"facebook", "limit"=>"25", "until"=> KoalaTest.search_time}]).should be_nil
451
430
  end
452
431
  end
453
432
  end
@@ -455,48 +434,48 @@ end
455
434
 
456
435
  shared_examples_for "Koala GraphAPI without an access token" do
457
436
 
458
- it "should not get private data about a user" do
437
+ it "can't get private data about a user" do
459
438
  result = @api.get_object("koppel")
460
439
  # updated_time should be a pretty fixed test case
461
440
  result["updated_time"].should be_nil
462
441
  end
463
442
 
464
- it "should not be able to get data about 'me'" do
443
+ it "can't get data about 'me'" do
465
444
  lambda { @api.get_object("me") }.should raise_error(Koala::Facebook::APIError)
466
445
  end
467
446
 
468
- it "shouldn't be able to access connections from users" do
447
+ it "can't access connections from users" do
469
448
  lambda { @api.get_connections("lukeshepard", "friends") }.should raise_error(Koala::Facebook::APIError)
470
449
  end
471
450
 
472
- it "should not be able to put an object" do
451
+ it "can't put an object" do
473
452
  lambda { @result = @api.put_object("lukeshepard", "feed", :message => "Hello, world") }.should raise_error(Koala::Facebook::APIError)
474
453
  end
475
454
 
476
455
  # these are not strictly necessary as the other put methods resolve to put_object, but are here for completeness
477
- it "should not be able to post to a feed" do
456
+ it "can't post to a feed" do
478
457
  (lambda do
479
458
  attachment = {:name => "OAuth Playground", :link => "http://oauth.twoalex.com/"}
480
459
  @result = @api.put_wall_post("Hello, world", attachment, "contextoptional")
481
460
  end).should raise_error(Koala::Facebook::APIError)
482
461
  end
483
462
 
484
- it "should not be able to comment on an object" do
463
+ it "can't comment on an object" do
485
464
  # random public post on the ContextOptional wall
486
465
  lambda { @result = @api.put_comment("7204941866_119776748033392", "The hackathon was great!") }.should raise_error(Koala::Facebook::APIError)
487
466
  end
488
467
 
489
- it "should not be able to like an object" do
468
+ it "can't like an object" do
490
469
  lambda { @api.put_like("7204941866_119776748033392") }.should raise_error(Koala::Facebook::APIError)
491
470
  end
492
471
 
493
472
  # DELETE
494
- it "should not be able to delete posts" do
473
+ it "can't delete posts" do
495
474
  # test post on the Ruby SDK Test application
496
475
  lambda { @result = @api.delete_object("115349521819193_113815981982767") }.should raise_error(Koala::Facebook::APIError)
497
476
  end
498
477
 
499
- it "should not be able to delete a like" do
478
+ it "can't delete a like" do
500
479
  lambda { @api.delete_like("7204941866_119776748033392") }.should raise_error(Koala::Facebook::APIError)
501
480
  end
502
481
  end
@@ -3,7 +3,7 @@ module KoalaTest
3
3
 
4
4
  class << self
5
5
  attr_accessor :oauth_token, :app_id, :secret, :app_access_token, :code, :session_key
6
- attr_accessor :oauth_test_data, :subscription_test_data
6
+ attr_accessor :oauth_test_data, :subscription_test_data, :search_time
7
7
  end
8
8
 
9
9
  # Test setup
@@ -35,6 +35,8 @@ module KoalaTest
35
35
  rescue LoadError
36
36
  puts "Unable to load adapter #{adapter}, using Net::HTTP."
37
37
  end
38
+
39
+ Koala.http_service.http_options[:beta] = true if ENV["beta"] || ENV["BETA"]
38
40
 
39
41
  # use a test user unless the developer wants to test against a real profile
40
42
  unless token = KoalaTest.oauth_token
@@ -55,16 +57,16 @@ module KoalaTest
55
57
  end
56
58
 
57
59
  config.after :each do
58
- # clean up any objects posted to Facebook
59
- if @temporary_object_id && !KoalaTest.mock_interface?
60
- api = @api || (@test_users ? @test_users.graph_api : nil)
61
- 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
62
64
 
63
65
  # wait 10ms to allow Facebook to propagate data so we can delete it
64
66
  sleep(0.01)
65
67
 
66
68
  # clean up any objects we've posted
67
- result = (api.delete_object(@temporary_object_id) rescue false)
69
+ result = (@api.delete_object(@temporary_object_id) rescue false)
68
70
  # if we errored out or Facebook returned false, track that
69
71
  puts "Unable to delete #{@temporary_object_id}: #{result} (probably a photo or video, which can't be deleted through the API)" unless result
70
72
  end
@@ -82,6 +84,9 @@ module KoalaTest
82
84
  self.secret = data["oauth_test_data"]["secret"]
83
85
  self.code = data["oauth_test_data"]["code"]
84
86
  self.session_key = data["oauth_test_data"]["session_key"]
87
+
88
+ # fix the search time so it can be used in the mock responses
89
+ self.search_time = data["search_time"] || (Time.now - 3600).to_s
85
90
  end
86
91
 
87
92
  def self.testing_permissions
@@ -89,21 +94,42 @@ module KoalaTest
89
94
  end
90
95
 
91
96
  def self.setup_test_users
92
- # note: we don't have to delete the two test users explicitly, since the test user specs do that for us
93
- # technically, this is a point of brittleness and would break if the tests were run out of order
94
- # however, for now we can live with it since it would slow tests way too much to constantly recreate our test users
95
97
  print "Setting up test users..."
96
98
  @test_user_api = Koala::Facebook::TestUsers.new(:app_id => self.app_id, :secret => self.secret)
97
99
 
98
- # create two test users with specific names and befriend them
99
- @live_testing_user = @test_user_api.create(true, testing_permissions, :name => user1_name)
100
- @live_testing_friend = @test_user_api.create(true, testing_permissions, :name => user2_name)
101
- @test_user_api.befriend(@live_testing_user, @live_testing_friend)
102
- self.oauth_token = @live_testing_user["access_token"]
100
+ RSpec.configure do |config|
101
+ config.before :suite do
102
+ # before each test module, create two test users with specific names and befriend them
103
+ KoalaTest.create_test_users
104
+ end
105
+
106
+ config.after :suite do
107
+ # after each test module, delete the test users to avoid cluttering up the application
108
+ KoalaTest.destroy_test_users
109
+ end
110
+ end
103
111
 
104
112
  puts "done."
105
113
  end
106
114
 
115
+ def self.create_test_users
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
125
+ end
126
+
127
+ def self.destroy_test_users
128
+ [@live_testing_user, @live_testing_friend].each do |u|
129
+ puts "Unable to delete test user #{u.inspect}" if u && !(@test_user_api.delete(u) rescue false)
130
+ end
131
+ end
132
+
107
133
  def self.validate_user_info(token)
108
134
  print "Validating permissions for live testing..."
109
135
  # make sure we have the necessary permissions
@@ -120,7 +146,7 @@ module KoalaTest
120
146
 
121
147
  # Info about the testing environment
122
148
  def self.real_user?
123
- !(mock_interface? || @test_user)
149
+ !(mock_interface? || @test_user_api)
124
150
  end
125
151
 
126
152
  def self.test_user?
@@ -18,7 +18,8 @@ module Koala
18
18
  TEST_DATA = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'fixtures', 'facebook_data.yml'))
19
19
  TEST_DATA.merge!('oauth_token' => Koala::MockHTTPService::ACCESS_TOKEN)
20
20
  TEST_DATA['oauth_test_data'].merge!('code' => Koala::MockHTTPService::OAUTH_CODE)
21
-
21
+ TEST_DATA['search_time'] = (Time.now - 3600).to_s
22
+
22
23
  # Useful in mock_facebook_responses.yml
23
24
  OAUTH_DATA = TEST_DATA['oauth_test_data']
24
25
  OAUTH_DATA.merge!({