Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow default_host and default_scopes to be passed to create_channel #134

Merged
merged 12 commits into from
Feb 5, 2021
Prev Previous commit
Next Next commit
test: update tests
  • Loading branch information
busunkim96 committed Feb 4, 2021
commit 724cdaa09289c4ac71937d10ea1b31e5fc68510d
9 changes: 3 additions & 6 deletions google/api_core/grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ def _create_composite_credentials(
scopes=scopes,
default_scopes=default_scopes
)
# google-auth < x.x.x does not have `default_scopes`
# TODO: remove this try/except once google-auth >= x.x.x is required
# TODO: remove this try/except once google-auth >= 1.25.0 is required
except TypeError:
credentials = google.auth.credentials.with_scopes_if_required(
credentials,
Expand All @@ -247,8 +246,7 @@ def _create_composite_credentials(
else:
try:
credentials, _ = google.auth.default(scopes=scopes, default_scopes=default_scopes)
# google-auth < x.x.x does not have `default_scopes`
# TODO: remove this try/except once google-auth >= x.x.x is required
# TODO: remove this try/except once google-auth >= 1.25.0 is required
except TypeError:
credentials, _ = google.auth.default(scopes=scopes or default_scopes)

Expand All @@ -259,8 +257,7 @@ def _create_composite_credentials(

# Create the metadata plugin for inserting the authorization header.

# google-auth < x.x.x does not have `default_host`
# TODO: remove this try/except once google-auth >= x.x.x is required
# TODO: remove this try/except once google-auth >= 1.25.0 is required
try:
metadata_plugin = google.auth.transport.grpc.AuthMetadataPlugin(
credentials, request, default_host=default_host,
Expand Down
22 changes: 19 additions & 3 deletions tests/asyncio/test_grpc_helpers_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import grpc
from grpc.experimental import aio
import mock
import pkg_resources
import pytest

from google.api_core import exceptions
Expand Down Expand Up @@ -401,7 +402,12 @@ def test_create_channel_explicit_scoped(grpc_secure_channel, composite_creds_cal
target, credentials=credentials, scopes=scopes
)

credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None)
# TODO: remove if/else once google-auth >= 1.25.0 is required
if pkg_resources.get_distribution("google-auth").version >= "1.25.0":
credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None)
else:
credentials.with_scopes.assert_called_once_with(scopes)

assert channel is grpc_secure_channel.return_value
grpc_secure_channel.assert_called_once_with(target, composite_creds)

Expand All @@ -421,7 +427,12 @@ def test_create_channel_explicit_default_scopes(grpc_secure_channel, composite_c
target, credentials=credentials, scopes=scopes, default_scopes=default_scopes
)

credentials.with_scopes.assert_called_once_with(scopes, default_scopes=default_scopes)
# TODO: remove if/else once google-auth >= 1.25.0 is required
if pkg_resources.get_distribution("google-auth").version >= "1.25.0":
credentials.with_scopes.assert_called_once_with(scopes, default_scopes=default_scopes)
else:
credentials.with_scopes.assert_called_once_with(scopes)

assert channel is grpc_secure_channel.return_value
grpc_secure_channel.assert_called_once_with(target, composite_creds)

Expand Down Expand Up @@ -520,7 +531,12 @@ def test_create_channel_without_grpc_gcp(grpc_secure_channel):

grpc_helpers_async.create_channel(target, credentials=credentials, scopes=scopes)
grpc_secure_channel.assert_called()
credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None)

# TODO: remove if/else once google-auth >= 1.25.0 is required
if pkg_resources.get_distribution("google-auth").version >= "1.25.0":
credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None)
else:
credentials.with_scopes.assert_called_once_with(scopes)


@pytest.mark.asyncio
Expand Down
22 changes: 19 additions & 3 deletions tests/unit/test_grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import grpc
import mock
import pkg_resources
import pytest

from google.api_core import exceptions
Expand Down Expand Up @@ -377,7 +378,12 @@ def test_create_channel_explicit_scoped(grpc_secure_channel, composite_creds_cal
target, credentials=credentials, scopes=scopes
)

credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None)
# TODO: remove if/else once google-auth >= 1.25.0 is required
if pkg_resources.get_distribution("google-auth").version >= "1.25.0":
credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None)
else:
credentials.with_scopes.assert_called_once_with(scopes)

assert channel is grpc_secure_channel.return_value
if grpc_helpers.HAS_GRPC_GCP:
grpc_secure_channel.assert_called_once_with(target, composite_creds, None)
Expand All @@ -400,7 +406,12 @@ def test_create_channel_explicit_default_scopes(grpc_secure_channel, composite_c
target, credentials=credentials, scopes=scopes, default_scopes=default_scopes
)

credentials.with_scopes.assert_called_once_with(scopes, default_scopes=default_scopes)
# TODO: remove if/else once google-auth >= 1.25.0 is required
if pkg_resources.get_distribution("google-auth").version >= "1.25.0":
credentials.with_scopes.assert_called_once_with(scopes, default_scopes=default_scopes)
else:
credentials.with_scopes.assert_called_once_with(scopes)

assert channel is grpc_secure_channel.return_value
if grpc_helpers.HAS_GRPC_GCP:
grpc_secure_channel.assert_called_once_with(target, composite_creds, None)
Expand Down Expand Up @@ -534,7 +545,12 @@ def test_create_channel_without_grpc_gcp(grpc_secure_channel):

grpc_helpers.create_channel(target, credentials=credentials, scopes=scopes)
grpc_secure_channel.assert_called()
credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None)

# TODO: remove if/else once google-auth >= 1.25.0 is required
if pkg_resources.get_distribution("google-auth").version >= "1.25.0":
credentials.with_scopes.assert_called_once_with(scopes, default_scopes=None)
else:
credentials.with_scopes.assert_called_once_with(scopes)


class TestChannelStub(object):
Expand Down