From d402f1026500a043cd22deb25568ead6a70a4123 Mon Sep 17 00:00:00 2001
From: Jon Wayne Parrott <jonwayne@google.com>
Date: Wed, 7 Dec 2016 16:00:24 -0800
Subject: [PATCH] Raise ValueError if credentials are not from google-auth
 (#2828)

---
 google-cloud-speech/tox.ini                   |  1 +
 google-cloud-speech/unit_tests/test__gax.py   |  9 +++-
 google-cloud-speech/unit_tests/test_client.py | 45 +++++++++++--------
 3 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/google-cloud-speech/tox.ini b/google-cloud-speech/tox.ini
index f829dcf5b262..4ded9cd86434 100644
--- a/google-cloud-speech/tox.ini
+++ b/google-cloud-speech/tox.ini
@@ -7,6 +7,7 @@ localdeps =
     pip install --quiet --upgrade {toxinidir}/../core
 deps =
     {toxinidir}/../core
+    mock
     pytest
 covercmd =
     py.test --quiet \
diff --git a/google-cloud-speech/unit_tests/test__gax.py b/google-cloud-speech/unit_tests/test__gax.py
index 4fed73c629b0..2c34b93abd77 100644
--- a/google-cloud-speech/unit_tests/test__gax.py
+++ b/google-cloud-speech/unit_tests/test__gax.py
@@ -14,6 +14,13 @@
 
 import unittest
 
+import mock
+
+
+def _make_credentials():
+    import google.auth.credentials
+    return mock.Mock(spec=google.auth.credentials.Credentials)
+
 
 class TestGAPICSpeechAPI(unittest.TestCase):
     SAMPLE_RATE = 16000
@@ -31,7 +38,7 @@ def test_use_bytes_instead_of_file_like_object(self):
         from google.cloud import speech
         from google.cloud.speech.sample import Sample
 
-        credentials = {}
+        credentials = _make_credentials()
         client = speech.Client(credentials=credentials, use_gax=True)
         client.connection = _Connection()
         client.connection.credentials = credentials
diff --git a/google-cloud-speech/unit_tests/test_client.py b/google-cloud-speech/unit_tests/test_client.py
index 0ba0cc932679..bf11eb4f7c65 100644
--- a/google-cloud-speech/unit_tests/test_client.py
+++ b/google-cloud-speech/unit_tests/test_client.py
@@ -14,6 +14,13 @@
 
 import unittest
 
+import mock
+
+
+def _make_credentials():
+    import google.auth.credentials
+    return mock.Mock(spec=google.auth.credentials.Credentials)
+
 
 def _make_result(alternatives=()):
     from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2
@@ -79,7 +86,7 @@ def _make_one(self, *args, **kw):
     def test_ctor(self):
         from google.cloud.speech.connection import Connection
 
-        creds = object()
+        creds = _make_credentials()
         http = object()
         client = self._make_one(credentials=creds, http=http)
         self.assertIsInstance(client._connection, Connection)
@@ -87,7 +94,7 @@ def test_ctor(self):
         self.assertTrue(client._connection.http is http)
 
     def test_ctor_use_gax_preset(self):
-        creds = object()
+        creds = _make_credentials()
         http = object()
         client = self._make_one(credentials=creds, http=http, use_gax=True)
         self.assertTrue(client._use_gax)
@@ -96,7 +103,7 @@ def test_create_sample_from_client(self):
         from google.cloud import speech
         from google.cloud.speech.sample import Sample
 
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials)
 
         sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
@@ -144,7 +151,7 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
                 'content': _B64_AUDIO_CONTENT,
             }
         }
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials, use_gax=False)
         client._connection = _Connection(RETURNED)
 
@@ -187,7 +194,7 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self):
                 'uri': self.AUDIO_SOURCE_URI,
             }
         }
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials, use_gax=False)
         client._connection = _Connection(RETURNED)
 
@@ -216,7 +223,7 @@ def test_sync_recognize_with_empty_results_no_gax(self):
         from google.cloud import speech
         from unit_tests._fixtures import SYNC_RECOGNIZE_EMPTY_RESPONSE
 
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials, use_gax=False)
         client._connection = _Connection(SYNC_RECOGNIZE_EMPTY_RESPONSE)
 
@@ -233,7 +240,7 @@ def test_sync_recognize_with_empty_results_gax(self):
         from google.cloud import speech
         from google.cloud.speech import _gax
 
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials, use_gax=True)
         client._connection = _Connection()
         client._connection.credentials = credentials
@@ -276,7 +283,7 @@ def test_sync_recognize_with_gax(self):
         from google.cloud import speech
         from google.cloud.speech import _gax
 
-        creds = object()
+        creds = _make_credentials()
         client = self._make_one(credentials=creds, use_gax=True)
         client._connection = _Connection()
         client._connection.credentials = creds
@@ -336,7 +343,7 @@ def speech_api(channel=None):
     def test_async_supported_encodings(self):
         from google.cloud import speech
 
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials)
         client._connection = _Connection({})
 
@@ -353,7 +360,7 @@ def test_async_recognize_no_gax(self):
 
         RETURNED = ASYNC_RECOGNIZE_RESPONSE
 
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials, use_gax=False)
         client._connection = _Connection(RETURNED)
 
@@ -375,7 +382,7 @@ def test_async_recognize_with_gax(self):
         from google.cloud.speech import _gax
         from google.cloud.speech.operation import Operation
 
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials,
                                 use_gax=True)
         client._connection = _Connection()
@@ -417,7 +424,7 @@ def speech_api(channel=None):
     def test_streaming_depends_on_gax(self):
         from google.cloud import speech
 
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials, use_gax=False)
         client.connection = _Connection()
         sample = client.sample(content=self.AUDIO_CONTENT,
@@ -436,7 +443,7 @@ def test_streaming_closed_stream(self):
         from google.cloud.speech.encoding import Encoding
 
         stream = BytesIO(b'Some audio data...')
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials)
         client.connection = _Connection()
         client.connection.credentials = credentials
@@ -477,7 +484,7 @@ def test_stream_recognize_interim_results(self):
         from google.cloud.speech.result import StreamingSpeechResult
 
         stream = BytesIO(b'Some audio data...')
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials)
         client.connection = _Connection()
         client.connection.credentials = credentials
@@ -553,7 +560,7 @@ def test_stream_recognize(self):
         from google.cloud.speech.encoding import Encoding
 
         stream = BytesIO(b'Some audio data...')
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials)
         client.connection = _Connection()
         client.connection.credentials = credentials
@@ -609,7 +616,7 @@ def test_stream_recognize_no_results(self):
         from google.cloud.speech.encoding import Encoding
 
         stream = BytesIO(b'Some audio data...')
-        credentials = object()
+        credentials = _make_credentials()
         client = self._make_one(credentials=credentials)
         client.connection = _Connection()
         client.connection.credentials = credentials
@@ -645,7 +652,7 @@ def test_speech_api_with_gax(self):
 
         from google.cloud.speech import _gax
 
-        creds = object()
+        creds = _make_credentials()
         client = self._make_one(credentials=creds, use_gax=True)
         client._connection = _Connection()
         client._connection.credentials = creds
@@ -678,14 +685,14 @@ def test_speech_api_without_gax(self):
         from google.cloud._http import Connection
         from google.cloud.speech.client import _JSONSpeechAPI
 
-        creds = object()
+        creds = _make_credentials()
         client = self._make_one(credentials=creds, use_gax=False)
         self.assertIsNone(client._speech_api)
         self.assertIsInstance(client.speech_api, _JSONSpeechAPI)
         self.assertIsInstance(client.speech_api._connection, Connection)
 
     def test_speech_api_preset(self):
-        creds = object()
+        creds = _make_credentials()
         client = self._make_one(credentials=creds)
         fake_api = object()
         client._speech_api = fake_api