koala 0.10.0 → 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,156 @@
1
+ require 'koala/http_services'
2
+ class TyphoeusServiceTests < Test::Unit::TestCase
3
+ module Bear
4
+ include Koala::TyphoeusService
5
+ end
6
+
7
+ describe "TyphoeusService module holder class Bear" do
8
+ before :each do
9
+ # reset the always_use_ssl parameter
10
+ Bear.always_use_ssl = nil
11
+ end
12
+
13
+ it "should define a make_request static module method" do
14
+ Bear.respond_to?(:make_request).should be_true
15
+ end
16
+
17
+ it "should include the Koala::HTTPService module defining common features" do
18
+ Bear.included_modules.include?(Koala::HTTPService).should be_true
19
+ end
20
+
21
+ describe "when making a request" do
22
+ before(:each) do
23
+ # Setup stubs for make_request to execute without exceptions
24
+ @mock_body = stub('Typhoeus response body')
25
+ @mock_headers_hash = stub({:value => "headers hash"})
26
+ @mock_http_response = stub(Typhoeus::Response, :code => 1, :headers_hash => @mock_headers_hash, :body => @mock_body)
27
+
28
+ # Typhoeus is an included module, so we stub methods on Bear itself
29
+ Bear.stub(:post).and_return(@mock_http_response)
30
+ Bear.stub(:get).and_return(@mock_http_response)
31
+ end
32
+
33
+ it "should use POST if verb is not GET" do
34
+ Bear.should_receive(:post).and_return(@mock_http_response)
35
+ Bear.make_request('anything', {}, 'anything')
36
+ end
37
+
38
+ it "should use GET if that verb is specified" do
39
+ Bear.should_receive(:get).and_return(@mock_http_response)
40
+ Bear.make_request('anything', {}, 'get')
41
+ end
42
+
43
+ describe "the connection" do
44
+ it "should use SSL if the request has an access token" do
45
+ Bear.should_receive(:post).with(/https\:/, anything)
46
+
47
+ Bear.make_request('anything', {"access_token" => "123"}, 'anything')
48
+ end
49
+
50
+ it "should use SSL if always_use_ssl is true, even if there's no token" do
51
+ Bear.should_receive(:post).with(/https\:/, anything)
52
+
53
+ Bear.always_use_ssl = true
54
+ Bear.make_request('anything', {}, 'anything')
55
+ end
56
+
57
+ it "should use SSL if the :use_ssl option is provided, even if there's no token" do
58
+ Bear.should_receive(:post).with(/https\:/, anything)
59
+
60
+ Bear.always_use_ssl = true
61
+ Bear.make_request('anything', {}, 'anything', :use_ssl => true)
62
+ end
63
+
64
+ it "should not use SSL if always_use_ssl is false and there's no token" do
65
+ Bear.should_receive(:post).with(/http\:/, anything)
66
+
67
+ Bear.make_request('anything', {}, 'anything')
68
+ end
69
+
70
+ it "should use the graph server by default" do
71
+ Bear.should_receive(:post).with(Regexp.new(Koala::Facebook::GRAPH_SERVER), anything)
72
+
73
+ Bear.make_request('anything', {}, 'anything')
74
+ end
75
+
76
+ it "should use the REST server if the :rest_api option is true" do
77
+ Bear.should_receive(:post).with(Regexp.new(Koala::Facebook::REST_SERVER), anything)
78
+
79
+ Bear.make_request('anything', {}, 'anything', :rest_api => true)
80
+ end
81
+ end
82
+
83
+ it "should pass the arguments to Typhoeus under the :params key" do
84
+ args = {:a => 2}
85
+ Bear.should_receive(:post).with(anything, hash_including(:params => args))
86
+
87
+ Bear.make_request('anything', args, "post")
88
+ end
89
+
90
+ it "should add the method to the arguments if the method isn't get or post" do
91
+ method = "telekenesis"
92
+ Bear.should_receive(:post).with(anything, hash_including(:params => {:method => method}))
93
+
94
+ Bear.make_request('anything', {}, method)
95
+ end
96
+
97
+ it "should pass :typhoeus_options to Typhoeus if provided" do
98
+ t_options = {:a => :b}
99
+ Bear.should_receive(:post).with(anything, hash_including(t_options))
100
+
101
+ Bear.make_request("anything", {}, "post", :typhoeus_options => t_options)
102
+ end
103
+
104
+ it "should include the path in the request" do
105
+ path = "/a/b/c/1"
106
+ Bear.should_receive(:post).with(Regexp.new(path), anything)
107
+
108
+ Bear.make_request(path, {}, "post")
109
+ end
110
+
111
+ describe "the returned value" do
112
+ before(:each) do
113
+ @response = Bear.make_request('anything', {}, 'anything')
114
+ end
115
+
116
+ it "should return a Koala::Response object" do
117
+ @response.class.should == Koala::Response
118
+ end
119
+
120
+ it "should return a Koala::Response with the right status" do
121
+ @response.status.should == @mock_http_response.code
122
+ end
123
+
124
+ it "should reutrn a Koala::Response with the right body" do
125
+ @response.body.should == @mock_body
126
+ end
127
+
128
+ it "should return a Koala::Response with the Net::HTTPResponse object as headers" do
129
+ @response.headers.should == @mock_headers_hash
130
+ end
131
+ end # describe return value
132
+ end
133
+
134
+ describe "with file upload" do
135
+ it "should include an interface to NetHTTPService called NetHTTPInterface" do
136
+ # we should be able to access it
137
+ lambda { Koala::TyphoeusService::NetHTTPInterface }.should_not raise_exception(Exception)
138
+ Koala::TyphoeusService::NetHTTPInterface.included_modules.include?(Koala::NetHTTPService).should be_true
139
+ end
140
+
141
+ it "should call the NetHTTPInterface if multipart is required" do
142
+ method = "any_method"
143
+ args = {}
144
+ verb = "get"
145
+ options = {}
146
+
147
+ Bear.stub(:params_require_multipart?).and_return(true)
148
+ Koala::TyphoeusService::NetHTTPInterface.should_receive(:make_request).with(method, args, verb, options)
149
+
150
+ Bear.make_request(method, args, verb, options)
151
+ end
152
+
153
+ # for live tests, run the Graph API tests with Typhoues, which will run file uploads
154
+ end
155
+ end
156
+ end
@@ -5,7 +5,7 @@ if defined?(RUBY_VERSION) && RUBY_VERSION =~ /1\.9/
5
5
  Rspec.configure do |c|
6
6
  c.mock_with :rspec
7
7
  end
8
-
8
+
9
9
  else
10
10
  # Ruby 1.8.x
11
11
  require 'test/unit'
@@ -23,10 +23,12 @@ require 'koala/live_testing_data_helper'
23
23
  # API tests
24
24
  require 'koala/api_base_tests'
25
25
 
26
+ require 'koala/graph_api/graph_api_tests'
26
27
  require 'koala/graph_api/graph_collection_tests'
27
28
  require 'koala/graph_api/graph_api_no_access_token_tests'
28
29
  require 'koala/graph_api/graph_api_with_access_token_tests'
29
30
 
31
+ require 'koala/rest_api/rest_api_tests'
30
32
  require 'koala/rest_api/rest_api_no_access_token_tests'
31
33
  require 'koala/rest_api/rest_api_with_access_token_tests'
32
34
 
@@ -39,10 +41,30 @@ require 'koala/oauth/oauth_tests'
39
41
  # Subscriptions tests
40
42
  require 'koala/realtime_updates/realtime_updates_tests'
41
43
 
42
- # Services tests
43
- require 'koala/net_http_service_tests'
44
-
45
44
  # Test users tests
46
45
  require 'koala/test_users/test_users_tests'
47
46
 
47
+ # Services tests
48
+ require 'koala/net_http_service_tests'
49
+ begin
50
+ require 'koala/typhoeus_service_tests'
51
+ rescue LoadError
52
+ puts "Typhoeus tests will not be run because Typhoeus is not installed."
53
+ end
48
54
 
55
+ module KoalaTest
56
+ def self.validate_user_info(token)
57
+ print "Validating permissions for live testing..."
58
+ # make sure we have the necessary permissions
59
+ api = Koala::Facebook::GraphAndRestAPI.new(token)
60
+ uid = api.get_object("me")["id"]
61
+ perms = api.fql_query("select read_stream, publish_stream, user_photos from permissions where uid = #{uid}")[0]
62
+ perms.each_pair do |perm, value|
63
+ unless value == 1
64
+ puts "failed!\n" # put a new line after the print above
65
+ raise ArgumentError, "Your access token must have the read_stream, publish_stream, and user_photos permissions. You have: #{perms.inspect}"
66
+ end
67
+ end
68
+ puts "done!"
69
+ end
70
+ end
@@ -6,8 +6,6 @@ require 'koala_spec_helper'
6
6
  # specs to run. See facebook_data.yml for more information.
7
7
 
8
8
  # load testing data (see note in readme.md)
9
- # I'm seeing a bug with spec and gets where the facebook_test_suite.rb file gets read in when gets is called
10
- # until that's solved, we'll need to store/update tokens in the access_token file
11
9
  $testing_data = YAML.load_file(File.join(File.dirname(__FILE__), 'facebook_data.yml')) rescue {}
12
10
 
13
11
  unless $testing_data["oauth_token"]
@@ -16,4 +14,6 @@ end
16
14
 
17
15
  unless $testing_data["oauth_test_data"] && $testing_data["oauth_test_data"]["code"] && $testing_data["oauth_test_data"]["secret"]
18
16
  puts "OAuth code tests will fail until you store valid data for the user's OAuth code and the app secret in facebook_data.yml"
19
- end
17
+ end
18
+
19
+ KoalaTest.validate_user_info $testing_data["oauth_token"]
@@ -82,7 +82,15 @@ graph_api:
82
82
  link=http://www.contextoptional.com/&message=Hello, world, from the test suite again!&name=Context Optional:
83
83
  post:
84
84
  with_token: '{"id": "FEED_ITEM_CONTEXT"}'
85
-
85
+ /me/photos:
86
+ source=[FILE]:
87
+ post:
88
+ <<: *token_required
89
+ with_token: '{"id": "MOCK_PHOTO"}'
90
+ message=This is the test message&source=[FILE]:
91
+ post:
92
+ <<: *token_required
93
+ with_token: '{"id": "MOCK_PHOTO"}'
86
94
  /koppel:
87
95
  no_args:
88
96
  get:
@@ -237,12 +245,23 @@ graph_api:
237
245
  /FEED_ITEM_DELETE:
238
246
  no_args:
239
247
  <<: *item_deleted
240
-
248
+
249
+ /FEED_ITEM_DELETE/likes:
250
+ no_args:
251
+ <<: *item_deleted
252
+ post:
253
+ with_token: 'true'
254
+
241
255
  /MOCK_COMMENT:
242
256
  no_args:
243
257
  <<: *item_deleted
244
258
  get:
245
259
  with_token: "{\"message\": \"it\'s my comment!\"}"
260
+ /MOCK_PHOTO:
261
+ no_args:
262
+ <<: *item_deleted
263
+ get:
264
+ with_token: "{\"name\": \"This is the test message\"}"
246
265
 
247
266
  # -- Mock Test User Responses --
248
267
  /<%= APP_ID %>/accounts/test-users:
@@ -285,5 +304,4 @@ graph_api:
285
304
  /888888888/friends/999999999:
286
305
  no_args:
287
306
  get:
288
- with_token: 'true'
289
-
307
+ with_token: 'true'
@@ -33,6 +33,8 @@ module Koala
33
33
  def self.included(base)
34
34
  base.class_eval do
35
35
 
36
+ include Koala::HTTPService
37
+
36
38
  def self.make_request(path, args, verb, options = {})
37
39
  path = 'root' if path == '' || path == '/'
38
40
  verb ||= 'get'
@@ -43,7 +45,7 @@ module Koala
43
45
  args.delete('format')
44
46
 
45
47
  # Create a hash key for the arguments
46
- args = args.empty? ? 'no_args' : args.sort{|a,b| a[0].to_s <=> b[0].to_s }.map{|arr| arr.join('=')}.join('&')
48
+ args = create_params_key(args)
47
49
 
48
50
  begin
49
51
  response = RESPONSES[server][path][args][verb][with_token]
@@ -76,6 +78,18 @@ module Koala
76
78
  response_object
77
79
  end
78
80
 
81
+ protected
82
+ def self.create_params_key(params_hash)
83
+ if params_hash.empty?
84
+ 'no_args'
85
+ else
86
+ params_hash.sort{ |a,b| a[0].to_s <=> b[0].to_s}.map do |arr|
87
+ arr[1] = '[FILE]' if arr[1].kind_of?(File) || is_valid_file_hash?(arr[1])
88
+ arr.join('=')
89
+ end.join('&')
90
+ end
91
+ end
92
+
79
93
  end # class_eval
80
94
  end # included
81
95
  end
metadata CHANGED
@@ -1,12 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koala
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 31098193
5
+ prerelease: 6
5
6
  segments:
7
+ - 1
6
8
  - 0
7
- - 10
8
9
  - 0
9
- version: 0.10.0
10
+ - beta
11
+ version: 1.0.0.beta
10
12
  platform: ruby
11
13
  authors:
12
14
  - Alex Koppel, Chris Baclig, Rafi Jacoby, Context Optional
@@ -14,22 +16,39 @@ autorequire:
14
16
  bindir: bin
15
17
  cert_chain: []
16
18
 
17
- date: 2010-12-15 00:00:00 +01:00
19
+ date: 2011-01-26 00:00:00 +01:00
18
20
  default_executable:
19
21
  dependencies:
20
22
  - !ruby/object:Gem::Dependency
21
23
  name: json
22
24
  prerelease: false
23
25
  requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
24
27
  requirements:
25
28
  - - ">="
26
29
  - !ruby/object:Gem::Version
30
+ hash: 15
27
31
  segments:
28
32
  - 1
29
33
  - 0
30
34
  version: "1.0"
31
35
  type: :runtime
32
36
  version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: multipart-post
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 15
46
+ segments:
47
+ - 1
48
+ - 0
49
+ version: "1.0"
50
+ type: :runtime
51
+ version_requirements: *id002
33
52
  description: Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write access to the social graph via the Graph API and the older REST API, as well as support for realtime updates and OAuth and Facebook Connect authentication. Koala is fully tested and supports Net::HTTP and Typhoeus connections out of the box and can accept custom modules for other services.
34
53
  email: alex@alexkoppel.com
35
54
  executables: []
@@ -61,9 +80,11 @@ files:
61
80
  - readme.md
62
81
  - spec/facebook_data.yml
63
82
  - spec/koala/api_base_tests.rb
83
+ - spec/koala/assets/beach.jpg
64
84
  - spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb
65
85
  - spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb
66
86
  - spec/koala/graph_api/graph_api_no_access_token_tests.rb
87
+ - spec/koala/graph_api/graph_api_tests.rb
67
88
  - spec/koala/graph_api/graph_api_with_access_token_tests.rb
68
89
  - spec/koala/graph_api/graph_collection_tests.rb
69
90
  - spec/koala/live_testing_data_helper.rb
@@ -71,8 +92,10 @@ files:
71
92
  - spec/koala/oauth/oauth_tests.rb
72
93
  - spec/koala/realtime_updates/realtime_updates_tests.rb
73
94
  - spec/koala/rest_api/rest_api_no_access_token_tests.rb
95
+ - spec/koala/rest_api/rest_api_tests.rb
74
96
  - spec/koala/rest_api/rest_api_with_access_token_tests.rb
75
97
  - spec/koala/test_users/test_users_tests.rb
98
+ - spec/koala/typhoeus_service_tests.rb
76
99
  - spec/koala_spec.rb
77
100
  - spec/koala_spec_helper.rb
78
101
  - spec/koala_spec_without_mocks.rb
@@ -88,21 +111,23 @@ rdoc_options:
88
111
  - --inline-source
89
112
  - --title
90
113
  - Koala
91
- - --main
92
- - readme.md
93
114
  require_paths:
94
115
  - lib
95
116
  required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
96
118
  requirements:
97
119
  - - ">="
98
120
  - !ruby/object:Gem::Version
121
+ hash: 3
99
122
  segments:
100
123
  - 0
101
124
  version: "0"
102
125
  required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
103
127
  requirements:
104
128
  - - ">="
105
129
  - !ruby/object:Gem::Version
130
+ hash: 11
106
131
  segments:
107
132
  - 1
108
133
  - 2
@@ -110,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
135
  requirements: []
111
136
 
112
137
  rubyforge_project: koala
113
- rubygems_version: 1.3.6
138
+ rubygems_version: 1.4.2
114
139
  signing_key:
115
140
  specification_version: 3
116
141
  summary: A lightweight, flexible library for Facebook with support for the Graph API, the old REST API, realtime updates, and OAuth validation.