1 # Copyright 2012 Google Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 require 'google/api_client'
19 describe Google::APIClient::JWTAsserter do
20 include ConnectionHelpers
23 @key = OpenSSL::PKey::RSA.new 2048
26 it 'should generate valid JWTs' do
27 asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
31 claim = JWT.decode(jwt, @key.public_key, true)
32 claim["iss"].should == 'client1'
33 claim["scope"].should == 'scope1 scope2'
36 it 'should send valid access token request' do
37 conn = stub_connection do |stub|
38 stub.post('/o/oauth2/token') do |env|
39 params = Addressable::URI.form_unencode(env[:body])
40 JWT.decode(params.assoc("assertion").last, @key.public_key)
41 params.assoc("grant_type").should == ['grant_type','urn:ietf:params:oauth:grant-type:jwt-bearer']
43 "access_token" : "1/abcdef1234567890",
44 "token_type" : "Bearer",
49 asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
50 auth = asserter.authorize(nil, { :connection => conn })
51 auth.should_not == nil?
52 auth.access_token.should == "1/abcdef1234567890"