Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions github-auth.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'sinatra', '~> 1.4.4'
spec.add_development_dependency 'thin', '~> 1.6.1'

spec.add_runtime_dependency 'httparty', '~> 0.12.0'
spec.add_runtime_dependency 'thor', '~> 0.18.1'
spec.add_runtime_dependency 'faraday', '~> 0.9.0'
spec.add_runtime_dependency 'thor', '~> 0.18.1'
end
16 changes: 9 additions & 7 deletions lib/github/auth/keys_client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'cgi'
require 'httparty'
require 'json'
require 'faraday'

module Github::Auth
# Client for fetching public SSH keys using the Github API
Expand Down Expand Up @@ -35,17 +36,18 @@ def keys
private

def github_response
response = http_client.get(
response = http_client.get \
"#{hostname}/users/#{username}/keys", headers: headers
)
raise GithubUserDoesNotExistError if response.code == 404
response.parsed_response
rescue SocketError, Errno::ECONNREFUSED => e

raise GithubUserDoesNotExistError if response.status == 404

JSON.parse response.body
rescue Faraday::Error => e
raise GithubUnavailableError, e
end

def http_client
HTTParty
Faraday
end

def headers
Expand Down
6 changes: 3 additions & 3 deletions spec/support/mock_github_server.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'httparty'
require 'faraday'
require 'sinatra/base'
require 'json'

Expand Down Expand Up @@ -34,9 +34,9 @@ def with_mock_github_server

while true
begin
HTTParty.get(hostname)
Faraday.get hostname
break
rescue Errno::ECONNREFUSED
rescue Faraday::ConnectionFailed
# Do nothing, try again
end
end
Expand Down
30 changes: 16 additions & 14 deletions spec/unit/github/auth/keys_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
let(:username) { 'chrishunt' }
let(:http_client) { double('HttpClient', get: response) }
let(:response_code) { 200 }
let(:parsed_response) { nil }
let(:body) { [] }
let(:response) {
double('HTTParty::Response', {
code: response_code,
parsed_response: parsed_response
double('Faraday::Response', {
status: response_code,
body: JSON.generate(body)
})
}

Expand Down Expand Up @@ -55,13 +55,13 @@
end

context 'when the github user has keys' do
let(:parsed_response) {[
let(:body) {[
{ 'id' => 123, 'key' => 'abc123' },
{ 'id' => 456, 'key' => 'def456' }
]}

it 'returns the keys' do
expected_keys = parsed_response.map do |entry|
expected_keys = body.map do |entry|
Github::Auth::Key.new username, entry.fetch('key')
end

Expand All @@ -70,7 +70,7 @@
end

context 'when the github user does not have keys' do
let(:parsed_response) { [] }
let(:body) { [] }

it 'returns an empty array' do
expect(subject.keys).to eq []
Expand All @@ -88,14 +88,16 @@
end

context 'when there is an issue connecting to Github' do
[SocketError, Errno::ECONNREFUSED].each do |exception|
before { http_client.stub(:get).and_raise exception }
before do
http_client
.stub(:get)
.and_raise Faraday::Error::ConnectionFailed.new('Oops!')
end

it 'raises a GithubUnavailableError' do
expect {
subject.keys
}.to raise_error Github::Auth::KeysClient::GithubUnavailableError
end
it 'raises a GithubUnavailableError' do
expect {
subject.keys
}.to raise_error Github::Auth::KeysClient::GithubUnavailableError
end
end
end
Expand Down