cotweet_koala 0.8.0.1

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.
@@ -0,0 +1,80 @@
1
+ class ApiBaseTests < Test::Unit::TestCase
2
+ describe "Koala API base class" do
3
+ before(:each) do
4
+ @service = Koala::Facebook::API.new
5
+ end
6
+
7
+ it "should not include an access token if none was given" do
8
+ Koala.should_receive(:make_request).with(
9
+ anything,
10
+ hash_not_including('access_token' => 1),
11
+ anything,
12
+ anything
13
+ ).and_return(Koala::Response.new(200, "", ""))
14
+
15
+ @service.api('anything')
16
+ end
17
+
18
+ it "should include an access token if given" do
19
+ token = 'adfadf'
20
+ service = Koala::Facebook::API.new token
21
+
22
+ Koala.should_receive(:make_request).with(
23
+ anything,
24
+ hash_including('access_token' => token),
25
+ anything,
26
+ anything
27
+ ).and_return(Koala::Response.new(200, "", ""))
28
+
29
+ service.api('anything')
30
+ end
31
+
32
+ it "should get the attribute of a Koala::Response given by the http_component parameter" do
33
+ http_component = :method_name
34
+
35
+ response = mock('Mock KoalaResponse', :body => '', :status => 200)
36
+ response.should_receive(http_component).and_return('')
37
+
38
+ Koala.stub(:make_request).and_return(response)
39
+
40
+ @service.api('anything', 'get', {}, :http_component => http_component)
41
+ end
42
+
43
+ it "should return the body of the request as JSON if no http_component is given" do
44
+ response = stub('response', :body => 'body', :status => 200)
45
+ Koala.stub(:make_request).and_return(response)
46
+
47
+ json_body = mock('JSON body')
48
+ JSON.stub(:parse).and_return([json_body])
49
+
50
+ @service.api('anything').should == json_body
51
+ end
52
+
53
+ it "should execute a block with the response body if passed one" do
54
+ body = '{}'
55
+ Koala.stub(:make_request).and_return(Koala::Response.new(200, body, {}))
56
+
57
+ yield_test = mock('Yield Tester')
58
+ yield_test.should_receive(:pass)
59
+
60
+ @service.api('anything') do |arg|
61
+ yield_test.pass
62
+ arg.should == JSON.parse(body)
63
+ end
64
+ end
65
+
66
+ it "should raise an API error if the HTTP response code is greater than or equal to 500" do
67
+ Koala.stub(:make_request).and_return(Koala::Response.new(500, 'response body', {}))
68
+
69
+ lambda { @service.api('anything') }.should raise_exception(Koala::Facebook::APIError)
70
+ end
71
+
72
+ it "should handle rogue true/false as responses" do
73
+ Koala.should_receive(:make_request).and_return(Koala::Response.new(200, 'true', {}))
74
+ @service.api('anything').should be_true
75
+
76
+ Koala.should_receive(:make_request).and_return(Koala::Response.new(200, 'false', {}))
77
+ @service.api('anything').should be_false
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,10 @@
1
+ class GraphAndRestAPINoTokenTests < Test::Unit::TestCase
2
+ describe "Koala GraphAndRestAPI without an access token" do
3
+ before(:each) do
4
+ @api = Koala::Facebook::GraphAndRestAPI.new
5
+ end
6
+
7
+ it_should_behave_like "Koala RestAPI without an access token"
8
+ it_should_behave_like "Koala GraphAPI without an access token"
9
+ end
10
+ end
@@ -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,105 @@
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 a user's picture, given a picture type" do
38
+ @api.get_picture("chris.baclig", {:type => 'large'}).should =~ /^http\:\/\//
39
+ end
40
+
41
+ it "should be able to access connections from public Pages" do
42
+ result = @api.get_connections("contextoptional", "likes")
43
+ result.should be_a(Array)
44
+ end
45
+
46
+ it "should not be able to put an object" do
47
+ lambda { @result = @api.put_object("lukeshepard", "feed", :message => "Hello, world") }.should raise_error(Koala::Facebook::APIError)
48
+ puts "Error! Object #{@result.inspect} somehow put onto Luke Shepard's wall!" if @result
49
+ end
50
+
51
+ # these are not strictly necessary as the other put methods resolve to put_object, but are here for completeness
52
+ it "should not be able to post to a feed" do
53
+ (lambda do
54
+ attachment = {:name => "Context Optional", :link => "http://www.contextoptional.com/"}
55
+ @result = @api.put_wall_post("Hello, world", attachment, "contextoptional")
56
+ end).should raise_error(Koala::Facebook::APIError)
57
+ puts "Error! Object #{@result.inspect} somehow put onto Context Optional's wall!" if @result
58
+ end
59
+
60
+ it "should not be able to comment on an object" do
61
+ # random public post on the ContextOptional wall
62
+ lambda { @result = @api.put_comment("7204941866_119776748033392", "The hackathon was great!") }.should raise_error(Koala::Facebook::APIError)
63
+ puts "Error! Object #{@result.inspect} somehow commented on post 7204941866_119776748033392!" if @result
64
+ end
65
+
66
+ it "should not be able to like an object" do
67
+ lambda { @api.put_like("7204941866_119776748033392") }.should raise_error(Koala::Facebook::APIError)
68
+ end
69
+
70
+
71
+ # DELETE
72
+ it "should not be able to delete posts" do
73
+ # test post on the Ruby SDK Test application
74
+ lambda { @result = @api.delete_object("115349521819193_113815981982767") }.should raise_error(Koala::Facebook::APIError)
75
+ end
76
+
77
+ # SEARCH
78
+ it "should be able to search" do
79
+ result = @api.search("facebook")
80
+ result["data"].should be_an(Array)
81
+ end
82
+
83
+ # API
84
+ it "should never use the rest api server" do
85
+ Koala.should_receive(:make_request).with(
86
+ anything,
87
+ anything,
88
+ anything,
89
+ hash_not_including(:rest_api => true)
90
+ ).and_return(Koala::Response.new(200, "", {}))
91
+
92
+ @api.api("anything")
93
+ end
94
+ end
95
+
96
+ class FacebookNoAccessTokenTests < Test::Unit::TestCase
97
+ describe "Koala GraphAPI without an access token" do
98
+ before :each do
99
+ @api = Koala::Facebook::GraphAPI.new
100
+ end
101
+
102
+ it_should_behave_like "Koala GraphAPI without an access token"
103
+ end
104
+ end
105
+
@@ -0,0 +1,139 @@
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")
4
+ # the results should have an ID and a name, among other things
5
+ (result["id"] && result["name"]).should_not be_nil
6
+ end
7
+
8
+ it "should 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_not 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 get data about 'me'" do
21
+ result = @api.get_object("me")
22
+ result["updated_time"].should
23
+ end
24
+
25
+ it "should be able to get multiple objects" do
26
+ result = @api.get_objects(["contextoptional", "naitik"])
27
+ result.length.should == 2
28
+ end
29
+
30
+ it "should be able to access a user's picture" do
31
+ @api.get_picture("chris.baclig").should =~ /http\:\/\//
32
+ end
33
+
34
+ it "should be able to access a user's picture, given a picture type" do
35
+ @api.get_picture("chris.baclig", {:type => 'large'}).should =~ /^http\:\/\//
36
+ end
37
+
38
+ it "should be able to access connections from users" do
39
+ result = @api.get_connections("lukeshepard", "likes")
40
+ result.length.should > 0
41
+ end
42
+
43
+ it "should be able to access connections from public Pages" do
44
+ result = @api.get_connections("contextoptional", "likes")
45
+ result.should be_a(Array)
46
+ end
47
+
48
+ # PUT
49
+ it "should be able to write an object to the graph" do
50
+ result = @api.put_wall_post("Hello, world, from the test suite!")
51
+ @temporary_object_id = result["id"]
52
+ @temporary_object_id.should_not be_nil
53
+ end
54
+
55
+ # DELETE
56
+ it "should be able to delete posts" do
57
+ result = @api.put_wall_post("Hello, world, from the test suite delete method!")
58
+ object_id_to_delete = result["id"]
59
+ delete_result = @api.delete_object(object_id_to_delete)
60
+ delete_result.should == true
61
+ end
62
+
63
+ # additional put tests
64
+ it "should be able to verify messages posted to a wall" do
65
+ message = "the cats are asleep"
66
+ put_result = @api.put_wall_post(message)
67
+ @temporary_object_id = put_result["id"]
68
+ get_result = @api.get_object(@temporary_object_id)
69
+
70
+ # make sure the message we sent is the message that got posted
71
+ get_result["message"].should == message
72
+ end
73
+
74
+ it "should be able to post a message with an attachment to a feed" do
75
+ result = @api.put_wall_post("Hello, world, from the test suite again!", {:name => "Context Optional", :link => "http://www.contextoptional.com/"})
76
+ @temporary_object_id = result["id"]
77
+ @temporary_object_id.should_not be_nil
78
+ end
79
+
80
+ it "should be able to verify a message with an attachment posted to a feed" do
81
+ attachment = {"name" => "Context Optional", "link" => "http://www.contextoptional.com/"}
82
+ result = @api.put_wall_post("Hello, world, from the test suite again!", attachment)
83
+ @temporary_object_id = result["id"]
84
+ get_result = @api.get_object(@temporary_object_id)
85
+
86
+ # make sure the result we fetch includes all the parameters we sent
87
+ it_matches = attachment.inject(true) {|valid, param| valid && (get_result[param[0]] == attachment[param[0]])}
88
+ it_matches.should == true
89
+ end
90
+
91
+ it "should be able to comment on an object" do
92
+ result = @api.put_wall_post("Hello, world, from the test suite, testing comments!")
93
+ @temporary_object_id = result["id"]
94
+
95
+ # this will be deleted when the post gets deleted
96
+ comment_result = @api.put_comment(@temporary_object_id, "it's my comment!")
97
+ comment_result.should_not be_nil
98
+ end
99
+
100
+ it "should be able to verify a comment posted about an object" do
101
+ message_text = "Hello, world, from the test suite, testing comments!"
102
+ result = @api.put_wall_post(message_text)
103
+ @temporary_object_id = result["id"]
104
+
105
+ # this will be deleted when the post gets deleted
106
+ comment_text = "it's my comment!"
107
+ comment_result = @api.put_comment(@temporary_object_id, comment_text)
108
+ get_result = @api.get_object(comment_result["id"])
109
+
110
+ # make sure the text of the comment matches what we sent
111
+ get_result["message"].should == comment_text
112
+ end
113
+
114
+ it "should be able to like an object" do
115
+ result = @api.put_wall_post("Hello, world, from the test suite, testing comments!")
116
+ @temporary_object_id = result["id"]
117
+ like_result = @api.put_like(@temporary_object_id)
118
+ end
119
+
120
+ # SEARCH
121
+ it "should be able to search" do
122
+ result = @api.search("facebook")
123
+ result["data"].should be_an(Array)
124
+ end
125
+
126
+ # API
127
+ # the above tests test this already, but we should consider additional api tests
128
+ end
129
+
130
+ class FacebookWithAccessTokenTests < Test::Unit::TestCase
131
+ describe "Koala GraphAPI with an access token" do
132
+ it_should_behave_like "live testing examples"
133
+ it_should_behave_like "Koala GraphAPI with an access token"
134
+
135
+ before :each do
136
+ @api = Koala::Facebook::GraphAPI.new(@token)
137
+ end
138
+ end
139
+ 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,181 @@
1
+ require 'koala/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
+ describe "when making a request" do
13
+ before(:each) do
14
+ # Setup stubs for make_request to execute without exceptions
15
+ @mock_http_response = stub('Net::HTTPResponse', :code => 1)
16
+ @mock_body = stub('Net::HTTPResponse body')
17
+ @http_request_result = [@mock_http_response, @mock_body]
18
+
19
+ @http_yield_mock = mock('Net::HTTP start yielded object')
20
+
21
+ @http_yield_mock.stub(:post).and_return(@http_request_result)
22
+ @http_yield_mock.stub(:get).and_return(@http_request_result)
23
+
24
+ @http_mock = stub('Net::HTTP object', 'use_ssl=' => true, 'verify_mode=' => true)
25
+ @http_mock.stub(:start).and_yield(@http_yield_mock)
26
+
27
+ Net::HTTP.stub(:new).and_return(@http_mock)
28
+ end
29
+
30
+ it "should use POST if verb is not GET" do
31
+ @http_yield_mock.should_receive(:post).and_return(@mock_http_response)
32
+ @http_mock.should_receive(:start).and_yield(@http_yield_mock)
33
+
34
+ Bear.make_request('anything', {}, 'anything')
35
+ end
36
+
37
+ it "should use port 443" do
38
+ Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
39
+
40
+ Bear.make_request('anything', {}, 'anything')
41
+ end
42
+
43
+ it "should use SSL" do
44
+ @http_mock.should_receive('use_ssl=').with(true)
45
+
46
+ Bear.make_request('anything', {}, 'anything')
47
+ end
48
+
49
+ it "should use the graph server by default" do
50
+ Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything).and_return(@http_mock)
51
+ Bear.make_request('anything', {}, 'anything')
52
+ end
53
+
54
+ it "should use the REST server if the :rest_api option is true" do
55
+ Net::HTTP.should_receive(:new).with(Koala::Facebook::REST_SERVER, anything).and_return(@http_mock)
56
+ Bear.make_request('anything', {}, 'anything', :rest_api => true)
57
+ end
58
+
59
+ it "should turn off vertificate validaiton warnings" do
60
+ @http_mock.should_receive('verify_mode=').with(OpenSSL::SSL::VERIFY_NONE)
61
+
62
+ Bear.make_request('anything', {}, 'anything')
63
+ end
64
+
65
+ it "should start an HTTP connection" do
66
+ @http_mock.should_receive(:start).and_yield(@http_yield_mock)
67
+ Bear.make_request('anything', {}, 'anything')
68
+ end
69
+
70
+ describe "via POST" do
71
+ it "should use Net::HTTP to make a POST request" do
72
+ @http_yield_mock.should_receive(:post).and_return(@http_request_result)
73
+
74
+ Bear.make_request('anything', {}, 'post')
75
+ end
76
+
77
+ it "should go to the specified path" do
78
+ path = mock('Path')
79
+ @http_yield_mock.should_receive(:post).with(path, anything).and_return(@http_request_result)
80
+
81
+ Bear.make_request(path, {}, 'post')
82
+ end
83
+
84
+ it "should use encoded parameters" do
85
+ args = {}
86
+ params = mock('Encoded parameters')
87
+ Bear.should_receive(:encode_params).with(args).and_return(params)
88
+
89
+ @http_yield_mock.should_receive(:post).with(anything, params).and_return(@http_request_result)
90
+
91
+ Bear.make_request('anything', args, 'post')
92
+ end
93
+ end
94
+
95
+ describe "via GET" do
96
+ it "should use Net::HTTP to make a GET request" do
97
+ @http_yield_mock.should_receive(:get).and_return(@http_request_result)
98
+
99
+ Bear.make_request('anything', {}, 'get')
100
+ end
101
+
102
+ it "should use the correct path, including arguments" do
103
+ path = mock('Path')
104
+ params = mock('Encoded parameters')
105
+ args = {}
106
+
107
+ Bear.should_receive(:encode_params).with(args).and_return(params)
108
+ @http_yield_mock.should_receive(:get).with("#{path}?#{params}").and_return(@http_request_result)
109
+
110
+ Bear.make_request(path, args, 'get')
111
+ end
112
+ end
113
+
114
+ describe "the returned value" do
115
+ before(:each) do
116
+ @response = Bear.make_request('anything', {}, 'anything')
117
+ end
118
+
119
+ it "should return a Koala::Response object" do
120
+ @response.class.should == Koala::Response
121
+ end
122
+
123
+ it "should return a Koala::Response with the right status" do
124
+ @response.status.should == @mock_http_response.code
125
+ end
126
+
127
+ it "should reutrn a Koala::Response with the right body" do
128
+ @response.body.should == @mock_body
129
+ end
130
+
131
+ it "should return a Koala::Response with the Net::HTTPResponse object as headers" do
132
+ @response.headers.should == @mock_http_response
133
+ end
134
+ end # describe return value
135
+ end # describe when making a request
136
+
137
+ describe "when encoding parameters" do
138
+ it "should return an empty string if param_hash evaluates to false" do
139
+ Bear.encode_params(nil).should == ''
140
+ end
141
+
142
+ it "should convert values to JSON if the value is not a String" do
143
+ val = 'json_value'
144
+ not_a_string = 'not_a_string'
145
+ not_a_string.stub(:class).and_return('NotAString')
146
+ not_a_string.should_receive(:to_json).and_return(val)
147
+
148
+ string = "hi"
149
+
150
+ args = {
151
+ not_a_string => not_a_string,
152
+ string => string
153
+ }
154
+
155
+ result = Bear.encode_params(args)
156
+ result.split('&').find do |key_and_val|
157
+ key_and_val.match("#{not_a_string}=#{val}")
158
+ end.should be_true
159
+ end
160
+
161
+ it "should escape all values" do
162
+ args = Hash[*(1..4).map {|i| [i.to_s, "Value #{i}($"]}.flatten]
163
+
164
+ result = Bear.encode_params(args)
165
+ result.split('&').each do |key_val|
166
+ key, val = key_val.split('=')
167
+ val.should == CGI.escape(args[key])
168
+ end
169
+ end
170
+
171
+ it "should convert all keys to Strings" do
172
+ args = Hash[*(1..4).map {|i| [i, "val#{i}"]}.flatten]
173
+
174
+ result = Bear.encode_params(args)
175
+ result.split('&').each do |key_val|
176
+ key, val = key_val.split('=')
177
+ key.should == args.find{|key_val_arr| key_val_arr.last == val}.first.to_s
178
+ end
179
+ end
180
+ end
181
+ end