Skip to content

Commit b30f15b

Browse files
committed
more spec
1 parent 0e17ec5 commit b30f15b

18 files changed

+213
-14
lines changed

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--format=documentation

Gemfile.lock

+6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ GEM
1919
builder (~> 2.1.2)
2020
i18n (~> 0.5.0)
2121
activesupport (3.0.9)
22+
addressable (2.2.6)
2223
attr_required (0.0.3)
2324
builder (2.1.2)
25+
crack (0.1.8)
2426
diff-lcs (1.1.2)
2527
httpclient (2.2.1)
2628
i18n (0.5.0)
@@ -60,6 +62,9 @@ GEM
6062
mail (>= 2.2.5)
6163
validate_url (0.2.0)
6264
activemodel (>= 3.0.0)
65+
webmock (1.6.4)
66+
addressable (> 2.2.5, ~> 2.2)
67+
crack (>= 0.1.7)
6368

6469
PLATFORMS
6570
ruby
@@ -69,3 +74,4 @@ DEPENDENCIES
6974
rake (>= 0.8)
7075
rcov (>= 0.9)
7176
rspec (>= 2)
77+
webmock (>= 1.6.2)

lib/openid_connect/access_token.rb

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ module OpenIDConnect
22
class AccessToken < Rack::OAuth2::AccessToken::Bearer
33
attr_required :client
44

5+
def initialize(attributes = {})
6+
super
7+
@token_type = :bearer
8+
end
9+
510
def user_info!(scheme = :openid)
6-
klass = case scheme
7-
when :openid
8-
ResponseObject::UserInfo::OpenID
9-
else
10-
raise Exception.new("Unknown Scheme: #{scheme}")
11-
end
1211
hash = resource_request do
1312
get client.user_info_uri
1413
end
15-
klass.new hash
14+
ResponseObject::UserInfo::OpenID.new hash
1615
end
1716

1817
def id_token!
@@ -29,10 +28,14 @@ def resource_request
2928
case res.status
3029
when 200
3130
JSON.parse(res.body).with_indifferent_access
31+
when 400
32+
raise BadRequest.new('API Access Faild')
3233
when 401
33-
raise OpenIDConnect::Unauthorized.new('Access Token Invalid or Expired')
34+
raise Unauthorized.new('Access Token Invalid or Expired')
35+
when 403
36+
raise Forbidden.new('Insufficient Scope')
3437
else
35-
raise OpenIDConnect::BadRequest.new('API Access Faild')
38+
raise HttpError.new(res.status, 'Unknown HttpError')
3639
end
3740
end
3841
end

lib/openid_connect/client.rb

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def authorization_uri(params = {})
1919

2020
def access_token!
2121
token = super
22+
raise Exception.new("Unexpected Token Type: #{token.token_type}") unless token.token_type == :bearer
2223
AccessToken.new token.token_response.merge(:client => self)
2324
end
2425

openid_connect.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ Gem::Specification.new do |s|
2121
s.add_development_dependency "rake", ">= 0.8"
2222
s.add_development_dependency "rcov", ">= 0.9"
2323
s.add_development_dependency "rspec", ">= 2"
24+
s.add_development_dependency "webmock", ">= 1.6.2"
2425
end

spec/helpers/webmock_helper.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'webmock/rspec'
2+
3+
module WebMockHelper
4+
def mock_json(method, endpoint, response_file, options = {})
5+
stub_request(method, endpoint).with(
6+
request_for(method, options)
7+
).to_return(
8+
response_for(response_file, options)
9+
)
10+
yield
11+
a_request(method, endpoint).with(
12+
request_for(method, options)
13+
).should have_been_made.once
14+
end
15+
16+
private
17+
18+
def request_for(method, options = {})
19+
request = {}
20+
if options[:params]
21+
case method
22+
when :post, :put
23+
request[:body] = options[:params]
24+
else
25+
request[:query] = options[:params]
26+
end
27+
end
28+
request
29+
end
30+
31+
def response_for(response_file, options = {})
32+
response = {}
33+
response[:body] = File.new(File.join(File.dirname(__FILE__), '../mock_response', "#{response_file}.#{options[:format] || :json}"))
34+
if options[:status]
35+
response[:status] = options[:status]
36+
end
37+
response
38+
end
39+
end
40+
41+
include WebMockHelper
42+
WebMock.disable_net_connect!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"access_token":"access_token",
3+
"refresh_token":"refresh_token",
4+
"token_type":"bearer",
5+
"expires_in":3600
6+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"token_type": "mac",
3+
"mac_algorithm": "hmac-sha-256",
4+
"expires_in": 3600,
5+
"mac_key": "secret",
6+
"refresh_token": "refresh_token",
7+
"access_token": "access_token"
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"error": "insufficient_scope"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"error": "invalid_access_token"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"error": "invalid_request"
3+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fuckin Unknown Error

spec/mock_response/id_token.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"iss": "http://server.example.com",
3+
"client_id": "http://client.example.com",
4+
"aud": "http://client.example.com",
5+
"user_id": "user_328723",
6+
"exp": 1303852880
7+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"id": "90125",
3+
"name": "Jonathan Q. Doe",
4+
"given_name": "Jonathan",
5+
"middle_name": "Q.",
6+
"family_name": "Doe",
7+
"nickname": "John",
8+
"email": "[email protected]",
9+
"verified": true,
10+
"profile": "http://example.com/johndoe/",
11+
"picture": "http://example.com/johndoe/me.jpg",
12+
"website": "http://john.doe.blogs.example.net/",
13+
"gender": "male",
14+
"birthday": "05/02/0000",
15+
"zoneinfo": "America/Los_Angeles",
16+
"locale": "en_US",
17+
"phone_number": "+1 (425) 555-1212",
18+
"address": {
19+
"region": "WA",
20+
"country": "United States"
21+
},
22+
"last_updated": "2011-06-29T21:10:22+0000"
23+
}
+59-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,68 @@
11
require 'spec_helper'
22

33
describe OpenIDConnect::AccessToken do
4+
subject { token }
5+
let :client do
6+
OpenIDConnect::Client.new(
7+
:identifier => 'client_id',
8+
:host => 'server.example.com'
9+
)
10+
end
11+
let :token do
12+
OpenIDConnect::AccessToken.new(
13+
:access_token => 'access_token',
14+
:client => client
15+
)
16+
end
17+
its(:token_type) { should == :bearer }
18+
419
describe '#user_info!' do
5-
it :TODO
20+
it 'should return OpenIDConnect::ResponseObject::UserInfo::OpenID' do
21+
mock_json :get, client.user_info_uri, 'user_info/openid', :HTTP_AUTHORIZATION => 'Bearer access_token' do
22+
token.user_info!.should be_a OpenIDConnect::ResponseObject::UserInfo::OpenID
23+
end
24+
end
25+
26+
describe 'error handling' do
27+
context 'when bad_request' do
28+
it 'should raise OpenIDConnect::Forbidden' do
29+
mock_json :get, client.user_info_uri, 'errors/invalid_request', :HTTP_AUTHORIZATION => 'Bearer access_token', :status => 400 do
30+
expect { token.user_info! }.should raise_error OpenIDConnect::BadRequest
31+
end
32+
end
33+
end
34+
35+
context 'when unauthorized' do
36+
it 'should raise OpenIDConnect::Unauthorized' do
37+
mock_json :get, client.user_info_uri, 'errors/invalid_access_token', :HTTP_AUTHORIZATION => 'Bearer access_token', :status => 401 do
38+
expect { token.user_info! }.should raise_error OpenIDConnect::Unauthorized
39+
end
40+
end
41+
end
42+
43+
context 'when forbidden' do
44+
it 'should raise OpenIDConnect::Forbidden' do
45+
mock_json :get, client.user_info_uri, 'errors/insufficient_scope', :HTTP_AUTHORIZATION => 'Bearer access_token', :status => 403 do
46+
expect { token.user_info! }.should raise_error OpenIDConnect::Forbidden
47+
end
48+
end
49+
end
50+
51+
context 'when unknown' do
52+
it 'should raise OpenIDConnect::HttpError' do
53+
mock_json :get, client.user_info_uri, 'errors/unknown', :HTTP_AUTHORIZATION => 'Bearer access_token', :status => 500 do
54+
expect { token.user_info! }.should raise_error OpenIDConnect::HttpError
55+
end
56+
end
57+
end
58+
end
659
end
760

861
describe '#id_token!' do
9-
it :TODO
62+
it 'should return OpenIDConnect::ResponseObject::IdToken' do
63+
mock_json :get, client.introspection_uri, 'id_token', :HTTP_AUTHORIZATION => 'Bearer access_token' do
64+
token.id_token!.should be_a OpenIDConnect::ResponseObject::IdToken
65+
end
66+
end
1067
end
1168
end

spec/openid_connect/client_spec.rb

+33-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
describe OpenIDConnect::Client do
44
subject { client }
55
let(:client) { OpenIDConnect::Client.new attributes }
6+
let(:attributes) { required_attributes }
67
let :required_attributes do
78
{
89
:identifier => 'client_id'
@@ -23,7 +24,6 @@
2324
end
2425

2526
context 'otherwise' do
26-
let(:attributes) { required_attributes }
2727
[:authorization_uri, :introspection_uri, :user_info_uri].each do |endpoint|
2828
describe endpoint do
2929
it do
@@ -66,6 +66,37 @@
6666
end
6767

6868
describe '#access_token!' do
69-
it :TODO
69+
let :attributes do
70+
required_attributes.merge(
71+
:secret => 'client_secret',
72+
:token_endpoint => 'http://server.example.com/access_tokens'
73+
)
74+
end
75+
let :protocol_params do
76+
{
77+
:client_id => 'client_id',
78+
:client_secret => 'client_secret',
79+
:grant_type => 'authorization_code',
80+
:code => 'code'
81+
}
82+
end
83+
84+
context 'when bearer token is returned' do
85+
it 'should return OpenIDConnect::AccessToken' do
86+
mock_json :post, client.token_endpoint, 'access_token/bearer', :params => protocol_params do
87+
client.authorization_code = 'code'
88+
client.access_token!.should be_a OpenIDConnect::AccessToken
89+
end
90+
end
91+
end
92+
93+
context 'otherwise' do
94+
it 'should raise Unexpected Token Type exception' do
95+
mock_json :post, client.token_endpoint, 'access_token/mac', :params => protocol_params do
96+
client.authorization_code = 'code'
97+
expect { client.access_token! }.should raise_error OpenIDConnect::Exception, 'Unexpected Token Type: mac'
98+
end
99+
end
100+
end
70101
end
71102
end

spec/openid_connect/response_object/id_token_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
context 'otherwise' do
3030
let(:attributes) { required_attributes }
3131
it do
32-
expect { id_token.to_jwt }.should raise_error(OpenIDConnect::Exception, 'Secret Required')
32+
expect { id_token.to_jwt }.should raise_error OpenIDConnect::Exception, 'Secret Required'
3333
end
3434
end
3535
end

spec/spec_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
require 'rspec'
22
require 'openid_connect'
3+
4+
require 'helpers/webmock_helper'

0 commit comments

Comments
 (0)