blob: 02242e94f820a1aa23b5cffb2a38faac6289af30 [file] [log] [blame]
Jon Wayne Parrott421cc9b2016-10-26 21:05:16 -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 os
16
17import py
18
19import google.auth
20from google.auth import environment_vars
21import google.oauth2.credentials
22from google.oauth2 import service_account
23
24
25def 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
35def 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
48def 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
61def 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
74def 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
89def 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
108def 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)