Skip to content

Commit b5a9b90

Browse files
dhermeslandrito
authored andcommitted
Removing get_credentials() from core. (googleapis#3667)
* Removing `get_credentials()` from `core`. In the process also: - Slight re-org on `nox.py` config (to pass posargs) for `core` and `datastore` - Getting rid of last usage of `_Monkey` in datastore This is part of `@jonparrott`'s effort to slim down / stabilize `core`. * Removing `google.cloud.credentials` module from docs.
1 parent 0d2ea02 commit b5a9b90

File tree

13 files changed

+95
-175
lines changed

13 files changed

+95
-175
lines changed

bigtable/google/cloud/bigtable/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import os
3333

34+
import google.auth
3435
import google.auth.credentials
3536
from google.gax.utils import metrics
3637
from google.longrunning import operations_grpc
@@ -40,7 +41,6 @@
4041
from google.cloud._http import DEFAULT_USER_AGENT
4142
from google.cloud.client import _ClientFactoryMixin
4243
from google.cloud.client import _ClientProjectMixin
43-
from google.cloud.credentials import get_credentials
4444
from google.cloud.environment_vars import BIGTABLE_EMULATOR
4545

4646
from google.cloud.bigtable import __version__
@@ -211,7 +211,7 @@ def __init__(self, project=None, credentials=None,
211211
read_only=False, admin=False, user_agent=DEFAULT_USER_AGENT):
212212
_ClientProjectMixin.__init__(self, project=project)
213213
if credentials is None:
214-
credentials = get_credentials()
214+
credentials, _ = google.auth.default()
215215

216216
if read_only and admin:
217217
raise ValueError('A read-only client cannot also perform'

bigtable/tests/unit/test_client.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,20 +360,19 @@ def test_constructor_both_admin_and_read_only(self):
360360
read_only=True)
361361

362362
def test_constructor_implicit_credentials(self):
363-
from google.cloud._testing import _Monkey
364-
from google.cloud.bigtable import client as MUT
363+
from google.cloud.bigtable.client import DATA_SCOPE
365364

366365
creds = _make_credentials()
367-
expected_scopes = [MUT.DATA_SCOPE]
368-
369-
def mock_get_credentials():
370-
return creds
366+
expected_scopes = [DATA_SCOPE]
371367

372-
with _Monkey(MUT, get_credentials=mock_get_credentials):
368+
patch = mock.patch(
369+
'google.auth.default', return_value=(creds, None))
370+
with patch as default:
373371
self._constructor_test_helper(
374372
None, None,
375373
expected_creds=creds.with_scopes.return_value)
376374

375+
default.assert_called_once_with()
377376
creds.with_scopes.assert_called_once_with(expected_scopes)
378377

379378
def test_constructor_credentials_wo_create_scoped(self):

core/google/cloud/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
import google_auth_httplib2
2222
import six
2323

24+
import google.auth
2425
import google.auth.credentials
2526
from google.cloud._helpers import _determine_default_project
26-
from google.cloud.credentials import get_credentials
2727
from google.oauth2 import service_account
2828

2929

@@ -135,7 +135,7 @@ def __init__(self, credentials=None, _http=None):
135135
credentials, google.auth.credentials.Credentials)):
136136
raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
137137
if credentials is None and _http is None:
138-
credentials = get_credentials()
138+
credentials, _ = google.auth.default()
139139
self._credentials = google.auth.credentials.with_scopes_if_required(
140140
credentials, self.SCOPE)
141141
self._http_internal = _http

core/google/cloud/credentials.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

core/nox.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
from __future__ import absolute_import
16+
import os
1617

1718
import nox
1819

@@ -29,16 +30,26 @@ def unit_tests(session, python_version):
2930
session.virtualenv_dirname = 'unit-' + python_version
3031

3132
# Install all test dependencies, then install this package in-place.
32-
session.install('mock', 'pytest', 'pytest-cov',
33-
'grpcio >= 1.0.2')
33+
session.install(
34+
'mock',
35+
'pytest',
36+
'pytest-cov',
37+
'grpcio >= 1.0.2',
38+
)
3439
session.install('-e', '.')
3540

3641
# Run py.test against the unit tests.
3742
session.run(
38-
'py.test', '--quiet',
39-
'--cov=google.cloud', '--cov=tests.unit', '--cov-append',
40-
'--cov-config=.coveragerc', '--cov-report=', '--cov-fail-under=97',
41-
'tests/unit',
43+
'py.test',
44+
'--quiet',
45+
'--cov=google.cloud',
46+
'--cov=tests.unit',
47+
'--cov-append',
48+
'--cov-config=.coveragerc',
49+
'--cov-report=',
50+
'--cov-fail-under=97',
51+
os.path.join('tests', 'unit'),
52+
*session.posargs
4253
)
4354

4455

core/tests/unit/test_client.py

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,31 @@ def test_unpickleable(self):
5959
with self.assertRaises(pickle.PicklingError):
6060
pickle.dumps(client_obj)
6161

62-
def test_ctor_defaults(self):
63-
from google.cloud._testing import _Monkey
64-
from google.cloud import client
65-
66-
CREDENTIALS = _make_credentials()
67-
FUNC_CALLS = []
68-
69-
def mock_get_credentials():
70-
FUNC_CALLS.append('get_credentials')
71-
return CREDENTIALS
62+
def test_constructor_defaults(self):
63+
credentials = _make_credentials()
7264

73-
with _Monkey(client, get_credentials=mock_get_credentials):
65+
patch = mock.patch(
66+
'google.auth.default', return_value=(credentials, None))
67+
with patch as default:
7468
client_obj = self._make_one()
7569

76-
self.assertIs(client_obj._credentials, CREDENTIALS)
70+
self.assertIs(client_obj._credentials, credentials)
7771
self.assertIsNone(client_obj._http_internal)
78-
self.assertEqual(FUNC_CALLS, ['get_credentials'])
72+
default.assert_called_once_with()
7973

80-
def test_ctor_explicit(self):
81-
CREDENTIALS = _make_credentials()
82-
HTTP = object()
83-
client_obj = self._make_one(credentials=CREDENTIALS, _http=HTTP)
74+
def test_constructor_explicit(self):
75+
credentials = _make_credentials()
76+
http = mock.sentinel.http
77+
client_obj = self._make_one(credentials=credentials, _http=http)
8478

85-
self.assertIs(client_obj._credentials, CREDENTIALS)
86-
self.assertIs(client_obj._http_internal, HTTP)
79+
self.assertIs(client_obj._credentials, credentials)
80+
self.assertIs(client_obj._http_internal, http)
8781

88-
def test_ctor_bad_credentials(self):
89-
CREDENTIALS = object()
82+
def test_constructor_bad_credentials(self):
83+
credentials = mock.sentinel.credentials
9084

9185
with self.assertRaises(ValueError):
92-
self._make_one(credentials=CREDENTIALS)
86+
self._make_one(credentials=credentials)
9387

9488
def test_from_service_account_json(self):
9589
from google.cloud import _helpers
@@ -162,34 +156,27 @@ def _get_target_class():
162156
def _make_one(self, *args, **kw):
163157
return self._get_target_class()(*args, **kw)
164158

165-
def test_ctor_defaults(self):
166-
from google.cloud._testing import _Monkey
167-
from google.cloud import client
168-
169-
PROJECT = 'PROJECT'
170-
CREDENTIALS = _make_credentials()
171-
FUNC_CALLS = []
172-
173-
def mock_determine_proj(project):
174-
FUNC_CALLS.append((project, '_determine_default_project'))
175-
return PROJECT
159+
def test_constructor_defaults(self):
160+
credentials = _make_credentials()
161+
patch1 = mock.patch(
162+
'google.auth.default', return_value=(credentials, None))
176163

177-
def mock_get_credentials():
178-
FUNC_CALLS.append('get_credentials')
179-
return CREDENTIALS
164+
project = 'prahj-ekt'
165+
patch2 = mock.patch(
166+
'google.cloud.client._determine_default_project',
167+
return_value=project)
180168

181-
with _Monkey(client, get_credentials=mock_get_credentials,
182-
_determine_default_project=mock_determine_proj):
183-
client_obj = self._make_one()
169+
with patch1 as default:
170+
with patch2 as _determine_default_project:
171+
client_obj = self._make_one()
184172

185-
self.assertEqual(client_obj.project, PROJECT)
186-
self.assertIs(client_obj._credentials, CREDENTIALS)
173+
self.assertEqual(client_obj.project, project)
174+
self.assertIs(client_obj._credentials, credentials)
187175
self.assertIsNone(client_obj._http_internal)
188-
self.assertEqual(
189-
FUNC_CALLS,
190-
[(None, '_determine_default_project'), 'get_credentials'])
176+
default.assert_called_once_with()
177+
_determine_default_project.assert_called_once_with(None)
191178

192-
def test_ctor_missing_project(self):
179+
def test_constructor_missing_project(self):
193180
from google.cloud._testing import _Monkey
194181
from google.cloud import client
195182

@@ -204,7 +191,7 @@ def mock_determine_proj(project):
204191

205192
self.assertEqual(FUNC_CALLS, [(None, '_determine_default_project')])
206193

207-
def test_ctor_w_invalid_project(self):
194+
def test_constructor_w_invalid_project(self):
208195
CREDENTIALS = _make_credentials()
209196
HTTP = object()
210197
with self.assertRaises(ValueError):
@@ -227,11 +214,11 @@ def _explicit_ctor_helper(self, project):
227214
self.assertIs(client_obj._credentials, CREDENTIALS)
228215
self.assertIs(client_obj._http_internal, HTTP)
229216

230-
def test_ctor_explicit_bytes(self):
217+
def test_constructor_explicit_bytes(self):
231218
PROJECT = b'PROJECT'
232219
self._explicit_ctor_helper(PROJECT)
233220

234-
def test_ctor_explicit_unicode(self):
221+
def test_constructor_explicit_unicode(self):
235222
PROJECT = u'PROJECT'
236223
self._explicit_ctor_helper(PROJECT)
237224

core/tests/unit/test_credentials.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

datastore/nox.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ def unit_tests(session, python_version):
3838
session.install('-e', '.')
3939

4040
# Run py.test against the unit tests.
41-
session.run('py.test', '--quiet',
42-
'--cov=google.cloud.datastore', '--cov=tests.unit', '--cov-append',
43-
'--cov-config=.coveragerc', '--cov-report=', '--cov-fail-under=97',
44-
'tests/unit',
41+
session.run(
42+
'py.test',
43+
'--quiet',
44+
'--cov=google.cloud.datastore',
45+
'--cov=tests.unit',
46+
'--cov-append',
47+
'--cov-config=.coveragerc',
48+
'--cov-report=',
49+
'--cov-fail-under=97',
50+
os.path.join('tests', 'unit'),
51+
*session.posargs
4552
)
4653

4754

datastore/tests/unit/test_client.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,16 @@ def test_constructor_w_implicit_inputs(self):
148148

149149
other = 'other'
150150
creds = _make_credentials()
151-
default_called = []
152-
153-
def fallback_mock(project):
154-
default_called.append(project)
155-
return project or other
156151

157152
klass = self._get_target_class()
158153
patch1 = mock.patch(
159154
'google.cloud.datastore.client._determine_default_project',
160-
new=fallback_mock)
155+
return_value=other)
161156
patch2 = mock.patch(
162-
'google.cloud.client.get_credentials',
163-
return_value=creds)
157+
'google.auth.default', return_value=(creds, None))
164158

165-
with patch1:
166-
with patch2:
159+
with patch1 as _determine_default_project:
160+
with patch2 as default:
167161
client = klass()
168162

169163
self.assertEqual(client.project, other)
@@ -174,7 +168,9 @@ def fallback_mock(project):
174168

175169
self.assertIsNone(client.current_batch)
176170
self.assertIsNone(client.current_transaction)
177-
self.assertEqual(default_called, [None])
171+
172+
default.assert_called_once_with()
173+
_determine_default_project.assert_called_once_with(None)
178174

179175
def test_constructor_w_explicit_inputs(self):
180176
from google.cloud.datastore.client import _DATASTORE_BASE_URL

datastore/tests/unit/test_query.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -550,21 +550,14 @@ def _call_fut(self, iterator, entity_pb):
550550
return _item_to_entity(iterator, entity_pb)
551551

552552
def test_it(self):
553-
from google.cloud._testing import _Monkey
554-
from google.cloud.datastore import helpers
555-
556-
result = object()
557-
entities = []
558-
559-
def mocked(entity_pb):
560-
entities.append(entity_pb)
561-
return result
562-
563-
entity_pb = object()
564-
with _Monkey(helpers, entity_from_protobuf=mocked):
565-
self.assertIs(result, self._call_fut(None, entity_pb))
566-
567-
self.assertEqual(entities, [entity_pb])
553+
entity_pb = mock.sentinel.entity_pb
554+
patch = mock.patch(
555+
'google.cloud.datastore.helpers.entity_from_protobuf')
556+
with patch as entity_from_protobuf:
557+
result = self._call_fut(None, entity_pb)
558+
self.assertIs(result, entity_from_protobuf.return_value)
559+
560+
entity_from_protobuf.assert_called_once_with(entity_pb)
568561

569562

570563
class Test__pb_from_query(unittest.TestCase):

0 commit comments

Comments
 (0)