koala 0.10.0 → 1.0.0.beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. data/CHANGELOG +33 -7
  2. data/Manifest +8 -1
  3. data/Rakefile +4 -4
  4. data/koala.gemspec +10 -8
  5. data/lib/koala/graph_api.rb +188 -123
  6. data/lib/koala/http_services.rb +93 -17
  7. data/lib/koala/rest_api.rb +73 -6
  8. data/lib/koala/test_users.rb +18 -5
  9. data/lib/koala/uploadable_io.rb +115 -0
  10. data/lib/koala.rb +95 -72
  11. data/readme.md +18 -12
  12. data/spec/facebook_data.yml +9 -3
  13. data/spec/koala/assets/beach.jpg +0 -0
  14. data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +5 -1
  15. data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +8 -3
  16. data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +12 -61
  17. data/spec/koala/graph_api/graph_api_tests.rb +85 -0
  18. data/spec/koala/graph_api/graph_api_with_access_token_tests.rb +167 -123
  19. data/spec/koala/http_services/http_service_tests.rb +51 -0
  20. data/spec/koala/http_services/net_http_service_tests.rb +339 -0
  21. data/spec/koala/http_services/typhoeus_service_tests.rb +162 -0
  22. data/spec/koala/live_testing_data_helper.rb +1 -1
  23. data/spec/koala/oauth/oauth_tests.rb +35 -64
  24. data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +5 -74
  25. data/spec/koala/rest_api/rest_api_tests.rb +118 -0
  26. data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +5 -3
  27. data/spec/koala/test_users/test_users_tests.rb +49 -48
  28. data/spec/koala/uploadable_io/uploadable_io_tests.rb +246 -0
  29. data/spec/koala_spec_helper.rb +30 -4
  30. data/spec/koala_spec_without_mocks.rb +3 -3
  31. data/spec/mock_facebook_responses.yml +41 -18
  32. data/spec/mock_http_service.rb +16 -3
  33. metadata +38 -8
  34. data/spec/koala/net_http_service_tests.rb +0 -186
@@ -1,94 +1,25 @@
1
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
2
  # FQL_QUERY
53
3
  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
4
  it "should be able to access public information via FQL" do
75
5
  @result = @api.fql_query("select first_name from user where uid = 216743")
76
6
  @result.size.should == 1
77
7
  @result.first["first_name"].should == "Chris"
78
8
  end
79
-
9
+
80
10
  it "should not be able to access protected information via FQL" do
81
11
  lambda { @api.fql_query("select read_stream from permissions where uid = 216743") }.should raise_error(Koala::Facebook::APIError)
82
12
  end
83
13
  end
84
14
  end
85
15
 
86
- class FacebookRestAPINoAccessTokenTest < Test::Unit::TestCase
16
+ class FacebookRestAPINoAccessTokenTest < Test::Unit::TestCase
87
17
  describe "Koala RestAPI without an access token" do
88
18
  before :each do
89
19
  @api = Koala::Facebook::RestAPI.new
90
20
  end
91
-
92
- it_should_behave_like "Koala RestAPI without an access token"
21
+
22
+ it_should_behave_like "Koala RestAPI"
23
+ it_should_behave_like "Koala RestAPI without an access token"
93
24
  end
94
25
  end
@@ -0,0 +1,118 @@
1
+ shared_examples_for "Koala RestAPI" do
2
+ # REST_CALL
3
+ describe "when making a rest request" do
4
+ it "should use the proper path" do
5
+ method = stub('methodName')
6
+ @api.should_receive(:api).with(
7
+ "method/#{method}",
8
+ anything,
9
+ anything,
10
+ anything
11
+ )
12
+
13
+ @api.rest_call(method)
14
+ end
15
+
16
+ it "should always use the rest api" do
17
+ @api.should_receive(:api).with(
18
+ anything,
19
+ anything,
20
+ anything,
21
+ hash_including(:rest_api => true)
22
+ )
23
+
24
+ @api.rest_call('anything')
25
+ end
26
+
27
+ it "should set the read_only option to true if the method is listed in the read-only list" do
28
+ method = Koala::Facebook::RestAPI::READ_ONLY_METHODS.first
29
+
30
+ @api.should_receive(:api).with(
31
+ anything,
32
+ anything,
33
+ anything,
34
+ hash_including(:read_only => true)
35
+ )
36
+
37
+ @api.rest_call(method)
38
+ end
39
+
40
+ it "should set the read_only option to false if the method is not inthe read-only list" do
41
+ method = "I'm not a read-only method"
42
+
43
+ @api.should_receive(:api).with(
44
+ anything,
45
+ anything,
46
+ anything,
47
+ hash_including(:read_only => false)
48
+ )
49
+
50
+ @api.rest_call(method)
51
+ end
52
+
53
+
54
+ it "should take an optional hash of arguments" do
55
+ args = {:arg1 => 'arg1'}
56
+
57
+ @api.should_receive(:api).with(
58
+ anything,
59
+ hash_including(args),
60
+ anything,
61
+ anything
62
+ )
63
+
64
+ @api.rest_call('anything', args)
65
+ end
66
+
67
+ it "should always ask for JSON" do
68
+ @api.should_receive(:api).with(
69
+ anything,
70
+ hash_including('format' => 'json'),
71
+ anything,
72
+ anything
73
+ )
74
+
75
+ @api.rest_call('anything')
76
+ end
77
+
78
+ it "should pass any options provided to the API" do
79
+ options = {:a => 2}
80
+
81
+ @api.should_receive(:api).with(
82
+ anything,
83
+ hash_including('format' => 'json'),
84
+ anything,
85
+ hash_including(options)
86
+ )
87
+
88
+ @api.rest_call('anything', {}, options)
89
+ end
90
+
91
+ it "should throw an APIError if the result hash has an error key" do
92
+ Koala.stub(:make_request).and_return(Koala::Response.new(500, {"error_code" => "An error occurred!"}, {}))
93
+ lambda { @api.rest_call("koppel", {}) }.should raise_exception(Koala::Facebook::APIError)
94
+ end
95
+
96
+ describe "when making a FQL request" do
97
+ it "should call fql.query method" do
98
+ @api.should_receive(:rest_call).with(
99
+ "fql.query",
100
+ anything
101
+ ).and_return(Koala::Response.new(200, "2", {}))
102
+
103
+ @api.fql_query stub('query string')
104
+ end
105
+
106
+ it "should pass a query argument" do
107
+ query = stub('query string')
108
+
109
+ @api.should_receive(:rest_call).with(
110
+ anything,
111
+ hash_including("query" => query)
112
+ )
113
+
114
+ @api.fql_query(query)
115
+ end
116
+ end
117
+ end
118
+ end
@@ -8,7 +8,7 @@ shared_examples_for "Koala RestAPI with an access token" do
8
8
 
9
9
  it "should be able to access protected information via FQL" do
10
10
  # Tests agains the permissions fql table
11
-
11
+
12
12
  # get the current user's ID
13
13
  # we're sneakily using the Graph API, which should be okay since it has its own tests
14
14
  g = Koala::Facebook::GraphAPI.new(@token)
@@ -27,10 +27,12 @@ end
27
27
  class FacebookRestAPIWithAccessTokenTests < Test::Unit::TestCase
28
28
  describe "Koala RestAPI with an access token" do
29
29
  include LiveTestingDataHelper
30
- it_should_behave_like "Koala RestAPI with an access token"
31
-
30
+
32
31
  before :each do
33
32
  @api = Koala::Facebook::RestAPI.new(@token)
34
33
  end
34
+
35
+ it_should_behave_like "Koala RestAPI"
36
+ it_should_behave_like "Koala RestAPI with an access token"
35
37
  end
36
38
  end
@@ -10,37 +10,37 @@ class TestUsersTests < Test::Unit::TestCase
10
10
  @app_id = @oauth_data["app_id"]
11
11
  @secret = @oauth_data["secret"]
12
12
  @app_access_token = @oauth_data["app_access_token"]
13
-
13
+
14
14
  # check OAuth data
15
15
  unless @app_id && @secret && @app_access_token
16
- raise Exception, "Must supply OAuth app id, secret, app_access_token, and callback to run live subscription tests!"
16
+ raise Exception, "Must supply OAuth app id, secret, app_access_token, and callback to run live subscription tests!"
17
17
  end
18
-
18
+
19
19
  @is_mock = defined?(Koala::IS_MOCK) && Koala::IS_MOCK
20
20
  end
21
-
21
+
22
22
  describe "when initializing" do
23
23
  # basic initialization
24
24
  it "should initialize properly with an app_id and an app_access_token" do
25
25
  test_users = Facebook::TestUsers.new(:app_id => @app_id, :app_access_token => @app_access_token)
26
26
  test_users.should be_a(Facebook::TestUsers)
27
27
  end
28
-
28
+
29
29
  # init with secret / fetching the token
30
- it "should initialize properly with an app_id and a secret" do
30
+ it "should initialize properly with an app_id and a secret" do
31
31
  test_users = Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
32
- test_users.should be_a(Facebook::TestUsers)
32
+ test_users.should be_a(Facebook::TestUsers)
33
33
  end
34
-
34
+
35
35
  it "should use the OAuth class to fetch a token when provided an app_id and a secret" do
36
36
  oauth = Facebook::OAuth.new(@app_id, @secret)
37
37
  token = oauth.get_app_access_token
38
38
  oauth.should_receive(:get_app_access_token).and_return(token)
39
- Facebook::OAuth.should_receive(:new).with(@app_id, @secret).and_return(oauth)
39
+ Facebook::OAuth.should_receive(:new).with(@app_id, @secret).and_return(oauth)
40
40
  test_users = Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
41
41
  end
42
42
  end
43
-
43
+
44
44
  describe "when used without network" do
45
45
  before :each do
46
46
  @test_users = Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
@@ -53,31 +53,31 @@ class TestUsersTests < Test::Unit::TestCase
53
53
  result.should be_a(Hash)
54
54
  (result["id"] && result["access_token"] && result["login_url"]).should
55
55
  end
56
-
56
+
57
57
  it "should create a test user when not given installed, ignoring permissions" do
58
58
  result = @test_users.create(false, "read_stream")
59
59
  @temporary_object_id = result["id"]
60
60
  result.should be_a(Hash)
61
61
  (result["id"] && result["access_token"] && result["login_url"]).should
62
62
  end
63
-
63
+
64
64
  it "should accept permissions as a string" do
65
65
  @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including("permissions" => "read_stream,publish_stream"), anything)
66
66
  result = @test_users.create(true, "read_stream,publish_stream")
67
67
  end
68
-
68
+
69
69
  it "should accept permissions as an array" do
70
70
  @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including("permissions" => "read_stream,publish_stream"), anything)
71
71
  result = @test_users.create(true, ["read_stream", "publish_stream"])
72
72
  end
73
-
73
+
74
74
  it "should create a test user when given installed and a permission" do
75
75
  result = @test_users.create(true, "read_stream")
76
76
  @temporary_object_id = result["id"]
77
77
  result.should be_a(Hash)
78
78
  (result["id"] && result["access_token"] && result["login_url"]).should
79
79
  end
80
-
80
+
81
81
  describe "with a user to delete" do
82
82
  before :each do
83
83
  @user1 = @test_users.create(true, "read_stream")
@@ -87,25 +87,25 @@ class TestUsersTests < Test::Unit::TestCase
87
87
  after :each do
88
88
  print "\nCleaning up test users..."
89
89
  @test_users.delete(@user1) if @user1
90
- @test_users.delete(@user2) if @user2
90
+ @test_users.delete(@user2) if @user2
91
91
  puts "done."
92
92
  end
93
-
93
+
94
94
  it "should delete a user by id" do
95
95
  @test_users.delete(@user1['id']).should be_true
96
96
  @user1 = nil
97
97
  end
98
-
98
+
99
99
  it "should delete a user by hash" do
100
100
  @test_users.delete(@user2).should be_true
101
101
  @user2 = nil
102
102
  end
103
-
103
+
104
104
  it "should not delete users when provided a false ID" do
105
105
  lambda { @test_users.delete("#{@user1['id']}1") }.should raise_exception(Koala::Facebook::APIError)
106
106
  end
107
107
  end
108
-
108
+
109
109
  describe "with delete_all" do
110
110
  it "should delete all users found by the list commnand" do
111
111
  array = [1, 2, 3]
@@ -114,18 +114,18 @@ class TestUsersTests < Test::Unit::TestCase
114
114
  @test_users.delete_all
115
115
  end
116
116
  end
117
-
117
+
118
118
  describe "with existing users" do
119
119
  before :each do
120
120
  @user1 = @test_users.create(true, "read_stream")
121
121
  @user2 = @test_users.create(true, "read_stream,user_interests")
122
122
  end
123
-
123
+
124
124
  after :each do
125
125
  @test_users.delete(@user1)
126
126
  @test_users.delete(@user2)
127
127
  end
128
-
128
+
129
129
  it "should list test users" do
130
130
  result = @test_users.list
131
131
  result.should be_an(Array)
@@ -134,36 +134,44 @@ class TestUsersTests < Test::Unit::TestCase
134
134
  (second_user["id"] && second_user["access_token"] && second_user["login_url"]).should
135
135
  end
136
136
 
137
- it "should make two users into friends by id" do
138
- result = @test_users.befriend(@user1['id'], @user2['id'])
137
+ it "should make two users into friends with string hashes" do
138
+ result = @test_users.befriend(@user1, @user2)
139
139
  result.should be_true
140
140
  end
141
141
 
142
- it "should make two users into friends by hash" do
143
- result = @test_users.befriend(@user1, @user2)
142
+ it "should make two users into friends with symbol hashes" do
143
+ new_user_1 = {}
144
+ @user1.each_pair {|k, v| new_user_1[k.to_sym] = v}
145
+ new_user_2 = {}
146
+ @user2.each_pair {|k, v| new_user_2[k.to_sym] = v}
147
+
148
+ result = @test_users.befriend(new_user_1, new_user_2)
144
149
  result.should be_true
145
- end
150
+ end
146
151
 
152
+ it "should not accept user IDs anymore" do
153
+ lambda { @test_users.befriend(@user1["id"], @user2["id"]) }.should raise_exception
154
+ end
147
155
  end # with existing users
148
-
156
+
149
157
  end # when used without network
150
-
151
- describe "when creating a network of friends" do
158
+
159
+ describe "when creating a network of friends" do
152
160
  before :each do
153
161
  @test_users = Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
154
162
  @network = []
155
-
163
+
156
164
  if @is_mock
157
165
  id_counter = 999999900
158
166
  @test_users.stub!(:create).and_return do
159
167
  id_counter += 1
160
- {"id" => id_counter, "access_token" => "119908831367602|o3wswWQ88LYjEC9-ukR_gjRIOMw.", "login_url" => "https://www.facebook.com/platform/test_account.."}
168
+ {"id" => id_counter, "access_token" => @token, "login_url" => "https://www.facebook.com/platform/test_account.."}
161
169
  end
162
170
  @test_users.stub!(:befriend).and_return(true)
163
171
  @test_users.stub!(:delete).and_return(true)
164
172
  end
165
173
  end
166
-
174
+
167
175
  describe "tests that create users" do
168
176
  before :each do
169
177
  print "\nCleaning up test user network..."
@@ -178,22 +186,15 @@ class TestUsersTests < Test::Unit::TestCase
178
186
  test_users.delete_all
179
187
  puts "done!"
180
188
  end
181
-
182
- it "should create a 2 person network" do
183
- @network = @test_users.create_network(2)
184
- @network.should be_a(Array)
185
- @network.size.should == 2
186
- end
187
-
188
- it "should create a 50 person network" do
189
- puts "\nStarting 50-person network test (this may take several minutes)..."
190
- @network = @test_users.create_network(50)
189
+
190
+ it "should create a 5 person network" do
191
+ size = 5
192
+ @network = @test_users.create_network(size)
191
193
  @network.should be_a(Array)
192
- @network.size.should == 50
193
- puts "done!"
194
+ @network.size.should == size
194
195
  end
195
196
  end
196
-
197
+
197
198
  it "should limit to a 50 person network" do
198
199
  @test_users.should_receive(:create).exactly(50).times
199
200
  @test_users.stub!(:befriend)
@@ -208,7 +209,7 @@ class TestUsersTests < Test::Unit::TestCase
208
209
  @test_users.stub!(:befriend)
209
210
  @network = @test_users.create_network(count, installed, perms)
210
211
  end
211
-
212
+
212
213
  end # when creating network
213
214
 
214
215
  end # describe Koala TestUsers