koala 1.3.0rc1 → 1.3.0rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.gitignore +3 -1
  2. data/.travis.yml +4 -0
  3. data/.yardopts +3 -0
  4. data/CHANGELOG +7 -0
  5. data/Gemfile +14 -0
  6. data/Guardfile +6 -0
  7. data/lib/koala.rb +16 -97
  8. data/lib/koala/api.rb +93 -0
  9. data/lib/koala/api/batch_operation.rb +83 -0
  10. data/lib/koala/api/graph_api.rb +476 -0
  11. data/lib/koala/{graph_batch_api.rb → api/graph_batch_api.rb} +20 -16
  12. data/lib/koala/api/graph_collection.rb +107 -0
  13. data/lib/koala/api/legacy.rb +26 -0
  14. data/lib/koala/{rest_api.rb → api/rest_api.rb} +33 -3
  15. data/lib/koala/http_service.rb +69 -19
  16. data/lib/koala/http_service/multipart_request.rb +41 -0
  17. data/lib/koala/http_service/response.rb +18 -0
  18. data/lib/koala/http_service/uploadable_io.rb +187 -0
  19. data/lib/koala/oauth.rb +117 -14
  20. data/lib/koala/realtime_updates.rb +89 -51
  21. data/lib/koala/test_users.rb +109 -33
  22. data/lib/koala/utils.rb +4 -0
  23. data/lib/koala/version.rb +1 -1
  24. data/spec/cases/api_spec.rb +19 -12
  25. data/spec/cases/graph_api_batch_spec.rb +41 -41
  26. data/spec/cases/http_service_spec.rb +1 -22
  27. data/spec/cases/legacy_spec.rb +107 -0
  28. data/spec/cases/multipart_request_spec.rb +5 -5
  29. data/spec/cases/oauth_spec.rb +9 -9
  30. data/spec/cases/realtime_updates_spec.rb +154 -47
  31. data/spec/cases/test_users_spec.rb +268 -219
  32. data/spec/fixtures/mock_facebook_responses.yml +10 -6
  33. data/spec/support/graph_api_shared_examples.rb +17 -12
  34. data/spec/support/koala_test.rb +1 -1
  35. data/spec/support/mock_http_service.rb +2 -2
  36. data/spec/support/rest_api_shared_examples.rb +1 -1
  37. metadata +82 -104
  38. data/lib/koala/batch_operation.rb +0 -74
  39. data/lib/koala/graph_api.rb +0 -289
  40. data/lib/koala/graph_collection.rb +0 -63
  41. data/lib/koala/multipart_request.rb +0 -35
  42. data/lib/koala/uploadable_io.rb +0 -181
  43. data/spec/cases/graph_and_rest_api_spec.rb +0 -22
  44. data/spec/cases/graph_api_spec.rb +0 -22
  45. data/spec/cases/rest_api_spec.rb +0 -22
@@ -350,17 +350,17 @@ describe "Koala::Facebook::OAuth" do
350
350
  describe "#get_access_token_info" do
351
351
  it "uses options[:redirect_uri] if provided" do
352
352
  uri = "foo"
353
- Koala.should_receive(:make_request).with(anything, hash_including(:redirect_uri => uri), anything, anything).and_return(Koala::Response.new(200, "", {}))
353
+ Koala.should_receive(:make_request).with(anything, hash_including(:redirect_uri => uri), anything, anything).and_return(Koala::HTTPService::Response.new(200, "", {}))
354
354
  @oauth.get_access_token_info(@code, :redirect_uri => uri)
355
355
  end
356
356
 
357
357
  it "uses the redirect_uri used to create the @oauth if no :redirect_uri option is provided" do
358
- Koala.should_receive(:make_request).with(anything, hash_including(:redirect_uri => @callback_url), anything, anything).and_return(Koala::Response.new(200, "", {}))
358
+ Koala.should_receive(:make_request).with(anything, hash_including(:redirect_uri => @callback_url), anything, anything).and_return(Koala::HTTPService::Response.new(200, "", {}))
359
359
  @oauth.get_access_token_info(@code)
360
360
  end
361
361
 
362
362
  it "makes a GET request" do
363
- Koala.should_receive(:make_request).with(anything, anything, "get", anything).and_return(Koala::Response.new(200, "", {}))
363
+ Koala.should_receive(:make_request).with(anything, anything, "get", anything).and_return(Koala::HTTPService::Response.new(200, "", {}))
364
364
  @oauth.get_access_token_info(@code)
365
365
  end
366
366
 
@@ -385,7 +385,7 @@ describe "Koala::Facebook::OAuth" do
385
385
  # TODO refactor these to be proper tests with stubs and tests against real data
386
386
  it "passes on any options provided to make_request" do
387
387
  options = {:a => 2}
388
- Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "", {}))
388
+ Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
389
389
  @oauth.get_access_token(@code, options)
390
390
  end
391
391
 
@@ -424,7 +424,7 @@ describe "Koala::Facebook::OAuth" do
424
424
 
425
425
  it "passes on any options provided to make_request" do
426
426
  options = {:a => 2}
427
- Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "", {}))
427
+ Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
428
428
  @oauth.get_app_access_token_info(options)
429
429
  end
430
430
  end
@@ -443,7 +443,7 @@ describe "Koala::Facebook::OAuth" do
443
443
 
444
444
  it "passes on any options provided to make_request" do
445
445
  options = {:a => 2}
446
- Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "", {}))
446
+ Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
447
447
  @oauth.get_app_access_token(options)
448
448
  end
449
449
  end
@@ -524,7 +524,7 @@ describe "Koala::Facebook::OAuth" do
524
524
 
525
525
  it "passes on any options provided to make_request" do
526
526
  options = {:a => 2}
527
- Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "[{}]", {}))
527
+ Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "[{}]", {}))
528
528
  @oauth.get_token_info_from_session_keys([], options)
529
529
  end
530
530
  end
@@ -556,7 +556,7 @@ describe "Koala::Facebook::OAuth" do
556
556
 
557
557
  it "passes on any options provided to make_request" do
558
558
  options = {:a => 2}
559
- Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "[{}]", {}))
559
+ Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "[{}]", {}))
560
560
  @oauth.get_tokens_from_session_keys([], options)
561
561
  end
562
562
  end
@@ -586,7 +586,7 @@ describe "Koala::Facebook::OAuth" do
586
586
 
587
587
  it "passes on any options provided to make_request" do
588
588
  options = {:a => 2}
589
- Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::Response.new(200, "[{}]", {}))
589
+ Koala.should_receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "[{}]", {}))
590
590
  @oauth.get_token_from_session_key("", options)
591
591
  end
592
592
  end
@@ -24,33 +24,37 @@ describe "Koala::Facebook::RealtimeUpdates" do
24
24
  end
25
25
  end
26
26
 
27
- describe "when initializing" do
27
+ before :each do
28
+ @updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
29
+ end
30
+
31
+ describe ".new" do
28
32
  # basic initialization
29
- it "should initialize properly with an app_id and an app_access_token" do
33
+ it "initializes properly with an app_id and an app_access_token" do
30
34
  updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
31
35
  updates.should be_a(Koala::Facebook::RealtimeUpdates)
32
36
  end
33
37
 
34
38
  # attributes
35
- it "should allow read access to app_id" do
39
+ it "allows read access to app_id" do
36
40
  # in Ruby 1.9, .method returns symbols
37
41
  Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should include(:app_id)
38
42
  Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should_not include(:app_id=)
39
43
  end
40
44
 
41
- it "should allow read access to app_access_token" do
45
+ it "allows read access to app_access_token" do
42
46
  # in Ruby 1.9, .method returns symbols
43
47
  Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should include(:app_access_token)
44
48
  Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should_not include(:app_access_token=)
45
49
  end
46
50
 
47
- it "should allow read access to secret" do
51
+ it "allows read access to secret" do
48
52
  # in Ruby 1.9, .method returns symbols
49
53
  Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should include(:secret)
50
54
  Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should_not include(:secret=)
51
55
  end
52
56
 
53
- it "should allow read access to api" do
57
+ it "allows read access to api" do
54
58
  # in Ruby 1.9, .method returns symbols
55
59
  Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should include(:api)
56
60
  Koala::Facebook::RealtimeUpdates.instance_methods.map(&:to_sym).should_not include(:api=)
@@ -69,72 +73,178 @@ describe "Koala::Facebook::RealtimeUpdates" do
69
73
  end
70
74
 
71
75
  # init with secret / fetching the token
72
- it "should initialize properly with an app_id and a secret" do
76
+ it "initializes properly with an app_id and a secret" do
73
77
  updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
74
78
  updates.should be_a(Koala::Facebook::RealtimeUpdates)
75
79
  end
76
80
 
77
- it "should fetch an app_token from Facebook when provided an app_id and a secret" do
81
+ it "fetches an app_token from Facebook when provided an app_id and a secret" do
78
82
  updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
79
83
  updates.app_access_token.should_not be_nil
80
84
  end
81
-
82
- it "should use the OAuth class to fetch a token when provided an app_id and a secret" do
85
+
86
+ it "uses the OAuth class to fetch a token when provided an app_id and a secret" do
83
87
  oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
84
88
  token = oauth.get_app_access_token
85
89
  oauth.should_receive(:get_app_access_token).and_return(token)
86
90
  Koala::Facebook::OAuth.should_receive(:new).with(@app_id, @secret).and_return(oauth)
87
91
  updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
88
92
  end
93
+
94
+ it "sets up the with the app acces token" do
95
+ updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :app_access_token => @app_access_token)
96
+ updates.api.should be_a(Koala::Facebook::API)
97
+ updates.api.access_token.should == @app_access_token
98
+ end
99
+
89
100
  end
90
101
 
91
- describe "when used" do
92
- before :each do
93
- @updates = Koala::Facebook::RealtimeUpdates.new(:app_id => @app_id, :secret => @secret)
102
+ describe "#subscribe" do
103
+ it "makes a POST to the subscription path" do
104
+ @updates.api.should_receive(:graph_call).with(@updates.subscription_path, anything, "post", anything)
105
+ @updates.subscribe("user", "name", @subscription_path, @verify_token)
106
+ end
107
+
108
+ it "properly formats the subscription request" do
109
+ obj = "user"
110
+ fields = "name"
111
+ @updates.api.should_receive(:graph_call).with(anything, hash_including(
112
+ :object => obj,
113
+ :fields => fields,
114
+ :callback_url => @subscription_path,
115
+ :verify_token => @verify_token
116
+ ), anything, anything)
117
+ @updates.subscribe("user", "name", @subscription_path, @verify_token)
118
+ end
119
+
120
+ pending "doesn't require a verify_token" do
121
+ # see https://github.com/arsduo/koala/issues/150
122
+ obj = "user"
123
+ fields = "name"
124
+ @updates.api.should_not_receive(:graph_call).with(anything, hash_including(:verify_token => anything), anything, anything)
125
+ @updates.subscribe("user", "name", @subscription_path)
126
+ end
127
+
128
+ it "requires verify_token" do
129
+ expect { @updates.subscribe("user", "name", @subscription_path) }.to raise_exception
94
130
  end
95
131
 
96
- it "should send a subscription request to a valid server" do
97
- result = @updates.subscribe("user", "name", @subscription_path, @verify_token)
98
- result.should be_true
132
+ it "requests the status component" do
133
+ @updates.api.should_receive(:graph_call).with(anything, anything, anything, hash_including(:http_component => :status))
134
+ @updates.subscribe("user", "name", @subscription_path, @verify_token)
99
135
  end
100
136
 
101
- it "should send a subscription request to a valid server" do
102
- result = @updates.subscribe("user", "name", @subscription_path, @verify_token)
103
- result.should be_true
137
+ it "accepts an options hash" do
138
+ options = {:a => 2, :b => "c"}
139
+ @updates.api.should_receive(:graph_call).with(anything, anything, anything, hash_including(options))
140
+ @updates.subscribe("user", "name", @subscription_path, @verify_token, options)
104
141
  end
105
142
 
106
- it "should send a subscription request to an invalid path on a valid server" do
107
- lambda { result = @updates.subscribe("user", "name", @subscription_path + "foo", @verify_token) }.should raise_exception(Koala::Facebook::APIError)
143
+ it "overrides any provided http_component" do
144
+ # since we test against status, we need to ensure that we get that
145
+ options = {:http_component => :body}
146
+ @updates.api.should_receive(:graph_call).with(anything, anything, anything, hash_including(:http_component => :status))
147
+ @updates.subscribe("user", "name", @subscription_path, @verify_token, options)
108
148
  end
149
+
150
+ describe "in practice" do
151
+ it "sends a subscription request" do
152
+ result = @updates.subscribe("user", "name", @subscription_path, @verify_token)
153
+ result.should be_true
154
+ end
109
155
 
110
- it "should fail to send a subscription request to an invalid server" do
111
- lambda { @updates.subscribe("user", "name", "foo", @verify_token) }.should raise_exception(Koala::Facebook::APIError)
156
+ pending "sends a subscription request without a verify token" do
157
+ result = @updates.subscribe("user", "name", @subscription_path)
158
+ result.should be_true
159
+ end
160
+
161
+ it "fails if you try to hit an invalid path on your valid server" do
162
+ expect { result = @updates.subscribe("user", "name", @subscription_path + "foo", @verify_token) }.to raise_exception(Koala::Facebook::APIError)
163
+ end
164
+
165
+ it "fails to send a subscription request to an invalid server" do
166
+ expect { @updates.subscribe("user", "name", "foo", @verify_token) }.to raise_exception(Koala::Facebook::APIError)
167
+ end
112
168
  end
169
+ end
113
170
 
114
- it "should unsubscribe a valid individual object successfully" do
115
- @updates.unsubscribe("user").should be_true
171
+ describe "#unsubscribe" do
172
+ it "makes a DELETE to the subscription path" do
173
+ @updates.api.should_receive(:graph_call).with(@updates.subscription_path, anything, "delete", anything)
174
+ @updates.unsubscribe("user")
116
175
  end
117
176
 
118
- it "should unsubscribe all subscriptions successfully" do
119
- @updates.unsubscribe.should be_true
177
+ it "includes the object if provided" do
178
+ obj = "user"
179
+ @updates.api.should_receive(:graph_call).with(anything, hash_including(:object => obj), anything, anything)
180
+ @updates.unsubscribe(obj)
120
181
  end
182
+
183
+ it "requests the status component" do
184
+ @updates.api.should_receive(:graph_call).with(anything, anything, anything, hash_including(:http_component => :status))
185
+ @updates.unsubscribe("user")
186
+ end
187
+
188
+ it "accepts an options hash" do
189
+ options = {:a => 2, :b => "C"}
190
+ @updates.api.should_receive(:graph_call).with(anything, anything, anything, hash_including(options))
191
+ @updates.unsubscribe("user", options)
192
+ end
193
+
194
+ it "overrides any provided http_component" do
195
+ # since we test against status, we need to ensure that we get that
196
+ options = {:http_component => :body}
197
+ @updates.api.should_receive(:graph_call).with(anything, anything, anything, hash_including(:http_component => :status))
198
+ @updates.unsubscribe("user", options)
199
+ end
200
+
201
+ describe "in practice" do
202
+ it "unsubscribes a valid individual object successfully" do
203
+ @updates.unsubscribe("user").should be_true
204
+ end
205
+
206
+ it "unsubscribes all subscriptions successfully" do
207
+ @updates.unsubscribe.should be_true
208
+ end
121
209
 
122
- it "should fail when an invalid object is provided to unsubscribe" do
123
- lambda { @updates.unsubscribe("kittens") }.should raise_error(Koala::Facebook::APIError)
210
+ it "fails when an invalid object is provided to unsubscribe" do
211
+ expect { @updates.unsubscribe("kittens") }.to raise_error(Koala::Facebook::APIError)
212
+ end
213
+ end
214
+ end
215
+
216
+ describe "#list_subscriptions" do
217
+ it "GETs the subscription path" do
218
+ @updates.api.should_receive(:graph_call).with(@updates.subscription_path, anything, "get", anything)
219
+ @updates.list_subscriptions
124
220
  end
125
221
 
126
- it "should is subscriptions properly" do
127
- @updates.list_subscriptions.should be_a(Array)
222
+ it "accepts options" do
223
+ options = {:a => 3, :b => "D"}
224
+ @updates.api.should_receive(:graph_call).with(anything, anything, anything, hash_including(options))
225
+ @updates.list_subscriptions(options)
128
226
  end
129
- end # describe "when used"
227
+
228
+ describe "in practice" do
229
+ it "lists subscriptions properly" do
230
+ @updates.list_subscriptions.should be_a(Array)
231
+ end
232
+ end
233
+ end
234
+
235
+ describe "#subscription_path" do
236
+ it "returns the app_id/subscriptions" do
237
+ @updates.subscription_path.should == "#{@app_id}/subscriptions"
238
+ end
239
+ end
130
240
 
131
- describe "when meeting challenge" do
132
- it "should return false if hub.mode isn't subscribe" do
241
+ describe ".meet_challenge" do
242
+ it "returns false if hub.mode isn't subscribe" do
133
243
  params = {'hub.mode' => 'not subscribe'}
134
244
  Koala::Facebook::RealtimeUpdates.meet_challenge(params).should be_false
135
245
  end
136
246
 
137
- it "should return false if not given a verify_token or block" do
247
+ it "returns false if not given a verify_token or block" do
138
248
  params = {'hub.mode' => 'subscribe'}
139
249
  Koala::Facebook::RealtimeUpdates.meet_challenge(params).should be_false
140
250
  end
@@ -150,11 +260,11 @@ describe "Koala::Facebook::RealtimeUpdates" do
150
260
  @params['hub.verify_token'] = @token
151
261
  end
152
262
 
153
- it "should return false if the given verify token doesn't match" do
263
+ it "returns false if the given verify token doesn't match" do
154
264
  Koala::Facebook::RealtimeUpdates.meet_challenge(@params, @token + '1').should be_false
155
265
  end
156
266
 
157
- it "should return the challenge if the given verify token matches" do
267
+ it "returns the challenge if the given verify token matches" do
158
268
  @params['hub.challenge'] = 'challenge val'
159
269
  Koala::Facebook::RealtimeUpdates.meet_challenge(@params, @token).should == @params['hub.challenge']
160
270
  end
@@ -165,34 +275,31 @@ describe "Koala::Facebook::RealtimeUpdates" do
165
275
  @params['hub.verify_token'] = @token
166
276
  end
167
277
 
168
- it "should give the block the token as a parameter" do
278
+ it "gives the block the token as a parameter" do
169
279
  Koala::Facebook::RealtimeUpdates.meet_challenge(@params) do |token|
170
280
  token.should == @token
171
281
  end
172
282
  end
173
283
 
174
- it "should return false if the given block return false" do
284
+ it "returns false if the given block return false" do
175
285
  Koala::Facebook::RealtimeUpdates.meet_challenge(@params) do |token|
176
286
  false
177
287
  end.should be_false
178
288
  end
179
289
 
180
- it "should return false if the given block returns nil" do
290
+ it "returns false if the given block returns nil" do
181
291
  Koala::Facebook::RealtimeUpdates.meet_challenge(@params) do |token|
182
292
  nil
183
293
  end.should be_false
184
294
  end
185
295
 
186
- it "should return the challenge if the given block returns true" do
296
+ it "returns the challenge if the given block returns true" do
187
297
  @params['hub.challenge'] = 'challenge val'
188
298
  Koala::Facebook::RealtimeUpdates.meet_challenge(@params) do |token|
189
299
  true
190
300
  end.should be_true
191
301
  end
192
302
  end
193
-
194
- end # describe "and mode is subscribe"
195
-
196
- end # describe "when meeting challenge"
197
-
198
- end # describe
303
+ end
304
+ end
305
+ end
@@ -1,271 +1,320 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "Koala::Facebook::TestUsers" do
4
- context "with access token" do
5
- before :all do
6
- # get oauth data
7
- @app_id = KoalaTest.app_id
8
- @secret = KoalaTest.secret
9
- @app_access_token = KoalaTest.app_access_token
10
-
11
- # check OAuth data
12
- unless @app_id && @secret && @app_access_token
13
- raise Exception, "Must supply OAuth app id, secret, app_access_token, and callback to run live subscription tests!"
14
- end
4
+ before :all do
5
+ # get oauth data
6
+ @app_id = KoalaTest.app_id
7
+ @secret = KoalaTest.secret
8
+ @app_access_token = KoalaTest.app_access_token
9
+
10
+ @test_users = Koala::Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
11
+
12
+ # check OAuth data
13
+ unless @app_id && @secret && @app_access_token
14
+ raise Exception, "Must supply OAuth app id, secret, app_access_token, and callback to run live subscription tests!"
15
15
  end
16
-
17
- after :each do
18
- # clean up any test users
19
- ((@network || []) + [@user1, @user2]).each do |u|
20
- puts "Unable to delete test user #{u.inspect}" if u && !(@test_users.delete(u) rescue false)
21
- end
16
+ end
17
+
18
+ after :each do
19
+ # clean up any test users
20
+ ((@network || []) + [@user1, @user2]).each do |u|
21
+ puts "Unable to delete test user #{u.inspect}" if u && !(@test_users.delete(u) rescue false)
22
22
  end
23
+ end
23
24
 
24
- describe "when initializing" do
25
- # basic initialization
26
- it "should initialize properly with an app_id and an app_access_token" do
27
- test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :app_access_token => @app_access_token)
28
- test_users.should be_a(Koala::Facebook::TestUsers)
29
- end
25
+ describe "when initializing" do
26
+ # basic initialization
27
+ it "initializes properly with an app_id and an app_access_token" do
28
+ test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :app_access_token => @app_access_token)
29
+ test_users.should be_a(Koala::Facebook::TestUsers)
30
+ end
30
31
 
31
- # init with secret / fetching the token
32
- it "should initialize properly with an app_id and a secret" do
33
- test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
34
- test_users.should be_a(Koala::Facebook::TestUsers)
35
- end
32
+ # init with secret / fetching the token
33
+ it "initializes properly with an app_id and a secret" do
34
+ test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
35
+ test_users.should be_a(Koala::Facebook::TestUsers)
36
+ end
37
+
38
+ it "uses the OAuth class to fetch a token when provided an app_id and a secret" do
39
+ oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
40
+ token = oauth.get_app_access_token
41
+ oauth.should_receive(:get_app_access_token).and_return(token)
42
+ Koala::Facebook::OAuth.should_receive(:new).with(@app_id, @secret).and_return(oauth)
43
+ test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
44
+ end
45
+
46
+ # attributes
47
+ it "allows read access to app_id, app_access_token, and secret" do
48
+ # in Ruby 1.9, .method returns symbols
49
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should include(:app_id)
50
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should_not include(:app_id=)
51
+ end
52
+
53
+ it "allows read access to app_access_token" do
54
+ # in Ruby 1.9, .method returns symbols
55
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should include(:app_access_token)
56
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should_not include(:app_access_token=)
57
+ end
58
+
59
+ it "allows read access to secret" do
60
+ # in Ruby 1.9, .method returns symbols
61
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should include(:secret)
62
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should_not include(:secret=)
63
+ end
64
+
65
+ it "allows read access to api" do
66
+ # in Ruby 1.9, .method returns symbols
67
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should include(:api)
68
+ Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should_not include(:api=)
69
+ end
70
+
71
+ # old graph_api accessor
72
+ it "returns the api object when graph_api is called" do
73
+ test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
74
+ test_users.graph_api.should == test_users.api
75
+ end
36
76
 
37
- it "should use the OAuth class to fetch a token when provided an app_id and a secret" do
38
- oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
39
- token = oauth.get_app_access_token
40
- oauth.should_receive(:get_app_access_token).and_return(token)
41
- Koala::Facebook::OAuth.should_receive(:new).with(@app_id, @secret).and_return(oauth)
42
- test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
77
+ it "fire a deprecation warning when graph_api is called" do
78
+ test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
79
+ Koala::Utils.should_receive(:deprecate)
80
+ test_users.graph_api
81
+ end
82
+ end
83
+
84
+ describe "when used without network" do
85
+ # TEST USER MANAGEMENT
86
+
87
+ describe "#create" do
88
+ it "creates a test user when not given installed" do
89
+ result = @test_users.create(false)
90
+ @user1 = result["id"]
91
+ result.should be_a(Hash)
92
+ (result["id"] && result["access_token"] && result["login_url"]).should
43
93
  end
44
94
 
45
- # attributes
46
- it "should allow read access to app_id, app_access_token, and secret" do
47
- # in Ruby 1.9, .method returns symbols
48
- Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should include(:app_id)
49
- Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should_not include(:app_id=)
95
+ it "creates a test user when not given installed, ignoring permissions" do
96
+ result = @test_users.create(false, "read_stream")
97
+ @user1 = result["id"]
98
+ result.should be_a(Hash)
99
+ (result["id"] && result["access_token"] && result["login_url"]).should
50
100
  end
51
101
 
52
- it "should allow read access to app_access_token" do
53
- # in Ruby 1.9, .method returns symbols
54
- Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should include(:app_access_token)
55
- Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should_not include(:app_access_token=)
102
+ it "accepts permissions as a string" do
103
+ @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including("permissions" => "read_stream,publish_stream"), anything, anything)
104
+ result = @test_users.create(true, "read_stream,publish_stream")
56
105
  end
57
106
 
58
- it "should allow read access to secret" do
59
- # in Ruby 1.9, .method returns symbols
60
- Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should include(:secret)
61
- Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should_not include(:secret=)
107
+ it "accepts permissions as an array" do
108
+ @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including("permissions" => "read_stream,publish_stream"), anything, anything)
109
+ result = @test_users.create(true, ["read_stream", "publish_stream"])
62
110
  end
63
111
 
64
- it "should allow read access to api" do
65
- # in Ruby 1.9, .method returns symbols
66
- Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should include(:api)
67
- Koala::Facebook::TestUsers.instance_methods.map(&:to_sym).should_not include(:api=)
112
+ it "creates a test user when given installed and a permission" do
113
+ result = @test_users.create(true, "read_stream")
114
+ @user1 = result["id"]
115
+ result.should be_a(Hash)
116
+ (result["id"] && result["access_token"] && result["login_url"]).should
68
117
  end
69
118
 
70
- # old graph_api accessor
71
- it "returns the api object when graph_api is called" do
72
- test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
73
- test_users.graph_api.should == test_users.api
119
+ it "lets you specify other graph arguments, like uid and access token" do
120
+ args = {:uid => "some test user ID", :owner_access_token => "some owner access token"}
121
+ @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including(args), anything, anything)
122
+ @test_users.create(true, nil, args)
74
123
  end
75
124
 
76
- it "fire a deprecation warning when graph_api is called" do
77
- test_users = Koala::Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
78
- Koala::Utils.should_receive(:deprecate)
79
- test_users.graph_api
125
+ it "lets you specify http options that get passed through to the graph call" do
126
+ options = {:some_http_option => true}
127
+ @test_users.graph_api.should_receive(:graph_call).with(anything, anything, anything, options)
128
+ @test_users.create(true, nil, {}, options)
80
129
  end
81
130
  end
82
-
83
- describe "when used without network" do
131
+
132
+ describe "#list" do
84
133
  before :each do
85
- @test_users = Koala::Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
134
+ @user1 = @test_users.create(true, "read_stream")
135
+ @user2 = @test_users.create(true, "read_stream,user_interests")
86
136
  end
87
137
 
88
- # TEST USER MANAGEMENT
89
-
90
- describe "#create" do
91
- it "should create a test user when not given installed" do
92
- result = @test_users.create(false)
93
- @user1 = result["id"]
94
- result.should be_a(Hash)
95
- (result["id"] && result["access_token"] && result["login_url"]).should
96
- end
97
-
98
- it "should create a test user when not given installed, ignoring permissions" do
99
- result = @test_users.create(false, "read_stream")
100
- @user1 = result["id"]
101
- result.should be_a(Hash)
102
- (result["id"] && result["access_token"] && result["login_url"]).should
103
- end
104
-
105
- it "should accept permissions as a string" do
106
- @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including("permissions" => "read_stream,publish_stream"), anything, anything)
107
- result = @test_users.create(true, "read_stream,publish_stream")
108
- end
109
-
110
- it "should accept permissions as an array" do
111
- @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including("permissions" => "read_stream,publish_stream"), anything, anything)
112
- result = @test_users.create(true, ["read_stream", "publish_stream"])
113
- end
114
-
115
- it "should create a test user when given installed and a permission" do
116
- result = @test_users.create(true, "read_stream")
117
- @user1 = result["id"]
118
- result.should be_a(Hash)
119
- (result["id"] && result["access_token"] && result["login_url"]).should
120
- end
121
-
122
- it "lets you specify other graph arguments, like uid and access token" do
123
- args = {:uid => "some test user ID", :owner_access_token => "some owner access token"}
124
- @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including(args), anything, anything)
125
- @test_users.create(true, nil, args)
126
- end
127
-
128
- it "lets you specify http options that get passed through to the graph call" do
129
- options = {:some_http_option => true}
130
- @test_users.graph_api.should_receive(:graph_call).with(anything, anything, anything, options)
131
- @test_users.create(true, nil, {}, options)
132
- end
138
+ it "lists test users" do
139
+ result = @test_users.list
140
+ result.should be_an(Array)
141
+ first_user, second_user = result[0], result[1]
142
+ (first_user["id"] && first_user["access_token"] && first_user["login_url"]).should
143
+ (second_user["id"] && second_user["access_token"] && second_user["login_url"]).should
133
144
  end
145
+
146
+ it "accepts http options" do
147
+ options = {:some_http_option => true}
148
+ @test_users.api.should_receive(:graph_call).with(anything, anything, anything, options)
149
+ @test_users.list(options)
150
+ end
151
+ end
134
152
 
135
- describe "#delete" do
136
- before :each do
137
- @user1 = @test_users.create(true, "read_stream")
138
- @user2 = @test_users.create(true, "read_stream,user_interests")
139
- end
140
-
141
- it "should delete a user by id" do
142
- @test_users.delete(@user1['id']).should be_true
143
- @user1 = nil
144
- end
145
-
146
- it "should delete a user by hash" do
147
- @test_users.delete(@user2).should be_true
148
- @user2 = nil
149
- end
153
+ describe "#delete" do
154
+ before :each do
155
+ @user1 = @test_users.create(true, "read_stream")
156
+ @user2 = @test_users.create(true, "read_stream,user_interests")
157
+ end
150
158
 
151
- it "should not delete users when provided a false ID" do
152
- lambda { @test_users.delete("#{@user1['id']}1") }.should raise_exception(Koala::Facebook::APIError)
153
- end
159
+ it "deletes a user by id" do
160
+ @test_users.delete(@user1['id']).should be_true
161
+ @user1 = nil
154
162
  end
155
163
 
156
- describe "#delete_all" do
157
- it "should delete all users found by the list commnand" do
158
- array = [1, 2, 3]
159
- @test_users.should_receive(:list).and_return(array)
160
- array.each {|i| @test_users.should_receive(:delete).with(i) }
161
- @test_users.delete_all
162
- end
164
+ it "deletes a user by hash" do
165
+ @test_users.delete(@user2).should be_true
166
+ @user2 = nil
163
167
  end
164
168
 
165
- describe "#update" do
166
- before :each do
167
- @updates = {:name => "Foo Baz"}
168
- # we stub out :graph_call, but still need to be able to delete the users
169
- @test_users2 = Koala::Facebook::TestUsers.new(:app_id => @test_users.app_id, :app_access_token => @test_users.app_access_token)
170
- end
169
+ it "does not delete users when provided a false ID" do
170
+ lambda { @test_users.delete("#{@user1['id']}1") }.should raise_exception(Koala::Facebook::APIError)
171
+ end
172
+
173
+ it "lets you specify http options that get passed through to the graph call" do
174
+ options = {:some_http_option => true}
175
+ # technically this goes through delete_object, but this makes it less brittle
176
+ @test_users.graph_api.should_receive(:graph_call).with(anything, anything, anything, options)
177
+ @test_users.delete("user", options)
178
+ end
179
+ end
171
180
 
172
- it "makes a POST with the test user Graph API " do
173
- @user1 = @test_users2.create(true)
174
- @test_users2.graph_api.should_receive(:graph_call).with(anything, anything, "post", anything)
175
- @test_users2.update(@user1, @updates)
176
- end
181
+ describe "#delete_all" do
182
+ it "deletes all users found by the list commnand" do
183
+ array = [1, 2, 3]
184
+ @test_users.should_receive(:list).and_return(array)
185
+ array.each {|i| @test_users.should_receive(:delete).with(i, anything) }
186
+ @test_users.delete_all
187
+ end
188
+
189
+ it "accepts http options that get passed to both list and delete" do
190
+ array = [1, 2, 3]
191
+ options = {:some_http_option => true}
192
+ @test_users.should_receive(:list).with(options).and_return(array)
193
+ array.each {|i| @test_users.should_receive(:delete).with(anything, options) }
194
+ @test_users.delete_all(options)
195
+ end
196
+ end
177
197
 
178
- it "makes a request to the test user with the update params " do
179
- @user1 = @test_users2.create(true)
180
- @test_users2.graph_api.should_receive(:graph_call).with(@user1["id"], @updates, anything, anything)
181
- @test_users2.update(@user1, @updates)
182
- end
198
+ describe "#update" do
199
+ before :each do
200
+ @updates = {:name => "Foo Baz"}
201
+ # we stub out :graph_call, but still need to be able to delete the users
202
+ @test_users2 = Koala::Facebook::TestUsers.new(:app_id => @test_users.app_id, :app_access_token => @test_users.app_access_token)
203
+ end
183
204
 
184
- it "works" do
185
- @user1 = @test_users.create(true)
186
- @test_users.update(@user1, @updates)
187
- user_info = Koala::Facebook::API.new(@user1["access_token"]).get_object(@user1["id"])
188
- user_info["name"].should == @updates[:name]
189
- end
205
+ it "makes a POST with the test user Graph API " do
206
+ @user1 = @test_users2.create(true)
207
+ @test_users2.graph_api.should_receive(:graph_call).with(anything, anything, "post", anything)
208
+ @test_users2.update(@user1, @updates)
190
209
  end
191
210
 
192
- describe "with existing users" do
193
- before :each do
194
- @user1 = @test_users.create(true, "read_stream")
195
- @user2 = @test_users.create(true, "read_stream,user_interests")
196
- end
211
+ it "makes a request to the test user with the update params " do
212
+ @user1 = @test_users2.create(true)
213
+ @test_users2.graph_api.should_receive(:graph_call).with(@user1["id"], @updates, anything, anything)
214
+ @test_users2.update(@user1, @updates)
215
+ end
216
+
217
+ it "accepts an options hash" do
218
+ options = {:some_http_option => true}
219
+ @test_users2.graph_api.should_receive(:graph_call).with(anything, anything, anything, options)
220
+ @test_users2.update("foo", @updates, options)
221
+ end
197
222
 
198
- it "should list test users" do
199
- result = @test_users.list
200
- result.should be_an(Array)
201
- first_user, second_user = result[0], result[1]
202
- (first_user["id"] && first_user["access_token"] && first_user["login_url"]).should
203
- (second_user["id"] && second_user["access_token"] && second_user["login_url"]).should
204
- end
223
+ it "works" do
224
+ @user1 = @test_users.create(true)
225
+ @test_users.update(@user1, @updates)
226
+ user_info = Koala::Facebook::API.new(@user1["access_token"]).get_object(@user1["id"])
227
+ user_info["name"].should == @updates[:name]
228
+ end
229
+ end
230
+
231
+ describe "#befriend" do
232
+ before :each do
233
+ @user1 = @test_users.create(true, "read_stream")
234
+ @user2 = @test_users.create(true, "read_stream,user_interests")
235
+ end
236
+
237
+ it "makes two users into friends with string hashes" do
238
+ result = @test_users.befriend(@user1, @user2)
239
+ result.should be_true
240
+ end
205
241
 
206
- it "should make two users into friends with string hashes" do
207
- result = @test_users.befriend(@user1, @user2)
208
- result.should be_true
209
- end
242
+ it "makes two users into friends with symbol hashes" do
243
+ new_user_1 = {}
244
+ @user1.each_pair {|k, v| new_user_1[k.to_sym] = v}
245
+ new_user_2 = {}
246
+ @user2.each_pair {|k, v| new_user_2[k.to_sym] = v}
210
247
 
211
- it "should make two users into friends with symbol hashes" do
212
- new_user_1 = {}
213
- @user1.each_pair {|k, v| new_user_1[k.to_sym] = v}
214
- new_user_2 = {}
215
- @user2.each_pair {|k, v| new_user_2[k.to_sym] = v}
248
+ result = @test_users.befriend(new_user_1, new_user_2)
249
+ result.should be_true
250
+ end
216
251
 
217
- result = @test_users.befriend(new_user_1, new_user_2)
218
- result.should be_true
219
- end
252
+ it "does not accept user IDs anymore" do
253
+ lambda { @test_users.befriend(@user1["id"], @user2["id"]) }.should raise_exception
254
+ end
255
+
256
+ it "accepts http options passed to both calls" do
257
+ options = {:some_http_option => true}
258
+ # should come twice, once for each user
259
+ Koala.http_service.should_receive(:make_request).with(anything, anything, anything, options).twice.and_return(Koala::HTTPService::Response.new(200, "{}", {}))
260
+ @test_users.befriend(@user1, @user2, options)
261
+ end
262
+ end
263
+ end # when used without network
220
264
 
221
- it "should not accept user IDs anymore" do
222
- lambda { @test_users.befriend(@user1["id"], @user2["id"]) }.should raise_exception
223
- end
224
- end # with existing users
265
+ describe "#test_user_accounts_path" do
266
+ it "returns the app_id/accounts/test-users" do
267
+ @test_users.test_user_accounts_path.should == "/#{@app_id}/accounts/test-users"
268
+ end
269
+ end
225
270
 
226
- end # when used without network
271
+ describe "when creating a network of friends" do
272
+ before :each do
273
+ @network = []
227
274
 
228
- describe "when creating a network of friends" do
229
- before :each do
230
- @test_users = Koala::Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
231
- @network = []
232
-
233
- if KoalaTest.mock_interface?
234
- id_counter = 999999900
235
- @test_users.stub!(:create).and_return do
236
- id_counter += 1
237
- {"id" => id_counter, "access_token" => @token, "login_url" => "https://www.facebook.com/platform/test_account.."}
238
- end
239
- @test_users.stub!(:befriend).and_return(true)
240
- @test_users.stub!(:delete).and_return(true)
275
+ if KoalaTest.mock_interface?
276
+ id_counter = 999999900
277
+ @test_users.stub!(:create).and_return do
278
+ id_counter += 1
279
+ {"id" => id_counter, "access_token" => @token, "login_url" => "https://www.facebook.com/platform/test_account.."}
241
280
  end
281
+ @test_users.stub!(:befriend).and_return(true)
282
+ @test_users.stub!(:delete).and_return(true)
242
283
  end
284
+ end
243
285
 
244
- describe "tests that create users" do
245
- it "should create a 5 person network" do
246
- size = 5
247
- @network = @test_users.create_network(size)
248
- @network.should be_a(Array)
249
- @network.size.should == size
250
- end
286
+ describe "tests that create users" do
287
+ it "creates a 5 person network" do
288
+ size = 5
289
+ @network = @test_users.create_network(size)
290
+ @network.should be_a(Array)
291
+ @network.size.should == size
251
292
  end
293
+ end
252
294
 
253
- it "has no built-in network size limit" do
254
- times = 100
255
- @test_users.should_receive(:create).exactly(times).times
256
- @test_users.stub!(:befriend)
257
- @network = @test_users.create_network(times)
258
- end
295
+ it "has no built-in network size limit" do
296
+ times = 100
297
+ @test_users.should_receive(:create).exactly(times).times
298
+ @test_users.stub!(:befriend)
299
+ @test_users.create_network(times)
300
+ end
259
301
 
260
- it "should pass on the installed and permissions parameters to create" do
261
- perms = ["read_stream", "offline_access"]
262
- installed = false
263
- count = 25
264
- @test_users.should_receive(:create).exactly(count).times.with(installed, perms)
265
- @test_users.stub!(:befriend)
266
- @network = @test_users.create_network(count, installed, perms)
267
- end
302
+ it "passes on the installed and permissions parameters to create" do
303
+ perms = ["read_stream", "offline_access"]
304
+ installed = false
305
+ count = 25
306
+ @test_users.should_receive(:create).exactly(count).times.with(installed, perms, anything)
307
+ @test_users.stub!(:befriend)
308
+ @test_users.create_network(count, installed, perms)
309
+ end
268
310
 
269
- end # when creating network
270
- end
311
+ it "accepts http options that are passed to both the create and befriend calls" do
312
+ count = 25
313
+ options = {:some_http_option => true}
314
+ @test_users.should_receive(:create).exactly(count).times.with(anything, anything, options).and_return({})
315
+ # there are more befriends than creates, but we don't need to do the extra work to calculate out the exact #
316
+ @test_users.should_receive(:befriend).at_least(count).times.with(anything, anything, options)
317
+ @test_users.create_network(count, true, "", options)
318
+ end
319
+ end # when creating network
271
320
  end # describe Koala TestUsers