Skip to content

Commit 12bf8cf

Browse files
author
Jon Wayne Parrott
authored
Add oauth2.credentials system tests (#56)
* Add script to generate user auth tokens. * Add oauth2.credentials system tests
1 parent 6c7f5d9 commit 12bf8cf

5 files changed

Lines changed: 115 additions & 1 deletion

File tree

packages/google-auth/.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ deploy:
3838
repo: GoogleCloudPlatform/google-auth-library-python
3939
env:
4040
global:
41-
secure: KjV9daSNZsM3/jth2d87MEu4irHtwNwUcdIUVglz3QrIX7fAcbFqHu/2Ux3yGC+iPJd5/i2jZWWz9F6iyuKdhyMySz2WaBsbySmCs1pREcRYWcK5GL49yPb3ZOZucprD/n0VIer0+eublQeBQRMxNdxfJEs6tgkHxTeiOJ3mHaeLa/nE1PXW9Ih6fgS5NT/7CE7SO0fw1th9ZdLdRyo3a9fphDWqiGlt0oRURRnoJ7qctLYAZ7uXDn3c6oXJ/dZsio6Hjx16dPNjn0dpkpCBFYN3D7wXD02Ysm/7u+SGl2FjqA76FmmnJsqJ5Nog1QV4v7YpJdTtpfXBOuXAov4Fz9xHVssX4dYZkW5JKsfVFFIilo1vQbO4mBjYw58/gJswJygD5smwO2GawAfm62mGpdx4mFv2lwZoqJbLg1qcuI/qc8DqPrc3Y1nVNu3oGdMcts5q2IeuZgI1yzdb/Qz0gtkkIDtPzoyBlAZE9hsylBI7SdoP7VpmP+vIW21yC3GpE3TajbVD8p53c70fV4YH0LSX6pFF6RBuSFLwarG+WYtPTEy2k3d0ATMdH0gBaf19FJ04RTwsbYZPqAy328Dl3RLioZffm8BHAeKzuVsocrIiiHjJM6PqtL4II0UfbiKahxLcI7t1cGTWVhUtqrnZKZwJrbLYGd08aBvioTne/Nk=
41+
secure: s6GdhJklftl8w/9WoETwLtvtKL4ledPA/TuBuqCXQxSuYWaPuTdRVcvoejGkHJpp7i/7v2T/0etYl+5koyskKm5+QZZweaaL7MAyjPGp+hmIaIlWQRz6w481NOf3i9uSmoQycssT0mNmwScNIqo+igbA2y14mr/e9aBuOcxNNzNzFQp2vaRMEju6q7xZMjYdcudUWL48vq9CoNa3X2ZArpqjkApR/TfYlG7glOj43NxuVDN4z9wIyUjaMHBfPgEhjaOaRyEFgEYITRwX1qDoXqcZdTVIq4Cn0uCH+Mvrz6Y+oUJGTJqH1k7N/DhzbSN9lJnVYaQW/yuvGHiGAwbb6Tcxiq2UqqhA9MfbPpmstDECs46v9Z3BT252KvYEQY7Q1v9g2gFhHvFGWISUxs80rnnPhEYfa11JoLvj2t8cowkE4pvj4OH32Eoyvc5H07hW3F5xpuF7Jt7N09TNZkUrpmiRJEhfrVNgjsrWO77/q5h8mXGd+9vYmz++yzKu+63x8x1MpeigGCG73Dpu9Otm5eydOZfpJ39ZfZWUb7G2JahgHaGweM9dmnpJtzHQgijmHjjfAx9jgnQ8IQz9nkFmyMI8H7HouwalnrJtpSSbvMqOQ0kiZhMzdBKH5pD3tjLgSlgA0pKelBwlooY6jGlj4LrtbDAxa6cZyXiFoqWpT1w=
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
"""This program obtains a set of user credentials.
16+
17+
These credentials are needed to run the system test for OAuth2 credentials.
18+
It's expected that a developer will run this program manually once to obtain
19+
a refresh token. It's highly recommended to use a Google account created
20+
specifically created for testing.
21+
"""
22+
23+
import json
24+
import os
25+
26+
from oauth2client import client
27+
from oauth2client import tools
28+
29+
HERE = os.path.dirname(__file__)
30+
CLIENT_SECRETS_PATH = os.path.abspath(os.path.join(
31+
HERE, '..', 'system_tests', 'data', 'client_secret.json'))
32+
AUTHORIZED_USER_PATH = os.path.abspath(os.path.join(
33+
HERE, '..', 'system_tests', 'data', 'authorized_user.json'))
34+
SCOPES = ['email', 'profile']
35+
36+
37+
class NullStorage(client.Storage):
38+
"""Null storage implementation to prevent oauth2client from failing
39+
on storage.put."""
40+
def locked_put(self, credentials):
41+
pass
42+
43+
44+
def main():
45+
flow = client.flow_from_clientsecrets(CLIENT_SECRETS_PATH, SCOPES)
46+
47+
print('Starting credentials flow...')
48+
credentials = tools.run_flow(flow, NullStorage())
49+
50+
# Save the credentials in the same format as the Cloud SDK's authorized
51+
# user file.
52+
data = {
53+
'type': 'authorized_user',
54+
'client_id': flow.client_id,
55+
'client_secret': flow.client_secret,
56+
'refresh_token': credentials.refresh_token
57+
}
58+
59+
with open(AUTHORIZED_USER_PATH, 'w') as fh:
60+
json.dump(data, fh, indent=4)
61+
62+
print('Created {}.'.format(AUTHORIZED_USER_PATH))
63+
64+
if __name__ == '__main__':
65+
main()

packages/google-auth/system_tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ def service_account_file():
3333
yield os.path.join(DATA_DIR, 'service_account.json')
3434

3535

36+
@pytest.fixture
37+
def 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+
3642
@pytest.fixture
3743
def request():
3844
"""A transport.request object."""
6.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+
import json
16+
17+
from google.auth import _helpers
18+
import google.oauth2.credentials
19+
20+
GOOGLE_OAUTH2_TOKEN_ENDPOINT = 'https://accounts.google.com/o/oauth2/token'
21+
22+
23+
def test_refresh(authorized_user_file, request, token_info):
24+
with open(authorized_user_file, 'r') as fh:
25+
info = json.load(fh)
26+
27+
credentials = google.oauth2.credentials.Credentials(
28+
None, # No access token, must be refreshed.
29+
refresh_token=info['refresh_token'],
30+
token_uri=GOOGLE_OAUTH2_TOKEN_ENDPOINT,
31+
client_id=info['client_id'],
32+
client_secret=info['client_secret'])
33+
34+
credentials.refresh(request)
35+
36+
assert credentials.token
37+
38+
info = token_info(credentials.token)
39+
40+
info_scopes = _helpers.string_to_scopes(info['scope'])
41+
assert set(info_scopes) == set([
42+
'https://www.googleapis.com/auth/userinfo.email',
43+
'https://www.googleapis.com/auth/userinfo.profile'])

0 commit comments

Comments
 (0)