Skip to content

Commit

Permalink
Stub Google::Auth.get_application_default in tests so that they pass …
Browse files Browse the repository at this point in the history
…on systems without credentials.
  • Loading branch information
jeremywadsack committed Nov 21, 2016
1 parent 1a5cb7e commit f9b604b
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions test/test_kubeclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -625,47 +625,47 @@ def test_auth_use_default_gcp_success
stub_request(:get, %r{/api/v1$})
.to_return(body: open_test_file('core_api_resource_list.json'),
status: 200)
stub_request(:post, 'https://www.googleapis.com/oauth2/v3/token')
.to_return(status: 200,
body: '{
"access_token": "valid_token",
"token_type": "Bearer",
"expires_in": 3600,
"id_token": "1234567890"
}',
headers: { 'Content-Type' => 'application/json; charset=UTF-8' })
stub_request(:get, 'http://localhost:8080/api/v1/pods')
.with(headers: { Authorization: 'Bearer valid_token' })
.to_return(body: open_test_file('pod_list.json'),
status: 200)

client = Kubeclient::Client.new('http://localhost:8080/api/',
auth_options: {
use_default_gcp: true
})

pods = client.get_pods
auth = Minitest::Mock.new
auth.expect(:apply, nil, [{}])
auth.expect(:access_token, 'valid_token')
Google::Auth.stub(:get_application_default, auth) do
client = Kubeclient::Client.new('http://localhost:8080/api/',
auth_options: {
use_default_gcp: true
})
pods = client.get_pods

assert_equal('Pod', pods.kind)
assert_equal(1, pods.size)
assert_equal('Pod', pods.kind)
assert_equal(1, pods.size)
end
end

def test_auth_use_default_gcp_failure
stub_request(:get, %r{/api/v1$})
.to_return(body: open_test_file('core_api_resource_list.json'),
status: 200)
stub_request(:post, 'https://www.googleapis.com/oauth2/v3/token')
.to_return(status: 401,
body: '',
headers: { 'Content-Type' => 'application/json; charset=UTF-8' })

expected_msg = 'Authorization failed.'
response = OpenStruct.new(body: '', status: 401)
auth = Minitest::Mock.new
auth.expect(:apply, nil) do
fail Signet::AuthorizationError.new(expected_msg, response: response)
end

exception = assert_raises(KubeException) do
Kubeclient::Client.new('http://localhost:8080',
auth_options: {
use_default_gcp: true
})
Google::Auth.stub(:get_application_default, auth) do
Kubeclient::Client.new('http://localhost:8080',
auth_options: {
use_default_gcp: true
})
end
end

assert_equal(401, exception.error_code)
assert_equal(expected_msg, exception.message)
end
Expand Down

0 comments on commit f9b604b

Please sign in to comment.