File size: 4,879 Bytes
065fee7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import io
import json
import os.path
import tempfile
import click.testing
import google.oauth2.credentials
import mock
import pytest
import google_auth_oauthlib.flow
import google_auth_oauthlib.tool.__main__ as cli
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
CLIENT_SECRETS_FILE = os.path.join(DATA_DIR, "client_secrets.json")
class TestMain(object):
@pytest.fixture
def runner(self):
return click.testing.CliRunner()
@pytest.fixture
def dummy_credentials(self):
return google.oauth2.credentials.Credentials(
token="dummy_access_token",
refresh_token="dummy_refresh_token",
token_uri="dummy_token_uri",
client_id="dummy_client_id",
client_secret="dummy_client_secret",
scopes=["dummy_scope1", "dummy_scope2"],
)
@pytest.fixture
def local_server_mock(self, dummy_credentials):
run_local_server_patch = mock.patch.object(
google_auth_oauthlib.flow.InstalledAppFlow,
"run_local_server",
autospec=True,
)
with run_local_server_patch as flow:
flow.return_value = dummy_credentials
yield flow
def test_help(self, runner):
result = runner.invoke(cli.main, ["--help"])
assert not result.exception
assert "RFC6749" in result.output
assert "OAuth 2.0 authorization flow" in result.output
assert "not intended for production use" in result.output
assert result.exit_code == 0
def test_defaults(self, runner, dummy_credentials, local_server_mock):
result = runner.invoke(
cli.main, ["--client-secrets", CLIENT_SECRETS_FILE, "--scope", "somescope"]
)
local_server_mock.assert_called_with(mock.ANY)
assert not result.exception
assert result.exit_code == 0
creds_data = json.loads(result.output)
creds = google.oauth2.credentials.Credentials(**creds_data)
assert creds.token == dummy_credentials.token
assert creds.refresh_token == dummy_credentials.refresh_token
assert creds.token_uri == dummy_credentials.token_uri
assert creds.client_id == dummy_credentials.client_id
assert creds.client_secret == dummy_credentials.client_secret
assert creds.scopes == dummy_credentials.scopes
def test_save_new_dir(self, runner, dummy_credentials, local_server_mock):
credentials_tmpdir = tempfile.mkdtemp()
credentials_path = os.path.join(
credentials_tmpdir, "new-directory", "credentials.json"
)
result = runner.invoke(
cli.main,
[
"--client-secrets",
CLIENT_SECRETS_FILE,
"--scope",
"somescope",
"--credentials",
credentials_path,
"--save",
],
)
local_server_mock.assert_called_with(mock.ANY)
assert not result.exception
assert "saved" in result.output
assert result.exit_code == 0
with io.open(credentials_path) as f: # pylint: disable=invalid-name
creds_data = json.load(f)
assert "access_token" not in creds_data
creds = google.oauth2.credentials.Credentials(token=None, **creds_data)
assert creds.token is None
assert creds.refresh_token == dummy_credentials.refresh_token
assert creds.token_uri == dummy_credentials.token_uri
assert creds.client_id == dummy_credentials.client_id
assert creds.client_secret == dummy_credentials.client_secret
def test_save_existing_dir(self, runner, local_server_mock):
credentials_tmpdir = tempfile.mkdtemp()
result = runner.invoke(
cli.main,
[
"--client-secrets",
CLIENT_SECRETS_FILE,
"--scope",
"somescope",
"--credentials",
os.path.join(credentials_tmpdir, "credentials.json"),
"--save",
],
)
local_server_mock.assert_called_with(mock.ANY)
assert not result.exception
assert "saved" in result.output
assert result.exit_code == 0
|