From dea6029b84d25e54abc811ef81191717187c042a Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Wed, 7 Dec 2016 16:00:24 -0800 Subject: [PATCH] Raise ValueError if credentials are not from google-auth (#2828) --- bigquery/unit_tests/test_client.py | 62 +++++------ bigtable/unit_tests/test_client.py | 66 +++++++----- core/google/cloud/client.py | 12 +++ core/unit_tests/test_client.py | 24 +++-- datastore/unit_tests/test_client.py | 119 +++++++++------------ dns/tox.ini | 1 + dns/unit_tests/test_client.py | 23 ++-- error_reporting/unit_tests/test_client.py | 17 ++- logging/unit_tests/test__gax.py | 17 +-- logging/unit_tests/test__http.py | 15 ++- logging/unit_tests/test_client.py | 66 ++++++------ logging/unit_tests/test_logger.py | 27 +++-- monitoring/unit_tests/test_client.py | 57 +++++++--- pubsub/unit_tests/test__gax.py | 22 ++-- pubsub/unit_tests/test__http.py | 12 ++- pubsub/unit_tests/test_client.py | 38 +++---- pubsub/unit_tests/test_topic.py | 20 ++-- resource_manager/tox.ini | 1 + resource_manager/unit_tests/test_client.py | 25 +++-- runtimeconfig/tox.ini | 1 + runtimeconfig/unit_tests/test_client.py | 9 +- speech/tox.ini | 1 + speech/unit_tests/test__gax.py | 9 +- speech/unit_tests/test_client.py | 45 ++++---- storage/unit_tests/test_batch.py | 13 ++- storage/unit_tests/test_client.py | 45 ++++---- vision/unit_tests/test_client.py | 54 +++++----- 27 files changed, 464 insertions(+), 337 deletions(-) diff --git a/bigquery/unit_tests/test_client.py b/bigquery/unit_tests/test_client.py index 152714839ebd..7442d7ad8b5d 100644 --- a/bigquery/unit_tests/test_client.py +++ b/bigquery/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) + class TestClient(unittest.TestCase): @@ -28,7 +35,7 @@ def _make_one(self, *args, **kw): def test_ctor(self): from google.cloud.bigquery._http import Connection PROJECT = 'PROJECT' - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=PROJECT, credentials=creds, http=http) self.assertIsInstance(client._connection, Connection) @@ -57,7 +64,7 @@ def test_list_projects_defaults(self): 'friendlyName': 'Two'}, ] } - creds = object() + creds = _make_credentials() client = self._make_one(PROJECT_1, creds) conn = client._connection = _Connection(DATA) @@ -86,7 +93,7 @@ def test_list_projects_explicit_response_missing_projects_key(self): PATH = 'projects' TOKEN = 'TOKEN' DATA = {} - creds = object() + creds = _make_credentials() client = self._make_one(PROJECT, creds) conn = client._connection = _Connection(DATA) @@ -128,7 +135,7 @@ def test_list_datasets_defaults(self): 'friendlyName': 'Two'}, ] } - creds = object() + creds = _make_credentials() client = self._make_one(PROJECT, creds) conn = client._connection = _Connection(DATA) @@ -156,7 +163,7 @@ def test_list_datasets_explicit_response_missing_datasets_key(self): PATH = 'projects/%s/datasets' % PROJECT TOKEN = 'TOKEN' DATA = {} - creds = object() + creds = _make_credentials() client = self._make_one(PROJECT, creds) conn = client._connection = _Connection(DATA) @@ -176,36 +183,21 @@ def test_list_datasets_explicit_response_missing_datasets_key(self): self.assertEqual(req['query_params'], {'all': True, 'maxResults': 3, 'pageToken': TOKEN}) - def test_dataset_defaults(self): + def test_dataset(self): from google.cloud.bigquery.dataset import Dataset PROJECT = 'PROJECT' DATASET = 'dataset_name' - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=PROJECT, credentials=creds, http=http) dataset = client.dataset(DATASET) self.assertIsInstance(dataset, Dataset) self.assertEqual(dataset.name, DATASET) self.assertIs(dataset._client, client) - self.assertEqual(dataset.project, PROJECT) - - def test_dataset_explicit(self): - from google.cloud.bigquery.dataset import Dataset - PROJECT = 'my-project-123' - OTHER_PROJECT = 'other-project-456' - DATASET = 'dataset_name' - creds = object() - http = object() - client = self._make_one(project=PROJECT, credentials=creds, http=http) - dataset = client.dataset(DATASET, project=OTHER_PROJECT) - self.assertIsInstance(dataset, Dataset) - self.assertEqual(dataset.name, DATASET) - self.assertIs(dataset._client, client) - self.assertEqual(dataset.project, OTHER_PROJECT) def test_job_from_resource_unknown_type(self): PROJECT = 'PROJECT' - creds = object() + creds = _make_credentials() client = self._make_one(PROJECT, creds) with self.assertRaises(ValueError): client.job_from_resource({'configuration': {'nonesuch': {}}}) @@ -319,7 +311,7 @@ def test_list_jobs_defaults(self): LOAD_DATA, ] } - creds = object() + creds = _make_credentials() client = self._make_one(PROJECT, creds) conn = client._connection = _Connection(DATA) @@ -375,7 +367,7 @@ def test_list_jobs_load_job_wo_sourceUris(self): LOAD_DATA, ] } - creds = object() + creds = _make_credentials() client = self._make_one(PROJECT, creds) conn = client._connection = _Connection(DATA) @@ -403,7 +395,7 @@ def test_list_jobs_explicit_missing(self): PATH = 'projects/%s/jobs' % PROJECT DATA = {} TOKEN = 'TOKEN' - creds = object() + creds = _make_credentials() client = self._make_one(PROJECT, creds) conn = client._connection = _Connection(DATA) @@ -434,7 +426,7 @@ def test_load_table_from_storage(self): DATASET = 'dataset_name' DESTINATION = 'destination_table' SOURCE_URI = 'http://example.com/source.csv' - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=PROJECT, credentials=creds, http=http) dataset = client.dataset(DATASET) @@ -453,7 +445,7 @@ def test_copy_table(self): DATASET = 'dataset_name' SOURCE = 'source_table' DESTINATION = 'destination_table' - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=PROJECT, credentials=creds, http=http) dataset = client.dataset(DATASET) @@ -473,7 +465,7 @@ def test_extract_table_to_storage(self): DATASET = 'dataset_name' SOURCE = 'source_table' DESTINATION = 'gs://bucket_name/object_name' - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=PROJECT, credentials=creds, http=http) dataset = client.dataset(DATASET) @@ -490,7 +482,7 @@ def test_run_async_query_defaults(self): PROJECT = 'PROJECT' JOB = 'job_name' QUERY = 'select count(*) from persons' - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=PROJECT, credentials=creds, http=http) job = client.run_async_query(JOB, QUERY) @@ -508,7 +500,7 @@ def test_run_async_w_udf_resources(self): PROJECT = 'PROJECT' JOB = 'job_name' QUERY = 'select count(*) from persons' - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=PROJECT, credentials=creds, http=http) udf_resources = [UDFResource("resourceUri", RESOURCE_URI)] @@ -526,7 +518,7 @@ def test_run_async_w_query_parameters(self): PROJECT = 'PROJECT' JOB = 'job_name' QUERY = 'select count(*) from persons' - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=PROJECT, credentials=creds, http=http) query_parameters = [ScalarQueryParameter('foo', 'INT64', 123)] @@ -543,7 +535,7 @@ def test_run_sync_query_defaults(self): from google.cloud.bigquery.query import QueryResults PROJECT = 'PROJECT' QUERY = 'select count(*) from persons' - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=PROJECT, credentials=creds, http=http) query = client.run_sync_query(QUERY) @@ -560,7 +552,7 @@ def test_run_sync_query_w_udf_resources(self): RESOURCE_URI = 'gs://some-bucket/js/lib.js' PROJECT = 'PROJECT' QUERY = 'select count(*) from persons' - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=PROJECT, credentials=creds, http=http) udf_resources = [UDFResource("resourceUri", RESOURCE_URI)] @@ -577,7 +569,7 @@ def test_run_sync_query_w_query_parameters(self): from google.cloud.bigquery.query import QueryResults PROJECT = 'PROJECT' QUERY = 'select count(*) from persons' - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=PROJECT, credentials=creds, http=http) query_parameters = [ScalarQueryParameter('foo', 'INT64', 123)] diff --git a/bigtable/unit_tests/test_client.py b/bigtable/unit_tests/test_client.py index a6b82cde0aae..4c912b4794d3 100644 --- a/bigtable/unit_tests/test_client.py +++ b/bigtable/unit_tests/test_client.py @@ -15,6 +15,19 @@ import unittest +import mock + + +def _make_credentials(): + import google.auth.credentials + + class _CredentialsWithScopes( + google.auth.credentials.Credentials, + google.auth.credentials.Scoped): + pass + + return mock.Mock(spec=_CredentialsWithScopes) + class Test__make_data_stub(unittest.TestCase): @@ -26,7 +39,7 @@ def test_without_emulator(self): from google.cloud._testing import _Monkey from google.cloud.bigtable import client as MUT - credentials = object() + credentials = _make_credentials() user_agent = 'you-sir-age-int' client = _Client(credentials, user_agent) @@ -86,7 +99,7 @@ def test_without_emulator(self): from google.cloud._testing import _Monkey from google.cloud.bigtable import client as MUT - credentials = object() + credentials = _make_credentials() user_agent = 'you-sir-age-int' client = _Client(credentials, user_agent) @@ -148,7 +161,7 @@ def test_without_emulator(self): from google.cloud._testing import _Monkey from google.cloud.bigtable import client as MUT - credentials = object() + credentials = _make_credentials() user_agent = 'you-sir-age-int' client = _Client(credentials, user_agent) @@ -210,7 +223,7 @@ def test_without_emulator(self): from google.cloud._testing import _Monkey from google.cloud.bigtable import client as MUT - credentials = object() + credentials = _make_credentials() user_agent = 'you-sir-age-int' client = _Client(credentials, user_agent) @@ -289,11 +302,6 @@ def _make_oneWithMocks(self, *args, **kwargs): _make_table_stub=mock_make_table_stub): return self._make_one(*args, **kwargs) - def _make_credentials(self): - import mock - import google.auth.credentials - return mock.Mock(spec=google.auth.credentials.Scoped) - def _constructor_test_helper(self, expected_scopes, creds, read_only=False, admin=False, user_agent=None, expected_creds=None): @@ -351,7 +359,7 @@ def test_constructor_default_scopes(self): from google.cloud.bigtable import client as MUT expected_scopes = [MUT.DATA_SCOPE] - creds = self._make_credentials() + creds = _make_credentials() self._constructor_test_helper(expected_scopes, creds) def test_constructor_custom_user_agent(self): @@ -359,7 +367,7 @@ def test_constructor_custom_user_agent(self): CUSTOM_USER_AGENT = 'custom-application' expected_scopes = [MUT.DATA_SCOPE] - creds = self._make_credentials() + creds = _make_credentials() self._constructor_test_helper(expected_scopes, creds, user_agent=CUSTOM_USER_AGENT) @@ -367,18 +375,18 @@ def test_constructor_with_admin(self): from google.cloud.bigtable import client as MUT expected_scopes = [MUT.DATA_SCOPE, MUT.ADMIN_SCOPE] - creds = self._make_credentials() + creds = _make_credentials() self._constructor_test_helper(expected_scopes, creds, admin=True) def test_constructor_with_read_only(self): from google.cloud.bigtable import client as MUT expected_scopes = [MUT.READ_ONLY_SCOPE] - creds = self._make_credentials() + creds = _make_credentials() self._constructor_test_helper(expected_scopes, creds, read_only=True) def test_constructor_both_admin_and_read_only(self): - creds = self._make_credentials() + creds = _make_credentials() with self.assertRaises(ValueError): self._constructor_test_helper([], creds, admin=True, read_only=True) @@ -387,7 +395,7 @@ def test_constructor_implicit_credentials(self): from google.cloud._testing import _Monkey from google.cloud.bigtable import client as MUT - creds = self._make_credentials() + creds = _make_credentials() expected_scopes = [MUT.DATA_SCOPE] def mock_get_credentials(): @@ -401,7 +409,7 @@ def mock_get_credentials(): creds.with_scopes.assert_called_once_with(expected_scopes) def test_constructor_credentials_wo_create_scoped(self): - creds = self._make_credentials() + creds = _make_credentials() expected_scopes = None self._constructor_test_helper(expected_scopes, creds) @@ -409,7 +417,7 @@ def _copy_test_helper(self, read_only=False, admin=False): from google.cloud._testing import _Monkey from google.cloud.bigtable import client as MUT - credentials = self._make_credentials() + credentials = _make_credentials() client = self._make_oneWithMocks( project=self.PROJECT, credentials=credentials, @@ -456,14 +464,14 @@ def test_copy_read_only(self): self._copy_test_helper(read_only=True) def test_credentials_getter(self): - credentials = object() + credentials = _make_credentials() project = 'PROJECT' client = self._make_oneWithMocks(project=project, credentials=credentials) - self.assertIs(client.credentials, credentials) + self.assertIs(client.credentials, credentials.with_scopes.return_value) def test_project_name_property(self): - credentials = object() + credentials = _make_credentials() project = 'PROJECT' client = self._make_oneWithMocks(project=project, credentials=credentials) @@ -471,14 +479,14 @@ def test_project_name_property(self): self.assertEqual(client.project_name, project_name) def test_instance_stub_getter(self): - credentials = object() + credentials = _make_credentials() project = 'PROJECT' client = self._make_oneWithMocks(project=project, credentials=credentials, admin=True) self.assertIs(client._instance_stub, client._instance_stub_internal) def test_instance_stub_non_admin_failure(self): - credentials = object() + credentials = _make_credentials() project = 'PROJECT' client = self._make_oneWithMocks(project=project, credentials=credentials, admin=False) @@ -486,7 +494,7 @@ def test_instance_stub_non_admin_failure(self): getattr(client, '_instance_stub') def test_operations_stub_getter(self): - credentials = object() + credentials = _make_credentials() project = 'PROJECT' client = self._make_oneWithMocks(project=project, credentials=credentials, admin=True) @@ -494,7 +502,7 @@ def test_operations_stub_getter(self): client._operations_stub_internal) def test_operations_stub_non_admin_failure(self): - credentials = object() + credentials = _make_credentials() project = 'PROJECT' client = self._make_oneWithMocks(project=project, credentials=credentials, admin=False) @@ -502,14 +510,14 @@ def test_operations_stub_non_admin_failure(self): getattr(client, '_operations_stub') def test_table_stub_getter(self): - credentials = object() + credentials = _make_credentials() project = 'PROJECT' client = self._make_oneWithMocks(project=project, credentials=credentials, admin=True) self.assertIs(client._table_stub, client._table_stub_internal) def test_table_stub_non_admin_failure(self): - credentials = object() + credentials = _make_credentials() project = 'PROJECT' client = self._make_oneWithMocks(project=project, credentials=credentials, admin=False) @@ -525,7 +533,7 @@ def test_instance_factory_defaults(self): PROJECT = 'PROJECT' INSTANCE_ID = 'instance-id' DISPLAY_NAME = 'display-name' - credentials = object() + credentials = _make_credentials() client = self._make_oneWithMocks(project=PROJECT, credentials=credentials) @@ -547,7 +555,7 @@ def test_instance_factory_w_explicit_serve_nodes(self): DISPLAY_NAME = 'display-name' LOCATION_ID = 'locname' SERVE_NODES = 5 - credentials = object() + credentials = _make_credentials() client = self._make_oneWithMocks(project=PROJECT, credentials=credentials) @@ -578,7 +586,7 @@ def test_list_instances(self): INSTANCE_NAME2 = ( 'projects/' + self.PROJECT + '/instances/' + INSTANCE_ID2) - credentials = object() + credentials = _make_credentials() client = self._make_oneWithMocks( project=self.PROJECT, credentials=credentials, diff --git a/core/google/cloud/client.py b/core/google/cloud/client.py index a63c614888ef..ab413f91a652 100644 --- a/core/google/cloud/client.py +++ b/core/google/cloud/client.py @@ -14,6 +14,7 @@ """Base classes for client used to interact with Google Cloud APIs.""" +import google.auth.credentials from google.oauth2 import service_account import six @@ -22,6 +23,13 @@ from google.cloud.credentials import get_credentials +_GOOGLE_AUTH_CREDENTIALS_HELP = ( + 'This library only supports credentials from google-auth-library-python. ' + 'See https://google-cloud-python.readthedocs.io/en/latest/' + 'google-cloud-auth.html for help on authentication with this library.' +) + + class _ClientFactoryMixin(object): """Mixin to allow factories that create credentials. @@ -83,6 +91,10 @@ class Client(_ClientFactoryMixin): _connection_class = Connection def __init__(self, credentials=None, http=None): + if (credentials is not None and + not isinstance( + credentials, google.auth.credentials.Credentials)): + raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP) if credentials is None and http is None: credentials = get_credentials() self._connection = self._connection_class( diff --git a/core/unit_tests/test_client.py b/core/unit_tests/test_client.py index 975dcc710b27..21d036c06ad0 100644 --- a/core/unit_tests/test_client.py +++ b/core/unit_tests/test_client.py @@ -17,6 +17,11 @@ import mock +def _make_credentials(): + import google.auth.credentials + return mock.Mock(spec=google.auth.credentials.Credentials) + + class Test_ClientFactoryMixin(unittest.TestCase): @staticmethod @@ -52,7 +57,7 @@ def test_ctor_defaults(self): from google.cloud._testing import _Monkey from google.cloud import client - CREDENTIALS = object() + CREDENTIALS = _make_credentials() FUNC_CALLS = [] def mock_get_credentials(): @@ -67,7 +72,7 @@ def mock_get_credentials(): self.assertEqual(FUNC_CALLS, ['get_credentials']) def test_ctor_explicit(self): - CREDENTIALS = object() + CREDENTIALS = _make_credentials() HTTP = object() client_obj = self._make_one(credentials=CREDENTIALS, http=HTTP) @@ -75,12 +80,19 @@ def test_ctor_explicit(self): self.assertIs(client_obj._connection.credentials, CREDENTIALS) self.assertIs(client_obj._connection.http, HTTP) + def test_ctor_bad_credentials(self): + CREDENTIALS = object() + + with self.assertRaises(ValueError): + self._make_one(credentials=CREDENTIALS) + def test_from_service_account_json(self): KLASS = self._get_target_class() constructor_patch = mock.patch( 'google.oauth2.service_account.Credentials.' - 'from_service_account_file') + 'from_service_account_file', + return_value=_make_credentials()) with constructor_patch as constructor: client_obj = KLASS.from_service_account_json( @@ -122,7 +134,7 @@ def test_ctor_defaults(self): from google.cloud import client PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() FUNC_CALLS = [] def mock_determine_proj(project): @@ -160,7 +172,7 @@ def mock_determine_proj(project): self.assertEqual(FUNC_CALLS, [(None, '_determine_default_project')]) def test_ctor_w_invalid_project(self): - CREDENTIALS = object() + CREDENTIALS = _make_credentials() HTTP = object() with self.assertRaises(ValueError): self._make_one(project=object(), credentials=CREDENTIALS, @@ -169,7 +181,7 @@ def test_ctor_w_invalid_project(self): def _explicit_ctor_helper(self, project): import six - CREDENTIALS = object() + CREDENTIALS = _make_credentials() HTTP = object() client_obj = self._make_one(project=project, credentials=CREDENTIALS, diff --git a/datastore/unit_tests/test_client.py b/datastore/unit_tests/test_client.py index a817e4066a25..f6e016d03712 100644 --- a/datastore/unit_tests/test_client.py +++ b/datastore/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_entity_pb(project, kind, integer_id, name=None, str_val=None): from google.cloud.datastore._generated import entity_pb2 @@ -38,15 +45,12 @@ def _call_fut(self): return _get_gcd_project() def test_no_value(self): - import mock - environ = {} with mock.patch('os.getenv', new=environ.get): project = self._call_fut() self.assertIsNone(project) def test_value_set(self): - import mock from google.cloud.datastore.client import GCD_DATASET MOCK_PROJECT = object() @@ -65,8 +69,6 @@ def _call_fut(self, project=None): def _determine_default_helper(self, gcd=None, fallback=None, project_called=None): - import mock - _callers = [] def gcd_mock(): @@ -137,8 +139,6 @@ def _make_one(self, project=PROJECT, namespace=None, http=http) def test_ctor_w_project_no_environ(self): - import mock - # Some environments (e.g. AppVeyor CI) run in GCE, so # this test would fail artificially. patch = mock.patch( @@ -148,10 +148,8 @@ def test_ctor_w_project_no_environ(self): self.assertRaises(EnvironmentError, self._make_one, None) def test_ctor_w_implicit_inputs(self): - import mock - OTHER = 'other' - creds = object() + creds = _make_credentials() default_called = [] def fallback_mock(project): @@ -181,7 +179,7 @@ def fallback_mock(project): def test_ctor_w_explicit_inputs(self): OTHER = 'other' NAMESPACE = 'namespace' - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=OTHER, namespace=NAMESPACE, @@ -196,7 +194,7 @@ def test_ctor_w_explicit_inputs(self): self.assertEqual(list(client._batch_stack), []) def test__push_batch_and__pop_batch(self): - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) batch = client.batch() xact = client.transaction() @@ -221,7 +219,7 @@ def _get_multi(*args, **kw): _called_with.append((args, kw)) return [] - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) client.get_multi = _get_multi @@ -244,7 +242,7 @@ def _get_multi(*args, **kw): _called_with.append((args, kw)) return [_entity] - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) client.get_multi = _get_multi @@ -259,7 +257,7 @@ def _get_multi(*args, **kw): self.assertEqual(_called_with[0][1]['transaction'], TXN_ID) def test_get_multi_no_keys(self): - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) results = client.get_multi([]) self.assertEqual(results, []) @@ -267,7 +265,7 @@ def test_get_multi_no_keys(self): def test_get_multi_miss(self): from google.cloud.datastore.key import Key - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) client._connection._add_lookup_result() key = Key('Kind', 1234, project=self.PROJECT) @@ -288,7 +286,7 @@ def test_get_multi_miss_w_missing(self): path_element.kind = KIND path_element.id = ID - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) # Set missing entity on mock connection. client._connection._add_lookup_result(missing=[missed]) @@ -303,7 +301,7 @@ def test_get_multi_miss_w_missing(self): def test_get_multi_w_missing_non_empty(self): from google.cloud.datastore.key import Key - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) key = Key('Kind', 1234, project=self.PROJECT) @@ -314,7 +312,7 @@ def test_get_multi_w_missing_non_empty(self): def test_get_multi_w_deferred_non_empty(self): from google.cloud.datastore.key import Key - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) key = Key('Kind', 1234, project=self.PROJECT) @@ -328,7 +326,7 @@ def test_get_multi_miss_w_deferred(self): key = Key('Kind', 1234, project=self.PROJECT) # Set deferred entity on mock connection. - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) client._connection._add_lookup_result(deferred=[key.to_protobuf()]) @@ -353,7 +351,7 @@ def test_get_multi_w_deferred_from_backend_but_not_passed(self): entity2_pb = entity_pb2.Entity() entity2_pb.key.CopyFrom(key2_pb) - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) # mock up two separate requests client._connection._add_lookup_result([entity1_pb], deferred=[key2_pb]) @@ -402,7 +400,7 @@ def test_get_multi_hit(self): entity_pb = _make_entity_pb(self.PROJECT, KIND, ID, 'foo', 'Foo') # Make a connection to return the entity pb. - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) client._connection._add_lookup_result([entity_pb]) @@ -429,7 +427,7 @@ def test_get_multi_hit_w_transaction(self): entity_pb = _make_entity_pb(self.PROJECT, KIND, ID, 'foo', 'Foo') # Make a connection to return the entity pb. - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) client._connection._add_lookup_result([entity_pb]) @@ -463,7 +461,7 @@ def test_get_multi_hit_multiple_keys_same_project(self): entity_pb2 = _make_entity_pb(self.PROJECT, KIND, ID2) # Make a connection to return the entity pbs. - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) client._connection._add_lookup_result([entity_pb1, entity_pb2]) @@ -489,14 +487,13 @@ def test_get_multi_hit_multiple_keys_different_project(self): key1 = Key('KIND', 1234, project=PROJECT1) key2 = Key('KIND', 1234, project=PROJECT2) - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) with self.assertRaises(ValueError): client.get_multi([key1, key2]) def test_get_multi_max_loops(self): - import mock from google.cloud.datastore.key import Key KIND = 'Kind' @@ -506,7 +503,7 @@ def test_get_multi_max_loops(self): entity_pb = _make_entity_pb(self.PROJECT, KIND, ID, 'foo', 'Foo') # Make a connection to return the entity pb. - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) client._connection._add_lookup_result([entity_pb]) @@ -532,7 +529,7 @@ def test_put(self): def _put_multi(*args, **kw): _called_with.append((args, kw)) - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) client.put_multi = _put_multi entity = object() @@ -543,7 +540,7 @@ def _put_multi(*args, **kw): self.assertEqual(_called_with[0][1]['entities'], [entity]) def test_put_multi_no_entities(self): - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) self.assertIsNone(client.put_multi([])) @@ -551,7 +548,7 @@ def test_put_multi_w_single_empty_entity(self): # https://github.com/GoogleCloudPlatform/google-cloud-python/issues/649 from google.cloud.datastore.entity import Entity - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) self.assertRaises(ValueError, client.put_multi, Entity()) @@ -562,7 +559,7 @@ def test_put_multi_no_batch_w_partial_key(self): key = entity.key = _Key(self.PROJECT) key._id = None - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) client._connection._commit.append([_KeyPB(key)]) @@ -588,7 +585,7 @@ def test_put_multi_no_batch_w_partial_key(self): def test_put_multi_existing_batch_w_completed_key(self): from google.cloud.datastore.helpers import _property_tuples - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) entity = _Entity(foo=u'bar') key = entity.key = _Key(self.PROJECT) @@ -612,7 +609,7 @@ def test_delete(self): def _delete_multi(*args, **kw): _called_with.append((args, kw)) - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) client.delete_multi = _delete_multi key = object() @@ -623,7 +620,7 @@ def _delete_multi(*args, **kw): self.assertEqual(_called_with[0][1]['keys'], [key]) def test_delete_multi_no_keys(self): - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) result = client.delete_multi([]) self.assertIsNone(result) @@ -632,7 +629,7 @@ def test_delete_multi_no_keys(self): def test_delete_multi_no_batch(self): key = _Key(self.PROJECT) - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) client._connection._commit.append([]) @@ -648,7 +645,7 @@ def test_delete_multi_no_batch(self): self.assertIsNone(transaction_id) def test_delete_multi_w_existing_batch(self): - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) key = _Key(self.PROJECT) @@ -661,7 +658,7 @@ def test_delete_multi_w_existing_batch(self): self.assertEqual(len(client._connection._commit_cw), 0) def test_delete_multi_w_existing_transaction(self): - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) key = _Key(self.PROJECT) @@ -679,7 +676,7 @@ def test_allocate_ids_w_partial_key(self): INCOMPLETE_KEY = _Key(self.PROJECT) INCOMPLETE_KEY._id = None - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) result = client.allocate_ids(INCOMPLETE_KEY, NUM_IDS) @@ -688,7 +685,7 @@ def test_allocate_ids_w_partial_key(self): self.assertEqual([key._id for key in result], list(range(NUM_IDS))) def test_allocate_ids_with_completed_key(self): - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) COMPLETE_KEY = _Key(self.PROJECT) @@ -698,19 +695,17 @@ def test_key_w_project(self): KIND = 'KIND' ID = 1234 - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) self.assertRaises(TypeError, client.key, KIND, ID, project=self.PROJECT) def test_key_wo_project(self): - import mock - KIND = 'KIND' ID = 1234 - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) patch = mock.patch( @@ -727,13 +722,11 @@ def test_key_wo_project(self): self.assertEqual(key.kwargs, expected_kwargs) def test_key_w_namespace(self): - import mock - KIND = 'KIND' ID = 1234 NAMESPACE = object() - creds = object() + creds = _make_credentials() client = self._make_one(namespace=NAMESPACE, credentials=creds) patch = mock.patch( @@ -749,14 +742,12 @@ def test_key_w_namespace(self): self.assertEqual(key.kwargs, expected_kwargs) def test_key_w_namespace_collision(self): - import mock - KIND = 'KIND' ID = 1234 NAMESPACE1 = object() NAMESPACE2 = object() - creds = object() + creds = _make_credentials() client = self._make_one(namespace=NAMESPACE1, credentials=creds) patch = mock.patch( @@ -772,9 +763,7 @@ def test_key_w_namespace_collision(self): self.assertEqual(key.kwargs, expected_kwargs) def test_batch(self): - import mock - - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) patch = mock.patch( @@ -787,9 +776,7 @@ def test_batch(self): self.assertEqual(batch.kwargs, {}) def test_transaction_defaults(self): - import mock - - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) patch = mock.patch( @@ -804,25 +791,23 @@ def test_transaction_defaults(self): def test_query_w_client(self): KIND = 'KIND' - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) - other = self._make_one(credentials=object()) + other = self._make_one(credentials=_make_credentials()) self.assertRaises(TypeError, client.query, kind=KIND, client=other) def test_query_w_project(self): KIND = 'KIND' - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) self.assertRaises(TypeError, client.query, kind=KIND, project=self.PROJECT) def test_query_w_defaults(self): - import mock - - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) patch = mock.patch( @@ -839,8 +824,6 @@ def test_query_w_defaults(self): self.assertEqual(query.kwargs, expected_kwargs) def test_query_explicit(self): - import mock - KIND = 'KIND' NAMESPACE = 'NAMESPACE' ANCESTOR = object() @@ -849,7 +832,7 @@ def test_query_explicit(self): ORDER = ['PROPERTY'] DISTINCT_ON = ['DISTINCT_ON'] - creds = object() + creds = _make_credentials() client = self._make_one(credentials=creds) patch = mock.patch( @@ -880,12 +863,10 @@ def test_query_explicit(self): self.assertEqual(query.kwargs, kwargs) def test_query_w_namespace(self): - import mock - KIND = 'KIND' NAMESPACE = object() - creds = object() + creds = _make_credentials() client = self._make_one(namespace=NAMESPACE, credentials=creds) patch = mock.patch( @@ -903,13 +884,11 @@ def test_query_w_namespace(self): self.assertEqual(query.kwargs, expected_kwargs) def test_query_w_namespace_collision(self): - import mock - KIND = 'KIND' NAMESPACE1 = object() NAMESPACE2 = object() - creds = object() + creds = _make_credentials() client = self._make_one(namespace=NAMESPACE1, credentials=creds) patch = mock.patch( diff --git a/dns/tox.ini b/dns/tox.ini index 227f7ef09ce0..f4dd23f414cb 100644 --- a/dns/tox.ini +++ b/dns/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/dns/unit_tests/test_client.py b/dns/unit_tests/test_client.py index eeca097c63b9..e633207d3a25 100644 --- a/dns/unit_tests/test_client.py +++ b/dns/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) + class TestClient(unittest.TestCase): @@ -30,7 +37,7 @@ def _make_one(self, *args, **kw): def test_ctor(self): from google.cloud.dns.connection import Connection - creds = object() + creds = _make_credentials() http = object() client = self._make_one(project=self.PROJECT, credentials=creds, http=http) @@ -58,7 +65,7 @@ def test_quotas_defaults(self): } CONVERTED = {key: int(value) for key, value in DATA['quota'].items()} - creds = object() + creds = _make_credentials() client = self._make_one(self.PROJECT, creds) conn = client._connection = _Connection(DATA) @@ -93,7 +100,7 @@ def test_quotas_w_kind_key(self): for key, value in DATA['quota'].items()} WITH_KIND = {'quota': DATA['quota'].copy()} WITH_KIND['quota']['kind'] = 'dns#quota' - creds = object() + creds = _make_credentials() client = self._make_one(self.PROJECT, creds) conn = client._connection = _Connection(WITH_KIND) @@ -130,7 +137,7 @@ def test_list_zones_defaults(self): 'dnsName': DNS_2}, ] } - creds = object() + creds = _make_credentials() client = self._make_one(self.PROJECT, creds) conn = client._connection = _Connection(DATA) @@ -175,7 +182,7 @@ def test_list_zones_explicit(self): 'dnsName': DNS_2}, ] } - creds = object() + creds = _make_credentials() client = self._make_one(self.PROJECT, creds) conn = client._connection = _Connection(DATA) @@ -203,7 +210,7 @@ def test_zone_explicit(self): from google.cloud.dns.zone import ManagedZone DESCRIPTION = 'DESCRIPTION' DNS_NAME = 'test.example.com' - creds = object() + creds = _make_credentials() client = self._make_one(self.PROJECT, creds) zone = client.zone(self.ZONE_NAME, DNS_NAME, DESCRIPTION) self.assertIsInstance(zone, ManagedZone) @@ -215,7 +222,7 @@ def test_zone_explicit(self): def test_zone_w_dns_name_wo_description(self): from google.cloud.dns.zone import ManagedZone DNS_NAME = 'test.example.com' - creds = object() + creds = _make_credentials() client = self._make_one(self.PROJECT, creds) zone = client.zone(self.ZONE_NAME, DNS_NAME) self.assertIsInstance(zone, ManagedZone) @@ -226,7 +233,7 @@ def test_zone_w_dns_name_wo_description(self): def test_zone_wo_dns_name(self): from google.cloud.dns.zone import ManagedZone - creds = object() + creds = _make_credentials() client = self._make_one(self.PROJECT, creds) zone = client.zone(self.ZONE_NAME) self.assertIsInstance(zone, ManagedZone) diff --git a/error_reporting/unit_tests/test_client.py b/error_reporting/unit_tests/test_client.py index ca042b511ddf..2750cd20dba4 100644 --- a/error_reporting/unit_tests/test_client.py +++ b/error_reporting/unit_tests/test_client.py @@ -15,6 +15,13 @@ import unittest +import mock + + +def _make_credentials(): + import google.auth.credentials + return mock.Mock(spec=google.auth.credentials.Credentials) + class TestClient(unittest.TestCase): @@ -38,14 +45,14 @@ def _makeHTTP(self, *args, **kw): VERSION = 'myversion' def test_ctor_default(self): - CREDENTIALS = object() + CREDENTIALS = _make_credentials() target = self._make_one(project=self.PROJECT, credentials=CREDENTIALS) self.assertEquals(target.service, target.DEFAULT_SERVICE) self.assertEquals(target.version, None) def test_ctor_params(self): - CREDENTIALS = object() + CREDENTIALS = _make_credentials() target = self._make_one(project=self.PROJECT, credentials=CREDENTIALS, service=self.SERVICE, @@ -54,7 +61,7 @@ def test_ctor_params(self): self.assertEquals(target.version, self.VERSION) def test_report_exception(self): - CREDENTIALS = object() + CREDENTIALS = _make_credentials() target = self._make_one(project=self.PROJECT, credentials=CREDENTIALS) @@ -74,7 +81,7 @@ def test_report_exception(self): self.assertIn('test_client.py', payload['message']) def test_report_exception_with_service_version_in_constructor(self): - CREDENTIALS = object() + CREDENTIALS = _make_credentials() SERVICE = "notdefault" VERSION = "notdefaultversion" target = self._make_one(project=self.PROJECT, @@ -109,7 +116,7 @@ def test_report_exception_with_service_version_in_constructor(self): self.assertEquals(payload['context']['user'], USER) def test_report(self): - CREDENTIALS = object() + CREDENTIALS = _make_credentials() target = self._make_one(project=self.PROJECT, credentials=CREDENTIALS) diff --git a/logging/unit_tests/test__gax.py b/logging/unit_tests/test__gax.py index 2d7280996a0b..4d269236e3e6 100644 --- a/logging/unit_tests/test__gax.py +++ b/logging/unit_tests/test__gax.py @@ -14,6 +14,7 @@ import unittest +import mock try: # pylint: disable=unused-import @@ -27,6 +28,13 @@ from google.cloud._testing import _GAXBaseAPI +def _make_credentials(): + # pylint: disable=redefined-outer-name + import google.auth.credentials + # pylint: enable=redefined-outer-name + return mock.Mock(spec=google.auth.credentials.Credentials) + + class _Base(object): PROJECT = 'PROJECT' PROJECT_PATH = 'projects/%s' % (PROJECT,) @@ -79,7 +87,7 @@ def test_list_entries_no_paging(self): text_payload=TEXT) response = _GAXPageIterator([entry_pb], page_token=TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) - client = Client(project=self.PROJECT, credentials=object(), + client = Client(project=self.PROJECT, credentials=_make_credentials(), use_gax=True) api = self._make_one(gax_api, client) @@ -136,7 +144,7 @@ def _list_entries_with_paging_helper(self, payload, struct_pb): json_payload=struct_pb) response = _GAXPageIterator([entry_pb], page_token=NEW_TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) - client = Client(project=self.PROJECT, credentials=object(), + client = Client(project=self.PROJECT, credentials=_make_credentials(), use_gax=True) api = self._make_one(gax_api, client) @@ -277,7 +285,7 @@ def test_list_entries_with_extra_properties(self): response = _GAXPageIterator([entry_pb], page_token=NEW_TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) - client = Client(project=self.PROJECT, credentials=object(), + client = Client(project=self.PROJECT, credentials=_make_credentials(), use_gax=True) api = self._make_one(gax_api, client) @@ -1073,7 +1081,6 @@ def _call_fut(self, client): return make_gax_logging_api(client) def test_it(self): - import mock from google.cloud.logging._gax import _LoggingAPI from google.cloud.logging._gax import DEFAULT_USER_AGENT @@ -1119,7 +1126,6 @@ def _call_fut(self, client): return make_gax_metrics_api(client) def test_it(self): - import mock from google.cloud.logging._gax import _MetricsAPI from google.cloud.logging._gax import DEFAULT_USER_AGENT @@ -1165,7 +1171,6 @@ def _call_fut(self, client): return make_gax_sinks_api(client) def test_it(self): - import mock from google.cloud.logging._gax import _SinksAPI from google.cloud.logging._gax import DEFAULT_USER_AGENT diff --git a/logging/unit_tests/test__http.py b/logging/unit_tests/test__http.py index 8dccefa1dcb0..6fe8c825feef 100644 --- a/logging/unit_tests/test__http.py +++ b/logging/unit_tests/test__http.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 TestConnection(unittest.TestCase): @@ -29,7 +36,7 @@ def _make_one(self, *args, **kw): return self._get_target_class()(*args, **kw) def test_default_url(self): - creds = object() + creds = _make_credentials() conn = self._make_one(creds) self.assertEqual(conn.credentials, creds) @@ -91,7 +98,7 @@ def test_list_entries_no_paging(self): }], 'nextPageToken': TOKEN, } - client = Client(project=self.PROJECT, credentials=object(), + client = Client(project=self.PROJECT, credentials=_make_credentials(), use_gax=False) client._connection = _Connection(RETURNED) api = self._make_one(client) @@ -169,7 +176,7 @@ def test_list_entries_w_paging(self): self.PROJECT, self.LOGGER_NAME), }], } - client = Client(project=self.PROJECT, credentials=object(), + client = Client(project=self.PROJECT, credentials=_make_credentials(), use_gax=False) client._connection = _Connection(RETURNED) api = self._make_one(client) @@ -301,7 +308,7 @@ def _make_one(self, *args, **kw): return self._get_target_class()(*args, **kw) def test_ctor(self): - connection = object() + connection = _make_credentials() client = _Client(connection) api = self._make_one(client) self.assertIs(api._connection, connection) diff --git a/logging/unit_tests/test_client.py b/logging/unit_tests/test_client.py index 4e0cf9129c67..d4a3c1a21855 100644 --- a/logging/unit_tests/test_client.py +++ b/logging/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) + class TestClient(unittest.TestCase): @@ -35,14 +42,15 @@ def _make_one(self, *args, **kw): return self._get_target_class()(*args, **kw) def test_ctor(self): - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) self.assertEqual(client.project, self.PROJECT) def test_logging_api_wo_gax(self): from google.cloud.logging._http import _LoggingAPI - client = self._make_one(self.PROJECT, credentials=object(), + client = self._make_one(self.PROJECT, + credentials=_make_credentials(), use_gax=False) conn = client._connection = object() api = client.logging_api @@ -54,8 +62,6 @@ def test_logging_api_wo_gax(self): self.assertIs(again, api) def test_logging_api_w_gax(self): - import mock - clients = [] api_obj = object() @@ -63,7 +69,7 @@ def make_api(client_obj): clients.append(client_obj) return api_obj - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds, use_gax=True) @@ -80,10 +86,9 @@ def make_api(client_obj): self.assertIs(again, api) def test_no_gax_ctor(self): - import mock from google.cloud.logging._http import _LoggingAPI - creds = object() + creds = _make_credentials() patch = mock.patch( 'google.cloud.logging.client._USE_GAX', new=True) @@ -98,7 +103,7 @@ def test_sinks_api_wo_gax(self): from google.cloud.logging._http import _SinksAPI client = self._make_one( - self.PROJECT, credentials=object(), + self.PROJECT, credentials=_make_credentials(), use_gax=False) conn = client._connection = object() @@ -111,8 +116,6 @@ def test_sinks_api_wo_gax(self): self.assertIs(again, api) def test_sinks_api_w_gax(self): - import mock - clients = [] api_obj = object() @@ -120,7 +123,7 @@ def make_api(client_obj): clients.append(client_obj) return api_obj - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds, use_gax=True) @@ -140,7 +143,7 @@ def test_metrics_api_wo_gax(self): from google.cloud.logging._http import _MetricsAPI client = self._make_one( - self.PROJECT, credentials=object(), + self.PROJECT, credentials=_make_credentials(), use_gax=False) conn = client._connection = object() @@ -153,8 +156,6 @@ def test_metrics_api_wo_gax(self): self.assertIs(again, api) def test_metrics_api_w_gax(self): - import mock - clients = [] api_obj = object() @@ -162,7 +163,7 @@ def make_api(client_obj): clients.append(client_obj) return api_obj - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds, use_gax=True) @@ -180,7 +181,7 @@ def make_api(client_obj): def test_logger(self): from google.cloud.logging.logger import Logger - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) logger = client.logger(self.LOGGER_NAME) self.assertIsInstance(logger, Logger) @@ -204,7 +205,7 @@ def test_list_entries_defaults(self): 'logName': 'projects/%s/logs/%s' % ( self.PROJECT, self.LOGGER_NAME), }] - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds, use_gax=False) returned = { @@ -269,7 +270,7 @@ def test_list_entries_explicit(self): 'logName': 'projects/%s/logs/%s' % ( self.PROJECT, self.LOGGER_NAME), }] - client = self._make_one(self.PROJECT, credentials=object(), + client = self._make_one(self.PROJECT, credentials=_make_credentials(), use_gax=False) returned = {'entries': ENTRIES} client._connection = _Connection(returned) @@ -320,7 +321,7 @@ def test_list_entries_explicit(self): def test_sink_defaults(self): from google.cloud.logging.sink import Sink - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) sink = client.sink(self.SINK_NAME) self.assertIsInstance(sink, Sink) @@ -332,7 +333,7 @@ def test_sink_defaults(self): def test_sink_explicit(self): from google.cloud.logging.sink import Sink - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) sink = client.sink(self.SINK_NAME, self.FILTER, self.DESTINATION_URI) self.assertIsInstance(sink, Sink) @@ -355,7 +356,8 @@ def test_list_sinks_no_paging(self): 'filter': FILTER, 'destination': self.DESTINATION_URI, }] - client = self._make_one(project=PROJECT, credentials=object(), + client = self._make_one(project=PROJECT, + credentials=_make_credentials(), use_gax=False) returned = { 'sinks': SINKS, @@ -401,8 +403,8 @@ def test_list_sinks_with_paging(self): 'filter': FILTER, 'destination': self.DESTINATION_URI, }] - client = self._make_one(project=PROJECT, credentials=object(), - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=_make_credentials(), use_gax=False) returned = { 'sinks': SINKS, } @@ -437,7 +439,7 @@ def test_list_sinks_with_paging(self): def test_metric_defaults(self): from google.cloud.logging.metric import Metric - creds = object() + creds = _make_credentials() client_obj = self._make_one(project=self.PROJECT, credentials=creds) metric = client_obj.metric(self.METRIC_NAME) @@ -450,7 +452,7 @@ def test_metric_defaults(self): def test_metric_explicit(self): from google.cloud.logging.metric import Metric - creds = object() + creds = _make_credentials() client_obj = self._make_one(project=self.PROJECT, credentials=creds) metric = client_obj.metric(self.METRIC_NAME, self.FILTER, @@ -471,7 +473,7 @@ def test_list_metrics_no_paging(self): 'description': self.DESCRIPTION, }] client = self._make_one( - project=self.PROJECT, credentials=object(), + project=self.PROJECT, credentials=_make_credentials(), use_gax=False) returned = { 'metrics': metrics, @@ -513,7 +515,7 @@ def test_list_metrics_with_paging(self): 'description': self.DESCRIPTION, }] client = self._make_one( - project=self.PROJECT, credentials=object(), + project=self.PROJECT, credentials=_make_credentials(), use_gax=False) returned = { 'metrics': metrics, @@ -557,7 +559,7 @@ def test_get_default_handler_app_engine(self): from google.cloud.logging.handlers import AppEngineHandler client = self._make_one(project=self.PROJECT, - credentials=object(), + credentials=_make_credentials(), use_gax=False) with _Monkey(_MUT, _LOG_PATH_TEMPLATE='{pid}'): @@ -573,7 +575,7 @@ def test_get_default_handler_container_engine(self): from google.cloud.logging.handlers import ContainerEngineHandler client = self._make_one(project=self.PROJECT, - credentials=object(), + credentials=_make_credentials(), use_gax=False) with _Monkey(os, environ={_CONTAINER_ENGINE_ENV: 'True'}): @@ -583,11 +585,10 @@ def test_get_default_handler_container_engine(self): def test_get_default_handler_general(self): import httplib2 - import mock from google.cloud.logging.handlers import CloudLoggingHandler http_mock = mock.Mock(spec=httplib2.Http) - credentials = object() + credentials = _make_credentials() deepcopy = mock.Mock(return_value=http_mock) with mock.patch('copy.deepcopy', new=deepcopy): @@ -601,13 +602,12 @@ def test_get_default_handler_general(self): def test_setup_logging(self): import httplib2 - import mock http_mock = mock.Mock(spec=httplib2.Http) deepcopy = mock.Mock(return_value=http_mock) setup_logging = mock.Mock() - credentials = object() + credentials = _make_credentials() with mock.patch('copy.deepcopy', new=deepcopy): with mock.patch('google.cloud.logging.client.setup_logging', diff --git a/logging/unit_tests/test_logger.py b/logging/unit_tests/test_logger.py index cbe149102445..15e7e7146b80 100644 --- a/logging/unit_tests/test_logger.py +++ b/logging/unit_tests/test_logger.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 TestLogger(unittest.TestCase): @@ -352,7 +359,8 @@ def test_list_entries_defaults(self): TOKEN = 'TOKEN' - client = Client(project=self.PROJECT, credentials=object(), + client = Client(project=self.PROJECT, + credentials=_make_credentials(), use_gax=False) returned = { 'nextPageToken': TOKEN, @@ -389,7 +397,8 @@ def test_list_entries_explicit(self): FILTER = 'resource.type:global' TOKEN = 'TOKEN' PAGE_SIZE = 42 - client = Client(project=self.PROJECT, credentials=object(), + client = Client(project=self.PROJECT, + credentials=_make_credentials(), use_gax=False) client._connection = _Connection({}) logger = self._make_one(self.LOGGER_NAME, client=client) @@ -440,7 +449,7 @@ def test_ctor_defaults(self): def test_log_text_defaults(self): TEXT = 'This is the entry text' - client = _Client(project=self.PROJECT, connection=object()) + client = _Client(project=self.PROJECT, connection=_make_credentials()) logger = _Logger() batch = self._make_one(logger, client=client) batch.log_text(TEXT) @@ -460,7 +469,7 @@ def test_log_text_explicit(self): 'requestUrl': URI, 'status': STATUS, } - client = _Client(project=self.PROJECT, connection=object()) + client = _Client(project=self.PROJECT, connection=_make_credentials()) logger = _Logger() batch = self._make_one(logger, client=client) batch.log_text(TEXT, labels=LABELS, insert_id=IID, severity=SEVERITY, @@ -470,7 +479,7 @@ def test_log_text_explicit(self): def test_log_struct_defaults(self): STRUCT = {'message': 'Message text', 'weather': 'partly cloudy'} - client = _Client(project=self.PROJECT, connection=object()) + client = _Client(project=self.PROJECT, connection=_make_credentials()) logger = _Logger() batch = self._make_one(logger, client=client) batch.log_struct(STRUCT) @@ -490,7 +499,7 @@ def test_log_struct_explicit(self): 'requestUrl': URI, 'status': STATUS, } - client = _Client(project=self.PROJECT, connection=object()) + client = _Client(project=self.PROJECT, connection=_make_credentials()) logger = _Logger() batch = self._make_one(logger, client=client) batch.log_struct(STRUCT, labels=LABELS, insert_id=IID, @@ -501,7 +510,7 @@ def test_log_struct_explicit(self): def test_log_proto_defaults(self): from google.protobuf.struct_pb2 import Struct, Value message = Struct(fields={'foo': Value(bool_value=True)}) - client = _Client(project=self.PROJECT, connection=object()) + client = _Client(project=self.PROJECT, connection=_make_credentials()) logger = _Logger() batch = self._make_one(logger, client=client) batch.log_proto(message) @@ -522,7 +531,7 @@ def test_log_proto_explicit(self): 'requestUrl': URI, 'status': STATUS, } - client = _Client(project=self.PROJECT, connection=object()) + client = _Client(project=self.PROJECT, connection=_make_credentials()) logger = _Logger() batch = self._make_one(logger, client=client) batch.log_proto(message, labels=LABELS, insert_id=IID, @@ -532,7 +541,7 @@ def test_log_proto_explicit(self): def test_commit_w_invalid_entry_type(self): logger = _Logger() - client = _Client(project=self.PROJECT, connection=object()) + client = _Client(project=self.PROJECT, connection=_make_credentials()) batch = self._make_one(logger, client) batch.entries.append(('bogus', 'BOGUS', None, None, None, None)) with self.assertRaises(ValueError): diff --git a/monitoring/unit_tests/test_client.py b/monitoring/unit_tests/test_client.py index 1d6807e5ca46..2baba6309421 100644 --- a/monitoring/unit_tests/test_client.py +++ b/monitoring/unit_tests/test_client.py @@ -14,9 +14,17 @@ import unittest +import mock + + PROJECT = 'my-project' +def _make_credentials(): + import google.auth.credentials + return mock.Mock(spec=google.auth.credentials.Credentials) + + class TestClient(unittest.TestCase): @staticmethod @@ -85,7 +93,8 @@ def P(timestamp, value): RESPONSE = {'timeSeries': [SERIES1, SERIES2]} - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) connection = client._connection = _Connection(RESPONSE) # A simple query. In practice, it can be very convenient to let the @@ -138,7 +147,8 @@ def test_metric_descriptor_factory(self): VALUE_TYPE = 'DOUBLE' DESCRIPTION = 'This is my metric.' - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) client._connection = _Connection() # For safety's sake. descriptor = client.metric_descriptor(TYPE, metric_kind=METRIC_KIND, @@ -164,7 +174,8 @@ def test_metric_factory(self): 'instance_name': 'my-instance' } - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) client._connection = _Connection() # For safety's sake. metric = client.metric(TYPE, LABELS) self.assertEqual(metric.type, TYPE) @@ -177,7 +188,8 @@ def test_resource_factory(self): 'zone': 'us-central1-f' } - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) client._connection = _Connection() # For safety's sake. resource = client.resource(TYPE, LABELS) self.assertEqual(resource.type, TYPE) @@ -185,7 +197,6 @@ def test_resource_factory(self): def test_timeseries_factory_gauge(self): import datetime - import mock from google.cloud._helpers import _datetime_to_rfc3339 METRIC_TYPE = 'custom.googleapis.com/my_metric' METRIC_LABELS = { @@ -202,7 +213,8 @@ def test_timeseries_factory_gauge(self): TIME1 = datetime.datetime.utcnow() TIME1_STR = _datetime_to_rfc3339(TIME1, ignore_zone=False) - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) client._connection = _Connection() # For safety's sake. metric = client.metric(METRIC_TYPE, METRIC_LABELS) resource = client.resource(RESOURCE_TYPE, RESOURCE_LABELS) @@ -242,7 +254,8 @@ def test_timeseries_factory_cumulative(self): 'zone': 'us-central1-f' } - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) client._connection = _Connection() # For safety's sake. resource = client.resource(RESOURCE_TYPE, RESOURCE_LABELS) @@ -296,7 +309,8 @@ def test_fetch_metric_descriptor(self): # This test is identical to TestMetricDescriptor.test_fetch() # except for the following three lines. - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) connection = client._connection = _Connection(METRIC_DESCRIPTOR) descriptor = client.fetch_metric_descriptor(TYPE) @@ -340,7 +354,8 @@ def test_list_metric_descriptors(self): # This test is identical to TestMetricDescriptor.test_list() # except for the following three lines. - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) connection = client._connection = _Connection(RESPONSE) descriptors = client.list_metric_descriptors() @@ -385,7 +400,8 @@ def test_fetch_resource_descriptor(self): # This test is identical to TestResourceDescriptor.test_fetch() # except for the following three lines. - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) connection = client._connection = _Connection(RESOURCE_DESCRIPTOR) descriptor = client.fetch_resource_descriptor(TYPE) @@ -433,7 +449,8 @@ def test_list_resource_descriptors(self): # This test is identical to TestResourceDescriptor.test_list() # except for the following three lines. - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) connection = client._connection = _Connection(RESPONSE) descriptors = client.list_resource_descriptors() @@ -460,7 +477,8 @@ def test_group(self): FILTER = 'resource.type = "gce_instance"' IS_CLUSTER = False - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) group = client.group(GROUP_ID, display_name=DISPLAY_NAME, parent_id=PARENT_ID, filter_string=FILTER, is_cluster=IS_CLUSTER) @@ -472,7 +490,8 @@ def test_group(self): self.assertEqual(group.is_cluster, IS_CLUSTER) def test_group_defaults(self): - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) group = client.group() self.assertIsNone(group.id) @@ -499,7 +518,8 @@ def test_fetch_group(self): 'isCluster': IS_CLUSTER } - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) connection = client._connection = _Connection(GROUP) group = client.fetch_group(GROUP_ID) @@ -532,7 +552,8 @@ def test_list_groups(self): RESPONSE = { 'group': [GROUP], } - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) connection = client._connection = _Connection(RESPONSE) groups = client.list_groups() @@ -552,7 +573,8 @@ def test_list_groups(self): def test_write_time_series(self): PATH = '/projects/{project}/timeSeries/'.format(project=PROJECT) - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) RESOURCE_TYPE = 'gce_instance' RESOURCE_LABELS = { @@ -594,7 +616,8 @@ def test_write_time_series(self): def test_write_point(self): import datetime PATH = '/projects/{project}/timeSeries/'.format(project=PROJECT) - client = self._make_one(project=PROJECT, credentials=object()) + client = self._make_one( + project=PROJECT, credentials=_make_credentials()) RESOURCE_TYPE = 'gce_instance' RESOURCE_LABELS = { diff --git a/pubsub/unit_tests/test__gax.py b/pubsub/unit_tests/test__gax.py index ac13bc36758f..4f2037d7c4dd 100644 --- a/pubsub/unit_tests/test__gax.py +++ b/pubsub/unit_tests/test__gax.py @@ -14,6 +14,7 @@ import unittest +import mock try: # pylint: disable=unused-import @@ -27,6 +28,13 @@ from google.cloud._testing import _GAXBaseAPI +def _make_credentials(): + # pylint: disable=redefined-outer-name + import google.auth.credentials + # pylint: enable=redefined-outer-name + return mock.Mock(spec=google.auth.credentials.Credentials) + + class _Base(object): PROJECT = 'PROJECT' PROJECT_PATH = 'projects/%s' % (PROJECT,) @@ -431,7 +439,7 @@ def test_list_subscriptions_no_paging(self): push_config=push_cfg_pb) response = _GAXPageIterator([sub_pb]) gax_api = _GAXSubscriberAPI(_list_subscriptions_response=response) - creds = object() + creds = _make_credentials() client = Client(project=self.PROJECT, credentials=creds) api = self._make_one(gax_api, client) @@ -478,7 +486,7 @@ def test_list_subscriptions_with_paging(self): response = _GAXPageIterator([sub_pb], page_token=NEW_TOKEN) gax_api = _GAXSubscriberAPI(_list_subscriptions_response=response) client = _Client(self.PROJECT) - creds = object() + creds = _make_credentials() client = Client(project=self.PROJECT, credentials=creds) api = self._make_one(gax_api, client) @@ -897,7 +905,6 @@ def _call_fut(self, connection): return make_gax_publisher_api(connection) def test_live_api(self): - import mock from google.cloud.pubsub._gax import DEFAULT_USER_AGENT channels = [] @@ -916,7 +923,7 @@ def make_channel(*args): mock_publisher_api.SERVICE_ADDRESS = host - creds = object() + creds = _make_credentials() connection = _Connection(in_emulator=False, credentials=creds) patch = mock.patch.multiple( @@ -932,8 +939,6 @@ def make_channel(*args): [(creds, DEFAULT_USER_AGENT, host)]) def test_emulator(self): - import mock - channels = [] mock_result = object() insecure_args = [] @@ -969,7 +974,6 @@ def _call_fut(self, connection): return make_gax_subscriber_api(connection) def test_live_api(self): - import mock from google.cloud.pubsub._gax import DEFAULT_USER_AGENT channels = [] @@ -988,7 +992,7 @@ def make_channel(*args): mock_subscriber_api.SERVICE_ADDRESS = host - creds = object() + creds = _make_credentials() connection = _Connection(in_emulator=False, credentials=creds) patch = mock.patch.multiple( @@ -1004,8 +1008,6 @@ def make_channel(*args): [(creds, DEFAULT_USER_AGENT, host)]) def test_emulator(self): - import mock - channels = [] mock_result = object() insecure_args = [] diff --git a/pubsub/unit_tests/test__http.py b/pubsub/unit_tests/test__http.py index be0f69835683..955fc06a9104 100644 --- a/pubsub/unit_tests/test__http.py +++ b/pubsub/unit_tests/test__http.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 _Base(unittest.TestCase): PROJECT = 'PROJECT' @@ -42,7 +49,6 @@ def test_default_url(self): self.assertEqual(conn.api_base_url, klass.API_BASE_URL) def test_custom_url_from_env(self): - import mock from google.cloud.environment_vars import PUBSUB_EMULATOR HOST = 'localhost:8187' @@ -454,7 +460,7 @@ def test_list_subscriptions_no_paging(self): SUB_INFO = {'name': self.SUB_PATH, 'topic': self.TOPIC_PATH} RETURNED = {'subscriptions': [SUB_INFO]} connection = _Connection(RETURNED) - creds = object() + creds = _make_credentials() client = Client(project=self.PROJECT, credentials=creds) client._connection = connection api = self._make_one(client) @@ -497,7 +503,7 @@ def test_list_subscriptions_with_paging(self): 'nextPageToken': 'TOKEN2', } connection = _Connection(RETURNED) - creds = object() + creds = _make_credentials() client = Client(project=self.PROJECT, credentials=creds) client._connection = connection api = self._make_one(client) diff --git a/pubsub/unit_tests/test_client.py b/pubsub/unit_tests/test_client.py index 1e549f8f7f4b..3bde01417359 100644 --- a/pubsub/unit_tests/test_client.py +++ b/pubsub/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) + class TestClient(unittest.TestCase): PROJECT = 'PROJECT' @@ -33,7 +40,7 @@ def _make_one(self, *args, **kw): def test_publisher_api_wo_gax(self): from google.cloud.pubsub._http import _PublisherAPI - creds = object() + creds = _make_credentials() client = self._make_one( project=self.PROJECT, credentials=creds, @@ -49,10 +56,9 @@ def test_publisher_api_wo_gax(self): self.assertIs(again, api) def test_no_gax_ctor(self): - import mock from google.cloud.pubsub._http import _PublisherAPI - creds = object() + creds = _make_credentials() with mock.patch('google.cloud.pubsub.client._USE_GAX', new=True): client = self._make_one(project=self.PROJECT, credentials=creds, @@ -63,8 +69,6 @@ def test_no_gax_ctor(self): self.assertIsInstance(api, _PublisherAPI) def test_publisher_api_w_gax(self): - import mock - wrapped = object() _called_with = [] @@ -78,7 +82,7 @@ def __init__(self, _wrapped, client): self._wrapped = _wrapped self._client = client - creds = object() + creds = _make_credentials() client = self._make_one( project=self.PROJECT, credentials=creds, use_gax=True) @@ -102,7 +106,7 @@ def __init__(self, _wrapped, client): def test_subscriber_api_wo_gax(self): from google.cloud.pubsub._http import _SubscriberAPI - creds = object() + creds = _make_credentials() client = self._make_one( project=self.PROJECT, credentials=creds, use_gax=False) @@ -117,8 +121,6 @@ def test_subscriber_api_wo_gax(self): self.assertIs(again, api) def test_subscriber_api_w_gax(self): - import mock - wrapped = object() _called_with = [] @@ -132,7 +134,7 @@ def __init__(self, _wrapped, client): self._wrapped = _wrapped self._client = client - creds = object() + creds = _make_credentials() client = self._make_one( project=self.PROJECT, credentials=creds, use_gax=True) @@ -155,7 +157,7 @@ def __init__(self, _wrapped, client): def test_iam_policy_api(self): from google.cloud.pubsub._http import _IAMPolicyAPI - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) conn = client._connection = object() api = client.iam_policy_api @@ -168,7 +170,7 @@ def test_iam_policy_api(self): def test_list_topics_no_paging(self): from google.cloud.pubsub.topic import Topic - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) client._connection = object() api = _FauxPublisherAPI(items=[Topic(self.TOPIC_NAME, client)]) @@ -191,7 +193,7 @@ def test_list_topics_with_paging(self): TOKEN1 = 'TOKEN1' TOKEN2 = 'TOKEN2' SIZE = 1 - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) client._connection = object() api = _FauxPublisherAPI([Topic(self.TOPIC_NAME, client)], TOKEN2) @@ -209,7 +211,7 @@ def test_list_topics_with_paging(self): self.assertEqual(api._listed_topics, (self.PROJECT, 1, TOKEN1)) def test_list_topics_missing_key(self): - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds) client._connection = object() api = _FauxPublisherAPI() @@ -229,7 +231,7 @@ def test_list_subscriptions_no_paging(self): from google.cloud.pubsub.topic import Topic SUB_INFO = {'name': self.SUB_PATH, 'topic': self.TOPIC_PATH} - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds, use_gax=False) returned = {'subscriptions': [SUB_INFO]} @@ -267,7 +269,7 @@ def test_list_subscriptions_with_paging(self): from google.cloud.pubsub.topic import Topic SUB_INFO = {'name': self.SUB_PATH, 'topic': self.TOPIC_PATH} - creds = object() + creds = _make_credentials() client = self._make_one(project=self.PROJECT, credentials=creds, use_gax=False) @@ -320,7 +322,7 @@ def test_list_subscriptions_with_paging(self): def test_list_subscriptions_w_missing_key(self): PROJECT = 'PROJECT' - creds = object() + creds = _make_credentials() client = self._make_one(project=PROJECT, credentials=creds) client._connection = object() @@ -338,7 +340,7 @@ def test_list_subscriptions_w_missing_key(self): def test_topic(self): PROJECT = 'PROJECT' TOPIC_NAME = 'TOPIC_NAME' - creds = object() + creds = _make_credentials() client_obj = self._make_one(project=PROJECT, credentials=creds) new_topic = client_obj.topic(TOPIC_NAME) diff --git a/pubsub/unit_tests/test_topic.py b/pubsub/unit_tests/test_topic.py index 0a7e4cede8d7..f23967081fa5 100644 --- a/pubsub/unit_tests/test_topic.py +++ b/pubsub/unit_tests/test_topic.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 TestTopic(unittest.TestCase): PROJECT = 'PROJECT' @@ -136,7 +143,6 @@ def test_publish_single_bytes_wo_attrs_w_bound_client(self): def test_publish_single_bytes_wo_attrs_w_add_timestamp_alt_client(self): import datetime - import mock from google.cloud._helpers import _RFC3339_MICROS NOW = datetime.datetime.utcnow() @@ -314,8 +320,8 @@ def test_list_subscriptions_no_paging(self): from google.cloud.pubsub.client import Client from google.cloud.pubsub.subscription import Subscription - client = Client(project=self.PROJECT, credentials=object(), - use_gax=False) + client = Client(project=self.PROJECT, + credentials=_make_credentials(), use_gax=False) SUB_NAME_1 = 'subscription_1' SUB_PATH_1 = 'projects/%s/subscriptions/%s' % ( @@ -364,8 +370,8 @@ def test_list_subscriptions_with_paging(self): from google.cloud.pubsub.client import Client from google.cloud.pubsub.subscription import Subscription - client = Client(project=self.PROJECT, credentials=object(), - use_gax=False) + client = Client(project=self.PROJECT, + credentials=_make_credentials(), use_gax=False) SUB_NAME_1 = 'subscription_1' SUB_PATH_1 = 'projects/%s/subscriptions/%s' % ( @@ -414,8 +420,8 @@ def test_list_subscriptions_with_paging(self): def test_list_subscriptions_missing_key(self): from google.cloud.pubsub.client import Client - client = Client(project=self.PROJECT, credentials=object(), - use_gax=False) + client = Client(project=self.PROJECT, + credentials=_make_credentials(), use_gax=False) client._connection = _Connection({}) topic = self._make_one(self.TOPIC_NAME, client=client) diff --git a/resource_manager/tox.ini b/resource_manager/tox.ini index 55414628dff7..9e1f41a089d3 100644 --- a/resource_manager/tox.ini +++ b/resource_manager/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/resource_manager/unit_tests/test_client.py b/resource_manager/unit_tests/test_client.py index 72464f085005..3d19c6d717dc 100644 --- a/resource_manager/unit_tests/test_client.py +++ b/resource_manager/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) + class TestClient(unittest.TestCase): @@ -29,7 +36,7 @@ def test_constructor(self): from google.cloud.resource_manager.connection import Connection http = object() - credentials = object() + credentials = _make_credentials() client = self._make_one(credentials=credentials, http=http) self.assertIsInstance(client._connection, Connection) self.assertEqual(client._connection._credentials, credentials) @@ -38,7 +45,7 @@ def test_constructor(self): def test_new_project_factory(self): from google.cloud.resource_manager.project import Project - credentials = object() + credentials = _make_credentials() client = self._make_one(credentials=credentials) project_id = 'project_id' name = object() @@ -66,7 +73,7 @@ def test_fetch_project(self): 'lifecycleState': 'ACTIVE', } - credentials = object() + credentials = _make_credentials() client = self._make_one(credentials=credentials) # Patch the connection with one we can easily control. client._connection = _Connection(project_resource) @@ -81,7 +88,7 @@ def test_fetch_project(self): def test_list_projects_return_type(self): from google.cloud.iterator import HTTPIterator - credentials = object() + credentials = _make_credentials() client = self._make_one(credentials=credentials) # Patch the connection with one we can easily control. client._connection = _Connection({}) @@ -90,7 +97,7 @@ def test_list_projects_return_type(self): self.assertIsInstance(results, HTTPIterator) def test_list_projects_no_paging(self): - credentials = object() + credentials = _make_credentials() client = self._make_one(credentials=credentials) PROJECT_ID = 'project-id' @@ -118,7 +125,7 @@ def test_list_projects_no_paging(self): self.assertEqual(project.status, STATUS) def test_list_projects_with_paging(self): - credentials = object() + credentials = _make_credentials() client = self._make_one(credentials=credentials) PROJECT_ID1 = 'project-id' @@ -181,7 +188,7 @@ def test_list_projects_with_paging(self): }) def test_list_projects_with_filter(self): - credentials = object() + credentials = _make_credentials() client = self._make_one(credentials=credentials) PROJECT_ID = 'project-id' @@ -220,7 +227,7 @@ def test_list_projects_with_filter(self): def test_page_empty_response(self): from google.cloud.iterator import Page - credentials = object() + credentials = _make_credentials() client = self._make_one(credentials=credentials) iterator = client.list_projects() page = Page(iterator, (), None) @@ -246,7 +253,7 @@ def test_page_non_empty_response(self): 'lifecycleState': project_lifecycle_state, } response = {'projects': [api_resource]} - credentials = object() + credentials = _make_credentials() client = self._make_one(credentials=credentials) def dummy_response(): diff --git a/runtimeconfig/tox.ini b/runtimeconfig/tox.ini index def122db9960..03a0428a1ee0 100644 --- a/runtimeconfig/tox.ini +++ b/runtimeconfig/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/runtimeconfig/unit_tests/test_client.py b/runtimeconfig/unit_tests/test_client.py index caecec3af843..aa7f7c20a176 100644 --- a/runtimeconfig/unit_tests/test_client.py +++ b/runtimeconfig/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) + class TestClient(unittest.TestCase): @@ -28,7 +35,7 @@ def _make_one(self, *args, **kw): def test_config(self): PROJECT = 'PROJECT' CONFIG_NAME = 'config_name' - creds = object() + creds = _make_credentials() client_obj = self._make_one(project=PROJECT, credentials=creds) new_config = client_obj.config(CONFIG_NAME) diff --git a/speech/tox.ini b/speech/tox.ini index f829dcf5b262..4ded9cd86434 100644 --- a/speech/tox.ini +++ b/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/speech/unit_tests/test__gax.py b/speech/unit_tests/test__gax.py index 4fed73c629b0..2c34b93abd77 100644 --- a/speech/unit_tests/test__gax.py +++ b/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/speech/unit_tests/test_client.py b/speech/unit_tests/test_client.py index 0ba0cc932679..bf11eb4f7c65 100644 --- a/speech/unit_tests/test_client.py +++ b/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 diff --git a/storage/unit_tests/test_batch.py b/storage/unit_tests/test_batch.py index b0abea6dcc85..39cebbf680d1 100644 --- a/storage/unit_tests/test_batch.py +++ b/storage/unit_tests/test_batch.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 TestMIMEApplicationHTTP(unittest.TestCase): @@ -89,7 +96,7 @@ def test_ctor(self): def test_current(self): from google.cloud.storage.client import Client project = 'PROJECT' - credentials = object() + credentials = _make_credentials() client = Client(project=project, credentials=credentials) batch1 = self._make_one(client) self.assertIsNone(batch1.current()) @@ -378,7 +385,7 @@ def test_as_context_mgr_wo_error(self): expected['content-type'] = 'multipart/mixed; boundary="DEADBEEF="' http = _HTTP((expected, _THREE_PART_MIME_RESPONSE)) project = 'PROJECT' - credentials = object() + credentials = _make_credentials() client = Client(project=project, credentials=credentials) client._base_connection._http = http @@ -414,7 +421,7 @@ def test_as_context_mgr_w_error(self): http = _HTTP() connection = _Connection(http=http) project = 'PROJECT' - credentials = object() + credentials = _make_credentials() client = Client(project=project, credentials=credentials) client._base_connection = connection diff --git a/storage/unit_tests/test_client.py b/storage/unit_tests/test_client.py index 8a6dcbfde0c6..0b2fdcae1cf5 100644 --- a/storage/unit_tests/test_client.py +++ b/storage/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) + class TestClient(unittest.TestCase): @@ -29,7 +36,7 @@ def test_ctor_connection_type(self): from google.cloud.storage._http import Connection PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) self.assertEqual(client.project, PROJECT) @@ -42,7 +49,7 @@ def test__push_batch_and__pop_batch(self): from google.cloud.storage.batch import Batch PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) batch1 = Batch(client) @@ -61,7 +68,7 @@ def test__push_batch_and__pop_batch(self): def test__connection_setter(self): PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) client._base_connection = None # Unset the value from the constructor client._connection = connection = object() @@ -69,13 +76,13 @@ def test__connection_setter(self): def test__connection_setter_when_set(self): PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) self.assertRaises(ValueError, setattr, client, '_connection', None) def test__connection_getter_no_batch(self): PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) self.assertIs(client._connection, client._base_connection) self.assertIsNone(client.current_batch) @@ -83,7 +90,7 @@ def test__connection_getter_no_batch(self): def test__connection_getter_with_batch(self): from google.cloud.storage.batch import Batch PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) batch = Batch(client) client._push_batch(batch) @@ -95,7 +102,7 @@ def test_bucket(self): from google.cloud.storage.bucket import Bucket PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() BUCKET_NAME = 'BUCKET_NAME' client = self._make_one(project=PROJECT, credentials=CREDENTIALS) @@ -108,7 +115,7 @@ def test_batch(self): from google.cloud.storage.batch import Batch PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) batch = client.batch() @@ -119,7 +126,7 @@ def test_get_bucket_miss(self): from google.cloud.exceptions import NotFound PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) NONESUCH = 'nonesuch' @@ -142,7 +149,7 @@ def test_get_bucket_hit(self): from google.cloud.storage.bucket import Bucket PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) BLOB_NAME = 'blob-name' @@ -166,7 +173,7 @@ def test_get_bucket_hit(self): def test_lookup_bucket_miss(self): PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) NONESUCH = 'nonesuch' @@ -190,7 +197,7 @@ def test_lookup_bucket_hit(self): from google.cloud.storage.bucket import Bucket PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) BLOB_NAME = 'blob-name' @@ -216,7 +223,7 @@ def test_create_bucket_conflict(self): from google.cloud.exceptions import Conflict PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) BLOB_NAME = 'blob-name' @@ -239,7 +246,7 @@ def test_create_bucket_success(self): from google.cloud.storage.bucket import Bucket PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) BLOB_NAME = 'blob-name' @@ -265,7 +272,7 @@ def test_list_buckets_empty(self): from six.moves.urllib.parse import urlparse PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) EXPECTED_QUERY = { @@ -297,7 +304,7 @@ def test_list_buckets_non_empty(self): from six.moves.urllib.parse import urlencode from six.moves.urllib.parse import urlparse PROJECT = 'PROJECT' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) BUCKET_NAME = 'bucket-name' @@ -326,7 +333,7 @@ def test_list_buckets_all_arguments(self): from six.moves.urllib.parse import urlparse PROJECT = 'foo-bar' - CREDENTIALS = object() + CREDENTIALS = _make_credentials() client = self._make_one(project=PROJECT, credentials=CREDENTIALS) MAX_RESULTS = 10 @@ -374,7 +381,7 @@ def test_page_empty_response(self): from google.cloud.iterator import Page project = 'PROJECT' - credentials = object() + credentials = _make_credentials() client = self._make_one(project=project, credentials=credentials) iterator = client.list_buckets() page = Page(iterator, (), None) @@ -386,7 +393,7 @@ def test_page_non_empty_response(self): from google.cloud.storage.bucket import Bucket project = 'PROJECT' - credentials = object() + credentials = _make_credentials() client = self._make_one(project=project, credentials=credentials) blob_name = 'blob-name' diff --git a/vision/unit_tests/test_client.py b/vision/unit_tests/test_client.py index 81baa18c1d5c..af6e23a6b01a 100644 --- a/vision/unit_tests/test_client.py +++ b/vision/unit_tests/test_client.py @@ -15,14 +15,18 @@ import base64 import unittest -from google.cloud._helpers import _to_bytes -from google.cloud._helpers import _bytes_to_unicode +import mock -IMAGE_CONTENT = _to_bytes('/9j/4QNURXhpZgAASUkq') +IMAGE_CONTENT = b'/9j/4QNURXhpZgAASUkq' IMAGE_SOURCE = 'gs://some/image.jpg' PROJECT = 'PROJECT' -B64_IMAGE_CONTENT = _bytes_to_unicode(base64.b64encode(IMAGE_CONTENT)) +B64_IMAGE_CONTENT = base64.b64encode(IMAGE_CONTENT).decode('ascii') + + +def _make_credentials(): + import google.auth.credentials + return mock.Mock(spec=google.auth.credentials.Credentials) class TestClient(unittest.TestCase): @@ -35,7 +39,7 @@ def _make_one(self, *args, **kw): return self._get_target_class()(*args, **kw) def test_ctor(self): - creds = object() + creds = _make_credentials() client = self._make_one(project=PROJECT, credentials=creds) self.assertEqual(client.project, PROJECT) @@ -59,7 +63,7 @@ def test_face_annotation(self): } ] } - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -75,7 +79,7 @@ def test_face_annotation(self): def test_image_with_client_gcs_source(self): from google.cloud.vision.image import Image - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) gcs_image = client.image(source_uri=IMAGE_SOURCE) @@ -85,7 +89,7 @@ def test_image_with_client_gcs_source(self): def test_image_with_client_raw_content(self): from google.cloud.vision.image import Image - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) raw_image = client.image(content=IMAGE_CONTENT) @@ -97,7 +101,7 @@ def test_image_with_client_filename(self): from mock import patch from google.cloud.vision.image import Image - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) with patch('google.cloud.vision.image.open', @@ -118,7 +122,7 @@ def test_multiple_detection_from_content(self): logos = copy.deepcopy(LOGO_DETECTION_RESPONSE['responses'][0]) returned['responses'][0]['logoAnnotations'] = logos['logoAnnotations'] - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(returned) @@ -165,7 +169,7 @@ def test_face_detection_from_source(self): from google.cloud.vision.face import Face from unit_tests._fixtures import FACE_DETECTION_RESPONSE RETURNED = FACE_DETECTION_RESPONSE - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -182,7 +186,7 @@ def test_face_detection_from_content(self): from google.cloud.vision.face import Face from unit_tests._fixtures import FACE_DETECTION_RESPONSE RETURNED = FACE_DETECTION_RESPONSE - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -200,7 +204,7 @@ def test_face_detection_from_content_no_results(self): RETURNED = { 'responses': [{}] } - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -219,7 +223,7 @@ def test_label_detection_from_source(self): from unit_tests._fixtures import ( LABEL_DETECTION_RESPONSE as RETURNED) - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -240,7 +244,7 @@ def test_label_detection_no_results(self): RETURNED = { 'responses': [{}] } - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -254,7 +258,7 @@ def test_landmark_detection_from_source(self): from unit_tests._fixtures import ( LANDMARK_DETECTION_RESPONSE as RETURNED) - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -276,7 +280,7 @@ def test_landmark_detection_from_content(self): from unit_tests._fixtures import ( LANDMARK_DETECTION_RESPONSE as RETURNED) - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -293,7 +297,7 @@ def test_landmark_detection_no_results(self): RETURNED = { 'responses': [{}] } - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -306,7 +310,7 @@ def test_logo_detection_from_source(self): from google.cloud.vision.entity import EntityAnnotation from unit_tests._fixtures import LOGO_DETECTION_RESPONSE RETURNED = LOGO_DETECTION_RESPONSE - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -323,7 +327,7 @@ def test_logo_detection_from_content(self): from google.cloud.vision.entity import EntityAnnotation from unit_tests._fixtures import LOGO_DETECTION_RESPONSE RETURNED = LOGO_DETECTION_RESPONSE - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -341,7 +345,7 @@ def test_text_detection_from_source(self): from unit_tests._fixtures import ( TEXT_DETECTION_RESPONSE as RETURNED) - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -364,7 +368,7 @@ def test_safe_search_detection_from_source(self): from unit_tests._fixtures import SAFE_SEARCH_DETECTION_RESPONSE RETURNED = SAFE_SEARCH_DETECTION_RESPONSE - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -383,7 +387,7 @@ def test_safe_search_no_results(self): RETURNED = { 'responses': [{}] } - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -397,7 +401,7 @@ def test_image_properties_detection_from_source(self): from unit_tests._fixtures import IMAGE_PROPERTIES_RESPONSE RETURNED = IMAGE_PROPERTIES_RESPONSE - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED) @@ -419,7 +423,7 @@ def test_image_properties_no_results(self): RETURNED = { 'responses': [{}] } - credentials = object() + credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials) client._connection = _Connection(RETURNED)