Browse files

added access token request spec

  • Loading branch information...
1 parent 500a32f commit 1efee8d47ae91daf349782b96dfb1ab074400d01 @markburns markburns committed Dec 13, 2012
Showing with 43 additions and 21 deletions.
  1. +6 −5 lib/shutl/auth/access_token_request.rb
  2. +37 −16 spec/access_token_request_spec.rb
View
11 lib/shutl/auth/access_token_request.rb
@@ -5,11 +5,12 @@ def initialize
#TODO: handle the various exceptions that can be thrown by the OAuth2 gem
#and turn into Shutl specific exceptions
@client = Rack::OAuth2::Client.new(
- identifier: Shutl::Auth.client_id,
- secret: Shutl::Auth.client_secret,
- host: uri.host,
- port: uri.port,
- scheme: uri.scheme
+ identifier: Shutl::Auth.client_id,
+ secret: Shutl::Auth.client_secret,
+ token_endpoint: '/token',
+ host: uri.host,
+ port: uri.port,
+ scheme: uri.scheme
)
end
View
53 spec/access_token_request_spec.rb
@@ -1,21 +1,18 @@
-require File.expand_path('spec/spec_helper')
+require 'spec_helper'
describe Shutl::Auth::AccessTokenRequest do
- before do
- Shutl::Auth.config do |s|
- s.url = 'http://localhost:3001'
- s.client_id = 'asdf'
- s.client_secret = 'asdf'
- end
- end
-
-
subject { Shutl::Auth::AccessTokenRequest.new }
let(:oauth_client) { mock 'oauth client' }
before do
Rack::OAuth2::Client.stub(:new).and_return oauth_client
+
+ Shutl::Auth.config do |c|
+ c.url = "http://localhost:3000"
+ c.client_id = "QUOTE_SERVICE_CLIENT_ID"
+ c.client_secret = "QUOTE_SERVICE_CLIENT_SECRET"
+ end
end
context 'successful request to authentication service' do
@@ -29,14 +26,38 @@
end
describe 'retries on network error' do
- before do
- oauth_client.should_receive(:access_token!).
- exactly(3).times.and_raise Timeout::Error
+ let(:oauth_client) do
+ FailNTimesThenSucceed.new number_of_failures, 'token'
end
- specify do
- subject.access_token!
+ context 'succeed on third attempt' do
+ let(:number_of_failures) { 2 }
+
+ specify do
+ Shutl::Auth::AccessTokenRequest.new.access_token!.should == 'token'
+ end
+ end
+
+ context 'fail more than two times' do
+ let(:number_of_failures) { 3 }
+
+ specify do
+ Shutl::Auth::AccessTokenRequest.new.access_token!.should be_nil
+ end
+ end
+
+ class FailNTimesThenSucceed
+ def initialize number_of_failures, access_token
+ @number_of_failures = number_of_failures
+ @access_token = access_token
+ @counter = 0
+ end
+
+ def access_token!
+ @counter += 1
+ raise Errno::ECONNREFUSED if @counter <= @number_of_failures
+ @access_token
+ end
end
end
end
-

0 comments on commit 1efee8d

Please sign in to comment.