koala 0.10.0 → 1.0.0.rc

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 (50) hide show
  1. data/.gitignore +3 -0
  2. data/CHANGELOG +39 -7
  3. data/Gemfile +3 -0
  4. data/Manifest +8 -1
  5. data/Rakefile +13 -14
  6. data/koala.gemspec +36 -19
  7. data/lib/koala/graph_api.rb +188 -123
  8. data/lib/koala/http_services.rb +93 -18
  9. data/lib/koala/rest_api.rb +73 -6
  10. data/lib/koala/test_users.rb +21 -8
  11. data/lib/koala/uploadable_io.rb +115 -0
  12. data/lib/koala.rb +86 -86
  13. data/readme.md +18 -12
  14. data/spec/cases/api_base_spec.rb +101 -0
  15. data/spec/cases/graph_and_rest_api_spec.rb +31 -0
  16. data/spec/cases/graph_api_spec.rb +25 -0
  17. data/spec/cases/http_services/http_service_spec.rb +54 -0
  18. data/spec/cases/http_services/net_http_service_spec.rb +350 -0
  19. data/spec/cases/http_services/typhoeus_service_spec.rb +144 -0
  20. data/spec/cases/oauth_spec.rb +374 -0
  21. data/spec/cases/realtime_updates_spec.rb +184 -0
  22. data/spec/cases/rest_api_spec.rb +25 -0
  23. data/spec/{koala/test_users/test_users_tests.rb → cases/test_users_spec.rb} +78 -72
  24. data/spec/cases/uploadable_io_spec.rb +151 -0
  25. data/spec/fixtures/beach.jpg +0 -0
  26. data/spec/{facebook_data.yml → fixtures/facebook_data.yml} +13 -9
  27. data/spec/{mock_facebook_responses.yml → fixtures/mock_facebook_responses.yml} +312 -289
  28. data/spec/spec_helper.rb +18 -0
  29. data/spec/support/graph_api_shared_examples.rb +424 -0
  30. data/spec/{koala → support}/live_testing_data_helper.rb +39 -42
  31. data/spec/{mock_http_service.rb → support/mock_http_service.rb} +94 -81
  32. data/spec/support/rest_api_shared_examples.rb +161 -0
  33. data/spec/support/setup_mocks_or_live.rb +52 -0
  34. data/spec/support/uploadable_io_shared_examples.rb +76 -0
  35. metadata +130 -43
  36. data/init.rb +0 -2
  37. data/spec/koala/api_base_tests.rb +0 -102
  38. data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +0 -10
  39. data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +0 -11
  40. data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +0 -114
  41. data/spec/koala/graph_api/graph_api_with_access_token_tests.rb +0 -150
  42. data/spec/koala/graph_api/graph_collection_tests.rb +0 -104
  43. data/spec/koala/net_http_service_tests.rb +0 -186
  44. data/spec/koala/oauth/oauth_tests.rb +0 -438
  45. data/spec/koala/realtime_updates/realtime_updates_tests.rb +0 -187
  46. data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +0 -94
  47. data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +0 -36
  48. data/spec/koala_spec.rb +0 -18
  49. data/spec/koala_spec_helper.rb +0 -48
  50. data/spec/koala_spec_without_mocks.rb +0 -19
@@ -1,82 +1,95 @@
1
- require 'erb'
2
- require 'yaml'
3
-
4
- module Koala
5
- module MockHTTPService
6
- # Mocks all HTTP requests for with koala_spec_with_mocks.rb
7
- IS_MOCK = true # this lets our tests figure out if we want to stub methods
8
-
9
- # Mocked values to be included in TEST_DATA used in specs
10
- ACCESS_TOKEN = '*'
11
- OAUTH_CODE = 'OAUTHCODE'
12
-
13
- # Loads testing data
14
- TEST_DATA = YAML.load_file(File.join(File.dirname(__FILE__), 'facebook_data.yml'))
15
- TEST_DATA.merge!('oauth_token' => Koala::MockHTTPService::ACCESS_TOKEN)
16
- TEST_DATA['oauth_test_data'].merge!('code' => Koala::MockHTTPService::OAUTH_CODE)
17
-
18
- # Useful in mock_facebook_responses.yml
19
- OAUTH_DATA = TEST_DATA['oauth_test_data']
20
- OAUTH_DATA.merge!({
21
- 'app_access_token' => Koala::MockHTTPService::ACCESS_TOKEN,
22
- 'session_key' => "session_key",
23
- 'multiple_session_keys' => ["session_key", "session_key_2"]
24
- })
25
- APP_ID = OAUTH_DATA['app_id']
26
- SECRET = OAUTH_DATA['secret']
27
- SUBSCRIPTION_DATA = TEST_DATA["subscription_test_data"]
28
-
29
- # Loads the mock response data via ERB to substitue values for TEST_DATA (see oauth/access_token)
30
- mock_response_file_path = File.join(File.dirname(__FILE__), 'mock_facebook_responses.yml')
31
- RESPONSES = YAML.load(ERB.new(IO.read(mock_response_file_path)).result(binding))
32
-
33
- def self.included(base)
34
- base.class_eval do
35
-
36
- def self.make_request(path, args, verb, options = {})
37
- path = 'root' if path == '' || path == '/'
38
- verb ||= 'get'
39
- server = options[:rest_api] ? 'rest_api' : 'graph_api'
40
- with_token = args.delete('access_token') == ACCESS_TOKEN ? 'with_token' : 'no_token'
41
-
42
- # Assume format is always JSON
43
- args.delete('format')
44
-
45
- # 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('&')
47
-
48
- begin
49
- response = RESPONSES[server][path][args][verb][with_token]
50
-
51
- # Raises an error of with_token/no_token key is missing
52
- raise NoMethodError unless response
53
-
54
- # create response class object
55
- response_object = if response.is_a? String
56
- Koala::Response.new(200, response, {})
57
- else
58
- Koala::Response.new(response["code"] || 200, response["body"] || "", response["headers"] || {})
59
- end
60
-
61
- rescue NoMethodError
62
- # Raises an error message with the place in the data YML
63
- # to place a mock as well as a URL to request from
64
- # Facebook's servers for the actual data
65
- # (Don't forget to replace ACCESS_TOKEN with a real access token)
66
-
67
- data_trace = [server, path, args, verb, with_token] * ': '
68
-
69
- args = args == 'no_args' ? '' : "#{args}&"
70
- args += 'format=json'
71
- args += "&access_token=#{ACCESS_TOKEN}" if with_token
72
-
73
- raise "Missing a mock response for #{data_trace}\nAPI PATH: #{[path, args].join('?')}"
74
- end
75
-
76
- response_object
77
- end
78
-
79
- end # class_eval
80
- end # included
81
- end
1
+ require 'erb'
2
+ require 'yaml'
3
+
4
+ module Koala
5
+ module MockHTTPService
6
+ # Mocks all HTTP requests for with koala_spec_with_mocks.rb
7
+ IS_MOCK = true # this lets our tests figure out if we want to stub methods
8
+
9
+ # Mocked values to be included in TEST_DATA used in specs
10
+ ACCESS_TOKEN = '*'
11
+ OAUTH_CODE = 'OAUTHCODE'
12
+
13
+ # Loads testing data
14
+ TEST_DATA = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'fixtures', 'facebook_data.yml'))
15
+ TEST_DATA.merge!('oauth_token' => Koala::MockHTTPService::ACCESS_TOKEN)
16
+ TEST_DATA['oauth_test_data'].merge!('code' => Koala::MockHTTPService::OAUTH_CODE)
17
+
18
+ # Useful in mock_facebook_responses.yml
19
+ OAUTH_DATA = TEST_DATA['oauth_test_data']
20
+ OAUTH_DATA.merge!({
21
+ 'app_access_token' => Koala::MockHTTPService::ACCESS_TOKEN,
22
+ 'session_key' => "session_key",
23
+ 'multiple_session_keys' => ["session_key", "session_key_2"]
24
+ })
25
+ APP_ID = OAUTH_DATA['app_id']
26
+ SECRET = OAUTH_DATA['secret']
27
+ SUBSCRIPTION_DATA = TEST_DATA["subscription_test_data"]
28
+
29
+ # Loads the mock response data via ERB to substitue values for TEST_DATA (see oauth/access_token)
30
+ mock_response_file_path = File.join(File.dirname(__FILE__), '..', 'fixtures', 'mock_facebook_responses.yml')
31
+ RESPONSES = YAML.load(ERB.new(IO.read(mock_response_file_path)).result(binding))
32
+
33
+ def self.included(base)
34
+ base.class_eval do
35
+
36
+ include Koala::HTTPService
37
+
38
+ def self.make_request(path, args, verb, options = {})
39
+ path = 'root' if path == '' || path == '/'
40
+ verb ||= 'get'
41
+ server = options[:rest_api] ? 'rest_api' : 'graph_api'
42
+ with_token = args.delete('access_token') == ACCESS_TOKEN ? 'with_token' : 'no_token'
43
+
44
+ # Assume format is always JSON
45
+ args.delete('format')
46
+
47
+ # Create a hash key for the arguments
48
+ args = create_params_key(args)
49
+
50
+ begin
51
+ response = RESPONSES[server][path][args][verb][with_token]
52
+
53
+ # Raises an error of with_token/no_token key is missing
54
+ raise NoMethodError unless response
55
+
56
+ # create response class object
57
+ response_object = if response.is_a? String
58
+ Koala::Response.new(200, response, {})
59
+ else
60
+ Koala::Response.new(response["code"] || 200, response["body"] || "", response["headers"] || {})
61
+ end
62
+
63
+ rescue NoMethodError
64
+ # Raises an error message with the place in the data YML
65
+ # to place a mock as well as a URL to request from
66
+ # Facebook's servers for the actual data
67
+ # (Don't forget to replace ACCESS_TOKEN with a real access token)
68
+ data_trace = [server, path, args, verb, with_token] * ': '
69
+
70
+ args = args == 'no_args' ? '' : "#{args}&"
71
+ args += 'format=json'
72
+ args += "&access_token=#{ACCESS_TOKEN}" if with_token
73
+
74
+ raise "Missing a mock response for #{data_trace}\nAPI PATH: #{[path, args].join('?')}"
75
+ end
76
+
77
+ response_object
78
+ end
79
+
80
+ protected
81
+ def self.create_params_key(params_hash)
82
+ if params_hash.empty?
83
+ 'no_args'
84
+ else
85
+ params_hash.sort{ |a,b| a[0].to_s <=> b[0].to_s}.map do |arr|
86
+ arr[1] = '[FILE]' if arr[1].kind_of?(Koala::UploadableIO)
87
+ arr.join('=')
88
+ end.join('&')
89
+ end
90
+ end
91
+
92
+ end # class_eval
93
+ end # included
94
+ end
82
95
  end
@@ -0,0 +1,161 @@
1
+ shared_examples_for "Koala RestAPI" do
2
+ # REST_CALL
3
+ describe "when making a rest request" do
4
+ it "should use the proper path" do
5
+ method = stub('methodName')
6
+ @api.should_receive(:api).with(
7
+ "method/#{method}",
8
+ anything,
9
+ anything,
10
+ anything
11
+ )
12
+
13
+ @api.rest_call(method)
14
+ end
15
+
16
+ it "should always use the rest api" do
17
+ @api.should_receive(:api).with(
18
+ anything,
19
+ anything,
20
+ anything,
21
+ hash_including(:rest_api => true)
22
+ )
23
+
24
+ @api.rest_call('anything')
25
+ end
26
+
27
+ it "should set the read_only option to true if the method is listed in the read-only list" do
28
+ method = Koala::Facebook::RestAPI::READ_ONLY_METHODS.first
29
+
30
+ @api.should_receive(:api).with(
31
+ anything,
32
+ anything,
33
+ anything,
34
+ hash_including(:read_only => true)
35
+ )
36
+
37
+ @api.rest_call(method)
38
+ end
39
+
40
+ it "should set the read_only option to false if the method is not inthe read-only list" do
41
+ method = "I'm not a read-only method"
42
+
43
+ @api.should_receive(:api).with(
44
+ anything,
45
+ anything,
46
+ anything,
47
+ hash_including(:read_only => false)
48
+ )
49
+
50
+ @api.rest_call(method)
51
+ end
52
+
53
+
54
+ it "should take an optional hash of arguments" do
55
+ args = {:arg1 => 'arg1'}
56
+
57
+ @api.should_receive(:api).with(
58
+ anything,
59
+ hash_including(args),
60
+ anything,
61
+ anything
62
+ )
63
+
64
+ @api.rest_call('anything', args)
65
+ end
66
+
67
+ it "should always ask for JSON" do
68
+ @api.should_receive(:api).with(
69
+ anything,
70
+ hash_including('format' => 'json'),
71
+ anything,
72
+ anything
73
+ )
74
+
75
+ @api.rest_call('anything')
76
+ end
77
+
78
+ it "should pass any options provided to the API" do
79
+ options = {:a => 2}
80
+
81
+ @api.should_receive(:api).with(
82
+ anything,
83
+ hash_including('format' => 'json'),
84
+ anything,
85
+ hash_including(options)
86
+ )
87
+
88
+ @api.rest_call('anything', {}, options)
89
+ end
90
+
91
+ it "should throw an APIError if the result hash has an error key" do
92
+ Koala.stub(:make_request).and_return(Koala::Response.new(500, {"error_code" => "An error occurred!"}, {}))
93
+ lambda { @api.rest_call("koppel", {}) }.should raise_exception(Koala::Facebook::APIError)
94
+ end
95
+
96
+ describe "when making a FQL request" do
97
+ it "should call fql.query method" do
98
+ @api.should_receive(:rest_call).with(
99
+ "fql.query",
100
+ anything
101
+ ).and_return(Koala::Response.new(200, "2", {}))
102
+
103
+ @api.fql_query stub('query string')
104
+ end
105
+
106
+ it "should pass a query argument" do
107
+ query = stub('query string')
108
+
109
+ @api.should_receive(:rest_call).with(
110
+ anything,
111
+ hash_including("query" => query)
112
+ )
113
+
114
+ @api.fql_query(query)
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+
121
+ shared_examples_for "Koala RestAPI with an access token" do
122
+ # FQL
123
+ it "should be able to access public information via FQL" do
124
+ result = @api.fql_query('select first_name from user where uid = 216743')
125
+ result.size.should == 1
126
+ result.first['first_name'].should == 'Chris'
127
+ end
128
+
129
+ it "should be able to access protected information via FQL" do
130
+ # Tests agains the permissions fql table
131
+
132
+ # get the current user's ID
133
+ # we're sneakily using the Graph API, which should be okay since it has its own tests
134
+ g = Koala::Facebook::GraphAPI.new(@token)
135
+ id = g.get_object("me", :fields => "id")["id"]
136
+
137
+ # now send a query about your permissions
138
+ result = @api.fql_query("select read_stream from permissions where uid = #{id}")
139
+
140
+ result.size.should == 1
141
+ # we assume that you have read_stream permissions, so we can test against that
142
+ # (should we keep this?)
143
+ result.first["read_stream"].should == 1
144
+ end
145
+ end
146
+
147
+
148
+ shared_examples_for "Koala RestAPI without an access token" do
149
+ # FQL_QUERY
150
+ describe "when making a FQL request" do
151
+ it "should be able to access public information via FQL" do
152
+ @result = @api.fql_query("select first_name from user where uid = 216743")
153
+ @result.size.should == 1
154
+ @result.first["first_name"].should == "Chris"
155
+ end
156
+
157
+ it "should not be able to access protected information via FQL" do
158
+ lambda { @api.fql_query("select read_stream from permissions where uid = 216743") }.should raise_error(Koala::Facebook::APIError)
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,52 @@
1
+ # small helper method for live testing
2
+ module KoalaTest
3
+ def self.validate_user_info(token)
4
+ print "Validating permissions for live testing..."
5
+ # make sure we have the necessary permissions
6
+ api = Koala::Facebook::GraphAndRestAPI.new(token)
7
+ uid = api.get_object("me")["id"]
8
+ perms = api.fql_query("select read_stream, publish_stream, user_photos from permissions where uid = #{uid}")[0]
9
+ perms.each_pair do |perm, value|
10
+ unless value == 1
11
+ puts "failed!\n" # put a new line after the print above
12
+ raise ArgumentError, "Your access token must have the read_stream, publish_stream, and user_photos permissions. You have: #{perms.inspect}"
13
+ end
14
+ end
15
+ puts "done!"
16
+ end
17
+ end
18
+
19
+
20
+ unless ENV['LIVE']
21
+ # By default the Koala specs are run using stubs for HTTP requests
22
+ #
23
+ # Valid OAuth token and code are not necessary to run these
24
+ # specs. Because of this, specs do not fail due to Facebook
25
+ # imposed rate-limits or server timeouts.
26
+ #
27
+ # However as a result they are more brittle since
28
+ # we are not testing the latest responses from the Facebook servers.
29
+ # Therefore, to be certain all specs pass with the current
30
+ # Facebook services, run koala_spec_without_mocks.rb.
31
+ Koala.http_service = Koala::MockHTTPService
32
+
33
+ $testing_data = Koala::MockHTTPService::TEST_DATA
34
+ else
35
+ # Runs Koala specs through the Facebook servers
36
+ #
37
+ # Note that you need a valid OAuth token and code for these
38
+ # specs to run. See facebook_data.yml for more information.
39
+
40
+ # load testing data (see note in readme.md)
41
+ $testing_data = YAML.load_file(File.join(File.dirname(__FILE__), '../fixtures/facebook_data.yml'))
42
+
43
+ unless $testing_data["oauth_token"]
44
+ puts "Access token tests will fail until you store a valid token in facebook_data.yml"
45
+ end
46
+
47
+ unless $testing_data["oauth_test_data"] && $testing_data["oauth_test_data"]["code"] && $testing_data["oauth_test_data"]["secret"]
48
+ 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"
49
+ end
50
+
51
+ KoalaTest.validate_user_info $testing_data["oauth_token"]
52
+ end
@@ -0,0 +1,76 @@
1
+ shared_examples_for "MIME::Types can't return results" do
2
+ {
3
+ "jpg" => "image/jpeg",
4
+ "jpeg" => "image/jpeg",
5
+ "png" => "image/png",
6
+ "gif" => "image/gif"
7
+ }.each_pair do |extension, mime_type|
8
+ it "should properly get content types for #{extension} using basic analysis" do
9
+ path = "filename.#{extension}"
10
+ if @koala_io_params[0].is_a?(File)
11
+ @koala_io_params[0].stub!(:path).and_return(path)
12
+ else
13
+ @koala_io_params[0] = path
14
+ end
15
+ Koala::UploadableIO.new(*@koala_io_params).content_type.should == mime_type
16
+ end
17
+
18
+ it "should get content types for #{extension} using basic analysis with file names with more than one dot" do
19
+ path = "file.name.#{extension}"
20
+ if @koala_io_params[0].is_a?(File)
21
+ @koala_io_params[0].stub!(:path).and_return(path)
22
+ else
23
+ @koala_io_params[0] = path
24
+ end
25
+ Koala::UploadableIO.new(*@koala_io_params).content_type.should == mime_type
26
+ end
27
+ end
28
+
29
+ describe "if the MIME type can't be determined" do
30
+ before :each do
31
+ path = "badfile.badextension"
32
+ if @koala_io_params[0].is_a?(File)
33
+ @koala_io_params[0].stub!(:path).and_return(path)
34
+ else
35
+ @koala_io_params[0] = path
36
+ end
37
+ end
38
+
39
+ it "should throw an exception if the MIME type can't be determined and the HTTP service requires content type" do
40
+ Koala.stub!(:multipart_requires_content_type?).and_return(true)
41
+ lambda { Koala::UploadableIO.new(*@koala_io_params) }.should raise_exception(Koala::KoalaError)
42
+ end
43
+
44
+ it "should just have @content_type == nil if the HTTP service doesn't require content type" do
45
+ Koala.stub!(:multipart_requires_content_type?).and_return(false)
46
+ Koala::UploadableIO.new(*@koala_io_params).content_type.should be_nil
47
+ end
48
+ end
49
+ end
50
+
51
+ shared_examples_for "determining a mime type" do
52
+ describe "if MIME::Types is available" do
53
+ it "should return an UploadIO with MIME::Types-determined type if the type exists" do
54
+ type_result = ["type"]
55
+ Koala::MIME::Types.stub(:type_for).and_return(type_result)
56
+ Koala::UploadableIO.new(*@koala_io_params).content_type.should == type_result.first
57
+ end
58
+ end
59
+
60
+ describe "if MIME::Types is unavailable" do
61
+ before :each do
62
+ # fake that MIME::Types doesn't exist
63
+ Koala::MIME::Types.stub(:type_for).and_raise(NameError)
64
+ end
65
+ it_should_behave_like "MIME::Types can't return results"
66
+ end
67
+
68
+ describe "if MIME::Types can't find the result" do
69
+ before :each do
70
+ # fake that MIME::Types doesn't exist
71
+ Koala::MIME::Types.stub(:type_for).and_return([])
72
+ end
73
+
74
+ it_should_behave_like "MIME::Types can't return results"
75
+ end
76
+ end