koala 1.0.0 → 1.2.0beta1

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 (52) hide show
  1. data/.autotest +12 -0
  2. data/.gitignore +3 -1
  3. data/.travis.yml +9 -0
  4. data/CHANGELOG +52 -2
  5. data/Gemfile +8 -0
  6. data/Rakefile +0 -1
  7. data/autotest/discover.rb +1 -0
  8. data/koala.gemspec +14 -14
  9. data/lib/koala/batch_operation.rb +74 -0
  10. data/lib/koala/graph_api.rb +157 -133
  11. data/lib/koala/graph_batch_api.rb +87 -0
  12. data/lib/koala/graph_collection.rb +54 -0
  13. data/lib/koala/http_service.rb +177 -0
  14. data/lib/koala/oauth.rb +181 -0
  15. data/lib/koala/realtime_updates.rb +23 -29
  16. data/lib/koala/rest_api.rb +13 -8
  17. data/lib/koala/test_users.rb +33 -16
  18. data/lib/koala/uploadable_io.rb +152 -87
  19. data/lib/koala/utils.rb +11 -0
  20. data/lib/koala.rb +54 -217
  21. data/readme.md +71 -52
  22. data/spec/cases/api_base_spec.rb +6 -6
  23. data/spec/cases/error_spec.rb +32 -0
  24. data/spec/cases/graph_and_rest_api_spec.rb +20 -3
  25. data/spec/cases/graph_api_batch_spec.rb +600 -0
  26. data/spec/cases/graph_api_spec.rb +21 -4
  27. data/spec/cases/http_service_spec.rb +446 -0
  28. data/spec/cases/koala_spec.rb +50 -0
  29. data/spec/cases/oauth_spec.rb +220 -201
  30. data/spec/cases/realtime_updates_spec.rb +45 -31
  31. data/spec/cases/rest_api_spec.rb +23 -7
  32. data/spec/cases/test_users_spec.rb +112 -52
  33. data/spec/cases/uploadable_io_spec.rb +92 -37
  34. data/spec/cases/utils_spec.rb +10 -0
  35. data/spec/fixtures/cat.m4v +0 -0
  36. data/spec/fixtures/facebook_data.yml +23 -22
  37. data/spec/fixtures/mock_facebook_responses.yml +201 -76
  38. data/spec/spec_helper.rb +30 -5
  39. data/spec/support/graph_api_shared_examples.rb +134 -56
  40. data/spec/support/json_testing_fix.rb +42 -0
  41. data/spec/support/koala_test.rb +163 -0
  42. data/spec/support/mock_http_service.rb +60 -57
  43. data/spec/support/ordered_hash.rb +205 -0
  44. data/spec/support/rest_api_shared_examples.rb +139 -15
  45. data/spec/support/uploadable_io_shared_examples.rb +2 -8
  46. metadata +98 -112
  47. data/lib/koala/http_services.rb +0 -146
  48. data/spec/cases/http_services/http_service_spec.rb +0 -54
  49. data/spec/cases/http_services/net_http_service_spec.rb +0 -350
  50. data/spec/cases/http_services/typhoeus_service_spec.rb +0 -144
  51. data/spec/support/live_testing_data_helper.rb +0 -40
  52. data/spec/support/setup_mocks_or_live.rb +0 -52
@@ -3,11 +3,10 @@ require 'spec_helper'
3
3
  describe "Koala::Facebook::RealtimeUpdates" do
4
4
  before :all do
5
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"]
6
+ @app_id = KoalaTest.app_id
7
+ @secret = KoalaTest.secret
8
+ @callback_url = KoalaTest.oauth_test_data["callback_url"]
9
+ @app_access_token = KoalaTest.app_access_token
11
10
 
12
11
  # check OAuth data
13
12
  unless @app_id && @secret && @callback_url && @app_access_token
@@ -15,14 +14,13 @@ describe "Koala::Facebook::RealtimeUpdates" do
15
14
  end
16
15
 
17
16
  # 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"]
17
+ @verify_token = KoalaTest.subscription_test_data["verify_token"]
18
+ @challenge_data = KoalaTest.subscription_test_data["challenge_data"]
19
+ @subscription_path = KoalaTest.subscription_test_data["subscription_path"]
22
20
 
23
21
  # check subscription data
24
22
  unless @verify_token && @challenge_data && @subscription_path
25
- raise Exception, "Must supply verify_token and equivalent challenge_data to run live subscription tests!"
23
+ raise Exception, "Must supply verify_token and equivalent challenge_data to run subscription tests!"
26
24
  end
27
25
  end
28
26
 
@@ -34,28 +32,40 @@ describe "Koala::Facebook::RealtimeUpdates" do
34
32
  end
35
33
 
36
34
  # 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
35
+ it "should allow read access to app_id" do
36
+ # in Ruby 1.9, .method returns symbols
37
+ Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should include(:app_id)
38
+ Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should_not include(:app_id=)
41
39
  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
- lambda { updates.app_id = 2 }.should raise_error(NoMethodError)
40
+
41
+ it "should allow read access to app_access_token" do
42
+ # in Ruby 1.9, .method returns symbols
43
+ Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should include(:app_access_token)
44
+ Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should_not include(:app_access_token=)
47
45
  end
48
46
 
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
- lambda { updates.app_access_token = 2 }.should raise_error(NoMethodError)
47
+ it "should allow read access to secret" do
48
+ # in Ruby 1.9, .method returns symbols
49
+ Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should include(:secret)
50
+ Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should_not include(:secret=)
53
51
  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
- lambda { updates.secret = 2 }.should raise_error(NoMethodError)
52
+
53
+ it "should allow read access to api" do
54
+ # in Ruby 1.9, .method returns symbols
55
+ Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should include(:api)
56
+ Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should_not include(:api=)
57
+ end
58
+
59
+ # old graph_api accessor
60
+ it "returns the api object when graph_api is called" do
61
+ updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
62
+ updates.graph_api.should == updates.api
63
+ end
64
+
65
+ it "fire a deprecation warning when graph_api is called" do
66
+ updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
67
+ Koala::Utils.should_receive(:deprecate)
68
+ updates.graph_api
59
69
  end
60
70
 
61
71
  # init with secret / fetching the token
@@ -151,20 +161,24 @@ describe "Koala::Facebook::RealtimeUpdates" do
151
161
  end
152
162
 
153
163
  describe "and a block is given" do
164
+ before :each do
165
+ @params['hub.verify_token'] = @token
166
+ end
167
+
154
168
  it "should give the block the token as a parameter" do
155
- Koala::Facebook::RealtimeUpdates.meet_challenge(@params)do |token|
169
+ Koala::Facebook::RealtimeUpdates.meet_challenge(@params) do |token|
156
170
  token.should == @token
157
171
  end
158
172
  end
159
173
 
160
174
  it "should return false if the given block return false" do
161
- Koala::Facebook::RealtimeUpdates.meet_challenge(@params)do |token|
175
+ Koala::Facebook::RealtimeUpdates.meet_challenge(@params) do |token|
162
176
  false
163
177
  end.should be_false
164
178
  end
165
179
 
166
180
  it "should return false if the given block returns nil" do
167
- Koala::Facebook::RealtimeUpdates.meet_challenge(@params)do |token|
181
+ Koala::Facebook::RealtimeUpdates.meet_challenge(@params) do |token|
168
182
  nil
169
183
  end.should be_false
170
184
  end
@@ -1,25 +1,41 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "Koala::Facebook::RestAPI" do
4
-
4
+ describe "class consolidation" do
5
+ before :each do
6
+ Koala::Utils.stub(:deprecate) # avoid actual messages to stderr
7
+ end
8
+
9
+ it "still allows you to instantiate a GraphAndRestAPI object" do
10
+ api = Koala::Facebook::RestAPI.new("token").should be_a(Koala::Facebook::RestAPI)
11
+ end
12
+
13
+ it "ultimately creates an API object" do
14
+ api = Koala::Facebook::RestAPI.new("token").should be_a(Koala::Facebook::API)
15
+ end
16
+
17
+ it "fires a depreciation warning" do
18
+ Koala::Utils.should_receive(:deprecate)
19
+ api = Koala::Facebook::RestAPI.new("token")
20
+ end
21
+ end
22
+
5
23
  context "without an access token" do
6
24
  before :each do
7
- @api = Koala::Facebook::RestAPI.new
25
+ @api = Koala::Facebook::API.new
8
26
  end
9
27
 
10
28
  it_should_behave_like "Koala RestAPI"
11
29
  it_should_behave_like "Koala RestAPI without an access token"
12
30
  end
13
-
14
- context "with an access token" do
15
- include LiveTestingDataHelper
16
31
 
32
+ context "with an access token" do
17
33
  before :each do
18
- @api = Koala::Facebook::RestAPI.new(@token)
34
+ @api = Koala::Facebook::API.new(@token)
19
35
  end
20
36
 
21
37
  it_should_behave_like "Koala RestAPI"
22
38
  it_should_behave_like "Koala RestAPI with an access token"
23
39
  end
24
-
40
+
25
41
  end
@@ -2,21 +2,16 @@ require 'spec_helper'
2
2
 
3
3
  describe "Koala::Facebook::TestUsers" do
4
4
  context "with access token" do
5
- include LiveTestingDataHelper
6
-
7
5
  before :all do
8
6
  # 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"]
7
+ @app_id = KoalaTest.app_id
8
+ @secret = KoalaTest.secret
9
+ @app_access_token = KoalaTest.app_access_token
13
10
 
14
11
  # check OAuth data
15
12
  unless @app_id && @secret && @app_access_token
16
13
  raise Exception, "Must supply OAuth app id, secret, app_access_token, and callback to run live subscription tests!"
17
14
  end
18
-
19
- @is_mock = defined?(Koala::IS_MOCK) && Koala::IS_MOCK
20
15
  end
21
16
 
22
17
  describe "when initializing" do
@@ -39,58 +34,98 @@ describe "Koala::Facebook::TestUsers" do
39
34
  Koala::Facebook::OAuth.should_receive(:new).with(@app_id, @secret).and_return(oauth)
40
35
  test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
41
36
  end
42
- end
43
37
 
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})
38
+ # attributes
39
+ it "should allow read access to app_id, app_access_token, and secret" do
40
+ # in Ruby 1.9, .method returns symbols
41
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should include(:app_id)
42
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should_not include(:app_id=)
47
43
  end
48
44
 
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
45
+ it "should allow read access to app_access_token" do
46
+ # in Ruby 1.9, .method returns symbols
47
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should include(:app_access_token)
48
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should_not include(:app_access_token=)
55
49
  end
56
50
 
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
51
+ it "should allow read access to secret" do
52
+ # in Ruby 1.9, .method returns symbols
53
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should include(:secret)
54
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should_not include(:secret=)
62
55
  end
63
56
 
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")
57
+ it "should allow read access to api" do
58
+ # in Ruby 1.9, .method returns symbols
59
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should include(:api)
60
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should_not include(:api=)
67
61
  end
68
62
 
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"])
63
+ # old graph_api accessor
64
+ it "returns the api object when graph_api is called" do
65
+ test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
66
+ test_users.graph_api.should == test_users.api
72
67
  end
73
68
 
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
69
+ it "fire a deprecation warning when graph_api is called" do
70
+ test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
71
+ Koala::Utils.should_receive(:deprecate)
72
+ test_users.graph_api
79
73
  end
74
+ end
80
75
 
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)
76
+ describe "when used without network" do
77
+ before :each do
78
+ @test_users = Koala::Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
85
79
  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)
80
+
81
+ # TEST USER MANAGEMENT
82
+
83
+ describe ".create" do
84
+ it "should create a test user when not given installed" do
85
+ result = @test_users.create(false)
86
+ @temporary_object_id = result["id"]
87
+ result.should be_a(Hash)
88
+ (result["id"] && result["access_token"] && result["login_url"]).should
89
+ end
90
+
91
+ it "should create a test user when not given installed, ignoring permissions" do
92
+ result = @test_users.create(false, "read_stream")
93
+ @temporary_object_id = result["id"]
94
+ result.should be_a(Hash)
95
+ (result["id"] && result["access_token"] && result["login_url"]).should
96
+ end
97
+
98
+ it "should accept permissions as a string" do
99
+ @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including("permissions" => "read_stream,publish_stream"), anything, anything)
100
+ result = @test_users.create(true, "read_stream,publish_stream")
101
+ end
102
+
103
+ it "should accept permissions as an array" do
104
+ @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including("permissions" => "read_stream,publish_stream"), anything, anything)
105
+ result = @test_users.create(true, ["read_stream", "publish_stream"])
106
+ end
107
+
108
+ it "should create a test user when given installed and a permission" do
109
+ result = @test_users.create(true, "read_stream")
110
+ @temporary_object_id = result["id"]
111
+ result.should be_a(Hash)
112
+ (result["id"] && result["access_token"] && result["login_url"]).should
113
+ end
114
+
115
+ it "lets you specify other graph arguments, like uid and access token" do
116
+ args = {:uid => "some test user ID", :owner_access_token => "some owner access token"}
117
+ @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including(args), anything, anything)
118
+ @test_users.create(true, nil, args)
119
+ end
120
+
121
+ it "lets you specify http options that get passed through to the graph call" do
122
+ options = {:some_http_option => true}
123
+ @test_users.graph_api.should_receive(:graph_call).with(anything, anything, anything, options)
124
+ @test_users.create(true, nil, {}, options)
125
+ end
91
126
  end
92
127
 
93
- describe "with a user to delete" do
128
+ describe ".delete" do
94
129
  before :each do
95
130
  @user1 = @test_users.create(true, "read_stream")
96
131
  @user2 = @test_users.create(true, "read_stream,user_interests")
@@ -116,7 +151,7 @@ describe "Koala::Facebook::TestUsers" do
116
151
  end
117
152
  end
118
153
 
119
- describe "with delete_all" do
154
+ describe ".delete_all" do
120
155
  it "should delete all users found by the list commnand" do
121
156
  array = [1, 2, 3]
122
157
  @test_users.should_receive(:list).and_return(array)
@@ -125,6 +160,31 @@ describe "Koala::Facebook::TestUsers" do
125
160
  end
126
161
  end
127
162
 
163
+ describe ".update" do
164
+ before :each do
165
+ @updates = {:name => "Foo Baz"}
166
+ end
167
+
168
+ it "makes a POST with the test user Graph API " do
169
+ user = @test_users.create(true)
170
+ @test_users.graph_api.should_receive(:graph_call).with(anything, anything, "post", anything)
171
+ @test_users.update(user, @updates)
172
+ end
173
+
174
+ it "makes a request to the test user with the update params " do
175
+ user = @test_users.create(true)
176
+ @test_users.graph_api.should_receive(:graph_call).with(user["id"], @updates, anything, anything)
177
+ @test_users.update(user, @updates)
178
+ end
179
+
180
+ it "works" do
181
+ user = @test_users.create(true)
182
+ @test_users.update(user, @updates)
183
+ user_info = Koala::Facebook::API.new(user["access_token"]).get_object(user["id"])
184
+ user_info["name"].should == @updates[:name]
185
+ end
186
+ end
187
+
128
188
  describe "with existing users" do
129
189
  before :each do
130
190
  @user1 = @test_users.create(true, "read_stream")
@@ -143,22 +203,22 @@ describe "Koala::Facebook::TestUsers" do
143
203
  (first_user["id"] && first_user["access_token"] && first_user["login_url"]).should
144
204
  (second_user["id"] && second_user["access_token"] && second_user["login_url"]).should
145
205
  end
146
-
206
+
147
207
  it "should make two users into friends with string hashes" do
148
208
  result = @test_users.befriend(@user1, @user2)
149
209
  result.should be_true
150
210
  end
151
-
211
+
152
212
  it "should make two users into friends with symbol hashes" do
153
213
  new_user_1 = {}
154
214
  @user1.each_pair {|k, v| new_user_1[k.to_sym] = v}
155
215
  new_user_2 = {}
156
216
  @user2.each_pair {|k, v| new_user_2[k.to_sym] = v}
157
-
217
+
158
218
  result = @test_users.befriend(new_user_1, new_user_2)
159
219
  result.should be_true
160
- end
161
-
220
+ end
221
+
162
222
  it "should not accept user IDs anymore" do
163
223
  lambda { @test_users.befriend(@user1["id"], @user2["id"]) }.should raise_exception
164
224
  end
@@ -171,7 +231,7 @@ describe "Koala::Facebook::TestUsers" do
171
231
  @test_users = Koala::Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
172
232
  @network = []
173
233
 
174
- if @is_mock
234
+ if KoalaTest.mock_interface?
175
235
  id_counter = 999999900
176
236
  @test_users.stub!(:create).and_return do
177
237
  id_counter += 1
@@ -218,4 +278,4 @@ describe "Koala::Facebook::TestUsers" do
218
278
 
219
279
  end # when creating network
220
280
  end
221
- end # describe Koala TestUsers
281
+ end # describe Koala TestUsers
@@ -9,25 +9,45 @@ module Koala::MIME
9
9
  end
10
10
 
11
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
+ :original_filename => "bar"
18
+ )
19
+ tempfile.stub!(:respond_to?).with(:path).and_return(true)
20
+
21
+ [tempfile, uploaded_file]
22
+ end
23
+
24
+ def sinatra_mocks
25
+ {:type => "type", :tempfile => "Tempfile", :filename => "foo.bar"}
26
+ end
27
+
12
28
  describe "the constructor" do
13
29
  describe "when given a file path" do
14
30
  before(:each) do
15
- @koala_io_params = [File.open(BEACH_BALL_PATH)]
31
+ @path = BEACH_BALL_PATH
32
+ @koala_io_params = [@path]
16
33
  end
17
34
 
18
35
  describe "and a content type" do
19
36
  before :each do
20
- @koala_io_params.concat([stub("image/jpg")])
37
+ @stub_type = stub("image/jpg")
38
+ @koala_io_params.concat([@stub_type])
39
+ end
40
+
41
+ it "returns an UploadIO with the same file path" do
42
+ Koala::UploadableIO.new(*@koala_io_params).io_or_path.should == @path
21
43
  end
22
44
 
23
- it "should return an UploadIO with the same file path" do
24
- stub_path = @koala_io_params[0] = "/stub/path/to/file"
25
- Koala::UploadableIO.new(*@koala_io_params).io_or_path.should == stub_path
45
+ it "returns an UploadIO with the same content type" do
46
+ Koala::UploadableIO.new(*@koala_io_params).content_type.should == @stub_type
26
47
  end
27
48
 
28
- it "should return an UploadIO with the same content type" do
29
- stub_type = @koala_io_params[1] = stub('Content Type')
30
- Koala::UploadableIO.new(*@koala_io_params).content_type.should == stub_type
49
+ it "returns an UploadIO with the file's name" do
50
+ Koala::UploadableIO.new(*@koala_io_params).filename.should == File.basename(@path)
31
51
  end
32
52
  end
33
53
 
@@ -38,7 +58,8 @@ describe "Koala::UploadableIO" do
38
58
 
39
59
  describe "when given a File object" do
40
60
  before(:each) do
41
- @koala_io_params = [File.open(BEACH_BALL_PATH)]
61
+ @file = File.open(BEACH_BALL_PATH)
62
+ @koala_io_params = [@file]
42
63
  end
43
64
 
44
65
  describe "and a content type" do
@@ -46,14 +67,18 @@ describe "Koala::UploadableIO" do
46
67
  @koala_io_params.concat(["image/jpg"])
47
68
  end
48
69
 
49
- it "should return an UploadIO with the same io" do
70
+ it "returns an UploadIO with the same io" do
50
71
  Koala::UploadableIO.new(*@koala_io_params).io_or_path.should == @koala_io_params[0]
51
72
  end
52
73
 
53
- it "should return an UplaodIO with the same content_type" do
74
+ it "returns an UploadableIO with the same content_type" do
54
75
  content_stub = @koala_io_params[1] = stub('Content Type')
55
76
  Koala::UploadableIO.new(*@koala_io_params).content_type.should == content_stub
56
77
  end
78
+
79
+ it "returns an UploadableIO with the right filename" do
80
+ Koala::UploadableIO.new(*@koala_io_params).filename.should == File.basename(@file.path)
81
+ end
57
82
  end
58
83
 
59
84
  describe "and no content type" do
@@ -63,37 +88,40 @@ describe "Koala::UploadableIO" do
63
88
 
64
89
  describe "when given a Rails 3 ActionDispatch::Http::UploadedFile" do
65
90
  before(:each) do
66
- @tempfile = stub('Tempfile', :path => "foo")
67
- @uploaded_file = stub('ActionDispatch::Http::UploadedFile',
68
- :content_type => true,
69
- :tempfile => @tempfile
70
- )
91
+ @tempfile, @uploaded_file = rails_3_mocks
92
+ end
71
93
 
72
- @tempfile.stub!(:respond_to?).with(:path).and_return(true)
94
+ it "gets the path from the tempfile associated with the UploadedFile" do
95
+ expected_path = stub('Tempfile')
96
+ @tempfile.should_receive(:path).and_return(expected_path)
97
+ Koala::UploadableIO.new(@uploaded_file).io_or_path.should == expected_path
73
98
  end
74
99
 
75
- it "should get the content type via the content_type method" do
100
+ it "gets the content type via the content_type method" do
76
101
  expected_content_type = stub('Content Type')
77
102
  @uploaded_file.should_receive(:content_type).and_return(expected_content_type)
78
103
  Koala::UploadableIO.new(@uploaded_file).content_type.should == expected_content_type
79
104
  end
80
105
 
81
- it "should get the path from the tempfile associated with the UploadedFile" do
82
- expected_path = stub('Tempfile')
83
- @tempfile.should_receive(:path).and_return(expected_path)
84
- Koala::UploadableIO.new(@uploaded_file).io_or_path.should == expected_path
106
+ it "gets the filename from the UploadedFile" do
107
+ Koala::UploadableIO.new(@uploaded_file).filename.should == @uploaded_file.original_filename
85
108
  end
86
109
  end
87
110
 
88
111
  describe "when given a Sinatra file parameter hash" do
89
112
  before(:each) do
90
- @file_hash = {
91
- :type => "type",
92
- :tempfile => "Tempfile"
93
- }
113
+ @file_hash = sinatra_mocks
94
114
  end
95
115
 
96
- it "should get the content type from the :type key" do
116
+ it "gets 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
+
124
+ it "gets the content type from the :type key" do
97
125
  expected_content_type = stub('Content Type')
98
126
  @file_hash[:type] = expected_content_type
99
127
 
@@ -101,12 +129,9 @@ describe "Koala::UploadableIO" do
101
129
  uploadable.content_type.should == expected_content_type
102
130
  end
103
131
 
104
- it "should get the io_or_path from the :tempfile key" do
105
- expected_file = stub('File')
106
- @file_hash[:tempfile] = expected_file
107
-
132
+ it "gets the content type from the :type key" do
108
133
  uploadable = Koala::UploadableIO.new(@file_hash)
109
- uploadable.io_or_path.should == expected_file
134
+ uploadable.filename.should == @file_hash[:filename]
110
135
  end
111
136
  end
112
137
 
@@ -130,14 +155,24 @@ describe "Koala::UploadableIO" do
130
155
  UploadIO.stub!(:new).with(anything, anything, anything).and_return(@upload_io)
131
156
  end
132
157
 
133
- it "should call the constructor with the content type, file name, and a dummy file name" do
134
- UploadIO.should_receive(:new).with(BEACH_BALL_PATH, "content/type", anything).and_return(@upload_io)
135
- Koala::UploadableIO.new(BEACH_BALL_PATH, "content/type").to_upload_io.should == @upload_io
158
+ context "if no filename was provided" do
159
+ it "should call the constructor with the content type, file name, and a dummy file name" do
160
+ UploadIO.should_receive(:new).with(BEACH_BALL_PATH, "content/type", anything).and_return(@upload_io)
161
+ Koala::UploadableIO.new(BEACH_BALL_PATH, "content/type").to_upload_io.should == @upload_io
162
+ end
163
+ end
164
+
165
+ context "if a filename was provided" do
166
+ it "should call the constructor with the content type, file name, and the filename" do
167
+ filename = "file"
168
+ UploadIO.should_receive(:new).with(BEACH_BALL_PATH, "content/type", filename).and_return(@upload_io)
169
+ Koala::UploadableIO.new(BEACH_BALL_PATH, "content/type", filename).to_upload_io
170
+ end
136
171
  end
137
172
  end
138
173
 
139
174
  describe "getting a file" do
140
- it "should return the File if initialized with a file" do
175
+ it "returns the File if initialized with a file" do
141
176
  f = File.new(BEACH_BALL_PATH)
142
177
  Koala::UploadableIO.new(f).to_file.should == f
143
178
  end
@@ -148,4 +183,24 @@ describe "Koala::UploadableIO" do
148
183
  Koala::UploadableIO.new(BEACH_BALL_PATH).to_file.should == result
149
184
  end
150
185
  end
151
- end # describe UploadableIO
186
+
187
+ describe "#binary_content?" do
188
+ it "returns true for Rails 3 file uploads" do
189
+ Koala::UploadableIO.binary_content?(rails_3_mocks.last).should be_true
190
+ end
191
+
192
+ it "returns true for Sinatra file uploads" do
193
+ Koala::UploadableIO.binary_content?(rails_3_mocks.last).should be_true
194
+ end
195
+
196
+ it "returns true for File objects" do
197
+ Koala::UploadableIO.binary_content?(File.open(BEACH_BALL_PATH)).should be_true
198
+ end
199
+
200
+ it "returns false for everything else" do
201
+ Koala::UploadableIO.binary_content?(StringIO.new).should be_false
202
+ Koala::UploadableIO.binary_content?(BEACH_BALL_PATH).should be_false
203
+ Koala::UploadableIO.binary_content?(nil).should be_false
204
+ end
205
+ end
206
+ end # describe UploadableIO
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe Koala::Utils do
4
+ it "has a deprecate method" do
5
+ Koala::Utils.should respond_to(:deprecate)
6
+ end
7
+
8
+ # AFAIK there's no way to test that (Kernel.)warn receives the text
9
+ # Kernel.should_receive(:warn) doesn't seem to work, even though the text gets printed
10
+ end
Binary file