blob: 35ee4269b5d484376a241742cb7f6ed7052fd204 [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
19import pytest
20
21from google.auth import _cloud_sdk
22from google.auth import environment_vars
23import google.oauth2.credentials
24
25
26DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
27AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json')
28
29with open(AUTHORIZED_USER_FILE) as fh:
30 AUTHORIZED_USER_FILE_DATA = json.load(fh)
31
32SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json')
33
34with open(SERVICE_ACCOUNT_FILE) as fh:
35 SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
36
37with open(os.path.join(DATA_DIR, 'cloud_sdk.cfg')) as fh:
38 CLOUD_SDK_CONFIG_DATA = fh.read()
39
40CONFIG_PATH_PATCH = mock.patch('google.auth._cloud_sdk.get_config_path')
41
42
43@pytest.fixture
44def config_file(tmpdir):
45 config_dir = tmpdir.join(
46 '.config', _cloud_sdk._CONFIG_DIRECTORY)
47 config_file = config_dir.join(
48 _cloud_sdk._ACTIVE_CONFIG_FILENAME)
49
50 with CONFIG_PATH_PATCH as mock_get_config_dir:
51 mock_get_config_dir.return_value = str(config_dir)
52 yield config_file
53
54
55def test_get_project_id(config_file):
56 config_file.write(CLOUD_SDK_CONFIG_DATA, ensure=True)
57 project_id = _cloud_sdk.get_project_id()
58 assert project_id == 'example-project'
59
60
61def test_get_project_id_non_existent(config_file):
62 project_id = _cloud_sdk.get_project_id()
63 assert project_id is None
64
65
66def test_get_project_id_bad_file(config_file):
67 config_file.write('<<<badconfig', ensure=True)
68 project_id = _cloud_sdk.get_project_id()
69 assert project_id is None
70
71
72def test_get_project_id_no_section(config_file):
73 config_file.write('[section]', ensure=True)
74 project_id = _cloud_sdk.get_project_id()
75 assert project_id is None
76
77
78@CONFIG_PATH_PATCH
79def test_get_application_default_credentials_path(mock_get_config_dir):
80 config_path = 'config_path'
81 mock_get_config_dir.return_value = config_path
82 credentials_path = _cloud_sdk.get_application_default_credentials_path()
83 assert credentials_path == os.path.join(
84 config_path, _cloud_sdk._CREDENTIALS_FILENAME)
85
86
87def test_get_config_path_env_var(monkeypatch):
88 config_path_sentinel = 'config_path'
89 monkeypatch.setenv(
90 environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
91 config_path = _cloud_sdk.get_config_path()
92 assert config_path == config_path_sentinel
93
94
95@mock.patch('os.path.expanduser')
96def test_get_config_path_unix(mock_expanduser):
97 mock_expanduser.side_effect = lambda path: path
98
99 config_path = _cloud_sdk.get_config_path()
100
101 assert os.path.split(config_path) == (
102 '~/.config', _cloud_sdk._CONFIG_DIRECTORY)
103
104
105@mock.patch('os.name', new='nt')
106def test_get_config_path_windows(monkeypatch):
107 appdata = 'appdata'
108 monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)
109
110 config_path = _cloud_sdk.get_config_path()
111
112 assert os.path.split(config_path) == (
113 appdata, _cloud_sdk._CONFIG_DIRECTORY)
114
115
116@mock.patch('os.name', new='nt')
117def test_get_config_path_no_appdata(monkeypatch):
118 monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
119 monkeypatch.setenv('SystemDrive', 'G:')
120
121 config_path = _cloud_sdk.get_config_path()
122
123 assert os.path.split(config_path) == (
124 'G:/\\', _cloud_sdk._CONFIG_DIRECTORY)
125
126
127def test_load_authorized_user_credentials():
128 credentials = _cloud_sdk.load_authorized_user_credentials(
129 AUTHORIZED_USER_FILE_DATA)
130
131 assert isinstance(credentials, google.oauth2.credentials.Credentials)
132
133 assert credentials.token is None
134 assert (credentials._refresh_token ==
135 AUTHORIZED_USER_FILE_DATA['refresh_token'])
136 assert credentials._client_id == AUTHORIZED_USER_FILE_DATA['client_id']
137 assert (credentials._client_secret ==
138 AUTHORIZED_USER_FILE_DATA['client_secret'])
139 assert credentials._token_uri == _cloud_sdk._GOOGLE_OAUTH2_TOKEN_ENDPOINT
140
141
142def test_load_authorized_user_credentials_bad_format():
143 with pytest.raises(ValueError) as excinfo:
144 _cloud_sdk.load_authorized_user_credentials({})
145
146 assert excinfo.match(r'missing fields')