koala 0.10.0 → 1.0.0.beta

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.
@@ -16,6 +16,10 @@ class FacebookOAuthTests < Test::Unit::TestCase
16
16
  @request_secret = @oauth_data["request_secret"] || @secret
17
17
  @signed_request = @oauth_data["signed_request"]
18
18
  @signed_request_result = @oauth_data["signed_request_result"]
19
+ # for signed requests (http://developers.facebook.com/docs/authentication/canvas/encryption_proposal)
20
+ @signed_params_secret = @oauth_data["signed_params_secret"] || @secret
21
+ @signed_params = @oauth_data["signed_params"]
22
+ @signed_params_result = @oauth_data["signed_params_result"]
19
23
 
20
24
  # this should expanded to cover all variables
21
25
  raise Exception, "Must supply app data to run FacebookOAuthTests!" unless @app_id && @secret && @callback_url &&
@@ -24,9 +28,9 @@ class FacebookOAuthTests < Test::Unit::TestCase
24
28
 
25
29
  @oauth = Koala::Facebook::OAuth.new(@app_id, @secret, @callback_url)
26
30
 
27
- time = Time.now
28
- Time.stub!(:now).and_return(time)
29
- time.stub!(:to_i).and_return(1273363199)
31
+ @time = Time.now
32
+ Time.stub!(:now).and_return(@time)
33
+ @time.stub!(:to_i).and_return(1273363199)
30
34
  end
31
35
 
32
36
  # initialization
@@ -356,81 +360,48 @@ class FacebookOAuthTests < Test::Unit::TestCase
356
360
  @oauth = Koala::Facebook::OAuth.new(@app_id, @request_secret || @app_secret)
357
361
  end
358
362
 
359
- it "should break the request into the encoded signature and the payload" do
360
- @signed_request.should_receive(:split).with(".").and_return(["", ""])
361
- @oauth.parse_signed_request(@signed_request)
363
+ # the signed request code comes directly from Facebook
364
+ # so we only need to test at a high level that it works
365
+ # signed params refers to http://developers.facebook.com/docs/authentication/canvas
366
+ # signed request refers to http://developers.facebook.com/docs/authentication/canvas/encryption_proposal
367
+ it "should throw an error if the algorithm is unsupported" do
368
+ JSON.stub!(:parse).and_return("algorithm" => "my fun algorithm")
369
+ lambda { @oauth.parse_signed_request(@signed_request) }.should raise_error
362
370
  end
363
371
 
364
- it "should base64 URL decode the signed request" do
365
- sig = ""
366
- @signed_request.should_receive(:split).with(".").and_return([sig, "1"])
367
- @oauth.should_receive(:base64_url_decode).with(sig).and_return("4")
368
- @oauth.parse_signed_request(@signed_request)
372
+ it "should throw an error if the signature is invalid" do
373
+ OpenSSL::HMAC.stub!(:hexdigest).and_return("i'm an invalid signature")
374
+ lambda { @oauth.parse_signed_request(@signed_request) }.should raise_error
369
375
  end
370
376
 
371
- it "should base64 URL decode the signed request" do
372
- sig = @signed_request.split(".")[0]
373
- @oauth.should_receive(:base64_url_decode).with(sig).and_return(nil)
374
- @oauth.parse_signed_request(@signed_request)
375
- end
376
-
377
- it "should get the sha64 encoded payload using proper arguments from OpenSSL::HMAC" do
378
- payload = ""
379
- @signed_request.should_receive(:split).with(".").and_return(["1", payload])
380
- OpenSSL::HMAC.should_receive(:digest).with("sha256", @request_secret, payload)
381
- @oauth.parse_signed_request(@signed_request)
377
+ describe "for signed params" do
378
+ it "should work" do
379
+ @oauth.parse_signed_request(@signed_request).should == @signed_request_result
380
+ end
382
381
  end
383
382
 
384
- it "should compare the encoded payload with the signature" do
385
- sig = "2"
386
- @oauth.should_receive(:base64_url_decode).and_return(sig)
387
- encoded_payload = "1"
388
- OpenSSL::HMAC.should_receive(:digest).with(anything, anything, anything).and_return(encoded_payload)
389
- encoded_payload.should_receive(:==).with(sig)
390
- @oauth.parse_signed_request(@signed_request)
391
- end
392
-
393
- describe "if the encoded payload matches the signature" do
394
- before :each do
395
- # set it up so the sig will match the encoded payload
396
- raw_sig = ""
397
- @sig = "2"
398
- @payload = "1"
399
- @signed_request.should_receive(:split).and_return([raw_sig, @payload])
400
- @oauth.should_receive(:base64_url_decode).with(raw_sig).and_return(@sig)
401
- OpenSSL::HMAC.should_receive(:digest).with(anything, anything, anything).and_return(@sig.dup)
383
+ describe "for signed requests" do
384
+ it "should work" do
385
+ @oauth = Koala::Facebook::OAuth.new(@app_id, @signed_params_secret || @app_secret)
386
+ @oauth.parse_signed_request(@signed_params).should == @signed_params_result
402
387
  end
403
388
 
404
- it "should base64_url_decode the payload" do
405
- @oauth.should_receive(:base64_url_decode).with(@payload).ordered.and_return("{}")
406
- @oauth.parse_signed_request(@signed_request)
389
+ it "should throw an error if the params are too old" do
390
+ @time.stub!(:to_i).and_return(1287601988 + 4000)
391
+ @oauth = Koala::Facebook::OAuth.new(@app_id, @signed_params_secret || @app_secret)
392
+
393
+ lambda { @oauth.parse_signed_request(@signed_params) }.should raise_error
407
394
  end
408
395
 
409
- it "should JSON decode the payload" do
410
- result = "{}"
411
- @oauth.should_receive(:base64_url_decode).with(@payload).and_return(result)
412
- JSON.should_receive(:parse).with(result)
413
- @oauth.parse_signed_request(@signed_request)
414
- end
415
- end
416
-
417
- describe "if the encoded payload does not match the signature" do
418
- before :each do
419
- sig = ""
420
- @signed_request.should_receive(:split).and_return([sig, ""])
421
- OpenSSL::HMAC.should_receive(:digest).with(anything, anything, anything).and_return("hi")
396
+ it "should let you specify the max age for a request" do
397
+ @time.stub!(:to_i).and_return(1287601988 + 4000)
398
+ @oauth = Koala::Facebook::OAuth.new(@app_id, @signed_params_secret || @app_secret)
399
+
400
+ lambda { @oauth.parse_signed_request(@signed_params, 4001) }.should_not raise_error
422
401
  end
423
402
 
424
- it "should return nil" do
425
- @oauth.parse_signed_request(@signed_request).should be_nil
426
- end
427
403
  end
428
404
 
429
- describe "run against data" do
430
- it "should work" do
431
- @oauth.parse_signed_request(@signed_request).should == @signed_request_result
432
- end
433
- end
434
405
  end
435
406
 
436
407
  end # describe
@@ -1,94 +1,25 @@
1
1
  shared_examples_for "Koala RestAPI without an access token" do
2
- # REST_CALL
3
- describe "when making a rest request" do
4
- it "should use the proper path" do
5
- method = stub('methodName')
6
- @api.should_receive(:api).with(
7
- "method/#{method}",
8
- anything,
9
- anything,
10
- anything
11
- )
12
-
13
- @api.rest_call(method)
14
- end
15
-
16
- it "should always use the rest api" do
17
- @api.should_receive(:api).with(
18
- anything,
19
- anything,
20
- anything,
21
- :rest_api => true
22
- )
23
-
24
- @api.rest_call('anything')
25
- end
26
-
27
- it "should take an optional hash of arguments" do
28
- args = {:arg1 => 'arg1'}
29
-
30
- @api.should_receive(:api).with(
31
- anything,
32
- hash_including(args),
33
- anything,
34
- anything
35
- )
36
-
37
- @api.rest_call('anything', args)
38
- end
39
-
40
- it "should always ask for JSON" do
41
- @api.should_receive(:api).with(
42
- anything,
43
- hash_including('format' => 'json'),
44
- anything,
45
- anything
46
- )
47
-
48
- @api.rest_call('anything')
49
- end
50
- end
51
-
52
2
  # FQL_QUERY
53
3
  describe "when making a FQL request" do
54
- it "should call fql.query method" do
55
- @api.should_receive(:rest_call).with(
56
- "fql.query",
57
- anything
58
- ).and_return(Koala::Response.new(200, "2", {}))
59
-
60
- @api.fql_query stub('query string')
61
- end
62
-
63
- it "should pass a query argument" do
64
- query = stub('query string')
65
-
66
- @api.should_receive(:rest_call).with(
67
- anything,
68
- hash_including("query" => query)
69
- )
70
-
71
- @api.fql_query(query)
72
- end
73
-
74
4
  it "should be able to access public information via FQL" do
75
5
  @result = @api.fql_query("select first_name from user where uid = 216743")
76
6
  @result.size.should == 1
77
7
  @result.first["first_name"].should == "Chris"
78
8
  end
79
-
9
+
80
10
  it "should not be able to access protected information via FQL" do
81
11
  lambda { @api.fql_query("select read_stream from permissions where uid = 216743") }.should raise_error(Koala::Facebook::APIError)
82
12
  end
83
13
  end
84
14
  end
85
15
 
86
- class FacebookRestAPINoAccessTokenTest < Test::Unit::TestCase
16
+ class FacebookRestAPINoAccessTokenTest < Test::Unit::TestCase
87
17
  describe "Koala RestAPI without an access token" do
88
18
  before :each do
89
19
  @api = Koala::Facebook::RestAPI.new
90
20
  end
91
-
92
- it_should_behave_like "Koala RestAPI without an access token"
21
+
22
+ it_should_behave_like "Koala RestAPI"
23
+ it_should_behave_like "Koala RestAPI without an access token"
93
24
  end
94
25
  end
@@ -0,0 +1,118 @@
1
+ shared_examples_for "Koala RestAPI" do
2
+ # REST_CALL
3
+ describe "when making a rest request" do
4
+ it "should use the proper path" do
5
+ method = stub('methodName')
6
+ @api.should_receive(:api).with(
7
+ "method/#{method}",
8
+ anything,
9
+ anything,
10
+ anything
11
+ )
12
+
13
+ @api.rest_call(method)
14
+ end
15
+
16
+ it "should always use the rest api" do
17
+ @api.should_receive(:api).with(
18
+ anything,
19
+ anything,
20
+ anything,
21
+ hash_including(:rest_api => true)
22
+ )
23
+
24
+ @api.rest_call('anything')
25
+ end
26
+
27
+ it "should set the read_only option to true if the method is listed in the read-only list" do
28
+ method = Koala::Facebook::RestAPI::READ_ONLY_METHODS.first
29
+
30
+ @api.should_receive(:api).with(
31
+ anything,
32
+ anything,
33
+ anything,
34
+ hash_including(:read_only => true)
35
+ )
36
+
37
+ @api.rest_call(method)
38
+ end
39
+
40
+ it "should set the read_only option to false if the method is not inthe read-only list" do
41
+ method = "I'm not a read-only method"
42
+
43
+ @api.should_receive(:api).with(
44
+ anything,
45
+ anything,
46
+ anything,
47
+ hash_including(:read_only => false)
48
+ )
49
+
50
+ @api.rest_call(method)
51
+ end
52
+
53
+
54
+ it "should take an optional hash of arguments" do
55
+ args = {:arg1 => 'arg1'}
56
+
57
+ @api.should_receive(:api).with(
58
+ anything,
59
+ hash_including(args),
60
+ anything,
61
+ anything
62
+ )
63
+
64
+ @api.rest_call('anything', args)
65
+ end
66
+
67
+ it "should always ask for JSON" do
68
+ @api.should_receive(:api).with(
69
+ anything,
70
+ hash_including('format' => 'json'),
71
+ anything,
72
+ anything
73
+ )
74
+
75
+ @api.rest_call('anything')
76
+ end
77
+
78
+ it "should pass any options provided to the API" do
79
+ options = {:a => 2}
80
+
81
+ @api.should_receive(:api).with(
82
+ anything,
83
+ hash_including('format' => 'json'),
84
+ anything,
85
+ hash_including(options)
86
+ )
87
+
88
+ @api.rest_call('anything', {}, options)
89
+ end
90
+
91
+ it "should throw an APIError if the result hash has an error key" do
92
+ Koala.stub(:make_request).and_return(Koala::Response.new(500, {"error_code" => "An error occurred!"}, {}))
93
+ lambda { @api.rest_call("koppel", {}) }.should raise_exception(Koala::Facebook::APIError)
94
+ end
95
+
96
+ describe "when making a FQL request" do
97
+ it "should call fql.query method" do
98
+ @api.should_receive(:rest_call).with(
99
+ "fql.query",
100
+ anything
101
+ ).and_return(Koala::Response.new(200, "2", {}))
102
+
103
+ @api.fql_query stub('query string')
104
+ end
105
+
106
+ it "should pass a query argument" do
107
+ query = stub('query string')
108
+
109
+ @api.should_receive(:rest_call).with(
110
+ anything,
111
+ hash_including("query" => query)
112
+ )
113
+
114
+ @api.fql_query(query)
115
+ end
116
+ end
117
+ end
118
+ end
@@ -8,7 +8,7 @@ shared_examples_for "Koala RestAPI with an access token" do
8
8
 
9
9
  it "should be able to access protected information via FQL" do
10
10
  # Tests agains the permissions fql table
11
-
11
+
12
12
  # get the current user's ID
13
13
  # we're sneakily using the Graph API, which should be okay since it has its own tests
14
14
  g = Koala::Facebook::GraphAPI.new(@token)
@@ -27,10 +27,12 @@ end
27
27
  class FacebookRestAPIWithAccessTokenTests < Test::Unit::TestCase
28
28
  describe "Koala RestAPI with an access token" do
29
29
  include LiveTestingDataHelper
30
- it_should_behave_like "Koala RestAPI with an access token"
31
-
30
+
32
31
  before :each do
33
32
  @api = Koala::Facebook::RestAPI.new(@token)
34
33
  end
34
+
35
+ it_should_behave_like "Koala RestAPI"
36
+ it_should_behave_like "Koala RestAPI with an access token"
35
37
  end
36
38
  end
@@ -10,37 +10,37 @@ class TestUsersTests < Test::Unit::TestCase
10
10
  @app_id = @oauth_data["app_id"]
11
11
  @secret = @oauth_data["secret"]
12
12
  @app_access_token = @oauth_data["app_access_token"]
13
-
13
+
14
14
  # check OAuth data
15
15
  unless @app_id && @secret && @app_access_token
16
- raise Exception, "Must supply OAuth app id, secret, app_access_token, and callback to run live subscription tests!"
16
+ raise Exception, "Must supply OAuth app id, secret, app_access_token, and callback to run live subscription tests!"
17
17
  end
18
-
18
+
19
19
  @is_mock = defined?(Koala::IS_MOCK) && Koala::IS_MOCK
20
20
  end
21
-
21
+
22
22
  describe "when initializing" do
23
23
  # basic initialization
24
24
  it "should initialize properly with an app_id and an app_access_token" do
25
25
  test_users = Facebook::TestUsers.new(:app_id => @app_id, :app_access_token => @app_access_token)
26
26
  test_users.should be_a(Facebook::TestUsers)
27
27
  end
28
-
28
+
29
29
  # init with secret / fetching the token
30
- it "should initialize properly with an app_id and a secret" do
30
+ it "should initialize properly with an app_id and a secret" do
31
31
  test_users = Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
32
- test_users.should be_a(Facebook::TestUsers)
32
+ test_users.should be_a(Facebook::TestUsers)
33
33
  end
34
-
34
+
35
35
  it "should use the OAuth class to fetch a token when provided an app_id and a secret" do
36
36
  oauth = Facebook::OAuth.new(@app_id, @secret)
37
37
  token = oauth.get_app_access_token
38
38
  oauth.should_receive(:get_app_access_token).and_return(token)
39
- Facebook::OAuth.should_receive(:new).with(@app_id, @secret).and_return(oauth)
39
+ Facebook::OAuth.should_receive(:new).with(@app_id, @secret).and_return(oauth)
40
40
  test_users = Facebook::TestUsers.new(:app_id => @app_id, :secret => @secret)
41
41
  end
42
42
  end
43
-
43
+
44
44
  describe "when used without network" do
45
45
  before :each do
46
46
  @test_users = Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
@@ -53,31 +53,31 @@ class TestUsersTests < Test::Unit::TestCase
53
53
  result.should be_a(Hash)
54
54
  (result["id"] && result["access_token"] && result["login_url"]).should
55
55
  end
56
-
56
+
57
57
  it "should create a test user when not given installed, ignoring permissions" do
58
58
  result = @test_users.create(false, "read_stream")
59
59
  @temporary_object_id = result["id"]
60
60
  result.should be_a(Hash)
61
61
  (result["id"] && result["access_token"] && result["login_url"]).should
62
62
  end
63
-
63
+
64
64
  it "should accept permissions as a string" do
65
65
  @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including("permissions" => "read_stream,publish_stream"), anything)
66
66
  result = @test_users.create(true, "read_stream,publish_stream")
67
67
  end
68
-
68
+
69
69
  it "should accept permissions as an array" do
70
70
  @test_users.graph_api.should_receive(:graph_call).with(anything, hash_including("permissions" => "read_stream,publish_stream"), anything)
71
71
  result = @test_users.create(true, ["read_stream", "publish_stream"])
72
72
  end
73
-
73
+
74
74
  it "should create a test user when given installed and a permission" do
75
75
  result = @test_users.create(true, "read_stream")
76
76
  @temporary_object_id = result["id"]
77
77
  result.should be_a(Hash)
78
78
  (result["id"] && result["access_token"] && result["login_url"]).should
79
79
  end
80
-
80
+
81
81
  describe "with a user to delete" do
82
82
  before :each do
83
83
  @user1 = @test_users.create(true, "read_stream")
@@ -87,25 +87,25 @@ class TestUsersTests < Test::Unit::TestCase
87
87
  after :each do
88
88
  print "\nCleaning up test users..."
89
89
  @test_users.delete(@user1) if @user1
90
- @test_users.delete(@user2) if @user2
90
+ @test_users.delete(@user2) if @user2
91
91
  puts "done."
92
92
  end
93
-
93
+
94
94
  it "should delete a user by id" do
95
95
  @test_users.delete(@user1['id']).should be_true
96
96
  @user1 = nil
97
97
  end
98
-
98
+
99
99
  it "should delete a user by hash" do
100
100
  @test_users.delete(@user2).should be_true
101
101
  @user2 = nil
102
102
  end
103
-
103
+
104
104
  it "should not delete users when provided a false ID" do
105
105
  lambda { @test_users.delete("#{@user1['id']}1") }.should raise_exception(Koala::Facebook::APIError)
106
106
  end
107
107
  end
108
-
108
+
109
109
  describe "with delete_all" do
110
110
  it "should delete all users found by the list commnand" do
111
111
  array = [1, 2, 3]
@@ -114,18 +114,18 @@ class TestUsersTests < Test::Unit::TestCase
114
114
  @test_users.delete_all
115
115
  end
116
116
  end
117
-
117
+
118
118
  describe "with existing users" do
119
119
  before :each do
120
120
  @user1 = @test_users.create(true, "read_stream")
121
121
  @user2 = @test_users.create(true, "read_stream,user_interests")
122
122
  end
123
-
123
+
124
124
  after :each do
125
125
  @test_users.delete(@user1)
126
126
  @test_users.delete(@user2)
127
127
  end
128
-
128
+
129
129
  it "should list test users" do
130
130
  result = @test_users.list
131
131
  result.should be_an(Array)
@@ -133,26 +133,26 @@ class TestUsersTests < Test::Unit::TestCase
133
133
  (first_user["id"] && first_user["access_token"] && first_user["login_url"]).should
134
134
  (second_user["id"] && second_user["access_token"] && second_user["login_url"]).should
135
135
  end
136
-
136
+
137
137
  it "should make two users into friends by id" do
138
138
  result = @test_users.befriend(@user1['id'], @user2['id'])
139
139
  result.should be_true
140
140
  end
141
-
141
+
142
142
  it "should make two users into friends by hash" do
143
143
  result = @test_users.befriend(@user1, @user2)
144
144
  result.should be_true
145
145
  end
146
-
146
+
147
147
  end # with existing users
148
-
148
+
149
149
  end # when used without network
150
-
151
- describe "when creating a network of friends" do
150
+
151
+ describe "when creating a network of friends" do
152
152
  before :each do
153
153
  @test_users = Facebook::TestUsers.new({:app_access_token => @app_access_token, :app_id => @app_id})
154
154
  @network = []
155
-
155
+
156
156
  if @is_mock
157
157
  id_counter = 999999900
158
158
  @test_users.stub!(:create).and_return do
@@ -163,7 +163,7 @@ class TestUsersTests < Test::Unit::TestCase
163
163
  @test_users.stub!(:delete).and_return(true)
164
164
  end
165
165
  end
166
-
166
+
167
167
  describe "tests that create users" do
168
168
  before :each do
169
169
  print "\nCleaning up test user network..."
@@ -178,22 +178,15 @@ class TestUsersTests < Test::Unit::TestCase
178
178
  test_users.delete_all
179
179
  puts "done!"
180
180
  end
181
-
182
- it "should create a 2 person network" do
183
- @network = @test_users.create_network(2)
184
- @network.should be_a(Array)
185
- @network.size.should == 2
186
- end
187
-
188
- it "should create a 50 person network" do
189
- puts "\nStarting 50-person network test (this may take several minutes)..."
190
- @network = @test_users.create_network(50)
181
+
182
+ it "should create a 5 person network" do
183
+ size = 5
184
+ @network = @test_users.create_network(size)
191
185
  @network.should be_a(Array)
192
- @network.size.should == 50
193
- puts "done!"
186
+ @network.size.should == size
194
187
  end
195
188
  end
196
-
189
+
197
190
  it "should limit to a 50 person network" do
198
191
  @test_users.should_receive(:create).exactly(50).times
199
192
  @test_users.stub!(:befriend)
@@ -208,7 +201,7 @@ class TestUsersTests < Test::Unit::TestCase
208
201
  @test_users.stub!(:befriend)
209
202
  @network = @test_users.create_network(count, installed, perms)
210
203
  end
211
-
204
+
212
205
  end # when creating network
213
206
 
214
207
  end # describe Koala TestUsers