2013-11-15 08:36:30 +00:00
|
|
|
require 'spec_helper'
|
2013-12-03 10:11:51 +00:00
|
|
|
|
|
|
|
require 'google/api_client'
|
|
|
|
require 'google/api_client/version'
|
2013-11-15 08:36:30 +00:00
|
|
|
|
|
|
|
describe Google::APIClient::Storage do
|
|
|
|
let(:client) { Google::APIClient.new(:application_name => 'API Client Tests') }
|
|
|
|
let(:root_path) { File.expand_path(File.join(__FILE__, '..', '..', '..')) }
|
|
|
|
let(:json_file) { File.expand_path(File.join(root_path, 'fixtures', 'files', 'auth_stored_credentials.json')) }
|
|
|
|
|
2013-11-18 10:39:58 +00:00
|
|
|
let(:store) { double }
|
|
|
|
let(:client_stub) { double }
|
|
|
|
subject { Google::APIClient::Storage.new(store) }
|
|
|
|
|
2013-11-15 08:36:30 +00:00
|
|
|
describe 'authorize' do
|
2013-11-18 10:39:58 +00:00
|
|
|
it 'should authorize' do
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(subject).to respond_to(:authorization)
|
|
|
|
expect(subject.store).to be == store
|
2013-11-18 10:39:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'authorize' do
|
|
|
|
describe 'with credentials' do
|
|
|
|
|
|
|
|
it 'should initialize a new OAuth Client' do
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(subject).to receive(:load_credentials).and_return({:first => 'a dummy'})
|
|
|
|
expect(client_stub).to receive(:issued_at=)
|
|
|
|
expect(client_stub).to receive(:expired?).and_return(false)
|
|
|
|
expect(Signet::OAuth2::Client).to receive(:new).and_return(client_stub)
|
|
|
|
expect(subject).not_to receive(:refresh_authorization)
|
2013-11-18 10:39:58 +00:00
|
|
|
subject.authorize
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should refresh authorization' do
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(subject).to receive(:load_credentials).and_return({:first => 'a dummy'})
|
|
|
|
expect(client_stub).to receive(:issued_at=)
|
|
|
|
expect(client_stub).to receive(:expired?).and_return(true)
|
|
|
|
expect(Signet::OAuth2::Client).to receive(:new).and_return(client_stub)
|
|
|
|
expect(subject).to receive(:refresh_authorization)
|
2013-12-03 07:30:28 +00:00
|
|
|
auth = subject.authorize
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(auth).to be == subject.authorization
|
|
|
|
expect(auth).not_to be_nil
|
2013-11-18 10:39:58 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-28 07:14:41 +00:00
|
|
|
|
2013-11-18 10:39:58 +00:00
|
|
|
describe 'without credentials' do
|
|
|
|
|
2013-12-03 07:30:28 +00:00
|
|
|
it 'should return nil' do
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(subject.authorization).to be_nil
|
|
|
|
expect(subject).to receive(:load_credentials).and_return({})
|
|
|
|
expect(subject.authorize).to be_nil
|
|
|
|
expect(subject.authorization).to be_nil
|
2013-11-18 10:39:58 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-15 08:36:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'write_credentials' do
|
2013-11-28 07:14:41 +00:00
|
|
|
it 'should call store to write credentials' do
|
|
|
|
authorization_stub = double
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(authorization_stub).to receive(:refresh_token).and_return(true)
|
|
|
|
expect(subject).to receive(:credentials_hash)
|
|
|
|
expect(subject.store).to receive(:write_credentials)
|
2013-11-28 07:14:41 +00:00
|
|
|
subject.write_credentials(authorization_stub)
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(subject.authorization).to be == authorization_stub
|
2013-11-28 07:14:41 +00:00
|
|
|
end
|
2013-11-15 08:36:30 +00:00
|
|
|
|
2013-11-28 07:14:41 +00:00
|
|
|
it 'should not call store to write credentials' do
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(subject).not_to receive(:credentials_hash)
|
|
|
|
expect(subject.store).not_to receive(:write_credentials)
|
2013-11-28 07:14:41 +00:00
|
|
|
expect {
|
|
|
|
subject.write_credentials()
|
|
|
|
}.not_to raise_error
|
|
|
|
end
|
|
|
|
it 'should not call store to write credentials' do
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(subject).not_to receive(:credentials_hash)
|
|
|
|
expect(subject.store).not_to receive(:write_credentials)
|
2013-11-28 07:14:41 +00:00
|
|
|
expect {
|
|
|
|
subject.write_credentials('something')
|
|
|
|
}.not_to raise_error
|
|
|
|
end
|
2013-11-15 08:36:30 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'refresh_authorization' do
|
2013-11-18 10:39:58 +00:00
|
|
|
it 'should call refresh and write credentials' do
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(subject).to receive(:write_credentials)
|
2013-11-18 10:39:58 +00:00
|
|
|
authorization_stub = double
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(subject).to receive(:authorization).and_return(authorization_stub)
|
|
|
|
expect(authorization_stub).to receive(:refresh!).and_return(true)
|
2013-11-18 10:39:58 +00:00
|
|
|
subject.refresh_authorization
|
|
|
|
end
|
2013-11-15 08:36:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'load_credentials' do
|
2013-11-28 07:14:41 +00:00
|
|
|
it 'should call store to load credentials' do
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(subject.store).to receive(:load_credentials)
|
2013-11-28 07:14:41 +00:00
|
|
|
subject.send(:load_credentials)
|
|
|
|
end
|
2013-11-15 08:36:30 +00:00
|
|
|
end
|
|
|
|
|
2013-11-28 07:14:41 +00:00
|
|
|
describe 'credentials_hash' do
|
|
|
|
it 'should return an hash' do
|
|
|
|
authorization_stub = double
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(authorization_stub).to receive(:access_token)
|
|
|
|
expect(authorization_stub).to receive(:client_id)
|
|
|
|
expect(authorization_stub).to receive(:client_secret)
|
|
|
|
expect(authorization_stub).to receive(:expires_in)
|
|
|
|
expect(authorization_stub).to receive(:refresh_token)
|
|
|
|
expect(authorization_stub).to receive(:issued_at).and_return('100')
|
|
|
|
allow(subject).to receive(:authorization).and_return(authorization_stub)
|
2013-11-28 07:14:41 +00:00
|
|
|
credentials = subject.send(:credentials_hash)
|
2014-12-15 21:32:54 +00:00
|
|
|
expect(credentials).to include(:access_token)
|
|
|
|
expect(credentials).to include(:authorization_uri)
|
|
|
|
expect(credentials).to include(:client_id)
|
|
|
|
expect(credentials).to include(:client_secret)
|
|
|
|
expect(credentials).to include(:expires_in)
|
|
|
|
expect(credentials).to include(:refresh_token)
|
|
|
|
expect(credentials).to include(:token_credential_uri)
|
|
|
|
expect(credentials).to include(:issued_at)
|
2013-11-28 07:14:41 +00:00
|
|
|
end
|
|
|
|
end
|
2013-11-15 08:36:30 +00:00
|
|
|
end
|