From 7e42656b1c85bfff9c43425111c43d60351fafa3 Mon Sep 17 00:00:00 2001 From: Graham Paye Date: Wed, 10 Oct 2018 18:23:50 -0700 Subject: [PATCH] make tests windows friendly (#164) --- .../get_application_default_spec.rb | 26 ++++++++++++++----- spec/googleauth/service_account_spec.rb | 18 ++++++++++++- spec/googleauth/user_refresh_spec.rb | 17 +++++++++++- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/spec/googleauth/get_application_default_spec.rb b/spec/googleauth/get_application_default_spec.rb index 2794bc7..d1cf6ba 100644 --- a/spec/googleauth/get_application_default_spec.rb +++ b/spec/googleauth/get_application_default_spec.rb @@ -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 diff --git a/spec/googleauth/service_account_spec.rb b/spec/googleauth/service_account_spec.rb index 799b051..ef86aa0 100644 --- a/spec/googleauth/service_account_spec.rb +++ b/spec/googleauth/service_account_spec.rb @@ -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 diff --git a/spec/googleauth/user_refresh_spec.rb b/spec/googleauth/user_refresh_spec.rb index 9c7cffd..17068cf 100644 --- a/spec/googleauth/user_refresh_spec.rb +++ b/spec/googleauth/user_refresh_spec.rb @@ -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