Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5642851

Browse files
author
Jon Wayne Parrott
authored
Add system tests for service account credentials (#51)
1 parent e017a4a commit 5642851

File tree

11 files changed

+239
-4
lines changed

11 files changed

+239
-4
lines changed

packages/google-auth/.travis.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ matrix:
1616
env: TOXENV=pypy
1717
- python: 3.5
1818
env: TOXENV=cover
19+
- python: 3.5
20+
env: TOXENV=py35-system SYSTEM_TEST=1
21+
- python: 2.7
22+
env: TOXENV=py27-system SYSTEM_TEST=1
1923
cache:
2024
directories:
2125
- ${HOME}/.cache
2226
install:
2327
- pip install --upgrade tox
2428
script:
25-
- tox
29+
- scripts/travis.sh
2630
deploy:
2731
provider: pypi
2832
user: google_opensource
@@ -32,3 +36,6 @@ deploy:
3236
on:
3337
tags: true
3438
repo: GoogleCloudPlatform/google-auth-library-python
39+
env:
40+
global:
41+
secure: KjV9daSNZsM3/jth2d87MEu4irHtwNwUcdIUVglz3QrIX7fAcbFqHu/2Ux3yGC+iPJd5/i2jZWWz9F6iyuKdhyMySz2WaBsbySmCs1pREcRYWcK5GL49yPb3ZOZucprD/n0VIer0+eublQeBQRMxNdxfJEs6tgkHxTeiOJ3mHaeLa/nE1PXW9Ih6fgS5NT/7CE7SO0fw1th9ZdLdRyo3a9fphDWqiGlt0oRURRnoJ7qctLYAZ7uXDn3c6oXJ/dZsio6Hjx16dPNjn0dpkpCBFYN3D7wXD02Ysm/7u+SGl2FjqA76FmmnJsqJ5Nog1QV4v7YpJdTtpfXBOuXAov4Fz9xHVssX4dYZkW5JKsfVFFIilo1vQbO4mBjYw58/gJswJygD5smwO2GawAfm62mGpdx4mFv2lwZoqJbLg1qcuI/qc8DqPrc3Y1nVNu3oGdMcts5q2IeuZgI1yzdb/Qz0gtkkIDtPzoyBlAZE9hsylBI7SdoP7VpmP+vIW21yC3GpE3TajbVD8p53c70fV4YH0LSX6pFF6RBuSFLwarG+WYtPTEy2k3d0ATMdH0gBaf19FJ04RTwsbYZPqAy328Dl3RLioZffm8BHAeKzuVsocrIiiHjJM6PqtL4II0UfbiKahxLcI7t1cGTWVhUtqrnZKZwJrbLYGd08aBvioTne/Nk=
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# Copyright 2015 Google Inc. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
18+
ROOT=$( dirname "$DIR" )
19+
20+
# Work from the project root.
21+
cd $ROOT
22+
23+
openssl aes-256-cbc -k "$1" \
24+
-in system_tests/secrets.tar.enc \
25+
-out system_tests/secrets.tar -d
26+
tar xvf system_tests/secrets.tar
27+
rm system_tests/secrets.tar
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
# Copyright 2015 Google Inc. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
18+
ROOT=$( dirname "$DIR" )
19+
20+
# Work from the project root.
21+
cd $ROOT
22+
23+
read -s -p "Enter password for encryption: " PASSWORD
24+
echo
25+
26+
tar cvf system_tests/secrets.tar system_tests/data
27+
openssl aes-256-cbc -k "$PASSWORD" \
28+
-in system_tests/secrets.tar \
29+
-out system_tests/secrets.tar.enc
30+
rm system_tests/secrets.tar
31+
32+
travis encrypt "SECRETS_PASSWORD=$PASSWORD" --add --override
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
# Copyright 2015 Google Inc. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
18+
ROOT=$( dirname "$DIR" )
19+
20+
# Work from the project root.
21+
cd $ROOT
22+
23+
# Decrypt secrets and run system tests if not on an external PR.
24+
if [[ -n $SYSTEM_TEST ]]; then
25+
if [[ $TRAVIS_SECURE_ENV_VARS == "true" ]]; then
26+
echo 'Extracting secrets.'
27+
scripts/decrypt-secrets.sh "$SECRETS_PASSWORD"
28+
else
29+
# This is an external PR, so just mark system tests as green.
30+
echo 'In system test but secrets are not available, skipping.'
31+
exit 0
32+
fi
33+
fi
34+
35+
# Run tox.
36+
tox

packages/google-auth/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
description='Google Authentication Library',
3636
long_description=long_description,
3737
url='https://github.com/GoogleCloudPlatform/google-auth-library-python',
38-
packages=find_packages(exclude='tests'),
38+
packages=find_packages(exclude=('tests', 'system_tests')),
3939
namespace_packages=('google',),
4040
install_requires=DEPENDENCIES,
4141
license='Apache 2.0',
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
data
2+
secrets.tar

packages/google-auth/system_tests/__init__.py

Whitespace-only changes.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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
37+
def request():
38+
"""A transport.request object."""
39+
yield google.auth.transport.urllib3.Request(HTTP)
40+
41+
42+
@pytest.fixture
43+
def token_info(request):
44+
"""Returns a function that obtains OAuth2 token info."""
45+
def _token_info(access_token=None, id_token=None):
46+
query_params = {}
47+
48+
if access_token is not None:
49+
query_params['access_token'] = access_token
50+
elif id_token is not None:
51+
query_params['id_token'] = id_token
52+
else:
53+
raise ValueError('No token specified.')
54+
55+
url = _helpers.update_query(TOKEN_INFO_URL, query_params)
56+
57+
response = request(url=url, method='GET')
58+
59+
return json.loads(response.data.decode('utf-8'))
60+
61+
yield _token_info
62+
63+
64+
def verify_environment():
65+
"""Checks to make sure that requisite data files are available."""
66+
if not os.path.isdir(DATA_DIR):
67+
raise EnvironmentError(
68+
'In order to run system tests, test data must exist in '
69+
'system_tests/data. See CONTRIBUTING.rst for details.')
70+
71+
72+
def pytest_configure(config):
73+
"""Pytest hook that runs before Pytest collects any tests."""
74+
verify_environment()
11.5 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
from google.auth import exceptions
16+
from google.oauth2 import service_account
17+
import pytest
18+
19+
20+
@pytest.fixture
21+
def credentials(service_account_file):
22+
yield service_account.Credentials.from_service_account_file(
23+
service_account_file)
24+
25+
26+
def test_refresh_no_scopes(request, credentials):
27+
with pytest.raises(exceptions.RefreshError):
28+
credentials.refresh(request)
29+
30+
31+
def test_refresh_success(request, credentials, token_info):
32+
credentials = credentials.with_scopes(['email', 'profile'])
33+
34+
credentials.refresh(request)
35+
36+
assert credentials.token
37+
38+
info = token_info(credentials.token)
39+
40+
assert info['email'] == credentials._service_account_email
41+
assert info['scope'] == (
42+
'https://www.googleapis.com/auth/userinfo.email '
43+
'https://www.googleapis.com/auth/userinfo.profile')

0 commit comments

Comments
 (0)