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 json 16import os 17 18from google.auth import _helpers 19import google.auth.transport.requests 20import google.auth.transport.urllib3 21import pytest 22import requests 23import urllib3 24 25import aiohttp 26from google.auth.transport import _aiohttp_requests as aiohttp_requests 27from system_tests.system_tests_sync import conftest as sync_conftest 28 29 30TOKEN_INFO_URL = "https://www.googleapis.com/oauth2/v3/tokeninfo" 31 32 33@pytest.fixture 34def service_account_file(): 35 """The full path to a valid service account key file.""" 36 yield sync_conftest.SERVICE_ACCOUNT_FILE 37 38 39@pytest.fixture 40def impersonated_service_account_file(): 41 """The full path to a valid service account key file.""" 42 yield sync_conftest.IMPERSONATED_SERVICE_ACCOUNT_FILE 43 44 45@pytest.fixture 46def authorized_user_file(): 47 """The full path to a valid authorized user file.""" 48 yield sync_conftest.AUTHORIZED_USER_FILE 49 50 51@pytest.fixture 52async def aiohttp_session(): 53 async with aiohttp.ClientSession(auto_decompress=False) as session: 54 yield session 55 56 57@pytest.fixture(params=["aiohttp"]) 58async def http_request(request, aiohttp_session): 59 """A transport.request object.""" 60 yield aiohttp_requests.Request(aiohttp_session) 61 62 63@pytest.fixture 64async def token_info(http_request): 65 """Returns a function that obtains OAuth2 token info.""" 66 67 async def _token_info(access_token=None, id_token=None): 68 query_params = {} 69 70 if access_token is not None: 71 query_params["access_token"] = access_token 72 elif id_token is not None: 73 query_params["id_token"] = id_token 74 else: 75 raise ValueError("No token specified.") 76 77 url = _helpers.update_query(sync_conftest.TOKEN_INFO_URL, query_params) 78 79 response = await http_request(url=url, method="GET") 80 81 data = await response.content() 82 83 return json.loads(data.decode("utf-8")) 84 85 yield _token_info 86 87 88@pytest.fixture 89async def verify_refresh(http_request): 90 """Returns a function that verifies that credentials can be refreshed.""" 91 92 async def _verify_refresh(credentials): 93 if credentials.requires_scopes: 94 credentials = credentials.with_scopes(["email", "profile"]) 95 96 await credentials.refresh(http_request) 97 98 assert credentials.token 99 assert credentials.valid 100 101 yield _verify_refresh 102 103 104def verify_environment(): 105 """Checks to make sure that requisite data files are available.""" 106 if not os.path.isdir(sync_conftest.DATA_DIR): 107 raise EnvironmentError( 108 "In order to run system tests, test data must exist in " 109 "system_tests/data. See CONTRIBUTING.rst for details." 110 ) 111 112 113def pytest_configure(config): 114 """Pytest hook that runs before Pytest collects any tests.""" 115 verify_environment() 116