1# Copyright 2020 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 pytest 16 17from google.auth import _helpers 18from google.auth import exceptions 19from google.auth import iam 20from google.oauth2 import _service_account_async 21 22 23@pytest.fixture 24def credentials(service_account_file): 25 yield _service_account_async.Credentials.from_service_account_file(service_account_file) 26 27 28@pytest.mark.asyncio 29async def test_refresh_no_scopes(http_request, credentials): 30 """ 31 We expect the http request to refresh credentials 32 without scopes provided to throw an error. 33 """ 34 with pytest.raises(exceptions.RefreshError): 35 await credentials.refresh(http_request) 36 37@pytest.mark.asyncio 38async def test_refresh_success(http_request, credentials, token_info): 39 credentials = credentials.with_scopes(["email", "profile"]) 40 await credentials.refresh(http_request) 41 42 assert credentials.token 43 44 info = await token_info(credentials.token) 45 46 assert info["email"] == credentials.service_account_email 47 info_scopes = _helpers.string_to_scopes(info["scope"]) 48 assert set(info_scopes) == set( 49 [ 50 "https://www.googleapis.com/auth/userinfo.email", 51 "https://www.googleapis.com/auth/userinfo.profile", 52 ] 53 ) 54