2 require_relative '../../../../lib/google/api_client'
3 require_relative '../../../../lib/google/api_client/auth/storage'
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')) }
10 let(:store) { double }
11 let(:client_stub) { double }
12 subject { Google::APIClient::Storage.new(store) }
14 it 'should initialize'
16 describe 'authorize' do
17 it 'should authorize' do
18 subject.should respond_to(:authorization)
19 subject.store.should == store
23 describe 'authorize' do
24 describe 'with credentials' do
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)
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 auth = subject.authorize
42 auth.should == subject.authorization
43 auth.should_not be_nil
47 describe 'without credentials' do
49 it 'should return nil' do
50 subject.authorization.should be_nil
51 subject.should_receive(:load_credentials).and_return({})
52 subject.authorize.should be_nil
53 subject.authorization.should be_nil
58 describe 'write_credentials' do
59 it 'should call store to write credentials' do
60 authorization_stub = double
61 authorization_stub.should_receive(:refresh_token).and_return(true)
62 subject.should_receive(:credentials_hash)
63 subject.store.should_receive(:write_credentials)
64 subject.write_credentials(authorization_stub)
65 subject.authorization.should == authorization_stub
68 it 'should not call store to write credentials' do
69 subject.should_not_receive(:credentials_hash)
70 subject.store.should_not_receive(:write_credentials)
72 subject.write_credentials()
75 it 'should not call store to write credentials' do
76 subject.should_not_receive(:credentials_hash)
77 subject.store.should_not_receive(:write_credentials)
79 subject.write_credentials('something')
85 describe 'refresh_authorization' do
86 it 'should call refresh and write credentials' do
87 subject.should_receive(:write_credentials)
88 authorization_stub = double
89 subject.stub(:authorization).and_return(authorization_stub)
90 authorization_stub.should_receive(:refresh!).and_return(true)
91 subject.refresh_authorization
95 describe 'load_credentials' do
96 it 'should call store to load credentials' do
97 subject.store.should_receive(:load_credentials)
98 subject.send(:load_credentials)
102 describe 'credentials_hash' do
103 it 'should return an hash' do
104 authorization_stub = double
105 authorization_stub.should_receive(:access_token)
106 authorization_stub.should_receive(:client_id)
107 authorization_stub.should_receive(:client_secret)
108 authorization_stub.should_receive(:expires_in)
109 authorization_stub.should_receive(:refresh_token)
110 authorization_stub.should_receive(:issued_at).and_return('100')
111 subject.stub(:authorization).and_return(authorization_stub)
112 credentials = subject.send(:credentials_hash)
113 credentials.should include(:access_token)
114 credentials.should include(:authorization_uri)
115 credentials.should include(:client_id)
116 credentials.should include(:client_secret)
117 credentials.should include(:expires_in)
118 credentials.should include(:refresh_token)
119 credentials.should include(:token_credential_uri)
120 credentials.should include(:issued_at)