changes for ruby 1.8.7
[arvados.git] / spec / google / api_client / auth / storage_spec.rb
1 require 'spec_helper'
2
3 require 'google/api_client'
4 require 'google/api_client/version'
5
6 describe Google::APIClient::Storage do
7   let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') }
8   let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..')) }
9   let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) }
10
11   let(:store) { double }
12   let(:client_stub) { double }
13   subject { Google::APIClient::Storage.new(store) }
14
15   describe 'authorize' do
16     it 'should authorize' do
17       subject.should respond_to(:authorization)
18       subject.store.should == store
19     end
20   end
21
22   describe 'authorize' do
23     describe 'with credentials' do
24
25       it 'should initialize a new OAuth Client' do
26         subject.should_receive(:load_credentials).and_return({:first => 'a dummy'})
27         client_stub.stub(:issued_at=)
28         client_stub.stub(:expired?).and_return(false)
29         Signet::OAuth2::Client.should_receive(:new).and_return(client_stub)
30         subject.should_not_receive(:refresh_authorization)
31         subject.authorize
32       end
33
34       it 'should refresh authorization' do
35         subject.should_receive(:load_credentials).and_return({:first => 'a dummy'})
36         client_stub.stub(:issued_at=)
37         client_stub.stub(:expired?).and_return(true)
38         Signet::OAuth2::Client.should_receive(:new).and_return(client_stub)
39         subject.should_receive(:refresh_authorization)
40         auth = subject.authorize
41         auth.should == subject.authorization
42         auth.should_not be_nil
43       end
44     end
45
46     describe 'without credentials' do
47
48       it 'should return nil' do
49         subject.authorization.should be_nil
50         subject.should_receive(:load_credentials).and_return({})
51         subject.authorize.should be_nil
52         subject.authorization.should be_nil
53       end
54     end
55   end
56
57   describe 'write_credentials' do
58     it 'should call store to write credentials' do
59       authorization_stub = double
60       authorization_stub.should_receive(:refresh_token).and_return(true)
61       subject.should_receive(:credentials_hash)
62       subject.store.should_receive(:write_credentials)
63       subject.write_credentials(authorization_stub)
64       subject.authorization.should == authorization_stub
65     end
66
67     it 'should not call store to write credentials' do
68       subject.should_not_receive(:credentials_hash)
69       subject.store.should_not_receive(:write_credentials)
70       expect {
71         subject.write_credentials()
72       }.not_to raise_error
73     end
74     it 'should not call store to write credentials' do
75       subject.should_not_receive(:credentials_hash)
76       subject.store.should_not_receive(:write_credentials)
77       expect {
78         subject.write_credentials('something')
79       }.not_to raise_error
80     end
81
82   end
83
84   describe 'refresh_authorization' do
85     it 'should call refresh and write credentials' do
86       subject.should_receive(:write_credentials)
87       authorization_stub = double
88       subject.stub(:authorization).and_return(authorization_stub)
89       authorization_stub.should_receive(:refresh!).and_return(true)
90       subject.refresh_authorization
91     end
92   end
93
94   describe 'load_credentials' do
95     it 'should call store to load credentials' do
96       subject.store.should_receive(:load_credentials)
97       subject.send(:load_credentials)
98     end
99   end
100
101   describe 'credentials_hash' do
102     it 'should return an hash' do
103       authorization_stub = double
104       authorization_stub.should_receive(:access_token)
105       authorization_stub.should_receive(:client_id)
106       authorization_stub.should_receive(:client_secret)
107       authorization_stub.should_receive(:expires_in)
108       authorization_stub.should_receive(:refresh_token)
109       authorization_stub.should_receive(:issued_at).and_return('100')
110       subject.stub(:authorization).and_return(authorization_stub)
111       credentials = subject.send(:credentials_hash)
112       credentials.should include(:access_token)
113       credentials.should include(:authorization_uri)
114       credentials.should include(:client_id)
115       credentials.should include(:client_secret)
116       credentials.should include(:expires_in)
117       credentials.should include(:refresh_token)
118       credentials.should include(:token_credential_uri)
119       credentials.should include(:issued_at)
120     end
121   end
122 end