C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 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 json |
| 16 | import os |
| 17 | |
| 18 | from google.auth import _helpers |
Jon Wayne Parrott | 9a9ce2c | 2016-10-31 14:59:52 -0700 | [diff] [blame] | 19 | import google.auth.transport.requests |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 20 | import google.auth.transport.urllib3 |
| 21 | import pytest |
Jon Wayne Parrott | 9a9ce2c | 2016-10-31 14:59:52 -0700 | [diff] [blame] | 22 | import requests |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 23 | import urllib3 |
| 24 | |
| 25 | |
| 26 | HERE = os.path.dirname(__file__) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 27 | DATA_DIR = os.path.join(HERE, "data") |
Bu Sun Kim | 82e224b | 2020-03-13 13:21:18 -0700 | [diff] [blame^] | 28 | IMPERSONATED_SERVICE_ACCOUNT_FILE = os.path.join( |
| 29 | DATA_DIR, "impersonated_service_account.json" |
| 30 | ) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 31 | SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, "service_account.json") |
| 32 | AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, "authorized_user.json") |
Jon Wayne Parrott | 9a9ce2c | 2016-10-31 14:59:52 -0700 | [diff] [blame] | 33 | URLLIB3_HTTP = urllib3.PoolManager(retries=False) |
| 34 | REQUESTS_SESSION = requests.Session() |
| 35 | REQUESTS_SESSION.verify = False |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 36 | TOKEN_INFO_URL = "https://www.googleapis.com/oauth2/v3/tokeninfo" |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 37 | |
| 38 | |
| 39 | @pytest.fixture |
| 40 | def service_account_file(): |
| 41 | """The full path to a valid service account key file.""" |
Jon Wayne Parrott | bbc3943 | 2016-10-27 23:12:39 -0700 | [diff] [blame] | 42 | yield SERVICE_ACCOUNT_FILE |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 43 | |
| 44 | |
| 45 | @pytest.fixture |
Bu Sun Kim | 82e224b | 2020-03-13 13:21:18 -0700 | [diff] [blame^] | 46 | def impersonated_service_account_file(): |
| 47 | """The full path to a valid service account key file.""" |
| 48 | yield IMPERSONATED_SERVICE_ACCOUNT_FILE |
| 49 | |
| 50 | |
| 51 | @pytest.fixture |
Jon Wayne Parrott | cf672c2 | 2016-10-26 09:41:00 -0700 | [diff] [blame] | 52 | def authorized_user_file(): |
| 53 | """The full path to a valid authorized user file.""" |
Jon Wayne Parrott | bbc3943 | 2016-10-27 23:12:39 -0700 | [diff] [blame] | 54 | yield AUTHORIZED_USER_FILE |
Jon Wayne Parrott | cf672c2 | 2016-10-26 09:41:00 -0700 | [diff] [blame] | 55 | |
| 56 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 57 | @pytest.fixture(params=["urllib3", "requests"]) |
Jon Wayne Parrott | 9a9ce2c | 2016-10-31 14:59:52 -0700 | [diff] [blame] | 58 | def http_request(request): |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 59 | """A transport.request object.""" |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 60 | if request.param == "urllib3": |
Jon Wayne Parrott | 9a9ce2c | 2016-10-31 14:59:52 -0700 | [diff] [blame] | 61 | yield google.auth.transport.urllib3.Request(URLLIB3_HTTP) |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 62 | elif request.param == "requests": |
Jon Wayne Parrott | 9a9ce2c | 2016-10-31 14:59:52 -0700 | [diff] [blame] | 63 | yield google.auth.transport.requests.Request(REQUESTS_SESSION) |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 64 | |
| 65 | |
| 66 | @pytest.fixture |
Jon Wayne Parrott | 421cc9b | 2016-10-26 21:05:16 -0700 | [diff] [blame] | 67 | def token_info(http_request): |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 68 | """Returns a function that obtains OAuth2 token info.""" |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 69 | |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 70 | def _token_info(access_token=None, id_token=None): |
| 71 | query_params = {} |
| 72 | |
| 73 | if access_token is not None: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 74 | query_params["access_token"] = access_token |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 75 | elif id_token is not None: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 76 | query_params["id_token"] = id_token |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 77 | else: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 78 | raise ValueError("No token specified.") |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 79 | |
| 80 | url = _helpers.update_query(TOKEN_INFO_URL, query_params) |
| 81 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 82 | response = http_request(url=url, method="GET") |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 83 | |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 84 | return json.loads(response.data.decode("utf-8")) |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 85 | |
| 86 | yield _token_info |
| 87 | |
| 88 | |
Jon Wayne Parrott | bbc3943 | 2016-10-27 23:12:39 -0700 | [diff] [blame] | 89 | @pytest.fixture |
| 90 | def verify_refresh(http_request): |
| 91 | """Returns a function that verifies that credentials can be refreshed.""" |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 92 | |
Jon Wayne Parrott | bbc3943 | 2016-10-27 23:12:39 -0700 | [diff] [blame] | 93 | def _verify_refresh(credentials): |
| 94 | if credentials.requires_scopes: |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 95 | credentials = credentials.with_scopes(["email", "profile"]) |
Jon Wayne Parrott | bbc3943 | 2016-10-27 23:12:39 -0700 | [diff] [blame] | 96 | |
| 97 | credentials.refresh(http_request) |
| 98 | |
| 99 | assert credentials.token |
| 100 | assert credentials.valid |
| 101 | |
| 102 | yield _verify_refresh |
| 103 | |
| 104 | |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 105 | def verify_environment(): |
| 106 | """Checks to make sure that requisite data files are available.""" |
| 107 | if not os.path.isdir(DATA_DIR): |
| 108 | raise EnvironmentError( |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 109 | "In order to run system tests, test data must exist in " |
| 110 | "system_tests/data. See CONTRIBUTING.rst for details." |
| 111 | ) |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 112 | |
| 113 | |
| 114 | def pytest_configure(config): |
| 115 | """Pytest hook that runs before Pytest collects any tests.""" |
| 116 | verify_environment() |