blob: 6e92ca6455dbfd3610493809916b688c9d7bef51 [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
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070015import io
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070016import json
17import os
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070018import subprocess
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070019
20import mock
21import pytest
22
23from google.auth import _cloud_sdk
24from google.auth import environment_vars
25import google.oauth2.credentials
26
27
28DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
29AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json')
30
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070031with io.open(AUTHORIZED_USER_FILE) as fh:
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070032 AUTHORIZED_USER_FILE_DATA = json.load(fh)
33
34SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json')
35
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070036with io.open(SERVICE_ACCOUNT_FILE) as fh:
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070037 SERVICE_ACCOUNT_FILE_DATA = json.load(fh)
38
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070039with io.open(os.path.join(DATA_DIR, 'cloud_sdk_config.json'), 'rb') as fh:
40 CLOUD_SDK_CONFIG_FILE_DATA = fh.read()
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070041
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070042
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070043@pytest.mark.parametrize('data, expected_project_id', [
44 (CLOUD_SDK_CONFIG_FILE_DATA, 'example-project'),
45 (b'I am some bad json', None),
46 (b'{}', None)
47])
48def test_get_project_id(data, expected_project_id):
49 check_output_patch = mock.patch(
50 'subprocess.check_output', autospec=True, return_value=data)
51
52 with check_output_patch as check_output:
53 project_id = _cloud_sdk.get_project_id()
54
55 assert project_id == expected_project_id
56 assert check_output.called
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070057
58
59@mock.patch(
60 'subprocess.check_output', autospec=True,
61 side_effect=subprocess.CalledProcessError(-1, None))
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070062def test_get_project_id_call_error(check_output):
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070063 project_id = _cloud_sdk.get_project_id()
64 assert project_id is None
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070065 assert check_output.called
Jon Wayne Parrott0c09c732017-03-24 12:10:44 -070066
67
68@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -080069 'google.auth._cloud_sdk.get_config_path', autospec=True)
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070070def test_get_application_default_credentials_path(get_config_dir):
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070071 config_path = 'config_path'
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070072 get_config_dir.return_value = config_path
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070073 credentials_path = _cloud_sdk.get_application_default_credentials_path()
74 assert credentials_path == os.path.join(
75 config_path, _cloud_sdk._CREDENTIALS_FILENAME)
76
77
78def test_get_config_path_env_var(monkeypatch):
79 config_path_sentinel = 'config_path'
80 monkeypatch.setenv(
81 environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
82 config_path = _cloud_sdk.get_config_path()
83 assert config_path == config_path_sentinel
84
85
86@mock.patch('os.path.expanduser')
Jon Wayne Parrott78fec2c2017-06-30 10:25:08 -070087def test_get_config_path_unix(expanduser):
88 expanduser.side_effect = lambda path: path
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070089
90 config_path = _cloud_sdk.get_config_path()
91
92 assert os.path.split(config_path) == (
93 '~/.config', _cloud_sdk._CONFIG_DIRECTORY)
94
95
96@mock.patch('os.name', new='nt')
97def test_get_config_path_windows(monkeypatch):
98 appdata = 'appdata'
99 monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)
100
101 config_path = _cloud_sdk.get_config_path()
102
103 assert os.path.split(config_path) == (
104 appdata, _cloud_sdk._CONFIG_DIRECTORY)
105
106
107@mock.patch('os.name', new='nt')
108def test_get_config_path_no_appdata(monkeypatch):
109 monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
110 monkeypatch.setenv('SystemDrive', 'G:')
111
112 config_path = _cloud_sdk.get_config_path()
113
114 assert os.path.split(config_path) == (
115 'G:/\\', _cloud_sdk._CONFIG_DIRECTORY)
116
117
118def test_load_authorized_user_credentials():
119 credentials = _cloud_sdk.load_authorized_user_credentials(
120 AUTHORIZED_USER_FILE_DATA)
121
122 assert isinstance(credentials, google.oauth2.credentials.Credentials)
123
124 assert credentials.token is None
125 assert (credentials._refresh_token ==
126 AUTHORIZED_USER_FILE_DATA['refresh_token'])
127 assert credentials._client_id == AUTHORIZED_USER_FILE_DATA['client_id']
128 assert (credentials._client_secret ==
129 AUTHORIZED_USER_FILE_DATA['client_secret'])
130 assert credentials._token_uri == _cloud_sdk._GOOGLE_OAUTH2_TOKEN_ENDPOINT
131
132
133def test_load_authorized_user_credentials_bad_format():
134 with pytest.raises(ValueError) as excinfo:
135 _cloud_sdk.load_authorized_user_credentials({})
136
137 assert excinfo.match(r'missing fields')