blob: 482a7ed51a815de8fc614d621d08e7f0345c41b0 [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
43@mock.patch(
44 'subprocess.check_output', autospec=True,
45 return_value=CLOUD_SDK_CONFIG_FILE_DATA)
46def test_get_project_id(check_output_mock):
47 project_id = _cloud_sdk.get_project_id()
48 assert project_id == 'example-project'
49
50
51@mock.patch(
52 'subprocess.check_output', autospec=True,
53 side_effect=subprocess.CalledProcessError(-1, None))
54def test_get_project_id_call_error(check_output_mock):
55 project_id = _cloud_sdk.get_project_id()
56 assert project_id is None
57
58
59@mock.patch(
60 'subprocess.check_output', autospec=True,
61 return_value=b'I am some bad json')
62def test_get_project_id_bad_json(check_output_mock):
63 project_id = _cloud_sdk.get_project_id()
64 assert project_id is None
65
66
67@mock.patch(
68 'subprocess.check_output', autospec=True,
69 return_value=b'{}')
70def test_get_project_id_missing_value(check_output_mock):
71 project_id = _cloud_sdk.get_project_id()
72 assert project_id is None
73
74
75@mock.patch(
Jon Wayne Parrott8784b232016-11-10 12:53:55 -080076 'google.auth._cloud_sdk.get_config_path', autospec=True)
Jon Wayne Parrottaadb3de2016-10-19 09:34:05 -070077def test_get_application_default_credentials_path(mock_get_config_dir):
78 config_path = 'config_path'
79 mock_get_config_dir.return_value = config_path
80 credentials_path = _cloud_sdk.get_application_default_credentials_path()
81 assert credentials_path == os.path.join(
82 config_path, _cloud_sdk._CREDENTIALS_FILENAME)
83
84
85def test_get_config_path_env_var(monkeypatch):
86 config_path_sentinel = 'config_path'
87 monkeypatch.setenv(
88 environment_vars.CLOUD_SDK_CONFIG_DIR, config_path_sentinel)
89 config_path = _cloud_sdk.get_config_path()
90 assert config_path == config_path_sentinel
91
92
93@mock.patch('os.path.expanduser')
94def test_get_config_path_unix(mock_expanduser):
95 mock_expanduser.side_effect = lambda path: path
96
97 config_path = _cloud_sdk.get_config_path()
98
99 assert os.path.split(config_path) == (
100 '~/.config', _cloud_sdk._CONFIG_DIRECTORY)
101
102
103@mock.patch('os.name', new='nt')
104def test_get_config_path_windows(monkeypatch):
105 appdata = 'appdata'
106 monkeypatch.setenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, appdata)
107
108 config_path = _cloud_sdk.get_config_path()
109
110 assert os.path.split(config_path) == (
111 appdata, _cloud_sdk._CONFIG_DIRECTORY)
112
113
114@mock.patch('os.name', new='nt')
115def test_get_config_path_no_appdata(monkeypatch):
116 monkeypatch.delenv(_cloud_sdk._WINDOWS_CONFIG_ROOT_ENV_VAR, raising=False)
117 monkeypatch.setenv('SystemDrive', 'G:')
118
119 config_path = _cloud_sdk.get_config_path()
120
121 assert os.path.split(config_path) == (
122 'G:/\\', _cloud_sdk._CONFIG_DIRECTORY)
123
124
125def test_load_authorized_user_credentials():
126 credentials = _cloud_sdk.load_authorized_user_credentials(
127 AUTHORIZED_USER_FILE_DATA)
128
129 assert isinstance(credentials, google.oauth2.credentials.Credentials)
130
131 assert credentials.token is None
132 assert (credentials._refresh_token ==
133 AUTHORIZED_USER_FILE_DATA['refresh_token'])
134 assert credentials._client_id == AUTHORIZED_USER_FILE_DATA['client_id']
135 assert (credentials._client_secret ==
136 AUTHORIZED_USER_FILE_DATA['client_secret'])
137 assert credentials._token_uri == _cloud_sdk._GOOGLE_OAUTH2_TOKEN_ENDPOINT
138
139
140def test_load_authorized_user_credentials_bad_format():
141 with pytest.raises(ValueError) as excinfo:
142 _cloud_sdk.load_authorized_user_credentials({})
143
144 assert excinfo.match(r'missing fields')