Merge branch 'main' from workbench2.git
[arvados.git] / sdk / ruby-google-api-client / 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       expect(subject).to respond_to(:authorization)
18       expect(subject.store).to be == 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         expect(subject).to receive(:load_credentials).and_return({:first => 'a dummy'})
27         expect(client_stub).to receive(:issued_at=)
28         expect(client_stub).to receive(:expired?).and_return(false)
29         expect(Signet::OAuth2::Client).to receive(:new).and_return(client_stub)
30         expect(subject).not_to receive(:refresh_authorization)
31         subject.authorize
32       end
33
34       it 'should refresh authorization' do
35         expect(subject).to receive(:load_credentials).and_return({:first => 'a dummy'})
36         expect(client_stub).to receive(:issued_at=)
37         expect(client_stub).to receive(:expired?).and_return(true)
38         expect(Signet::OAuth2::Client).to receive(:new).and_return(client_stub)
39         expect(subject).to receive(:refresh_authorization)
40         auth = subject.authorize
41         expect(auth).to be == subject.authorization
42         expect(auth).not_to be_nil
43       end
44     end
45
46     describe 'without credentials' do
47
48       it 'should return nil' do
49         expect(subject.authorization).to be_nil
50         expect(subject).to receive(:load_credentials).and_return({})
51         expect(subject.authorize).to be_nil
52         expect(subject.authorization).to 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       expect(authorization_stub).to receive(:refresh_token).and_return(true)
61       expect(subject).to receive(:credentials_hash)
62       expect(subject.store).to receive(:write_credentials)
63       subject.write_credentials(authorization_stub)
64       expect(subject.authorization).to be == authorization_stub
65     end
66
67     it 'should not call store to write credentials' do
68       expect(subject).not_to receive(:credentials_hash)
69       expect(subject.store).not_to 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       expect(subject).not_to receive(:credentials_hash)
76       expect(subject.store).not_to 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       expect(subject).to receive(:write_credentials)
87       authorization_stub = double
88       expect(subject).to receive(:authorization).and_return(authorization_stub)
89       expect(authorization_stub).to 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       expect(subject.store).to 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       expect(authorization_stub).to receive(:access_token)
105       expect(authorization_stub).to receive(:client_id)
106       expect(authorization_stub).to receive(:client_secret)
107       expect(authorization_stub).to receive(:expires_in)
108       expect(authorization_stub).to receive(:refresh_token)
109       expect(authorization_stub).to receive(:issued_at).and_return('100')
110       allow(subject).to receive(:authorization).and_return(authorization_stub)
111       credentials = subject.send(:credentials_hash)
112       expect(credentials).to include(:access_token)
113       expect(credentials).to include(:authorization_uri)
114       expect(credentials).to include(:client_id)
115       expect(credentials).to include(:client_secret)
116       expect(credentials).to include(:expires_in)
117       expect(credentials).to include(:refresh_token)
118       expect(credentials).to include(:token_credential_uri)
119       expect(credentials).to include(:issued_at)
120     end
121   end
122 end