Skip to content

Commit

Permalink
Fix issue with scopes listing and close issue #219
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jun 28, 2016
1 parent b7efe31 commit f1da49d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 28 deletions.
2 changes: 0 additions & 2 deletions lib/github_api/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
module Github
module Authorization

attr_accessor :scopes

# Setup OAuth2 instance
def client
@client ||= ::OAuth2::Client.new(client_id, client_secret,
Expand Down
4 changes: 2 additions & 2 deletions lib/github_api/client/authorizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Client::Authorizations < API
#
# @example
# github = Github.new basic_auth: 'login:password'
# github.oauth.list
# github.oauth.list { |auth| ... }
# github.auth.list
# github.auth.list { |auth| ... }
#
# @api public
def list(*args)
Expand Down
40 changes: 33 additions & 7 deletions lib/github_api/client/scopes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,41 @@ module Github
class Client::Scopes < API
# Check what OAuth scopes you have.
#
# = Examples
# github = Github.new :oauth_token => 'token'
# github.scopes.all
# @see https://developer.github.com/v3/oauth/#scopes
#
# @example
# github = Github.new oauth_token: 'e72e16c7e42f292c6912e7710c838347ae17'
# github.scopes.all
#
# @example
# github = Github.new
# github.scopes.list 'e72e16c7e42f292c6912e7710c838347ae17'
#
# @example
# github = Github.new
# github.scopes.list token: 'e72e16c7e42f292c6912e7710c838347ae17'
#
# @api public
def list(*args)
arguments(args)
response = get_request("/user", arguments.params)
response.headers.oauth_scopes ? response.headers.oauth_scopes.split(',') : response
params = arguments.params
token = args.shift

if token.is_a?(Hash) && !params['token'].nil?
token = params.delete('token')
elsif token.nil?
token = oauth_token
end

if token.nil?
raise ArgumentError, 'Access token required'
end

headers = { 'Authorization' => "token #{token}" }
params['headers'] = headers
response = get_request("/user", params)
response.headers.oauth_scopes.split(',').map(&:strip)
end
alias :all :list
end
alias all list
end # Client::Scopes
end # Github
39 changes: 22 additions & 17 deletions spec/unit/client/scopes/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,37 @@

require 'spec_helper'

describe Github::Client::Scopes, '#list' do
RSpec.describe Github::Client::Scopes, '#list' do
let(:request_path) { "/user" }
let(:body) { '[]' }
let(:status) { 200 }
let(:accepted_scopes) { "delete_repo, repo, public_repo, repo:status" }
let(:scopes) { 'repo' }

before {
subject.oauth_token = OAUTH_TOKEN
stub_get(request_path).
with(:query => { :access_token => "#{OAUTH_TOKEN}"}).
to_return(:body => body, :status => status,
:headers => {:content_type => "application/json; charset=utf-8",
'x-accepted-oauth-scopes' => accepted_scopes, 'x-oauth-scopes' => 'repo'
before do
stub_get(request_path).with(headers: {'Authorization' => "token 123abc"}).
to_return(body: body, status: status,
headers: {content_type: "application/json; charset=utf-8",
'X-Accepted-OAuth-Scopes' => "user",
'X-OAuth-Scopes' => 'repo, user'
})
}
end

it 'performs request with token as argument' do
subject.list('123abc')
expect(a_get(request_path)).to have_been_made
end

after { reset_authentication_for(subject) }
it 'performs request with token as option' do
subject.list(token: '123abc')
expect(a_get(request_path)).to have_been_made
end

it 'performs request' do
subject.list
a_get(request_path).with(:query => {:access_token => OAUTH_TOKEN}).
should have_been_made
it "raises error without token" do
expect {
subject.list
}.to raise_error(ArgumentError, /Access token required/)
end

it 'queries oauth header' do
subject.list.should == [scopes]
expect(subject.list('123abc')).to eq(['repo', 'user'])
end
end # list

0 comments on commit f1da49d

Please sign in to comment.