|
| 1 | +# Copyright 2017 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 | + |
| 15 | +import io |
| 16 | +import json |
| 17 | +import os.path |
| 18 | +import tempfile |
| 19 | + |
| 20 | +import click.testing |
| 21 | +import google.oauth2.credentials |
| 22 | +import mock |
| 23 | +import pytest |
| 24 | + |
| 25 | +import google_auth_oauthlib.flow |
| 26 | +import google_auth_oauthlib.tool.__main__ as cli |
| 27 | + |
| 28 | +DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 29 | +CLIENT_SECRETS_FILE = os.path.join(DATA_DIR, 'client_secrets.json') |
| 30 | + |
| 31 | + |
| 32 | +class TestMain(object): |
| 33 | + @pytest.fixture |
| 34 | + def runner(self): |
| 35 | + return click.testing.CliRunner() |
| 36 | + |
| 37 | + @pytest.fixture |
| 38 | + def dummy_credentials(self): |
| 39 | + return google.oauth2.credentials.Credentials( |
| 40 | + token='dummy_access_token', |
| 41 | + refresh_token='dummy_refresh_token', |
| 42 | + token_uri='dummy_token_uri', |
| 43 | + client_id='dummy_client_id', |
| 44 | + client_secret='dummy_client_secret', |
| 45 | + scopes=['dummy_scope1', 'dummy_scope2'] |
| 46 | + ) |
| 47 | + |
| 48 | + @pytest.fixture |
| 49 | + def local_server_mock(self, dummy_credentials): |
| 50 | + run_local_server_patch = mock.patch.object( |
| 51 | + google_auth_oauthlib.flow.InstalledAppFlow, |
| 52 | + 'run_local_server', |
| 53 | + autospec=True) |
| 54 | + |
| 55 | + with run_local_server_patch as flow: |
| 56 | + flow.return_value = dummy_credentials |
| 57 | + yield flow |
| 58 | + |
| 59 | + @pytest.fixture |
| 60 | + def console_mock(self, dummy_credentials): |
| 61 | + run_console_patch = mock.patch.object( |
| 62 | + google_auth_oauthlib.flow.InstalledAppFlow, |
| 63 | + 'run_console', |
| 64 | + autospec=True) |
| 65 | + |
| 66 | + with run_console_patch as flow: |
| 67 | + flow.return_value = dummy_credentials |
| 68 | + yield flow |
| 69 | + |
| 70 | + def test_help(self, runner): |
| 71 | + result = runner.invoke(cli.main, ['--help']) |
| 72 | + assert not result.exception |
| 73 | + assert 'RFC6749' in result.output |
| 74 | + assert 'OAuth 2.0 authorization flow' in result.output |
| 75 | + assert 'not intended for production use' in result.output |
| 76 | + assert result.exit_code == 0 |
| 77 | + |
| 78 | + def test_defaults(self, runner, dummy_credentials, local_server_mock): |
| 79 | + result = runner.invoke(cli.main, [ |
| 80 | + '--client-secrets', CLIENT_SECRETS_FILE, |
| 81 | + '--scope', 'somescope', |
| 82 | + ]) |
| 83 | + local_server_mock.assert_called_with(mock.ANY) |
| 84 | + assert not result.exception |
| 85 | + assert result.exit_code == 0 |
| 86 | + creds_data = json.loads(result.output) |
| 87 | + creds = google.oauth2.credentials.Credentials(**creds_data) |
| 88 | + assert creds.token == dummy_credentials.token |
| 89 | + assert creds.refresh_token == dummy_credentials.refresh_token |
| 90 | + assert creds.token_uri == dummy_credentials.token_uri |
| 91 | + assert creds.client_id == dummy_credentials.client_id |
| 92 | + assert creds.client_secret == dummy_credentials.client_secret |
| 93 | + assert creds.scopes == dummy_credentials.scopes |
| 94 | + |
| 95 | + def test_headless(self, runner, dummy_credentials, console_mock): |
| 96 | + result = runner.invoke(cli.main, [ |
| 97 | + '--client-secrets', CLIENT_SECRETS_FILE, |
| 98 | + '--scope', 'somescope', |
| 99 | + '--headless' |
| 100 | + ]) |
| 101 | + console_mock.assert_called_with(mock.ANY) |
| 102 | + assert not result.exception |
| 103 | + assert dummy_credentials.refresh_token in result.output |
| 104 | + assert result.exit_code == 0 |
| 105 | + |
| 106 | + def test_save_new_dir(self, runner, dummy_credentials, local_server_mock): |
| 107 | + credentials_tmpdir = tempfile.mkdtemp() |
| 108 | + credentials_path = os.path.join( |
| 109 | + credentials_tmpdir, |
| 110 | + 'new-directory', |
| 111 | + 'credentials.json' |
| 112 | + ) |
| 113 | + result = runner.invoke(cli.main, [ |
| 114 | + '--client-secrets', CLIENT_SECRETS_FILE, |
| 115 | + '--scope', 'somescope', |
| 116 | + '--credentials', credentials_path, |
| 117 | + '--save' |
| 118 | + ]) |
| 119 | + local_server_mock.assert_called_with(mock.ANY) |
| 120 | + assert not result.exception |
| 121 | + assert 'saved' in result.output |
| 122 | + assert result.exit_code == 0 |
| 123 | + with io.open(credentials_path) as f: # pylint: disable=invalid-name |
| 124 | + creds_data = json.load(f) |
| 125 | + assert 'access_token' not in creds_data |
| 126 | + |
| 127 | + creds = google.oauth2.credentials.Credentials( |
| 128 | + token=None, **creds_data) |
| 129 | + assert creds.token is None |
| 130 | + assert creds.refresh_token == dummy_credentials.refresh_token |
| 131 | + assert creds.token_uri == dummy_credentials.token_uri |
| 132 | + assert creds.client_id == dummy_credentials.client_id |
| 133 | + assert creds.client_secret == dummy_credentials.client_secret |
| 134 | + |
| 135 | + def test_save_existing_dir(self, runner, local_server_mock): |
| 136 | + credentials_tmpdir = tempfile.mkdtemp() |
| 137 | + result = runner.invoke(cli.main, [ |
| 138 | + '--client-secrets', CLIENT_SECRETS_FILE, |
| 139 | + '--scope', 'somescope', |
| 140 | + '--credentials', os.path.join( |
| 141 | + credentials_tmpdir, |
| 142 | + 'credentials.json' |
| 143 | + ), |
| 144 | + '--save' |
| 145 | + ]) |
| 146 | + local_server_mock.assert_called_with(mock.ANY) |
| 147 | + assert not result.exception |
| 148 | + assert 'saved' in result.output |
| 149 | + assert result.exit_code == 0 |
0 commit comments