|
| 1 | +# 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 | + |
| 15 | +import json |
| 16 | +import os |
| 17 | + |
| 18 | +from google.auth import _helpers |
| 19 | +import google.auth.transport.urllib3 |
| 20 | +import pytest |
| 21 | +import urllib3 |
| 22 | + |
| 23 | + |
| 24 | +HERE = os.path.dirname(__file__) |
| 25 | +DATA_DIR = os.path.join(HERE, 'data') |
| 26 | +HTTP = urllib3.PoolManager() |
| 27 | +TOKEN_INFO_URL = 'https://www.googleapis.com/oauth2/v3/tokeninfo' |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def service_account_file(): |
| 32 | + """The full path to a valid service account key file.""" |
| 33 | + yield os.path.join(DATA_DIR, 'service_account.json') |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def request(): |
| 38 | + """A transport.request object.""" |
| 39 | + yield google.auth.transport.urllib3.Request(HTTP) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def token_info(request): |
| 44 | + """Returns a function that obtains OAuth2 token info.""" |
| 45 | + def _token_info(access_token=None, id_token=None): |
| 46 | + query_params = {} |
| 47 | + |
| 48 | + if access_token is not None: |
| 49 | + query_params['access_token'] = access_token |
| 50 | + elif id_token is not None: |
| 51 | + query_params['id_token'] = id_token |
| 52 | + else: |
| 53 | + raise ValueError('No token specified.') |
| 54 | + |
| 55 | + url = _helpers.update_query(TOKEN_INFO_URL, query_params) |
| 56 | + |
| 57 | + response = request(url=url, method='GET') |
| 58 | + |
| 59 | + return json.loads(response.data.decode('utf-8')) |
| 60 | + |
| 61 | + yield _token_info |
| 62 | + |
| 63 | + |
| 64 | +def verify_environment(): |
| 65 | + """Checks to make sure that requisite data files are available.""" |
| 66 | + if not os.path.isdir(DATA_DIR): |
| 67 | + raise EnvironmentError( |
| 68 | + 'In order to run system tests, test data must exist in ' |
| 69 | + 'system_tests/data. See CONTRIBUTING.rst for details.') |
| 70 | + |
| 71 | + |
| 72 | +def pytest_configure(config): |
| 73 | + """Pytest hook that runs before Pytest collects any tests.""" |
| 74 | + verify_environment() |
0 commit comments