koala 1.2.1 → 1.3.0rc1

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.
@@ -21,6 +21,9 @@ end
21
21
  # See https://github.com/tenderlove/psych/issues/8 for more details
22
22
  YAML::ENGINE.yamler = 'syck' if RUBY_VERSION == '1.9.2' && RUBY_PATCHLEVEL < 290
23
23
 
24
+ # load custom RSpec matchers
25
+ require 'support/custom_matchers'
26
+
24
27
  # load the library
25
28
  require 'koala'
26
29
 
@@ -0,0 +1,28 @@
1
+ # Verifies that two URLs are equal, ignoring the order of the query string parameters
2
+ RSpec::Matchers.define :match_url do |url|
3
+ match do |original_url|
4
+ base, query_string = url.split("?")
5
+ original_base, original_query_string = original_url.split("?")
6
+ query_hash = query_to_params(query_string)
7
+ original_query_hash = query_to_params(original_query_string)
8
+
9
+ # the base URLs need to match
10
+ base.should == original_base
11
+
12
+ # the number of parameters should match (avoid one being a subset of the other)
13
+ query_hash.values.length.should == original_query_hash.values.length
14
+
15
+ # and ensure all the keys and values match
16
+ query_hash.each_pair do |key, value|
17
+ original_query_hash[key].should == value
18
+ end
19
+ end
20
+
21
+ def query_to_params(query_string)
22
+ query_string.split("&").inject({}) do |hash, segment|
23
+ k, v = segment.split("=")
24
+ hash[k] = v
25
+ hash
26
+ end
27
+ end
28
+ end
@@ -86,7 +86,7 @@ shared_examples_for "Koala GraphAPI" do
86
86
  end
87
87
 
88
88
  it "can access a user's picture" do
89
- @api.get_picture("chris.baclig").should =~ /http[s]*\:\/\//
89
+ @api.get_picture(KoalaTest.user2).should =~ /http[s]*\:\/\//
90
90
  end
91
91
 
92
92
  it "can access a user's picture, given a picture type" do
@@ -128,6 +128,63 @@ shared_examples_for "Koala GraphAPI" do
128
128
  # the results should have an ID and a name, among other things
129
129
  (result["id"] && result["name"]).should_not be_nil
130
130
  end
131
+
132
+ # FQL
133
+ describe "#fql_query" do
134
+ it "makes a request to /fql" do
135
+ @api.should_receive(:get_object).with("fql", anything, anything)
136
+ @api.fql_query stub('query string')
137
+ end
138
+
139
+ it "passes a query argument" do
140
+ query = stub('query string')
141
+ @api.should_receive(:get_object).with(anything, hash_including(:q => query), anything)
142
+ @api.fql_query(query)
143
+ end
144
+
145
+ it "passes on any other arguments provided" do
146
+ args = {:a => 2}
147
+ @api.should_receive(:get_object).with(anything, hash_including(args), anything)
148
+ @api.fql_query("a query", args)
149
+ end
150
+ end
151
+
152
+ describe "#fql_multiquery" do
153
+ it "makes a request to /fql" do
154
+ @api.should_receive(:get_object).with("fql", anything, anything)
155
+ @api.fql_multiquery 'query string'
156
+ end
157
+
158
+ it "passes a queries argument" do
159
+ queries = stub('query string')
160
+ queries_json = "some JSON"
161
+ MultiJson.stub(:encode).with(queries).and_return(queries_json)
162
+
163
+ @api.should_receive(:get_object).with(anything, hash_including(:q => queries_json), anything)
164
+ @api.fql_multiquery(queries)
165
+ end
166
+
167
+ it "simplifies the response format" do
168
+ raw_results = [
169
+ {"name" => "query1", "fql_result_set" => [1, 2, 3]},
170
+ {"name" => "query2", "fql_result_set" => [:a, :b, :c]}
171
+ ]
172
+ expected_results = {
173
+ "query1" => [1, 2, 3],
174
+ "query2" => [:a, :b, :c]
175
+ }
176
+
177
+ @api.stub(:get_object).and_return(raw_results)
178
+ results = @api.fql_multiquery({:query => true})
179
+ results.should == expected_results
180
+ end
181
+
182
+ it "passes on any other arguments provided" do
183
+ args = {:a => 2}
184
+ @api.should_receive(:get_object).with(anything, hash_including(args), anything)
185
+ @api.fql_multiquery("a query", args)
186
+ end
187
+ end
131
188
  end
132
189
 
133
190
 
@@ -211,7 +268,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
211
268
  end
212
269
 
213
270
 
214
- describe ".put_picture" do
271
+ describe "#put_picture" do
215
272
  it "can post photos to the user's wall with an open file object" do
216
273
  content_type = "image/jpg"
217
274
  file = File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "beach.jpg"))
@@ -264,7 +321,7 @@ shared_examples_for "Koala GraphAPI with an access token" do
264
321
  end
265
322
  end
266
323
 
267
- describe ".put_video" do
324
+ describe "#put_video" do
268
325
  before :each do
269
326
  @cat_movie = File.join(File.dirname(__FILE__), "..", "fixtures", "cat.m4v")
270
327
  @content_type = "video/mpeg4"
@@ -340,10 +397,90 @@ shared_examples_for "Koala GraphAPI with an access token" do
340
397
  end
341
398
 
342
399
  # Page Access Token Support
343
- it "gets a page's access token" do
344
- # we can't test this live since test users (or random real users) can't be guaranteed to have pages to manage
345
- @api.should_receive(:api).with("my_page", {:fields => "access_token"}, "get", anything)
346
- @api.get_page_access_token("my_page")
400
+ describe "#get_page_access_token" do
401
+ it "gets the page object with the access_token field" do
402
+ # we can't test this live since test users (or random real users) can't be guaranteed to have pages to manage
403
+ @api.should_receive(:api).with("my_page", hash_including({:fields => "access_token"}), "get", anything)
404
+ @api.get_page_access_token("my_page")
405
+ end
406
+
407
+ it "merges in any other arguments" do
408
+ # we can't test this live since test users (or random real users) can't be guaranteed to have pages to manage
409
+ args = {:a => 3}
410
+ @api.should_receive(:api).with("my_page", hash_including(args), "get", anything)
411
+ @api.get_page_access_token("my_page", args)
412
+ end
413
+ end
414
+
415
+ describe "#set_app_restrictions" do
416
+ before :all do
417
+ oauth = Koala::Facebook::OAuth.new(KoalaTest.app_id, KoalaTest.secret)
418
+ app_token = oauth.get_app_access_token
419
+ @app_api = Koala::Facebook::API.new(app_token)
420
+ @restrictions = {"age_distr" => "13+"}
421
+ end
422
+
423
+ it "makes a POST to /app_id" do
424
+ @app_api.should_receive(:graph_call).with(KoalaTest.app_id, anything, "post", anything)
425
+ @app_api.set_app_restrictions(KoalaTest.app_id, @restrictions)
426
+ end
427
+
428
+ it "JSON-encodes the restrictions" do
429
+ @app_api.should_receive(:graph_call).with(anything, hash_including(:restrictions => MultiJson.encode(@restrictions)), anything, anything)
430
+ @app_api.set_app_restrictions(KoalaTest.app_id, @restrictions)
431
+ end
432
+
433
+ it "includes the other arguments" do
434
+ args = {:a => 2}
435
+ @app_api.should_receive(:graph_call).with(anything, hash_including(args), anything, anything)
436
+ @app_api.set_app_restrictions(KoalaTest.app_id, @restrictions, args)
437
+ end
438
+
439
+ it "works" do
440
+ @app_api.set_app_restrictions(KoalaTest.app_id, @restrictions).should be_true
441
+ end
442
+ end
443
+
444
+ it "can access public information via FQL" do
445
+ result = @api.fql_query("select first_name from user where uid = #{KoalaTest.user2_id}")
446
+ result.size.should == 1
447
+ result.first['first_name'].should == KoalaTest.user2_name
448
+ end
449
+
450
+ it "can access public information via FQL.multiquery" do
451
+ result = @api.fql_multiquery(
452
+ :query1 => "select first_name from user where uid = #{KoalaTest.user2_id}",
453
+ :query2 => "select first_name from user where uid = #{KoalaTest.user1_id}"
454
+ )
455
+ result.size.should == 2
456
+ result["query1"].first['first_name'].should == KoalaTest.user2_name
457
+ result["query2"].first['first_name'].should == KoalaTest.user1_name
458
+ end
459
+
460
+ it "can access protected information via FQL" do
461
+ # Tests agains the permissions fql table
462
+
463
+ # get the current user's ID
464
+ # we're sneakily using the Graph API, which should be okay since it has its own tests
465
+ g = Koala::Facebook::API.new(@token)
466
+ id = g.get_object("me", :fields => "id")["id"]
467
+
468
+ # now send a query about your permissions
469
+ result = @api.fql_query("select read_stream from permissions where uid = #{id}")
470
+
471
+ result.size.should == 1
472
+ # we've verified that you have read_stream permissions, so we can test against that
473
+ result.first["read_stream"].should == 1
474
+ end
475
+
476
+ it "can access protected information via FQL.multiquery" do
477
+ result = @api.fql_multiquery(
478
+ :query1 => "select post_id from stream where source_id = me()",
479
+ :query2 => "select fromid from comment where post_id in (select post_id from #query1)",
480
+ :query3 => "select uid, name from user where uid in (select fromid from #query2)"
481
+ )
482
+ result.size.should == 3
483
+ result.keys.should include("query1", "query2", "query3")
347
484
  end
348
485
 
349
486
  # test all methods to make sure they pass data through to the API
@@ -363,7 +500,11 @@ shared_examples_for "Koala GraphAPI with an access token" do
363
500
  :put_comment => 3,
364
501
  :put_like => 2, :delete_like => 2,
365
502
  :search => 3,
503
+ :set_app_restrictions => 4,
504
+ :get_page_access_token => 3,
505
+ :fql_query => 3, :fql_multiquery => 3,
366
506
  # methods that have special arguments
507
+ :get_comments_for_urls => [["url1", "url2"], {}],
367
508
  :put_picture => ["x.jpg", "image/jpg", {}, "me"],
368
509
  :put_video => ["x.mp4", "video/mpeg4", {}, "me"],
369
510
  :get_objects => [["x"], {}]
@@ -433,9 +574,8 @@ end
433
574
 
434
575
 
435
576
  shared_examples_for "Koala GraphAPI without an access token" do
436
-
437
577
  it "can't get private data about a user" do
438
- result = @api.get_object("koppel")
578
+ result = @api.get_object(KoalaTest.user1)
439
579
  # updated_time should be a pretty fixed test case
440
580
  result["updated_time"].should be_nil
441
581
  end
@@ -445,11 +585,11 @@ shared_examples_for "Koala GraphAPI without an access token" do
445
585
  end
446
586
 
447
587
  it "can't access connections from users" do
448
- lambda { @api.get_connections("lukeshepard", "friends") }.should raise_error(Koala::Facebook::APIError)
588
+ lambda { @api.get_connections(KoalaTest.user2, "friends") }.should raise_error(Koala::Facebook::APIError)
449
589
  end
450
590
 
451
591
  it "can't put an object" do
452
- lambda { @result = @api.put_object("lukeshepard", "feed", :message => "Hello, world") }.should raise_error(Koala::Facebook::APIError)
592
+ lambda { @result = @api.put_object(KoalaTest.user2, "feed", :message => "Hello, world") }.should raise_error(Koala::Facebook::APIError)
453
593
  end
454
594
 
455
595
  # these are not strictly necessary as the other put methods resolve to put_object, but are here for completeness
@@ -478,4 +618,37 @@ shared_examples_for "Koala GraphAPI without an access token" do
478
618
  it "can't delete a like" do
479
619
  lambda { @api.delete_like("7204941866_119776748033392") }.should raise_error(Koala::Facebook::APIError)
480
620
  end
621
+
622
+ # FQL_QUERY
623
+ describe "when making a FQL request" do
624
+ it "can access public information via FQL" do
625
+ result = @api.fql_query("select first_name from user where uid = #{KoalaTest.user2_id}")
626
+ result.size.should == 1
627
+ result.first['first_name'].should == KoalaTest.user2_name
628
+ end
629
+
630
+ it "can access public information via FQL.multiquery" do
631
+ result = @api.fql_multiquery(
632
+ :query1 => "select first_name from user where uid = #{KoalaTest.user2_id}",
633
+ :query2 => "select first_name from user where uid = #{KoalaTest.user1_id}"
634
+ )
635
+ result.size.should == 2
636
+ result["query1"].first['first_name'].should == KoalaTest.user2_name
637
+ result["query2"].first['first_name'].should == KoalaTest.user1_name
638
+ end
639
+
640
+ it "can't access protected information via FQL" do
641
+ lambda { @api.fql_query("select read_stream from permissions where uid = #{KoalaTest.user2_id}") }.should raise_error(Koala::Facebook::APIError)
642
+ end
643
+
644
+ it "can't access protected information via FQL.multiquery" do
645
+ lambda {
646
+ @api.fql_multiquery(
647
+ :query1 => "select post_id from stream where source_id = me()",
648
+ :query2 => "select fromid from comment where post_id in (select post_id from #query1)",
649
+ :query3 => "select uid, name from user where uid in (select fromid from #query2)"
650
+ )
651
+ }.should raise_error(Koala::Facebook::APIError)
652
+ end
653
+ end
481
654
  end
@@ -4,6 +4,7 @@ module KoalaTest
4
4
  class << self
5
5
  attr_accessor :oauth_token, :app_id, :secret, :app_access_token, :code, :session_key
6
6
  attr_accessor :oauth_test_data, :subscription_test_data, :search_time
7
+ attr_accessor :test_user_api
7
8
  end
8
9
 
9
10
  # Test setup
@@ -159,10 +160,13 @@ module KoalaTest
159
160
 
160
161
  # Data for testing
161
162
  def self.user1
163
+ # user ID, either numeric or username
162
164
  test_user? ? @live_testing_user["id"] : "koppel"
163
165
  end
164
166
 
165
167
  def self.user1_id
168
+ # numerical ID, used for FQL
169
+ # (otherwise the two IDs are interchangeable)
166
170
  test_user? ? @live_testing_user["id"] : 2905623
167
171
  end
168
172
 
@@ -171,10 +175,12 @@ module KoalaTest
171
175
  end
172
176
 
173
177
  def self.user2
178
+ # see notes for user1
174
179
  test_user? ? @live_testing_friend["id"] : "lukeshepard"
175
180
  end
176
181
 
177
182
  def self.user2_id
183
+ # see notes for user1
178
184
  test_user? ? @live_testing_friend["id"] : 2901279
179
185
  end
180
186
 
@@ -186,4 +192,7 @@ module KoalaTest
186
192
  "contextoptional"
187
193
  end
188
194
 
195
+ def self.app_properties
196
+ mock_interface? ? {"desktop" => 0} : {"description" => "A test framework for Koala and its users. (#{rand(10000).to_i})"}
197
+ end
189
198
  end
@@ -113,144 +113,17 @@ shared_examples_for "Koala RestAPI" do
113
113
 
114
114
  it "throws an APIError if the result hash has an error key" do
115
115
  Koala.stub(:make_request).and_return(Koala::Response.new(500, {"error_code" => "An error occurred!"}, {}))
116
- lambda { @api.rest_call("koppel", {}) }.should raise_exception(Koala::Facebook::APIError)
117
- end
118
-
119
- describe "when making a FQL request" do
120
- it "calls fql.query method" do
121
- @api.should_receive(:rest_call).with(
122
- "fql.query", anything, anything
123
- ).and_return(Koala::Response.new(200, "2", {}))
124
-
125
- @api.fql_query stub('query string')
126
- end
127
-
128
- it "passes a query argument" do
129
- query = stub('query string')
130
-
131
- @api.should_receive(:rest_call).with(
132
- anything, hash_including(:query => query), anything
133
- )
134
-
135
- @api.fql_query(query)
136
- end
137
-
138
- it "passes on any other arguments provided" do
139
- args = {:a => 2}
140
- @api.should_receive(:rest_call).with(anything, hash_including(args), anything)
141
- @api.fql_query("a query", args)
142
- end
143
-
144
- it "passes on any http options provided" do
145
- opts = {:a => 2}
146
- @api.should_receive(:rest_call).with(anything, anything, hash_including(opts))
147
- @api.fql_query("a query", {}, opts)
148
- end
149
- end
150
-
151
- describe "when making a FQL-multiquery request" do
152
- it "calls fql.multiquery method" do
153
- @api.should_receive(:rest_call).with(
154
- "fql.multiquery", anything, anything
155
- ).and_return({})
156
-
157
- @api.fql_multiquery 'query string'
158
- end
159
-
160
- it "passes a queries argument" do
161
- queries = stub('query string')
162
- queries_json = "some JSON"
163
- MultiJson.stub(:encode).with(queries).and_return(queries_json)
164
-
165
- @api.should_receive(:rest_call).with(
166
- anything,
167
- hash_including(:queries => queries_json),
168
- anything
169
- )
170
-
171
- @api.fql_multiquery(queries)
172
- end
173
-
174
- it "simplifies the response format" do
175
- raw_results = [
176
- {"name" => "query1", "fql_result_set" => [1, 2, 3]},
177
- {"name" => "query2", "fql_result_set" => [:a, :b, :c]}
178
- ]
179
- expected_results = {
180
- "query1" => [1, 2, 3],
181
- "query2" => [:a, :b, :c]
182
- }
183
-
184
- @api.stub(:rest_call).and_return(raw_results)
185
- results = @api.fql_multiquery({:query => true})
186
- results.should == expected_results
187
- end
188
-
189
- it "passes on any other arguments provided" do
190
- args = {:a => 2}
191
- @api.should_receive(:rest_call).with(anything, hash_including(args), anything)
192
- @api.fql_multiquery("a query", args)
193
- end
194
-
195
- it "passes on any http options provided" do
196
- opts = {:a => 2}
197
- @api.should_receive(:rest_call).with(anything, anything, hash_including(opts))
198
- @api.fql_multiquery("a query", {}, opts)
199
- end
116
+ lambda { @api.rest_call(KoalaTest.user1, {}) }.should raise_exception(Koala::Facebook::APIError)
200
117
  end
201
118
  end
202
119
 
203
120
  it "can use the beta tier" do
204
- @api.fql_query("select first_name from user where uid = #{KoalaTest.user2_id}", {}, :beta => true)
121
+ @api.rest_call("fql.query", {:query => "select first_name from user where uid = #{KoalaTest.user2_id}"}, :beta => true)
205
122
  end
206
123
  end
207
124
 
208
125
  shared_examples_for "Koala RestAPI with an access token" do
209
- # FQL
210
- it "can access public information via FQL" do
211
- result = @api.fql_query("select first_name from user where uid = #{KoalaTest.user2_id}")
212
- result.size.should == 1
213
- result.first['first_name'].should == KoalaTest.user2_name
214
- end
215
-
216
- it "can access public information via FQL.multiquery" do
217
- result = @api.fql_multiquery(
218
- :query1 => "select first_name from user where uid = #{KoalaTest.user2_id}",
219
- :query2 => "select first_name from user where uid = #{KoalaTest.user1_id}"
220
- )
221
- result.size.should == 2
222
- result["query1"].first['first_name'].should == KoalaTest.user2_name
223
- result["query2"].first['first_name'].should == KoalaTest.user1_name
224
- end
225
-
226
- it "can access protected information via FQL" do
227
- # Tests agains the permissions fql table
228
-
229
- # get the current user's ID
230
- # we're sneakily using the Graph API, which should be okay since it has its own tests
231
- g = Koala::Facebook::API.new(@token)
232
- id = g.get_object("me", :fields => "id")["id"]
233
-
234
- # now send a query about your permissions
235
- result = @api.fql_query("select read_stream from permissions where uid = #{id}")
236
-
237
- result.size.should == 1
238
- # we've verified that you have read_stream permissions, so we can test against that
239
- result.first["read_stream"].should == 1
240
- end
241
-
242
- it "can access protected information via FQL.multiquery" do
243
- result = @api.fql_multiquery(
244
- :query1 => "select post_id from stream where source_id = me()",
245
- :query2 => "select fromid from comment where post_id in (select post_id from #query1)",
246
- :query3 => "select uid, name from user where uid in (select fromid from #query2)"
247
- )
248
- result.size.should == 3
249
- result.keys.should include("query1", "query2", "query3")
250
- end
251
-
252
-
253
- describe ".set_app_properties" do
126
+ describe "#set_app_properties" do
254
127
  it "sends Facebook the properties JSON-encoded as :properties" do
255
128
  props = {:a => 2, :c => [1, 2, "d"]}
256
129
  @api.should_receive(:rest_call).with(anything, hash_including(:properties => MultiJson.encode(props)), anything, anything)
@@ -283,46 +156,13 @@ shared_examples_for "Koala RestAPI with an access token" do
283
156
  oauth = Koala::Facebook::OAuth.new(KoalaTest.app_id, KoalaTest.secret)
284
157
  app_token = oauth.get_app_access_token
285
158
  @app_api = Koala::Facebook::API.new(app_token)
286
- @app_api.set_app_properties(:desktop => 0).should be_true
159
+ @app_api.set_app_properties(KoalaTest.app_properties).should be_true
287
160
  end
288
161
  end
289
162
  end
290
163
 
291
164
 
292
165
  shared_examples_for "Koala RestAPI without an access token" do
293
- # FQL_QUERY
294
- describe "when making a FQL request" do
295
- it "can access public information via FQL" do
296
- result = @api.fql_query("select first_name from user where uid = #{KoalaTest.user2_id}")
297
- result.size.should == 1
298
- result.first['first_name'].should == KoalaTest.user2_name
299
- end
300
-
301
- it "can access public information via FQL.multiquery" do
302
- result = @api.fql_multiquery(
303
- :query1 => "select first_name from user where uid = #{KoalaTest.user2_id}",
304
- :query2 => "select first_name from user where uid = #{KoalaTest.user1_id}"
305
- )
306
- result.size.should == 2
307
- result["query1"].first['first_name'].should == KoalaTest.user2_name
308
- result["query2"].first['first_name'].should == KoalaTest.user1_name
309
- end
310
-
311
- it "can't access protected information via FQL" do
312
- lambda { @api.fql_query("select read_stream from permissions where uid = #{KoalaTest.user2_id}") }.should raise_error(Koala::Facebook::APIError)
313
- end
314
-
315
- it "can't access protected information via FQL.multiquery" do
316
- lambda {
317
- @api.fql_multiquery(
318
- :query1 => "select post_id from stream where source_id = me()",
319
- :query2 => "select fromid from comment where post_id in (select post_id from #query1)",
320
- :query3 => "select uid, name from user where uid in (select fromid from #query2)"
321
- )
322
- }.should raise_error(Koala::Facebook::APIError)
323
- end
324
- end
325
-
326
166
  it "can't use set_app_properties" do
327
167
  lambda { @api.set_app_properties(:desktop => 0) }.should raise_error(Koala::Facebook::APIError)
328
168
  end