Updating dependencies.
[arvados.git] / spec / google / api_client_spec.rb
1 # Copyright 2010 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 gem 'faraday', '~> 0.8.1'
18 require 'faraday'
19 require 'faraday/utils'
20
21 gem 'signet', '~> 0.4.0'
22 require 'signet/oauth_1/client'
23
24 require 'google/api_client'
25 require 'google/api_client/version'
26
27 shared_examples_for 'configurable user agent' do
28   it 'should allow the user agent to be modified' do
29     @client.user_agent = 'Custom User Agent/1.2.3'
30     @client.user_agent.should == 'Custom User Agent/1.2.3'
31   end
32
33   it 'should allow the user agent to be set to nil' do
34     @client.user_agent = nil
35     @client.user_agent.should == nil
36   end
37
38   it 'should not allow the user agent to be used with bogus values' do
39     (lambda do
40       @client.user_agent = 42
41       @client.transmit(
42         ['GET', 'http://www.google.com/', [], []]
43       )
44     end).should raise_error(TypeError)
45   end
46
47   it 'should transmit a User-Agent header when sending requests' do
48     @client.user_agent = 'Custom User Agent/1.2.3'
49     request = Faraday::Request.new(:get) do |req|
50       req.url('http://www.google.com/')
51     end
52     stubs = Faraday::Adapter::Test::Stubs.new do |stub|
53       stub.get('/') do |env|
54         headers = env[:request_headers]
55         headers.should have_key('User-Agent')
56         headers['User-Agent'].should == @client.user_agent
57         [200, {}, ['']]
58       end
59     end
60     connection = Faraday.new(:url => 'https://www.google.com') do |builder|
61       builder.adapter(:test, stubs)
62     end
63     @client.transmit(:request => request, :connection => connection)
64     stubs.verify_stubbed_calls
65   end
66 end
67
68 describe Google::APIClient do
69   before do
70     @client = Google::APIClient.new
71   end
72
73   it 'should make its version number available' do
74     Google::APIClient::VERSION::STRING.should be_instance_of(String)
75   end
76
77   it 'should default to OAuth 2' do
78     Signet::OAuth2::Client.should === @client.authorization
79   end
80
81   it_should_behave_like 'configurable user agent'
82
83   describe 'configured for OAuth 1' do
84     before do
85       @client.authorization = :oauth_1
86     end
87
88     it 'should use the default OAuth1 client configuration' do
89       @client.authorization.temporary_credential_uri.to_s.should ==
90         'https://www.google.com/accounts/OAuthGetRequestToken'
91       @client.authorization.authorization_uri.to_s.should include(
92         'https://www.google.com/accounts/OAuthAuthorizeToken'
93       )
94       @client.authorization.token_credential_uri.to_s.should ==
95         'https://www.google.com/accounts/OAuthGetAccessToken'
96       @client.authorization.client_credential_key.should == 'anonymous'
97       @client.authorization.client_credential_secret.should == 'anonymous'
98     end
99
100     it_should_behave_like 'configurable user agent'
101   end
102
103   describe 'configured for OAuth 2' do
104     before do
105       @client.authorization = :oauth_2
106     end
107
108     # TODO
109     it_should_behave_like 'configurable user agent'
110   end
111   
112   describe 'when executing requests' do
113     before do
114       @client.authorization = :oauth_2
115       @connection = Faraday.new(:url => 'https://www.googleapis.com') do |builder|
116         stubs = Faraday::Adapter::Test::Stubs.new do |stub|
117           stub.get('/test') do |env|
118             env[:request_headers]['Authorization'].should == 'Bearer 12345'
119           end
120         end
121         builder.adapter(:test, stubs)
122       end
123     end
124      
125     it 'should use default authorization' do
126       @client.authorization.access_token = "12345"
127       @client.execute(:http_method => :get, 
128                       :uri => 'https://www.googleapis.com/test',
129                       :connection => @connection)
130     end
131
132     it 'should use request scoped authorization when provided' do
133       @client.authorization.access_token = "abcdef"
134       new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
135       @client.execute(:http_method => :get, 
136                       :uri => 'https://www.googleapis.com/test',
137                       :connection => @connection,
138                       :authorization => new_auth)
139     end
140   end  
141 end