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
22 @key = OpenSSL::PKey::RSA.new 2048
25 it 'should generate valid JWTs' do
26 asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
30 claim = JWT.decode(jwt, @key.public_key, true)
31 claim["iss"].should == 'client1'
32 claim["scope"].should == 'scope1 scope2'
35 it 'should send valid access token request' do
36 stubs = Faraday::Adapter::Test::Stubs.new do |stub|
37 stub.post('/o/oauth2/token') do |env|
38 params = Addressable::URI.form_unencode(env[:body])
39 JWT.decode(params.assoc("assertion").last, @key.public_key)
40 params.assoc("grant_type").should == ['grant_type','urn:ietf:params:oauth:grant-type:jwt-bearer']
42 "access_token" : "1/abcdef1234567890",
43 "token_type" : "Bearer",
48 connection = Faraday.new(:url => 'https://accounts.google.com') do |builder|
49 builder.adapter(:test, stubs)
52 asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
53 auth = asserter.authorize(nil, { :connection => connection})
54 auth.should_not == nil?
55 auth.access_token.should == "1/abcdef1234567890"