Add test for options as 5th param in array style calls
[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 require 'faraday'
18 require 'faraday/utils'
19 require 'signet/oauth_1/client'
20 require 'google/api_client'
21 require 'google/api_client/version'
22
23 shared_examples_for 'configurable user agent' do
24   include ConnectionHelpers
25   
26   it 'should allow the user agent to be modified' do
27     client.user_agent = 'Custom User Agent/1.2.3'
28     client.user_agent.should == 'Custom User Agent/1.2.3'
29   end
30
31   it 'should allow the user agent to be set to nil' do
32     client.user_agent = nil
33     client.user_agent.should == nil
34   end
35
36   it 'should not allow the user agent to be used with bogus values' do
37     (lambda do
38       client.user_agent = 42
39       client.execute(:uri=>'http://www.google.com/')
40     end).should raise_error(TypeError)
41   end
42
43   it 'should transmit a User-Agent header when sending requests' do
44     client.user_agent = 'Custom User Agent/1.2.3'
45
46     conn = stub_connection do |stub|
47       stub.get('/') do |env|
48         headers = env[:request_headers]
49         headers.should have_key('User-Agent')
50         headers['User-Agent'].should == client.user_agent
51         [200, {}, ['']]
52       end
53     end
54     client.execute(:uri=>'http://www.google.com/', :connection => conn)
55     conn.verify
56   end
57 end
58
59 describe Google::APIClient do
60   include ConnectionHelpers
61
62   let(:client) { Google::APIClient.new }
63
64   it 'should make its version number available' do
65     Google::APIClient::VERSION::STRING.should be_instance_of(String)
66   end
67
68   it 'should default to OAuth 2' do
69     Signet::OAuth2::Client.should === client.authorization
70   end
71
72   describe 'configure for no authentication' do
73     before do
74       client.authorization = nil
75     end
76     it_should_behave_like 'configurable user agent'
77   end
78     
79   describe 'configured for OAuth 1' do
80     before do
81       client.authorization = :oauth_1
82       client.authorization.token_credential_key = 'abc'
83       client.authorization.token_credential_secret = '123'
84     end
85
86     it 'should use the default OAuth1 client configuration' do
87       client.authorization.temporary_credential_uri.to_s.should ==
88         'https://www.google.com/accounts/OAuthGetRequestToken'
89       client.authorization.authorization_uri.to_s.should include(
90         'https://www.google.com/accounts/OAuthAuthorizeToken'
91       )
92       client.authorization.token_credential_uri.to_s.should ==
93         'https://www.google.com/accounts/OAuthGetAccessToken'
94       client.authorization.client_credential_key.should == 'anonymous'
95       client.authorization.client_credential_secret.should == 'anonymous'
96     end
97
98     it_should_behave_like 'configurable user agent'
99   end
100
101   describe 'configured for OAuth 2' do
102     before do
103       client.authorization = :oauth_2
104       client.authorization.access_token = '12345'
105     end
106
107     # TODO
108     it_should_behave_like 'configurable user agent'
109   end
110   
111   describe 'when executing requests' do
112     before do
113       @prediction = client.discovered_api('prediction', 'v1.2')
114       client.authorization = :oauth_2
115       @connection = stub_connection do |stub|
116         stub.post('/prediction/v1.2/training?data=12345') do |env|
117           env[:request_headers]['Authorization'].should == 'Bearer 12345'
118         end
119       end
120     end
121
122     after do
123       @connection.verify
124     end
125     
126     it 'should use default authorization' do
127       client.authorization.access_token = "12345"
128       client.execute(  
129         :api_method => @prediction.training.insert,
130         :parameters => {'data' => '12345'},
131         :connection => @connection
132       )
133     end
134
135     it 'should use request scoped authorization when provided' do
136       client.authorization.access_token = "abcdef"
137       new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
138       client.execute(  
139         :api_method => @prediction.training.insert,
140         :parameters => {'data' => '12345'},
141         :authorization => new_auth,
142         :connection => @connection
143       )
144     end
145     
146     it 'should accept options in array style execute' do
147        client.authorization.access_token = "abcdef"
148        new_auth = Signet::OAuth2::Client.new(:access_token => '12345')
149        client.execute(  
150          @prediction.training.insert, {'data' => '12345'}, '', {},
151          { :authorization => new_auth, :connection => @connection }         
152        )
153      end
154   end  
155 end