koala 1.8.0 → 1.9.0rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -2
- data/Gemfile +5 -9
- data/changelog.md +22 -3
- data/koala.gemspec +0 -2
- data/lib/koala.rb +5 -0
- data/lib/koala/api.rb +12 -2
- data/lib/koala/api/graph_api.rb +4 -1
- data/lib/koala/version.rb +1 -1
- data/readme.md +17 -4
- data/spec/cases/api_spec.rb +81 -24
- data/spec/cases/error_spec.rb +16 -16
- data/spec/cases/graph_api_batch_spec.rb +103 -103
- data/spec/cases/graph_api_spec.rb +33 -10
- data/spec/cases/graph_collection_spec.rb +35 -35
- data/spec/cases/http_service_spec.rb +92 -92
- data/spec/cases/koala_spec.rb +9 -9
- data/spec/cases/legacy_spec.rb +22 -22
- data/spec/cases/multipart_request_spec.rb +20 -21
- data/spec/cases/oauth_spec.rb +125 -125
- data/spec/cases/realtime_updates_spec.rb +44 -44
- data/spec/cases/test_users_spec.rb +58 -58
- data/spec/cases/uploadable_io_spec.rb +36 -36
- data/spec/cases/utils_spec.rb +11 -11
- data/spec/spec_helper.rb +0 -19
- data/spec/support/custom_matchers.rb +3 -3
- data/spec/support/graph_api_shared_examples.rb +117 -114
- data/spec/support/koala_test.rb +3 -8
- data/spec/support/rest_api_shared_examples.rb +18 -19
- data/spec/support/uploadable_io_shared_examples.rb +10 -10
- metadata +20 -50
- data/spec/support/ordered_hash.rb +0 -201
data/spec/cases/koala_spec.rb
CHANGED
@@ -2,13 +2,13 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Koala do
|
4
4
|
it "has an http_service accessor" do
|
5
|
-
Koala.
|
6
|
-
Koala.
|
5
|
+
expect(Koala).to respond_to(:http_service)
|
6
|
+
expect(Koala).to respond_to(:http_service=)
|
7
7
|
end
|
8
8
|
|
9
9
|
describe "constants" do
|
10
10
|
it "has a version" do
|
11
|
-
Koala.const_defined?("VERSION").
|
11
|
+
expect(Koala.const_defined?("VERSION")).to be_truthy
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -23,21 +23,21 @@ describe Koala do
|
|
23
23
|
|
24
24
|
it "invokes deprecated_interface if present" do
|
25
25
|
mock_service = double("http service")
|
26
|
-
mock_service.
|
26
|
+
expect(mock_service).to receive(:deprecated_interface)
|
27
27
|
Koala.http_service = mock_service
|
28
28
|
end
|
29
29
|
|
30
30
|
it "does not set the service if it's deprecated" do
|
31
31
|
mock_service = double("http service")
|
32
|
-
mock_service.
|
32
|
+
allow(mock_service).to receive(:deprecated_interface)
|
33
33
|
Koala.http_service = mock_service
|
34
|
-
Koala.http_service.
|
34
|
+
expect(Koala.http_service).to eq(@service)
|
35
35
|
end
|
36
36
|
|
37
37
|
it "sets the service if it's not deprecated" do
|
38
38
|
mock_service = double("http service")
|
39
39
|
Koala.http_service = mock_service
|
40
|
-
Koala.http_service.
|
40
|
+
expect(Koala.http_service).to eq(mock_service)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -48,7 +48,7 @@ describe Koala do
|
|
48
48
|
verb = "get"
|
49
49
|
options = {:c => :d}
|
50
50
|
|
51
|
-
Koala.http_service.
|
51
|
+
expect(Koala.http_service).to receive(:make_request).with(path, args, verb, options)
|
52
52
|
Koala.make_request(path, args, verb, options)
|
53
53
|
end
|
54
54
|
end
|
@@ -77,7 +77,7 @@ describe Koala do
|
|
77
77
|
Koala.configure do |config|
|
78
78
|
config.graph_server = "some-new.graph_server.com"
|
79
79
|
end
|
80
|
-
Koala.config.graph_server.
|
80
|
+
expect(Koala.config.graph_server).to eq("some-new.graph_server.com")
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
data/spec/cases/legacy_spec.rb
CHANGED
@@ -5,27 +5,27 @@ describe "legacy APIs" do
|
|
5
5
|
|
6
6
|
it "deprecates the REST API" do
|
7
7
|
api = Koala::Facebook::API.new
|
8
|
-
api.
|
9
|
-
Koala::Utils.
|
8
|
+
allow(api).to receive(:api)
|
9
|
+
expect(Koala::Utils).to receive(:deprecate)
|
10
10
|
api.rest_call("stuff")
|
11
11
|
end
|
12
12
|
|
13
13
|
describe Koala::Facebook::GraphAPI do
|
14
14
|
describe "class consolidation" do
|
15
15
|
before :each do
|
16
|
-
Koala::Utils.
|
16
|
+
allow(Koala::Utils).to receive(:deprecate) # avoid actual messages to stderr
|
17
17
|
end
|
18
18
|
|
19
19
|
it "still allows you to instantiate a GraphAndRestAPI object" do
|
20
|
-
api = Koala::Facebook::GraphAPI.new("token").
|
20
|
+
api = expect(Koala::Facebook::GraphAPI.new("token")).to be_a(Koala::Facebook::GraphAPI)
|
21
21
|
end
|
22
22
|
|
23
23
|
it "ultimately creates an API object" do
|
24
|
-
api = Koala::Facebook::GraphAPI.new("token").
|
24
|
+
api = expect(Koala::Facebook::GraphAPI.new("token")).to be_a(Koala::Facebook::API)
|
25
25
|
end
|
26
26
|
|
27
27
|
it "fires a depreciation warning" do
|
28
|
-
Koala::Utils.
|
28
|
+
expect(Koala::Utils).to receive(:deprecate)
|
29
29
|
api = Koala::Facebook::GraphAPI.new("token")
|
30
30
|
end
|
31
31
|
end
|
@@ -34,19 +34,19 @@ describe "legacy APIs" do
|
|
34
34
|
describe Koala::Facebook::RestAPI do
|
35
35
|
describe "class consolidation" do
|
36
36
|
before :each do
|
37
|
-
Koala::Utils.
|
37
|
+
allow(Koala::Utils).to receive(:deprecate) # avoid actual messages to stderr
|
38
38
|
end
|
39
39
|
|
40
40
|
it "still allows you to instantiate a GraphAndRestAPI object" do
|
41
|
-
api = Koala::Facebook::RestAPI.new("token").
|
41
|
+
api = expect(Koala::Facebook::RestAPI.new("token")).to be_a(Koala::Facebook::RestAPI)
|
42
42
|
end
|
43
43
|
|
44
44
|
it "ultimately creates an API object" do
|
45
|
-
api = Koala::Facebook::RestAPI.new("token").
|
45
|
+
api = expect(Koala::Facebook::RestAPI.new("token")).to be_a(Koala::Facebook::API)
|
46
46
|
end
|
47
47
|
|
48
48
|
it "fires a depreciation warning" do
|
49
|
-
Koala::Utils.
|
49
|
+
expect(Koala::Utils).to receive(:deprecate)
|
50
50
|
api = Koala::Facebook::RestAPI.new("token")
|
51
51
|
end
|
52
52
|
end
|
@@ -55,19 +55,19 @@ describe "legacy APIs" do
|
|
55
55
|
describe Koala::Facebook::GraphAndRestAPI do
|
56
56
|
describe "class consolidation" do
|
57
57
|
before :each do
|
58
|
-
Koala::Utils.
|
58
|
+
allow(Koala::Utils).to receive(:deprecate) # avoid actual messages to stderr
|
59
59
|
end
|
60
60
|
|
61
61
|
it "still allows you to instantiate a GraphAndRestAPI object" do
|
62
|
-
api = Koala::Facebook::GraphAndRestAPI.new("token").
|
62
|
+
api = expect(Koala::Facebook::GraphAndRestAPI.new("token")).to be_a(Koala::Facebook::GraphAndRestAPI)
|
63
63
|
end
|
64
64
|
|
65
65
|
it "ultimately creates an API object" do
|
66
|
-
api = Koala::Facebook::GraphAndRestAPI.new("token").
|
66
|
+
api = expect(Koala::Facebook::GraphAndRestAPI.new("token")).to be_a(Koala::Facebook::API)
|
67
67
|
end
|
68
68
|
|
69
69
|
it "fires a depreciation warning" do
|
70
|
-
Koala::Utils.
|
70
|
+
expect(Koala::Utils).to receive(:deprecate)
|
71
71
|
api = Koala::Facebook::GraphAndRestAPI.new("token")
|
72
72
|
end
|
73
73
|
end
|
@@ -76,40 +76,40 @@ describe "legacy APIs" do
|
|
76
76
|
{:typhoeus => Koala::TyphoeusService, :net_http => Koala::NetHTTPService}.each_pair do |adapter, module_class|
|
77
77
|
describe module_class.to_s do
|
78
78
|
it "responds to deprecated_interface" do
|
79
|
-
module_class.
|
79
|
+
expect(module_class).to respond_to(:deprecated_interface)
|
80
80
|
end
|
81
81
|
|
82
82
|
it "issues a deprecation warning" do
|
83
|
-
Koala::Utils.
|
83
|
+
expect(Koala::Utils).to receive(:deprecate)
|
84
84
|
module_class.deprecated_interface
|
85
85
|
end
|
86
86
|
|
87
87
|
it "sets the default adapter to #{adapter}" do
|
88
88
|
module_class.deprecated_interface
|
89
|
-
Faraday.default_adapter.
|
89
|
+
expect(Faraday.default_adapter).to eq(adapter)
|
90
90
|
end
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
94
|
describe "moved classes" do
|
95
95
|
it "allows you to access Koala::HTTPService::MultipartRequest through the Koala module" do
|
96
|
-
Koala::MultipartRequest.
|
96
|
+
expect(Koala::MultipartRequest).to eq(Koala::HTTPService::MultipartRequest)
|
97
97
|
end
|
98
98
|
|
99
99
|
it "allows you to access Koala::Response through the Koala module" do
|
100
|
-
Koala::Response.
|
100
|
+
expect(Koala::Response).to eq(Koala::HTTPService::Response)
|
101
101
|
end
|
102
102
|
|
103
103
|
it "allows you to access Koala::Response through the Koala module" do
|
104
|
-
Koala::UploadableIO.
|
104
|
+
expect(Koala::UploadableIO).to eq(Koala::HTTPService::UploadableIO)
|
105
105
|
end
|
106
106
|
|
107
107
|
it "allows you to access Koala::Facebook::GraphBatchAPI::BatchOperation through the Koala::Facebook module" do
|
108
|
-
Koala::Facebook::BatchOperation.
|
108
|
+
expect(Koala::Facebook::BatchOperation).to eq(Koala::Facebook::GraphBatchAPI::BatchOperation)
|
109
109
|
end
|
110
110
|
|
111
111
|
it "allows you to access Koala::Facebook::API::GraphCollection through the Koala::Facebook module" do
|
112
|
-
Koala::Facebook::GraphCollection.
|
112
|
+
expect(Koala::Facebook::GraphCollection).to eq(Koala::Facebook::API::GraphCollection)
|
113
113
|
end
|
114
114
|
end
|
115
115
|
end
|
@@ -2,65 +2,64 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Koala::HTTPService::MultipartRequest do
|
4
4
|
it "is a subclass of Faraday::Request::Multipart" do
|
5
|
-
Koala::HTTPService::MultipartRequest.superclass.
|
5
|
+
expect(Koala::HTTPService::MultipartRequest.superclass).to eq(Faraday::Request::Multipart)
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
it "defines mime_type as multipart/form-data" do
|
9
|
-
Koala::HTTPService::MultipartRequest.mime_type.
|
9
|
+
expect(Koala::HTTPService::MultipartRequest.mime_type).to eq('multipart/form-data')
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
describe "#process_request?" do
|
13
13
|
before :each do
|
14
|
-
@env =
|
14
|
+
@env = Faraday::Env.new
|
15
15
|
@multipart = Koala::HTTPService::MultipartRequest.new
|
16
|
-
@multipart.
|
16
|
+
allow(@multipart).to receive(:request_type).and_return("")
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
# no way to test the call to super, unfortunately
|
20
20
|
it "returns true if env[:body] is a hash with at least one hash in its values" do
|
21
21
|
@env[:body] = {:a => {:c => 2}}
|
22
|
-
@multipart.process_request?(@env).
|
22
|
+
expect(@multipart.process_request?(@env)).to be_truthy
|
23
23
|
end
|
24
24
|
|
25
25
|
it "returns true if env[:body] is a hash with at least one array in its values" do
|
26
26
|
@env[:body] = {:a => [:c, 2]}
|
27
|
-
@multipart.process_request?(@env).
|
27
|
+
expect(@multipart.process_request?(@env)).to be_truthy
|
28
28
|
end
|
29
29
|
|
30
30
|
it "returns true if env[:body] is a hash with mixed objects in its values" do
|
31
31
|
@env[:body] = {:a => [:c, 2], :b => {:e => :f}}
|
32
|
-
@multipart.process_request?(@env).
|
32
|
+
expect(@multipart.process_request?(@env)).to be_truthy
|
33
33
|
end
|
34
34
|
|
35
35
|
it "returns false if env[:body] is a string" do
|
36
36
|
@env[:body] = "my body"
|
37
|
-
@multipart.process_request?(@env).
|
37
|
+
expect(@multipart.process_request?(@env)).to be_falsey
|
38
38
|
end
|
39
39
|
|
40
40
|
it "returns false if env[:body] is a hash without an array or hash value" do
|
41
41
|
@env[:body] = {:a => 3}
|
42
|
-
@multipart.process_request?(@env).
|
43
|
-
end
|
42
|
+
expect(@multipart.process_request?(@env)).to be_falsey
|
43
|
+
end
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
describe "#process_params" do
|
47
47
|
before :each do
|
48
48
|
@parent = Faraday::Request::Multipart.new
|
49
|
-
@multipart = Koala::HTTPService::MultipartRequest.new
|
50
|
-
@block = lambda {|k, v| "#{k}=#{v}"}
|
49
|
+
@multipart = Koala::HTTPService::MultipartRequest.new
|
50
|
+
@block = lambda {|k, v| "#{k}=#{v}"}
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
it "is identical to the parent for requests without a prefix" do
|
54
54
|
hash = {:a => 2, :c => "3"}
|
55
|
-
@multipart.process_params(hash, &@block).
|
55
|
+
expect(@multipart.process_params(hash, &@block)).to eq(@parent.process_params(hash, &@block))
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
it "replaces encodes [ and ] if the request has a prefix" do
|
59
59
|
hash = {:a => 2, :c => "3"}
|
60
60
|
prefix = "foo"
|
61
61
|
# process_params returns an array
|
62
|
-
@multipart.process_params(hash, prefix, &@block).join("&").
|
62
|
+
expect(@multipart.process_params(hash, prefix, &@block).join("&")).to eq(@parent.process_params(hash, prefix, &@block).join("&").gsub(/\[/, "%5B").gsub(/\]/, "%5D"))
|
63
63
|
end
|
64
64
|
end
|
65
|
-
|
66
65
|
end
|
data/spec/cases/oauth_spec.rb
CHANGED
@@ -28,19 +28,19 @@ describe "Koala::Facebook::OAuth" do
|
|
28
28
|
|
29
29
|
before :each do
|
30
30
|
@time = Time.now
|
31
|
-
Time.
|
32
|
-
@time.
|
31
|
+
allow(Time).to receive(:now).and_return(@time)
|
32
|
+
allow(@time).to receive(:to_i).and_return(1273363199)
|
33
33
|
end
|
34
34
|
|
35
35
|
describe ".new" do
|
36
36
|
it "properly initializes" do
|
37
|
-
@oauth.
|
37
|
+
expect(@oauth).to be_truthy
|
38
38
|
end
|
39
39
|
|
40
40
|
it "properly sets attributes" do
|
41
|
-
(@oauth.app_id == @app_id &&
|
41
|
+
expect(@oauth.app_id == @app_id &&
|
42
42
|
@oauth.app_secret == @secret &&
|
43
|
-
@oauth.oauth_callback_url == @callback_url).
|
43
|
+
@oauth.oauth_callback_url == @callback_url).to be_truthy
|
44
44
|
end
|
45
45
|
|
46
46
|
it "properly initializes without a callback_url" do
|
@@ -49,9 +49,9 @@ describe "Koala::Facebook::OAuth" do
|
|
49
49
|
|
50
50
|
it "properly sets attributes without a callback URL" do
|
51
51
|
@oauth = Koala::Facebook::OAuth.new(@app_id, @secret)
|
52
|
-
(@oauth.app_id == @app_id &&
|
52
|
+
expect(@oauth.app_id == @app_id &&
|
53
53
|
@oauth.app_secret == @secret &&
|
54
|
-
@oauth.oauth_callback_url == nil).
|
54
|
+
@oauth.oauth_callback_url == nil).to be_truthy
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -62,71 +62,71 @@ describe "Koala::Facebook::OAuth" do
|
|
62
62
|
# we don't actually want to make requests to Facebook to redeem the code
|
63
63
|
@cookie = KoalaTest.oauth_test_data["valid_signed_cookies"]
|
64
64
|
@token = "my token"
|
65
|
-
@oauth.
|
65
|
+
allow(@oauth).to receive(:get_access_token_info).and_return("access_token" => @token)
|
66
66
|
end
|
67
67
|
|
68
68
|
it "parses valid cookies" do
|
69
69
|
result = @oauth.get_user_info_from_cookies(@cookie)
|
70
|
-
result.
|
70
|
+
expect(result).to be_a(Hash)
|
71
71
|
end
|
72
72
|
|
73
73
|
it "returns all the components in the signed request" do
|
74
74
|
result = @oauth.get_user_info_from_cookies(@cookie)
|
75
75
|
@oauth.parse_signed_request(@cookie.values.first).each_pair do |k, v|
|
76
|
-
result[k].
|
76
|
+
expect(result[k]).to eq(v)
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
80
|
it "makes a request to Facebook to redeem the code if present" do
|
81
81
|
code = "foo"
|
82
|
-
@oauth.
|
83
|
-
@oauth.
|
82
|
+
allow(@oauth).to receive(:parse_signed_request).and_return({"code" => code})
|
83
|
+
expect(@oauth).to receive(:get_access_token_info).with(code, anything)
|
84
84
|
@oauth.get_user_info_from_cookies(@cookie)
|
85
85
|
end
|
86
86
|
|
87
87
|
it "sets the code redemption redirect_uri to ''" do
|
88
|
-
@oauth.
|
88
|
+
expect(@oauth).to receive(:get_access_token_info).with(anything, :redirect_uri => '')
|
89
89
|
@oauth.get_user_info_from_cookies(@cookie)
|
90
90
|
end
|
91
91
|
|
92
92
|
context "if the code is missing" do
|
93
93
|
it "doesn't make a request to Facebook" do
|
94
|
-
@oauth.
|
95
|
-
@oauth.
|
94
|
+
allow(@oauth).to receive(:parse_signed_request).and_return({})
|
95
|
+
expect(@oauth).to receive(:get_access_token_info).never
|
96
96
|
@oauth.get_user_info_from_cookies(@cookie)
|
97
97
|
end
|
98
98
|
|
99
99
|
it "returns nil" do
|
100
|
-
@oauth.
|
101
|
-
@oauth.get_user_info_from_cookies(@cookie).
|
100
|
+
allow(@oauth).to receive(:parse_signed_request).and_return({})
|
101
|
+
expect(@oauth.get_user_info_from_cookies(@cookie)).to be_nil
|
102
102
|
end
|
103
103
|
|
104
104
|
it "logs a warning" do
|
105
|
-
@oauth.
|
106
|
-
Koala::Utils.logger.
|
105
|
+
allow(@oauth).to receive(:parse_signed_request).and_return({})
|
106
|
+
expect(Koala::Utils.logger).to receive(:warn)
|
107
107
|
@oauth.get_user_info_from_cookies(@cookie)
|
108
108
|
end
|
109
109
|
end
|
110
110
|
|
111
111
|
context "if the code is present" do
|
112
112
|
it "adds the access_token into the hash" do
|
113
|
-
@oauth.get_user_info_from_cookies(@cookie)["access_token"].
|
113
|
+
expect(@oauth.get_user_info_from_cookies(@cookie)["access_token"]).to eq(@token)
|
114
114
|
end
|
115
115
|
|
116
116
|
it "returns nil if the call to FB returns no data" do
|
117
|
-
@oauth.
|
118
|
-
@oauth.get_user_info_from_cookies(@cookie).
|
117
|
+
allow(@oauth).to receive(:get_access_token_info).and_return(nil)
|
118
|
+
expect(@oauth.get_user_info_from_cookies(@cookie)).to be_nil
|
119
119
|
end
|
120
120
|
|
121
121
|
it "returns nil if the call to FB returns an expired code error" do
|
122
|
-
@oauth.
|
122
|
+
allow(@oauth).to receive(:get_access_token_info).and_raise(Koala::Facebook::OAuthTokenRequestError.new(400,
|
123
123
|
'{ "error": { "type": "OAuthException", "message": "Code was invalid or expired. Session has expired at unix time 1324044000. The current unix time is 1324300957." } }'
|
124
124
|
))
|
125
|
-
@oauth.get_user_info_from_cookies(@cookie).
|
125
|
+
expect(@oauth.get_user_info_from_cookies(@cookie)).to be_nil
|
126
126
|
end
|
127
127
|
|
128
128
|
it "raises the error if the call to FB returns a different error" do
|
129
|
-
@oauth.
|
129
|
+
allow(@oauth).to receive(:get_access_token_info).and_raise(Koala::Facebook::OAuthTokenRequestError.new(400,
|
130
130
|
'{ "error": { "type": "OtherError", "message": "A Facebook Error" } }'))
|
131
131
|
expect { @oauth.get_user_info_from_cookies(@cookie) }.to raise_exception(Koala::Facebook::OAuthTokenRequestError)
|
132
132
|
end
|
@@ -136,46 +136,46 @@ describe "Koala::Facebook::OAuth" do
|
|
136
136
|
# make an invalid string by replacing some values
|
137
137
|
bad_cookie_hash = @cookie.inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
|
138
138
|
result = @oauth.get_user_info_from_cookies(bad_cookie_hash)
|
139
|
-
result.
|
139
|
+
expect(result).to be_nil
|
140
140
|
end
|
141
141
|
end
|
142
142
|
|
143
143
|
context "for unsigned cookies" do
|
144
144
|
it "properly parses valid cookies" do
|
145
145
|
result = @oauth.get_user_info_from_cookies(KoalaTest.oauth_test_data["valid_cookies"])
|
146
|
-
result.
|
146
|
+
expect(result).to be_a(Hash)
|
147
147
|
end
|
148
148
|
|
149
149
|
it "returns all the cookie components from valid cookie string" do
|
150
150
|
cookie_data = KoalaTest.oauth_test_data["valid_cookies"]
|
151
151
|
parsing_results = @oauth.get_user_info_from_cookies(cookie_data)
|
152
152
|
number_of_components = cookie_data["fbs_#{@app_id.to_s}"].scan(/\=/).length
|
153
|
-
parsing_results.length.
|
153
|
+
expect(parsing_results.length).to eq(number_of_components)
|
154
154
|
end
|
155
155
|
|
156
156
|
it "properly parses valid offline access cookies (e.g. no expiration)" do
|
157
157
|
result = @oauth.get_user_info_from_cookies(KoalaTest.oauth_test_data["offline_access_cookies"])
|
158
|
-
result["uid"].
|
158
|
+
expect(result["uid"]).to be_truthy
|
159
159
|
end
|
160
160
|
|
161
161
|
it "returns all the cookie components from offline access cookies" do
|
162
162
|
cookie_data = KoalaTest.oauth_test_data["offline_access_cookies"]
|
163
163
|
parsing_results = @oauth.get_user_info_from_cookies(cookie_data)
|
164
164
|
number_of_components = cookie_data["fbs_#{@app_id.to_s}"].scan(/\=/).length
|
165
|
-
parsing_results.length.
|
165
|
+
expect(parsing_results.length).to eq(number_of_components)
|
166
166
|
end
|
167
167
|
|
168
168
|
it "doesn't parse expired cookies" do
|
169
169
|
new_time = @time.to_i * 2
|
170
|
-
@time.
|
171
|
-
@oauth.get_user_info_from_cookies(KoalaTest.oauth_test_data["valid_cookies"]).
|
170
|
+
allow(@time).to receive(:to_i).and_return(new_time)
|
171
|
+
expect(@oauth.get_user_info_from_cookies(KoalaTest.oauth_test_data["valid_cookies"])).to be_nil
|
172
172
|
end
|
173
173
|
|
174
174
|
it "doesn't parse invalid cookies" do
|
175
175
|
# make an invalid string by replacing some values
|
176
176
|
bad_cookie_hash = KoalaTest.oauth_test_data["valid_cookies"].inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
|
177
177
|
result = @oauth.get_user_info_from_cookies(bad_cookie_hash)
|
178
|
-
result.
|
178
|
+
expect(result).to be_nil
|
179
179
|
end
|
180
180
|
end
|
181
181
|
end
|
@@ -185,28 +185,28 @@ describe "Koala::Facebook::OAuth" do
|
|
185
185
|
before :each do
|
186
186
|
# we don't actually want to make requests to Facebook to redeem the code
|
187
187
|
@cookie = KoalaTest.oauth_test_data["valid_signed_cookies"]
|
188
|
-
@oauth.
|
188
|
+
allow(@oauth).to receive(:get_access_token_info).and_return("access_token" => "my token")
|
189
189
|
end
|
190
190
|
|
191
191
|
it "does not uses get_user_info_from_cookies to parse the cookies" do
|
192
|
-
@oauth.
|
192
|
+
expect(@oauth).not_to receive(:get_user_info_from_cookies).with(@cookie)
|
193
193
|
@oauth.get_user_from_cookies(@cookie)
|
194
194
|
end
|
195
195
|
|
196
196
|
it "uses return the facebook user id string if the cookies are valid" do
|
197
197
|
result = @oauth.get_user_from_cookies(@cookie)
|
198
|
-
result.
|
198
|
+
expect(result).to eq("2905623") # the user who generated the original test cookie
|
199
199
|
end
|
200
200
|
|
201
201
|
it "returns nil if the cookies are invalid" do
|
202
202
|
# make an invalid string by replacing some values
|
203
203
|
bad_cookie_hash = @cookie.inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
|
204
204
|
result = @oauth.get_user_from_cookies(bad_cookie_hash)
|
205
|
-
result.
|
205
|
+
expect(result).to be_nil
|
206
206
|
end
|
207
207
|
|
208
208
|
it "is deprecated" do
|
209
|
-
Koala::Utils.
|
209
|
+
expect(Koala::Utils).to receive(:deprecate)
|
210
210
|
@oauth.get_user_from_cookies({})
|
211
211
|
end
|
212
212
|
end
|
@@ -218,20 +218,20 @@ describe "Koala::Facebook::OAuth" do
|
|
218
218
|
end
|
219
219
|
|
220
220
|
it "uses get_user_info_from_cookies to parse the cookies" do
|
221
|
-
@oauth.
|
221
|
+
expect(@oauth).to receive(:get_user_info_from_cookies).with(@cookie).and_return({})
|
222
222
|
@oauth.get_user_from_cookies(@cookie)
|
223
223
|
end
|
224
224
|
|
225
225
|
it "uses return a string if the cookies are valid" do
|
226
226
|
result = @oauth.get_user_from_cookies(@cookie)
|
227
|
-
result.
|
227
|
+
expect(result).to eq("2905623") # the user who generated the original test cookie
|
228
228
|
end
|
229
229
|
|
230
230
|
it "returns nil if the cookies are invalid" do
|
231
231
|
# make an invalid string by replacing some values
|
232
232
|
bad_cookie_hash = @cookie.inject({}) { |hash, value| hash[value[0]] = value[1].gsub(/[0-9]/, "3") }
|
233
233
|
result = @oauth.get_user_from_cookies(bad_cookie_hash)
|
234
|
-
result.
|
234
|
+
expect(result).to be_nil
|
235
235
|
end
|
236
236
|
end
|
237
237
|
end
|
@@ -241,42 +241,42 @@ describe "Koala::Facebook::OAuth" do
|
|
241
241
|
describe "#url_for_oauth_code" do
|
242
242
|
it "generates a properly formatted OAuth code URL with the default values" do
|
243
243
|
url = @oauth.url_for_oauth_code
|
244
|
-
url.
|
244
|
+
expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&redirect_uri=#{CGI.escape @callback_url}")
|
245
245
|
end
|
246
246
|
|
247
247
|
it "generates a properly formatted OAuth code URL when a callback is given" do
|
248
248
|
callback = "foo.com"
|
249
249
|
url = @oauth.url_for_oauth_code(:callback => callback)
|
250
|
-
url.
|
250
|
+
expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&redirect_uri=#{callback}")
|
251
251
|
end
|
252
252
|
|
253
253
|
it "generates a properly formatted OAuth code URL when permissions are requested as a string" do
|
254
254
|
permissions = "publish_stream,read_stream"
|
255
255
|
url = @oauth.url_for_oauth_code(:permissions => permissions)
|
256
|
-
url.
|
256
|
+
expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&scope=#{CGI.escape permissions}&redirect_uri=#{CGI.escape @callback_url}")
|
257
257
|
end
|
258
258
|
|
259
259
|
it "generates a properly formatted OAuth code URL when permissions are requested as a string" do
|
260
260
|
permissions = ["publish_stream", "read_stream"]
|
261
261
|
url = @oauth.url_for_oauth_code(:permissions => permissions)
|
262
|
-
url.
|
262
|
+
expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&scope=#{CGI.escape permissions.join(",")}&redirect_uri=#{CGI.escape @callback_url}")
|
263
263
|
end
|
264
264
|
|
265
265
|
it "generates a properly formatted OAuth code URL when both permissions and callback are provided" do
|
266
266
|
permissions = "publish_stream,read_stream"
|
267
267
|
callback = "foo.com"
|
268
268
|
url = @oauth.url_for_oauth_code(:callback => callback, :permissions => permissions)
|
269
|
-
url.
|
269
|
+
expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&scope=#{CGI.escape permissions}&redirect_uri=#{CGI.escape callback}")
|
270
270
|
end
|
271
271
|
|
272
272
|
it "generates a properly formatted OAuth code URL when a display is given as a string" do
|
273
273
|
url = @oauth.url_for_oauth_code(:display => "page")
|
274
|
-
url.
|
274
|
+
expect(url).to match_url("https://#{Koala.config.dialog_host}/dialog/oauth?client_id=#{@app_id}&display=page&redirect_uri=#{CGI.escape @callback_url}")
|
275
275
|
end
|
276
276
|
|
277
277
|
it "raises an exception if no callback is given in initialization or the call" do
|
278
278
|
oauth2 = Koala::Facebook::OAuth.new(@app_id, @secret)
|
279
|
-
|
279
|
+
expect { oauth2.url_for_oauth_code }.to raise_error(ArgumentError)
|
280
280
|
end
|
281
281
|
|
282
282
|
it "includes any additional options as URL parameters, appropriately escaped" do
|
@@ -286,7 +286,7 @@ describe "Koala::Facebook::OAuth" do
|
|
286
286
|
}
|
287
287
|
url = @oauth.url_for_oauth_code(params)
|
288
288
|
params.each_pair do |key, value|
|
289
|
-
url.
|
289
|
+
expect(url).to match(/[\&\?]#{key}=#{CGI.escape value}/)
|
290
290
|
end
|
291
291
|
end
|
292
292
|
end
|
@@ -299,13 +299,13 @@ describe "Koala::Facebook::OAuth" do
|
|
299
299
|
|
300
300
|
it "generates a properly formatted OAuth token URL when provided a code" do
|
301
301
|
url = @oauth.url_for_access_token(@code)
|
302
|
-
url.
|
302
|
+
expect(url).to match_url("https://#{Koala.config.graph_server}/oauth/access_token?client_id=#{@app_id}&code=#{@code}&client_secret=#{@secret}&redirect_uri=#{CGI.escape @callback_url}")
|
303
303
|
end
|
304
304
|
|
305
305
|
it "generates a properly formatted OAuth token URL when provided a callback" do
|
306
306
|
callback = "foo.com"
|
307
307
|
url = @oauth.url_for_access_token(@code, :callback => callback)
|
308
|
-
url.
|
308
|
+
expect(url).to match_url("https://#{Koala.config.graph_server}/oauth/access_token?client_id=#{@app_id}&code=#{@code}&client_secret=#{@secret}&redirect_uri=#{CGI.escape callback}")
|
309
309
|
end
|
310
310
|
|
311
311
|
it "includes any additional options as URL parameters, appropriately escaped" do
|
@@ -315,7 +315,7 @@ describe "Koala::Facebook::OAuth" do
|
|
315
315
|
}
|
316
316
|
url = @oauth.url_for_access_token(@code, params)
|
317
317
|
params.each_pair do |key, value|
|
318
|
-
url.
|
318
|
+
expect(url).to match(/[\&\?]#{key}=#{CGI.escape value}/)
|
319
319
|
end
|
320
320
|
end
|
321
321
|
end
|
@@ -323,7 +323,7 @@ describe "Koala::Facebook::OAuth" do
|
|
323
323
|
describe "#url_for_dialog" do
|
324
324
|
it "builds the base properly" do
|
325
325
|
dialog_type = "my_dialog_type"
|
326
|
-
@oauth.url_for_dialog(dialog_type).
|
326
|
+
expect(@oauth.url_for_dialog(dialog_type)).to match(/^http:\/\/#{Koala.config.dialog_host}\/dialog\/#{dialog_type}/)
|
327
327
|
end
|
328
328
|
|
329
329
|
it "adds the app_id/client_id to the url" do
|
@@ -331,7 +331,7 @@ describe "Koala::Facebook::OAuth" do
|
|
331
331
|
url = @oauth.url_for_dialog("foo", automatic_params)
|
332
332
|
automatic_params.each_pair do |key, value|
|
333
333
|
# we're slightly simplifying how encode_params works, but for strings/ints, it's okay
|
334
|
-
url.
|
334
|
+
expect(url).to match(/[\&\?]#{key}=#{CGI.escape value.to_s}/)
|
335
335
|
end
|
336
336
|
end
|
337
337
|
|
@@ -343,7 +343,7 @@ describe "Koala::Facebook::OAuth" do
|
|
343
343
|
url = @oauth.url_for_dialog("friends", params)
|
344
344
|
params.each_pair do |key, value|
|
345
345
|
# we're slightly simplifying how encode_params works, but strings/ints, it's okay
|
346
|
-
url.
|
346
|
+
expect(url).to match(/[\&\?]#{key}=#{CGI.escape value.to_s}/)
|
347
347
|
end
|
348
348
|
end
|
349
349
|
|
@@ -352,22 +352,22 @@ describe "Koala::Facebook::OAuth" do
|
|
352
352
|
# slightly brittle (e.g. if parameter order changes), but still useful
|
353
353
|
it "can generate a send dialog" do
|
354
354
|
url = @oauth.url_for_dialog("send", :name => "People Argue Just to Win", :link => "http://www.nytimes.com/2011/06/15/arts/people-argue-just-to-win-scholars-assert.html")
|
355
|
-
url.
|
355
|
+
expect(url).to match_url("http://www.facebook.com/dialog/send?app_id=#{@app_id}&client_id=#{@app_id}&link=http%3A%2F%2Fwww.nytimes.com%2F2011%2F06%2F15%2Farts%2Fpeople-argue-just-to-win-scholars-assert.html&name=People+Argue+Just+to+Win&redirect_uri=#{CGI.escape @callback_url}")
|
356
356
|
end
|
357
357
|
|
358
358
|
it "can generate a feed dialog" do
|
359
359
|
url = @oauth.url_for_dialog("feed", :name => "People Argue Just to Win", :link => "http://www.nytimes.com/2011/06/15/arts/people-argue-just-to-win-scholars-assert.html")
|
360
|
-
url.
|
360
|
+
expect(url).to match_url("http://www.facebook.com/dialog/feed?app_id=#{@app_id}&client_id=#{@app_id}&link=http%3A%2F%2Fwww.nytimes.com%2F2011%2F06%2F15%2Farts%2Fpeople-argue-just-to-win-scholars-assert.html&name=People+Argue+Just+to+Win&redirect_uri=#{CGI.escape @callback_url}")
|
361
361
|
end
|
362
362
|
|
363
363
|
it "can generate a oauth dialog" do
|
364
364
|
url = @oauth.url_for_dialog("oauth", :scope => "email", :response_type => "token")
|
365
|
-
url.
|
365
|
+
expect(url).to match_url("http://www.facebook.com/dialog/oauth?app_id=#{@app_id}&client_id=#{@app_id}&redirect_uri=#{CGI.escape @callback_url}&response_type=token&scope=email")
|
366
366
|
end
|
367
367
|
|
368
368
|
it "can generate a pay dialog" do
|
369
369
|
url = @oauth.url_for_dialog("pay", :order_id => "foo", :credits_purchase => false)
|
370
|
-
url.
|
370
|
+
expect(url).to match_url("http://www.facebook.com/dialog/pay?app_id=#{@app_id}&client_id=#{@app_id}&order_id=foo&credits_purchase=false&redirect_uri=#{CGI.escape @callback_url}")
|
371
371
|
end
|
372
372
|
end
|
373
373
|
end
|
@@ -377,35 +377,35 @@ describe "Koala::Facebook::OAuth" do
|
|
377
377
|
describe '#generate_client_code' do
|
378
378
|
if KoalaTest.mock_interface? || KoalaTest.oauth_token
|
379
379
|
it 'makes a request using the correct endpoint' do
|
380
|
-
Koala.
|
380
|
+
expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(200, '{"code": "fake_client_code"}', {}))
|
381
381
|
@oauth.generate_client_code(KoalaTest.oauth_token)
|
382
382
|
end
|
383
383
|
|
384
384
|
it 'gets a valid client code returned' do
|
385
|
-
Koala.
|
385
|
+
expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(200, '{"code": "fake_client_code"}', {}))
|
386
386
|
result = @oauth.generate_client_code(KoalaTest.oauth_token)
|
387
|
-
result.
|
388
|
-
result.
|
387
|
+
expect(result).to be_a(String)
|
388
|
+
expect(result).to eq('fake_client_code')
|
389
389
|
end
|
390
390
|
|
391
391
|
it 'raises a BadFacebookResponse error when empty response body is returned' do
|
392
|
-
Koala.
|
393
|
-
|
392
|
+
expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(200, '', {}))
|
393
|
+
expect { @oauth.generate_client_code(KoalaTest.oauth_token) }.to raise_error(Koala::Facebook::BadFacebookResponse)
|
394
394
|
end
|
395
395
|
|
396
396
|
it 'raises an OAuthTokenRequestError when empty response body is returned' do
|
397
|
-
Koala.
|
398
|
-
|
397
|
+
expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(400, '', {}))
|
398
|
+
expect { @oauth.generate_client_code(KoalaTest.oauth_token) }.to raise_error(Koala::Facebook::OAuthTokenRequestError)
|
399
399
|
end
|
400
400
|
|
401
401
|
it 'raises a ServerError when empty response body is returned' do
|
402
|
-
Koala.
|
403
|
-
|
402
|
+
expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(500, '', {}))
|
403
|
+
expect { @oauth.generate_client_code(KoalaTest.oauth_token) }.to raise_error(Koala::Facebook::ServerError)
|
404
404
|
end
|
405
405
|
|
406
406
|
it 'raises a KoalaError when empty response body is returned' do
|
407
|
-
Koala.
|
408
|
-
|
407
|
+
expect(Koala).to receive(:make_request).with('/oauth/client_code', anything, 'get', anything).and_return(Koala::HTTPService::Response.new(200, '{"client_code":"should_not_be_returned"}', {}))
|
408
|
+
expect { @oauth.generate_client_code(KoalaTest.oauth_token) }.to raise_error(Koala::KoalaError)
|
409
409
|
end
|
410
410
|
else
|
411
411
|
pending "Some OAuth token exchange tests will not be run since the access token field in facebook_data.yml is blank."
|
@@ -417,33 +417,33 @@ describe "Koala::Facebook::OAuth" do
|
|
417
417
|
describe "#get_access_token_info" do
|
418
418
|
it "uses options[:redirect_uri] if provided" do
|
419
419
|
uri = "foo"
|
420
|
-
Koala.
|
420
|
+
expect(Koala).to receive(:make_request).with(anything, hash_including(:redirect_uri => uri), anything, anything).and_return(Koala::HTTPService::Response.new(200, "", {}))
|
421
421
|
@oauth.get_access_token_info(@code, :redirect_uri => uri)
|
422
422
|
end
|
423
423
|
|
424
424
|
it "uses the redirect_uri used to create the @oauth if no :redirect_uri option is provided" do
|
425
|
-
Koala.
|
425
|
+
expect(Koala).to receive(:make_request).with(anything, hash_including(:redirect_uri => @callback_url), anything, anything).and_return(Koala::HTTPService::Response.new(200, "", {}))
|
426
426
|
@oauth.get_access_token_info(@code)
|
427
427
|
end
|
428
428
|
|
429
429
|
it "makes a GET request" do
|
430
|
-
Koala.
|
430
|
+
expect(Koala).to receive(:make_request).with(anything, anything, "get", anything).and_return(Koala::HTTPService::Response.new(200, "", {}))
|
431
431
|
@oauth.get_access_token_info(@code)
|
432
432
|
end
|
433
433
|
|
434
434
|
if KoalaTest.code
|
435
435
|
it "properly gets and parses an access token token results into a hash" do
|
436
436
|
result = @oauth.get_access_token_info(@code)
|
437
|
-
result.
|
437
|
+
expect(result).to be_a(Hash)
|
438
438
|
end
|
439
439
|
|
440
440
|
it "properly includes the access token results" do
|
441
441
|
result = @oauth.get_access_token_info(@code)
|
442
|
-
result["access_token"].
|
442
|
+
expect(result["access_token"]).to be_truthy
|
443
443
|
end
|
444
444
|
|
445
445
|
it "raises an error when get_access_token is called with a bad code" do
|
446
|
-
|
446
|
+
expect { @oauth.get_access_token_info("foo") }.to raise_error(Koala::Facebook::OAuthTokenRequestError)
|
447
447
|
end
|
448
448
|
end
|
449
449
|
end
|
@@ -452,24 +452,24 @@ describe "Koala::Facebook::OAuth" do
|
|
452
452
|
# TODO refactor these to be proper tests with stubs and tests against real data
|
453
453
|
it "passes on any options provided to make_request" do
|
454
454
|
options = {:a => 2}
|
455
|
-
Koala.
|
455
|
+
expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
|
456
456
|
@oauth.get_access_token(@code, options)
|
457
457
|
end
|
458
458
|
|
459
459
|
if KoalaTest.code
|
460
460
|
it "uses get_access_token_info to get and parse an access token token results" do
|
461
461
|
result = @oauth.get_access_token(@code)
|
462
|
-
result.
|
462
|
+
expect(result).to be_a(String)
|
463
463
|
end
|
464
464
|
|
465
465
|
it "returns the access token as a string" do
|
466
466
|
result = @oauth.get_access_token(@code)
|
467
467
|
original = @oauth.get_access_token_info(@code)
|
468
|
-
result.
|
468
|
+
expect(result).to eq(original["access_token"])
|
469
469
|
end
|
470
470
|
|
471
471
|
it "raises an error when get_access_token is called with a bad code" do
|
472
|
-
|
472
|
+
expect { @oauth.get_access_token("foo") }.to raise_error(Koala::Facebook::OAuthTokenRequestError)
|
473
473
|
end
|
474
474
|
end
|
475
475
|
end
|
@@ -481,17 +481,17 @@ describe "Koala::Facebook::OAuth" do
|
|
481
481
|
describe "get_app_access_token_info" do
|
482
482
|
it "properly gets and parses an app's access token as a hash" do
|
483
483
|
result = @oauth.get_app_access_token_info
|
484
|
-
result.
|
484
|
+
expect(result).to be_a(Hash)
|
485
485
|
end
|
486
486
|
|
487
487
|
it "includes the access token" do
|
488
488
|
result = @oauth.get_app_access_token_info
|
489
|
-
result["access_token"].
|
489
|
+
expect(result["access_token"]).to be_truthy
|
490
490
|
end
|
491
491
|
|
492
492
|
it "passes on any options provided to make_request" do
|
493
493
|
options = {:a => 2}
|
494
|
-
Koala.
|
494
|
+
expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
|
495
495
|
@oauth.get_app_access_token_info(options)
|
496
496
|
end
|
497
497
|
end
|
@@ -499,18 +499,18 @@ describe "Koala::Facebook::OAuth" do
|
|
499
499
|
describe "get_app_access_token" do
|
500
500
|
it "uses get_access_token_info to get and parse an access token token results" do
|
501
501
|
result = @oauth.get_app_access_token
|
502
|
-
result.
|
502
|
+
expect(result).to be_a(String)
|
503
503
|
end
|
504
504
|
|
505
505
|
it "returns the access token as a string" do
|
506
506
|
result = @oauth.get_app_access_token
|
507
507
|
original = @oauth.get_app_access_token_info
|
508
|
-
result.
|
508
|
+
expect(result).to eq(original["access_token"])
|
509
509
|
end
|
510
510
|
|
511
511
|
it "passes on any options provided to make_request" do
|
512
512
|
options = {:a => 2}
|
513
|
-
Koala.
|
513
|
+
expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
|
514
514
|
@oauth.get_app_access_token(options)
|
515
515
|
end
|
516
516
|
end
|
@@ -519,12 +519,12 @@ describe "Koala::Facebook::OAuth" do
|
|
519
519
|
if KoalaTest.mock_interface? || KoalaTest.oauth_token
|
520
520
|
it "properly gets and parses an app's access token as a hash" do
|
521
521
|
result = @oauth.exchange_access_token_info(KoalaTest.oauth_token)
|
522
|
-
result.
|
522
|
+
expect(result).to be_a(Hash)
|
523
523
|
end
|
524
524
|
|
525
525
|
it "includes the access token" do
|
526
526
|
result = @oauth.exchange_access_token_info(KoalaTest.oauth_token)
|
527
|
-
result["access_token"].
|
527
|
+
expect(result["access_token"]).not_to be_nil
|
528
528
|
end
|
529
529
|
else
|
530
530
|
pending "Some OAuth token exchange tests will not be run since the access token field in facebook_data.yml is blank."
|
@@ -532,25 +532,25 @@ describe "Koala::Facebook::OAuth" do
|
|
532
532
|
|
533
533
|
it "passes on any options provided to make_request" do
|
534
534
|
options = {:a => 2}
|
535
|
-
Koala.
|
535
|
+
expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
|
536
536
|
@oauth.exchange_access_token_info(KoalaTest.oauth_token, options)
|
537
537
|
end
|
538
538
|
|
539
539
|
it "raises an error when exchange_access_token_info is called with a bad code" do
|
540
|
-
|
540
|
+
expect { @oauth.exchange_access_token_info("foo") }.to raise_error(Koala::Facebook::OAuthTokenRequestError)
|
541
541
|
end
|
542
542
|
end
|
543
543
|
|
544
544
|
describe "exchange_access_token" do
|
545
545
|
it "uses get_access_token_info to get and parse an access token token results" do
|
546
546
|
hash = {"access_token" => Time.now.to_i * rand}
|
547
|
-
@oauth.
|
548
|
-
@oauth.exchange_access_token(KoalaTest.oauth_token).
|
547
|
+
allow(@oauth).to receive(:exchange_access_token_info).and_return(hash)
|
548
|
+
expect(@oauth.exchange_access_token(KoalaTest.oauth_token)).to eq(hash["access_token"])
|
549
549
|
end
|
550
550
|
|
551
551
|
it "passes on any options provided to make_request" do
|
552
552
|
options = {:a => 2}
|
553
|
-
Koala.
|
553
|
+
expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "", {}))
|
554
554
|
@oauth.exchange_access_token(KoalaTest.oauth_token, options)
|
555
555
|
end
|
556
556
|
end
|
@@ -563,13 +563,13 @@ describe "Koala::Facebook::OAuth" do
|
|
563
563
|
it "properly parses access token results" do
|
564
564
|
result = @oauth.send(:parse_access_token, @raw_token_string)
|
565
565
|
has_both_parts = result["access_token"] && result["expires"]
|
566
|
-
has_both_parts.
|
566
|
+
expect(has_both_parts).to be_truthy
|
567
567
|
end
|
568
568
|
|
569
569
|
it "properly parses offline access token results" do
|
570
570
|
result = @oauth.send(:parse_access_token, @raw_offline_access_token_string)
|
571
571
|
has_both_parts = result["access_token"] && !result["expires"]
|
572
|
-
has_both_parts.
|
572
|
+
expect(has_both_parts).to be true
|
573
573
|
end
|
574
574
|
|
575
575
|
# fetch_token_string
|
@@ -578,7 +578,7 @@ describe "Koala::Facebook::OAuth" do
|
|
578
578
|
if KoalaTest.code
|
579
579
|
it "fetches a proper token string from Facebook when given a code" do
|
580
580
|
result = @oauth.send(:fetch_token_string, :code => @code, :redirect_uri => @callback_url)
|
581
|
-
result.
|
581
|
+
expect(result).to match(/^access_token/)
|
582
582
|
end
|
583
583
|
else
|
584
584
|
it "fetch_token_string code test will not be run since the code field in facebook_data.yml is blank."
|
@@ -586,7 +586,7 @@ describe "Koala::Facebook::OAuth" do
|
|
586
586
|
|
587
587
|
it "fetches a proper token string from Facebook when asked for the app token" do
|
588
588
|
result = @oauth.send(:fetch_token_string, {:grant_type => 'client_credentials'}, true)
|
589
|
-
result.
|
589
|
+
expect(result).to match(/^access_token/)
|
590
590
|
end
|
591
591
|
end
|
592
592
|
end
|
@@ -596,41 +596,41 @@ describe "Koala::Facebook::OAuth" do
|
|
596
596
|
describe "with get_token_info_from_session_keys" do
|
597
597
|
it "gets an array of session keys from Facebook when passed a single key" do
|
598
598
|
result = @oauth.get_tokens_from_session_keys([KoalaTest.session_key])
|
599
|
-
result.
|
600
|
-
result.length.
|
599
|
+
expect(result).to be_an(Array)
|
600
|
+
expect(result.length).to eq(1)
|
601
601
|
end
|
602
602
|
|
603
603
|
it "gets an array of session keys from Facebook when passed multiple keys" do
|
604
604
|
result = @oauth.get_tokens_from_session_keys(@multiple_session_keys)
|
605
|
-
result.
|
606
|
-
result.length.
|
605
|
+
expect(result).to be_an(Array)
|
606
|
+
expect(result.length).to eq(2)
|
607
607
|
end
|
608
608
|
|
609
609
|
it "returns the original hashes" do
|
610
610
|
result = @oauth.get_token_info_from_session_keys(@multiple_session_keys)
|
611
|
-
result[0].
|
611
|
+
expect(result[0]).to be_a(Hash)
|
612
612
|
end
|
613
613
|
|
614
614
|
it "properly handles invalid session keys" do
|
615
615
|
result = @oauth.get_token_info_from_session_keys(["foo", "bar"])
|
616
616
|
#it should return nil for each of the invalid ones
|
617
|
-
result.each {|r| r.
|
617
|
+
result.each {|r| expect(r).to be_nil}
|
618
618
|
end
|
619
619
|
|
620
620
|
it "properly handles a mix of valid and invalid session keys" do
|
621
621
|
result = @oauth.get_token_info_from_session_keys(["foo"].concat(@multiple_session_keys))
|
622
622
|
# it should return nil for each of the invalid ones
|
623
|
-
result.each_with_index {|r, index| index > 0 ? r.
|
623
|
+
result.each_with_index {|r, index| index > 0 ? expect(r).to(be_a(Hash)) : expect(r).to(be_nil)}
|
624
624
|
end
|
625
625
|
|
626
626
|
it "throws a BadFacebookResponse if Facebook returns an empty body (as happens for instance when the API breaks)" do
|
627
|
-
@oauth.
|
628
|
-
|
627
|
+
expect(@oauth).to receive(:fetch_token_string).and_return("")
|
628
|
+
expect { @oauth.get_token_info_from_session_keys(@multiple_session_keys) }.to raise_error(Koala::Facebook::BadFacebookResponse)
|
629
629
|
end
|
630
630
|
|
631
631
|
it "passes on any options provided to make_request" do
|
632
632
|
options = {:a => 2}
|
633
|
-
Koala.
|
633
|
+
expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "[{}]", {}))
|
634
634
|
@oauth.get_token_info_from_session_keys([], options)
|
635
635
|
end
|
636
636
|
end
|
@@ -638,31 +638,31 @@ describe "Koala::Facebook::OAuth" do
|
|
638
638
|
describe "with get_tokens_from_session_keys" do
|
639
639
|
it "calls get_token_info_from_session_keys" do
|
640
640
|
args = @multiple_session_keys
|
641
|
-
@oauth.
|
641
|
+
expect(@oauth).to receive(:get_token_info_from_session_keys).with(args, anything).and_return([])
|
642
642
|
@oauth.get_tokens_from_session_keys(args)
|
643
643
|
end
|
644
644
|
|
645
645
|
it "returns an array of strings" do
|
646
646
|
args = @multiple_session_keys
|
647
647
|
result = @oauth.get_tokens_from_session_keys(args)
|
648
|
-
result.each {|r| r.
|
648
|
+
result.each {|r| expect(r).to be_a(String) }
|
649
649
|
end
|
650
650
|
|
651
651
|
it "properly handles invalid session keys" do
|
652
652
|
result = @oauth.get_tokens_from_session_keys(["foo", "bar"])
|
653
653
|
# it should return nil for each of the invalid ones
|
654
|
-
result.each {|r| r.
|
654
|
+
result.each {|r| expect(r).to be_nil}
|
655
655
|
end
|
656
656
|
|
657
657
|
it "properly handles a mix of valid and invalid session keys" do
|
658
658
|
result = @oauth.get_tokens_from_session_keys(["foo"].concat(@multiple_session_keys))
|
659
659
|
# it should return nil for each of the invalid ones
|
660
|
-
result.each_with_index {|r, index| index > 0 ? r.
|
660
|
+
result.each_with_index {|r, index| index > 0 ? expect(r).to(be_a(String)) : expect(r).to(be_nil)}
|
661
661
|
end
|
662
662
|
|
663
663
|
it "passes on any options provided to make_request" do
|
664
664
|
options = {:a => 2}
|
665
|
-
Koala.
|
665
|
+
expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "[{}]", {}))
|
666
666
|
@oauth.get_tokens_from_session_keys([], options)
|
667
667
|
end
|
668
668
|
end
|
@@ -670,29 +670,29 @@ describe "Koala::Facebook::OAuth" do
|
|
670
670
|
describe "get_token_from_session_key" do
|
671
671
|
it "calls get_tokens_from_session_keys when the get_token_from_session_key is called" do
|
672
672
|
key = KoalaTest.session_key
|
673
|
-
@oauth.
|
673
|
+
expect(@oauth).to receive(:get_tokens_from_session_keys).with([key], anything).and_return([])
|
674
674
|
@oauth.get_token_from_session_key(key)
|
675
675
|
end
|
676
676
|
|
677
677
|
it "gets back the access token string from get_token_from_session_key" do
|
678
678
|
result = @oauth.get_token_from_session_key(KoalaTest.session_key)
|
679
|
-
result.
|
679
|
+
expect(result).to be_a(String)
|
680
680
|
end
|
681
681
|
|
682
682
|
it "returns the first value in the array" do
|
683
683
|
result = @oauth.get_token_from_session_key(KoalaTest.session_key)
|
684
684
|
array = @oauth.get_tokens_from_session_keys([KoalaTest.session_key])
|
685
|
-
result.
|
685
|
+
expect(result).to eq(array[0])
|
686
686
|
end
|
687
687
|
|
688
688
|
it "properly handles an invalid session key" do
|
689
689
|
result = @oauth.get_token_from_session_key("foo")
|
690
|
-
result.
|
690
|
+
expect(result).to be_nil
|
691
691
|
end
|
692
692
|
|
693
693
|
it "passes on any options provided to make_request" do
|
694
694
|
options = {:a => 2}
|
695
|
-
Koala.
|
695
|
+
expect(Koala).to receive(:make_request).with(anything, anything, anything, hash_including(options)).and_return(Koala::HTTPService::Response.new(200, "[{}]", {}))
|
696
696
|
@oauth.get_token_from_session_key("", options)
|
697
697
|
end
|
698
698
|
end
|
@@ -705,24 +705,24 @@ describe "Koala::Facebook::OAuth" do
|
|
705
705
|
# the signed request code is ported directly from Facebook
|
706
706
|
# so we only need to test at a high level that it works
|
707
707
|
it "throws an error if the algorithm is unsupported" do
|
708
|
-
MultiJson.
|
709
|
-
|
708
|
+
allow(MultiJson).to receive(:load).and_return("algorithm" => "my fun algorithm")
|
709
|
+
expect { @oauth.parse_signed_request(@signed_request) }.to raise_error
|
710
710
|
end
|
711
711
|
|
712
712
|
it "throws an error if the signature is invalid" do
|
713
|
-
OpenSSL::HMAC.
|
714
|
-
|
713
|
+
allow(OpenSSL::HMAC).to receive(:hexdigest).and_return("i'm an invalid signature")
|
714
|
+
expect { @oauth.parse_signed_request(@signed_request) }.to raise_error
|
715
715
|
end
|
716
716
|
|
717
717
|
it "throws an error if the signature string is empty" do
|
718
718
|
# this occasionally happens due to Facebook error
|
719
|
-
|
720
|
-
|
719
|
+
expect { @oauth.parse_signed_request("") }.to raise_error
|
720
|
+
expect { @oauth.parse_signed_request("abc-def") }.to raise_error
|
721
721
|
end
|
722
722
|
|
723
723
|
it "properly parses requests" do
|
724
724
|
@oauth = Koala::Facebook::OAuth.new(@app_id, @secret || @app_secret)
|
725
|
-
@oauth.parse_signed_request(@signed_params).
|
725
|
+
expect(@oauth.parse_signed_request(@signed_params)).to eq(@signed_params_result)
|
726
726
|
end
|
727
727
|
end
|
728
728
|
|