added ruby 2.3 compatability
also: * updated rubocop and applied styling * made small test refactorings (using the expect block notation) * made a small refactor to remove an unnecessary ``return credentials`` * bumped the version
This commit is contained in:
1 parent
72fb8df541
commit
82a4f8520c
25 files changed
+199
-160
No files matched your search
@@ -62,7 +62,8 @@ shared_examples 'apply/apply! are OK' do
|
||||
@client.on_refresh(&b)
|
||||
@client.fetch_access_token!
|
||||
end.to yield_with_args(have_attributes(
|
||||
access_token: '1/abcdef1234567890'))
|
||||
access_token: '1/abcdef1234567890'
|
||||
))
|
||||
expect(stub).to have_been_requested
|
||||
end
|
||||
end
|
||||
|
||||
@@ -104,7 +104,8 @@ describe Google::Auth::ClientId do
|
||||
|
||||
it 'should raise error' do
|
||||
expect { Google::Auth::ClientId.from_hash(config) }.to raise_error(
|
||||
/Expected top level property/)
|
||||
/Expected top level property/
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -119,7 +120,8 @@ describe Google::Auth::ClientId do
|
||||
|
||||
it 'should raise error' do
|
||||
expect { Google::Auth::ClientId.from_hash(config) }.to raise_error(
|
||||
/Client id can not be nil/)
|
||||
/Client id can not be nil/
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -134,7 +136,8 @@ describe Google::Auth::ClientId do
|
||||
|
||||
it 'should raise error' do
|
||||
expect { Google::Auth::ClientId.from_hash(config) }.to raise_error(
|
||||
/Client secret can not be nil/)
|
||||
/Client secret can not be nil/
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -37,7 +37,7 @@ require 'googleauth/compute_engine'
|
||||
require 'spec_helper'
|
||||
|
||||
describe Google::Auth::GCECredentials do
|
||||
MD_URI = 'http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token'
|
||||
MD_URI = 'http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token'.freeze
|
||||
GCECredentials = Google::Auth::GCECredentials
|
||||
|
||||
before(:example) do
|
||||
@@ -64,8 +64,8 @@ describe Google::Auth::GCECredentials do
|
||||
stub = stub_request(:get, MD_URI)
|
||||
.to_return(status: 404,
|
||||
headers: { 'Metadata-Flavor' => 'Google' })
|
||||
blk = proc { @client.fetch_access_token! }
|
||||
expect(&blk).to raise_error Signet::AuthorizationError
|
||||
expect { @client.fetch_access_token! }
|
||||
.to raise_error Signet::AuthorizationError
|
||||
expect(stub).to have_been_requested
|
||||
end
|
||||
|
||||
@@ -73,8 +73,8 @@ describe Google::Auth::GCECredentials do
|
||||
stub = stub_request(:get, MD_URI)
|
||||
.to_return(status: 503,
|
||||
headers: { 'Metadata-Flavor' => 'Google' })
|
||||
blk = proc { @client.fetch_access_token! }
|
||||
expect(&blk).to raise_error Signet::AuthorizationError
|
||||
expect { @client.fetch_access_token! }
|
||||
.to raise_error Signet::AuthorizationError
|
||||
expect(stub).to have_been_requested
|
||||
end
|
||||
end
|
||||
|
||||
@@ -45,7 +45,8 @@ describe '#get_application_default' do
|
||||
@var_name = ENV_VAR
|
||||
@credential_vars = [
|
||||
ENV_VAR, PRIVATE_KEY_VAR, CLIENT_EMAIL_VAR, CLIENT_ID_VAR,
|
||||
CLIENT_SECRET_VAR, REFRESH_TOKEN_VAR, ACCOUNT_TYPE_VAR]
|
||||
CLIENT_SECRET_VAR, REFRESH_TOKEN_VAR, ACCOUNT_TYPE_VAR
|
||||
]
|
||||
@original_env_vals = {}
|
||||
@credential_vars.each { |var| @original_env_vals[var] = ENV[var] }
|
||||
@home = ENV['HOME']
|
||||
@@ -74,10 +75,9 @@ describe '#get_application_default' do
|
||||
Dir.mktmpdir do |dir|
|
||||
ENV.delete(@var_name) unless ENV[@var_name].nil? # no env var
|
||||
ENV['HOME'] = dir # no config present in this tmp dir
|
||||
blk = proc do
|
||||
expect do
|
||||
Google::Auth.get_application_default(@scope, options)
|
||||
end
|
||||
expect(&blk).to raise_error RuntimeError
|
||||
end.to raise_error RuntimeError
|
||||
end
|
||||
expect(stub).to have_been_requested
|
||||
end
|
||||
@@ -215,10 +215,9 @@ describe '#get_application_default' do
|
||||
FileUtils.mkdir_p(File.dirname(key_path))
|
||||
File.write(key_path, cred_json_text)
|
||||
ENV[@var_name] = key_path
|
||||
blk = proc do
|
||||
expect do
|
||||
Google::Auth.get_application_default(@scope, options)
|
||||
end
|
||||
expect(&blk).to raise_error RuntimeError
|
||||
end.to raise_error RuntimeError
|
||||
end
|
||||
end
|
||||
|
||||
@@ -229,20 +228,18 @@ describe '#get_application_default' do
|
||||
FileUtils.mkdir_p(File.dirname(key_path))
|
||||
File.write(key_path, cred_json_text)
|
||||
ENV['HOME'] = dir
|
||||
blk = proc do
|
||||
expect do
|
||||
Google::Auth.get_application_default(@scope, options)
|
||||
end
|
||||
expect(&blk).to raise_error RuntimeError
|
||||
end.to raise_error RuntimeError
|
||||
end
|
||||
end
|
||||
|
||||
it 'fails if env vars are set' do
|
||||
ENV[PRIVATE_KEY_VAR] = cred_json[:private_key]
|
||||
ENV[CLIENT_EMAIL_VAR] = cred_json[:client_email]
|
||||
blk = proc do
|
||||
expect do
|
||||
Google::Auth.get_application_default(@scope, options)
|
||||
end
|
||||
expect(&blk).to raise_error RuntimeError
|
||||
end.to raise_error RuntimeError
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -39,13 +39,15 @@ describe Google::Auth::ScopeUtil do
|
||||
|
||||
it 'normalizes the email scope' do
|
||||
expect(normalized).to include(
|
||||
'https://www.googleapis.com/auth/userinfo.email')
|
||||
'https://www.googleapis.com/auth/userinfo.email'
|
||||
)
|
||||
expect(normalized).to_not include 'email'
|
||||
end
|
||||
|
||||
it 'normalizes the profile scope' do
|
||||
expect(normalized).to include(
|
||||
'https://www.googleapis.com/auth/userinfo.profile')
|
||||
'https://www.googleapis.com/auth/userinfo.profile'
|
||||
)
|
||||
expect(normalized).to_not include 'profile'
|
||||
end
|
||||
|
||||
|
||||
@@ -139,7 +139,8 @@ describe Google::Auth::ServiceAccountCredentials do
|
||||
end
|
||||
stub_request(:post, 'https://www.googleapis.com/oauth2/v3/token')
|
||||
.with(body: hash_including(
|
||||
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer'),
|
||||
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer'
|
||||
),
|
||||
&blk)
|
||||
.to_return(body: body,
|
||||
status: 200,
|
||||
@@ -164,7 +165,8 @@ describe Google::Auth::ServiceAccountCredentials do
|
||||
before(:example) do
|
||||
@var_name = ENV_VAR
|
||||
@credential_vars = [
|
||||
ENV_VAR, PRIVATE_KEY_VAR, CLIENT_EMAIL_VAR, ACCOUNT_TYPE_VAR]
|
||||
ENV_VAR, PRIVATE_KEY_VAR, CLIENT_EMAIL_VAR, ACCOUNT_TYPE_VAR
|
||||
]
|
||||
@original_env_vals = {}
|
||||
@credential_vars.each { |var| @original_env_vals[var] = ENV[var] }
|
||||
ENV[ACCOUNT_TYPE_VAR] = cred_json[:type]
|
||||
@@ -294,7 +296,8 @@ describe Google::Auth::ServiceAccountJwtHeaderCredentials do
|
||||
before(:example) do
|
||||
@var_name = ENV_VAR
|
||||
@credential_vars = [
|
||||
ENV_VAR, PRIVATE_KEY_VAR, CLIENT_EMAIL_VAR, ACCOUNT_TYPE_VAR]
|
||||
ENV_VAR, PRIVATE_KEY_VAR, CLIENT_EMAIL_VAR, ACCOUNT_TYPE_VAR
|
||||
]
|
||||
@original_env_vals = {}
|
||||
@credential_vars.each { |var| @original_env_vals[var] = ENV[var] }
|
||||
ENV[ACCOUNT_TYPE_VAR] = cred_json[:type]
|
||||
|
||||
@@ -45,7 +45,8 @@ describe Signet::OAuth2::Client do
|
||||
scope: 'https://www.googleapis.com/auth/userinfo.profile',
|
||||
issuer: 'app@example.com',
|
||||
audience: 'https://accounts.google.com/o/oauth2/token',
|
||||
signing_key: @key)
|
||||
signing_key: @key
|
||||
)
|
||||
end
|
||||
|
||||
def make_auth_stubs(opts)
|
||||
@@ -60,8 +61,8 @@ describe Signet::OAuth2::Client do
|
||||
end
|
||||
stub_request(:post, 'https://accounts.google.com/o/oauth2/token')
|
||||
.with(body: hash_including(
|
||||
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer'),
|
||||
&blk)
|
||||
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer'
|
||||
), &blk)
|
||||
.to_return(body: body,
|
||||
status: 200,
|
||||
headers: { 'Content-Type' => 'application/json' })
|
||||
|
||||
@@ -82,7 +82,8 @@ describe Google::Auth::UserAuthorizer do
|
||||
|
||||
it 'should include the callback uri' do
|
||||
expect(URI(uri).query).to match(
|
||||
%r{redirect_uri=https://www.example.com/oauth/callback})
|
||||
%r{redirect_uri=https://www.example.com/oauth/callback}
|
||||
)
|
||||
end
|
||||
|
||||
it 'should include the scope' do
|
||||
@@ -139,7 +140,8 @@ describe Google::Auth::UserAuthorizer do
|
||||
MultiJson.dump(
|
||||
access_token: 'accesstoken',
|
||||
refresh_token: 'refreshtoken',
|
||||
expiration_time_millis: 1_441_234_742_000)
|
||||
expiration_time_millis: 1_441_234_742_000
|
||||
)
|
||||
end
|
||||
|
||||
context 'with a valid user id' do
|
||||
@@ -150,7 +152,8 @@ describe Google::Auth::UserAuthorizer do
|
||||
|
||||
it 'should return an instance of UserRefreshCredentials' do
|
||||
expect(credentials).to be_instance_of(
|
||||
Google::Auth::UserRefreshCredentials)
|
||||
Google::Auth::UserRefreshCredentials
|
||||
)
|
||||
end
|
||||
|
||||
it 'should return credentials with a valid refresh token' do
|
||||
@@ -226,7 +229,8 @@ describe Google::Auth::UserAuthorizer do
|
||||
it 'should persist the expiry as milliseconds' do
|
||||
expected_expiry = expiry * 1000
|
||||
expect(MultiJson.load(token_json)['expiration_time_millis']).to eql(
|
||||
expected_expiry)
|
||||
expected_expiry
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -240,12 +244,14 @@ describe Google::Auth::UserAuthorizer do
|
||||
before(:example) do
|
||||
stub_request(:post, 'https://www.googleapis.com/oauth2/v3/token')
|
||||
.to_return(body: token_json, status: 200, headers: {
|
||||
'Content-Type' => 'application/json' })
|
||||
'Content-Type' => 'application/json'
|
||||
})
|
||||
end
|
||||
|
||||
it 'should exchange a code for credentials' do
|
||||
credentials = authorizer.get_credentials_from_code(
|
||||
user_id: 'user1', code: 'code')
|
||||
user_id: 'user1', code: 'code'
|
||||
)
|
||||
expect(credentials.access_token).to eq '1/abc123'
|
||||
end
|
||||
|
||||
@@ -256,7 +262,8 @@ describe Google::Auth::UserAuthorizer do
|
||||
|
||||
it 'should store credentials when requested' do
|
||||
authorizer.get_and_store_credentials_from_code(
|
||||
user_id: 'user1', code: 'code')
|
||||
user_id: 'user1', code: 'code'
|
||||
)
|
||||
expect(token_store.load('user1')).to_not be_nil
|
||||
end
|
||||
end
|
||||
@@ -286,20 +293,23 @@ describe Google::Auth::UserAuthorizer do
|
||||
MultiJson.dump(
|
||||
access_token: 'accesstoken',
|
||||
refresh_token: 'refreshtoken',
|
||||
expiration_time_millis: 1_441_234_742_000)
|
||||
expiration_time_millis: 1_441_234_742_000
|
||||
)
|
||||
end
|
||||
|
||||
before(:example) do
|
||||
token_store.store('user1', token_json)
|
||||
stub_request(
|
||||
:get, 'https://accounts.google.com/o/oauth2/revoke?token=refreshtoken')
|
||||
:get, 'https://accounts.google.com/o/oauth2/revoke?token=refreshtoken'
|
||||
)
|
||||
.to_return(status: 200)
|
||||
end
|
||||
|
||||
it 'should revoke the grant' do
|
||||
authorizer.revoke_authorization('user1')
|
||||
expect(a_request(
|
||||
:get, 'https://accounts.google.com/o/oauth2/revoke?token=refreshtoken'))
|
||||
:get, 'https://accounts.google.com/o/oauth2/revoke?token=refreshtoken'
|
||||
))
|
||||
.to have_been_made
|
||||
end
|
||||
|
||||
|
||||
@@ -87,7 +87,8 @@ describe Google::Auth::UserRefreshCredentials do
|
||||
@var_name = ENV_VAR
|
||||
@credential_vars = [
|
||||
ENV_VAR, CLIENT_ID_VAR, CLIENT_SECRET_VAR, REFRESH_TOKEN_VAR,
|
||||
ACCOUNT_TYPE_VAR]
|
||||
ACCOUNT_TYPE_VAR
|
||||
]
|
||||
@original_env_vals = {}
|
||||
@credential_vars.each { |var| @original_env_vals[var] = ENV[var] }
|
||||
@scope = 'https://www.googleapis.com/auth/userinfo.profile'
|
||||
@@ -288,7 +289,8 @@ describe Google::Auth::UserRefreshCredentials do
|
||||
it 'raises an authorization error' do
|
||||
stub
|
||||
expect { @client.revoke! }.to raise_error(
|
||||
Signet::AuthorizationError)
|
||||
Signet::AuthorizationError
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -52,13 +52,15 @@ describe Google::Auth::WebUserAuthorizer do
|
||||
let(:env) do
|
||||
Rack::MockRequest.env_for(
|
||||
'http://example.com:8080/test',
|
||||
'REMOTE_ADDR' => '10.10.10.10')
|
||||
'REMOTE_ADDR' => '10.10.10.10'
|
||||
)
|
||||
end
|
||||
let(:request) { Rack::Request.new(env) }
|
||||
it 'should include current url in state' do
|
||||
url = authorizer.get_authorization_url(request: request)
|
||||
expect(url).to match(
|
||||
%r{%22current_uri%22:%22http://example.com:8080/test%22})
|
||||
%r{%22current_uri%22:%22http://example.com:8080/test%22}
|
||||
)
|
||||
end
|
||||
|
||||
it 'should include request forgery token in state' do
|
||||
@@ -76,20 +78,23 @@ describe Google::Auth::WebUserAuthorizer do
|
||||
it 'should resolve callback against base URL' do
|
||||
url = authorizer.get_authorization_url(request: request)
|
||||
expect(url).to match(
|
||||
%r{redirect_uri=http://example.com:8080/oauth2callback})
|
||||
%r{redirect_uri=http://example.com:8080/oauth2callback}
|
||||
)
|
||||
end
|
||||
|
||||
it 'should allow overriding the current URL' do
|
||||
url = authorizer.get_authorization_url(
|
||||
request: request,
|
||||
redirect_to: '/foo')
|
||||
redirect_to: '/foo'
|
||||
)
|
||||
expect(url).to match %r{%22current_uri%22:%22/foo%22}
|
||||
end
|
||||
|
||||
it 'should pass through login hint' do
|
||||
url = authorizer.get_authorization_url(
|
||||
request: request,
|
||||
login_hint: 'user@example.com')
|
||||
login_hint: 'user@example.com'
|
||||
)
|
||||
expect(url).to match(/login_hint=user@example.com/)
|
||||
end
|
||||
end
|
||||
@@ -113,7 +118,8 @@ describe Google::Auth::WebUserAuthorizer do
|
||||
'http://example.com:8080/oauth2callback?code=authcode&'\
|
||||
'state=%7B%22current_uri%22%3A%22%2Ffoo%22%2C%22'\
|
||||
'session_id%22%3A%22abc%22%7D',
|
||||
'REMOTE_ADDR' => '10.10.10.10')
|
||||
'REMOTE_ADDR' => '10.10.10.10'
|
||||
)
|
||||
end
|
||||
let(:request) { Rack::Request.new(env) }
|
||||
|
||||
@@ -123,7 +129,8 @@ describe Google::Auth::WebUserAuthorizer do
|
||||
|
||||
it 'should return credentials when valid code present' do
|
||||
expect(credentials).to be_instance_of(
|
||||
Google::Auth::UserRefreshCredentials)
|
||||
Google::Auth::UserRefreshCredentials
|
||||
)
|
||||
end
|
||||
|
||||
it 'should return next URL to redirect to' do
|
||||
|
||||
Reference in new issue
Block a user