diff --git a/bigtable/google/cloud/bigtable/client.py b/bigtable/google/cloud/bigtable/client.py index 86ee7173c917b..62877371a945a 100644 --- a/bigtable/google/cloud/bigtable/client.py +++ b/bigtable/google/cloud/bigtable/client.py @@ -31,6 +31,7 @@ import os +import google.auth import google.auth.credentials from google.gax.utils import metrics from google.longrunning import operations_grpc @@ -40,7 +41,6 @@ from google.cloud._http import DEFAULT_USER_AGENT from google.cloud.client import _ClientFactoryMixin from google.cloud.client import _ClientProjectMixin -from google.cloud.credentials import get_credentials from google.cloud.environment_vars import BIGTABLE_EMULATOR from google.cloud.bigtable import __version__ @@ -211,7 +211,7 @@ def __init__(self, project=None, credentials=None, read_only=False, admin=False, user_agent=DEFAULT_USER_AGENT): _ClientProjectMixin.__init__(self, project=project) if credentials is None: - credentials = get_credentials() + credentials, _ = google.auth.default() if read_only and admin: raise ValueError('A read-only client cannot also perform' diff --git a/bigtable/tests/unit/test_client.py b/bigtable/tests/unit/test_client.py index 17656be60c00d..c3ab8d1ed8887 100644 --- a/bigtable/tests/unit/test_client.py +++ b/bigtable/tests/unit/test_client.py @@ -360,20 +360,19 @@ def test_constructor_both_admin_and_read_only(self): read_only=True) def test_constructor_implicit_credentials(self): - from google.cloud._testing import _Monkey - from google.cloud.bigtable import client as MUT + from google.cloud.bigtable.client import DATA_SCOPE creds = _make_credentials() - expected_scopes = [MUT.DATA_SCOPE] - - def mock_get_credentials(): - return creds + expected_scopes = [DATA_SCOPE] - with _Monkey(MUT, get_credentials=mock_get_credentials): + patch = mock.patch( + 'google.auth.default', return_value=(creds, None)) + with patch as default: self._constructor_test_helper( None, None, expected_creds=creds.with_scopes.return_value) + default.assert_called_once_with() creds.with_scopes.assert_called_once_with(expected_scopes) def test_constructor_credentials_wo_create_scoped(self): diff --git a/core/google/cloud/client.py b/core/google/cloud/client.py index 5906ab5ed1086..468cf9e40a526 100644 --- a/core/google/cloud/client.py +++ b/core/google/cloud/client.py @@ -21,9 +21,9 @@ import google_auth_httplib2 import six +import google.auth import google.auth.credentials from google.cloud._helpers import _determine_default_project -from google.cloud.credentials import get_credentials from google.oauth2 import service_account @@ -135,7 +135,7 @@ def __init__(self, credentials=None, _http=None): credentials, google.auth.credentials.Credentials)): raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP) if credentials is None and _http is None: - credentials = get_credentials() + credentials, _ = google.auth.default() self._credentials = google.auth.credentials.with_scopes_if_required( credentials, self.SCOPE) self._http_internal = _http diff --git a/core/google/cloud/credentials.py b/core/google/cloud/credentials.py index 29c4a5d310f4f..f33d61144cb05 100644 --- a/core/google/cloud/credentials.py +++ b/core/google/cloud/credentials.py @@ -27,19 +27,6 @@ from google.cloud._helpers import UTC -def get_credentials(): - """Gets credentials implicitly from the current environment. - - Uses :func:`google.auth.default()`. - - :rtype: :class:`google.auth.credentials.Credentials`, - :returns: A new credentials instance corresponding to the implicit - environment. - """ - credentials, _ = google.auth.default() - return credentials - - def _get_signed_query_params(credentials, expiration, string_to_sign): """Gets query parameters for creating a signed URL. diff --git a/core/nox.py b/core/nox.py index 48b55332283ea..1dca10eb9b694 100644 --- a/core/nox.py +++ b/core/nox.py @@ -13,6 +13,7 @@ # limitations under the License. from __future__ import absolute_import +import os import nox @@ -29,16 +30,26 @@ def unit_tests(session, python_version): session.virtualenv_dirname = 'unit-' + python_version # Install all test dependencies, then install this package in-place. - session.install('mock', 'pytest', 'pytest-cov', - 'grpcio >= 1.0.2') + session.install( + 'mock', + 'pytest', + 'pytest-cov', + 'grpcio >= 1.0.2', + ) session.install('-e', '.') # Run py.test against the unit tests. session.run( - 'py.test', '--quiet', - '--cov=google.cloud', '--cov=tests.unit', '--cov-append', - '--cov-config=.coveragerc', '--cov-report=', '--cov-fail-under=97', - 'tests/unit', + 'py.test', + '--quiet', + '--cov=google.cloud', + '--cov=tests.unit', + '--cov-append', + '--cov-config=.coveragerc', + '--cov-report=', + '--cov-fail-under=97', + os.path.join('tests', 'unit'), + *session.posargs ) diff --git a/core/tests/unit/test_client.py b/core/tests/unit/test_client.py index 14eac68abee32..25667712c69a9 100644 --- a/core/tests/unit/test_client.py +++ b/core/tests/unit/test_client.py @@ -59,37 +59,31 @@ def test_unpickleable(self): with self.assertRaises(pickle.PicklingError): pickle.dumps(client_obj) - def test_ctor_defaults(self): - from google.cloud._testing import _Monkey - from google.cloud import client - - CREDENTIALS = _make_credentials() - FUNC_CALLS = [] - - def mock_get_credentials(): - FUNC_CALLS.append('get_credentials') - return CREDENTIALS + def test_constructor_defaults(self): + credentials = _make_credentials() - with _Monkey(client, get_credentials=mock_get_credentials): + patch = mock.patch( + 'google.auth.default', return_value=(credentials, None)) + with patch as default: client_obj = self._make_one() - self.assertIs(client_obj._credentials, CREDENTIALS) + self.assertIs(client_obj._credentials, credentials) self.assertIsNone(client_obj._http_internal) - self.assertEqual(FUNC_CALLS, ['get_credentials']) + default.assert_called_once_with() - def test_ctor_explicit(self): - CREDENTIALS = _make_credentials() - HTTP = object() - client_obj = self._make_one(credentials=CREDENTIALS, _http=HTTP) + def test_constructor_explicit(self): + credentials = _make_credentials() + http = mock.sentinel.http + client_obj = self._make_one(credentials=credentials, _http=http) - self.assertIs(client_obj._credentials, CREDENTIALS) - self.assertIs(client_obj._http_internal, HTTP) + self.assertIs(client_obj._credentials, credentials) + self.assertIs(client_obj._http_internal, http) - def test_ctor_bad_credentials(self): - CREDENTIALS = object() + def test_constructor_bad_credentials(self): + credentials = mock.sentinel.credentials with self.assertRaises(ValueError): - self._make_one(credentials=CREDENTIALS) + self._make_one(credentials=credentials) def test_from_service_account_json(self): from google.cloud import _helpers @@ -162,34 +156,27 @@ def _get_target_class(): def _make_one(self, *args, **kw): return self._get_target_class()(*args, **kw) - def test_ctor_defaults(self): - from google.cloud._testing import _Monkey - from google.cloud import client - - PROJECT = 'PROJECT' - CREDENTIALS = _make_credentials() - FUNC_CALLS = [] - - def mock_determine_proj(project): - FUNC_CALLS.append((project, '_determine_default_project')) - return PROJECT + def test_constructor_defaults(self): + credentials = _make_credentials() + patch1 = mock.patch( + 'google.auth.default', return_value=(credentials, None)) - def mock_get_credentials(): - FUNC_CALLS.append('get_credentials') - return CREDENTIALS + project = 'prahj-ekt' + patch2 = mock.patch( + 'google.cloud.client._determine_default_project', + return_value=project) - with _Monkey(client, get_credentials=mock_get_credentials, - _determine_default_project=mock_determine_proj): - client_obj = self._make_one() + with patch1 as default: + with patch2 as _determine_default_project: + client_obj = self._make_one() - self.assertEqual(client_obj.project, PROJECT) - self.assertIs(client_obj._credentials, CREDENTIALS) + self.assertEqual(client_obj.project, project) + self.assertIs(client_obj._credentials, credentials) self.assertIsNone(client_obj._http_internal) - self.assertEqual( - FUNC_CALLS, - [(None, '_determine_default_project'), 'get_credentials']) + default.assert_called_once_with() + _determine_default_project.assert_called_once_with(None) - def test_ctor_missing_project(self): + def test_constructor_missing_project(self): from google.cloud._testing import _Monkey from google.cloud import client @@ -204,7 +191,7 @@ def mock_determine_proj(project): self.assertEqual(FUNC_CALLS, [(None, '_determine_default_project')]) - def test_ctor_w_invalid_project(self): + def test_constructor_w_invalid_project(self): CREDENTIALS = _make_credentials() HTTP = object() with self.assertRaises(ValueError): @@ -227,11 +214,11 @@ def _explicit_ctor_helper(self, project): self.assertIs(client_obj._credentials, CREDENTIALS) self.assertIs(client_obj._http_internal, HTTP) - def test_ctor_explicit_bytes(self): + def test_constructor_explicit_bytes(self): PROJECT = b'PROJECT' self._explicit_ctor_helper(PROJECT) - def test_ctor_explicit_unicode(self): + def test_constructor_explicit_unicode(self): PROJECT = u'PROJECT' self._explicit_ctor_helper(PROJECT) diff --git a/core/tests/unit/test_credentials.py b/core/tests/unit/test_credentials.py index aaffa907dda1c..bcc969c9db5de 100644 --- a/core/tests/unit/test_credentials.py +++ b/core/tests/unit/test_credentials.py @@ -12,29 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. +import base64 +import calendar +import datetime +import time import unittest import mock import six -class Test_get_credentials(unittest.TestCase): - - def _call_fut(self): - from google.cloud import credentials - - return credentials.get_credentials() - - def test_it(self): - with mock.patch('google.auth.default', autospec=True) as default: - default.return_value = ( - mock.sentinel.credentials, mock.sentinel.project) - found = self._call_fut() - - self.assertIs(found, mock.sentinel.credentials) - default.assert_called_once_with() - - class Test_generate_signed_url(unittest.TestCase): def _call_fut(self, *args, **kwargs): @@ -44,7 +31,6 @@ def _call_fut(self, *args, **kwargs): def _generate_helper(self, response_type=None, response_disposition=None, generation=None): - import base64 from six.moves.urllib.parse import parse_qs from six.moves.urllib.parse import urlsplit import google.auth.credentials @@ -108,7 +94,6 @@ def test_w_custom_fields(self): class Test_generate_signed_url_exception(unittest.TestCase): def test_with_google_credentials(self): - import time import google.auth.credentials from google.cloud.credentials import generate_signed_url @@ -129,7 +114,6 @@ def _call_fut(self, credentials, expiration, string_to_sign): string_to_sign) def test_it(self): - import base64 import google.auth.credentials SIG_BYTES = b'DEADBEEF' @@ -158,8 +142,6 @@ def _call_fut(self, expiration): return _get_expiration_seconds(expiration) def _utc_seconds(self, when): - import calendar - return int(calendar.timegm(when.timetuple())) def test_w_invalid(self): @@ -176,14 +158,11 @@ def test_w_long(self): self.assertEqual(self._call_fut(long(123)), 123) # noqa: F821 def test_w_naive_datetime(self): - import datetime - expiration_no_tz = datetime.datetime(2004, 8, 19, 0, 0, 0, 0) utc_seconds = self._utc_seconds(expiration_no_tz) self.assertEqual(self._call_fut(expiration_no_tz), utc_seconds) def test_w_utc_datetime(self): - import datetime from google.cloud._helpers import UTC expiration_utc = datetime.datetime(2004, 8, 19, 0, 0, 0, 0, UTC) @@ -191,7 +170,6 @@ def test_w_utc_datetime(self): self.assertEqual(self._call_fut(expiration_utc), utc_seconds) def test_w_other_zone_datetime(self): - import datetime from google.cloud._helpers import _UTC class CET(_UTC): @@ -205,7 +183,6 @@ class CET(_UTC): self.assertEqual(self._call_fut(expiration_other), cet_seconds) def test_w_timedelta_seconds(self): - import datetime from google.cloud._testing import _Monkey from google.cloud import credentials as MUT @@ -219,7 +196,6 @@ def test_w_timedelta_seconds(self): self.assertEqual(result, utc_seconds + 10) def test_w_timedelta_days(self): - import datetime from google.cloud._testing import _Monkey from google.cloud import credentials as MUT diff --git a/datastore/nox.py b/datastore/nox.py index 2cf2186aa45a0..f93b02944631b 100644 --- a/datastore/nox.py +++ b/datastore/nox.py @@ -38,10 +38,17 @@ def unit_tests(session, python_version): session.install('-e', '.') # Run py.test against the unit tests. - session.run('py.test', '--quiet', - '--cov=google.cloud.datastore', '--cov=tests.unit', '--cov-append', - '--cov-config=.coveragerc', '--cov-report=', '--cov-fail-under=97', - 'tests/unit', + session.run( + 'py.test', + '--quiet', + '--cov=google.cloud.datastore', + '--cov=tests.unit', + '--cov-append', + '--cov-config=.coveragerc', + '--cov-report=', + '--cov-fail-under=97', + os.path.join('tests', 'unit'), + *session.posargs ) diff --git a/datastore/tests/unit/test_client.py b/datastore/tests/unit/test_client.py index 9824e06b73ad0..a03bbe8b710fc 100644 --- a/datastore/tests/unit/test_client.py +++ b/datastore/tests/unit/test_client.py @@ -148,22 +148,16 @@ def test_constructor_w_implicit_inputs(self): other = 'other' creds = _make_credentials() - default_called = [] - - def fallback_mock(project): - default_called.append(project) - return project or other klass = self._get_target_class() patch1 = mock.patch( 'google.cloud.datastore.client._determine_default_project', - new=fallback_mock) + return_value=other) patch2 = mock.patch( - 'google.cloud.client.get_credentials', - return_value=creds) + 'google.auth.default', return_value=(creds, None)) - with patch1: - with patch2: + with patch1 as _determine_default_project: + with patch2 as default: client = klass() self.assertEqual(client.project, other) @@ -174,7 +168,9 @@ def fallback_mock(project): self.assertIsNone(client.current_batch) self.assertIsNone(client.current_transaction) - self.assertEqual(default_called, [None]) + + default.assert_called_once_with() + _determine_default_project.assert_called_once_with(None) def test_constructor_w_explicit_inputs(self): from google.cloud.datastore.client import _DATASTORE_BASE_URL diff --git a/datastore/tests/unit/test_query.py b/datastore/tests/unit/test_query.py index b361ec25a42fa..26c1b6cc0831d 100644 --- a/datastore/tests/unit/test_query.py +++ b/datastore/tests/unit/test_query.py @@ -550,21 +550,14 @@ def _call_fut(self, iterator, entity_pb): return _item_to_entity(iterator, entity_pb) def test_it(self): - from google.cloud._testing import _Monkey - from google.cloud.datastore import helpers - - result = object() - entities = [] - - def mocked(entity_pb): - entities.append(entity_pb) - return result - - entity_pb = object() - with _Monkey(helpers, entity_from_protobuf=mocked): - self.assertIs(result, self._call_fut(None, entity_pb)) - - self.assertEqual(entities, [entity_pb]) + entity_pb = mock.sentinel.entity_pb + patch = mock.patch( + 'google.cloud.datastore.helpers.entity_from_protobuf') + with patch as entity_from_protobuf: + result = self._call_fut(None, entity_pb) + self.assertIs(result, entity_from_protobuf.return_value) + + entity_from_protobuf.assert_called_once_with(entity_pb) class Test__pb_from_query(unittest.TestCase): diff --git a/spanner/google/cloud/spanner/client.py b/spanner/google/cloud/spanner/client.py index 875238aed2bc2..b701b017abb03 100644 --- a/spanner/google/cloud/spanner/client.py +++ b/spanner/google/cloud/spanner/client.py @@ -24,6 +24,7 @@ :class:`~google.cloud.spanner.database.Database` """ +import google.auth import google.auth.credentials from google.gax import INITIAL_PAGE # pylint: disable=line-too-long @@ -36,7 +37,6 @@ from google.cloud._http import DEFAULT_USER_AGENT from google.cloud.client import _ClientFactoryMixin from google.cloud.client import _ClientProjectMixin -from google.cloud.credentials import get_credentials from google.cloud.iterator import GAXIterator from google.cloud.spanner import __version__ from google.cloud.spanner._helpers import _options_with_prefix @@ -109,7 +109,7 @@ def __init__(self, project=None, credentials=None, _ClientProjectMixin.__init__(self, project=project) if credentials is None: - credentials = get_credentials() + credentials, _ = google.auth.default() scopes = [ SPANNER_ADMIN_SCOPE, diff --git a/spanner/tests/unit/test_client.py b/spanner/tests/unit/test_client.py index c71429c225352..e5e90fd6b7ab1 100644 --- a/spanner/tests/unit/test_client.py +++ b/spanner/tests/unit/test_client.py @@ -88,19 +88,17 @@ def test_constructor_custom_user_agent_and_timeout(self): user_agent=CUSTOM_USER_AGENT) def test_constructor_implicit_credentials(self): - from google.cloud._testing import _Monkey - from google.cloud.spanner import client as MUT - creds = _make_credentials() - def mock_get_credentials(): - return creds - - with _Monkey(MUT, get_credentials=mock_get_credentials): + patch = mock.patch( + 'google.auth.default', return_value=(creds, None)) + with patch as default: self._constructor_test_helper( None, None, expected_creds=creds.with_scopes.return_value) + default.assert_called_once_with() + def test_constructor_credentials_wo_create_scoped(self): creds = _make_credentials() expected_scopes = None