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:
parent
500a941d84
commit
d940869674
|
@ -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']
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue