blob: ba72072d43c74c378310945b301124f4bdfd42df [file] [log] [blame]
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -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
18import mock
Jon Wayne Parrott80f7ff22016-10-26 10:47:18 -070019import py
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070020import pytest
21
22from google.auth import _cloud_sdk
23from google.auth import environment_vars
24import google.oauth2.credentials
25
26
27DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
28AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json')
29
30with open(AUTHORIZED_USER_FILE) as fh:
31 AUTHORIZED_USER_FILE_DATA = json.load(fh)
32
33SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json')
34
35with open(SERVICE_ACCOUNT_FILE) as fh:
36 SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
37
38with open(os.path.join(DATA_DIR, 'cloud_sdk.cfg')) as fh:
39 CLOUD_SDK_CONFIG_DATA = fh.read()
40
Jon Wayne Parrott8784b232016-11-10 12:53:55 -080041CONFIG_PATH_PATCH = mock.patch(
42 'google.auth._cloud_sdk.get_config_path', autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070043
44
45@pytest.fixture
Jon Wayne Parrott80f7ff22016-10-26 10:47:18 -070046def config_dir(tmpdir):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070047 config_dir = tmpdir.join(
48 '.config', _cloud_sdk._CONFIG_DIRECTORY)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070049
50 with CONFIG_PATH_PATCH as mock_get_config_dir:
51 mock_get_config_dir.return_value = str(config_dir)
Jon Wayne Parrott80f7ff22016-10-26 10:47:18 -070052 yield config_dir
53
54
55@pytest.fixture
56def config_file(config_dir):
57 config_file = py.path.local(_cloud_sdk._get_config_file(
58 str(config_dir), 'default'))
59 yield config_file
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070060
61
62def test_get_project_id(config_file):
63 config_file.write(CLOUD_SDK_CONFIG_DATA, ensure=True)
64 project_id = _cloud_sdk.get_project_id()
65 assert project_id == 'example-project'
66
67
68def test_get_project_id_non_existent(config_file):
69 project_id = _cloud_sdk.get_project_id()
70 assert project_id is None
71
72
73def test_get_project_id_bad_file(config_file):
74 config_file.write('<<<badconfig', ensure=True)
75 project_id = _cloud_sdk.get_project_id()
76 assert project_id is None
77
78
79def test_get_project_id_no_section(config_file):
80 config_file.write('[section]', ensure=True)
81 project_id = _cloud_sdk.get_project_id()
82 assert project_id is None
83
84
Jon Wayne Parrott80f7ff22016-10-26 10:47:18 -070085def test_get_project_id_non_default_config(config_dir):
86 active_config = config_dir.join('active_config')
87 test_config = py.path.local(_cloud_sdk._get_config_file(
88 str(config_dir), 'test'))
89
90 # Create an active config file that points to the 'test' config.
91 active_config.write('test', ensure=True)
92 test_config.write(CLOUD_SDK_CONFIG_DATA, ensure=True)
93
94 project_id = _cloud_sdk.get_project_id()
95
96 assert project_id == 'example-project'
97
98
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070099@CONFIG_PATH_PATCH
100def test_get_application_default_credentials_path(mock_get_config_dir):
101 config_path = 'config_path'
102 mock_get_config_dir.return_value = config_path
103 credentials_path = _cloud_sdk.get_application_default_credentials_path()
104 assert credentials_path == os.path.join(
105 config_path, _cloud_sdk._CREDENTIALS_FILENAME)
106
107
108def test_get_config_path_env_var(monkeypatch):
109 config_path_sentinel = 'config_path'
110 monkeypatch.setenv(
111 environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
112 config_path = _cloud_sdk.get_config_path()
113 assert config_path == config_path_sentinel
114
115
116@mock.patch('os.path.expanduser')
117def test_get_config_path_unix(mock_expanduser):
118 mock_expanduser.side_effect = lambda path: path
119
120 config_path = _cloud_sdk.get_config_path()
121
122 assert os.path.split(config_path) == (
123 '~/.config', _cloud_sdk._CONFIG_DIRECTORY)
124
125
126@mock.patch('os.name', new='nt')
127def test_get_config_path_windows(monkeypatch):
128 appdata = 'appdata'
129 monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)
130
131 config_path = _cloud_sdk.get_config_path()
132
133 assert os.path.split(config_path) == (
134 appdata, _cloud_sdk._CONFIG_DIRECTORY)
135
136
137@mock.patch('os.name', new='nt')
138def test_get_config_path_no_appdata(monkeypatch):
139 monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
140 monkeypatch.setenv('SystemDrive', 'G:')
141
142 config_path = _cloud_sdk.get_config_path()
143
144 assert os.path.split(config_path) == (
145 'G:/\\', _cloud_sdk._CONFIG_DIRECTORY)
146
147
148def test_load_authorized_user_credentials():
149 credentials = _cloud_sdk.load_authorized_user_credentials(
150 AUTHORIZED_USER_FILE_DATA)
151
152 assert isinstance(credentials, google.oauth2.credentials.Credentials)
153
154 assert credentials.token is None
155 assert (credentials._refresh_token ==
156 AUTHORIZED_USER_FILE_DATA['refresh_token'])
157 assert credentials._client_id == AUTHORIZED_USER_FILE_DATA['client_id']
158 assert (credentials._client_secret ==
159 AUTHORIZED_USER_FILE_DATA['client_secret'])
160 assert credentials._token_uri == _cloud_sdk._GOOGLE_OAUTH2_TOKEN_ENDPOINT
161
162
163def test_load_authorized_user_credentials_bad_format():
164 with pytest.raises(ValueError) as excinfo:
165 _cloud_sdk.load_authorized_user_credentials({})
166
167 assert excinfo.match(r'missing fields')