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