|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import io |
15 | 16 | import json |
16 | 17 | import os |
| 18 | +import subprocess |
17 | 19 |
|
18 | 20 | import mock |
19 | | -import py |
20 | 21 | import pytest |
21 | 22 |
|
22 | 23 | from google.auth import _cloud_sdk |
|
27 | 28 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
28 | 29 | AUTHORIZED_USER_FILE = os.path.join(DATA_DIR, 'authorized_user.json') |
29 | 30 |
|
30 | | -with open(AUTHORIZED_USER_FILE) as fh: |
| 31 | +with io.open(AUTHORIZED_USER_FILE) as fh: |
31 | 32 | AUTHORIZED_USER_FILE_DATA = json.load(fh) |
32 | 33 |
|
33 | 34 | SERVICE_ACCOUNT_FILE = os.path.join(DATA_DIR, 'service_account.json') |
34 | 35 |
|
35 | | -with open(SERVICE_ACCOUNT_FILE) as fh: |
| 36 | +with io.open(SERVICE_ACCOUNT_FILE) as fh: |
36 | 37 | SERVICE_ACCOUNT_FILE_DATA = json.load(fh) |
37 | 38 |
|
38 | | -with open(os.path.join(DATA_DIR, 'cloud_sdk.cfg')) as fh: |
39 | | - CLOUD_SDK_CONFIG_DATA = fh.read() |
| 39 | +with io.open(os.path.join(DATA_DIR, 'cloud_sdk_config.json'), 'rb') as fh: |
| 40 | + CLOUD_SDK_CONFIG_FILE_DATA = fh.read() |
40 | 41 |
|
41 | | -CONFIG_PATH_PATCH = mock.patch( |
42 | | - 'google.auth._cloud_sdk.get_config_path', autospec=True) |
43 | | - |
44 | | - |
45 | | -@pytest.fixture |
46 | | -def config_dir(tmpdir): |
47 | | - config_dir = tmpdir.join( |
48 | | - '.config', _cloud_sdk._CONFIG_DIRECTORY) |
49 | | - |
50 | | - with CONFIG_PATH_PATCH as mock_get_config_dir: |
51 | | - mock_get_config_dir.return_value = str(config_dir) |
52 | | - yield config_dir |
53 | 42 |
|
54 | | - |
55 | | -@pytest.fixture |
56 | | -def config_file(config_dir): |
57 | | - config_file = py.path.local(_cloud_sdk._get_config_file( |
58 | | - str(config_dir), 'default')) |
59 | | - yield config_file |
60 | | - |
61 | | - |
62 | | -def test_get_project_id(config_file): |
63 | | - config_file.write(CLOUD_SDK_CONFIG_DATA, ensure=True) |
| 43 | +@mock.patch( |
| 44 | + 'subprocess.check_output', autospec=True, |
| 45 | + return_value=CLOUD_SDK_CONFIG_FILE_DATA) |
| 46 | +def test_get_project_id(check_output_mock): |
64 | 47 | project_id = _cloud_sdk.get_project_id() |
65 | 48 | assert project_id == 'example-project' |
66 | 49 |
|
67 | 50 |
|
68 | | -def test_get_project_id_non_existent(config_file): |
| 51 | +@mock.patch( |
| 52 | + 'subprocess.check_output', autospec=True, |
| 53 | + side_effect=subprocess.CalledProcessError(-1, None)) |
| 54 | +def test_get_project_id_call_error(check_output_mock): |
69 | 55 | project_id = _cloud_sdk.get_project_id() |
70 | 56 | assert project_id is None |
71 | 57 |
|
72 | 58 |
|
73 | | -def test_get_project_id_bad_file(config_file): |
74 | | - config_file.write('<<<badconfig', ensure=True) |
| 59 | +@mock.patch( |
| 60 | + 'subprocess.check_output', autospec=True, |
| 61 | + return_value=b'I am some bad json') |
| 62 | +def test_get_project_id_bad_json(check_output_mock): |
75 | 63 | project_id = _cloud_sdk.get_project_id() |
76 | 64 | assert project_id is None |
77 | 65 |
|
78 | 66 |
|
79 | | -def test_get_project_id_no_section(config_file): |
80 | | - config_file.write('[section]', ensure=True) |
| 67 | +@mock.patch( |
| 68 | + 'subprocess.check_output', autospec=True, |
| 69 | + return_value=b'{}') |
| 70 | +def test_get_project_id_missing_value(check_output_mock): |
81 | 71 | project_id = _cloud_sdk.get_project_id() |
82 | 72 | assert project_id is None |
83 | 73 |
|
84 | 74 |
|
85 | | -def 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 | | - |
99 | | -@CONFIG_PATH_PATCH |
| 75 | +@mock.patch( |
| 76 | + 'google.auth._cloud_sdk.get_config_path', autospec=True) |
100 | 77 | def test_get_application_default_credentials_path(mock_get_config_dir): |
101 | 78 | config_path = 'config_path' |
102 | 79 | mock_get_config_dir.return_value = config_path |
|
0 commit comments