Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Serialize/deserialize mode
Browse files Browse the repository at this point in the history
  • Loading branch information
RISCfuture committed May 16, 2011
1 parent c752471 commit e044f03
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
7 changes: 4 additions & 3 deletions lib/dropbox/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,17 @@ def access_token

def serialize
if authorized? then
[ @consumer.key, @consumer.secret, authorized?, @access_token.token, @access_token.secret, @ssl ].to_yaml
[ @consumer.key, @consumer.secret, authorized?, @access_token.token, @access_token.secret, @ssl, mode ].to_yaml
else
[ @consumer.key, @consumer.secret, authorized?, @request_token.token, @request_token.secret, @ssl ].to_yaml
[ @consumer.key, @consumer.secret, authorized?, @request_token.token, @request_token.secret, @ssl, mode ].to_yaml
end
end

# Deserializes an instance from a string created from the serialize method.
# Returns the recreated instance.

def self.deserialize(data)
consumer_key, consumer_secret, authorized, token, token_secret, ssl = YAML.load(StringIO.new(data))
consumer_key, consumer_secret, authorized, token, token_secret, ssl, mode = YAML.load(StringIO.new(data))
raise ArgumentError, "Must provide a properly serialized #{self.to_s} instance" unless [ consumer_key, consumer_secret, token, token_secret ].all? and authorized == true or authorized == false

session = self.new(consumer_key, consumer_secret, :ssl => ssl, :already_authorized => authorized)
Expand All @@ -175,6 +175,7 @@ def self.deserialize(data)
else
session.instance_variable_set :@request_token, OAuth::RequestToken.new(session.instance_variable_get(:@consumer), token, token_secret)
end
session.mode = mode if mode

return session
end
Expand Down
28 changes: 23 additions & 5 deletions spec/dropbox/session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
@token_mock.stub!(:token).and_return("request token")
@token_mock.stub!(:secret).and_return("request token secret")

@session.serialize.should eql([ "consumer key", "consumer secret", false, "request token", "request token secret", false ].to_yaml)
@session.serialize.should eql([ "consumer key", "consumer secret", false, "request token", "request token secret", false, :sandbox ].to_yaml)
end

it "should serialize the SSL setting" do
Expand All @@ -142,7 +142,18 @@
@token_mock.stub!(:token).and_return("request token")
@token_mock.stub!(:secret).and_return("request token secret")

@session.serialize.should eql([ "consumer key", "consumer secret", false, "request token", "request token secret", true ].to_yaml)
@session.serialize.should eql([ "consumer key", "consumer secret", false, "request token", "request token secret", true, :sandbox ].to_yaml)
end

it "should serialize the mode" do
@session = Dropbox::Session.new('foo', 'bar')
@consumer_mock.stub!(:key).and_return("consumer key")
@consumer_mock.stub!(:secret).and_return("consumer secret")
@token_mock.stub!(:token).and_return("request token")
@token_mock.stub!(:secret).and_return("request token secret")
@session.mode = :dropbox

@session.serialize.should eql([ "consumer key", "consumer secret", false, "request token", "request token secret", false, :dropbox ].to_yaml)
end
end

Expand All @@ -152,21 +163,28 @@
end

it "should raise an error if an improper YAML is provided" do
lambda { Dropbox::Session.deserialize([ 1, 2, 3].to_yaml) }.should raise_error(ArgumentError)
lambda { Dropbox::Session.deserialize([ 1, 2, 3 ].to_yaml) }.should raise_error(ArgumentError)
end

it "should return a properly initialized unauthorized instance" do
Dropbox::Session.should_receive(:new).once.with('key', 'secret', :ssl => true, :already_authorized => false).and_return(@mock_session)
@mock_session.should_receive(:mode=).once.with(:dropbox)

Dropbox::Session.deserialize([ 'key', 'secret', false, 'a', 'b', true ].to_yaml).should eql(@mock_session)
Dropbox::Session.deserialize([ 'key', 'secret', false, 'a', 'b', true, :dropbox ].to_yaml).should eql(@mock_session)
#TODO request token remains opaque for purposes of testing
end

it "should allow the SSL option to be left out" do
Dropbox::Session.should_receive(:new).once.with('key', 'secret', :ssl => nil, :already_authorized=>false).and_return(@mock_session)
@mock_session.stub!(:mode=)

Dropbox::Session.deserialize([ 'key', 'secret', false, 'a', 'b' ].to_yaml).should eql(@mock_session)
end

it "should allow the mode to be left out" do
Dropbox::Session.should_receive(:new).once.with('key', 'secret', :ssl => nil, :already_authorized=>false).and_return(@mock_session)
Dropbox::Session.deserialize([ 'key', 'secret', false, 'a', 'b' ].to_yaml).should eql(@mock_session)
end
end

describe "with Dropbox keys" do
Expand Down Expand Up @@ -231,7 +249,7 @@ def new_session
it "should return the consumer key and secret and the access token and secret in YAML form if authorized" do
@session = new_session
@session.authorize!.should be_true
@session.serialize.should eql([ @keys['key'], @keys['secret'], true, @session.send(:access_token).token, @session.send(:access_token).secret, false ].to_yaml)
@session.serialize.should eql([ @keys['key'], @keys['secret'], true, @session.send(:access_token).token, @session.send(:access_token).secret, false, :sandbox ].to_yaml)
end
end
end
Expand Down

0 comments on commit e044f03

Please sign in to comment.