Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Commit

Permalink
From review: use correct exception type, validate with new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-wovn committed Jul 29, 2021
1 parent 61a6626 commit 1f71d42
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/bing_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def request_new_access_token
when Net::HTTPServerError
raise UnavailableException.new("#{response.code}: Credentials server unavailable")
else
raise Exception.new("Unsuccessful Access Token call: Code: #{response.code} (Invalid credentials?)")
raise AuthenticationException.new("Unsuccessful Access Token call: Code: #{response.code} (Invalid credentials?)")
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/bing_translator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ def load_file(filename)
end
end

context 'when the authentication server is online' do
it 'throws an AuthenticationException if the authentication key is invalid' do
fake_api_key = '32'
authenticating_translator = BingTranslator.new(fake_api_key, skip_ssl_verify: false)

expect { authenticating_translator.translate 'hola', from: :es, to: :en }
.to raise_error(BingTranslator::Exception)
expect { authenticating_translator.translate 'hola', from: :es, to: :en }
.to raise_error(BingTranslator::AuthenticationException)
end

it 'throws an AuthenticationException if HTTP response code is not 200 or 5XX' do
authenticating_translator = BingTranslator.new(api_key, skip_ssl_verify: false)
stub_request(:any, COGNITIVE_ACCESS_TOKEN_URI).
to_return(status: [404, 'Not Found'])

expect { authenticating_translator.translate 'hola', from: :es, to: :en }
.to raise_error(BingTranslator::Exception)
expect { authenticating_translator.translate 'hola', from: :es, to: :en }
.to raise_error(BingTranslator::AuthenticationException)
end
end

context 'when invalid language is specified' do
it 'throws a reasonable error' do
expect { translator.translate 'hola', from: :invlaid, to: :en }
Expand Down

0 comments on commit 1f71d42

Please sign in to comment.