spec/oauth2/client_spec.rb in panjiva-oauth2-0.4.1 vs spec/oauth2/client_spec.rb in panjiva-oauth2-0.5.0
- old
+ new
@@ -1,21 +1,25 @@
-require 'spec_helper'
+require 'helper'
describe OAuth2::Client do
+ let!(:error_value) {'invalid_token'}
+ let!(:error_description_value) {'bad bad token'}
+
subject do
- cli = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com')
- cli.connection.build do |b|
- b.adapter :test do |stub|
+ OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com') do |builder|
+ builder.adapter :test do |stub|
stub.get('/success') {|env| [200, {'Content-Type' => 'text/awesome'}, 'yay']}
- stub.get('/unauthorized') {|env| [401, {'Content-Type' => 'text/plain'}, 'not authorized']}
- stub.get('/conflict') {|env| [409, {'Content-Type' => 'text/plain'}, 'not authorized']}
+ stub.get('/reflect') {|env| [200, {}, env[:body]]}
+ stub.post('/reflect') {|env| [200, {}, env[:body]]}
+ stub.get('/unauthorized') {|env| [401, {'Content-Type' => 'application/json'}, MultiJson.encode(:error => error_value, :error_description => error_description_value)]}
+ stub.get('/conflict') {|env| [409, {'Content-Type' => 'text/plain'}, 'not authorized']}
stub.get('/redirect') {|env| [302, {'Content-Type' => 'text/plain', 'location' => '/success' }, '']}
+ stub.post('/redirect') {|env| [303, {'Content-Type' => 'text/plain', 'location' => '/reflect' }, '']}
stub.get('/error') {|env| [500, {}, '']}
- stub.get('/json') {|env| [200, {'Content-Type' => 'application/json; charset=utf8'}, '{"abc":"def"}']}
+ stub.get('/empty_get') {|env| [204, {}, nil]}
end
end
- cli
end
describe '#initialize' do
it 'should assign id and secret' do
subject.id.should == 'abc'
@@ -32,125 +36,133 @@
it 'should leave Faraday::Connection#ssl unset' do
subject.connection.ssl.should == {}
end
- it "should be able to pass parameters to the adapter, e.g. Faraday::Adapter::ActionDispatch" do
+ it "should be able to pass a block to configure the connection" do
connection = stub('connection')
- Faraday::Connection.stub(:new => connection)
session = stub('session', :to_ary => nil)
builder = stub('builder')
connection.stub(:build).and_yield(builder)
+ Faraday::Connection.stub(:new => connection)
- builder.should_receive(:adapter).with(:action_dispatch, session)
+ builder.should_receive(:adapter).with(:test)
- OAuth2::Client.new('abc', 'def', :adapter => [:action_dispatch, session])
+ OAuth2::Client.new('abc', 'def') do |builder|
+ builder.adapter :test
+ end.connection
end
it "defaults raise_errors to true" do
- subject.raise_errors.should be_true
+ subject.options[:raise_errors].should be_true
end
it "allows true/false for raise_errors option" do
client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :raise_errors => false)
- client.raise_errors.should be_false
+ client.options[:raise_errors].should be_false
client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :raise_errors => true)
- client.raise_errors.should be_true
+ client.options[:raise_errors].should be_true
end
it "allows get/post for access_token_method option" do
client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :access_token_method => :get)
- client.token_method.should == :get
+ client.options[:access_token_method].should == :get
client = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :access_token_method => :post)
- client.token_method.should == :post
+ client.options[:access_token_method].should == :post
end
end
- %w(authorize access_token).each do |path_type|
- describe "##{path_type}_url" do
- it "should default to a path of /oauth/#{path_type}" do
- subject.send("#{path_type}_url").should == "https://api.example.com/oauth/#{path_type}"
+ %w(authorize token).each do |url_type|
+ describe ":#{url_type}_url option" do
+ it "should default to a path of /oauth/#{url_type}" do
+ subject.send("#{url_type}_url").should == "https://api.example.com/oauth/#{url_type}"
end
- it "should be settable via the :#{path_type}_path option" do
- subject.options[:"#{path_type}_path"] = '/oauth/custom'
- subject.send("#{path_type}_url").should == 'https://api.example.com/oauth/custom'
+ it "should be settable via the :#{url_type}_url option" do
+ subject.options[:"#{url_type}_url"] = '/oauth/custom'
+ subject.send("#{url_type}_url").should == 'https://api.example.com/oauth/custom'
end
- it "should be settable via the :#{path_type}_url option" do
- subject.options[:"#{path_type}_url"] = 'https://abc.com/authorize'
- subject.send("#{path_type}_url").should == 'https://abc.com/authorize'
+ it "allows a different host than the site" do
+ subject.options[:"#{url_type}_url"] = 'https://api.foo.com/oauth/custom'
+ subject.send("#{url_type}_url").should == 'https://api.foo.com/oauth/custom'
end
end
end
describe "#request" do
- it "returns ResponseString on successful response" do
- response = subject.request(:get, '/success', {}, {})
- response.should == 'yay'
- response.status.should == 200
- response.headers.should == {'Content-Type' => 'text/awesome'}
+ it "works with a null response body" do
+ subject.request(:get, 'empty_get').body.should == ''
end
- it "follows redirects properly" do
- response = subject.request(:get, '/redirect', {}, {})
- response.should == 'yay'
+ it "returns on a successful response" do
+ response = subject.request(:get, '/success')
+ response.body.should == 'yay'
response.status.should == 200
response.headers.should == {'Content-Type' => 'text/awesome'}
end
- it "returns ResponseString on error if raise_errors is false" do
- subject.raise_errors = false
- response = subject.request(:get, '/unauthorized', {}, {})
-
- response.should == 'not authorized'
- response.status.should == 401
- response.headers.should == {'Content-Type' => 'text/plain'}
+ it "posts a body" do
+ response = subject.request(:post, '/reflect', :body => 'foo=bar')
+ response.body.should == 'foo=bar'
end
- it "raises OAuth2::AccessDenied on 401 response" do
- lambda {subject.request(:get, '/unauthorized', {}, {})}.should raise_error(OAuth2::AccessDenied)
+ it "follows redirects properly" do
+ response = subject.request(:get, '/redirect')
+ response.body.should == 'yay'
+ response.status.should == 200
+ response.headers.should == {'Content-Type' => 'text/awesome'}
end
- it "raises OAuth2::Conflict on 409 response" do
- lambda {subject.request(:get, '/conflict', {}, {})}.should raise_error(OAuth2::Conflict)
+ it "redirects using GET on a 303" do
+ response = subject.request(:post, '/redirect', :body => 'foo=bar')
+ response.body.should be_empty
+ response.status.should == 200
end
- it "raises OAuth2::HTTPError on error response" do
- lambda {subject.request(:get, '/error', {}, {})}.should raise_error(OAuth2::HTTPError)
+ it "obeys the :max_redirects option" do
+ max_redirects = subject.options[:max_redirects]
+ subject.options[:max_redirects] = 0
+ response = subject.request(:get, '/redirect')
+ response.status.should == 302
+ subject.options[:max_redirects] = max_redirects
end
- end
- it '#web_server should instantiate a WebServer strategy with this client' do
- subject.web_server.should be_kind_of(OAuth2::Strategy::WebServer)
- end
+ it "returns if raise_errors is false" do
+ subject.options[:raise_errors] = false
+ response = subject.request(:get, '/unauthorized')
- context 'with JSON parsing' do
- before do
- subject.json = true
+ response.status.should == 401
+ response.headers.should == {'Content-Type' => 'application/json'}
+ response.error.should_not be_nil
end
- describe '#request' do
- it 'should return a response hash' do
- response = subject.request(:get, '/json')
- puts response.inspect
- response.should be_kind_of(OAuth2::ResponseHash)
- response['abc'].should == 'def'
+ %w(/unauthorized /conflict /error).each do |error_path|
+ it "raises OAuth2::Error on error response to path #{error_path}" do
+ lambda {subject.request(:get, error_path)}.should raise_error(OAuth2::Error)
end
+ end
- it 'should only try to decode application/json' do
- subject.request(:get, '/success').should == 'yay'
+ it 'parses OAuth2 standard error response' do
+ begin
+ subject.request(:get, '/unauthorized')
+ rescue Exception => e
+ e.code.should == error_value
+ e.description.should == error_description_value
end
end
- it 'should set json? based on the :parse_json option' do
- OAuth2::Client.new('abc', 'def', :site => 'http://example.com', :parse_json => true).should be_json
- OAuth2::Client.new('abc', 'def', :site => 'http://example.com', :parse_json => false).should_not be_json
+ it "provides the response in the Exception" do
+ begin
+ subject.request(:get, '/error')
+ rescue Exception => e
+ e.response.should_not be_nil
+ end
end
+ end
- after do
- subject.json = false
- end
+ it '#auth_code should instantiate a AuthCode strategy with this client' do
+ subject.auth_code.should be_kind_of(OAuth2::Strategy::AuthCode)
end
context 'with SSL options' do
subject do
cli = OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com', :ssl => {:ca_file => 'foo.pem'})