From 6e234ce26ca387ed9ac26faa540909ca99f7318b Mon Sep 17 00:00:00 2001 From: Niels van Hoorn Date: Thu, 27 Dec 2012 23:37:45 +0100 Subject: [PATCH] Fixed error with creating hash for invalid credentials --- lib/instapaper/client/user.rb | 7 ++++++- spec/fixtures/invalid_credentials.qline | 1 + spec/instapaper/client/user_spec.rb | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/invalid_credentials.qline diff --git a/lib/instapaper/client/user.rb b/lib/instapaper/client/user.rb index a0c338e..f03cdf4 100644 --- a/lib/instapaper/client/user.rb +++ b/lib/instapaper/client/user.rb @@ -6,7 +6,12 @@ module Instapaper # Gets an OAuth access token for a user. def access_token(username, password) response = post('oauth/access_token', { :x_auth_username => username, :x_auth_password => password, :x_auth_mode => "client_auth"}, true) - Hash[*response.body.split("&").map {|part| part.split("=") }.flatten] + params = response.body.split("&") + values = params.map {|part| part.split("=") }.flatten + if values.length == 1 + values.unshift('error') + end + Hash[*values] end end diff --git a/spec/fixtures/invalid_credentials.qline b/spec/fixtures/invalid_credentials.qline new file mode 100644 index 0000000..a5241d6 --- /dev/null +++ b/spec/fixtures/invalid_credentials.qline @@ -0,0 +1 @@ +Invalid xAuth credentials. \ No newline at end of file diff --git a/spec/instapaper/client/user_spec.rb b/spec/instapaper/client/user_spec.rb index 52d0bbd..a637bbe 100644 --- a/spec/instapaper/client/user_spec.rb +++ b/spec/instapaper/client/user_spec.rb @@ -9,6 +9,8 @@ describe Instapaper::Client::User do before do stub_post("oauth/access_token").with(:body => { :x_auth_username => 'ohai', :x_auth_password => 'p455w0rd', :x_auth_mode => 'client_auth'}). to_return(:body => fixture("access_token.qline"), :headers => {:content_type => "text/plain; charset=utf-8"}) + stub_post("oauth/access_token").with(:body => { :x_auth_username => 'inval1d', :x_auth_password => 'cr3dentials', :x_auth_mode => 'client_auth'}). + to_return(:body => fixture("invalid_credentials.qline"), :headers => {:content_type => "text/plain; charset=utf-8"}) end it "should get the correct resource" do @@ -23,6 +25,12 @@ describe Instapaper::Client::User do tokens.key?('oauth_token').should be_true tokens.key?('oauth_token_secret').should be_true end + + it "should return a hash containing the error on invalid credentials" do + tokens = @client.access_token('inval1d', 'cr3dentials') + tokens.should be_a Hash + tokens.key?('error').should be_true + end end end