cotweet_koala 0.8.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,308 @@
1
+ # stub the Time class to always return a time for which the valid cookie is still valid
2
+ class Time
3
+ def self.now
4
+ self
5
+ end
6
+
7
+ def self.to_i
8
+ 1273363199
9
+ end
10
+ end
11
+
12
+ class FacebookOAuthTests < Test::Unit::TestCase
13
+ describe "Koala GraphAPI without an access token" do
14
+ before :each do
15
+ # make the relevant test data easily accessible
16
+ @oauth_data = $testing_data["oauth_test_data"]
17
+ @app_id = @oauth_data["app_id"]
18
+ @secret = @oauth_data["secret"]
19
+ @code = @oauth_data["code"]
20
+ @callback_url = @oauth_data["callback_url"]
21
+ @raw_token_string = @oauth_data["raw_token_string"]
22
+ @raw_offline_access_token_string = @oauth_data["raw_offline_access_token_string"]
23
+
24
+ # this should expanded to cover all variables
25
+ raise Exception, "Must supply app data to run FacebookOAuthTests!" unless @app_id && @secret && @callback_url &&
26
+ @code && @raw_token_string &&
27
+ @raw_offline_access_token_string
28
+
29
+ @oauth = Koala::Facebook::OAuth.new(@app_id, @secret, @callback_url)
30
+ end
31
+
32
+ # initialization
33
+ it "should properly initialize" do
34
+ @oauth.should
35
+ end
36
+
37
+ it "should properly set attributes" do
38
+ (@oauth.app_id == @app_id &&
39
+ @oauth.app_secret == @secret &&
40
+ @oauth.oauth_callback_url == @callback_url).should be_true
41
+ end
42
+
43
+ it "should properly initialize without a callback_url" do
44
+ @oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
45
+ end
46
+
47
+ it "should properly set attributes without a callback URL" do
48
+ @oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
49
+ (@oauth.app_id == @app_id &&
50
+ @oauth.app_secret == @secret &&
51
+ @oauth.oauth_callback_url == nil).should be_true
52
+ end
53
+
54
+ describe "cookie parsing" do
55
+ describe "get_user_info_from_cookies" do
56
+ it "should properly parse valid cookies" do
57
+ result = @oauth.get_user_info_from_cookies(@oauth_data["valid_cookies"])
58
+ result.should be_a(Hash)
59
+ end
60
+
61
+ it "should return all the cookie components from valid cookie string" do
62
+ cookie_data = @oauth_data["valid_cookies"]
63
+ parsing_results = @oauth.get_user_info_from_cookies(cookie_data)
64
+ number_of_components = cookie_data["fbs_#{@app_id.to_s}"].scan(/\=/).length
65
+ parsing_results.length.should == number_of_components
66
+ end
67
+
68
+ it "should properly parse valid offline access cookies (e.g. no expiration)" do
69
+ result = @oauth.get_user_info_from_cookies(@oauth_data["offline_access_cookies"])
70
+ result["uid"].should
71
+ end
72
+
73
+ it "should return all the cookie components from offline access cookies" do
74
+ cookie_data = @oauth_data["offline_access_cookies"]
75
+ parsing_results = @oauth.get_user_info_from_cookies(cookie_data)
76
+ number_of_components = cookie_data["fbs_#{@app_id.to_s}"].scan(/\=/).length
77
+ parsing_results.length.should == number_of_components
78
+ end
79
+
80
+ it "shouldn't parse expired cookies" do
81
+ result = @oauth.get_user_info_from_cookies(@oauth_data["expired_cookies"])
82
+ result.should be_nil
83
+ end
84
+
85
+ it "shouldn't parse invalid cookies" do
86
+ # make an invalid string by replacing some values
87
+ bad_cookie_hash = @oauth_data["valid_cookies"].inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
88
+ result = @oauth.get_user_info_from_cookies(bad_cookie_hash)
89
+ result.should be_nil
90
+ end
91
+ end
92
+
93
+ describe "get_user_from_cookies" do
94
+ it "should use get_user_info_from_cookies to parse the cookies" do
95
+ data = @oauth_data["valid_cookies"]
96
+ @oauth.should_receive(:get_user_info_from_cookies).with(data).and_return({})
97
+ @oauth.get_user_from_cookies(data)
98
+ end
99
+
100
+ it "should use return a string if the cookies are valid" do
101
+ result = @oauth.get_user_from_cookies(@oauth_data["valid_cookies"])
102
+ result.should be_a(String)
103
+ end
104
+
105
+ it "should return nil if the cookies are invalid" do
106
+ # make an invalid string by replacing some values
107
+ bad_cookie_hash = @oauth_data["valid_cookies"].inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
108
+ result = @oauth.get_user_from_cookies(bad_cookie_hash)
109
+ result.should be_nil
110
+ end
111
+ end
112
+ end
113
+
114
+ # OAuth URLs
115
+
116
+ # url_for_oauth_code
117
+ it "should generate a properly formatted OAuth code URL with the default values" do
118
+ url = @oauth.url_for_oauth_code
119
+ url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}"
120
+ end
121
+
122
+ it "should generate a properly formatted OAuth code URL when a callback is given" do
123
+ callback = "foo.com"
124
+ url = @oauth.url_for_oauth_code(:callback => callback)
125
+ url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{callback}"
126
+ end
127
+
128
+ it "should generate a properly formatted OAuth code URL when permissions are requested as a string" do
129
+ permissions = "publish_stream,read_stream"
130
+ url = @oauth.url_for_oauth_code(:permissions => permissions)
131
+ url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}&scope=#{permissions}"
132
+ end
133
+
134
+ it "should generate a properly formatted OAuth code URL when permissions are requested as a string" do
135
+ permissions = ["publish_stream", "read_stream"]
136
+ url = @oauth.url_for_oauth_code(:permissions => permissions)
137
+ url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{@callback_url}&scope=#{permissions.join(",")}"
138
+ end
139
+
140
+ it "should generate a properly formatted OAuth code URL when both permissions and callback are provided" do
141
+ permissions = "publish_stream,read_stream"
142
+ callback = "foo.com"
143
+ url = @oauth.url_for_oauth_code(:callback => callback, :permissions => permissions)
144
+ url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{callback}&scope=#{permissions}"
145
+ end
146
+
147
+ it "should raise an exception if no callback is given in initialization or the call" do
148
+ oauth2 = Koala::Facebook::OAuth.new(@app_id, @secret)
149
+ lambda { oauth2.url_for_oauth_code }.should raise_error(ArgumentError)
150
+ end
151
+
152
+ # url_for_access_token
153
+ it "should generate a properly formatted OAuth token URL when provided a code" do
154
+ url = @oauth.url_for_access_token(@code)
155
+ url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{@callback_url}&client_secret=#{@secret}&code=#{@code}"
156
+ end
157
+
158
+ it "should generate a properly formatted OAuth token URL when provided a callback" do
159
+ callback = "foo.com"
160
+ url = @oauth.url_for_access_token(@code, :callback => callback)
161
+ url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{callback}&client_secret=#{@secret}&code=#{@code}"
162
+ end
163
+
164
+ describe "get_access_token_info" do
165
+ it "should properly get and parse an access token token results into a hash" do
166
+ result = @oauth.get_access_token_info(@code)
167
+ result.should be_a(Hash)
168
+ end
169
+
170
+ it "should properly include the access token results" do
171
+ result = @oauth.get_access_token_info(@code)
172
+ result["access_token"].should
173
+ end
174
+
175
+ it "should raise an error when get_access_token is called with a bad code" do
176
+ lambda { @oauth.get_access_token_info("foo") }.should raise_error(Koala::Facebook::APIError)
177
+ end
178
+ end
179
+
180
+ describe "get_access_token" do
181
+ it "should use get_access_token_info to get and parse an access token token results" do
182
+ result = @oauth.get_access_token(@code)
183
+ result.should be_a(String)
184
+ end
185
+
186
+ it "should return the access token as a string" do
187
+ result = @oauth.get_access_token(@code)
188
+ original = @oauth.get_access_token_info(@code)
189
+ result.should == original["access_token"]
190
+ end
191
+
192
+ it "should raise an error when get_access_token is called with a bad code" do
193
+ lambda { @oauth.get_access_token("foo") }.should raise_error(Koala::Facebook::APIError)
194
+ end
195
+ end
196
+
197
+ describe "get_app_access_token_info" do
198
+ it "should properly get and parse an app's access token as a hash" do
199
+ result = @oauth.get_app_access_token_info
200
+ result.should be_a(Hash)
201
+ end
202
+
203
+ it "should include the access token" do
204
+ result = @oauth.get_app_access_token_info
205
+ result["access_token"].should
206
+ end
207
+ end
208
+
209
+ describe "get_app_acess_token" do
210
+ it "should use get_access_token_info to get and parse an access token token results" do
211
+ result = @oauth.get_app_access_token
212
+ result.should be_a(String)
213
+ end
214
+
215
+ it "should return the access token as a string" do
216
+ result = @oauth.get_app_access_token
217
+ original = @oauth.get_app_access_token_info
218
+ result.should == original["access_token"]
219
+ end
220
+ end
221
+
222
+ describe "exchanging session keys" do
223
+ describe "with get_token_info_from_session_keys" do
224
+ it "should get an array of session keys from Facebook when passed a single key" do
225
+ result = @oauth.get_tokens_from_session_keys([@oauth_data["session_key"]])
226
+ result.should be_an(Array)
227
+ result.length.should == 1
228
+ end
229
+
230
+ it "should get an array of session keys from Facebook when passed multiple keys" do
231
+ result = @oauth.get_tokens_from_session_keys(@oauth_data["multiple_session_keys"])
232
+ result.should be_an(Array)
233
+ result.length.should == 2
234
+ end
235
+
236
+ it "should return the original hashes" do
237
+ result = @oauth.get_token_info_from_session_keys(@oauth_data["multiple_session_keys"])
238
+ result[0].should be_a(Hash)
239
+ end
240
+ end
241
+
242
+ describe "with get_tokens_from_session_keys" do
243
+ it "should call get_token_info_from_session_keys" do
244
+ args = @oauth_data["multiple_session_keys"]
245
+ @oauth.should_receive(:get_token_info_from_session_keys).with(args).and_return([])
246
+ @oauth.get_tokens_from_session_keys(args)
247
+ end
248
+
249
+ it "should return an array of strings" do
250
+ args = @oauth_data["multiple_session_keys"]
251
+ result = @oauth.get_tokens_from_session_keys(args)
252
+ result.each {|r| r.should be_a(String) }
253
+ end
254
+ end
255
+
256
+ describe "get_token_from_session_key" do
257
+ it "should call get_tokens_from_session_keys when the get_token_from_session_key is called" do
258
+ key = @oauth_data["session_key"]
259
+ @oauth.should_receive(:get_tokens_from_session_keys).with([key]).and_return([])
260
+ @oauth.get_token_from_session_key(key)
261
+ end
262
+
263
+ it "should get back the access token string from get_token_from_session_key" do
264
+ result = @oauth.get_token_from_session_key(@oauth_data["session_key"])
265
+ result.should be_a(String)
266
+ end
267
+
268
+ it "should be the first value in the array" do
269
+ result = @oauth.get_token_from_session_key(@oauth_data["session_key"])
270
+ array = @oauth.get_tokens_from_session_keys([@oauth_data["session_key"]])
271
+ result.should == array[0]
272
+ end
273
+ end
274
+ end
275
+
276
+ # protected methods
277
+ # since these are pretty fundamental and pretty testable, we want to test them
278
+
279
+ # parse_access_token
280
+ it "should properly parse access token results" do
281
+ result = @oauth.send(:parse_access_token, @raw_token_string)
282
+ has_both_parts = result["access_token"] && result["expires"]
283
+ has_both_parts.should
284
+ end
285
+
286
+ it "should properly parse offline access token results" do
287
+ result = @oauth.send(:parse_access_token, @raw_offline_access_token_string)
288
+ has_both_parts = result["access_token"] && !result["expires"]
289
+ has_both_parts.should
290
+ end
291
+
292
+ # fetch_token_string
293
+ # somewhat duplicative with the tests for get_access_token and get_app_access_token
294
+ # but no harm in thoroughness
295
+ it "should fetch a proper token string from Facebook when given a code" do
296
+ result = @oauth.send(:fetch_token_string, :code => @code, :redirect_uri => @callback_url)
297
+ result.should =~ /^access_token/
298
+ end
299
+
300
+ it "should fetch a proper token string from Facebook when asked for the app token" do
301
+ result = @oauth.send(:fetch_token_string, {:type => 'client_cred'}, true)
302
+ result.should =~ /^access_token/
303
+ end
304
+
305
+ # END CODE THAT NEEDS MOCKING
306
+ end # describe
307
+
308
+ end #class
@@ -0,0 +1,187 @@
1
+ class FacebookRealtimeUpdatesTests < Test::Unit::TestCase
2
+ include Koala
3
+
4
+ describe "Koala RealtimeUpdates" do
5
+ before :all do
6
+ # get oauth data
7
+ @oauth_data = $testing_data["oauth_test_data"]
8
+ @app_id = @oauth_data["app_id"]
9
+ @secret = @oauth_data["secret"]
10
+ @callback_url = @oauth_data["callback_url"]
11
+ @app_access_token = @oauth_data["app_access_token"]
12
+
13
+ # check OAuth data
14
+ unless @app_id && @secret && @callback_url && @app_access_token
15
+ raise Exception, "Must supply OAuth app id, secret, app_access_token, and callback to run live subscription tests!"
16
+ end
17
+
18
+ # get subscription data
19
+ @subscription_data = $testing_data["subscription_test_data"]
20
+ @verify_token = @subscription_data["verify_token"]
21
+ @challenge_data = @subscription_data["challenge_data"]
22
+ @subscription_path = @subscription_data["subscription_path"]
23
+
24
+ # check subscription data
25
+ unless @verify_token && @challenge_data && @subscription_path
26
+ raise Exception, "Must supply verify_token and equivalent challenge_data to run live subscription tests!"
27
+ end
28
+ end
29
+
30
+ describe "when initializing" do
31
+ # basic initialization
32
+ it "should initialize properly with an app_id and an app_access_token" do
33
+ updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
34
+ updates.should be_a(Facebook::RealtimeUpdates)
35
+ end
36
+
37
+ # attributes
38
+ it "should allow read access to app_id, app_access_token, and secret" do
39
+ updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
40
+ # this should not throw errors
41
+ updates.app_id && updates.app_access_token && updates.secret
42
+ end
43
+
44
+ it "should not allow write access to app_id" do
45
+ updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
46
+ # this should not throw errors
47
+ lambda { updates.app_id = 2 }.should raise_error(NoMethodError)
48
+ end
49
+
50
+ it "should not allow write access to app_access_token" do
51
+ updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
52
+ # this should not throw errors
53
+ lambda { updates.app_access_token = 2 }.should raise_error(NoMethodError)
54
+ end
55
+
56
+ it "should not allow write access to secret" do
57
+ updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
58
+ # this should not throw errors
59
+ lambda { updates.secret = 2 }.should raise_error(NoMethodError)
60
+ end
61
+
62
+ # init with secret / fetching the token
63
+ it "should initialize properly with an app_id and a secret" do
64
+ updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
65
+ updates.should be_a(Facebook::RealtimeUpdates)
66
+ end
67
+
68
+ it "should fetch an app_token from Facebook when provided an app_id and a secret" do
69
+ updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
70
+ updates.app_access_token.should_not be_nil
71
+ end
72
+
73
+ it "should use the OAuth class to fetch a token when provided an app_id and a secret" do
74
+ oauth = Facebook::OAuth.new(@app_id, @secret)
75
+ token = oauth.get_app_access_token
76
+ oauth.should_receive(:get_app_access_token).and_return(token)
77
+ Facebook::OAuth.should_receive(:new).with(@app_id, @secret).and_return(oauth)
78
+ updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
79
+ end
80
+ end
81
+
82
+ describe "when used" do
83
+ before :each do
84
+ @updates = Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
85
+ end
86
+
87
+ it "should send a subscription request to a valid server" do
88
+ result = @updates.subscribe("user", "name", @subscription_path, @verify_token)
89
+ result.should be_true
90
+ end
91
+
92
+ it "should send a subscription request to a valid server" do
93
+ result = @updates.subscribe("user", "name", @subscription_path, @verify_token)
94
+ result.should be_true
95
+ end
96
+
97
+ it "should send a subscription request to an invalid path on a valid server" do
98
+ lambda { result = @updates.subscribe("user", "name", @subscription_path + "foo", @verify_token) }.should raise_exception(Koala::Facebook::APIError)
99
+ end
100
+
101
+ it "should fail to send a subscription request to an invalid server" do
102
+ lambda { @updates.subscribe("user", "name", "foo", @verify_token) }.should raise_exception(Koala::Facebook::APIError)
103
+ end
104
+
105
+ it "should unsubscribe a valid individual object successfully" do
106
+ @updates.unsubscribe("user").should be_true
107
+ end
108
+
109
+ it "should unsubscribe all subscriptions successfully" do
110
+ @updates.unsubscribe.should be_true
111
+ end
112
+
113
+ it "should fail when an invalid object is provided to unsubscribe" do
114
+ lambda { @updates.unsubscribe("kittens") }.should raise_error(Koala::Facebook::APIError)
115
+ end
116
+
117
+ it "should is subscriptions properly" do
118
+ @updates.list_subscriptions.should be_a(Array)
119
+ end
120
+ end # describe "when used"
121
+
122
+ describe "when meeting challenge" do
123
+ it "should return false if hub.mode isn't subscribe" do
124
+ params = {'hub.mode' => 'not subscribe'}
125
+ Facebook::RealtimeUpdates.meet_challenge(params).should be_false
126
+ end
127
+
128
+ it "should return false if not given a verify_token or block" do
129
+ params = {'hub.mode' => 'subscribe'}
130
+ Facebook::RealtimeUpdates.meet_challenge(params).should be_false
131
+ end
132
+
133
+ describe "and mode is 'subscribe'" do
134
+ before(:each) do
135
+ @params = {'hub.mode' => 'subscribe'}
136
+ end
137
+
138
+ describe "and a token is given" do
139
+ before(:each) do
140
+ @token = 'token'
141
+ @params['hub.verify_token'] = @token
142
+ end
143
+
144
+ it "should return false if the given verify token doesn't match" do
145
+ Facebook::RealtimeUpdates.meet_challenge(@params, @token + '1').should be_false
146
+ end
147
+
148
+ it "should return the challenge if the given verify token matches" do
149
+ @params['hub.challenge'] = 'challenge val'
150
+ Facebook::RealtimeUpdates.meet_challenge(@params, @token).should == @params['hub.challenge']
151
+ end
152
+ end
153
+
154
+ describe "and a block is given" do
155
+ it "should give the block the token as a parameter" do
156
+ Facebook::RealtimeUpdates.meet_challenge(@params)do |token|
157
+ token.should == @token
158
+ end
159
+ end
160
+
161
+ it "should return false if the given block return false" do
162
+ Facebook::RealtimeUpdates.meet_challenge(@params)do |token|
163
+ false
164
+ end.should be_false
165
+ end
166
+
167
+ it "should return false if the given block returns nil" do
168
+ Facebook::RealtimeUpdates.meet_challenge(@params)do |token|
169
+ nil
170
+ end.should be_false
171
+ end
172
+
173
+ it "should return the challenge if the given block returns true" do
174
+ @params['hub.challenge'] = 'challenge val'
175
+ Facebook::RealtimeUpdates.meet_challenge(@params) do |token|
176
+ true
177
+ end.should be_true
178
+ end
179
+ end
180
+
181
+ end # describe "and mode is subscribe"
182
+
183
+ end # describe "when meeting challenge"
184
+
185
+ end # describe
186
+
187
+ end #class