Switched to using RSpec's let method for memoization.
[arvados.git] / spec / google / api_client / service_account_spec.rb
1 # Copyright 2012 Google Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 require 'spec_helper'
16
17 require 'google/api_client'
18
19 describe Google::APIClient::JWTAsserter do
20
21   before do
22     @key = OpenSSL::PKey::RSA.new 2048
23   end
24
25   it 'should generate valid JWTs' do
26     asserter = Google::APIClient::JWTAsserter.new('client1', 'scope1 scope2', @key)
27     jwt = asserter.to_jwt
28     jwt.should_not == nil
29
30     claim = JWT.decode(jwt, @key.public_key, true)
31     claim["iss"].should == 'client1'
32     claim["scope"].should == 'scope1 scope2'
33   end
34
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']
41         [200, {}, '{
42           "access_token" : "1/abcdef1234567890",
43           "token_type" : "Bearer",
44           "expires_in" : 3600
45         }']
46       end
47     end
48     connection = Faraday.new(:url => 'https://accounts.google.com') do |builder|
49       builder.adapter(:test, stubs)
50     end
51
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"
56   end
57 end
58