Skip to content

Commit f1da49d

Browse files
committed
Fix issue with scopes listing and close issue piotrmurach#219
1 parent b7efe31 commit f1da49d

4 files changed

Lines changed: 57 additions & 28 deletions

File tree

lib/github_api/authorization.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
module Github
44
module Authorization
55

6-
attr_accessor :scopes
7-
86
# Setup OAuth2 instance
97
def client
108
@client ||= ::OAuth2::Client.new(client_id, client_secret,

lib/github_api/client/authorizations.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class Client::Authorizations < API
1515
#
1616
# @example
1717
# github = Github.new basic_auth: 'login:password'
18-
# github.oauth.list
19-
# github.oauth.list { |auth| ... }
18+
# github.auth.list
19+
# github.auth.list { |auth| ... }
2020
#
2121
# @api public
2222
def list(*args)

lib/github_api/client/scopes.rb

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,41 @@ module Github
44
class Client::Scopes < API
55
# Check what OAuth scopes you have.
66
#
7-
# = Examples
8-
# github = Github.new :oauth_token => 'token'
9-
# github.scopes.all
7+
# @see https://developer.github.com/v3/oauth/#scopes
108
#
9+
# @example
10+
# github = Github.new oauth_token: 'e72e16c7e42f292c6912e7710c838347ae17'
11+
# github.scopes.all
12+
#
13+
# @example
14+
# github = Github.new
15+
# github.scopes.list 'e72e16c7e42f292c6912e7710c838347ae17'
16+
#
17+
# @example
18+
# github = Github.new
19+
# github.scopes.list token: 'e72e16c7e42f292c6912e7710c838347ae17'
20+
#
21+
# @api public
1122
def list(*args)
1223
arguments(args)
13-
response = get_request("/user", arguments.params)
14-
response.headers.oauth_scopes ? response.headers.oauth_scopes.split(',') : response
24+
params = arguments.params
25+
token = args.shift
26+
27+
if token.is_a?(Hash) && !params['token'].nil?
28+
token = params.delete('token')
29+
elsif token.nil?
30+
token = oauth_token
31+
end
32+
33+
if token.nil?
34+
raise ArgumentError, 'Access token required'
35+
end
36+
37+
headers = { 'Authorization' => "token #{token}" }
38+
params['headers'] = headers
39+
response = get_request("/user", params)
40+
response.headers.oauth_scopes.split(',').map(&:strip)
1541
end
16-
alias :all :list
17-
end
42+
alias all list
43+
end # Client::Scopes
1844
end # Github

spec/unit/client/scopes/list_spec.rb

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,37 @@
22

33
require 'spec_helper'
44

5-
describe Github::Client::Scopes, '#list' do
5+
RSpec.describe Github::Client::Scopes, '#list' do
66
let(:request_path) { "/user" }
77
let(:body) { '[]' }
88
let(:status) { 200 }
9-
let(:accepted_scopes) { "delete_repo, repo, public_repo, repo:status" }
10-
let(:scopes) { 'repo' }
119

12-
before {
13-
subject.oauth_token = OAUTH_TOKEN
14-
stub_get(request_path).
15-
with(:query => { :access_token => "#{OAUTH_TOKEN}"}).
16-
to_return(:body => body, :status => status,
17-
:headers => {:content_type => "application/json; charset=utf-8",
18-
'x-accepted-oauth-scopes' => accepted_scopes, 'x-oauth-scopes' => 'repo'
10+
before do
11+
stub_get(request_path).with(headers: {'Authorization' => "token 123abc"}).
12+
to_return(body: body, status: status,
13+
headers: {content_type: "application/json; charset=utf-8",
14+
'X-Accepted-OAuth-Scopes' => "user",
15+
'X-OAuth-Scopes' => 'repo, user'
1916
})
20-
}
17+
end
18+
19+
it 'performs request with token as argument' do
20+
subject.list('123abc')
21+
expect(a_get(request_path)).to have_been_made
22+
end
2123

22-
after { reset_authentication_for(subject) }
24+
it 'performs request with token as option' do
25+
subject.list(token: '123abc')
26+
expect(a_get(request_path)).to have_been_made
27+
end
2328

24-
it 'performs request' do
25-
subject.list
26-
a_get(request_path).with(:query => {:access_token => OAUTH_TOKEN}).
27-
should have_been_made
29+
it "raises error without token" do
30+
expect {
31+
subject.list
32+
}.to raise_error(ArgumentError, /Access token required/)
2833
end
2934

3035
it 'queries oauth header' do
31-
subject.list.should == [scopes]
36+
expect(subject.list('123abc')).to eq(['repo', 'user'])
3237
end
3338
end # list

0 commit comments

Comments
 (0)