blob: afa78545ec47cfdade98d55862079ab0dff81a83 [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')
Jon Wayne Parrottbbc39432016-10-27 23:12:39 -070026SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json')
27AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json')
28HTTP = urllib3.PoolManager(retries=False)
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070029TOKEN_INFO_URL = 'https://www.googleapis.com/oauth2/v3/tokeninfo'
30
31
32@pytest.fixture
33def service_account_file():
34 """The full path to a valid service account key file."""
Jon Wayne Parrottbbc39432016-10-27 23:12:39 -070035 yield SERVICE_ACCOUNT_FILE
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070036
37
38@pytest.fixture
Jon Wayne Parrottcf672c22016-10-26 09:41:00 -070039def authorized_user_file():
40 """The full path to a valid authorized user file."""
Jon Wayne Parrottbbc39432016-10-27 23:12:39 -070041 yield AUTHORIZED_USER_FILE
Jon Wayne Parrottcf672c22016-10-26 09:41:00 -070042
43
44@pytest.fixture
Jon Wayne Parrott421cc9b2016-10-26 21:05:16 -070045def http_request():
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070046 """A transport.request object."""
47 yield google.auth.transport.urllib3.Request(HTTP)
48
49
50@pytest.fixture
Jon Wayne Parrott421cc9b2016-10-26 21:05:16 -070051def token_info(http_request):
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070052 """Returns a function that obtains OAuth2 token info."""
53 def _token_info(access_token=None, id_token=None):
54 query_params = {}
55
56 if access_token is not None:
57 query_params['access_token'] = access_token
58 elif id_token is not None:
59 query_params['id_token'] = id_token
60 else:
61 raise ValueError('No token specified.')
62
63 url = _helpers.update_query(TOKEN_INFO_URL, query_params)
64
Jon Wayne Parrott421cc9b2016-10-26 21:05:16 -070065 response = http_request(url=url, method='GET')
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070066
67 return json.loads(response.data.decode('utf-8'))
68
69 yield _token_info
70
71
Jon Wayne Parrottbbc39432016-10-27 23:12:39 -070072@pytest.fixture
73def verify_refresh(http_request):
74 """Returns a function that verifies that credentials can be refreshed."""
75 def _verify_refresh(credentials):
76 if credentials.requires_scopes:
77 credentials = credentials.with_scopes(['email', 'profile'])
78
79 credentials.refresh(http_request)
80
81 assert credentials.token
82 assert credentials.valid
83
84 yield _verify_refresh
85
86
Jon Wayne Parrott447c5be2016-10-25 09:32:25 -070087def verify_environment():
88 """Checks to make sure that requisite data files are available."""
89 if not os.path.isdir(DATA_DIR):
90 raise EnvironmentError(
91 'In order to run system tests, test data must exist in '
92 'system_tests/data. See CONTRIBUTING.rst for details.')
93
94
95def pytest_configure(config):
96 """Pytest hook that runs before Pytest collects any tests."""
97 verify_environment()