koala 1.1.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 (50) hide show
  1. data/.travis.yml +2 -1
  2. data/CHANGELOG +26 -0
  3. data/Gemfile +6 -2
  4. data/Rakefile +0 -1
  5. data/koala.gemspec +8 -8
  6. data/lib/koala.rb +42 -45
  7. data/lib/koala/batch_operation.rb +15 -15
  8. data/lib/koala/graph_api.rb +81 -58
  9. data/lib/koala/graph_batch_api.rb +10 -10
  10. data/lib/koala/graph_collection.rb +6 -6
  11. data/lib/koala/http_service.rb +177 -0
  12. data/lib/koala/oauth.rb +2 -2
  13. data/lib/koala/realtime_updates.rb +20 -17
  14. data/lib/koala/rest_api.rb +1 -1
  15. data/lib/koala/test_users.rb +33 -16
  16. data/lib/koala/uploadable_io.rb +47 -42
  17. data/lib/koala/utils.rb +11 -0
  18. data/readme.md +38 -38
  19. data/spec/cases/api_base_spec.rb +2 -2
  20. data/spec/cases/error_spec.rb +32 -0
  21. data/spec/cases/graph_and_rest_api_spec.rb +20 -3
  22. data/spec/cases/graph_api_batch_spec.rb +88 -97
  23. data/spec/cases/graph_api_spec.rb +21 -4
  24. data/spec/cases/http_service_spec.rb +446 -0
  25. data/spec/cases/koala_spec.rb +33 -38
  26. data/spec/cases/oauth_spec.rb +219 -200
  27. data/spec/cases/realtime_updates_spec.rb +45 -31
  28. data/spec/cases/rest_api_spec.rb +23 -7
  29. data/spec/cases/test_users_spec.rb +112 -52
  30. data/spec/cases/uploadable_io_spec.rb +49 -36
  31. data/spec/cases/utils_spec.rb +10 -0
  32. data/spec/fixtures/facebook_data.yml +23 -22
  33. data/spec/fixtures/mock_facebook_responses.yml +126 -96
  34. data/spec/spec_helper.rb +29 -5
  35. data/spec/support/graph_api_shared_examples.rb +59 -52
  36. data/spec/support/json_testing_fix.rb +35 -11
  37. data/spec/support/koala_test.rb +163 -0
  38. data/spec/support/mock_http_service.rb +6 -4
  39. data/spec/support/ordered_hash.rb +205 -0
  40. data/spec/support/rest_api_shared_examples.rb +37 -37
  41. data/spec/support/uploadable_io_shared_examples.rb +2 -8
  42. metadata +78 -79
  43. data/lib/koala/http_services.rb +0 -46
  44. data/lib/koala/http_services/net_http_service.rb +0 -92
  45. data/lib/koala/http_services/typhoeus_service.rb +0 -37
  46. data/spec/cases/http_services/http_service_spec.rb +0 -129
  47. data/spec/cases/http_services/net_http_service_spec.rb +0 -532
  48. data/spec/cases/http_services/typhoeus_service_spec.rb +0 -152
  49. data/spec/support/live_testing_data_helper.rb +0 -40
  50. data/spec/support/setup_mocks_or_live.rb +0 -51
@@ -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
- updates.methods.map(&:to_sym).should_not include(:app_id=)
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
- updates.methods.map(&:to_sym).should_not include(:app_access_token=)
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
- updates.methods.map(&:to_sym).should_not include(:secret=)
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 = Koala.http_service.respond_to?(:mock?) && Koala.http_service.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
@@ -13,42 +13,41 @@ describe "Koala::UploadableIO" do
13
13
  tempfile = stub('Tempfile', :path => "foo")
14
14
  uploaded_file = stub('ActionDispatch::Http::UploadedFile',
15
15
  :content_type => true,
16
- :tempfile => tempfile
16
+ :tempfile => tempfile,
17
+ :original_filename => "bar"
17
18
  )
18
19
  tempfile.stub!(:respond_to?).with(:path).and_return(true)
19
20
 
20
21
  [tempfile, uploaded_file]
21
22
  end
22
-
23
+
23
24
  def sinatra_mocks
24
- {:type => "type", :tempfile => "Tempfile"}
25
+ {:type => "type", :tempfile => "Tempfile", :filename => "foo.bar"}
25
26
  end
26
-
27
+
27
28
  describe "the constructor" do
28
29
  describe "when given a file path" do
29
30
  before(:each) do
30
- @koala_io_params = [File.open(BEACH_BALL_PATH)]
31
+ @path = BEACH_BALL_PATH
32
+ @koala_io_params = [@path]
31
33
  end
32
34
 
33
35
  describe "and a content type" do
34
36
  before :each do
35
- @koala_io_params.concat([stub("image/jpg")])
37
+ @stub_type = stub("image/jpg")
38
+ @koala_io_params.concat([@stub_type])
36
39
  end
37
40
 
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
+ it "returns an UploadIO with the same file path" do
42
+ Koala::UploadableIO.new(*@koala_io_params).io_or_path.should == @path
41
43
  end
42
44
 
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
45
+ it "returns an UploadIO with the same content type" do
46
+ Koala::UploadableIO.new(*@koala_io_params).content_type.should == @stub_type
46
47
  end
47
48
 
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
49
+ it "returns an UploadIO with the file's name" do
50
+ Koala::UploadableIO.new(*@koala_io_params).filename.should == File.basename(@path)
52
51
  end
53
52
  end
54
53
 
@@ -59,7 +58,8 @@ describe "Koala::UploadableIO" do
59
58
 
60
59
  describe "when given a File object" do
61
60
  before(:each) do
62
- @koala_io_params = [File.open(BEACH_BALL_PATH)]
61
+ @file = File.open(BEACH_BALL_PATH)
62
+ @koala_io_params = [@file]
63
63
  end
64
64
 
65
65
  describe "and a content type" do
@@ -67,14 +67,18 @@ describe "Koala::UploadableIO" do
67
67
  @koala_io_params.concat(["image/jpg"])
68
68
  end
69
69
 
70
- it "should return an UploadIO with the same io" do
70
+ it "returns an UploadIO with the same io" do
71
71
  Koala::UploadableIO.new(*@koala_io_params).io_or_path.should == @koala_io_params[0]
72
72
  end
73
73
 
74
- it "should return an UplaodIO with the same content_type" do
74
+ it "returns an UploadableIO with the same content_type" do
75
75
  content_stub = @koala_io_params[1] = stub('Content Type')
76
76
  Koala::UploadableIO.new(*@koala_io_params).content_type.should == content_stub
77
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
78
82
  end
79
83
 
80
84
  describe "and no content type" do
@@ -87,16 +91,20 @@ describe "Koala::UploadableIO" do
87
91
  @tempfile, @uploaded_file = rails_3_mocks
88
92
  end
89
93
 
90
- it "should get the content type via the content_type method" do
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
98
+ end
99
+
100
+ it "gets the content type via the content_type method" do
91
101
  expected_content_type = stub('Content Type')
92
102
  @uploaded_file.should_receive(:content_type).and_return(expected_content_type)
93
103
  Koala::UploadableIO.new(@uploaded_file).content_type.should == expected_content_type
94
104
  end
95
105
 
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
106
+ it "gets the filename from the UploadedFile" do
107
+ Koala::UploadableIO.new(@uploaded_file).filename.should == @uploaded_file.original_filename
100
108
  end
101
109
  end
102
110
 
@@ -105,7 +113,15 @@ describe "Koala::UploadableIO" do
105
113
  @file_hash = sinatra_mocks
106
114
  end
107
115
 
108
- 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
109
125
  expected_content_type = stub('Content Type')
110
126
  @file_hash[:type] = expected_content_type
111
127
 
@@ -113,12 +129,9 @@ describe "Koala::UploadableIO" do
113
129
  uploadable.content_type.should == expected_content_type
114
130
  end
115
131
 
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
-
132
+ it "gets the content type from the :type key" do
120
133
  uploadable = Koala::UploadableIO.new(@file_hash)
121
- uploadable.io_or_path.should == expected_file
134
+ uploadable.filename.should == @file_hash[:filename]
122
135
  end
123
136
  end
124
137
 
@@ -148,7 +161,7 @@ describe "Koala::UploadableIO" do
148
161
  Koala::UploadableIO.new(BEACH_BALL_PATH, "content/type").to_upload_io.should == @upload_io
149
162
  end
150
163
  end
151
-
164
+
152
165
  context "if a filename was provided" do
153
166
  it "should call the constructor with the content type, file name, and the filename" do
154
167
  filename = "file"
@@ -159,7 +172,7 @@ describe "Koala::UploadableIO" do
159
172
  end
160
173
 
161
174
  describe "getting a file" do
162
- it "should return the File if initialized with a file" do
175
+ it "returns the File if initialized with a file" do
163
176
  f = File.new(BEACH_BALL_PATH)
164
177
  Koala::UploadableIO.new(f).to_file.should == f
165
178
  end
@@ -175,19 +188,19 @@ describe "Koala::UploadableIO" do
175
188
  it "returns true for Rails 3 file uploads" do
176
189
  Koala::UploadableIO.binary_content?(rails_3_mocks.last).should be_true
177
190
  end
178
-
191
+
179
192
  it "returns true for Sinatra file uploads" do
180
193
  Koala::UploadableIO.binary_content?(rails_3_mocks.last).should be_true
181
194
  end
182
-
195
+
183
196
  it "returns true for File objects" do
184
197
  Koala::UploadableIO.binary_content?(File.open(BEACH_BALL_PATH)).should be_true
185
198
  end
186
-
199
+
187
200
  it "returns false for everything else" do
188
201
  Koala::UploadableIO.binary_content?(StringIO.new).should be_false
189
202
  Koala::UploadableIO.binary_content?(BEACH_BALL_PATH).should be_false
190
203
  Koala::UploadableIO.binary_content?(nil).should be_false
191
204
  end
192
205
  end
193
- end # describe UploadableIO
206
+ end # describe UploadableIO