blob: 5969beeecd4d29169844472bae5a537eb6bd08e1 [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
19import google.auth.transport.urllib3
20import pytest
21import urllib3
22
23
24HERE = os.path.dirname(__file__)
25DATA_DIR = os.path.join(HERE, 'data')
26HTTP = urllib3.PoolManager()
27TOKEN_INFO_URL = 'https://www.googleapis.com/oauth2/v3/tokeninfo'
28
29
30@pytest.fixture
31def 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 Parrottcf672c22016-10-26 09:41:00 -070037def 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 Parrott421cc9b2016-10-26 21:05:16 -070043def http_request():
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070044 """A transport.request object."""
45 yield google.auth.transport.urllib3.Request(HTTP)
46
47
48@pytest.fixture
Jon Wayne Parrott421cc9b2016-10-26 21:05:16 -070049def token_info(http_request):
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070050 """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 Parrott421cc9b2016-10-26 21:05:16 -070063 response = http_request(url=url, method='GET')
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070064
65 return json.loads(response.data.decode('utf-8'))
66
67 yield _token_info
68
69
70def 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
78def pytest_configure(config):
79 """Pytest hook that runs before Pytest collects any tests."""
80 verify_environment()