make tests windows friendly (#164)

This commit is contained in:
Graham Paye 2018-10-10 18:23:50 -07:00 committed by GitHub
parent 6d9d63be3e
commit 7e42656b1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 9 deletions

View File

@ -50,12 +50,16 @@ describe '#get_application_default' do
@original_env_vals = {}
@credential_vars.each { |var| @original_env_vals[var] = ENV[var] }
@home = ENV['HOME']
@app_data = ENV['APPDATA']
@program_data = ENV['ProgramData']
@scope = 'https://www.googleapis.com/auth/userinfo.profile'
end
after(:example) do
@credential_vars.each { |var| ENV[var] = @original_env_vals[var] }
ENV['HOME'] = @home unless @home == ENV['HOME']
ENV['APPDATA'] = @app_data unless @app_data == ENV['APPDATA']
ENV['ProgramData'] = @program_data unless @program_data == ENV['ProgramData']
end
shared_examples 'it cannot load misconfigured credentials' do
@ -90,7 +94,7 @@ describe '#get_application_default' do
FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text)
ENV[@var_name] = key_path
expect(Google::Auth.get_application_default @scope, options)
expect(Google::Auth.get_application_default(@scope, options))
.to_not be_nil
end
end
@ -99,10 +103,12 @@ describe '#get_application_default' do
ENV.delete(@var_name) unless ENV[@var_name].nil?
Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', WELL_KNOWN_PATH)
key_path = File.join(dir, WELL_KNOWN_PATH) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text)
ENV['HOME'] = dir
expect(Google::Auth.get_application_default @scope, options)
ENV['APPDATA'] = dir
expect(Google::Auth.get_application_default(@scope, options))
.to_not be_nil
end
end
@ -111,10 +117,12 @@ describe '#get_application_default' do
ENV.delete(@var_name) unless ENV[@var_name].nil?
Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', WELL_KNOWN_PATH)
key_path = File.join(dir, WELL_KNOWN_PATH) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text)
ENV['HOME'] = dir
expect(Google::Auth.get_application_default nil, options).to_not be_nil
ENV['APPDATA'] = dir
expect(Google::Auth.get_application_default(nil, options)).to_not be_nil
end
end
@ -134,10 +142,12 @@ describe '#get_application_default' do
it 'succeeds with system default file' do
ENV.delete(@var_name) unless ENV[@var_name].nil?
FakeFS do
key_path = File.join('/etc/google/auth/', CREDENTIALS_FILE_NAME)
ENV['ProgramData'] = '/etc'
prefix = OS.windows? ? '/etc/Google/Auth/' : '/etc/google/auth/'
key_path = File.join(prefix, CREDENTIALS_FILE_NAME)
FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text)
expect(Google::Auth.get_application_default @scope, options)
expect(Google::Auth.get_application_default(@scope, options))
.to_not be_nil
File.delete(key_path)
end
@ -151,7 +161,7 @@ describe '#get_application_default' do
ENV[CLIENT_SECRET_VAR] = cred_json[:client_secret]
ENV[REFRESH_TOKEN_VAR] = cred_json[:refresh_token]
ENV[ACCOUNT_TYPE_VAR] = cred_json[:type]
expect(Google::Auth.get_application_default @scope, options)
expect(Google::Auth.get_application_default(@scope, options))
.to_not be_nil
end
@ -164,7 +174,7 @@ describe '#get_application_default' do
ENV[REFRESH_TOKEN_VAR] = cred_json[:refresh_token]
ENV[ACCOUNT_TYPE_VAR] = cred_json[:type]
expect { Google::Auth.get_application_default @scope, options }.to output(
Google::Auth::CredentialsLoader::CLOUD_SDK_CREDENTIALS_WARNING + "\n"
Google::Auth::CredentialsLoader::CLOUD_SDK_CREDENTIALS_WARNING + "\n"
).to_stderr
end
end
@ -238,9 +248,11 @@ describe '#get_application_default' do
ENV.delete(@var_name) unless ENV[@var_name].nil?
Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', WELL_KNOWN_PATH)
key_path = File.join(dir, WELL_KNOWN_PATH) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text)
ENV['HOME'] = dir
ENV['APPDATA'] = dir
expect do
Google::Auth.get_application_default @scope, options
end.to raise_error RuntimeError

View File

@ -40,6 +40,7 @@ require 'multi_json'
require 'openssl'
require 'spec_helper'
require 'tmpdir'
require 'os'
include Google::Auth::CredentialsLoader
@ -223,6 +224,7 @@ describe Google::Auth::ServiceAccountCredentials do
describe '#from_well_known_path' do
before(:example) do
@home = ENV['HOME']
@app_data = ENV['APPDATA']
@scope = 'https://www.googleapis.com/auth/userinfo.profile'
@known_path = WELL_KNOWN_PATH
@clz = ServiceAccountCredentials
@ -230,6 +232,7 @@ describe Google::Auth::ServiceAccountCredentials do
after(:example) do
ENV['HOME'] = @home unless @home == ENV['HOME']
ENV['APPDATA'] = @app_data unless @app_data == ENV['APPDATA']
end
it 'is nil if no file exists' do
@ -240,9 +243,11 @@ describe Google::Auth::ServiceAccountCredentials do
it 'successfully loads the file when it is present' do
Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', @known_path)
key_path = File.join(dir, WELL_KNOWN_PATH) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text)
ENV['HOME'] = dir
ENV['APPDATA'] = dir
expect(@clz.from_well_known_path(@scope)).to_not be_nil
end
end
@ -251,10 +256,16 @@ describe Google::Auth::ServiceAccountCredentials do
describe '#from_system_default_path' do
before(:example) do
@scope = 'https://www.googleapis.com/auth/userinfo.profile'
@path = File.join('/etc/google/auth/', CREDENTIALS_FILE_NAME)
@program_data = ENV['ProgramData']
@prefix = OS.windows? ? '/etc/Google/Auth/' : '/etc/google/auth/'
@path = File.join(@prefix, CREDENTIALS_FILE_NAME)
@clz = ServiceAccountCredentials
end
after(:example) do
ENV['ProgramData'] = @program_data
end
it 'is nil if no file exists' do
FakeFS do
expect(ServiceAccountCredentials.from_system_default_path(@scope))
@ -264,6 +275,7 @@ describe Google::Auth::ServiceAccountCredentials do
it 'successfully loads the file when it is present' do
FakeFS do
ENV['ProgramData'] = '/etc'
FileUtils.mkdir_p(File.dirname(@path))
File.write(@path, cred_json_text)
expect(@clz.from_system_default_path(@scope)).to_not be_nil
@ -351,10 +363,12 @@ describe Google::Auth::ServiceAccountJwtHeaderCredentials do
describe '#from_well_known_path' do
before(:example) do
@home = ENV['HOME']
@app_data = ENV['APPDATA']
end
after(:example) do
ENV['HOME'] = @home unless @home == ENV['HOME']
ENV['APPDATA'] = @app_data unless @app_data == ENV['APPDATA']
end
it 'is nil if no file exists' do
@ -365,9 +379,11 @@ describe Google::Auth::ServiceAccountJwtHeaderCredentials do
it 'successfully loads the file when it is present' do
Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', WELL_KNOWN_PATH)
key_path = File.join(dir, WELL_KNOWN_PATH) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text)
ENV['HOME'] = dir
ENV['APPDATA'] = dir
expect(clz.from_well_known_path).to_not be_nil
end
end

View File

@ -40,6 +40,7 @@ require 'multi_json'
require 'openssl'
require 'spec_helper'
require 'tmpdir'
require 'os'
include Google::Auth::CredentialsLoader
@ -154,6 +155,7 @@ describe Google::Auth::UserRefreshCredentials do
describe '#from_well_known_path' do
before(:example) do
@home = ENV['HOME']
@app_data = ENV['APPDATA']
@scope = 'https://www.googleapis.com/auth/userinfo.profile'
@known_path = WELL_KNOWN_PATH
@clz = UserRefreshCredentials
@ -161,6 +163,7 @@ describe Google::Auth::UserRefreshCredentials do
after(:example) do
ENV['HOME'] = @home unless @home == ENV['HOME']
ENV['APPDATA'] = @app_data unless @app_data == ENV['APPDATA']
end
it 'is nil if no file exists' do
@ -173,9 +176,11 @@ describe Google::Auth::UserRefreshCredentials do
needed.each do |missing|
Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', @known_path)
key_path = File.join(dir, @known_path) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text(missing))
ENV['HOME'] = dir
ENV['APPDATA'] = dir
expect { @clz.from_well_known_path(@scope) }
.to raise_error RuntimeError
end
@ -185,9 +190,11 @@ describe Google::Auth::UserRefreshCredentials do
it 'successfully loads the file when it is present' do
Dir.mktmpdir do |dir|
key_path = File.join(dir, '.config', @known_path)
key_path = File.join(dir, @known_path) if OS.windows?
FileUtils.mkdir_p(File.dirname(key_path))
File.write(key_path, cred_json_text)
ENV['HOME'] = dir
ENV['APPDATA'] = dir
expect(@clz.from_well_known_path(@scope)).to_not be_nil
end
end
@ -196,10 +203,16 @@ describe Google::Auth::UserRefreshCredentials do
describe '#from_system_default_path' do
before(:example) do
@scope = 'https://www.googleapis.com/auth/userinfo.profile'
@path = File.join('/etc/google/auth/', CREDENTIALS_FILE_NAME)
@prefix = OS.windows? ? '/etc/Google/Auth/' : '/etc/google/auth/'
@path = File.join(@prefix, CREDENTIALS_FILE_NAME)
@program_data = ENV['ProgramData']
@clz = UserRefreshCredentials
end
after(:example) do
ENV['ProgramData'] = @program_data
end
it 'is nil if no file exists' do
FakeFS do
expect(UserRefreshCredentials.from_system_default_path(@scope))
@ -211,6 +224,7 @@ describe Google::Auth::UserRefreshCredentials do
needed = %w(client_id client_secret refresh_token)
needed.each do |missing|
FakeFS do
ENV['ProgramData'] = '/etc'
FileUtils.mkdir_p(File.dirname(@path))
File.write(@path, cred_json_text(missing))
expect { @clz.from_system_default_path(@scope) }
@ -222,6 +236,7 @@ describe Google::Auth::UserRefreshCredentials do
it 'successfully loads the file when it is present' do
FakeFS do
ENV['ProgramData'] = '/etc'
FileUtils.mkdir_p(File.dirname(@path))
File.write(@path, cred_json_text)
expect(@clz.from_system_default_path(@scope)).to_not be_nil