koala 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +10 -0
- data/Manifest +16 -5
- data/Rakefile +1 -1
- data/koala.gemspec +4 -4
- data/lib/graph_api.rb +17 -12
- data/lib/http_services.rb +12 -2
- data/lib/koala.rb +50 -24
- data/lib/realtime_updates.rb +95 -0
- data/lib/rest_api.rb +7 -12
- data/readme.md +71 -25
- data/spec/facebook_data.yml +12 -3
- data/spec/koala/api_base_tests.rb +44 -0
- data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +10 -0
- data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +11 -0
- data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +101 -0
- data/spec/koala/{facebook_with_access_token_tests.rb → graph_api/graph_api_with_access_token_tests.rb} +40 -44
- data/spec/koala/live_testing_data_helper.rb +15 -0
- data/spec/koala/net_http_service_tests.rb +15 -0
- data/spec/koala/{facebook_oauth_tests.rb → oauth/oauth_tests.rb} +31 -13
- data/spec/koala/realtime_updates/realtime_updates_tests.rb +187 -0
- data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +94 -0
- data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +36 -0
- data/spec/koala_spec.rb +18 -38
- data/spec/koala_spec_helper.rb +30 -0
- data/spec/koala_spec_without_mocks.rb +19 -0
- data/spec/mock_facebook_responses.yml +208 -0
- data/spec/mock_http_service.rb +76 -0
- metadata +20 -8
- data/spec/koala/facebook_no_access_token_tests.rb +0 -88
- data/spec/koala/facebook_rest_api_no_access_token_test.rb +0 -20
- data/spec/koala/facebook_rest_api_with_access_token_test.rb +0 -33
@@ -0,0 +1,11 @@
|
|
1
|
+
class GraphAndRestAPIWithTokenTests < Test::Unit::TestCase
|
2
|
+
describe "Koala GraphAndRestAPI without an access token" do
|
3
|
+
it_should_behave_like "live testing examples"
|
4
|
+
it_should_behave_like "Koala RestAPI with an access token"
|
5
|
+
it_should_behave_like "Koala GraphAPI with an access token"
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@api = Koala::Facebook::GraphAndRestAPI.new(@token)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
shared_examples_for "Koala GraphAPI without an access token" do
|
2
|
+
it "should get public data about a user" do
|
3
|
+
result = @api.get_object("koppel")
|
4
|
+
# the results should have an ID and a name, among other things
|
5
|
+
(result["id"] && result["name"]).should
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should not get private data about a user" do
|
9
|
+
result = @api.get_object("koppel")
|
10
|
+
# updated_time should be a pretty fixed test case
|
11
|
+
result["updated_time"].should be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should get public data about a Page" do
|
15
|
+
result = @api.get_object("contextoptional")
|
16
|
+
# the results should have an ID and a name, among other things
|
17
|
+
(result["id"] && result["name"]).should
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not be able to get data about 'me'" do
|
21
|
+
lambda { @api.get_object("me") }.should raise_error(Koala::Facebook::APIError)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should be able to get multiple objects" do
|
25
|
+
results = @api.get_objects(["contextoptional", "naitik"])
|
26
|
+
results.length.should == 2
|
27
|
+
end
|
28
|
+
|
29
|
+
it "shouldn't be able to access connections from users" do
|
30
|
+
lambda { @api.get_connections("lukeshepard", "likes") }.should raise_error(Koala::Facebook::APIError)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should be able to access a user's picture" do
|
34
|
+
@api.get_picture("chris.baclig").should =~ /http\:\/\//
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should be able to access connections from public Pages" do
|
38
|
+
result = @api.get_connections("contextoptional", "likes")
|
39
|
+
result["data"].should be_a(Array)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should not be able to put an object" do
|
43
|
+
lambda { @result = @api.put_object("lukeshepard", "feed", :message => "Hello, world") }.should raise_error(Koala::Facebook::APIError)
|
44
|
+
puts "Error! Object #{@result.inspect} somehow put onto Luke Shepard's wall!" if @result
|
45
|
+
end
|
46
|
+
|
47
|
+
# these are not strictly necessary as the other put methods resolve to put_object, but are here for completeness
|
48
|
+
it "should not be able to post to a feed" do
|
49
|
+
(lambda do
|
50
|
+
attachment = {:name => "Context Optional", :link => "http://www.contextoptional.com/"}
|
51
|
+
@result = @api.put_wall_post("Hello, world", attachment, "contextoptional")
|
52
|
+
end).should raise_error(Koala::Facebook::APIError)
|
53
|
+
puts "Error! Object #{@result.inspect} somehow put onto Context Optional's wall!" if @result
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should not be able to comment on an object" do
|
57
|
+
# random public post on the ContextOptional wall
|
58
|
+
lambda { @result = @api.put_comment("7204941866_119776748033392", "The hackathon was great!") }.should raise_error(Koala::Facebook::APIError)
|
59
|
+
puts "Error! Object #{@result.inspect} somehow commented on post 7204941866_119776748033392!" if @result
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should not be able to like an object" do
|
63
|
+
lambda { @api.put_like("7204941866_119776748033392") }.should raise_error(Koala::Facebook::APIError)
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
# DELETE
|
68
|
+
it "should not be able to delete posts" do
|
69
|
+
# test post on the Ruby SDK Test application
|
70
|
+
lambda { @result = @api.delete_object("115349521819193_113815981982767") }.should raise_error(Koala::Facebook::APIError)
|
71
|
+
end
|
72
|
+
|
73
|
+
# SEARCH
|
74
|
+
it "should be able to search" do
|
75
|
+
result = @api.search("facebook")
|
76
|
+
result["data"].should be_an(Array)
|
77
|
+
end
|
78
|
+
|
79
|
+
# API
|
80
|
+
it "should never use the rest api server" do
|
81
|
+
Koala.should_receive(:make_request).with(
|
82
|
+
anything,
|
83
|
+
anything,
|
84
|
+
anything,
|
85
|
+
hash_not_including(:rest_api => true)
|
86
|
+
).and_return(Koala::Response.new(200, "", {}))
|
87
|
+
|
88
|
+
@api.api("anything")
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class FacebookNoAccessTokenTests < Test::Unit::TestCase
|
93
|
+
describe "Koala GraphAPI without an access token" do
|
94
|
+
before :each do
|
95
|
+
@api = Koala::Facebook::GraphAPI.new
|
96
|
+
end
|
97
|
+
|
98
|
+
it_should_behave_like "Koala GraphAPI without an access token"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
@@ -1,95 +1,83 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
token = $testing_data["oauth_token"]
|
5
|
-
raise Exception, "Must supply access token to run FacebookWithAccessTokenTests!" unless token
|
6
|
-
@graph = Koala::Facebook::GraphAPI.new(token)
|
7
|
-
end
|
8
|
-
|
9
|
-
after :each do
|
10
|
-
# clean up any temporary objects
|
11
|
-
if @temporary_object_id
|
12
|
-
puts "\nCleaning up temporary object #{@temporary_object_id.to_s}"
|
13
|
-
result = @graph.delete_object(@temporary_object_id)
|
14
|
-
raise "Unable to clean up temporary Graph object #{@temporary_object_id}!" unless result
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should get public data about a user" do
|
19
|
-
result = @graph.get_object("koppel")
|
1
|
+
shared_examples_for "Koala GraphAPI with an access token" do
|
2
|
+
it "should get public data about a user" do
|
3
|
+
result = @api.get_object("koppel")
|
20
4
|
# the results should have an ID and a name, among other things
|
21
5
|
(result["id"] && result["name"]).should_not be_nil
|
22
6
|
end
|
23
7
|
|
24
8
|
it "should get private data about a user" do
|
25
|
-
result = @
|
9
|
+
result = @api.get_object("koppel")
|
26
10
|
# updated_time should be a pretty fixed test case
|
27
11
|
result["updated_time"].should_not be_nil
|
28
12
|
end
|
29
13
|
|
30
14
|
it "should get public data about a Page" do
|
31
|
-
result = @
|
15
|
+
result = @api.get_object("contextoptional")
|
32
16
|
# the results should have an ID and a name, among other things
|
33
17
|
(result["id"] && result["name"]).should
|
34
18
|
end
|
35
19
|
|
36
20
|
it "should get data about 'me'" do
|
37
|
-
result = @
|
21
|
+
result = @api.get_object("me")
|
38
22
|
result["updated_time"].should
|
39
23
|
end
|
40
24
|
|
41
25
|
it "should be able to get multiple objects" do
|
42
|
-
result = @
|
26
|
+
result = @api.get_objects(["contextoptional", "naitik"])
|
43
27
|
result.length.should == 2
|
44
28
|
end
|
45
29
|
|
30
|
+
it "should be able to access a user's picture" do
|
31
|
+
@api.get_picture("chris.baclig").should =~ /http\:\/\//
|
32
|
+
end
|
33
|
+
|
46
34
|
it "should be able to access connections from users" do
|
47
|
-
result = @
|
35
|
+
result = @api.get_connections("lukeshepard", "likes")
|
48
36
|
result["data"].length.should > 0
|
49
37
|
end
|
50
38
|
|
51
39
|
it "should be able to access connections from public Pages" do
|
52
|
-
result = @
|
40
|
+
result = @api.get_connections("contextoptional", "likes")
|
53
41
|
result["data"].should be_a(Array)
|
54
42
|
end
|
55
43
|
|
56
44
|
# PUT
|
57
45
|
it "should be able to write an object to the graph" do
|
58
|
-
result = @
|
46
|
+
result = @api.put_wall_post("Hello, world, from the test suite!")
|
59
47
|
@temporary_object_id = result["id"]
|
60
48
|
@temporary_object_id.should_not be_nil
|
61
49
|
end
|
62
50
|
|
63
51
|
# DELETE
|
64
52
|
it "should be able to delete posts" do
|
65
|
-
result = @
|
53
|
+
result = @api.put_wall_post("Hello, world, from the test suite delete method!")
|
66
54
|
object_id_to_delete = result["id"]
|
67
|
-
delete_result = @
|
55
|
+
delete_result = @api.delete_object(object_id_to_delete)
|
68
56
|
delete_result.should == true
|
69
57
|
end
|
70
58
|
|
71
59
|
# additional put tests
|
72
60
|
it "should be able to verify messages posted to a wall" do
|
73
61
|
message = "the cats are asleep"
|
74
|
-
put_result = @
|
62
|
+
put_result = @api.put_wall_post(message)
|
75
63
|
@temporary_object_id = put_result["id"]
|
76
|
-
get_result = @
|
64
|
+
get_result = @api.get_object(@temporary_object_id)
|
77
65
|
|
78
66
|
# make sure the message we sent is the message that got posted
|
79
67
|
get_result["message"].should == message
|
80
68
|
end
|
81
69
|
|
82
70
|
it "should be able to post a message with an attachment to a feed" do
|
83
|
-
result = @
|
71
|
+
result = @api.put_wall_post("Hello, world, from the test suite again!", {:name => "Context Optional", :link => "http://www.contextoptional.com/"})
|
84
72
|
@temporary_object_id = result["id"]
|
85
73
|
@temporary_object_id.should_not be_nil
|
86
74
|
end
|
87
75
|
|
88
76
|
it "should be able to verify a message with an attachment posted to a feed" do
|
89
77
|
attachment = {"name" => "Context Optional", "link" => "http://www.contextoptional.com/"}
|
90
|
-
result = @
|
78
|
+
result = @api.put_wall_post("Hello, world, from the test suite again!", attachment)
|
91
79
|
@temporary_object_id = result["id"]
|
92
|
-
get_result = @
|
80
|
+
get_result = @api.get_object(@temporary_object_id)
|
93
81
|
|
94
82
|
# make sure the result we fetch includes all the parameters we sent
|
95
83
|
it_matches = attachment.inject(true) {|valid, param| valid && (get_result[param[0]] == attachment[param[0]])}
|
@@ -97,43 +85,51 @@ class FacebookWithAccessTokenTests < Test::Unit::TestCase
|
|
97
85
|
end
|
98
86
|
|
99
87
|
it "should be able to comment on an object" do
|
100
|
-
result = @
|
88
|
+
result = @api.put_wall_post("Hello, world, from the test suite, testing comments!")
|
101
89
|
@temporary_object_id = result["id"]
|
102
90
|
|
103
91
|
# this will be deleted when the post gets deleted
|
104
|
-
comment_result = @
|
92
|
+
comment_result = @api.put_comment(@temporary_object_id, "it's my comment!")
|
105
93
|
comment_result.should_not be_nil
|
106
94
|
end
|
107
95
|
|
108
96
|
it "should be able to verify a comment posted about an object" do
|
109
97
|
message_text = "Hello, world, from the test suite, testing comments!"
|
110
|
-
result = @
|
98
|
+
result = @api.put_wall_post(message_text)
|
111
99
|
@temporary_object_id = result["id"]
|
112
100
|
|
113
101
|
# this will be deleted when the post gets deleted
|
114
102
|
comment_text = "it's my comment!"
|
115
|
-
comment_result = @
|
116
|
-
get_result = @
|
103
|
+
comment_result = @api.put_comment(@temporary_object_id, comment_text)
|
104
|
+
get_result = @api.get_object(comment_result["id"])
|
117
105
|
|
118
106
|
# make sure the text of the comment matches what we sent
|
119
107
|
get_result["message"].should == comment_text
|
120
108
|
end
|
121
109
|
|
122
110
|
it "should be able to like an object" do
|
123
|
-
result = @
|
111
|
+
result = @api.put_wall_post("Hello, world, from the test suite, testing comments!")
|
124
112
|
@temporary_object_id = result["id"]
|
125
|
-
like_result = @
|
113
|
+
like_result = @api.put_like(@temporary_object_id)
|
126
114
|
end
|
127
115
|
|
128
116
|
# SEARCH
|
129
117
|
it "should be able to search" do
|
130
|
-
result = @
|
118
|
+
result = @api.search("facebook")
|
131
119
|
result["data"].should be_an(Array)
|
132
120
|
end
|
133
121
|
|
134
122
|
# API
|
135
123
|
# the above tests test this already, but we should consider additional api tests
|
136
|
-
|
137
|
-
end # describe
|
124
|
+
end
|
138
125
|
|
139
|
-
|
126
|
+
class FacebookWithAccessTokenTests < Test::Unit::TestCase
|
127
|
+
describe "Koala GraphAPI with an access token" do
|
128
|
+
it_should_behave_like "live testing examples"
|
129
|
+
it_should_behave_like "Koala GraphAPI with an access token"
|
130
|
+
|
131
|
+
before :each do
|
132
|
+
@api = Koala::Facebook::GraphAPI.new(@token)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
shared_examples_for "live testing examples" do
|
2
|
+
before :each do
|
3
|
+
@token = $testing_data["oauth_token"]
|
4
|
+
raise Exception, "Must supply access token to run FacebookWithAccessTokenTests!" unless @token
|
5
|
+
end
|
6
|
+
|
7
|
+
after :each do
|
8
|
+
# clean up any temporary objects
|
9
|
+
if @temporary_object_id
|
10
|
+
puts "\nCleaning up temporary object #{@temporary_object_id.to_s}"
|
11
|
+
result = @api.delete_object(@temporary_object_id)
|
12
|
+
raise "Unable to clean up temporary Graph object #{@temporary_object_id}!" unless result
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'http_services'
|
2
|
+
|
3
|
+
class NetHTTPServiceTests < Test::Unit::TestCase
|
4
|
+
module Bear
|
5
|
+
include Koala::NetHTTPService
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should define a make_request static module method" do
|
9
|
+
Bear.respond_to?(:make_request).should be_true
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should use POST if verb is not GET"
|
13
|
+
|
14
|
+
it "should return a Koala::Response object"
|
15
|
+
end
|
@@ -156,36 +156,54 @@ class FacebookOAuthTests < Test::Unit::TestCase
|
|
156
156
|
url.should == "https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{callback}&client_secret=#{@secret}&code=#{@code}"
|
157
157
|
out.should_not be_nil
|
158
158
|
end
|
159
|
+
|
160
|
+
# START CODE THAT NEEDS MOCKING
|
161
|
+
|
162
|
+
# get_access_token
|
163
|
+
it "should properly get and parse an access token token results" do
|
164
|
+
result = @oauth.get_access_token(@code)
|
165
|
+
result["access_token"].should
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should raise an error when get_access_token is called with a bad code" do
|
169
|
+
lambda { @oauth.get_access_token("foo") }.should raise_error(Koala::Facebook::APIError)
|
170
|
+
end
|
171
|
+
|
172
|
+
it "should properly get and parse an app's access token token results" do
|
173
|
+
result = @oauth.get_app_access_token
|
174
|
+
result["access_token"].should
|
175
|
+
end
|
176
|
+
|
177
|
+
# protected methods
|
178
|
+
# since these are pretty fundamental and pretty testable, we want to test them
|
159
179
|
|
160
180
|
# parse_access_token
|
161
181
|
it "should properly parse access token results" do
|
162
|
-
result = @oauth.parse_access_token
|
182
|
+
result = @oauth.send(:parse_access_token, @raw_token_string)
|
163
183
|
has_both_parts = result["access_token"] && result["expires"]
|
164
184
|
has_both_parts.should
|
165
185
|
end
|
166
186
|
|
167
187
|
it "should properly parse offline access token results" do
|
168
|
-
result = @oauth.parse_access_token
|
188
|
+
result = @oauth.send(:parse_access_token, @raw_offline_access_token_string)
|
169
189
|
has_both_parts = result["access_token"] && !result["expires"]
|
170
190
|
has_both_parts.should
|
171
191
|
end
|
172
|
-
|
192
|
+
|
173
193
|
# fetch_token_string
|
174
|
-
#
|
194
|
+
# somewhat duplicative with the tests for get_access_token and get_app_access_token
|
195
|
+
# but no harm in thoroughness
|
175
196
|
it "should fetch a proper token string from Facebook when given a code" do
|
176
|
-
result = @oauth.fetch_token_string
|
197
|
+
result = @oauth.send(:fetch_token_string, :code => @code, :redirect_uri => @callback_url)
|
177
198
|
result.should =~ /^access_token/
|
178
199
|
end
|
179
|
-
|
180
|
-
# get_access_token
|
181
|
-
it "should properly get and parse an access token token results" do
|
182
|
-
result = @oauth.get_access_token(@code)
|
183
|
-
result["access_token"].should
|
184
|
-
end
|
185
200
|
|
186
|
-
it "should
|
187
|
-
|
201
|
+
it "should fetch a proper token string from Facebook when asked for the app token" do
|
202
|
+
result = @oauth.send(:fetch_token_string, {:type => 'client_cred'}, true)
|
203
|
+
result.should =~ /^access_token/
|
188
204
|
end
|
205
|
+
|
206
|
+
# END CODE THAT NEEDS MOCKING
|
189
207
|
end # describe
|
190
208
|
|
191
209
|
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["data"].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
|
@@ -0,0 +1,94 @@
|
|
1
|
+
shared_examples_for "Koala RestAPI without an access token" 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
|
+
:rest_api => true
|
22
|
+
)
|
23
|
+
|
24
|
+
@api.rest_call('anything')
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should take an optional hash of arguments" do
|
28
|
+
args = {:arg1 => 'arg1'}
|
29
|
+
|
30
|
+
@api.should_receive(:api).with(
|
31
|
+
anything,
|
32
|
+
hash_including(args),
|
33
|
+
anything,
|
34
|
+
anything
|
35
|
+
)
|
36
|
+
|
37
|
+
@api.rest_call('anything', args)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should always ask for JSON" do
|
41
|
+
@api.should_receive(:api).with(
|
42
|
+
anything,
|
43
|
+
hash_including('format' => 'json'),
|
44
|
+
anything,
|
45
|
+
anything
|
46
|
+
)
|
47
|
+
|
48
|
+
@api.rest_call('anything')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# FQL_QUERY
|
53
|
+
describe "when making a FQL request" do
|
54
|
+
it "should call fql.query method" do
|
55
|
+
@api.should_receive(:rest_call).with(
|
56
|
+
"fql.query",
|
57
|
+
anything
|
58
|
+
).and_return(Koala::Response.new(200, "2", {}))
|
59
|
+
|
60
|
+
@api.fql_query stub('query string')
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should pass a query argument" do
|
64
|
+
query = stub('query string')
|
65
|
+
|
66
|
+
@api.should_receive(:rest_call).with(
|
67
|
+
anything,
|
68
|
+
hash_including("query" => query)
|
69
|
+
)
|
70
|
+
|
71
|
+
@api.fql_query(query)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should be able to access public information via FQL" do
|
75
|
+
@result = @api.fql_query("select first_name from user where uid = 216743")
|
76
|
+
@result.size.should == 1
|
77
|
+
@result.first["first_name"].should == "Chris"
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should not be able to access protected information via FQL" do
|
81
|
+
lambda { @api.fql_query("select read_stream from permissions where uid = 216743") }.should raise_error(Koala::Facebook::APIError)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class FacebookRestAPINoAccessTokenTest < Test::Unit::TestCase
|
87
|
+
before :each do
|
88
|
+
@api = Koala::Facebook::RestAPI.new
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "Koala RestAPI without an access token" do
|
92
|
+
it_should_behave_like "Koala RestAPI without an access token"
|
93
|
+
end
|
94
|
+
end
|