blob: d0f7cc02317b0a5a08c596c740a9987402575686 [file] [log] [blame]
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -07001# 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
15import json
16import os
17
18from google.auth import _helpers
Jon Wayne Parrott9a9ce2c2016-10-31 14:59:52 -070019import google.auth.transport.requests
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070020import google.auth.transport.urllib3
21import pytest
Jon Wayne Parrott9a9ce2c2016-10-31 14:59:52 -070022import requests
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070023import urllib3
24
25
26HERE = os.path.dirname(__file__)
27DATA_DIR = os.path.join(HERE, 'data')
Jon Wayne Parrottbbc39432016-10-27 23:12:39 -070028SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json')
29AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json')
Jon Wayne Parrott9a9ce2c2016-10-31 14:59:52 -070030URLLIB3_HTTP = urllib3.PoolManager(retries=False)
31REQUESTS_SESSION = requests.Session()
32REQUESTS_SESSION.verify = False
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070033TOKEN_INFO_URL = 'https://www.googleapis.com/oauth2/v3/tokeninfo'
34
35
36@pytest.fixture
37def service_account_file():
38 """The full path to a valid service account key file."""
Jon Wayne Parrottbbc39432016-10-27 23:12:39 -070039 yield SERVICE_ACCOUNT_FILE
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070040
41
42@pytest.fixture
Jon Wayne Parrottcf672c22016-10-26 09:41:00 -070043def authorized_user_file():
44 """The full path to a valid authorized user file."""
Jon Wayne Parrottbbc39432016-10-27 23:12:39 -070045 yield AUTHORIZED_USER_FILE
Jon Wayne Parrottcf672c22016-10-26 09:41:00 -070046
47
Jon Wayne Parrott9a9ce2c2016-10-31 14:59:52 -070048@pytest.fixture(params=['urllib3', 'requests'])
49def http_request(request):
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070050 """A transport.request object."""
Jon Wayne Parrott9a9ce2c2016-10-31 14:59:52 -070051 if request.param == 'urllib3':
52 yield google.auth.transport.urllib3.Request(URLLIB3_HTTP)
53 elif request.param == 'requests':
54 yield google.auth.transport.requests.Request(REQUESTS_SESSION)
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070055
56
57@pytest.fixture
Jon Wayne Parrott421cc9b2016-10-26 21:05:16 -070058def token_info(http_request):
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070059 """Returns a function that obtains OAuth2 token info."""
60 def _token_info(access_token=None, id_token=None):
61 query_params = {}
62
63 if access_token is not None:
64 query_params['access_token'] = access_token
65 elif id_token is not None:
66 query_params['id_token'] = id_token
67 else:
68 raise ValueError('No token specified.')
69
70 url = _helpers.update_query(TOKEN_INFO_URL, query_params)
71
Jon Wayne Parrott421cc9b2016-10-26 21:05:16 -070072 response = http_request(url=url, method='GET')
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070073
74 return json.loads(response.data.decode('utf-8'))
75
76 yield _token_info
77
78
Jon Wayne Parrottbbc39432016-10-27 23:12:39 -070079@pytest.fixture
80def verify_refresh(http_request):
81 """Returns a function that verifies that credentials can be refreshed."""
82 def _verify_refresh(credentials):
83 if credentials.requires_scopes:
84 credentials = credentials.with_scopes(['email', 'profile'])
85
86 credentials.refresh(http_request)
87
88 assert credentials.token
89 assert credentials.valid
90
91 yield _verify_refresh
92
93
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070094def verify_environment():
95 """Checks to make sure that requisite data files are available."""
96 if not os.path.isdir(DATA_DIR):
97 raise EnvironmentError(
98 'In order to run system tests, test data must exist in '
99 'system_tests/data. See CONTRIBUTING.rst for details.')
100
101
102def pytest_configure(config):
103 """Pytest hook that runs before Pytest collects any tests."""
104 verify_environment()