Skip to content

Commit

Permalink
Add system tests; address review comments; skip tests when grpc isn't…
Browse files Browse the repository at this point in the history
… available.
  • Loading branch information
Jon Wayne Parrott committed Oct 31, 2016
1 parent b6c6949 commit 9b5ba65
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 17 deletions.
14 changes: 6 additions & 8 deletions google/auth/transport/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""Authorization support for gRPC."""

from __future__ import absolute_import

import grpc


Expand Down Expand Up @@ -101,20 +103,16 @@ def secure_authorized_channel(
grpc.Channel: The created gRPC channel.
"""
# Create the metadata plugin for inserting the authorization header.
google_auth_credentials_metadata_plugin = AuthMetadataPlugin(
credentials, request)
metadata_plugin = AuthMetadataPlugin(credentials, request)

# Create a set of grpc.CallCredentials using the metadata plugin.
google_auth_credentials = grpc.metadata_call_credentials(
google_auth_credentials_metadata_plugin)
google_auth_credentials = grpc.metadata_call_credentials(metadata_plugin)

if not ssl_credentials:
if ssl_credentials is None:
ssl_credentials = grpc.ssl_channel_credentials()

# Combine the ssl credentials and the authorization credentials.
composite_credentials = grpc.composite_channel_credentials(
ssl_credentials, google_auth_credentials)

channel = grpc.secure_channel(target, composite_credentials)

return channel
return grpc.secure_channel(target, composite_credentials)
6 changes: 0 additions & 6 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,6 @@ method-rgx=[a-z_][a-z0-9_]{2,40}$
# especially those that implemented wordy RFCs.
function-rgx=[a-z_][a-z0-9_]{2,40}$

# Regular expression matching correct variable names
# DEFAULT: variable-rgx=[a-z_][a-z0-9_]{2,30}$
# RATIONALE: Prefer longer, more specific variable names to avoid ambiguity.
# especially for methods that implement wordy rfcs or use wordy libraries.
variable-rgx=[a-z_][a-z0-9_]{2,40}$

[TYPECHECK]
# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
Expand Down
2 changes: 1 addition & 1 deletion pylintrc.tests
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ reports=no
# DEFAULT: good-names=i,j,k,ex,Run,_
# RATIONALE: 'fh' is a well-known file handle variable name.
good-names = i, j, k, ex, Run, _,
fh
fh, pytestmark

# Regular expression matching correct method names
# DEFAULT: method-rgx=[a-z_][a-z0-9_]{2,30}$
Expand Down
6 changes: 6 additions & 0 deletions system_tests/nox.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,9 @@ def session_app_engine(session):
session.env['TEST_APP_URL'] = application_url
session.chdir(HERE)
session.run('pytest', 'test_app_engine.py')


def session_grpc(session):
session.virtualenv = False
session.env[EXPLICIT_CREDENTIALS_ENV] = SERVICE_ACCOUNT_FILE
session.run('pytest', 'test_grpc.py')
40 changes: 40 additions & 0 deletions system_tests/test_grpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import google.auth
import google.auth.credentials
import google.auth.transport.grpc
from google.cloud.gapic.pubsub.v1 import publisher_api


def test_grpc_request(http_request):
credentials, project_id = google.auth.default()
credentials = google.auth.credentials.with_scopes_if_required(
credentials, ['https://www.googleapis.com/auth/pubsub'])

target = '{}:{}'.format(
publisher_api.PublisherApi.SERVICE_ADDRESS,
publisher_api.PublisherApi.DEFAULT_SERVICE_PORT)

channel = google.auth.transport.grpc.secure_authorized_channel(
credentials, target, http_request)

# Create a pub/sub client.
client = publisher_api.PublisherApi(channel=channel)

# list the topics and drain the iterator to test that an authorized API
# call works.
list_topics_iter = client.list_topics(
project='projects/{}'.format(project_id))
list(list_topics_iter)
10 changes: 9 additions & 1 deletion tests/transport/test_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@

import mock

import pytest

import google.auth.transport.grpc
try:
import google.auth.transport.grpc
HAS_GRPC = True
except ImportError: # pragma: NO COVER
HAS_GRPC = False


pytestmark = pytest.mark.skipif(not HAS_GRPC, reason='gRPC is unavailable.')


class MockCredentials(object):
Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ deps =
urllib3
certifi
requests
grpcio
grpcio; platform_python_implementation != 'PyPy'
commands =
py.test --cov=google.auth --cov=google.oauth2 --cov=tests {posargs:tests}

Expand All @@ -31,6 +31,7 @@ commands =
deps =
{[testenv]deps}
nox-automation
gapic-google-pubsub-v1==0.11.1
passenv =
SKIP_APP_ENGINE_SYSTEM_TEST
CLOUD_SDK_ROOT
Expand All @@ -43,6 +44,7 @@ commands =
deps =
{[testenv]deps}
nox-automation
gapic-google-pubsub-v1==0.11.1
passenv =
SKIP_APP_ENGINE_SYSTEM_TEST
CLOUD_SDK_ROOT
Expand Down

0 comments on commit 9b5ba65

Please sign in to comment.