Properly handle authentication failures when creating a scout session

Also added a bit more tolerance to general API failures

Fixes #2
This commit is contained in:
R. Tyler Croy 2012-12-17 23:36:08 -08:00
parent 500a941d84
commit d940869674
4 changed files with 43 additions and 4 deletions

View File

@ -26,9 +26,18 @@ module Sauce
:password => config.access_key},
:headers => {'Content-Type' => 'application/json'})
return nil unless (response && response.code == 200)
if response.code == 401
raise Errors::SauceAuthenticationError, 'Authentication failed'
elsif response.code != 200
raise Errors::SauceUnknownError, 'Something is wrong with the Sauce Labs API'
end
begin
response = JSON.parse(response.body)
rescue JSON::ParserError
raise Errors::SauceUnknownError, 'Sauce API is returning invalid JSON'
end
response = JSON.parse(response.body)
# The response should contain the attribute `embed` which is the
# usable scout session URL
return response['embed']

View File

@ -120,7 +120,8 @@ access_key: #{apikey}
begin
url = @sauceapi.create_scout_session(body)
rescue ::Sauce::Heroku::Errors::SauceAuthenticationError
puts 'Auth error!'
display 'Authentication error! Check your ondemand.yml!'
return true
end
unless url.nil?

View File

@ -4,6 +4,7 @@ module Sauce
class SauceAuthenticationError < StandardError; end;
class InvalidParametersError < StandardError; end;
class SauceNotConfiguredError < StandardError; end;
class SauceUnknownError < StandardError; end;
end
end
end

View File

@ -41,6 +41,12 @@ describe Sauce::Heroku::API::Sauce do
include_context 'valid config'
let(:url) { 'http://fake/scout/url' }
let(:body) { {:url => 'http://saucelabs.com/'} }
let(:response) do
r = mock('Valid response')
r.stub(:code => 200)
r.stub(:body => '{"embed" : "http://rspec"}')
r
end
before :each do
config.stub(:configured? => true)
api.should_receive(:scout_url).and_return(url)
@ -49,9 +55,31 @@ describe Sauce::Heroku::API::Sauce do
it 'should POST to Sauce Labs properly' do
headers = {'Content-Type' => 'application/json'}
HTTParty.should_receive(:post).with(url,
hash_including(:headers => headers))
hash_including(:headers => headers)
).and_return(response)
api.create_scout_session(body)
end
context 'with invalid credentials' do
let(:response) { mock('Unauthorized Response', :code => 401) }
it 'should raise an error' do
HTTParty.should_receive(:post).and_return(response)
expect {
api.create_scout_session(body)
}.to raise_error(Sauce::Heroku::Errors::SauceAuthenticationError)
end
end
context 'when the API returns non-200s' do
let(:response) { mock('Unauthorized Response', :code => 500) }
it 'should raise an error' do
HTTParty.should_receive(:post).and_return(response)
expect {
api.create_scout_session(body)
}.to raise_error(Sauce::Heroku::Errors::SauceUnknownError)
end
end
end
end