blob: 86f69a15fff966285a5d64373ff2b5df9e9dbe9b [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
41CONFIG_PATH_PATCH = mock.patch('google.auth._cloud_sdk.get_config_path')
42
43
44@pytest.fixture
Jon Wayne Parrott80f7ff22016-10-26 10:47:18 -070045def config_dir(tmpdir):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070046 config_dir = tmpdir.join(
47 '.config', _cloud_sdk._CONFIG_DIRECTORY)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070048
49 with CONFIG_PATH_PATCH as mock_get_config_dir:
50 mock_get_config_dir.return_value = str(config_dir)
Jon Wayne Parrott80f7ff22016-10-26 10:47:18 -070051 yield config_dir
52
53
54@pytest.fixture
55def config_file(config_dir):
56 config_file = py.path.local(_cloud_sdk._get_config_file(
57 str(config_dir), 'default'))
58 yield config_file
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070059
60
61def test_get_project_id(config_file):
62 config_file.write(CLOUD_SDK_CONFIG_DATA, ensure=True)
63 project_id = _cloud_sdk.get_project_id()
64 assert project_id == 'example-project'
65
66
67def test_get_project_id_non_existent(config_file):
68 project_id = _cloud_sdk.get_project_id()
69 assert project_id is None
70
71
72def test_get_project_id_bad_file(config_file):
73 config_file.write('<<<badconfig', ensure=True)
74 project_id = _cloud_sdk.get_project_id()
75 assert project_id is None
76
77
78def test_get_project_id_no_section(config_file):
79 config_file.write('[section]', ensure=True)
80 project_id = _cloud_sdk.get_project_id()
81 assert project_id is None
82
83
Jon Wayne Parrott80f7ff22016-10-26 10:47:18 -070084def test_get_project_id_non_default_config(config_dir):
85 active_config = config_dir.join('active_config')
86 test_config = py.path.local(_cloud_sdk._get_config_file(
87 str(config_dir), 'test'))
88
89 # Create an active config file that points to the 'test' config.
90 active_config.write('test', ensure=True)
91 test_config.write(CLOUD_SDK_CONFIG_DATA, ensure=True)
92
93 project_id = _cloud_sdk.get_project_id()
94
95 assert project_id == 'example-project'
96
97
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070098@CONFIG_PATH_PATCH
99def test_get_application_default_credentials_path(mock_get_config_dir):
100 config_path = 'config_path'
101 mock_get_config_dir.return_value = config_path
102 credentials_path = _cloud_sdk.get_application_default_credentials_path()
103 assert credentials_path == os.path.join(
104 config_path, _cloud_sdk._CREDENTIALS_FILENAME)
105
106
107def test_get_config_path_env_var(monkeypatch):
108 config_path_sentinel = 'config_path'
109 monkeypatch.setenv(
110 environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
111 config_path = _cloud_sdk.get_config_path()
112 assert config_path == config_path_sentinel
113
114
115@mock.patch('os.path.expanduser')
116def test_get_config_path_unix(mock_expanduser):
117 mock_expanduser.side_effect = lambda path: path
118
119 config_path = _cloud_sdk.get_config_path()
120
121 assert os.path.split(config_path) == (
122 '~/.config', _cloud_sdk._CONFIG_DIRECTORY)
123
124
125@mock.patch('os.name', new='nt')
126def test_get_config_path_windows(monkeypatch):
127 appdata = 'appdata'
128 monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)
129
130 config_path = _cloud_sdk.get_config_path()
131
132 assert os.path.split(config_path) == (
133 appdata, _cloud_sdk._CONFIG_DIRECTORY)
134
135
136@mock.patch('os.name', new='nt')
137def test_get_config_path_no_appdata(monkeypatch):
138 monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
139 monkeypatch.setenv('SystemDrive', 'G:')
140
141 config_path = _cloud_sdk.get_config_path()
142
143 assert os.path.split(config_path) == (
144 'G:/\\', _cloud_sdk._CONFIG_DIRECTORY)
145
146
147def test_load_authorized_user_credentials():
148 credentials = _cloud_sdk.load_authorized_user_credentials(
149 AUTHORIZED_USER_FILE_DATA)
150
151 assert isinstance(credentials, google.oauth2.credentials.Credentials)
152
153 assert credentials.token is None
154 assert (credentials._refresh_token ==
155 AUTHORIZED_USER_FILE_DATA['refresh_token'])
156 assert credentials._client_id == AUTHORIZED_USER_FILE_DATA['client_id']
157 assert (credentials._client_secret ==
158 AUTHORIZED_USER_FILE_DATA['client_secret'])
159 assert credentials._token_uri == _cloud_sdk._GOOGLE_OAUTH2_TOKEN_ENDPOINT
160
161
162def test_load_authorized_user_credentials_bad_format():
163 with pytest.raises(ValueError) as excinfo:
164 _cloud_sdk.load_authorized_user_credentials({})
165
166 assert excinfo.match(r'missing fields')