1# Copyright 2021 Google LLC 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 17import google.auth.transport.requests 18from google.oauth2 import service_account 19 20 21def test_authorized_session_with_service_account_and_self_signed_jwt(): 22 credentials, project_id = google.auth.default() 23 24 credentials = credentials.with_scopes( 25 scopes=[], 26 default_scopes=["https://www.googleapis.com/auth/pubsub"], 27 ) 28 29 http = google.auth.transport.urllib3.AuthorizedHttp( 30 credentials=credentials, default_host="pubsub.googleapis.com" 31 ) 32 33 # List Pub/Sub Topics through the REST API 34 # https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/list 35 response = http.urlopen( 36 method="GET", 37 url="https://pubsub.googleapis.com/v1/projects/{}/topics".format(project_id) 38 ) 39 40 assert response.status == 200 41 42 # Check that self-signed JWT was created and is being used 43 assert credentials._jwt_credentials is not None 44 assert credentials._jwt_credentials.token == credentials.token 45