koala 1.1.0 → 1.2.0beta1
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.
- data/.travis.yml +2 -1
- data/CHANGELOG +26 -0
- data/Gemfile +6 -2
- data/Rakefile +0 -1
- data/koala.gemspec +8 -8
- data/lib/koala.rb +42 -45
- data/lib/koala/batch_operation.rb +15 -15
- data/lib/koala/graph_api.rb +81 -58
- data/lib/koala/graph_batch_api.rb +10 -10
- data/lib/koala/graph_collection.rb +6 -6
- data/lib/koala/http_service.rb +177 -0
- data/lib/koala/oauth.rb +2 -2
- data/lib/koala/realtime_updates.rb +20 -17
- data/lib/koala/rest_api.rb +1 -1
- data/lib/koala/test_users.rb +33 -16
- data/lib/koala/uploadable_io.rb +47 -42
- data/lib/koala/utils.rb +11 -0
- data/readme.md +38 -38
- data/spec/cases/api_base_spec.rb +2 -2
- data/spec/cases/error_spec.rb +32 -0
- data/spec/cases/graph_and_rest_api_spec.rb +20 -3
- data/spec/cases/graph_api_batch_spec.rb +88 -97
- data/spec/cases/graph_api_spec.rb +21 -4
- data/spec/cases/http_service_spec.rb +446 -0
- data/spec/cases/koala_spec.rb +33 -38
- data/spec/cases/oauth_spec.rb +219 -200
- data/spec/cases/realtime_updates_spec.rb +45 -31
- data/spec/cases/rest_api_spec.rb +23 -7
- data/spec/cases/test_users_spec.rb +112 -52
- data/spec/cases/uploadable_io_spec.rb +49 -36
- data/spec/cases/utils_spec.rb +10 -0
- data/spec/fixtures/facebook_data.yml +23 -22
- data/spec/fixtures/mock_facebook_responses.yml +126 -96
- data/spec/spec_helper.rb +29 -5
- data/spec/support/graph_api_shared_examples.rb +59 -52
- data/spec/support/json_testing_fix.rb +35 -11
- data/spec/support/koala_test.rb +163 -0
- data/spec/support/mock_http_service.rb +6 -4
- data/spec/support/ordered_hash.rb +205 -0
- data/spec/support/rest_api_shared_examples.rb +37 -37
- data/spec/support/uploadable_io_shared_examples.rb +2 -8
- metadata +78 -79
- data/lib/koala/http_services.rb +0 -46
- data/lib/koala/http_services/net_http_service.rb +0 -92
- data/lib/koala/http_services/typhoeus_service.rb +0 -37
- data/spec/cases/http_services/http_service_spec.rb +0 -129
- data/spec/cases/http_services/net_http_service_spec.rb +0 -532
- data/spec/cases/http_services/typhoeus_service_spec.rb +0 -152
- data/spec/support/live_testing_data_helper.rb +0 -40
- data/spec/support/setup_mocks_or_live.rb +0 -51
data/spec/cases/koala_spec.rb
CHANGED
@@ -2,54 +2,49 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "Koala" do
|
4
4
|
it "has an http_service accessor" do
|
5
|
-
Koala.respond_to
|
5
|
+
Koala.should respond_to(:http_service)
|
6
|
+
Koala.should respond_to(:http_service=)
|
6
7
|
end
|
7
|
-
|
8
|
-
|
9
|
-
current_service = Koala.http_service
|
10
|
-
Koala.http_service = Koala::MockHTTPService
|
11
|
-
Koala.http_service.should == Koala::MockHTTPService
|
12
|
-
# reset the service back to the original one (important for live tests)
|
13
|
-
Koala.http_service = current_service
|
14
|
-
end
|
15
|
-
|
16
|
-
it "sets Net::HTTP as the base service" do
|
17
|
-
Koala.base_http_service.should == Koala::NetHTTPService
|
18
|
-
end
|
19
|
-
|
20
|
-
describe ".always_use_ssl" do
|
21
|
-
it "should be added" do
|
22
|
-
# in Ruby 1.8, .methods returns strings
|
23
|
-
# in Ruby 1.9, .method returns symbols
|
24
|
-
Koala.methods.collect {|m| m.to_sym}.should include(:always_use_ssl)
|
25
|
-
Koala.methods.collect {|m| m.to_sym}.should include(:always_use_ssl=)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
describe ".make_request" do
|
30
|
-
|
8
|
+
|
9
|
+
context "for deprecated services" do
|
31
10
|
before :each do
|
32
|
-
@
|
33
|
-
Koala.http_service = Koala::MockHTTPService
|
11
|
+
@service = Koala.http_service
|
34
12
|
end
|
35
13
|
|
36
14
|
after :each do
|
37
|
-
Koala.http_service = @
|
15
|
+
Koala.http_service = @service
|
38
16
|
end
|
39
17
|
|
40
|
-
it "
|
41
|
-
|
42
|
-
|
43
|
-
Koala.
|
44
|
-
|
18
|
+
it "invokes deprecated_interface if present" do
|
19
|
+
mock_service = stub("http service")
|
20
|
+
mock_service.should_receive(:deprecated_interface)
|
21
|
+
Koala.http_service = mock_service
|
22
|
+
end
|
23
|
+
|
24
|
+
it "does not set the service if it's deprecated" do
|
25
|
+
mock_service = stub("http service")
|
26
|
+
mock_service.stub(:deprecated_interface)
|
27
|
+
Koala.http_service = mock_service
|
28
|
+
Koala.http_service.should == @service
|
45
29
|
end
|
46
30
|
|
47
|
-
it "
|
48
|
-
|
49
|
-
http_service
|
50
|
-
|
51
|
-
Koala.make_request(anything, anything, anything, :http_service => http_service)
|
31
|
+
it "sets the service if it's not deprecated" do
|
32
|
+
mock_service = stub("http service")
|
33
|
+
Koala.http_service = mock_service
|
34
|
+
Koala.http_service.should == mock_service
|
52
35
|
end
|
36
|
+
end
|
53
37
|
|
38
|
+
define "make_request" do
|
39
|
+
it "passes all its arguments to the http_service" do
|
40
|
+
http_service = stub("http_service")
|
41
|
+
path = "foo"
|
42
|
+
args = {:a => 2}
|
43
|
+
verb = "get"
|
44
|
+
options = {:c => :d}
|
45
|
+
http_service.should_receive(:make_request).with(path, args, verb, options)
|
46
|
+
Koala.make_request(path, args, verb, options)
|
47
|
+
end
|
54
48
|
end
|
49
|
+
|
55
50
|
end
|
data/spec/cases/oauth_spec.rb
CHANGED
@@ -3,38 +3,40 @@ require 'spec_helper'
|
|
3
3
|
describe "Koala::Facebook::OAuth" do
|
4
4
|
before :each do
|
5
5
|
# make the relevant test data easily accessible
|
6
|
-
@
|
7
|
-
@
|
8
|
-
@
|
9
|
-
@
|
10
|
-
@
|
11
|
-
@
|
12
|
-
|
13
|
-
|
6
|
+
@app_id = KoalaTest.app_id
|
7
|
+
@secret = KoalaTest.secret
|
8
|
+
@code = KoalaTest.code
|
9
|
+
@callback_url = KoalaTest.oauth_test_data["callback_url"]
|
10
|
+
@raw_token_string = KoalaTest.oauth_test_data["raw_token_string"]
|
11
|
+
@raw_offline_access_token_string = KoalaTest.oauth_test_data["raw_offline_access_token_string"]
|
12
|
+
|
14
13
|
# for signed requests (http://developers.facebook.com/docs/authentication/canvas/encryption_proposal)
|
15
|
-
@signed_params =
|
16
|
-
@signed_params_result =
|
17
|
-
|
14
|
+
@signed_params = KoalaTest.oauth_test_data["signed_params"]
|
15
|
+
@signed_params_result = KoalaTest.oauth_test_data["signed_params_result"]
|
16
|
+
|
18
17
|
# this should expanded to cover all variables
|
19
|
-
raise Exception, "Must supply app data to run FacebookOAuthTests!" unless @app_id && @secret && @callback_url &&
|
20
|
-
@
|
18
|
+
raise Exception, "Must supply app data to run FacebookOAuthTests!" unless @app_id && @secret && @callback_url &&
|
19
|
+
@raw_token_string &&
|
21
20
|
@raw_offline_access_token_string
|
22
21
|
|
22
|
+
# we can just test against the same key twice
|
23
|
+
@multiple_session_keys = [KoalaTest.session_key, KoalaTest.session_key] if KoalaTest.session_key
|
24
|
+
|
23
25
|
@oauth = Koala::Facebook::OAuth.new(@app_id, @secret, @callback_url)
|
24
26
|
|
25
27
|
@time = Time.now
|
26
28
|
Time.stub!(:now).and_return(@time)
|
27
29
|
@time.stub!(:to_i).and_return(1273363199)
|
28
30
|
end
|
29
|
-
|
31
|
+
|
30
32
|
# initialization
|
31
33
|
it "should properly initialize" do
|
32
34
|
@oauth.should
|
33
35
|
end
|
34
36
|
|
35
37
|
it "should properly set attributes" do
|
36
|
-
(@oauth.app_id == @app_id &&
|
37
|
-
@oauth.app_secret == @secret &&
|
38
|
+
(@oauth.app_id == @app_id &&
|
39
|
+
@oauth.app_secret == @secret &&
|
38
40
|
@oauth.oauth_callback_url == @callback_url).should be_true
|
39
41
|
end
|
40
42
|
|
@@ -44,126 +46,131 @@ describe "Koala::Facebook::OAuth" do
|
|
44
46
|
|
45
47
|
it "should properly set attributes without a callback URL" do
|
46
48
|
@oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
|
47
|
-
(@oauth.app_id == @app_id &&
|
48
|
-
@oauth.app_secret == @secret &&
|
49
|
+
(@oauth.app_id == @app_id &&
|
50
|
+
@oauth.app_secret == @secret &&
|
49
51
|
@oauth.oauth_callback_url == nil).should be_true
|
50
52
|
end
|
51
|
-
|
53
|
+
|
52
54
|
describe "for cookie parsing" do
|
53
55
|
describe "get_user_info_from_cookies" do
|
54
56
|
it "should properly parse valid cookies" do
|
55
|
-
result = @oauth.get_user_info_from_cookies(
|
57
|
+
result = @oauth.get_user_info_from_cookies(KoalaTest.oauth_test_data["valid_cookies"])
|
56
58
|
result.should be_a(Hash)
|
57
59
|
end
|
58
|
-
|
60
|
+
|
59
61
|
it "should return all the cookie components from valid cookie string" do
|
60
|
-
cookie_data =
|
62
|
+
cookie_data = KoalaTest.oauth_test_data["valid_cookies"]
|
61
63
|
parsing_results = @oauth.get_user_info_from_cookies(cookie_data)
|
62
64
|
number_of_components = cookie_data["fbs_#{@app_id.to_s}"].scan(/\=/).length
|
63
65
|
parsing_results.length.should == number_of_components
|
64
66
|
end
|
65
67
|
|
66
|
-
it "should properly parse valid offline access cookies (e.g. no expiration)" do
|
67
|
-
result = @oauth.get_user_info_from_cookies(
|
68
|
-
result["uid"].should
|
68
|
+
it "should properly parse valid offline access cookies (e.g. no expiration)" do
|
69
|
+
result = @oauth.get_user_info_from_cookies(KoalaTest.oauth_test_data["offline_access_cookies"])
|
70
|
+
result["uid"].should
|
69
71
|
end
|
70
72
|
|
71
73
|
it "should return all the cookie components from offline access cookies" do
|
72
|
-
cookie_data =
|
74
|
+
cookie_data = KoalaTest.oauth_test_data["offline_access_cookies"]
|
73
75
|
parsing_results = @oauth.get_user_info_from_cookies(cookie_data)
|
74
76
|
number_of_components = cookie_data["fbs_#{@app_id.to_s}"].scan(/\=/).length
|
75
77
|
parsing_results.length.should == number_of_components
|
76
78
|
end
|
77
79
|
|
78
80
|
it "shouldn't parse expired cookies" do
|
79
|
-
result = @oauth.get_user_info_from_cookies(
|
81
|
+
result = @oauth.get_user_info_from_cookies(KoalaTest.oauth_test_data["expired_cookies"])
|
80
82
|
result.should be_nil
|
81
83
|
end
|
82
|
-
|
84
|
+
|
83
85
|
it "shouldn't parse invalid cookies" do
|
84
86
|
# make an invalid string by replacing some values
|
85
|
-
bad_cookie_hash =
|
87
|
+
bad_cookie_hash = KoalaTest.oauth_test_data["valid_cookies"].inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
|
86
88
|
result = @oauth.get_user_info_from_cookies(bad_cookie_hash)
|
87
89
|
result.should be_nil
|
88
90
|
end
|
89
91
|
end
|
90
|
-
|
92
|
+
|
91
93
|
describe "get_user_from_cookies" do
|
92
94
|
it "should use get_user_info_from_cookies to parse the cookies" do
|
93
|
-
data =
|
95
|
+
data = KoalaTest.oauth_test_data["valid_cookies"]
|
94
96
|
@oauth.should_receive(:get_user_info_from_cookies).with(data).and_return({})
|
95
97
|
@oauth.get_user_from_cookies(data)
|
96
98
|
end
|
97
99
|
|
98
100
|
it "should use return a string if the cookies are valid" do
|
99
|
-
result = @oauth.get_user_from_cookies(
|
101
|
+
result = @oauth.get_user_from_cookies(KoalaTest.oauth_test_data["valid_cookies"])
|
100
102
|
result.should be_a(String)
|
101
103
|
end
|
102
|
-
|
104
|
+
|
103
105
|
it "should return nil if the cookies are invalid" do
|
104
106
|
# make an invalid string by replacing some values
|
105
|
-
bad_cookie_hash =
|
107
|
+
bad_cookie_hash = KoalaTest.oauth_test_data["valid_cookies"].inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
|
106
108
|
result = @oauth.get_user_from_cookies(bad_cookie_hash)
|
107
109
|
result.should be_nil
|
108
|
-
end
|
110
|
+
end
|
109
111
|
end
|
110
112
|
end
|
111
|
-
|
113
|
+
|
112
114
|
# OAuth URLs
|
113
|
-
|
115
|
+
|
114
116
|
describe "for URL generation" do
|
115
117
|
|
116
|
-
describe "for OAuth codes" do
|
118
|
+
describe "for OAuth codes" do
|
117
119
|
# url_for_oauth_code
|
118
|
-
it "should generate a properly formatted OAuth code URL with the default values" do
|
120
|
+
it "should generate a properly formatted OAuth code URL with the default values" do
|
119
121
|
url = @oauth.url_for_oauth_code
|
120
122
|
url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}"
|
121
123
|
end
|
122
124
|
|
123
|
-
it "should generate a properly formatted OAuth code URL when a callback is given" do
|
125
|
+
it "should generate a properly formatted OAuth code URL when a callback is given" do
|
124
126
|
callback = "foo.com"
|
125
127
|
url = @oauth.url_for_oauth_code(:callback => callback)
|
126
128
|
url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{callback}"
|
127
129
|
end
|
128
130
|
|
129
|
-
it "should generate a properly formatted OAuth code URL when permissions are requested as a string" do
|
131
|
+
it "should generate a properly formatted OAuth code URL when permissions are requested as a string" do
|
130
132
|
permissions = "publish_stream,read_stream"
|
131
133
|
url = @oauth.url_for_oauth_code(:permissions => permissions)
|
132
134
|
url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}&scope=#{permissions}"
|
133
135
|
end
|
134
136
|
|
135
|
-
it "should generate a properly formatted OAuth code URL when permissions are requested as a string" do
|
137
|
+
it "should generate a properly formatted OAuth code URL when permissions are requested as a string" do
|
136
138
|
permissions = ["publish_stream", "read_stream"]
|
137
139
|
url = @oauth.url_for_oauth_code(:permissions => permissions)
|
138
140
|
url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}&scope=#{permissions.join(",")}"
|
139
141
|
end
|
140
142
|
|
141
|
-
it "should generate a properly formatted OAuth code URL when both permissions and callback are provided" do
|
143
|
+
it "should generate a properly formatted OAuth code URL when both permissions and callback are provided" do
|
142
144
|
permissions = "publish_stream,read_stream"
|
143
145
|
callback = "foo.com"
|
144
146
|
url = @oauth.url_for_oauth_code(:callback => callback, :permissions => permissions)
|
145
147
|
url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{callback}&scope=#{permissions}"
|
146
148
|
end
|
147
149
|
|
148
|
-
it "should generate a properly formatted OAuth code URL when a display is given as a string" do
|
150
|
+
it "should generate a properly formatted OAuth code URL when a display is given as a string" do
|
149
151
|
url = @oauth.url_for_oauth_code(:display => "page")
|
150
152
|
url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}&display=page"
|
151
153
|
end
|
152
154
|
|
153
|
-
it "should raise an exception if no callback is given in initialization or the call" do
|
155
|
+
it "should raise an exception if no callback is given in initialization or the call" do
|
154
156
|
oauth2 = Koala::Facebook::OAuth.new(@app_id, @secret)
|
155
157
|
lambda { oauth2.url_for_oauth_code }.should raise_error(ArgumentError)
|
156
158
|
end
|
157
159
|
end
|
158
|
-
|
160
|
+
|
159
161
|
describe "for access token URLs" do
|
162
|
+
before :each do
|
163
|
+
# since we're just composing a URL here, we don't need to have a real code
|
164
|
+
@code ||= "test_code"
|
165
|
+
end
|
166
|
+
|
160
167
|
# url_for_access_token
|
161
|
-
it "should generate a properly formatted OAuth token URL when provided a code" do
|
168
|
+
it "should generate a properly formatted OAuth token URL when provided a code" do
|
162
169
|
url = @oauth.url_for_access_token(@code)
|
163
170
|
url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{@callback_url}&client_secret=#{@secret}&code=#{@code}"
|
164
171
|
end
|
165
172
|
|
166
|
-
it "should generate a properly formatted OAuth token URL when provided a callback" do
|
173
|
+
it "should generate a properly formatted OAuth token URL when provided a callback" do
|
167
174
|
callback = "foo.com"
|
168
175
|
url = @oauth.url_for_access_token(@code, :callback => callback)
|
169
176
|
url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{callback}&client_secret=#{@secret}&code=#{@code}"
|
@@ -171,44 +178,48 @@ describe "Koala::Facebook::OAuth" do
|
|
171
178
|
end
|
172
179
|
end
|
173
180
|
|
174
|
-
describe "for fetching access tokens" do
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
options
|
209
|
-
|
210
|
-
|
211
|
-
|
181
|
+
describe "for fetching access tokens" do
|
182
|
+
if KoalaTest.code
|
183
|
+
describe "get_access_token_info" do
|
184
|
+
it "should properly get and parse an access token token results into a hash" do
|
185
|
+
result = @oauth.get_access_token_info(@code)
|
186
|
+
result.should be_a(Hash)
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should properly include the access token results" do
|
190
|
+
result = @oauth.get_access_token_info(@code)
|
191
|
+
result["access_token"].should
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should raise an error when get_access_token is called with a bad code" do
|
195
|
+
lambda { @oauth.get_access_token_info("foo") }.should raise_error(Koala::Facebook::APIError)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
describe "get_access_token" do
|
200
|
+
it "should use get_access_token_info to get and parse an access token token results" do
|
201
|
+
result = @oauth.get_access_token(@code)
|
202
|
+
result.should be_a(String)
|
203
|
+
end
|
204
|
+
|
205
|
+
it "should return the access token as a string" do
|
206
|
+
result = @oauth.get_access_token(@code)
|
207
|
+
original = @oauth.get_access_token_info(@code)
|
208
|
+
result.should == original["access_token"]
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should raise an error when get_access_token is called with a bad code" do
|
212
|
+
lambda { @oauth.get_access_token("foo") }.should raise_error(Koala::Facebook::APIError)
|
213
|
+
end
|
214
|
+
|
215
|
+
it "should pass on any options provided to make_request" do
|
216
|
+
options = {:a => 2}
|
217
|
+
Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "", {}))
|
218
|
+
@oauth.get_access_token(@code, options)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
else
|
222
|
+
it "OAuth code tests will not be run since the code field in facebook_data.yml is blank."
|
212
223
|
end
|
213
224
|
|
214
225
|
describe "get_app_access_token_info" do
|
@@ -216,19 +227,19 @@ describe "Koala::Facebook::OAuth" do
|
|
216
227
|
result = @oauth.get_app_access_token_info
|
217
228
|
result.should be_a(Hash)
|
218
229
|
end
|
219
|
-
|
230
|
+
|
220
231
|
it "should include the access token" do
|
221
232
|
result = @oauth.get_app_access_token_info
|
222
233
|
result["access_token"].should
|
223
234
|
end
|
224
|
-
|
235
|
+
|
225
236
|
it "should pass on any options provided to make_request" do
|
226
237
|
options = {:a => 2}
|
227
238
|
Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "", {}))
|
228
239
|
@oauth.get_app_access_token_info(options)
|
229
240
|
end
|
230
241
|
end
|
231
|
-
|
242
|
+
|
232
243
|
describe "get_app_acess_token" do
|
233
244
|
it "should use get_access_token_info to get and parse an access token token results" do
|
234
245
|
result = @oauth.get_app_access_token
|
@@ -240,16 +251,16 @@ describe "Koala::Facebook::OAuth" do
|
|
240
251
|
original = @oauth.get_app_access_token_info
|
241
252
|
result.should == original["access_token"]
|
242
253
|
end
|
243
|
-
|
254
|
+
|
244
255
|
it "should pass on any options provided to make_request" do
|
245
256
|
options = {:a => 2}
|
246
257
|
Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "", {}))
|
247
258
|
@oauth.get_app_access_token(options)
|
248
259
|
end
|
249
260
|
end
|
250
|
-
|
261
|
+
|
251
262
|
describe "protected methods" do
|
252
|
-
|
263
|
+
|
253
264
|
# protected methods
|
254
265
|
# since these are pretty fundamental and pretty testable, we want to test them
|
255
266
|
|
@@ -269,9 +280,13 @@ describe "Koala::Facebook::OAuth" do
|
|
269
280
|
# fetch_token_string
|
270
281
|
# somewhat duplicative with the tests for get_access_token and get_app_access_token
|
271
282
|
# but no harm in thoroughness
|
272
|
-
|
273
|
-
|
274
|
-
|
283
|
+
if KoalaTest.code
|
284
|
+
it "should fetch a proper token string from Facebook when given a code" do
|
285
|
+
result = @oauth.send(:fetch_token_string, :code => @code, :redirect_uri => @callback_url)
|
286
|
+
result.should =~ /^access_token/
|
287
|
+
end
|
288
|
+
else
|
289
|
+
it "fetch_token_string code test will not be run since the code field in facebook_data.yml is blank."
|
275
290
|
end
|
276
291
|
|
277
292
|
it "should fetch a proper token string from Facebook when asked for the app token" do
|
@@ -282,119 +297,123 @@ describe "Koala::Facebook::OAuth" do
|
|
282
297
|
end
|
283
298
|
|
284
299
|
describe "for exchanging session keys" do
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
options
|
322
|
-
|
323
|
-
|
324
|
-
|
300
|
+
if KoalaTest.session_key
|
301
|
+
describe "with get_token_info_from_session_keys" do
|
302
|
+
it "should get an array of session keys from Facebook when passed a single key" do
|
303
|
+
result = @oauth.get_tokens_from_session_keys([KoalaTest.session_key])
|
304
|
+
result.should be_an(Array)
|
305
|
+
result.length.should == 1
|
306
|
+
end
|
307
|
+
|
308
|
+
it "should get an array of session keys from Facebook when passed multiple keys" do
|
309
|
+
result = @oauth.get_tokens_from_session_keys(@multiple_session_keys)
|
310
|
+
result.should be_an(Array)
|
311
|
+
result.length.should == 2
|
312
|
+
end
|
313
|
+
|
314
|
+
it "should return the original hashes" do
|
315
|
+
result = @oauth.get_token_info_from_session_keys(@multiple_session_keys)
|
316
|
+
result[0].should be_a(Hash)
|
317
|
+
end
|
318
|
+
|
319
|
+
it "should properly handle invalid session keys" do
|
320
|
+
result = @oauth.get_token_info_from_session_keys(["foo", "bar"])
|
321
|
+
#it should return nil for each of the invalid ones
|
322
|
+
result.each {|r| r.should be_nil}
|
323
|
+
end
|
324
|
+
|
325
|
+
it "should properly handle a mix of valid and invalid session keys" do
|
326
|
+
result = @oauth.get_token_info_from_session_keys(["foo"].concat(@multiple_session_keys))
|
327
|
+
# it should return nil for each of the invalid ones
|
328
|
+
result.each_with_index {|r, index| index > 0 ? r.should(be_a(Hash)) : r.should(be_nil)}
|
329
|
+
end
|
330
|
+
|
331
|
+
it "should throw an APIError if Facebook returns an empty body (as happens for instance when the API breaks)" do
|
332
|
+
@oauth.should_receive(:fetch_token_string).and_return("")
|
333
|
+
lambda { @oauth.get_token_info_from_session_keys(@multiple_session_keys) }.should raise_error(Koala::Facebook::APIError)
|
334
|
+
end
|
335
|
+
|
336
|
+
it "should pass on any options provided to make_request" do
|
337
|
+
options = {:a => 2}
|
338
|
+
Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "[{}]", {}))
|
339
|
+
@oauth.get_token_info_from_session_keys([], options)
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
describe "with get_tokens_from_session_keys" do
|
344
|
+
it "should call get_token_info_from_session_keys" do
|
345
|
+
args = @multiple_session_keys
|
346
|
+
@oauth.should_receive(:get_token_info_from_session_keys).with(args, anything).and_return([])
|
347
|
+
@oauth.get_tokens_from_session_keys(args)
|
348
|
+
end
|
349
|
+
|
350
|
+
it "should return an array of strings" do
|
351
|
+
args = @multiple_session_keys
|
352
|
+
result = @oauth.get_tokens_from_session_keys(args)
|
353
|
+
result.each {|r| r.should be_a(String) }
|
354
|
+
end
|
355
|
+
|
356
|
+
it "should properly handle invalid session keys" do
|
357
|
+
result = @oauth.get_tokens_from_session_keys(["foo", "bar"])
|
358
|
+
# it should return nil for each of the invalid ones
|
359
|
+
result.each {|r| r.should be_nil}
|
360
|
+
end
|
361
|
+
|
362
|
+
it "should properly handle a mix of valid and invalid session keys" do
|
363
|
+
result = @oauth.get_tokens_from_session_keys(["foo"].concat(@multiple_session_keys))
|
364
|
+
# it should return nil for each of the invalid ones
|
365
|
+
result.each_with_index {|r, index| index > 0 ? r.should(be_a(String)) : r.should(be_nil)}
|
366
|
+
end
|
367
|
+
|
368
|
+
it "should pass on any options provided to make_request" do
|
369
|
+
options = {:a => 2}
|
370
|
+
Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "[{}]", {}))
|
371
|
+
@oauth.get_tokens_from_session_keys([], options)
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
describe "get_token_from_session_key" do
|
376
|
+
it "should call get_tokens_from_session_keys when the get_token_from_session_key is called" do
|
377
|
+
key = KoalaTest.session_key
|
378
|
+
@oauth.should_receive(:get_tokens_from_session_keys).with([key], anything).and_return([])
|
379
|
+
@oauth.get_token_from_session_key(key)
|
380
|
+
end
|
381
|
+
|
382
|
+
it "should get back the access token string from get_token_from_session_key" do
|
383
|
+
result = @oauth.get_token_from_session_key(KoalaTest.session_key)
|
384
|
+
result.should be_a(String)
|
385
|
+
end
|
386
|
+
|
387
|
+
it "should be the first value in the array" do
|
388
|
+
result = @oauth.get_token_from_session_key(KoalaTest.session_key)
|
389
|
+
array = @oauth.get_tokens_from_session_keys([KoalaTest.session_key])
|
390
|
+
result.should == array[0]
|
391
|
+
end
|
392
|
+
|
393
|
+
it "should properly handle an invalid session key" do
|
394
|
+
result = @oauth.get_token_from_session_key("foo")
|
395
|
+
result.should be_nil
|
396
|
+
end
|
397
|
+
|
398
|
+
it "should pass on any options provided to make_request" do
|
399
|
+
options = {:a => 2}
|
400
|
+
Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "[{}]", {}))
|
401
|
+
@oauth.get_token_from_session_key("", options)
|
402
|
+
end
|
403
|
+
end
|
404
|
+
else
|
405
|
+
it "Session key exchange tests will not be run since the session key in facebook_data.yml is blank."
|
325
406
|
end
|
326
|
-
|
327
|
-
describe "with get_tokens_from_session_keys" do
|
328
|
-
it "should call get_token_info_from_session_keys" do
|
329
|
-
args = @oauth_data["multiple_session_keys"]
|
330
|
-
@oauth.should_receive(:get_token_info_from_session_keys).with(args, anything).and_return([])
|
331
|
-
@oauth.get_tokens_from_session_keys(args)
|
332
|
-
end
|
333
|
-
|
334
|
-
it "should return an array of strings" do
|
335
|
-
args = @oauth_data["multiple_session_keys"]
|
336
|
-
result = @oauth.get_tokens_from_session_keys(args)
|
337
|
-
result.each {|r| r.should be_a(String) }
|
338
|
-
end
|
339
|
-
|
340
|
-
it "should properly handle invalid session keys" do
|
341
|
-
result = @oauth.get_tokens_from_session_keys(["foo", "bar"])
|
342
|
-
# it should return nil for each of the invalid ones
|
343
|
-
result.each {|r| r.should be_nil}
|
344
|
-
end
|
345
|
-
|
346
|
-
it "should properly handle a mix of valid and invalid session keys" do
|
347
|
-
result = @oauth.get_tokens_from_session_keys(["foo"].concat(@oauth_data["multiple_session_keys"]))
|
348
|
-
# it should return nil for each of the invalid ones
|
349
|
-
result.each_with_index {|r, index| index > 0 ? r.should(be_a(String)) : r.should(be_nil)}
|
350
|
-
end
|
351
|
-
|
352
|
-
it "should pass on any options provided to make_request" do
|
353
|
-
options = {:a => 2}
|
354
|
-
Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "[{}]", {}))
|
355
|
-
@oauth.get_tokens_from_session_keys([], options)
|
356
|
-
end
|
357
|
-
end
|
358
|
-
|
359
|
-
describe "get_token_from_session_key" do
|
360
|
-
it "should call get_tokens_from_session_keys when the get_token_from_session_key is called" do
|
361
|
-
key = @oauth_data["session_key"]
|
362
|
-
@oauth.should_receive(:get_tokens_from_session_keys).with([key], anything).and_return([])
|
363
|
-
@oauth.get_token_from_session_key(key)
|
364
|
-
end
|
365
|
-
|
366
|
-
it "should get back the access token string from get_token_from_session_key" do
|
367
|
-
result = @oauth.get_token_from_session_key(@oauth_data["session_key"])
|
368
|
-
result.should be_a(String)
|
369
|
-
end
|
370
|
-
|
371
|
-
it "should be the first value in the array" do
|
372
|
-
result = @oauth.get_token_from_session_key(@oauth_data["session_key"])
|
373
|
-
array = @oauth.get_tokens_from_session_keys([@oauth_data["session_key"]])
|
374
|
-
result.should == array[0]
|
375
|
-
end
|
376
|
-
|
377
|
-
it "should properly handle an invalid session key" do
|
378
|
-
result = @oauth.get_token_from_session_key("foo")
|
379
|
-
result.should be_nil
|
380
|
-
end
|
381
|
-
|
382
|
-
it "should pass on any options provided to make_request" do
|
383
|
-
options = {:a => 2}
|
384
|
-
Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "[{}]", {}))
|
385
|
-
@oauth.get_token_from_session_key("", options)
|
386
|
-
end
|
387
|
-
end
|
388
407
|
end
|
389
|
-
|
408
|
+
|
390
409
|
describe "for parsing signed requests" do
|
391
410
|
# the signed request code is ported directly from Facebook
|
392
|
-
# so we only need to test at a high level that it works
|
411
|
+
# so we only need to test at a high level that it works
|
393
412
|
it "should throw an error if the algorithm is unsupported" do
|
394
413
|
MultiJson.stub(:decode).and_return("algorithm" => "my fun algorithm")
|
395
414
|
lambda { @oauth.parse_signed_request(@signed_request) }.should raise_error
|
396
415
|
end
|
397
|
-
|
416
|
+
|
398
417
|
it "should throw an error if the signature is invalid" do
|
399
418
|
OpenSSL::HMAC.stub!(:hexdigest).and_return("i'm an invalid signature")
|
400
419
|
lambda { @oauth.parse_signed_request(@signed_request) }.should raise_error
|