Import refactored library. See MIGRATING.MD for details
This commit is contained in:
1 parent
9b7809174d
commit
f584a2cec8
373 files changed
+255715
-474
No files matched your search
@@ -0,0 +1,120 @@
|
||||
require 'spec_helper'
|
||||
|
||||
require 'google/api_client/auth/storage'
|
||||
|
||||
describe Google::APIClient::Storage do
|
||||
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')) }
|
||||
|
||||
let(:store) { double }
|
||||
let(:client_stub) { double }
|
||||
subject { Google::APIClient::Storage.new(store) }
|
||||
|
||||
describe 'authorize' do
|
||||
it 'should authorize' do
|
||||
expect(subject).to respond_to(:authorization)
|
||||
expect(subject.store).to be == store
|
||||
end
|
||||
end
|
||||
|
||||
describe 'authorize' do
|
||||
describe 'with credentials' do
|
||||
|
||||
it 'should initialize a new OAuth Client' do
|
||||
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)
|
||||
subject.authorize
|
||||
end
|
||||
|
||||
it 'should refresh authorization' do
|
||||
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)
|
||||
auth = subject.authorize
|
||||
expect(auth).to be == subject.authorization
|
||||
expect(auth).not_to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe 'without credentials' do
|
||||
|
||||
it 'should return nil' do
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'write_credentials' do
|
||||
it 'should call store to write credentials' do
|
||||
authorization_stub = double
|
||||
expect(authorization_stub).to receive(:refresh_token).and_return(true)
|
||||
expect(subject).to receive(:credentials_hash)
|
||||
expect(subject.store).to receive(:write_credentials)
|
||||
subject.write_credentials(authorization_stub)
|
||||
expect(subject.authorization).to be == authorization_stub
|
||||
end
|
||||
|
||||
it 'should not call store to write credentials' do
|
||||
expect(subject).not_to receive(:credentials_hash)
|
||||
expect(subject.store).not_to receive(:write_credentials)
|
||||
expect {
|
||||
subject.write_credentials()
|
||||
}.not_to raise_error
|
||||
end
|
||||
it 'should not call store to write credentials' do
|
||||
expect(subject).not_to receive(:credentials_hash)
|
||||
expect(subject.store).not_to receive(:write_credentials)
|
||||
expect {
|
||||
subject.write_credentials('something')
|
||||
}.not_to raise_error
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe 'refresh_authorization' do
|
||||
it 'should call refresh and write credentials' do
|
||||
expect(subject).to receive(:write_credentials)
|
||||
authorization_stub = double
|
||||
expect(subject).to receive(:authorization).and_return(authorization_stub)
|
||||
expect(authorization_stub).to receive(:refresh!).and_return(true)
|
||||
subject.refresh_authorization
|
||||
end
|
||||
end
|
||||
|
||||
describe 'load_credentials' do
|
||||
it 'should call store to load credentials' do
|
||||
expect(subject.store).to receive(:load_credentials)
|
||||
subject.send(:load_credentials)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'credentials_hash' do
|
||||
it 'should return an hash' do
|
||||
authorization_stub = double
|
||||
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)
|
||||
credentials = subject.send(:credentials_hash)
|
||||
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)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,39 @@
|
||||
require 'spec_helper'
|
||||
|
||||
require 'google/api_client/auth/storages/file_store'
|
||||
|
||||
describe Google::APIClient::FileStore do
|
||||
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')) }
|
||||
|
||||
let(:credentials_hash) {{
|
||||
"access_token"=>"my_access_token",
|
||||
"authorization_uri"=>"https://accounts.google.com/o/oauth2/auth",
|
||||
"client_id"=>"123456_test_client_id@.apps.googleusercontent.com",
|
||||
"client_secret"=>"123456_client_secret",
|
||||
"expires_in"=>3600,
|
||||
"refresh_token"=>"my_refresh_token",
|
||||
"token_credential_uri"=>"https://accounts.google.com/o/oauth2/token",
|
||||
"issued_at"=>1384440275
|
||||
}}
|
||||
|
||||
subject{Google::APIClient::FileStore.new('a file path')}
|
||||
|
||||
it 'should have a path' do
|
||||
expect(subject.path).to be == 'a file path'
|
||||
subject.path = 'an other file path'
|
||||
expect(subject.path).to be == 'an other file path'
|
||||
end
|
||||
|
||||
it 'should load credentials' do
|
||||
subject.path = json_file
|
||||
credentials = subject.load_credentials
|
||||
expect(credentials).to include('access_token', 'authorization_uri', 'refresh_token')
|
||||
end
|
||||
|
||||
it 'should write credentials' do
|
||||
io_stub = StringIO.new
|
||||
expect(subject).to receive(:open).and_return(io_stub)
|
||||
subject.write_credentials(credentials_hash)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,68 @@
|
||||
require 'spec_helper'
|
||||
|
||||
require 'google/api_client/auth/storages/redis_store'
|
||||
|
||||
describe Google::APIClient::RedisStore do
|
||||
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')) }
|
||||
let(:redis) {double}
|
||||
|
||||
let(:credentials_hash) { {
|
||||
"access_token" => "my_access_token",
|
||||
"authorization_uri" => "https://accounts.google.com/o/oauth2/auth",
|
||||
"client_id" => "123456_test_client_id@.apps.googleusercontent.com",
|
||||
"client_secret" => "123456_client_secret",
|
||||
"expires_in" => 3600,
|
||||
"refresh_token" => "my_refresh_token",
|
||||
"token_credential_uri" => "https://accounts.google.com/o/oauth2/token",
|
||||
"issued_at" => 1384440275
|
||||
} }
|
||||
|
||||
subject { Google::APIClient::RedisStore.new('a redis instance') }
|
||||
|
||||
it 'should have a redis instance' do
|
||||
expect(subject.redis).to be == 'a redis instance'
|
||||
subject.redis = 'an other redis instance'
|
||||
expect(subject.redis).to be == 'an other redis instance'
|
||||
end
|
||||
|
||||
describe 'load_credentials' do
|
||||
|
||||
it 'should load credentials' do
|
||||
subject.redis= redis
|
||||
expect(redis).to receive(:get).and_return(credentials_hash.to_json)
|
||||
expect(subject.load_credentials).to be == credentials_hash
|
||||
end
|
||||
|
||||
it 'should return nil' do
|
||||
subject.redis= redis
|
||||
expect(redis).to receive(:get).and_return(nil)
|
||||
expect(subject.load_credentials).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe 'redis_credentials_key' do
|
||||
context 'without given key' do
|
||||
it 'should return default key' do
|
||||
expect(subject.redis_credentials_key).to be == "google_api_credentials"
|
||||
end
|
||||
end
|
||||
context 'with given key' do
|
||||
let(:redis_store) { Google::APIClient::RedisStore.new('a redis instance', 'another_google_api_credentials') }
|
||||
it 'should use given key' do
|
||||
expect(redis_store.redis_credentials_key).to be == "another_google_api_credentials"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe 'write credentials' do
|
||||
|
||||
it 'should write credentials' do
|
||||
subject.redis= redis
|
||||
expect(redis).to receive(:set).and_return('ok')
|
||||
expect(subject.write_credentials(credentials_hash)).to be_truthy
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,53 @@
|
||||
# encoding:utf-8
|
||||
|
||||
# Copyright 2013 Google Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
require 'google/api_client/client_secrets'
|
||||
|
||||
FIXTURES_PATH = File.expand_path('../../../fixtures', __FILE__)
|
||||
|
||||
RSpec.describe Google::APIClient::ClientSecrets do
|
||||
|
||||
context 'with JSON file' do
|
||||
let(:file) { File.join(FIXTURES_PATH, 'files', 'client_secrets.json') }
|
||||
subject(:secrets) { Google::APIClient::ClientSecrets.load(file)}
|
||||
|
||||
it 'should load the correct client ID' do
|
||||
expect(secrets.client_id).to be == '898243283568.apps.googleusercontent.com'
|
||||
end
|
||||
|
||||
it 'should load the correct client secret' do
|
||||
expect(secrets.client_secret).to be == 'i8YaXdGgiQ4_KrTVNGsB7QP1'
|
||||
end
|
||||
|
||||
context 'serialzed to hash' do
|
||||
subject(:hash) { secrets.to_hash }
|
||||
it 'should contain the flow as the first key' do
|
||||
expect(hash).to have_key "installed"
|
||||
end
|
||||
|
||||
it 'should contain the client ID' do
|
||||
expect(hash["installed"]["client_id"]).to be == '898243283568.apps.googleusercontent.com'
|
||||
end
|
||||
|
||||
it 'should contain the client secret' do
|
||||
expect(hash["installed"]["client_secret"]).to be == 'i8YaXdGgiQ4_KrTVNGsB7QP1'
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user