Skip to content

Commit 765d286

Browse files
committed
Merge branch 'master' of git://github.com/jeffrwells/github into jeffrwells-master
2 parents a9a5ad0 + ea52763 commit 765d286

3 files changed

Lines changed: 128 additions & 0 deletions

File tree

lib/github_api/client/authorizations/app.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ def delete(*args)
6565
alias :remove :delete
6666
alias :revoke :delete
6767

68+
# Check if an access token is a valid authorization for an application
69+
#
70+
# @example
71+
# github - Github.new basic_auth: "client_id:client_secret"
72+
# github.oauth.app.check 'client_id', 'access-token'
73+
# @api public
74+
75+
def check(*args)
76+
raise_authentication_error unless authenticated?
77+
params = arguments(args, required: [:client_id, :access_token]).params
78+
79+
if client_id
80+
begin
81+
get_request("/applications/#{client_id}/tokens/#{access_token}", params)
82+
rescue Github::Error::NotFound => e
83+
nil
84+
end
85+
else
86+
raise raise_app_authentication_error
87+
end
88+
end
89+
6890
protected
6991

7092
def raise_app_authentication_error

spec/fixtures/auths/check.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"id": 1,
3+
"url": "https://api.github.com/authorizations/1",
4+
"scopes": [
5+
"public_repo"
6+
],
7+
"token": "abc123",
8+
"app": {
9+
"url": "http://my-github-app.com",
10+
"name": "my github app",
11+
"client_id": "abcde12345fghij67890"
12+
},
13+
"note": "optional note",
14+
"note_url": "http://optional/note/url",
15+
"updated_at": "2011-09-06T20:39:23Z",
16+
"created_at": "2011-09-06T17:26:27Z",
17+
"user": {
18+
"login": "octocat",
19+
"id": 1,
20+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
21+
"gravatar_id": "somehexcode",
22+
"url": "https://api.github.com/users/octocat",
23+
"html_url": "https://github.com/octocat",
24+
"followers_url": "https://api.github.com/users/octocat/followers",
25+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
26+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
27+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
28+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
29+
"organizations_url": "https://api.github.com/users/octocat/orgs",
30+
"repos_url": "https://api.github.com/users/octocat/repos",
31+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
32+
"received_events_url": "https://api.github.com/users/octocat/received_events",
33+
"type": "User",
34+
"site_admin": false
35+
}
36+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Github::Authorizations::App, '#check' do
6+
let(:basic_auth) { 'login:password' }
7+
let(:host) { "https://#{basic_auth}@api.github.com" }
8+
let(:request_path) { "/applications/#{client_id}/tokens/#{access_token}" }
9+
let(:client_id) { 1 }
10+
let(:access_token) { 'abc123' }
11+
12+
before {
13+
subject.basic_auth = basic_auth
14+
15+
stub_get(request_path, host).to_return(body: body, status: status,
16+
headers: {content_type: 'application/json; charset=utf-8'})
17+
}
18+
19+
after { reset_authentication_for(subject) }
20+
21+
context 'when app makes a request' do
22+
let(:body) { "" }
23+
let(:status) { 200 }
24+
25+
it "checks resource successfully" do
26+
subject.check client_id, access_token
27+
a_get(request_path, host).should have_been_made
28+
end
29+
30+
it "fails without client_id" do
31+
expect { subject.check }.to raise_error(ArgumentError)
32+
end
33+
34+
it "fails without access_token" do
35+
expect {subject.check(client_id)}.to raise_error(ArgumentError)
36+
end
37+
38+
end
39+
40+
context 'when app checks a token that is valid' do
41+
let(:body) { fixture('auths/check.json') }
42+
let(:status) { 200 }
43+
44+
45+
it "returns the resource" do
46+
authorization = subject.check client_id, access_token
47+
authorization.should be_a Github::ResponseWrapper
48+
end
49+
50+
it "gets the authorization information" do
51+
authorization = subject.check client_id, access_token
52+
authorization.token.should == 'abc123'
53+
end
54+
end
55+
56+
context 'when app checks a token that is not valid' do
57+
let(:body) { '' }
58+
let(:status) { 404 }
59+
60+
it "does not raise error for expected 404" do
61+
expect { subject.check client_id, access_token }.to_not raise_error
62+
end
63+
64+
it "returns nil" do
65+
authorization = subject.check client_id, access_token
66+
authorization.should be_nil
67+
end
68+
69+
end
70+
end # check

0 commit comments

Comments
 (0)