blob: 365bc91d3481a719177865a0dde1950eca9c2109 [file] [log] [blame]
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -07001# Copyright 2016 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import google.auth
16import google.auth.credentials
Jon Wayne Parrottb8f48d02017-02-24 09:03:24 -080017import google.auth.jwt
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070018import google.auth.transport.grpc
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080019from google.cloud.gapic.pubsub.v1 import publisher_client
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070020
21
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080022def test_grpc_request_with_regular_credentials(http_request):
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070023 credentials, project_id = google.auth.default()
24 credentials = google.auth.credentials.with_scopes_if_required(
25 credentials, ['https://www.googleapis.com/auth/pubsub'])
26
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070027 channel = google.auth.transport.grpc.secure_authorized_channel(
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080028 credentials,
29 http_request,
30 publisher_client.PublisherClient.SERVICE_ADDRESS)
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070031
32 # Create a pub/sub client.
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080033 client = publisher_client.PublisherClient(channel=channel)
34
35 # list the topics and drain the iterator to test that an authorized API
36 # call works.
37 list_topics_iter = client.list_topics(
38 project='projects/{}'.format(project_id))
39 list(list_topics_iter)
40
41
Jon Wayne Parrottcfbfd252017-03-28 13:03:11 -070042def test_grpc_request_with_jwt_credentials():
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080043 credentials, project_id = google.auth.default()
Jon Wayne Parrottab086892017-02-23 09:20:14 -080044 audience = 'https://{}/google.pubsub.v1.Publisher'.format(
45 publisher_client.PublisherClient.SERVICE_ADDRESS)
Jon Wayne Parrottb8f48d02017-02-24 09:03:24 -080046 credentials = google.auth.jwt.Credentials.from_signing_credentials(
47 credentials,
Jon Wayne Parrottab086892017-02-23 09:20:14 -080048 audience=audience)
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080049
50 channel = google.auth.transport.grpc.secure_authorized_channel(
51 credentials,
Jon Wayne Parrottcfbfd252017-03-28 13:03:11 -070052 None,
53 publisher_client.PublisherClient.SERVICE_ADDRESS)
54
55 # Create a pub/sub client.
56 client = publisher_client.PublisherClient(channel=channel)
57
58 # list the topics and drain the iterator to test that an authorized API
59 # call works.
60 list_topics_iter = client.list_topics(
61 project='projects/{}'.format(project_id))
62 list(list_topics_iter)
63
64
65def test_grpc_request_with_on_demand_jwt_credentials():
66 credentials, project_id = google.auth.default()
67 credentials = google.auth.jwt.OnDemandCredentials.from_signing_credentials(
68 credentials)
69
70 channel = google.auth.transport.grpc.secure_authorized_channel(
71 credentials,
72 None,
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080073 publisher_client.PublisherClient.SERVICE_ADDRESS)
74
75 # Create a pub/sub client.
76 client = publisher_client.PublisherClient(channel=channel)
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070077
78 # list the topics and drain the iterator to test that an authorized API
79 # call works.
80 list_topics_iter = client.list_topics(
81 project='projects/{}'.format(project_id))
82 list(list_topics_iter)