blob: f025fc0b7969e51be28868e7d2a4fe11e30c9e7f [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
Bu Sun Kim65e33c02019-10-25 10:45:00 -070019from google.cloud import pubsub_v1
20from google.cloud.pubsub_v1.gapic import publisher_client
21from google.cloud.pubsub_v1.gapic.transports import publisher_grpc_transport
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070022
23
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080024def test_grpc_request_with_regular_credentials(http_request):
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070025 credentials, project_id = google.auth.default()
26 credentials = google.auth.credentials.with_scopes_if_required(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070027 credentials, ["https://www.googleapis.com/auth/pubsub"]
28 )
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070029
Bu Sun Kim65e33c02019-10-25 10:45:00 -070030 transport = publisher_grpc_transport.PublisherGrpcTransport(
31 address=publisher_client.PublisherClient.SERVICE_ADDRESS,
32 credentials=credentials,
Bu Sun Kim9eec0912019-10-21 17:04:21 -070033 )
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070034
35 # Create a pub/sub client.
Bu Sun Kim65e33c02019-10-25 10:45:00 -070036 client = pubsub_v1.PublisherClient(transport=transport)
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080037
38 # list the topics and drain the iterator to test that an authorized API
39 # call works.
Bu Sun Kim9eec0912019-10-21 17:04:21 -070040 list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080041 list(list_topics_iter)
42
43
Jon Wayne Parrottcfbfd252017-03-28 13:03:11 -070044def test_grpc_request_with_jwt_credentials():
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080045 credentials, project_id = google.auth.default()
Bu Sun Kim65e33c02019-10-25 10:45:00 -070046 audience = "https://pubsub.googleapis.com/google.pubsub.v1.Publisher"
Jon Wayne Parrottb8f48d02017-02-24 09:03:24 -080047 credentials = google.auth.jwt.Credentials.from_signing_credentials(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070048 credentials, audience=audience
49 )
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080050
Bu Sun Kim65e33c02019-10-25 10:45:00 -070051 transport = publisher_grpc_transport.PublisherGrpcTransport(
52 address=publisher_client.PublisherClient.SERVICE_ADDRESS,
53 credentials=credentials,
Bu Sun Kim9eec0912019-10-21 17:04:21 -070054 )
Jon Wayne Parrottcfbfd252017-03-28 13:03:11 -070055
56 # Create a pub/sub client.
Bu Sun Kim65e33c02019-10-25 10:45:00 -070057 client = pubsub_v1.PublisherClient(transport=transport)
Jon Wayne Parrottcfbfd252017-03-28 13:03:11 -070058
59 # list the topics and drain the iterator to test that an authorized API
60 # call works.
Bu Sun Kim9eec0912019-10-21 17:04:21 -070061 list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
Jon Wayne Parrottcfbfd252017-03-28 13:03:11 -070062 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(
Bu Sun Kim9eec0912019-10-21 17:04:21 -070068 credentials
69 )
Jon Wayne Parrottcfbfd252017-03-28 13:03:11 -070070
Bu Sun Kim65e33c02019-10-25 10:45:00 -070071 transport = publisher_grpc_transport.PublisherGrpcTransport(
72 address=publisher_client.PublisherClient.SERVICE_ADDRESS,
73 credentials=credentials,
Bu Sun Kim9eec0912019-10-21 17:04:21 -070074 )
Jon Wayne Parrotta2098192017-02-22 09:27:32 -080075
76 # Create a pub/sub client.
Bu Sun Kim65e33c02019-10-25 10:45:00 -070077 client = pubsub_v1.PublisherClient(transport=transport)
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070078
79 # list the topics and drain the iterator to test that an authorized API
80 # call works.
Bu Sun Kim9eec0912019-10-21 17:04:21 -070081 list_topics_iter = client.list_topics(project="projects/{}".format(project_id))
Jon Wayne Parrottb9897dc2016-11-02 20:31:14 -070082 list(list_topics_iter)