Skip to content

Commit d89142f

Browse files
author
Jon Wayne Parrott
authored
Add ADC system tests (#58)
1 parent 2fd54d0 commit d89142f

File tree

5 files changed

+134
-13
lines changed

5 files changed

+134
-13
lines changed

packages/google-auth/system_tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ def authorized_user_file():
4040

4141

4242
@pytest.fixture
43-
def request():
43+
def http_request():
4444
"""A transport.request object."""
4545
yield google.auth.transport.urllib3.Request(HTTP)
4646

4747

4848
@pytest.fixture
49-
def token_info(request):
49+
def token_info(http_request):
5050
"""Returns a function that obtains OAuth2 token info."""
5151
def _token_info(access_token=None, id_token=None):
5252
query_params = {}
@@ -60,7 +60,7 @@ def _token_info(access_token=None, id_token=None):
6060

6161
url = _helpers.update_query(TOKEN_INFO_URL, query_params)
6262

63-
response = request(url=url, method='GET')
63+
response = http_request(url=url, method='GET')
6464

6565
return json.loads(response.data.decode('utf-8'))
6666

packages/google-auth/system_tests/test_compute_engine.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020

2121

2222
@pytest.fixture(autouse=True)
23-
def check_gce_environment(request):
24-
if not _metadata.ping(request):
23+
def check_gce_environment(http_request):
24+
if not _metadata.ping(http_request):
2525
pytest.skip('Compute Engine metadata service is not available.')
2626

2727

28-
def test_refresh(request, token_info):
28+
def test_refresh(http_request, token_info):
2929
credentials = compute_engine.Credentials()
3030

31-
credentials.refresh(request)
31+
credentials.refresh(http_request)
3232

3333
assert credentials.token is not None
3434
assert credentials._service_account_email is not None
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 os
16+
17+
import py
18+
19+
import google.auth
20+
from google.auth import environment_vars
21+
import google.oauth2.credentials
22+
from google.oauth2 import service_account
23+
24+
25+
def validate_refresh(credentials, http_request):
26+
if credentials.requires_scopes:
27+
credentials = credentials.with_scopes(['email', 'profile'])
28+
29+
credentials.refresh(http_request)
30+
31+
assert credentials.token
32+
assert credentials.valid
33+
34+
35+
def test_explicit_credentials_service_account(
36+
monkeypatch, service_account_file, http_request):
37+
monkeypatch.setitem(
38+
os.environ, environment_vars.CREDENTIALS, service_account_file)
39+
40+
credentials, project_id = google.auth.default()
41+
42+
assert isinstance(credentials, service_account.Credentials)
43+
assert project_id is not None
44+
45+
validate_refresh(credentials, http_request)
46+
47+
48+
def test_explicit_credentials_authorized_user(
49+
monkeypatch, authorized_user_file, http_request):
50+
monkeypatch.setitem(
51+
os.environ, environment_vars.CREDENTIALS, authorized_user_file)
52+
53+
credentials, project_id = google.auth.default()
54+
55+
assert isinstance(credentials, google.oauth2.credentials.Credentials)
56+
assert project_id is None
57+
58+
validate_refresh(credentials, http_request)
59+
60+
61+
def test_explicit_credentials_explicit_project_id(
62+
monkeypatch, service_account_file, http_request):
63+
project = 'system-test-project'
64+
monkeypatch.setitem(
65+
os.environ, environment_vars.CREDENTIALS, service_account_file)
66+
monkeypatch.setitem(
67+
os.environ, environment_vars.PROJECT, project)
68+
69+
_, project_id = google.auth.default()
70+
71+
assert project_id == project
72+
73+
74+
def generate_cloud_sdk_config(
75+
tmpdir, credentials_file, active_config='default', project=None):
76+
tmpdir.join('active_config').write(
77+
'{}\n'.format(active_config), ensure=True)
78+
79+
if project is not None:
80+
config_file = tmpdir.join(
81+
'configurations', 'config_{}'.format(active_config))
82+
config_file.write(
83+
'[core]\nproject = {}'.format(project), ensure=True)
84+
85+
py.path.local(credentials_file).copy(
86+
tmpdir.join('application_default_credentials.json'))
87+
88+
89+
def test_cloud_sdk_credentials_service_account(
90+
tmpdir, monkeypatch, service_account_file, http_request):
91+
# Create the Cloud SDK configuration tree
92+
project = 'system-test-project'
93+
generate_cloud_sdk_config(tmpdir, service_account_file, project=project)
94+
monkeypatch.setitem(
95+
os.environ, environment_vars.CLOUD_SDK_CONFIG_DIR, str(tmpdir))
96+
97+
credentials, project_id = google.auth.default()
98+
99+
assert isinstance(credentials, service_account.Credentials)
100+
assert project_id is not None
101+
# The project ID should be the project ID specified in the the service
102+
# account file, not the project in the config.
103+
assert project_id is not project
104+
105+
validate_refresh(credentials, http_request)
106+
107+
108+
def test_cloud_sdk_credentials_authorized_user(
109+
tmpdir, monkeypatch, authorized_user_file, http_request):
110+
# Create the Cloud SDK configuration tree
111+
project = 'system-test-project'
112+
generate_cloud_sdk_config(tmpdir, authorized_user_file, project=project)
113+
monkeypatch.setitem(
114+
os.environ, environment_vars.CLOUD_SDK_CONFIG_DIR, str(tmpdir))
115+
116+
credentials, project_id = google.auth.default()
117+
118+
assert isinstance(credentials, google.oauth2.credentials.Credentials)
119+
assert project_id == project
120+
121+
validate_refresh(credentials, http_request)

packages/google-auth/system_tests/test_oauth2_credentials.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
GOOGLE_OAUTH2_TOKEN_ENDPOINT = 'https://accounts.google.com/o/oauth2/token'
2121

2222

23-
def test_refresh(authorized_user_file, request, token_info):
23+
def test_refresh(authorized_user_file, http_request, token_info):
2424
with open(authorized_user_file, 'r') as fh:
2525
info = json.load(fh)
2626

@@ -31,7 +31,7 @@ def test_refresh(authorized_user_file, request, token_info):
3131
client_id=info['client_id'],
3232
client_secret=info['client_secret'])
3333

34-
credentials.refresh(request)
34+
credentials.refresh(http_request)
3535

3636
assert credentials.token
3737

packages/google-auth/system_tests/test_service_account.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ def credentials(service_account_file):
2525
service_account_file)
2626

2727

28-
def test_refresh_no_scopes(request, credentials):
28+
def test_refresh_no_scopes(http_request, credentials):
2929
with pytest.raises(exceptions.RefreshError):
30-
credentials.refresh(request)
30+
credentials.refresh(http_request)
3131

3232

33-
def test_refresh_success(request, credentials, token_info):
33+
def test_refresh_success(http_request, credentials, token_info):
3434
credentials = credentials.with_scopes(['email', 'profile'])
3535

36-
credentials.refresh(request)
36+
credentials.refresh(http_request)
3737

3838
assert credentials.token
3939

0 commit comments

Comments
 (0)