reagent-fleakr 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +115 -20
- data/Rakefile +1 -1
- data/lib/fleakr.rb +66 -7
- data/lib/fleakr/api.rb +7 -0
- data/lib/fleakr/api/file_parameter.rb +47 -0
- data/lib/fleakr/api/method_request.rb +57 -0
- data/lib/fleakr/api/parameter.rb +35 -0
- data/lib/fleakr/api/parameter_list.rb +96 -0
- data/lib/fleakr/api/response.rb +2 -2
- data/lib/fleakr/api/upload_request.rb +64 -0
- data/lib/fleakr/api/value_parameter.rb +36 -0
- data/lib/fleakr/core_ext.rb +1 -0
- data/lib/fleakr/core_ext/hash.rb +22 -0
- data/lib/fleakr/objects.rb +9 -0
- data/lib/fleakr/objects/authentication_token.rb +43 -0
- data/lib/fleakr/objects/contact.rb +5 -5
- data/lib/fleakr/objects/error.rb +2 -2
- data/lib/fleakr/objects/group.rb +2 -2
- data/lib/fleakr/objects/image.rb +7 -7
- data/lib/fleakr/objects/photo.rb +69 -5
- data/lib/fleakr/objects/search.rb +3 -6
- data/lib/fleakr/objects/set.rb +11 -5
- data/lib/fleakr/objects/user.rb +14 -26
- data/lib/fleakr/support.rb +2 -0
- data/lib/fleakr/support/attribute.rb +30 -12
- data/lib/fleakr/support/object.rb +20 -4
- data/lib/fleakr/version.rb +1 -1
- data/test/fixtures/auth.checkToken.xml +8 -0
- data/test/fixtures/auth.getFullToken.xml +8 -0
- data/test/fixtures/people.getInfo.xml +1 -1
- data/test/fixtures/photos.getInfo.xml +20 -0
- data/test/test_helper.rb +18 -3
- data/test/unit/fleakr/api/file_parameter_test.rb +63 -0
- data/test/unit/fleakr/api/method_request_test.rb +103 -0
- data/test/unit/fleakr/api/parameter_list_test.rb +161 -0
- data/test/unit/fleakr/api/parameter_test.rb +34 -0
- data/test/unit/fleakr/api/upload_request_test.rb +133 -0
- data/test/unit/fleakr/api/value_parameter_test.rb +41 -0
- data/test/unit/fleakr/core_ext/hash_test.rb +32 -0
- data/test/unit/fleakr/objects/authentication_token_test.rb +47 -0
- data/test/unit/fleakr/objects/image_test.rb +10 -5
- data/test/unit/fleakr/objects/photo_test.rb +96 -36
- data/test/unit/fleakr/objects/search_test.rb +1 -1
- data/test/unit/fleakr/objects/set_test.rb +12 -1
- data/test/unit/fleakr/objects/user_test.rb +2 -16
- data/test/unit/fleakr/support/attribute_test.rb +82 -24
- data/test/unit/fleakr/support/object_test.rb +26 -3
- data/test/unit/fleakr_test.rb +65 -6
- metadata +27 -4
- data/lib/fleakr/api/request.rb +0 -58
- data/test/unit/fleakr/api/request_test.rb +0 -93
@@ -0,0 +1,161 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../../test_helper'
|
2
|
+
|
3
|
+
module Fleakr::Api
|
4
|
+
class ParameterListTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
describe "An instance of the ParameterList class" do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@api_key = 'key'
|
10
|
+
@secret = 'foobar'
|
11
|
+
|
12
|
+
Fleakr.stubs(:shared_secret).with().returns(@secret)
|
13
|
+
Fleakr.stubs(:api_key).with().returns(@api_key)
|
14
|
+
@parameter_list = ParameterList.new
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should contain the :api_key by default" do
|
18
|
+
@parameter_list[:api_key].name.should == 'api_key'
|
19
|
+
@parameter_list[:api_key].value.should == @api_key
|
20
|
+
@parameter_list[:api_key].include_in_signature?.should be(true)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be able to create an initial list of parameters" do
|
24
|
+
parameter_list = ParameterList.new(:one => 'two')
|
25
|
+
parameter_list[:one].value.should == 'two'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be able to add parameters to its list" do
|
29
|
+
parameter = ValueParameter.new('foo', 'bar')
|
30
|
+
|
31
|
+
@parameter_list << parameter
|
32
|
+
@parameter_list['foo'].should == parameter
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should allow access to parameters by symbol" do
|
36
|
+
parameter = ValueParameter.new('foo', 'bar')
|
37
|
+
@parameter_list << parameter
|
38
|
+
|
39
|
+
@parameter_list[:foo].should == parameter
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should overwrite existing values when a duplicate is added" do
|
43
|
+
length = @parameter_list.instance_variable_get(:@list).length
|
44
|
+
2.times {@parameter_list << ValueParameter.new('foo', 'bar') }
|
45
|
+
|
46
|
+
@parameter_list.instance_variable_get(:@list).length.should == length + 1
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should be able to calculate the signature of the parameters" do
|
50
|
+
@parameter_list << ValueParameter.new('foo', 'bar')
|
51
|
+
@parameter_list.signature.should == Digest::MD5.hexdigest("#{@secret}api_key#{@api_key}foobar")
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should use the correct order when signing a list of multiple parameters" do
|
55
|
+
@parameter_list << ValueParameter.new('z', 'a')
|
56
|
+
@parameter_list << ValueParameter.new('a', 'z')
|
57
|
+
|
58
|
+
@parameter_list.signature.should == Digest::MD5.hexdigest("#{@secret}azapi_key#{@api_key}za")
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should ignore the parameters that aren't included in the signature" do
|
62
|
+
@parameter_list << ValueParameter.new('foo', 'bar')
|
63
|
+
@parameter_list << ValueParameter.new('yes', 'no', false)
|
64
|
+
|
65
|
+
@parameter_list.signature.should == Digest::MD5.hexdigest("#{@secret}api_key#{@api_key}foobar")
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should be able to generate a boundary for post data" do
|
69
|
+
rand = '0.123'
|
70
|
+
|
71
|
+
@parameter_list.stubs(:rand).with().returns(stub(:to_s => rand))
|
72
|
+
@parameter_list.boundary.should == Digest::MD5.hexdigest(rand)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should know that it doesn't need to sign the request by default" do
|
76
|
+
@parameter_list.sign?.should be(false)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should know that it needs to sign the request when asked" do
|
80
|
+
parameter_list = ParameterList.new(:sign? => true)
|
81
|
+
parameter_list.sign?.should be(true)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should know that it doesn't need to authenticate the request by default" do
|
85
|
+
@parameter_list.authenticate?.should be(false)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should know to authenticate the request when asked" do
|
89
|
+
Fleakr.expects(:token).with().returns(stub(:value => 'toke'))
|
90
|
+
|
91
|
+
parameter_list = ParameterList.new(:authenticate? => true)
|
92
|
+
parameter_list.authenticate?.should be(true)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should contain the :auth_token parameter in the list if the request is to be authenticated" do
|
96
|
+
Fleakr.expects(:token).with().returns(stub(:value => 'toke'))
|
97
|
+
|
98
|
+
parameter_list = ParameterList.new(:authenticate? => true)
|
99
|
+
auth_param = parameter_list[:auth_token]
|
100
|
+
|
101
|
+
auth_param.name.should == 'auth_token'
|
102
|
+
auth_param.value.should == 'toke'
|
103
|
+
auth_param.include_in_signature?.should be(true)
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should know that it needs to sign the request when it is to be authenticated" do
|
107
|
+
Fleakr.expects(:token).with().returns(stub(:value => 'toke'))
|
108
|
+
|
109
|
+
parameter_list = ParameterList.new(:authenticate? => true)
|
110
|
+
parameter_list.sign?.should be(true)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should include the signature in the list of parameters if the request is to be signed" do
|
114
|
+
parameter_list = ParameterList.new(:sign? => true)
|
115
|
+
parameter_list.stubs(:signature).with().returns('sig')
|
116
|
+
|
117
|
+
signature_param = parameter_list[:api_sig]
|
118
|
+
|
119
|
+
signature_param.name.should == 'api_sig'
|
120
|
+
signature_param.value.should == 'sig'
|
121
|
+
signature_param.include_in_signature?.should be(false)
|
122
|
+
end
|
123
|
+
|
124
|
+
context "with associated parameters" do
|
125
|
+
|
126
|
+
before do
|
127
|
+
@p1 = ValueParameter.new('a', 'b')
|
128
|
+
@p2 = ValueParameter.new('c', 'd')
|
129
|
+
|
130
|
+
@p1.stubs(:to_query).with().returns('q1')
|
131
|
+
@p1.stubs(:to_form).with().returns('f1')
|
132
|
+
|
133
|
+
@p2.stubs(:to_query).with().returns('q2')
|
134
|
+
@p2.stubs(:to_form).with().returns('f2')
|
135
|
+
|
136
|
+
@parameter_list.stubs(:list).with().returns('a' => @p1, 'c' => @p2)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should be able to generate a query representation of itself" do
|
140
|
+
@parameter_list.to_query.should == 'q1&q2'
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should be able to represent a form representation of itself" do
|
144
|
+
@parameter_list.stubs(:boundary).returns('bound')
|
145
|
+
|
146
|
+
expected =
|
147
|
+
"--bound\r\n" +
|
148
|
+
"f1" +
|
149
|
+
"--bound\r\n" +
|
150
|
+
"f2" +
|
151
|
+
"--bound--"
|
152
|
+
|
153
|
+
@parameter_list.to_form.should == expected
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
161
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../../test_helper'
|
2
|
+
|
3
|
+
module Fleakr::Api
|
4
|
+
class ParameterTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
describe "An instance of the Parameter class" do
|
7
|
+
|
8
|
+
it "should have a name" do
|
9
|
+
parameter = Parameter.new('foo')
|
10
|
+
parameter.name.should == 'foo'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should know to include it in the signature by default" do
|
14
|
+
parameter = Parameter.new('foo')
|
15
|
+
parameter.include_in_signature?.should be(true)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should know not to include it in the signature" do
|
19
|
+
parameter = Parameter.new('foo', false)
|
20
|
+
parameter.include_in_signature?.should be(false)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be able to compare itself to another parameter instance" do
|
24
|
+
p1 = Parameter.new('z', 'a')
|
25
|
+
p2 = Parameter.new('a', 'z')
|
26
|
+
|
27
|
+
(p1 <=> p2).should == 1
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../../test_helper'
|
2
|
+
|
3
|
+
module Fleakr::Api
|
4
|
+
class UploadRequestTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
describe "An instance of the UploadRequest class" do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@secret = 'sekrit'
|
10
|
+
Fleakr.stubs(:shared_secret).with().returns(@secret)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should create a file parameter on initialization" do
|
14
|
+
filename = '/path/to/image.jpg'
|
15
|
+
|
16
|
+
parameter = stub()
|
17
|
+
FileParameter.expects(:new).with('photo', filename).returns(parameter)
|
18
|
+
|
19
|
+
parameter_list = mock() {|m| m.expects(:<<).with(parameter) }
|
20
|
+
ParameterList.expects(:new).with(:authenticate? => true).returns(parameter_list)
|
21
|
+
|
22
|
+
UploadRequest.new(filename)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should allow setting additional parameters on initialization" do
|
26
|
+
params = {:is_public => 1}
|
27
|
+
|
28
|
+
ParameterList.expects(:new).with({:is_public => 1, :authenticate? => true}).returns(stub(:<<))
|
29
|
+
|
30
|
+
UploadRequest.new('filename', params)
|
31
|
+
end
|
32
|
+
|
33
|
+
context "after initialization" do
|
34
|
+
|
35
|
+
before { ParameterList.stubs(:new).returns(stub(:<< => nil)) }
|
36
|
+
|
37
|
+
it "should default the type to :create" do
|
38
|
+
request = UploadRequest.new('file')
|
39
|
+
request.type.should == :create
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should allow setting the type to :update" do
|
43
|
+
request = UploadRequest.new('file', :type => :update)
|
44
|
+
request.type.should == :update
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should know the endpoint_uri" do
|
48
|
+
request = UploadRequest.new('filename')
|
49
|
+
request.__send__(:endpoint_uri).should == URI.parse('http://api.flickr.com/services/upload/')
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should know the endpoint_uri for an :update request" do
|
53
|
+
request = UploadRequest.new('filename', :type => :update)
|
54
|
+
request.__send__(:endpoint_uri).should == URI.parse('http://api.flickr.com/services/replace/')
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should only parse the endpoint URI once" do
|
58
|
+
request = UploadRequest.new('filename')
|
59
|
+
URI.expects(:parse).with(kind_of(String)).once.returns('uri')
|
60
|
+
|
61
|
+
2.times { request.__send__(:endpoint_uri) }
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should have a collection of required headers" do
|
65
|
+
form_data = 'data'
|
66
|
+
|
67
|
+
request = UploadRequest.new('filename')
|
68
|
+
request.parameters.stubs(:boundary).with().returns('bound')
|
69
|
+
request.parameters.stubs(:to_form).with().returns(form_data)
|
70
|
+
|
71
|
+
expected = {
|
72
|
+
'Content-Type' => 'multipart/form-data; boundary=bound'
|
73
|
+
}
|
74
|
+
|
75
|
+
request.headers.should == expected
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should be able to send a POST request to the API service" do
|
79
|
+
request = UploadRequest.new('filename')
|
80
|
+
|
81
|
+
uri = stub do |s|
|
82
|
+
s.stubs(:host).with().returns('host')
|
83
|
+
s.stubs(:path).with().returns('path')
|
84
|
+
s.stubs(:port).with().returns(80)
|
85
|
+
end
|
86
|
+
|
87
|
+
request.stubs(:endpoint_uri).with().returns(uri)
|
88
|
+
|
89
|
+
request.stubs(:headers).with().returns('header' => 'value')
|
90
|
+
request.parameters.stubs(:to_form).with().returns('form')
|
91
|
+
|
92
|
+
http = mock {|m| m.expects(:post).with('path', 'form', {'header' => 'value'}) }
|
93
|
+
|
94
|
+
Net::HTTP.expects(:start).with('host', 80).yields(http).returns(stub(:body))
|
95
|
+
|
96
|
+
request.send
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should get back a response from a POST operation" do
|
100
|
+
response = stub()
|
101
|
+
|
102
|
+
Net::HTTP.expects(:start).with(kind_of(String), kind_of(Fixnum)).returns(stub(:body => '<xml>'))
|
103
|
+
Response.expects(:new).with('<xml>').returns(response)
|
104
|
+
|
105
|
+
request = UploadRequest.new('filename')
|
106
|
+
request.send.should == response
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should be able to make a full request and response cycle" do
|
110
|
+
filename = 'filename'
|
111
|
+
response = stub(:error? => false)
|
112
|
+
|
113
|
+
UploadRequest.expects(:new).with(filename, {}).returns(stub(:send => response))
|
114
|
+
UploadRequest.with_response!(filename).should == response
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should raise an exception when the full request / response cycle has errors" do
|
118
|
+
filename = 'filename'
|
119
|
+
response = stub(:error? => true, :error => stub(:code => '1', :message => 'User not found'))
|
120
|
+
|
121
|
+
UploadRequest.expects(:new).with(filename, {}).returns(stub(:send => response))
|
122
|
+
|
123
|
+
lambda do
|
124
|
+
UploadRequest.with_response!(filename)
|
125
|
+
end.should raise_error(Fleakr::ApiError)
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../../test_helper'
|
2
|
+
|
3
|
+
module Fleakr::Api
|
4
|
+
class ValueParameterTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
describe "An instance of the ValueParameter class" do
|
7
|
+
|
8
|
+
it "should have a value" do
|
9
|
+
parameter = ValueParameter.new('foo', 'bar')
|
10
|
+
parameter.value.should == 'bar'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should know how to generate the query representation of itself" do
|
14
|
+
parameter = ValueParameter.new('foo', 'bar')
|
15
|
+
parameter.to_query.should == 'foo=bar'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should escape the value when generating the query representation" do
|
19
|
+
parameter = ValueParameter.new('foo', 'Mr. Crystal?')
|
20
|
+
parameter.to_query.should == 'foo=Mr.+Crystal%3F'
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should use an empty string when generating a query if the parameter value is nil" do
|
24
|
+
parameter = ValueParameter.new('foo', nil)
|
25
|
+
parameter.to_query.should == 'foo='
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should know how to generate the form representation of itself" do
|
29
|
+
parameter = ValueParameter.new('foo', 'bar')
|
30
|
+
expected =
|
31
|
+
'Content-Disposition: form-data; name="foo"' + "\r\n" +
|
32
|
+
"\r\n" +
|
33
|
+
"bar\r\n"
|
34
|
+
|
35
|
+
parameter.to_form.should == expected
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../../test_helper'
|
2
|
+
|
3
|
+
class HashTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
describe "An instance of Hash" do
|
6
|
+
context "when extracting key/value pairs" do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@hash = {:one => 'two', :three => 'four'}
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return a hash with the matching key/value pairs" do
|
13
|
+
@hash.extract!(:one).should == {:one => 'two'}
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return an empty hash if the key isn't found" do
|
17
|
+
@hash.extract!(:foo).should == {}
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should alter the original hash when a value is extracted" do
|
21
|
+
@hash.extract!(:one)
|
22
|
+
@hash.should == {:three => 'four'}
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should be able to extract multiple keys" do
|
26
|
+
@hash.extract!(:one, :three, :missing).should == {:one => 'two', :three => 'four'}
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../../test_helper'
|
2
|
+
|
3
|
+
module Fleakr::Objects
|
4
|
+
class AuthenticationTokenTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
describe "The AuthenticationToken class" do
|
7
|
+
|
8
|
+
it "should be able to create an instance from a mini_token" do
|
9
|
+
token = '123-123-123'
|
10
|
+
auth_token = stub()
|
11
|
+
|
12
|
+
response = mock_request_cycle :for => 'auth.getFullToken', :with => {:mini_token => token, :sign? => true}
|
13
|
+
|
14
|
+
AuthenticationToken.expects(:new).with(response.body).returns(auth_token)
|
15
|
+
|
16
|
+
AuthenticationToken.from_mini_token(token).should == auth_token
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should be able to create an instance from an auth_token" do
|
20
|
+
token = 'abc123'
|
21
|
+
auth_token = stub()
|
22
|
+
|
23
|
+
response = mock_request_cycle :for => 'auth.checkToken', :with => {:auth_token => token, :sign? => true}
|
24
|
+
|
25
|
+
AuthenticationToken.expects(:new).with(response.body).returns(auth_token)
|
26
|
+
AuthenticationToken.from_auth_token(token).should == auth_token
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "An instance of the AuthenticationToken class" do
|
32
|
+
|
33
|
+
context "when populating from an XML document" do
|
34
|
+
|
35
|
+
before do
|
36
|
+
@object = AuthenticationToken.new(Hpricot.XML(read_fixture('auth.getFullToken')))
|
37
|
+
end
|
38
|
+
|
39
|
+
should_have_a_value_for :value => 'abc-123'
|
40
|
+
should_have_a_value_for :permissions => 'delete'
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|