diff --git a/test/test_kubeclient.rb b/test/test_kubeclient.rb index 9a91dc20..6de75b97 100644 --- a/test/test_kubeclient.rb +++ b/test/test_kubeclient.rb @@ -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