Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 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 |
Jon Wayne Parrott | cf672c2 | 2016-10-26 09:41:00 -0700 | [diff] [blame] | 37 | def authorized_user_file(): |
| 38 | """The full path to a valid authorized user file.""" |
| 39 | yield os.path.join(DATA_DIR, 'authorized_user.json') |
| 40 | |
| 41 | |
| 42 | @pytest.fixture |
Jon Wayne Parrott | 421cc9b | 2016-10-26 21:05:16 -0700 | [diff] [blame] | 43 | def http_request(): |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 44 | """A transport.request object.""" |
| 45 | yield google.auth.transport.urllib3.Request(HTTP) |
| 46 | |
| 47 | |
| 48 | @pytest.fixture |
Jon Wayne Parrott | 421cc9b | 2016-10-26 21:05:16 -0700 | [diff] [blame] | 49 | def token_info(http_request): |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 50 | """Returns a function that obtains OAuth2 token info.""" |
| 51 | def _token_info(access_token=None, id_token=None): |
| 52 | query_params = {} |
| 53 | |
| 54 | if access_token is not None: |
| 55 | query_params['access_token'] = access_token |
| 56 | elif id_token is not None: |
| 57 | query_params['id_token'] = id_token |
| 58 | else: |
| 59 | raise ValueError('No token specified.') |
| 60 | |
| 61 | url = _helpers.update_query(TOKEN_INFO_URL, query_params) |
| 62 | |
Jon Wayne Parrott | 421cc9b | 2016-10-26 21:05:16 -0700 | [diff] [blame] | 63 | response = http_request(url=url, method='GET') |
Jon Wayne Parrott | 447c5be | 2016-10-25 09:32:25 -0700 | [diff] [blame] | 64 | |
| 65 | return json.loads(response.data.decode('utf-8')) |
| 66 | |
| 67 | yield _token_info |
| 68 | |
| 69 | |
| 70 | def verify_environment(): |
| 71 | """Checks to make sure that requisite data files are available.""" |
| 72 | if not os.path.isdir(DATA_DIR): |
| 73 | raise EnvironmentError( |
| 74 | 'In order to run system tests, test data must exist in ' |
| 75 | 'system_tests/data. See CONTRIBUTING.rst for details.') |
| 76 | |
| 77 | |
| 78 | def pytest_configure(config): |
| 79 | """Pytest hook that runs before Pytest collects any tests.""" |
| 80 | verify_environment() |