Skip to content

Commit

Permalink
Removing get_credentials() from core.
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
dhermes committed Jul 25, 2017
1 parent 70088aa commit f040a4e
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 145 deletions.
4 changes: 2 additions & 2 deletions bigtable/google/cloud/bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__
Expand Down Expand Up @@ -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'
Expand Down
13 changes: 6 additions & 7 deletions bigtable/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions core/google/cloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions core/google/cloud/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 17 additions & 6 deletions core/nox.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from __future__ import absolute_import
import os

import nox

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


Expand Down
83 changes: 35 additions & 48 deletions core/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

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

Expand Down
32 changes: 4 additions & 28 deletions core/tests/unit/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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'
Expand Down Expand Up @@ -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):
Expand All @@ -176,22 +158,18 @@ 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)
utc_seconds = self._utc_seconds(expiration_utc)
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):
Expand All @@ -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

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

Expand Down
15 changes: 11 additions & 4 deletions datastore/nox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down
Loading

0 comments on commit f040a4e

Please sign in to comment.