Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 1 | # 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 | |
| 15 | import google.auth |
| 16 | import google.auth.credentials |
| 17 | import google.auth.transport.grpc |
Jon Wayne Parrott | a209819 | 2017-02-22 09:27:32 -0800 | [diff] [blame] | 18 | from google.cloud.gapic.pubsub.v1 import publisher_client |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 19 | |
| 20 | |
Jon Wayne Parrott | a209819 | 2017-02-22 09:27:32 -0800 | [diff] [blame] | 21 | def test_grpc_request_with_regular_credentials(http_request): |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 22 | credentials, project_id = google.auth.default() |
| 23 | credentials = google.auth.credentials.with_scopes_if_required( |
| 24 | credentials, ['https://www.googleapis.com/auth/pubsub']) |
| 25 | |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 26 | channel = google.auth.transport.grpc.secure_authorized_channel( |
Jon Wayne Parrott | a209819 | 2017-02-22 09:27:32 -0800 | [diff] [blame] | 27 | credentials, |
| 28 | http_request, |
| 29 | publisher_client.PublisherClient.SERVICE_ADDRESS) |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 30 | |
| 31 | # Create a pub/sub client. |
Jon Wayne Parrott | a209819 | 2017-02-22 09:27:32 -0800 | [diff] [blame] | 32 | client = publisher_client.PublisherClient(channel=channel) |
| 33 | |
| 34 | # list the topics and drain the iterator to test that an authorized API |
| 35 | # call works. |
| 36 | list_topics_iter = client.list_topics( |
| 37 | project='projects/{}'.format(project_id)) |
| 38 | list(list_topics_iter) |
| 39 | |
| 40 | |
| 41 | def test_grpc_request_with_jwt_credentials(http_request): |
| 42 | credentials, project_id = google.auth.default() |
Jon Wayne Parrott | ab08689 | 2017-02-23 09:20:14 -0800 | [diff] [blame^] | 43 | audience = 'https://{}/google.pubsub.v1.Publisher'.format( |
| 44 | publisher_client.PublisherClient.SERVICE_ADDRESS) |
| 45 | credentials = credentials.to_jwt_credentials( |
| 46 | audience=audience) |
Jon Wayne Parrott | a209819 | 2017-02-22 09:27:32 -0800 | [diff] [blame] | 47 | |
| 48 | channel = google.auth.transport.grpc.secure_authorized_channel( |
| 49 | credentials, |
| 50 | http_request, |
| 51 | publisher_client.PublisherClient.SERVICE_ADDRESS) |
| 52 | |
| 53 | # Create a pub/sub client. |
| 54 | client = publisher_client.PublisherClient(channel=channel) |
Jon Wayne Parrott | b9897dc | 2016-11-02 20:31:14 -0700 | [diff] [blame] | 55 | |
| 56 | # list the topics and drain the iterator to test that an authorized API |
| 57 | # call works. |
| 58 | list_topics_iter = client.list_topics( |
| 59 | project='projects/{}'.format(project_id)) |
| 60 | list(list_topics_iter) |