Skip to content

Commit f798d8c

Browse files
author
Jon Wayne Parrott
authored
Raise ValueError if credentials are not from google-auth (#2828)
1 parent ddd1758 commit f798d8c

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

packages/google-cloud-speech/tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ localdeps =
77
pip install --quiet --upgrade {toxinidir}/../core
88
deps =
99
{toxinidir}/../core
10+
mock
1011
pytest
1112
covercmd =
1213
py.test --quiet \

packages/google-cloud-speech/unit_tests/test__gax.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
import unittest
1616

17+
import mock
18+
19+
20+
def _make_credentials():
21+
import google.auth.credentials
22+
return mock.Mock(spec=google.auth.credentials.Credentials)
23+
1724

1825
class TestGAPICSpeechAPI(unittest.TestCase):
1926
SAMPLE_RATE = 16000
@@ -31,7 +38,7 @@ def test_use_bytes_instead_of_file_like_object(self):
3138
from google.cloud import speech
3239
from google.cloud.speech.sample import Sample
3340

34-
credentials = {}
41+
credentials = _make_credentials()
3542
client = speech.Client(credentials=credentials, use_gax=True)
3643
client.connection = _Connection()
3744
client.connection.credentials = credentials

packages/google-cloud-speech/unit_tests/test_client.py

+26-19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
import unittest
1616

17+
import mock
18+
19+
20+
def _make_credentials():
21+
import google.auth.credentials
22+
return mock.Mock(spec=google.auth.credentials.Credentials)
23+
1724

1825
def _make_result(alternatives=()):
1926
from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2
@@ -79,15 +86,15 @@ def _make_one(self, *args, **kw):
7986
def test_ctor(self):
8087
from google.cloud.speech.connection import Connection
8188

82-
creds = object()
89+
creds = _make_credentials()
8390
http = object()
8491
client = self._make_one(credentials=creds, http=http)
8592
self.assertIsInstance(client._connection, Connection)
8693
self.assertTrue(client._connection.credentials is creds)
8794
self.assertTrue(client._connection.http is http)
8895

8996
def test_ctor_use_gax_preset(self):
90-
creds = object()
97+
creds = _make_credentials()
9198
http = object()
9299
client = self._make_one(credentials=creds, http=http, use_gax=True)
93100
self.assertTrue(client._use_gax)
@@ -96,7 +103,7 @@ def test_create_sample_from_client(self):
96103
from google.cloud import speech
97104
from google.cloud.speech.sample import Sample
98105

99-
credentials = object()
106+
credentials = _make_credentials()
100107
client = self._make_one(credentials=credentials)
101108

102109
sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
@@ -144,7 +151,7 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
144151
'content': _B64_AUDIO_CONTENT,
145152
}
146153
}
147-
credentials = object()
154+
credentials = _make_credentials()
148155
client = self._make_one(credentials=credentials, use_gax=False)
149156
client._connection = _Connection(RETURNED)
150157

@@ -187,7 +194,7 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self):
187194
'uri': self.AUDIO_SOURCE_URI,
188195
}
189196
}
190-
credentials = object()
197+
credentials = _make_credentials()
191198
client = self._make_one(credentials=credentials, use_gax=False)
192199
client._connection = _Connection(RETURNED)
193200

@@ -216,7 +223,7 @@ def test_sync_recognize_with_empty_results_no_gax(self):
216223
from google.cloud import speech
217224
from unit_tests._fixtures import SYNC_RECOGNIZE_EMPTY_RESPONSE
218225

219-
credentials = object()
226+
credentials = _make_credentials()
220227
client = self._make_one(credentials=credentials, use_gax=False)
221228
client._connection = _Connection(SYNC_RECOGNIZE_EMPTY_RESPONSE)
222229

@@ -233,7 +240,7 @@ def test_sync_recognize_with_empty_results_gax(self):
233240
from google.cloud import speech
234241
from google.cloud.speech import _gax
235242

236-
credentials = object()
243+
credentials = _make_credentials()
237244
client = self._make_one(credentials=credentials, use_gax=True)
238245
client._connection = _Connection()
239246
client._connection.credentials = credentials
@@ -276,7 +283,7 @@ def test_sync_recognize_with_gax(self):
276283
from google.cloud import speech
277284
from google.cloud.speech import _gax
278285

279-
creds = object()
286+
creds = _make_credentials()
280287
client = self._make_one(credentials=creds, use_gax=True)
281288
client._connection = _Connection()
282289
client._connection.credentials = creds
@@ -336,7 +343,7 @@ def speech_api(channel=None):
336343
def test_async_supported_encodings(self):
337344
from google.cloud import speech
338345

339-
credentials = object()
346+
credentials = _make_credentials()
340347
client = self._make_one(credentials=credentials)
341348
client._connection = _Connection({})
342349

@@ -353,7 +360,7 @@ def test_async_recognize_no_gax(self):
353360

354361
RETURNED = ASYNC_RECOGNIZE_RESPONSE
355362

356-
credentials = object()
363+
credentials = _make_credentials()
357364
client = self._make_one(credentials=credentials, use_gax=False)
358365
client._connection = _Connection(RETURNED)
359366

@@ -375,7 +382,7 @@ def test_async_recognize_with_gax(self):
375382
from google.cloud.speech import _gax
376383
from google.cloud.speech.operation import Operation
377384

378-
credentials = object()
385+
credentials = _make_credentials()
379386
client = self._make_one(credentials=credentials,
380387
use_gax=True)
381388
client._connection = _Connection()
@@ -417,7 +424,7 @@ def speech_api(channel=None):
417424
def test_streaming_depends_on_gax(self):
418425
from google.cloud import speech
419426

420-
credentials = object()
427+
credentials = _make_credentials()
421428
client = self._make_one(credentials=credentials, use_gax=False)
422429
client.connection = _Connection()
423430
sample = client.sample(content=self.AUDIO_CONTENT,
@@ -436,7 +443,7 @@ def test_streaming_closed_stream(self):
436443
from google.cloud.speech.encoding import Encoding
437444

438445
stream = BytesIO(b'Some audio data...')
439-
credentials = object()
446+
credentials = _make_credentials()
440447
client = self._make_one(credentials=credentials)
441448
client.connection = _Connection()
442449
client.connection.credentials = credentials
@@ -477,7 +484,7 @@ def test_stream_recognize_interim_results(self):
477484
from google.cloud.speech.result import StreamingSpeechResult
478485

479486
stream = BytesIO(b'Some audio data...')
480-
credentials = object()
487+
credentials = _make_credentials()
481488
client = self._make_one(credentials=credentials)
482489
client.connection = _Connection()
483490
client.connection.credentials = credentials
@@ -553,7 +560,7 @@ def test_stream_recognize(self):
553560
from google.cloud.speech.encoding import Encoding
554561

555562
stream = BytesIO(b'Some audio data...')
556-
credentials = object()
563+
credentials = _make_credentials()
557564
client = self._make_one(credentials=credentials)
558565
client.connection = _Connection()
559566
client.connection.credentials = credentials
@@ -609,7 +616,7 @@ def test_stream_recognize_no_results(self):
609616
from google.cloud.speech.encoding import Encoding
610617

611618
stream = BytesIO(b'Some audio data...')
612-
credentials = object()
619+
credentials = _make_credentials()
613620
client = self._make_one(credentials=credentials)
614621
client.connection = _Connection()
615622
client.connection.credentials = credentials
@@ -645,7 +652,7 @@ def test_speech_api_with_gax(self):
645652

646653
from google.cloud.speech import _gax
647654

648-
creds = object()
655+
creds = _make_credentials()
649656
client = self._make_one(credentials=creds, use_gax=True)
650657
client._connection = _Connection()
651658
client._connection.credentials = creds
@@ -678,14 +685,14 @@ def test_speech_api_without_gax(self):
678685
from google.cloud._http import Connection
679686
from google.cloud.speech.client import _JSONSpeechAPI
680687

681-
creds = object()
688+
creds = _make_credentials()
682689
client = self._make_one(credentials=creds, use_gax=False)
683690
self.assertIsNone(client._speech_api)
684691
self.assertIsInstance(client.speech_api, _JSONSpeechAPI)
685692
self.assertIsInstance(client.speech_api._connection, Connection)
686693

687694
def test_speech_api_preset(self):
688-
creds = object()
695+
creds = _make_credentials()
689696
client = self._make_one(credentials=creds)
690697
fake_api = object()
691698
client._speech_api = fake_api

0 commit comments

Comments
 (0)