Skip to content

Commit

Permalink
Raise ValueError if credentials are not from google-auth (#2828)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Wayne Parrott authored Dec 8, 2016
1 parent ddd1758 commit f798d8c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/google-cloud-speech/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ localdeps =
pip install --quiet --upgrade {toxinidir}/../core
deps =
{toxinidir}/../core
mock
pytest
covercmd =
py.test --quiet \
Expand Down
9 changes: 8 additions & 1 deletion packages/google-cloud-speech/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
45 changes: 26 additions & 19 deletions packages/google-cloud-speech/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,15 +86,15 @@ 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)
self.assertTrue(client._connection.credentials is creds)
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)
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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({})

Expand All @@ -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)

Expand All @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f798d8c

Please sign in to comment.