diff --git a/lib/github_api/authorization.rb b/lib/github_api/authorization.rb index ff9c3343..ff38e935 100644 --- a/lib/github_api/authorization.rb +++ b/lib/github_api/authorization.rb @@ -3,8 +3,6 @@ module Github module Authorization - attr_accessor :scopes - # Setup OAuth2 instance def client @client ||= ::OAuth2::Client.new(client_id, client_secret, diff --git a/lib/github_api/client/authorizations.rb b/lib/github_api/client/authorizations.rb index cd3130a9..c6cea24d 100644 --- a/lib/github_api/client/authorizations.rb +++ b/lib/github_api/client/authorizations.rb @@ -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) diff --git a/lib/github_api/client/scopes.rb b/lib/github_api/client/scopes.rb index 19348553..9361b242 100644 --- a/lib/github_api/client/scopes.rb +++ b/lib/github_api/client/scopes.rb @@ -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 diff --git a/spec/unit/client/scopes/list_spec.rb b/spec/unit/client/scopes/list_spec.rb index a53679eb..3c34e478 100644 --- a/spec/unit/client/scopes/list_spec.rb +++ b/spec/unit/client/scopes/list_spec.rb @@ -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