koala 0.9.0 → 1.1.0

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 (82) hide show
  1. data/.autotest +12 -0
  2. data/.gitignore +5 -0
  3. data/.travis.yml +8 -0
  4. data/CHANGELOG +71 -7
  5. data/Gemfile +7 -0
  6. data/LICENSE +1 -1
  7. data/Manifest +10 -15
  8. data/Rakefile +13 -13
  9. data/autotest/discover.rb +1 -0
  10. data/koala.gemspec +36 -16
  11. data/lib/koala/batch_operation.rb +74 -0
  12. data/lib/koala/graph_api.rb +200 -134
  13. data/lib/koala/graph_batch_api.rb +87 -0
  14. data/lib/koala/graph_collection.rb +54 -0
  15. data/lib/koala/http_services/net_http_service.rb +92 -0
  16. data/lib/koala/http_services/typhoeus_service.rb +37 -0
  17. data/lib/koala/http_services.rb +22 -47
  18. data/lib/koala/oauth.rb +181 -0
  19. data/lib/koala/realtime_updates.rb +5 -14
  20. data/lib/koala/rest_api.rb +82 -10
  21. data/lib/koala/test_users.rb +85 -0
  22. data/lib/koala/uploadable_io.rb +175 -0
  23. data/lib/koala.rb +67 -229
  24. data/readme.md +71 -38
  25. data/spec/cases/api_base_spec.rb +101 -0
  26. data/spec/cases/graph_and_rest_api_spec.rb +31 -0
  27. data/spec/cases/graph_api_batch_spec.rb +609 -0
  28. data/spec/cases/graph_api_spec.rb +25 -0
  29. data/spec/cases/http_services/http_service_spec.rb +129 -0
  30. data/spec/cases/http_services/net_http_service_spec.rb +532 -0
  31. data/spec/cases/http_services/typhoeus_service_spec.rb +152 -0
  32. data/spec/cases/koala_spec.rb +55 -0
  33. data/spec/cases/oauth_spec.rb +409 -0
  34. data/spec/cases/realtime_updates_spec.rb +184 -0
  35. data/spec/cases/rest_api_spec.rb +25 -0
  36. data/spec/cases/test_users_spec.rb +221 -0
  37. data/spec/cases/uploadable_io_spec.rb +193 -0
  38. data/spec/fixtures/beach.jpg +0 -0
  39. data/spec/fixtures/cat.m4v +0 -0
  40. data/spec/{facebook_data.yml → fixtures/facebook_data.yml} +18 -14
  41. data/spec/fixtures/mock_facebook_responses.yml +409 -0
  42. data/spec/spec_helper.rb +19 -0
  43. data/spec/support/graph_api_shared_examples.rb +495 -0
  44. data/spec/support/json_testing_fix.rb +18 -0
  45. data/spec/support/live_testing_data_helper.rb +40 -0
  46. data/spec/support/mock_http_service.rb +96 -0
  47. data/spec/support/rest_api_shared_examples.rb +285 -0
  48. data/spec/support/setup_mocks_or_live.rb +51 -0
  49. data/spec/support/uploadable_io_shared_examples.rb +76 -0
  50. metadata +131 -59
  51. data/examples/oauth_playground/Capfile +0 -2
  52. data/examples/oauth_playground/LICENSE +0 -22
  53. data/examples/oauth_playground/Rakefile +0 -4
  54. data/examples/oauth_playground/config/deploy.rb +0 -39
  55. data/examples/oauth_playground/config/facebook.yml +0 -13
  56. data/examples/oauth_playground/config.ru +0 -27
  57. data/examples/oauth_playground/lib/load_facebook.rb +0 -3
  58. data/examples/oauth_playground/lib/oauth_playground.rb +0 -187
  59. data/examples/oauth_playground/readme.md +0 -8
  60. data/examples/oauth_playground/spec/oauth_playground_spec.rb +0 -35
  61. data/examples/oauth_playground/spec/spec_helper.rb +0 -36
  62. data/examples/oauth_playground/tmp/restart.txt +0 -0
  63. data/examples/oauth_playground/views/index.erb +0 -206
  64. data/examples/oauth_playground/views/layout.erb +0 -39
  65. data/init.rb +0 -2
  66. data/spec/koala/api_base_tests.rb +0 -95
  67. data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +0 -10
  68. data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +0 -11
  69. data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +0 -114
  70. data/spec/koala/graph_api/graph_api_with_access_token_tests.rb +0 -150
  71. data/spec/koala/graph_api/graph_collection_tests.rb +0 -104
  72. data/spec/koala/live_testing_data_helper.rb +0 -15
  73. data/spec/koala/net_http_service_tests.rb +0 -181
  74. data/spec/koala/oauth/oauth_tests.rb +0 -440
  75. data/spec/koala/realtime_updates/realtime_updates_tests.rb +0 -187
  76. data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +0 -94
  77. data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +0 -36
  78. data/spec/koala_spec.rb +0 -18
  79. data/spec/koala_spec_helper.rb +0 -31
  80. data/spec/koala_spec_without_mocks.rb +0 -19
  81. data/spec/mock_facebook_responses.yml +0 -241
  82. data/spec/mock_http_service.rb +0 -81
@@ -0,0 +1,184 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Koala::Facebook::RealtimeUpdates" do
4
+ before :all do
5
+ # get oauth data
6
+ @oauth_data = $testing_data["oauth_test_data"]
7
+ @app_id = @oauth_data["app_id"]
8
+ @secret = @oauth_data["secret"]
9
+ @callback_url = @oauth_data["callback_url"]
10
+ @app_access_token = @oauth_data["app_access_token"]
11
+
12
+ # check OAuth data
13
+ unless @app_id && @secret && @callback_url && @app_access_token
14
+ raise Exception, "Must supply OAuth app id, secret, app_access_token, and callback to run live subscription tests!"
15
+ end
16
+
17
+ # get subscription data
18
+ @subscription_data = $testing_data["subscription_test_data"]
19
+ @verify_token = @subscription_data["verify_token"]
20
+ @challenge_data = @subscription_data["challenge_data"]
21
+ @subscription_path = @subscription_data["subscription_path"]
22
+
23
+ # check subscription data
24
+ unless @verify_token && @challenge_data && @subscription_path
25
+ raise Exception, "Must supply verify_token and equivalent challenge_data to run live subscription tests!"
26
+ end
27
+ end
28
+
29
+ describe "when initializing" do
30
+ # basic initialization
31
+ it "should initialize properly with an app_id and an app_access_token" do
32
+ updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
33
+ updates.should be_a(Koala::Facebook::RealtimeUpdates)
34
+ end
35
+
36
+ # attributes
37
+ it "should allow read access to app_id, app_access_token, and secret" do
38
+ updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
39
+ # this should not throw errors
40
+ updates.app_id && updates.app_access_token && updates.secret
41
+ end
42
+
43
+ it "should not allow write access to app_id" do
44
+ updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
45
+ # this should not throw errors
46
+ updates.methods.map(&:to_sym).should_not include(:app_id=)
47
+ end
48
+
49
+ it "should not allow write access to app_access_token" do
50
+ updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
51
+ # this should not throw errors
52
+ updates.methods.map(&:to_sym).should_not include(:app_access_token=)
53
+ end
54
+
55
+ it "should not allow write access to secret" do
56
+ updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
57
+ # this should not throw errors
58
+ updates.methods.map(&:to_sym).should_not include(:secret=)
59
+ end
60
+
61
+ # init with secret / fetching the token
62
+ it "should initialize properly with an app_id and a secret" do
63
+ updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
64
+ updates.should be_a(Koala::Facebook::RealtimeUpdates)
65
+ end
66
+
67
+ it "should fetch an app_token from Facebook when provided an app_id and a secret" do
68
+ updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
69
+ updates.app_access_token.should_not be_nil
70
+ end
71
+
72
+ it "should use the OAuth class to fetch a token when provided an app_id and a secret" do
73
+ oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
74
+ token = oauth.get_app_access_token
75
+ oauth.should_receive(:get_app_access_token).and_return(token)
76
+ Koala::Facebook::OAuth.should_receive(:new).with(@app_id, @secret).and_return(oauth)
77
+ updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
78
+ end
79
+ end
80
+
81
+ describe "when used" do
82
+ before :each do
83
+ @updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
84
+ end
85
+
86
+ it "should send a subscription request to a valid server" do
87
+ result = @updates.subscribe("user", "name", @subscription_path, @verify_token)
88
+ result.should be_true
89
+ end
90
+
91
+ it "should send a subscription request to a valid server" do
92
+ result = @updates.subscribe("user", "name", @subscription_path, @verify_token)
93
+ result.should be_true
94
+ end
95
+
96
+ it "should send a subscription request to an invalid path on a valid server" do
97
+ lambda { result = @updates.subscribe("user", "name", @subscription_path + "foo", @verify_token) }.should raise_exception(Koala::Facebook::APIError)
98
+ end
99
+
100
+ it "should fail to send a subscription request to an invalid server" do
101
+ lambda { @updates.subscribe("user", "name", "foo", @verify_token) }.should raise_exception(Koala::Facebook::APIError)
102
+ end
103
+
104
+ it "should unsubscribe a valid individual object successfully" do
105
+ @updates.unsubscribe("user").should be_true
106
+ end
107
+
108
+ it "should unsubscribe all subscriptions successfully" do
109
+ @updates.unsubscribe.should be_true
110
+ end
111
+
112
+ it "should fail when an invalid object is provided to unsubscribe" do
113
+ lambda { @updates.unsubscribe("kittens") }.should raise_error(Koala::Facebook::APIError)
114
+ end
115
+
116
+ it "should is subscriptions properly" do
117
+ @updates.list_subscriptions.should be_a(Array)
118
+ end
119
+ end # describe "when used"
120
+
121
+ describe "when meeting challenge" do
122
+ it "should return false if hub.mode isn't subscribe" do
123
+ params = {'hub.mode' => 'not subscribe'}
124
+ Koala::Facebook::RealtimeUpdates.meet_challenge(params).should be_false
125
+ end
126
+
127
+ it "should return false if not given a verify_token or block" do
128
+ params = {'hub.mode' => 'subscribe'}
129
+ Koala::Facebook::RealtimeUpdates.meet_challenge(params).should be_false
130
+ end
131
+
132
+ describe "and mode is 'subscribe'" do
133
+ before(:each) do
134
+ @params = {'hub.mode' => 'subscribe'}
135
+ end
136
+
137
+ describe "and a token is given" do
138
+ before(:each) do
139
+ @token = 'token'
140
+ @params['hub.verify_token'] = @token
141
+ end
142
+
143
+ it "should return false if the given verify token doesn't match" do
144
+ Koala::Facebook::RealtimeUpdates.meet_challenge(@params, @token + '1').should be_false
145
+ end
146
+
147
+ it "should return the challenge if the given verify token matches" do
148
+ @params['hub.challenge'] = 'challenge val'
149
+ Koala::Facebook::RealtimeUpdates.meet_challenge(@params, @token).should == @params['hub.challenge']
150
+ end
151
+ end
152
+
153
+ describe "and a block is given" do
154
+ it "should give the block the token as a parameter" do
155
+ Koala::Facebook::RealtimeUpdates.meet_challenge(@params)do |token|
156
+ token.should == @token
157
+ end
158
+ end
159
+
160
+ it "should return false if the given block return false" do
161
+ Koala::Facebook::RealtimeUpdates.meet_challenge(@params)do |token|
162
+ false
163
+ end.should be_false
164
+ end
165
+
166
+ it "should return false if the given block returns nil" do
167
+ Koala::Facebook::RealtimeUpdates.meet_challenge(@params)do |token|
168
+ nil
169
+ end.should be_false
170
+ end
171
+
172
+ it "should return the challenge if the given block returns true" do
173
+ @params['hub.challenge'] = 'challenge val'
174
+ Koala::Facebook::RealtimeUpdates.meet_challenge(@params) do |token|
175
+ true
176
+ end.should be_true
177
+ end
178
+ end
179
+
180
+ end # describe "and mode is subscribe"
181
+
182
+ end # describe "when meeting challenge"
183
+
184
+ end # describe
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Koala::Facebook::RestAPI" do
4
+
5
+ context "without an access token" do
6
+ before :each do
7
+ @api = Koala::Facebook::RestAPI.new
8
+ end
9
+
10
+ it_should_behave_like "Koala RestAPI"
11
+ it_should_behave_like "Koala RestAPI without an access token"
12
+ end
13
+
14
+ context "with an access token" do
15
+ include LiveTestingDataHelper
16
+
17
+ before :each do
18
+ @api = Koala::Facebook::RestAPI.new(@token)
19
+ end
20
+
21
+ it_should_behave_like "Koala RestAPI"
22
+ it_should_behave_like "Koala RestAPI with an access token"
23
+ end
24
+
25
+ end
@@ -0,0 +1,221 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Koala::Facebook::TestUsers" do
4
+ context "with access token" do
5
+ include LiveTestingDataHelper
6
+
7
+ before :all do
8
+ # get oauth data
9
+ @oauth_data = $testing_data["oauth_test_data"]
10
+ @app_id = @oauth_data["app_id"]
11
+ @secret = @oauth_data["secret"]
12
+ @app_access_token = @oauth_data["app_access_token"]
13
+
14
+ # check OAuth data
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!"
17
+ end
18
+
19
+ @is_mock = Koala.http_service.respond_to?(:mock?) && Koala.http_service.mock?
20
+ end
21
+
22
+ describe "when initializing" do
23
+ # basic initialization
24
+ it "should initialize properly with an app_id and an app_access_token" do
25
+ test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :app_access_token => @app_access_token)
26
+ test_users.should be_a(Koala::Facebook::TestUsers)
27
+ end
28
+
29
+ # init with secret / fetching the token
30
+ it "should initialize properly with an app_id and a secret" do
31
+ test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
32
+ test_users.should be_a(Koala::Facebook::TestUsers)
33
+ end
34
+
35
+ it "should use the OAuth class to fetch a token when provided an app_id and a secret" do
36
+ oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
37
+ token = oauth.get_app_access_token
38
+ oauth.should_receive(:get_app_access_token).and_return(token)
39
+ Koala::Facebook::OAuth.should_receive(:new).with(@app_id, @secret).and_return(oauth)
40
+ test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
41
+ end
42
+ end
43
+
44
+ describe "when used without network" do
45
+ before :each do
46
+ @test_users = Koala::Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
47
+ end
48
+
49
+ # TEST USER MANAGEMENT
50
+ it "should create a test user when not given installed" do
51
+ result = @test_users.create(false)
52
+ @temporary_object_id = result["id"]
53
+ result.should be_a(Hash)
54
+ (result["id"] && result["access_token"] && result["login_url"]).should
55
+ end
56
+
57
+ it "should create a test user when not given installed, ignoring permissions" do
58
+ result = @test_users.create(false, "read_stream")
59
+ @temporary_object_id = result["id"]
60
+ result.should be_a(Hash)
61
+ (result["id"] && result["access_token"] && result["login_url"]).should
62
+ end
63
+
64
+ it "should accept permissions as a string" do
65
+ @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including("permissions" => "read_stream,publish_stream"), anything, anything)
66
+ result = @test_users.create(true, "read_stream,publish_stream")
67
+ end
68
+
69
+ it "should accept permissions as an array" do
70
+ @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including("permissions" => "read_stream,publish_stream"), anything, anything)
71
+ result = @test_users.create(true, ["read_stream", "publish_stream"])
72
+ end
73
+
74
+ it "should create a test user when given installed and a permission" do
75
+ result = @test_users.create(true, "read_stream")
76
+ @temporary_object_id = result["id"]
77
+ result.should be_a(Hash)
78
+ (result["id"] && result["access_token"] && result["login_url"]).should
79
+ end
80
+
81
+ it "lets you specify other graph arguments, like uid and access token" do
82
+ args = {:uid => "some test user ID", :owner_access_token => "some owner access token"}
83
+ @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including(args), anything, anything)
84
+ @test_users.create(true, nil, args)
85
+ end
86
+
87
+ it "lets you specify http options that get passed through to the graph call" do
88
+ options = {:some_http_option => true}
89
+ @test_users.graph_api.should_receive(:graph_call).with(anything, anything, anything, options)
90
+ @test_users.create(true, nil, {}, options)
91
+ end
92
+
93
+ describe "with a user to delete" do
94
+ before :each do
95
+ @user1 = @test_users.create(true, "read_stream")
96
+ @user2 = @test_users.create(true, "read_stream,user_interests")
97
+ end
98
+
99
+ after :each do
100
+ @test_users.delete(@user1) if @user1
101
+ @test_users.delete(@user2) if @user2
102
+ end
103
+
104
+ it "should delete a user by id" do
105
+ @test_users.delete(@user1['id']).should be_true
106
+ @user1 = nil
107
+ end
108
+
109
+ it "should delete a user by hash" do
110
+ @test_users.delete(@user2).should be_true
111
+ @user2 = nil
112
+ end
113
+
114
+ it "should not delete users when provided a false ID" do
115
+ lambda { @test_users.delete("#{@user1['id']}1") }.should raise_exception(Koala::Facebook::APIError)
116
+ end
117
+ end
118
+
119
+ describe "with delete_all" do
120
+ it "should delete all users found by the list commnand" do
121
+ array = [1, 2, 3]
122
+ @test_users.should_receive(:list).and_return(array)
123
+ array.each {|i| @test_users.should_receive(:delete).with(i) }
124
+ @test_users.delete_all
125
+ end
126
+ end
127
+
128
+ describe "with existing users" do
129
+ before :each do
130
+ @user1 = @test_users.create(true, "read_stream")
131
+ @user2 = @test_users.create(true, "read_stream,user_interests")
132
+ end
133
+
134
+ after :each do
135
+ @test_users.delete(@user1)
136
+ @test_users.delete(@user2)
137
+ end
138
+
139
+ it "should list test users" do
140
+ result = @test_users.list
141
+ result.should be_an(Array)
142
+ first_user, second_user = result[0], result[1]
143
+ (first_user["id"] && first_user["access_token"] && first_user["login_url"]).should
144
+ (second_user["id"] && second_user["access_token"] && second_user["login_url"]).should
145
+ end
146
+
147
+ it "should make two users into friends with string hashes" do
148
+ result = @test_users.befriend(@user1, @user2)
149
+ result.should be_true
150
+ end
151
+
152
+ it "should make two users into friends with symbol hashes" do
153
+ new_user_1 = {}
154
+ @user1.each_pair {|k, v| new_user_1[k.to_sym] = v}
155
+ new_user_2 = {}
156
+ @user2.each_pair {|k, v| new_user_2[k.to_sym] = v}
157
+
158
+ result = @test_users.befriend(new_user_1, new_user_2)
159
+ result.should be_true
160
+ end
161
+
162
+ it "should not accept user IDs anymore" do
163
+ lambda { @test_users.befriend(@user1["id"], @user2["id"]) }.should raise_exception
164
+ end
165
+ end # with existing users
166
+
167
+ end # when used without network
168
+
169
+ describe "when creating a network of friends" do
170
+ before :each do
171
+ @test_users = Koala::Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
172
+ @network = []
173
+
174
+ if @is_mock
175
+ id_counter = 999999900
176
+ @test_users.stub!(:create).and_return do
177
+ id_counter += 1
178
+ {"id" => id_counter, "access_token" => @token, "login_url" => "https://www.facebook.com/platform/test_account.."}
179
+ end
180
+ @test_users.stub!(:befriend).and_return(true)
181
+ @test_users.stub!(:delete).and_return(true)
182
+ end
183
+ end
184
+
185
+ describe "tests that create users" do
186
+ before :each do
187
+ test_users = Koala::Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
188
+ test_users.delete_all
189
+ end
190
+
191
+ after :each do
192
+ test_users = Koala::Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
193
+ test_users.delete_all
194
+ end
195
+
196
+ it "should create a 5 person network" do
197
+ size = 5
198
+ @network = @test_users.create_network(size)
199
+ @network.should be_a(Array)
200
+ @network.size.should == size
201
+ end
202
+ end
203
+
204
+ it "should limit to a 50 person network" do
205
+ @test_users.should_receive(:create).exactly(50).times
206
+ @test_users.stub!(:befriend)
207
+ @network = @test_users.create_network(51)
208
+ end
209
+
210
+ it "should pass on the installed and permissions parameters to create" do
211
+ perms = ["read_stream", "offline_access"]
212
+ installed = false
213
+ count = 25
214
+ @test_users.should_receive(:create).exactly(count).times.with(installed, perms)
215
+ @test_users.stub!(:befriend)
216
+ @network = @test_users.create_network(count, installed, perms)
217
+ end
218
+
219
+ end # when creating network
220
+ end
221
+ end # describe Koala TestUsers
@@ -0,0 +1,193 @@
1
+ # fake MIME::Types
2
+ module Koala::MIME
3
+ module Types
4
+ def self.type_for(type)
5
+ # this should be faked out in tests
6
+ nil
7
+ end
8
+ end
9
+ end
10
+
11
+ describe "Koala::UploadableIO" do
12
+ def rails_3_mocks
13
+ tempfile = stub('Tempfile', :path => "foo")
14
+ uploaded_file = stub('ActionDispatch::Http::UploadedFile',
15
+ :content_type => true,
16
+ :tempfile => tempfile
17
+ )
18
+ tempfile.stub!(:respond_to?).with(:path).and_return(true)
19
+
20
+ [tempfile, uploaded_file]
21
+ end
22
+
23
+ def sinatra_mocks
24
+ {:type => "type", :tempfile => "Tempfile"}
25
+ end
26
+
27
+ describe "the constructor" do
28
+ describe "when given a file path" do
29
+ before(:each) do
30
+ @koala_io_params = [File.open(BEACH_BALL_PATH)]
31
+ end
32
+
33
+ describe "and a content type" do
34
+ before :each do
35
+ @koala_io_params.concat([stub("image/jpg")])
36
+ end
37
+
38
+ it "should return an UploadIO with the same file path" do
39
+ stub_path = @koala_io_params[0] = "/stub/path/to/file"
40
+ Koala::UploadableIO.new(*@koala_io_params).io_or_path.should == stub_path
41
+ end
42
+
43
+ it "should return an UploadIO with the same content type" do
44
+ stub_type = @koala_io_params[1] = stub('Content Type')
45
+ Koala::UploadableIO.new(*@koala_io_params).content_type.should == stub_type
46
+ end
47
+
48
+ it "should detect that NetHTTPService must be used" do
49
+ @koala_io_params[0] = mock
50
+ @koala_io_params[0].stub!(:read)
51
+ Koala::UploadableIO.new(*@koala_io_params).requires_base_http_service.should be_true
52
+ end
53
+ end
54
+
55
+ describe "and no content type" do
56
+ it_should_behave_like "determining a mime type"
57
+ end
58
+ end
59
+
60
+ describe "when given a File object" do
61
+ before(:each) do
62
+ @koala_io_params = [File.open(BEACH_BALL_PATH)]
63
+ end
64
+
65
+ describe "and a content type" do
66
+ before :each do
67
+ @koala_io_params.concat(["image/jpg"])
68
+ end
69
+
70
+ it "should return an UploadIO with the same io" do
71
+ Koala::UploadableIO.new(*@koala_io_params).io_or_path.should == @koala_io_params[0]
72
+ end
73
+
74
+ it "should return an UplaodIO with the same content_type" do
75
+ content_stub = @koala_io_params[1] = stub('Content Type')
76
+ Koala::UploadableIO.new(*@koala_io_params).content_type.should == content_stub
77
+ end
78
+ end
79
+
80
+ describe "and no content type" do
81
+ it_should_behave_like "determining a mime type"
82
+ end
83
+ end
84
+
85
+ describe "when given a Rails 3 ActionDispatch::Http::UploadedFile" do
86
+ before(:each) do
87
+ @tempfile, @uploaded_file = rails_3_mocks
88
+ end
89
+
90
+ it "should get the content type via the content_type method" do
91
+ expected_content_type = stub('Content Type')
92
+ @uploaded_file.should_receive(:content_type).and_return(expected_content_type)
93
+ Koala::UploadableIO.new(@uploaded_file).content_type.should == expected_content_type
94
+ end
95
+
96
+ it "should get the path from the tempfile associated with the UploadedFile" do
97
+ expected_path = stub('Tempfile')
98
+ @tempfile.should_receive(:path).and_return(expected_path)
99
+ Koala::UploadableIO.new(@uploaded_file).io_or_path.should == expected_path
100
+ end
101
+ end
102
+
103
+ describe "when given a Sinatra file parameter hash" do
104
+ before(:each) do
105
+ @file_hash = sinatra_mocks
106
+ end
107
+
108
+ it "should get the content type from the :type key" do
109
+ expected_content_type = stub('Content Type')
110
+ @file_hash[:type] = expected_content_type
111
+
112
+ uploadable = Koala::UploadableIO.new(@file_hash)
113
+ uploadable.content_type.should == expected_content_type
114
+ end
115
+
116
+ it "should get the io_or_path from the :tempfile key" do
117
+ expected_file = stub('File')
118
+ @file_hash[:tempfile] = expected_file
119
+
120
+ uploadable = Koala::UploadableIO.new(@file_hash)
121
+ uploadable.io_or_path.should == expected_file
122
+ end
123
+ end
124
+
125
+ describe "for files with with recognizable MIME types" do
126
+ # what that means is tested below
127
+ it "should accept a file object alone" do
128
+ params = [BEACH_BALL_PATH]
129
+ lambda { Koala::UploadableIO.new(*params) }.should_not raise_exception(Koala::KoalaError)
130
+ end
131
+
132
+ it "should accept a file path alone" do
133
+ params = [BEACH_BALL_PATH]
134
+ lambda { Koala::UploadableIO.new(*params) }.should_not raise_exception(Koala::KoalaError)
135
+ end
136
+ end
137
+ end
138
+
139
+ describe "getting an UploadableIO" do
140
+ before(:each) do
141
+ @upload_io = stub("UploadIO")
142
+ UploadIO.stub!(:new).with(anything, anything, anything).and_return(@upload_io)
143
+ end
144
+
145
+ context "if no filename was provided" do
146
+ it "should call the constructor with the content type, file name, and a dummy file name" do
147
+ UploadIO.should_receive(:new).with(BEACH_BALL_PATH, "content/type", anything).and_return(@upload_io)
148
+ Koala::UploadableIO.new(BEACH_BALL_PATH, "content/type").to_upload_io.should == @upload_io
149
+ end
150
+ end
151
+
152
+ context "if a filename was provided" do
153
+ it "should call the constructor with the content type, file name, and the filename" do
154
+ filename = "file"
155
+ UploadIO.should_receive(:new).with(BEACH_BALL_PATH, "content/type", filename).and_return(@upload_io)
156
+ Koala::UploadableIO.new(BEACH_BALL_PATH, "content/type", filename).to_upload_io
157
+ end
158
+ end
159
+ end
160
+
161
+ describe "getting a file" do
162
+ it "should return the File if initialized with a file" do
163
+ f = File.new(BEACH_BALL_PATH)
164
+ Koala::UploadableIO.new(f).to_file.should == f
165
+ end
166
+
167
+ it "should open up and return a file corresponding to the path if io_or_path is a path" do
168
+ result = stub("File")
169
+ File.should_receive(:open).with(BEACH_BALL_PATH).and_return(result)
170
+ Koala::UploadableIO.new(BEACH_BALL_PATH).to_file.should == result
171
+ end
172
+ end
173
+
174
+ describe "#binary_content?" do
175
+ it "returns true for Rails 3 file uploads" do
176
+ Koala::UploadableIO.binary_content?(rails_3_mocks.last).should be_true
177
+ end
178
+
179
+ it "returns true for Sinatra file uploads" do
180
+ Koala::UploadableIO.binary_content?(rails_3_mocks.last).should be_true
181
+ end
182
+
183
+ it "returns true for File objects" do
184
+ Koala::UploadableIO.binary_content?(File.open(BEACH_BALL_PATH)).should be_true
185
+ end
186
+
187
+ it "returns false for everything else" do
188
+ Koala::UploadableIO.binary_content?(StringIO.new).should be_false
189
+ Koala::UploadableIO.binary_content?(BEACH_BALL_PATH).should be_false
190
+ Koala::UploadableIO.binary_content?(nil).should be_false
191
+ end
192
+ end
193
+ end # describe UploadableIO
Binary file
Binary file
@@ -4,19 +4,19 @@
4
4
  # Just remember to update all fields!
5
5
 
6
6
  # You must supply this value yourself to test the GraphAPI class.
7
- # Your OAuth token should have publish_stream and read_stream permissions.
8
- oauth_token: 119908831367602|2.K0IVdhrRngS7VQM4Z_s6_g__.3600.1285844400-2905623|Q3VNUb9haS3s29X4SYPk6VL1f9A
7
+ # Your OAuth token should have publish_stream, read_stream, and user_photos permissions.
8
+ oauth_token:
9
9
 
10
10
  # for testing the OAuth class
11
11
  # baseline app
12
12
  oauth_test_data:
13
13
  # You must supply this value yourself, since they will expire.
14
- code: 2.K0IVdhrRngS7VQM4Z_s6_g__.3600.1285844400-2905623|b5PCXOhEMe2FJXPHAg_mY3Psl6M
14
+ code:
15
15
  # easiest way to get session keys: use multiple test accounts with the Javascript login at http://oauth.twoalex.com
16
- session_key: 2.K0IVdhrRngS7VQM4Z_s6_g__.3600.1285844400-2905623
16
+ session_key:
17
17
  multiple_session_keys:
18
- - 2.K0IVdhrRngS7VQM4Z_s6_g__.3600.1285844400-2905623
19
- - 2.K0IVdhrRngS7VQM4Z_s6_g__.3600.1285844400-2905623
18
+ -
19
+ -
20
20
 
21
21
  # These values will work out of the box
22
22
  app_id: 119908831367602
@@ -35,17 +35,21 @@ oauth_test_data:
35
35
  # note: I've revoked the offline access for security reasons, so you can't make calls against this :)
36
36
  fbs_119908831367602: '"access_token=119908831367602|08170230801eb225068e7a70-2905623|Q3LDCYYF8CX9cstxnZLsxiR0nwg.&expires=0&secret=78abaee300b392e275072a9f2727d436&session_key=08170230801eb225068e7a70-2905623&sig=423b8aa4b6fa1f9a571955f8e929d567&uid=2905623"'
37
37
 
38
- # These values will work out of the box
39
- # They're from Facebook's example at http://developers.facebook.com/docs/authentication/canvas
38
+ # These values from the OAuth Playground (see above) will work out of the box
40
39
  # You can update this to live data if desired
41
40
  # request_secret is optional and will fall back to the secret above if absent
42
- request_secret: "secret"
43
- signed_request: "vlXgu64BQGFSQrY0ZcJBZASMvYvTHu9GQ0YM9rjPSso.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsIjAiOiJwYXlsb2FkIn0"
44
- signed_request_result:
45
- "0": payload
41
+ signed_params: "zWRm0gd5oHW_jzXP_WA9CirO7c5CLHotn-SKRqH2NmU.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEzMDE5MjIwMDAsImlzc3VlZF9hdCI6MTMwMTkxNzI5OSwib2F1dGhfdG9rZW4iOiIxMTk5MDg4MzEzNjc2MDJ8Mi56VkZfNk5yTUVMSHVKYTRnSVU5dEt3X18uMzYwMC4xMzAxOTIyMDAwLTI5MDU2MjN8emdxUHNyMkJHOUxvT0s5a2VrR2dSVVJaeDBrIiwidXNlciI6eyJjb3VudHJ5IjoiZGUiLCJsb2NhbGUiOiJkZV9ERSIsImFnZSI6eyJtaW4iOjIxfX0sInVzZXJfaWQiOiIyOTA1NjIzIn0"
42
+ signed_params_result:
43
+ expires: 1301922000
46
44
  algorithm: HMAC-SHA256
47
-
48
-
45
+ user_id: "2905623"
46
+ oauth_token: 119908831367602|2.zVF_6NrMELHuJa4gIU9tKw__.3600.1301922000-2905623|zgqPsr2BG9LoOK9kekGgRURZx0k
47
+ user:
48
+ country: de
49
+ locale: de_DE
50
+ age:
51
+ min: 21
52
+ issued_at: 1301917299
49
53
 
50
54
  subscription_test_data:
51
55
  subscription_path: http://oauth.twoalex.com/subscriptions