Skip to content

Commit f040a4e

Browse files
committed
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`.
1 parent 70088aa commit f040a4e

File tree

12 files changed

+99
-145
lines changed

12 files changed

+99
-145
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 & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,6 @@
2727
from google.cloud._helpers import UTC
2828

2929

30-
def get_credentials():
31-
"""Gets credentials implicitly from the current environment.
32-
33-
Uses :func:`google.auth.default()`.
34-
35-
:rtype: :class:`google.auth.credentials.Credentials`,
36-
:returns: A new credentials instance corresponding to the implicit
37-
environment.
38-
"""
39-
credentials, _ = google.auth.default()
40-
return credentials
41-
42-
4330
def _get_signed_query_params(credentials, expiration, string_to_sign):
4431
"""Gets query parameters for creating a signed URL.
4532

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: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,16 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import base64
16+
import calendar
17+
import datetime
18+
import time
1519
import unittest
1620

1721
import mock
1822
import six
1923

2024

21-
class Test_get_credentials(unittest.TestCase):
22-
23-
def _call_fut(self):
24-
from google.cloud import credentials
25-
26-
return credentials.get_credentials()
27-
28-
def test_it(self):
29-
with mock.patch('google.auth.default', autospec=True) as default:
30-
default.return_value = (
31-
mock.sentinel.credentials, mock.sentinel.project)
32-
found = self._call_fut()
33-
34-
self.assertIs(found, mock.sentinel.credentials)
35-
default.assert_called_once_with()
36-
37-
3825
class Test_generate_signed_url(unittest.TestCase):
3926

4027
def _call_fut(self, *args, **kwargs):
@@ -44,7 +31,6 @@ def _call_fut(self, *args, **kwargs):
4431

4532
def _generate_helper(self, response_type=None, response_disposition=None,
4633
generation=None):
47-
import base64
4834
from six.moves.urllib.parse import parse_qs
4935
from six.moves.urllib.parse import urlsplit
5036
import google.auth.credentials
@@ -108,7 +94,6 @@ def test_w_custom_fields(self):
10894

10995
class Test_generate_signed_url_exception(unittest.TestCase):
11096
def test_with_google_credentials(self):
111-
import time
11297
import google.auth.credentials
11398
from google.cloud.credentials import generate_signed_url
11499

@@ -129,7 +114,6 @@ def _call_fut(self, credentials, expiration, string_to_sign):
129114
string_to_sign)
130115

131116
def test_it(self):
132-
import base64
133117
import google.auth.credentials
134118

135119
SIG_BYTES = b'DEADBEEF'
@@ -158,8 +142,6 @@ def _call_fut(self, expiration):
158142
return _get_expiration_seconds(expiration)
159143

160144
def _utc_seconds(self, when):
161-
import calendar
162-
163145
return int(calendar.timegm(when.timetuple()))
164146

165147
def test_w_invalid(self):
@@ -176,22 +158,18 @@ def test_w_long(self):
176158
self.assertEqual(self._call_fut(long(123)), 123) # noqa: F821
177159

178160
def test_w_naive_datetime(self):
179-
import datetime
180-
181161
expiration_no_tz = datetime.datetime(2004, 8, 19, 0, 0, 0, 0)
182162
utc_seconds = self._utc_seconds(expiration_no_tz)
183163
self.assertEqual(self._call_fut(expiration_no_tz), utc_seconds)
184164

185165
def test_w_utc_datetime(self):
186-
import datetime
187166
from google.cloud._helpers import UTC
188167

189168
expiration_utc = datetime.datetime(2004, 8, 19, 0, 0, 0, 0, UTC)
190169
utc_seconds = self._utc_seconds(expiration_utc)
191170
self.assertEqual(self._call_fut(expiration_utc), utc_seconds)
192171

193172
def test_w_other_zone_datetime(self):
194-
import datetime
195173
from google.cloud._helpers import _UTC
196174

197175
class CET(_UTC):
@@ -205,7 +183,6 @@ class CET(_UTC):
205183
self.assertEqual(self._call_fut(expiration_other), cet_seconds)
206184

207185
def test_w_timedelta_seconds(self):
208-
import datetime
209186
from google.cloud._testing import _Monkey
210187
from google.cloud import credentials as MUT
211188

@@ -219,7 +196,6 @@ def test_w_timedelta_seconds(self):
219196
self.assertEqual(result, utc_seconds + 10)
220197

221198
def test_w_timedelta_days(self):
222-
import datetime
223199
from google.cloud._testing import _Monkey
224200
from google.cloud import credentials as MUT
225201

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

0 commit comments

Comments
 (0)